cmst.h 17 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. * Header for CMS types.
  6. */
  7. #ifndef _CMST_H_
  8. #define _CMST_H_
  9. #include "seccomon.h"
  10. #include "secoidt.h"
  11. #include "certt.h"
  12. #include "secmodt.h"
  13. #include "secmodt.h"
  14. #include "plarena.h"
  15. /* Non-opaque objects. NOTE, though: I want them to be treated as
  16. * opaque as much as possible. If I could hide them completely,
  17. * I would. (I tried, but ran into trouble that was taking me too
  18. * much time to get out of.) I still intend to try to do so.
  19. * In fact, the only type that "outsiders" should even *name* is
  20. * NSSCMSMessage, and they should not reference its fields.
  21. */
  22. /* rjr: PKCS #11 cert handling (pk11cert.c) does use NSSCMSRecipientInfo's.
  23. * This is because when we search the recipient list for the cert and key we
  24. * want, we need to invert the order of the loops we used to have. The old
  25. * loops were:
  26. *
  27. * For each recipient {
  28. * find_cert = PK11_Find_AllCert(recipient->issuerSN);
  29. * [which unrolls to... ]
  30. * For each slot {
  31. * Log into slot;
  32. * search slot for cert;
  33. * }
  34. * }
  35. *
  36. * the new loop searchs all the recipients at once on a slot. this allows
  37. * PKCS #11 to order slots in such a way that logout slots don't get checked
  38. * if we can find the cert on a logged in slot. This eliminates lots of
  39. * spurious password prompts when smart cards are installed... so why this
  40. * comment? If you make NSSCMSRecipientInfo completely opaque, you need
  41. * to provide a non-opaque list of issuerSN's (the only field PKCS#11 needs
  42. * and fix up pk11cert.c first. NOTE: Only S/MIME calls this special PKCS #11
  43. * function.
  44. */
  45. typedef struct NSSCMSMessageStr NSSCMSMessage;
  46. typedef union NSSCMSContentUnion NSSCMSContent;
  47. typedef struct NSSCMSContentInfoStr NSSCMSContentInfo;
  48. typedef struct NSSCMSSignedDataStr NSSCMSSignedData;
  49. typedef struct NSSCMSSignerInfoStr NSSCMSSignerInfo;
  50. typedef struct NSSCMSSignerIdentifierStr NSSCMSSignerIdentifier;
  51. typedef struct NSSCMSEnvelopedDataStr NSSCMSEnvelopedData;
  52. typedef struct NSSCMSOriginatorInfoStr NSSCMSOriginatorInfo;
  53. typedef struct NSSCMSRecipientInfoStr NSSCMSRecipientInfo;
  54. typedef struct NSSCMSDigestedDataStr NSSCMSDigestedData;
  55. typedef struct NSSCMSEncryptedDataStr NSSCMSEncryptedData;
  56. typedef struct NSSCMSGenericWrapperDataStr NSSCMSGenericWrapperData;
  57. typedef struct NSSCMSAttributeStr NSSCMSAttribute;
  58. typedef struct NSSCMSDecoderContextStr NSSCMSDecoderContext;
  59. typedef struct NSSCMSEncoderContextStr NSSCMSEncoderContext;
  60. typedef struct NSSCMSCipherContextStr NSSCMSCipherContext;
  61. typedef struct NSSCMSDigestContextStr NSSCMSDigestContext;
  62. typedef struct NSSCMSContentInfoPrivateStr NSSCMSContentInfoPrivate;
  63. typedef SECStatus (*NSSCMSGenericWrapperDataCallback)(NSSCMSGenericWrapperData *);
  64. typedef void (*NSSCMSGenericWrapperDataDestroy)(NSSCMSGenericWrapperData *);
  65. extern const SEC_ASN1Template NSSCMSGenericWrapperDataTemplate[];
  66. extern const SEC_ASN1Template NSS_PointerToCMSGenericWrapperDataTemplate[];
  67. SEC_ASN1_CHOOSER_DECLARE(NSS_PointerToCMSGenericWrapperDataTemplate)
  68. SEC_ASN1_CHOOSER_DECLARE(NSSCMSGenericWrapperDataTemplate)
  69. /*
  70. * Type of function passed to NSSCMSDecode or NSSCMSDecoderStart.
  71. * If specified, this is where the content bytes (only) will be "sent"
  72. * as they are recovered during the decoding.
  73. * And:
  74. * Type of function passed to NSSCMSEncode or NSSCMSEncoderStart.
  75. * This is where the DER-encoded bytes will be "sent".
  76. *
  77. * XXX Should just combine this with NSSCMSEncoderContentCallback type
  78. * and use a simpler, common name.
  79. */
  80. typedef void (*NSSCMSContentCallback)(void *arg, const char *buf, unsigned long len);
  81. /*
  82. * Type of function passed to NSSCMSDecode or NSSCMSDecoderStart
  83. * to retrieve the decryption key. This function is intended to be
  84. * used for EncryptedData content info's which do not have a key available
  85. * in a certificate, etc.
  86. */
  87. typedef PK11SymKey *(*NSSCMSGetDecryptKeyCallback)(void *arg, SECAlgorithmID *algid);
  88. /* =============================================================================
  89. * ENCAPSULATED CONTENTINFO & CONTENTINFO
  90. */
  91. union NSSCMSContentUnion {
  92. /* either unstructured */
  93. SECItem *data;
  94. /* or structured data */
  95. NSSCMSDigestedData *digestedData;
  96. NSSCMSEncryptedData *encryptedData;
  97. NSSCMSEnvelopedData *envelopedData;
  98. NSSCMSSignedData *signedData;
  99. NSSCMSGenericWrapperData *genericData;
  100. /* or anonymous pointer to something */
  101. void *pointer;
  102. };
  103. struct NSSCMSContentInfoStr {
  104. SECItem contentType;
  105. NSSCMSContent content;
  106. /* --------- local; not part of encoding --------- */
  107. SECOidData *contentTypeTag;
  108. /* additional info for encryptedData and envelopedData */
  109. /* we waste this space for signedData and digestedData. sue me. */
  110. SECAlgorithmID contentEncAlg;
  111. SECItem *rawContent; /* encrypted DER, optional */
  112. /* XXXX bytes not encrypted, but encoded? */
  113. /* --------- local; not part of encoding --------- */
  114. PK11SymKey *bulkkey; /* bulk encryption key */
  115. int keysize; /* size of bulk encryption key
  116. * (only used by creation code) */
  117. SECOidTag contentEncAlgTag; /* oid tag of encryption algorithm
  118. * (only used by creation code) */
  119. NSSCMSContentInfoPrivate *privateInfo; /* place for NSS private info */
  120. void *reserved; /* keep binary compatibility */
  121. };
  122. /* =============================================================================
  123. * MESSAGE
  124. */
  125. struct NSSCMSMessageStr {
  126. NSSCMSContentInfo contentInfo; /* "outer" cinfo */
  127. /* --------- local; not part of encoding --------- */
  128. PLArenaPool *poolp;
  129. PRBool poolp_is_ours;
  130. int refCount;
  131. /* properties of the "inner" data */
  132. SECAlgorithmID **detached_digestalgs;
  133. SECItem **detached_digests;
  134. void *pwfn_arg;
  135. NSSCMSGetDecryptKeyCallback decrypt_key_cb;
  136. void *decrypt_key_cb_arg;
  137. };
  138. /* ============================================================================
  139. * GENERIC WRAPPER
  140. *
  141. * used for user defined types.
  142. */
  143. struct NSSCMSGenericWrapperDataStr {
  144. NSSCMSContentInfo contentInfo;
  145. /* ---- local; not part of encoding ------ */
  146. NSSCMSMessage *cmsg;
  147. /* wrapperspecific data starts here */
  148. };
  149. /* =============================================================================
  150. * SIGNEDDATA
  151. */
  152. struct NSSCMSSignedDataStr {
  153. SECItem version;
  154. SECAlgorithmID **digestAlgorithms;
  155. NSSCMSContentInfo contentInfo;
  156. SECItem **rawCerts;
  157. CERTSignedCrl **crls;
  158. NSSCMSSignerInfo **signerInfos;
  159. /* --------- local; not part of encoding --------- */
  160. NSSCMSMessage *cmsg; /* back pointer to message */
  161. SECItem **digests;
  162. CERTCertificate **certs;
  163. CERTCertificateList **certLists;
  164. CERTCertificate **tempCerts; /* temporary certs, needed
  165. * for example for signature
  166. * verification */
  167. };
  168. #define NSS_CMS_SIGNED_DATA_VERSION_BASIC 1 /* what we *create* */
  169. #define NSS_CMS_SIGNED_DATA_VERSION_EXT 3 /* what we *create* */
  170. typedef enum {
  171. NSSCMSVS_Unverified = 0,
  172. NSSCMSVS_GoodSignature = 1,
  173. NSSCMSVS_BadSignature = 2,
  174. NSSCMSVS_DigestMismatch = 3,
  175. NSSCMSVS_SigningCertNotFound = 4,
  176. NSSCMSVS_SigningCertNotTrusted = 5,
  177. NSSCMSVS_SignatureAlgorithmUnknown = 6,
  178. NSSCMSVS_SignatureAlgorithmUnsupported = 7,
  179. NSSCMSVS_MalformedSignature = 8,
  180. NSSCMSVS_ProcessingError = 9
  181. } NSSCMSVerificationStatus;
  182. typedef enum {
  183. NSSCMSSignerID_IssuerSN = 0,
  184. NSSCMSSignerID_SubjectKeyID = 1
  185. } NSSCMSSignerIDSelector;
  186. struct NSSCMSSignerIdentifierStr {
  187. NSSCMSSignerIDSelector identifierType;
  188. union {
  189. CERTIssuerAndSN *issuerAndSN;
  190. SECItem *subjectKeyID;
  191. } id;
  192. };
  193. struct NSSCMSSignerInfoStr {
  194. SECItem version;
  195. NSSCMSSignerIdentifier signerIdentifier;
  196. SECAlgorithmID digestAlg;
  197. NSSCMSAttribute **authAttr;
  198. SECAlgorithmID digestEncAlg;
  199. SECItem encDigest;
  200. NSSCMSAttribute **unAuthAttr;
  201. /* --------- local; not part of encoding --------- */
  202. NSSCMSMessage *cmsg; /* back pointer to message */
  203. CERTCertificate *cert;
  204. CERTCertificateList *certList;
  205. PRTime signingTime;
  206. NSSCMSVerificationStatus verificationStatus;
  207. SECKEYPrivateKey *signingKey; /* Used if we're using subjKeyID*/
  208. SECKEYPublicKey *pubKey;
  209. };
  210. #define NSS_CMS_SIGNER_INFO_VERSION_ISSUERSN 1 /* what we *create* */
  211. #define NSS_CMS_SIGNER_INFO_VERSION_SUBJKEY 3 /* what we *create* */
  212. typedef enum {
  213. NSSCMSCM_None = 0,
  214. NSSCMSCM_CertOnly = 1,
  215. NSSCMSCM_CertChain = 2,
  216. NSSCMSCM_CertChainWithRoot = 3
  217. } NSSCMSCertChainMode;
  218. /* =============================================================================
  219. * ENVELOPED DATA
  220. */
  221. struct NSSCMSEnvelopedDataStr {
  222. SECItem version;
  223. NSSCMSOriginatorInfo *originatorInfo; /* optional */
  224. NSSCMSRecipientInfo **recipientInfos;
  225. NSSCMSContentInfo contentInfo;
  226. NSSCMSAttribute **unprotectedAttr;
  227. /* --------- local; not part of encoding --------- */
  228. NSSCMSMessage *cmsg; /* back pointer to message */
  229. };
  230. #define NSS_CMS_ENVELOPED_DATA_VERSION_REG 0 /* what we *create* */
  231. #define NSS_CMS_ENVELOPED_DATA_VERSION_ADV 2 /* what we *create* */
  232. struct NSSCMSOriginatorInfoStr {
  233. SECItem **rawCerts;
  234. CERTSignedCrl **crls;
  235. /* --------- local; not part of encoding --------- */
  236. CERTCertificate **certs;
  237. };
  238. /* -----------------------------------------------------------------------------
  239. * key transport recipient info
  240. */
  241. typedef enum {
  242. NSSCMSRecipientID_IssuerSN = 0,
  243. NSSCMSRecipientID_SubjectKeyID = 1,
  244. NSSCMSRecipientID_BrandNew = 2
  245. } NSSCMSRecipientIDSelector;
  246. struct NSSCMSRecipientIdentifierStr {
  247. NSSCMSRecipientIDSelector identifierType;
  248. union {
  249. CERTIssuerAndSN *issuerAndSN;
  250. SECItem *subjectKeyID;
  251. } id;
  252. };
  253. typedef struct NSSCMSRecipientIdentifierStr NSSCMSRecipientIdentifier;
  254. struct NSSCMSKeyTransRecipientInfoStr {
  255. SECItem version;
  256. NSSCMSRecipientIdentifier recipientIdentifier;
  257. SECAlgorithmID keyEncAlg;
  258. SECItem encKey;
  259. };
  260. typedef struct NSSCMSKeyTransRecipientInfoStr NSSCMSKeyTransRecipientInfo;
  261. /*
  262. * View comments before NSSCMSRecipientInfoStr for purpose of this
  263. * structure.
  264. */
  265. struct NSSCMSKeyTransRecipientInfoExStr {
  266. NSSCMSKeyTransRecipientInfo recipientInfo;
  267. int version; /* version of this structure (0) */
  268. SECKEYPublicKey *pubKey;
  269. };
  270. typedef struct NSSCMSKeyTransRecipientInfoExStr NSSCMSKeyTransRecipientInfoEx;
  271. #define NSS_CMS_KEYTRANS_RECIPIENT_INFO_VERSION_ISSUERSN 0 /* what we *create* */
  272. #define NSS_CMS_KEYTRANS_RECIPIENT_INFO_VERSION_SUBJKEY 2 /* what we *create* */
  273. /* -----------------------------------------------------------------------------
  274. * key agreement recipient info
  275. */
  276. struct NSSCMSOriginatorPublicKeyStr {
  277. SECAlgorithmID algorithmIdentifier;
  278. SECItem publicKey; /* bit string! */
  279. };
  280. typedef struct NSSCMSOriginatorPublicKeyStr NSSCMSOriginatorPublicKey;
  281. typedef enum {
  282. NSSCMSOriginatorIDOrKey_IssuerSN = 0,
  283. NSSCMSOriginatorIDOrKey_SubjectKeyID = 1,
  284. NSSCMSOriginatorIDOrKey_OriginatorPublicKey = 2
  285. } NSSCMSOriginatorIDOrKeySelector;
  286. struct NSSCMSOriginatorIdentifierOrKeyStr {
  287. NSSCMSOriginatorIDOrKeySelector identifierType;
  288. union {
  289. CERTIssuerAndSN *issuerAndSN; /* static-static */
  290. SECItem *subjectKeyID; /* static-static */
  291. NSSCMSOriginatorPublicKey originatorPublicKey; /* ephemeral-static */
  292. } id;
  293. };
  294. typedef struct NSSCMSOriginatorIdentifierOrKeyStr NSSCMSOriginatorIdentifierOrKey;
  295. struct NSSCMSRecipientKeyIdentifierStr {
  296. SECItem *subjectKeyIdentifier;
  297. SECItem *date; /* optional */
  298. SECItem *other; /* optional */
  299. };
  300. typedef struct NSSCMSRecipientKeyIdentifierStr NSSCMSRecipientKeyIdentifier;
  301. typedef enum {
  302. NSSCMSKeyAgreeRecipientID_IssuerSN = 0,
  303. NSSCMSKeyAgreeRecipientID_RKeyID = 1
  304. } NSSCMSKeyAgreeRecipientIDSelector;
  305. struct NSSCMSKeyAgreeRecipientIdentifierStr {
  306. NSSCMSKeyAgreeRecipientIDSelector identifierType;
  307. union {
  308. CERTIssuerAndSN *issuerAndSN;
  309. NSSCMSRecipientKeyIdentifier recipientKeyIdentifier;
  310. } id;
  311. };
  312. typedef struct NSSCMSKeyAgreeRecipientIdentifierStr NSSCMSKeyAgreeRecipientIdentifier;
  313. struct NSSCMSRecipientEncryptedKeyStr {
  314. NSSCMSKeyAgreeRecipientIdentifier recipientIdentifier;
  315. SECItem encKey;
  316. };
  317. typedef struct NSSCMSRecipientEncryptedKeyStr NSSCMSRecipientEncryptedKey;
  318. struct NSSCMSKeyAgreeRecipientInfoStr {
  319. SECItem version;
  320. NSSCMSOriginatorIdentifierOrKey originatorIdentifierOrKey;
  321. SECItem *ukm; /* optional */
  322. SECAlgorithmID keyEncAlg;
  323. NSSCMSRecipientEncryptedKey **recipientEncryptedKeys;
  324. };
  325. typedef struct NSSCMSKeyAgreeRecipientInfoStr NSSCMSKeyAgreeRecipientInfo;
  326. #define NSS_CMS_KEYAGREE_RECIPIENT_INFO_VERSION 3 /* what we *create* */
  327. /* -----------------------------------------------------------------------------
  328. * KEK recipient info
  329. */
  330. struct NSSCMSKEKIdentifierStr {
  331. SECItem keyIdentifier;
  332. SECItem *date; /* optional */
  333. SECItem *other; /* optional */
  334. };
  335. typedef struct NSSCMSKEKIdentifierStr NSSCMSKEKIdentifier;
  336. struct NSSCMSKEKRecipientInfoStr {
  337. SECItem version;
  338. NSSCMSKEKIdentifier kekIdentifier;
  339. SECAlgorithmID keyEncAlg;
  340. SECItem encKey;
  341. };
  342. typedef struct NSSCMSKEKRecipientInfoStr NSSCMSKEKRecipientInfo;
  343. #define NSS_CMS_KEK_RECIPIENT_INFO_VERSION 4 /* what we *create* */
  344. /* -----------------------------------------------------------------------------
  345. * recipient info
  346. */
  347. typedef enum {
  348. NSSCMSRecipientInfoID_KeyTrans = 0,
  349. NSSCMSRecipientInfoID_KeyAgree = 1,
  350. NSSCMSRecipientInfoID_KEK = 2
  351. } NSSCMSRecipientInfoIDSelector;
  352. /*
  353. * In order to preserve backwards binary compatibility when implementing
  354. * creation of Recipient Info's that uses subjectKeyID in the
  355. * keyTransRecipientInfo we need to stash a public key pointer in this
  356. * structure somewhere. We figured out that NSSCMSKeyTransRecipientInfo
  357. * is the smallest member of the ri union. We're in luck since that's
  358. * the very structure that would need to use the public key. So we created
  359. * a new structure NSSCMSKeyTransRecipientInfoEx which has a member
  360. * NSSCMSKeyTransRecipientInfo as the first member followed by a version
  361. * and a public key pointer. This way we can keep backwards compatibility
  362. * without changing the size of this structure.
  363. *
  364. * BTW, size of structure:
  365. * NSSCMSKeyTransRecipientInfo: 9 ints, 4 pointers
  366. * NSSCMSKeyAgreeRecipientInfo: 12 ints, 8 pointers
  367. * NSSCMSKEKRecipientInfo: 10 ints, 7 pointers
  368. *
  369. * The new structure:
  370. * NSSCMSKeyTransRecipientInfoEx: sizeof(NSSCMSKeyTransRecipientInfo) +
  371. * 1 int, 1 pointer
  372. */
  373. struct NSSCMSRecipientInfoStr {
  374. NSSCMSRecipientInfoIDSelector recipientInfoType;
  375. union {
  376. NSSCMSKeyTransRecipientInfo keyTransRecipientInfo;
  377. NSSCMSKeyAgreeRecipientInfo keyAgreeRecipientInfo;
  378. NSSCMSKEKRecipientInfo kekRecipientInfo;
  379. NSSCMSKeyTransRecipientInfoEx keyTransRecipientInfoEx;
  380. } ri;
  381. /* --------- local; not part of encoding --------- */
  382. NSSCMSMessage *cmsg; /* back pointer to message */
  383. CERTCertificate *cert; /* recipient's certificate */
  384. };
  385. /* =============================================================================
  386. * DIGESTED DATA
  387. */
  388. struct NSSCMSDigestedDataStr {
  389. SECItem version;
  390. SECAlgorithmID digestAlg;
  391. NSSCMSContentInfo contentInfo;
  392. SECItem digest;
  393. /* --------- local; not part of encoding --------- */
  394. NSSCMSMessage *cmsg; /* back pointer */
  395. SECItem cdigest; /* calculated digest */
  396. };
  397. #define NSS_CMS_DIGESTED_DATA_VERSION_DATA 0 /* what we *create* */
  398. #define NSS_CMS_DIGESTED_DATA_VERSION_ENCAP 2 /* what we *create* */
  399. /* =============================================================================
  400. * ENCRYPTED DATA
  401. */
  402. struct NSSCMSEncryptedDataStr {
  403. SECItem version;
  404. NSSCMSContentInfo contentInfo;
  405. NSSCMSAttribute **unprotectedAttr; /* optional */
  406. /* --------- local; not part of encoding --------- */
  407. NSSCMSMessage *cmsg; /* back pointer */
  408. };
  409. #define NSS_CMS_ENCRYPTED_DATA_VERSION 0 /* what we *create* */
  410. #define NSS_CMS_ENCRYPTED_DATA_VERSION_UPATTR 2 /* what we *create* */
  411. /*
  412. * *****************************************************************************
  413. * *****************************************************************************
  414. * *****************************************************************************
  415. */
  416. /*
  417. * See comment above about this type not really belonging to CMS.
  418. */
  419. struct NSSCMSAttributeStr {
  420. /* The following fields make up an encoded Attribute: */
  421. SECItem type;
  422. SECItem **values; /* data may or may not be encoded */
  423. /* The following fields are not part of an encoded Attribute: */
  424. SECOidData *typeTag;
  425. PRBool encoded; /* when true, values are encoded */
  426. };
  427. #endif /* _CMST_H_ */