cms_lcl.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef HEADER_CMS_LCL_H
  10. # define HEADER_CMS_LCL_H
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. # include <openssl/x509.h>
  15. /*
  16. * Cryptographic message syntax (CMS) structures: taken from RFC3852
  17. */
  18. /* Forward references */
  19. typedef struct CMS_IssuerAndSerialNumber_st CMS_IssuerAndSerialNumber;
  20. typedef struct CMS_EncapsulatedContentInfo_st CMS_EncapsulatedContentInfo;
  21. typedef struct CMS_SignerIdentifier_st CMS_SignerIdentifier;
  22. typedef struct CMS_SignedData_st CMS_SignedData;
  23. typedef struct CMS_OtherRevocationInfoFormat_st CMS_OtherRevocationInfoFormat;
  24. typedef struct CMS_OriginatorInfo_st CMS_OriginatorInfo;
  25. typedef struct CMS_EncryptedContentInfo_st CMS_EncryptedContentInfo;
  26. typedef struct CMS_EnvelopedData_st CMS_EnvelopedData;
  27. typedef struct CMS_DigestedData_st CMS_DigestedData;
  28. typedef struct CMS_EncryptedData_st CMS_EncryptedData;
  29. typedef struct CMS_AuthenticatedData_st CMS_AuthenticatedData;
  30. typedef struct CMS_CompressedData_st CMS_CompressedData;
  31. typedef struct CMS_OtherCertificateFormat_st CMS_OtherCertificateFormat;
  32. typedef struct CMS_KeyTransRecipientInfo_st CMS_KeyTransRecipientInfo;
  33. typedef struct CMS_OriginatorPublicKey_st CMS_OriginatorPublicKey;
  34. typedef struct CMS_OriginatorIdentifierOrKey_st CMS_OriginatorIdentifierOrKey;
  35. typedef struct CMS_KeyAgreeRecipientInfo_st CMS_KeyAgreeRecipientInfo;
  36. typedef struct CMS_RecipientKeyIdentifier_st CMS_RecipientKeyIdentifier;
  37. typedef struct CMS_KeyAgreeRecipientIdentifier_st
  38. CMS_KeyAgreeRecipientIdentifier;
  39. typedef struct CMS_KEKIdentifier_st CMS_KEKIdentifier;
  40. typedef struct CMS_KEKRecipientInfo_st CMS_KEKRecipientInfo;
  41. typedef struct CMS_PasswordRecipientInfo_st CMS_PasswordRecipientInfo;
  42. typedef struct CMS_OtherRecipientInfo_st CMS_OtherRecipientInfo;
  43. typedef struct CMS_ReceiptsFrom_st CMS_ReceiptsFrom;
  44. struct CMS_ContentInfo_st {
  45. ASN1_OBJECT *contentType;
  46. union {
  47. ASN1_OCTET_STRING *data;
  48. CMS_SignedData *signedData;
  49. CMS_EnvelopedData *envelopedData;
  50. CMS_DigestedData *digestedData;
  51. CMS_EncryptedData *encryptedData;
  52. CMS_AuthenticatedData *authenticatedData;
  53. CMS_CompressedData *compressedData;
  54. ASN1_TYPE *other;
  55. /* Other types ... */
  56. void *otherData;
  57. } d;
  58. };
  59. DEFINE_STACK_OF(CMS_CertificateChoices)
  60. struct CMS_SignedData_st {
  61. int32_t version;
  62. STACK_OF(X509_ALGOR) *digestAlgorithms;
  63. CMS_EncapsulatedContentInfo *encapContentInfo;
  64. STACK_OF(CMS_CertificateChoices) *certificates;
  65. STACK_OF(CMS_RevocationInfoChoice) *crls;
  66. STACK_OF(CMS_SignerInfo) *signerInfos;
  67. };
  68. struct CMS_EncapsulatedContentInfo_st {
  69. ASN1_OBJECT *eContentType;
  70. ASN1_OCTET_STRING *eContent;
  71. /* Set to 1 if incomplete structure only part set up */
  72. int partial;
  73. };
  74. struct CMS_SignerInfo_st {
  75. int32_t version;
  76. CMS_SignerIdentifier *sid;
  77. X509_ALGOR *digestAlgorithm;
  78. STACK_OF(X509_ATTRIBUTE) *signedAttrs;
  79. X509_ALGOR *signatureAlgorithm;
  80. ASN1_OCTET_STRING *signature;
  81. STACK_OF(X509_ATTRIBUTE) *unsignedAttrs;
  82. /* Signing certificate and key */
  83. X509 *signer;
  84. EVP_PKEY *pkey;
  85. /* Digest and public key context for alternative parameters */
  86. EVP_MD_CTX *mctx;
  87. EVP_PKEY_CTX *pctx;
  88. };
  89. struct CMS_SignerIdentifier_st {
  90. int type;
  91. union {
  92. CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
  93. ASN1_OCTET_STRING *subjectKeyIdentifier;
  94. } d;
  95. };
  96. struct CMS_EnvelopedData_st {
  97. int32_t version;
  98. CMS_OriginatorInfo *originatorInfo;
  99. STACK_OF(CMS_RecipientInfo) *recipientInfos;
  100. CMS_EncryptedContentInfo *encryptedContentInfo;
  101. STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
  102. };
  103. struct CMS_OriginatorInfo_st {
  104. STACK_OF(CMS_CertificateChoices) *certificates;
  105. STACK_OF(CMS_RevocationInfoChoice) *crls;
  106. };
  107. struct CMS_EncryptedContentInfo_st {
  108. ASN1_OBJECT *contentType;
  109. X509_ALGOR *contentEncryptionAlgorithm;
  110. ASN1_OCTET_STRING *encryptedContent;
  111. /* Content encryption algorithm and key */
  112. const EVP_CIPHER *cipher;
  113. unsigned char *key;
  114. size_t keylen;
  115. /* Set to 1 if we are debugging decrypt and don't fake keys for MMA */
  116. int debug;
  117. };
  118. struct CMS_RecipientInfo_st {
  119. int type;
  120. union {
  121. CMS_KeyTransRecipientInfo *ktri;
  122. CMS_KeyAgreeRecipientInfo *kari;
  123. CMS_KEKRecipientInfo *kekri;
  124. CMS_PasswordRecipientInfo *pwri;
  125. CMS_OtherRecipientInfo *ori;
  126. } d;
  127. };
  128. typedef CMS_SignerIdentifier CMS_RecipientIdentifier;
  129. struct CMS_KeyTransRecipientInfo_st {
  130. int32_t version;
  131. CMS_RecipientIdentifier *rid;
  132. X509_ALGOR *keyEncryptionAlgorithm;
  133. ASN1_OCTET_STRING *encryptedKey;
  134. /* Recipient Key and cert */
  135. X509 *recip;
  136. EVP_PKEY *pkey;
  137. /* Public key context for this operation */
  138. EVP_PKEY_CTX *pctx;
  139. };
  140. struct CMS_KeyAgreeRecipientInfo_st {
  141. int32_t version;
  142. CMS_OriginatorIdentifierOrKey *originator;
  143. ASN1_OCTET_STRING *ukm;
  144. X509_ALGOR *keyEncryptionAlgorithm;
  145. STACK_OF(CMS_RecipientEncryptedKey) *recipientEncryptedKeys;
  146. /* Public key context associated with current operation */
  147. EVP_PKEY_CTX *pctx;
  148. /* Cipher context for CEK wrapping */
  149. EVP_CIPHER_CTX *ctx;
  150. };
  151. struct CMS_OriginatorIdentifierOrKey_st {
  152. int type;
  153. union {
  154. CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
  155. ASN1_OCTET_STRING *subjectKeyIdentifier;
  156. CMS_OriginatorPublicKey *originatorKey;
  157. } d;
  158. };
  159. struct CMS_OriginatorPublicKey_st {
  160. X509_ALGOR *algorithm;
  161. ASN1_BIT_STRING *publicKey;
  162. };
  163. struct CMS_RecipientEncryptedKey_st {
  164. CMS_KeyAgreeRecipientIdentifier *rid;
  165. ASN1_OCTET_STRING *encryptedKey;
  166. /* Public key associated with this recipient */
  167. EVP_PKEY *pkey;
  168. };
  169. struct CMS_KeyAgreeRecipientIdentifier_st {
  170. int type;
  171. union {
  172. CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
  173. CMS_RecipientKeyIdentifier *rKeyId;
  174. } d;
  175. };
  176. struct CMS_RecipientKeyIdentifier_st {
  177. ASN1_OCTET_STRING *subjectKeyIdentifier;
  178. ASN1_GENERALIZEDTIME *date;
  179. CMS_OtherKeyAttribute *other;
  180. };
  181. struct CMS_KEKRecipientInfo_st {
  182. int32_t version;
  183. CMS_KEKIdentifier *kekid;
  184. X509_ALGOR *keyEncryptionAlgorithm;
  185. ASN1_OCTET_STRING *encryptedKey;
  186. /* Extra info: symmetric key to use */
  187. unsigned char *key;
  188. size_t keylen;
  189. };
  190. struct CMS_KEKIdentifier_st {
  191. ASN1_OCTET_STRING *keyIdentifier;
  192. ASN1_GENERALIZEDTIME *date;
  193. CMS_OtherKeyAttribute *other;
  194. };
  195. struct CMS_PasswordRecipientInfo_st {
  196. int32_t version;
  197. X509_ALGOR *keyDerivationAlgorithm;
  198. X509_ALGOR *keyEncryptionAlgorithm;
  199. ASN1_OCTET_STRING *encryptedKey;
  200. /* Extra info: password to use */
  201. unsigned char *pass;
  202. size_t passlen;
  203. };
  204. struct CMS_OtherRecipientInfo_st {
  205. ASN1_OBJECT *oriType;
  206. ASN1_TYPE *oriValue;
  207. };
  208. struct CMS_DigestedData_st {
  209. int32_t version;
  210. X509_ALGOR *digestAlgorithm;
  211. CMS_EncapsulatedContentInfo *encapContentInfo;
  212. ASN1_OCTET_STRING *digest;
  213. };
  214. struct CMS_EncryptedData_st {
  215. int32_t version;
  216. CMS_EncryptedContentInfo *encryptedContentInfo;
  217. STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
  218. };
  219. struct CMS_AuthenticatedData_st {
  220. int32_t version;
  221. CMS_OriginatorInfo *originatorInfo;
  222. STACK_OF(CMS_RecipientInfo) *recipientInfos;
  223. X509_ALGOR *macAlgorithm;
  224. X509_ALGOR *digestAlgorithm;
  225. CMS_EncapsulatedContentInfo *encapContentInfo;
  226. STACK_OF(X509_ATTRIBUTE) *authAttrs;
  227. ASN1_OCTET_STRING *mac;
  228. STACK_OF(X509_ATTRIBUTE) *unauthAttrs;
  229. };
  230. struct CMS_CompressedData_st {
  231. int32_t version;
  232. X509_ALGOR *compressionAlgorithm;
  233. STACK_OF(CMS_RecipientInfo) *recipientInfos;
  234. CMS_EncapsulatedContentInfo *encapContentInfo;
  235. };
  236. struct CMS_RevocationInfoChoice_st {
  237. int type;
  238. union {
  239. X509_CRL *crl;
  240. CMS_OtherRevocationInfoFormat *other;
  241. } d;
  242. };
  243. # define CMS_REVCHOICE_CRL 0
  244. # define CMS_REVCHOICE_OTHER 1
  245. struct CMS_OtherRevocationInfoFormat_st {
  246. ASN1_OBJECT *otherRevInfoFormat;
  247. ASN1_TYPE *otherRevInfo;
  248. };
  249. struct CMS_CertificateChoices {
  250. int type;
  251. union {
  252. X509 *certificate;
  253. ASN1_STRING *extendedCertificate; /* Obsolete */
  254. ASN1_STRING *v1AttrCert; /* Left encoded for now */
  255. ASN1_STRING *v2AttrCert; /* Left encoded for now */
  256. CMS_OtherCertificateFormat *other;
  257. } d;
  258. };
  259. # define CMS_CERTCHOICE_CERT 0
  260. # define CMS_CERTCHOICE_EXCERT 1
  261. # define CMS_CERTCHOICE_V1ACERT 2
  262. # define CMS_CERTCHOICE_V2ACERT 3
  263. # define CMS_CERTCHOICE_OTHER 4
  264. struct CMS_OtherCertificateFormat_st {
  265. ASN1_OBJECT *otherCertFormat;
  266. ASN1_TYPE *otherCert;
  267. };
  268. /*
  269. * This is also defined in pkcs7.h but we duplicate it to allow the CMS code
  270. * to be independent of PKCS#7
  271. */
  272. struct CMS_IssuerAndSerialNumber_st {
  273. X509_NAME *issuer;
  274. ASN1_INTEGER *serialNumber;
  275. };
  276. struct CMS_OtherKeyAttribute_st {
  277. ASN1_OBJECT *keyAttrId;
  278. ASN1_TYPE *keyAttr;
  279. };
  280. /* ESS structures */
  281. # ifdef HEADER_X509V3_H
  282. struct CMS_ReceiptRequest_st {
  283. ASN1_OCTET_STRING *signedContentIdentifier;
  284. CMS_ReceiptsFrom *receiptsFrom;
  285. STACK_OF(GENERAL_NAMES) *receiptsTo;
  286. };
  287. struct CMS_ReceiptsFrom_st {
  288. int type;
  289. union {
  290. int32_t allOrFirstTier;
  291. STACK_OF(GENERAL_NAMES) *receiptList;
  292. } d;
  293. };
  294. # endif
  295. struct CMS_Receipt_st {
  296. int32_t version;
  297. ASN1_OBJECT *contentType;
  298. ASN1_OCTET_STRING *signedContentIdentifier;
  299. ASN1_OCTET_STRING *originatorSignatureValue;
  300. };
  301. DECLARE_ASN1_FUNCTIONS(CMS_ContentInfo)
  302. DECLARE_ASN1_ITEM(CMS_SignerInfo)
  303. DECLARE_ASN1_ITEM(CMS_IssuerAndSerialNumber)
  304. DECLARE_ASN1_ITEM(CMS_Attributes_Sign)
  305. DECLARE_ASN1_ITEM(CMS_Attributes_Verify)
  306. DECLARE_ASN1_ITEM(CMS_RecipientInfo)
  307. DECLARE_ASN1_ITEM(CMS_PasswordRecipientInfo)
  308. DECLARE_ASN1_ALLOC_FUNCTIONS(CMS_IssuerAndSerialNumber)
  309. # define CMS_SIGNERINFO_ISSUER_SERIAL 0
  310. # define CMS_SIGNERINFO_KEYIDENTIFIER 1
  311. # define CMS_RECIPINFO_ISSUER_SERIAL 0
  312. # define CMS_RECIPINFO_KEYIDENTIFIER 1
  313. # define CMS_REK_ISSUER_SERIAL 0
  314. # define CMS_REK_KEYIDENTIFIER 1
  315. # define CMS_OIK_ISSUER_SERIAL 0
  316. # define CMS_OIK_KEYIDENTIFIER 1
  317. # define CMS_OIK_PUBKEY 2
  318. BIO *cms_content_bio(CMS_ContentInfo *cms);
  319. CMS_ContentInfo *cms_Data_create(void);
  320. CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md);
  321. BIO *cms_DigestedData_init_bio(CMS_ContentInfo *cms);
  322. int cms_DigestedData_do_final(CMS_ContentInfo *cms, BIO *chain, int verify);
  323. BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms);
  324. int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain);
  325. int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert,
  326. int type);
  327. int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
  328. ASN1_OCTET_STRING **keyid,
  329. X509_NAME **issuer,
  330. ASN1_INTEGER **sno);
  331. int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert);
  332. CMS_ContentInfo *cms_CompressedData_create(int comp_nid);
  333. BIO *cms_CompressedData_init_bio(CMS_ContentInfo *cms);
  334. BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm);
  335. int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
  336. X509_ALGOR *mdalg);
  337. int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert);
  338. int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert);
  339. int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert);
  340. int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert);
  341. BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec);
  342. BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms);
  343. int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
  344. const EVP_CIPHER *cipher,
  345. const unsigned char *key, size_t keylen);
  346. int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms);
  347. int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src);
  348. ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si);
  349. BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms);
  350. CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms);
  351. int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd);
  352. int cms_pkey_get_ri_type(EVP_PKEY *pk);
  353. /* KARI routines */
  354. int cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip,
  355. EVP_PKEY *pk, unsigned int flags);
  356. int cms_RecipientInfo_kari_encrypt(CMS_ContentInfo *cms,
  357. CMS_RecipientInfo *ri);
  358. /* PWRI routines */
  359. int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
  360. int en_de);
  361. DECLARE_ASN1_ITEM(CMS_CertificateChoices)
  362. DECLARE_ASN1_ITEM(CMS_DigestedData)
  363. DECLARE_ASN1_ITEM(CMS_EncryptedData)
  364. DECLARE_ASN1_ITEM(CMS_EnvelopedData)
  365. DECLARE_ASN1_ITEM(CMS_KEKRecipientInfo)
  366. DECLARE_ASN1_ITEM(CMS_KeyAgreeRecipientInfo)
  367. DECLARE_ASN1_ITEM(CMS_KeyTransRecipientInfo)
  368. DECLARE_ASN1_ITEM(CMS_OriginatorPublicKey)
  369. DECLARE_ASN1_ITEM(CMS_OtherKeyAttribute)
  370. DECLARE_ASN1_ITEM(CMS_Receipt)
  371. DECLARE_ASN1_ITEM(CMS_ReceiptRequest)
  372. DECLARE_ASN1_ITEM(CMS_RecipientEncryptedKey)
  373. DECLARE_ASN1_ITEM(CMS_RecipientKeyIdentifier)
  374. DECLARE_ASN1_ITEM(CMS_RevocationInfoChoice)
  375. DECLARE_ASN1_ITEM(CMS_SignedData)
  376. DECLARE_ASN1_ITEM(CMS_CompressedData)
  377. #ifdef __cplusplus
  378. }
  379. #endif
  380. #endif