cmsasn1.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /*
  5. * CMS ASN.1 templates
  6. */
  7. #include "cmslocal.h"
  8. #include "cert.h"
  9. #include "keyhi.h"
  10. #include "secasn1.h"
  11. #include "secitem.h"
  12. #include "secoid.h"
  13. #include "prtime.h"
  14. #include "secerr.h"
  15. extern const SEC_ASN1Template nss_cms_set_of_attribute_template[];
  16. SEC_ASN1_MKSUB(CERT_IssuerAndSNTemplate)
  17. SEC_ASN1_MKSUB(CERT_SetOfSignedCrlTemplate)
  18. SEC_ASN1_MKSUB(SECOID_AlgorithmIDTemplate)
  19. SEC_ASN1_MKSUB(SEC_BitStringTemplate)
  20. SEC_ASN1_MKSUB(SEC_OctetStringTemplate)
  21. SEC_ASN1_MKSUB(SEC_PointerToOctetStringTemplate)
  22. SEC_ASN1_MKSUB(SEC_SetOfAnyTemplate)
  23. /* -----------------------------------------------------------------------------
  24. * MESSAGE
  25. * (uses NSSCMSContentInfo)
  26. */
  27. /* forward declaration */
  28. static const SEC_ASN1Template *
  29. nss_cms_choose_content_template(void *src_or_dest, PRBool encoding);
  30. static const SEC_ASN1TemplateChooserPtr nss_cms_chooser = nss_cms_choose_content_template;
  31. const SEC_ASN1Template NSSCMSMessageTemplate[] = {
  32. { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
  33. 0, NULL, sizeof(NSSCMSMessage) },
  34. { SEC_ASN1_OBJECT_ID,
  35. offsetof(NSSCMSMessage, contentInfo.contentType) },
  36. { SEC_ASN1_OPTIONAL | SEC_ASN1_DYNAMIC | SEC_ASN1_MAY_STREAM |
  37. SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
  38. offsetof(NSSCMSMessage, contentInfo.content),
  39. &nss_cms_chooser },
  40. { 0 }
  41. };
  42. /* -----------------------------------------------------------------------------
  43. * ENCAPSULATED & ENCRYPTED CONTENTINFO
  44. * (both use a NSSCMSContentInfo)
  45. */
  46. static const SEC_ASN1Template NSSCMSEncapsulatedContentInfoTemplate[] = {
  47. { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
  48. 0, NULL, sizeof(NSSCMSContentInfo) },
  49. { SEC_ASN1_OBJECT_ID,
  50. offsetof(NSSCMSContentInfo, contentType) },
  51. { SEC_ASN1_OPTIONAL | SEC_ASN1_EXPLICIT | SEC_ASN1_MAY_STREAM |
  52. SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0,
  53. offsetof(NSSCMSContentInfo, rawContent),
  54. SEC_ASN1_SUB(SEC_PointerToOctetStringTemplate) },
  55. { 0 }
  56. };
  57. static const SEC_ASN1Template NSSCMSEncryptedContentInfoTemplate[] = {
  58. { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
  59. 0, NULL, sizeof(NSSCMSContentInfo) },
  60. { SEC_ASN1_OBJECT_ID,
  61. offsetof(NSSCMSContentInfo, contentType) },
  62. { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
  63. offsetof(NSSCMSContentInfo, contentEncAlg),
  64. SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
  65. { SEC_ASN1_OPTIONAL | SEC_ASN1_POINTER | SEC_ASN1_MAY_STREAM |
  66. SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0,
  67. offsetof(NSSCMSContentInfo, rawContent),
  68. SEC_ASN1_SUB(SEC_OctetStringTemplate) },
  69. { 0 }
  70. };
  71. /* -----------------------------------------------------------------------------
  72. * SIGNED DATA
  73. */
  74. const SEC_ASN1Template NSSCMSSignerInfoTemplate[];
  75. const SEC_ASN1Template NSSCMSSignedDataTemplate[] = {
  76. { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
  77. 0, NULL, sizeof(NSSCMSSignedData) },
  78. { SEC_ASN1_INTEGER,
  79. offsetof(NSSCMSSignedData, version) },
  80. { SEC_ASN1_SET_OF | SEC_ASN1_XTRN,
  81. offsetof(NSSCMSSignedData, digestAlgorithms),
  82. SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
  83. { SEC_ASN1_INLINE,
  84. offsetof(NSSCMSSignedData, contentInfo),
  85. NSSCMSEncapsulatedContentInfoTemplate },
  86. { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC |
  87. SEC_ASN1_XTRN | 0,
  88. offsetof(NSSCMSSignedData, rawCerts),
  89. SEC_ASN1_SUB(SEC_SetOfAnyTemplate) },
  90. { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC |
  91. SEC_ASN1_XTRN | 1,
  92. offsetof(NSSCMSSignedData, crls),
  93. SEC_ASN1_SUB(CERT_SetOfSignedCrlTemplate) },
  94. { SEC_ASN1_SET_OF,
  95. offsetof(NSSCMSSignedData, signerInfos),
  96. NSSCMSSignerInfoTemplate },
  97. { 0 }
  98. };
  99. const SEC_ASN1Template NSS_PointerToCMSSignedDataTemplate[] = {
  100. { SEC_ASN1_POINTER, 0, NSSCMSSignedDataTemplate }
  101. };
  102. /* -----------------------------------------------------------------------------
  103. * signeridentifier
  104. */
  105. static const SEC_ASN1Template NSSCMSSignerIdentifierTemplate[] = {
  106. { SEC_ASN1_CHOICE,
  107. offsetof(NSSCMSSignerIdentifier, identifierType), NULL,
  108. sizeof(NSSCMSSignerIdentifier) },
  109. { SEC_ASN1_POINTER | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0,
  110. offsetof(NSSCMSSignerIdentifier, id.subjectKeyID),
  111. SEC_ASN1_SUB(SEC_OctetStringTemplate),
  112. NSSCMSRecipientID_SubjectKeyID },
  113. { SEC_ASN1_POINTER | SEC_ASN1_XTRN,
  114. offsetof(NSSCMSSignerIdentifier, id.issuerAndSN),
  115. SEC_ASN1_SUB(CERT_IssuerAndSNTemplate),
  116. NSSCMSRecipientID_IssuerSN },
  117. { 0 }
  118. };
  119. /* -----------------------------------------------------------------------------
  120. * signerinfo
  121. */
  122. const SEC_ASN1Template NSSCMSSignerInfoTemplate[] = {
  123. { SEC_ASN1_SEQUENCE,
  124. 0, NULL, sizeof(NSSCMSSignerInfo) },
  125. { SEC_ASN1_INTEGER,
  126. offsetof(NSSCMSSignerInfo, version) },
  127. { SEC_ASN1_INLINE,
  128. offsetof(NSSCMSSignerInfo, signerIdentifier),
  129. NSSCMSSignerIdentifierTemplate },
  130. { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
  131. offsetof(NSSCMSSignerInfo, digestAlg),
  132. SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
  133. { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
  134. offsetof(NSSCMSSignerInfo, authAttr),
  135. nss_cms_set_of_attribute_template },
  136. { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
  137. offsetof(NSSCMSSignerInfo, digestEncAlg),
  138. SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
  139. { SEC_ASN1_OCTET_STRING,
  140. offsetof(NSSCMSSignerInfo, encDigest) },
  141. { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
  142. offsetof(NSSCMSSignerInfo, unAuthAttr),
  143. nss_cms_set_of_attribute_template },
  144. { 0 }
  145. };
  146. /* -----------------------------------------------------------------------------
  147. * ENVELOPED DATA
  148. */
  149. static const SEC_ASN1Template NSSCMSOriginatorInfoTemplate[] = {
  150. { SEC_ASN1_SEQUENCE,
  151. 0, NULL, sizeof(NSSCMSOriginatorInfo) },
  152. { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC |
  153. SEC_ASN1_XTRN | 0,
  154. offsetof(NSSCMSOriginatorInfo, rawCerts),
  155. SEC_ASN1_SUB(SEC_SetOfAnyTemplate) },
  156. { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC |
  157. SEC_ASN1_XTRN | 1,
  158. offsetof(NSSCMSOriginatorInfo, crls),
  159. SEC_ASN1_SUB(CERT_SetOfSignedCrlTemplate) },
  160. { 0 }
  161. };
  162. const SEC_ASN1Template NSSCMSRecipientInfoTemplate[];
  163. const SEC_ASN1Template NSSCMSEnvelopedDataTemplate[] = {
  164. { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
  165. 0, NULL, sizeof(NSSCMSEnvelopedData) },
  166. { SEC_ASN1_INTEGER,
  167. offsetof(NSSCMSEnvelopedData, version) },
  168. { SEC_ASN1_OPTIONAL | SEC_ASN1_POINTER | SEC_ASN1_CONSTRUCTED |
  169. SEC_ASN1_CONTEXT_SPECIFIC | 0,
  170. offsetof(NSSCMSEnvelopedData, originatorInfo),
  171. NSSCMSOriginatorInfoTemplate },
  172. { SEC_ASN1_SET_OF,
  173. offsetof(NSSCMSEnvelopedData, recipientInfos),
  174. NSSCMSRecipientInfoTemplate },
  175. { SEC_ASN1_INLINE,
  176. offsetof(NSSCMSEnvelopedData, contentInfo),
  177. NSSCMSEncryptedContentInfoTemplate },
  178. { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
  179. offsetof(NSSCMSEnvelopedData, unprotectedAttr),
  180. nss_cms_set_of_attribute_template },
  181. { 0 }
  182. };
  183. const SEC_ASN1Template NSS_PointerToCMSEnvelopedDataTemplate[] = {
  184. { SEC_ASN1_POINTER, 0, NSSCMSEnvelopedDataTemplate }
  185. };
  186. /* here come the 15 gazillion templates for all the v3 varieties of RecipientInfo */
  187. /* -----------------------------------------------------------------------------
  188. * key transport recipient info
  189. */
  190. static const SEC_ASN1Template NSSCMSRecipientIdentifierTemplate[] = {
  191. { SEC_ASN1_CHOICE,
  192. offsetof(NSSCMSRecipientIdentifier, identifierType), NULL,
  193. sizeof(NSSCMSRecipientIdentifier) },
  194. { SEC_ASN1_POINTER | SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 0,
  195. offsetof(NSSCMSRecipientIdentifier, id.subjectKeyID),
  196. SEC_ASN1_SUB(SEC_OctetStringTemplate),
  197. NSSCMSRecipientID_SubjectKeyID },
  198. { SEC_ASN1_POINTER | SEC_ASN1_XTRN,
  199. offsetof(NSSCMSRecipientIdentifier, id.issuerAndSN),
  200. SEC_ASN1_SUB(CERT_IssuerAndSNTemplate),
  201. NSSCMSRecipientID_IssuerSN },
  202. { 0 }
  203. };
  204. static const SEC_ASN1Template NSSCMSKeyTransRecipientInfoTemplate[] = {
  205. { SEC_ASN1_SEQUENCE,
  206. 0, NULL, sizeof(NSSCMSKeyTransRecipientInfo) },
  207. { SEC_ASN1_INTEGER,
  208. offsetof(NSSCMSKeyTransRecipientInfo, version) },
  209. { SEC_ASN1_INLINE,
  210. offsetof(NSSCMSKeyTransRecipientInfo, recipientIdentifier),
  211. NSSCMSRecipientIdentifierTemplate },
  212. { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
  213. offsetof(NSSCMSKeyTransRecipientInfo, keyEncAlg),
  214. SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
  215. { SEC_ASN1_OCTET_STRING,
  216. offsetof(NSSCMSKeyTransRecipientInfo, encKey) },
  217. { 0 }
  218. };
  219. /* -----------------------------------------------------------------------------
  220. * key agreement recipient info
  221. */
  222. static const SEC_ASN1Template NSSCMSOriginatorPublicKeyTemplate[] = {
  223. { SEC_ASN1_SEQUENCE,
  224. 0, NULL, sizeof(NSSCMSOriginatorPublicKey) },
  225. { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
  226. offsetof(NSSCMSOriginatorPublicKey, algorithmIdentifier),
  227. SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
  228. { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
  229. offsetof(NSSCMSOriginatorPublicKey, publicKey),
  230. SEC_ASN1_SUB(SEC_BitStringTemplate) },
  231. { 0 }
  232. };
  233. static const SEC_ASN1Template NSSCMSOriginatorIdentifierOrKeyTemplate[] = {
  234. { SEC_ASN1_CHOICE,
  235. offsetof(NSSCMSOriginatorIdentifierOrKey, identifierType), NULL,
  236. sizeof(NSSCMSOriginatorIdentifierOrKey) },
  237. { SEC_ASN1_POINTER | SEC_ASN1_XTRN,
  238. offsetof(NSSCMSOriginatorIdentifierOrKey, id.issuerAndSN),
  239. SEC_ASN1_SUB(CERT_IssuerAndSNTemplate),
  240. NSSCMSOriginatorIDOrKey_IssuerSN },
  241. { SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC |
  242. SEC_ASN1_XTRN | 1,
  243. offsetof(NSSCMSOriginatorIdentifierOrKey, id.subjectKeyID),
  244. SEC_ASN1_SUB(SEC_PointerToOctetStringTemplate),
  245. NSSCMSOriginatorIDOrKey_SubjectKeyID },
  246. { SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 2,
  247. offsetof(NSSCMSOriginatorIdentifierOrKey, id.originatorPublicKey),
  248. NSSCMSOriginatorPublicKeyTemplate,
  249. NSSCMSOriginatorIDOrKey_OriginatorPublicKey },
  250. { 0 }
  251. };
  252. const SEC_ASN1Template NSSCMSRecipientKeyIdentifierTemplate[] = {
  253. { SEC_ASN1_SEQUENCE,
  254. 0, NULL, sizeof(NSSCMSRecipientKeyIdentifier) },
  255. { SEC_ASN1_OCTET_STRING,
  256. offsetof(NSSCMSRecipientKeyIdentifier, subjectKeyIdentifier) },
  257. { SEC_ASN1_OPTIONAL | SEC_ASN1_OCTET_STRING,
  258. offsetof(NSSCMSRecipientKeyIdentifier, date) },
  259. { SEC_ASN1_OPTIONAL | SEC_ASN1_OCTET_STRING,
  260. offsetof(NSSCMSRecipientKeyIdentifier, other) },
  261. { 0 }
  262. };
  263. static const SEC_ASN1Template NSSCMSKeyAgreeRecipientIdentifierTemplate[] = {
  264. { SEC_ASN1_CHOICE,
  265. offsetof(NSSCMSKeyAgreeRecipientIdentifier, identifierType), NULL,
  266. sizeof(NSSCMSKeyAgreeRecipientIdentifier) },
  267. { SEC_ASN1_POINTER | SEC_ASN1_XTRN,
  268. offsetof(NSSCMSKeyAgreeRecipientIdentifier, id.issuerAndSN),
  269. SEC_ASN1_SUB(CERT_IssuerAndSNTemplate),
  270. NSSCMSKeyAgreeRecipientID_IssuerSN },
  271. { SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
  272. offsetof(NSSCMSKeyAgreeRecipientIdentifier, id.recipientKeyIdentifier),
  273. NSSCMSRecipientKeyIdentifierTemplate,
  274. NSSCMSKeyAgreeRecipientID_RKeyID },
  275. { 0 }
  276. };
  277. static const SEC_ASN1Template NSSCMSRecipientEncryptedKeyTemplate[] = {
  278. { SEC_ASN1_SEQUENCE,
  279. 0, NULL, sizeof(NSSCMSRecipientEncryptedKey) },
  280. { SEC_ASN1_INLINE,
  281. offsetof(NSSCMSRecipientEncryptedKey, recipientIdentifier),
  282. NSSCMSKeyAgreeRecipientIdentifierTemplate },
  283. { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
  284. offsetof(NSSCMSRecipientEncryptedKey, encKey),
  285. SEC_ASN1_SUB(SEC_BitStringTemplate) },
  286. { 0 }
  287. };
  288. static const SEC_ASN1Template NSSCMSKeyAgreeRecipientInfoTemplate[] = {
  289. { SEC_ASN1_SEQUENCE,
  290. 0, NULL, sizeof(NSSCMSKeyAgreeRecipientInfo) },
  291. { SEC_ASN1_INTEGER,
  292. offsetof(NSSCMSKeyAgreeRecipientInfo, version) },
  293. { SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0,
  294. offsetof(NSSCMSKeyAgreeRecipientInfo, originatorIdentifierOrKey),
  295. NSSCMSOriginatorIdentifierOrKeyTemplate },
  296. { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_EXPLICIT |
  297. SEC_ASN1_CONTEXT_SPECIFIC | SEC_ASN1_XTRN | 1,
  298. offsetof(NSSCMSKeyAgreeRecipientInfo, ukm),
  299. SEC_ASN1_SUB(SEC_OctetStringTemplate) },
  300. { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
  301. offsetof(NSSCMSKeyAgreeRecipientInfo, keyEncAlg),
  302. SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
  303. { SEC_ASN1_SEQUENCE_OF,
  304. offsetof(NSSCMSKeyAgreeRecipientInfo, recipientEncryptedKeys),
  305. NSSCMSRecipientEncryptedKeyTemplate },
  306. { 0 }
  307. };
  308. /* -----------------------------------------------------------------------------
  309. * KEK recipient info
  310. */
  311. static const SEC_ASN1Template NSSCMSKEKIdentifierTemplate[] = {
  312. { SEC_ASN1_SEQUENCE,
  313. 0, NULL, sizeof(NSSCMSKEKIdentifier) },
  314. { SEC_ASN1_OCTET_STRING,
  315. offsetof(NSSCMSKEKIdentifier, keyIdentifier) },
  316. { SEC_ASN1_OPTIONAL | SEC_ASN1_OCTET_STRING,
  317. offsetof(NSSCMSKEKIdentifier, date) },
  318. { SEC_ASN1_OPTIONAL | SEC_ASN1_OCTET_STRING,
  319. offsetof(NSSCMSKEKIdentifier, other) },
  320. { 0 }
  321. };
  322. static const SEC_ASN1Template NSSCMSKEKRecipientInfoTemplate[] = {
  323. { SEC_ASN1_SEQUENCE,
  324. 0, NULL, sizeof(NSSCMSKEKRecipientInfo) },
  325. { SEC_ASN1_INTEGER,
  326. offsetof(NSSCMSKEKRecipientInfo, version) },
  327. { SEC_ASN1_INLINE,
  328. offsetof(NSSCMSKEKRecipientInfo, kekIdentifier),
  329. NSSCMSKEKIdentifierTemplate },
  330. { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
  331. offsetof(NSSCMSKEKRecipientInfo, keyEncAlg),
  332. SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
  333. { SEC_ASN1_OCTET_STRING,
  334. offsetof(NSSCMSKEKRecipientInfo, encKey) },
  335. { 0 }
  336. };
  337. /* -----------------------------------------------------------------------------
  338. * recipient info
  339. */
  340. const SEC_ASN1Template NSSCMSRecipientInfoTemplate[] = {
  341. { SEC_ASN1_CHOICE,
  342. offsetof(NSSCMSRecipientInfo, recipientInfoType), NULL,
  343. sizeof(NSSCMSRecipientInfo) },
  344. { SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
  345. offsetof(NSSCMSRecipientInfo, ri.keyAgreeRecipientInfo),
  346. NSSCMSKeyAgreeRecipientInfoTemplate,
  347. NSSCMSRecipientInfoID_KeyAgree },
  348. { SEC_ASN1_EXPLICIT | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 2,
  349. offsetof(NSSCMSRecipientInfo, ri.kekRecipientInfo),
  350. NSSCMSKEKRecipientInfoTemplate,
  351. NSSCMSRecipientInfoID_KEK },
  352. { SEC_ASN1_INLINE,
  353. offsetof(NSSCMSRecipientInfo, ri.keyTransRecipientInfo),
  354. NSSCMSKeyTransRecipientInfoTemplate,
  355. NSSCMSRecipientInfoID_KeyTrans },
  356. { 0 }
  357. };
  358. /* -----------------------------------------------------------------------------
  359. *
  360. */
  361. const SEC_ASN1Template NSSCMSDigestedDataTemplate[] = {
  362. { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
  363. 0, NULL, sizeof(NSSCMSDigestedData) },
  364. { SEC_ASN1_INTEGER,
  365. offsetof(NSSCMSDigestedData, version) },
  366. { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
  367. offsetof(NSSCMSDigestedData, digestAlg),
  368. SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
  369. { SEC_ASN1_INLINE,
  370. offsetof(NSSCMSDigestedData, contentInfo),
  371. NSSCMSEncapsulatedContentInfoTemplate },
  372. { SEC_ASN1_OCTET_STRING,
  373. offsetof(NSSCMSDigestedData, digest) },
  374. { 0 }
  375. };
  376. const SEC_ASN1Template NSS_PointerToCMSDigestedDataTemplate[] = {
  377. { SEC_ASN1_POINTER, 0, NSSCMSDigestedDataTemplate }
  378. };
  379. const SEC_ASN1Template NSSCMSEncryptedDataTemplate[] = {
  380. { SEC_ASN1_SEQUENCE | SEC_ASN1_MAY_STREAM,
  381. 0, NULL, sizeof(NSSCMSEncryptedData) },
  382. { SEC_ASN1_INTEGER,
  383. offsetof(NSSCMSEncryptedData, version) },
  384. { SEC_ASN1_INLINE,
  385. offsetof(NSSCMSEncryptedData, contentInfo),
  386. NSSCMSEncryptedContentInfoTemplate },
  387. { SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 1,
  388. offsetof(NSSCMSEncryptedData, unprotectedAttr),
  389. nss_cms_set_of_attribute_template },
  390. { 0 }
  391. };
  392. const SEC_ASN1Template NSS_PointerToCMSEncryptedDataTemplate[] = {
  393. { SEC_ASN1_POINTER, 0, NSSCMSEncryptedDataTemplate }
  394. };
  395. const SEC_ASN1Template NSSCMSGenericWrapperDataTemplate[] = {
  396. { SEC_ASN1_INLINE,
  397. offsetof(NSSCMSGenericWrapperData, contentInfo),
  398. NSSCMSEncapsulatedContentInfoTemplate },
  399. };
  400. SEC_ASN1_CHOOSER_IMPLEMENT(NSSCMSGenericWrapperDataTemplate)
  401. const SEC_ASN1Template NSS_PointerToCMSGenericWrapperDataTemplate[] = {
  402. { SEC_ASN1_POINTER, 0, NSSCMSGenericWrapperDataTemplate }
  403. };
  404. SEC_ASN1_CHOOSER_IMPLEMENT(NSS_PointerToCMSGenericWrapperDataTemplate)
  405. /* -----------------------------------------------------------------------------
  406. *
  407. */
  408. static const SEC_ASN1Template *
  409. nss_cms_choose_content_template(void *src_or_dest, PRBool encoding)
  410. {
  411. const SEC_ASN1Template *theTemplate;
  412. NSSCMSContentInfo *cinfo;
  413. SECOidTag type;
  414. PORT_Assert(src_or_dest != NULL);
  415. if (src_or_dest == NULL)
  416. return NULL;
  417. cinfo = (NSSCMSContentInfo *)src_or_dest;
  418. type = NSS_CMSContentInfo_GetContentTypeTag(cinfo);
  419. switch (type) {
  420. default:
  421. theTemplate = NSS_CMSType_GetTemplate(type);
  422. break;
  423. case SEC_OID_PKCS7_DATA:
  424. theTemplate = SEC_ASN1_GET(SEC_PointerToOctetStringTemplate);
  425. break;
  426. case SEC_OID_PKCS7_SIGNED_DATA:
  427. theTemplate = NSS_PointerToCMSSignedDataTemplate;
  428. break;
  429. case SEC_OID_PKCS7_ENVELOPED_DATA:
  430. theTemplate = NSS_PointerToCMSEnvelopedDataTemplate;
  431. break;
  432. case SEC_OID_PKCS7_DIGESTED_DATA:
  433. theTemplate = NSS_PointerToCMSDigestedDataTemplate;
  434. break;
  435. case SEC_OID_PKCS7_ENCRYPTED_DATA:
  436. theTemplate = NSS_PointerToCMSEncryptedDataTemplate;
  437. break;
  438. }
  439. return theTemplate;
  440. }