cms_lcl.h 13 KB

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