2
0

cms_local.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Copyright 2008-2019 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 OSSL_CRYPTO_CMS_LOCAL_H
  10. # define OSSL_CRYPTO_CMS_LOCAL_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. /* Set to 1 if we have no cert and need extra safety measures for MMA */
  115. int havenocert;
  116. };
  117. struct CMS_RecipientInfo_st {
  118. int type;
  119. union {
  120. CMS_KeyTransRecipientInfo *ktri;
  121. CMS_KeyAgreeRecipientInfo *kari;
  122. CMS_KEKRecipientInfo *kekri;
  123. CMS_PasswordRecipientInfo *pwri;
  124. CMS_OtherRecipientInfo *ori;
  125. } d;
  126. };
  127. typedef CMS_SignerIdentifier CMS_RecipientIdentifier;
  128. struct CMS_KeyTransRecipientInfo_st {
  129. int32_t version;
  130. CMS_RecipientIdentifier *rid;
  131. X509_ALGOR *keyEncryptionAlgorithm;
  132. ASN1_OCTET_STRING *encryptedKey;
  133. /* Recipient Key and cert */
  134. X509 *recip;
  135. EVP_PKEY *pkey;
  136. /* Public key context for this operation */
  137. EVP_PKEY_CTX *pctx;
  138. };
  139. struct CMS_KeyAgreeRecipientInfo_st {
  140. int32_t version;
  141. CMS_OriginatorIdentifierOrKey *originator;
  142. ASN1_OCTET_STRING *ukm;
  143. X509_ALGOR *keyEncryptionAlgorithm;
  144. STACK_OF(CMS_RecipientEncryptedKey) *recipientEncryptedKeys;
  145. /* Public key context associated with current operation */
  146. EVP_PKEY_CTX *pctx;
  147. /* Cipher context for CEK wrapping */
  148. EVP_CIPHER_CTX *ctx;
  149. };
  150. struct CMS_OriginatorIdentifierOrKey_st {
  151. int type;
  152. union {
  153. CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
  154. ASN1_OCTET_STRING *subjectKeyIdentifier;
  155. CMS_OriginatorPublicKey *originatorKey;
  156. } d;
  157. };
  158. struct CMS_OriginatorPublicKey_st {
  159. X509_ALGOR *algorithm;
  160. ASN1_BIT_STRING *publicKey;
  161. };
  162. struct CMS_RecipientEncryptedKey_st {
  163. CMS_KeyAgreeRecipientIdentifier *rid;
  164. ASN1_OCTET_STRING *encryptedKey;
  165. /* Public key associated with this recipient */
  166. EVP_PKEY *pkey;
  167. };
  168. struct CMS_KeyAgreeRecipientIdentifier_st {
  169. int type;
  170. union {
  171. CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
  172. CMS_RecipientKeyIdentifier *rKeyId;
  173. } d;
  174. };
  175. struct CMS_RecipientKeyIdentifier_st {
  176. ASN1_OCTET_STRING *subjectKeyIdentifier;
  177. ASN1_GENERALIZEDTIME *date;
  178. CMS_OtherKeyAttribute *other;
  179. };
  180. struct CMS_KEKRecipientInfo_st {
  181. int32_t version;
  182. CMS_KEKIdentifier *kekid;
  183. X509_ALGOR *keyEncryptionAlgorithm;
  184. ASN1_OCTET_STRING *encryptedKey;
  185. /* Extra info: symmetric key to use */
  186. unsigned char *key;
  187. size_t keylen;
  188. };
  189. struct CMS_KEKIdentifier_st {
  190. ASN1_OCTET_STRING *keyIdentifier;
  191. ASN1_GENERALIZEDTIME *date;
  192. CMS_OtherKeyAttribute *other;
  193. };
  194. struct CMS_PasswordRecipientInfo_st {
  195. int32_t version;
  196. X509_ALGOR *keyDerivationAlgorithm;
  197. X509_ALGOR *keyEncryptionAlgorithm;
  198. ASN1_OCTET_STRING *encryptedKey;
  199. /* Extra info: password to use */
  200. unsigned char *pass;
  201. size_t passlen;
  202. };
  203. struct CMS_OtherRecipientInfo_st {
  204. ASN1_OBJECT *oriType;
  205. ASN1_TYPE *oriValue;
  206. };
  207. struct CMS_DigestedData_st {
  208. int32_t version;
  209. X509_ALGOR *digestAlgorithm;
  210. CMS_EncapsulatedContentInfo *encapContentInfo;
  211. ASN1_OCTET_STRING *digest;
  212. };
  213. struct CMS_EncryptedData_st {
  214. int32_t version;
  215. CMS_EncryptedContentInfo *encryptedContentInfo;
  216. STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
  217. };
  218. struct CMS_AuthenticatedData_st {
  219. int32_t version;
  220. CMS_OriginatorInfo *originatorInfo;
  221. STACK_OF(CMS_RecipientInfo) *recipientInfos;
  222. X509_ALGOR *macAlgorithm;
  223. X509_ALGOR *digestAlgorithm;
  224. CMS_EncapsulatedContentInfo *encapContentInfo;
  225. STACK_OF(X509_ATTRIBUTE) *authAttrs;
  226. ASN1_OCTET_STRING *mac;
  227. STACK_OF(X509_ATTRIBUTE) *unauthAttrs;
  228. };
  229. struct CMS_CompressedData_st {
  230. int32_t version;
  231. X509_ALGOR *compressionAlgorithm;
  232. STACK_OF(CMS_RecipientInfo) *recipientInfos;
  233. CMS_EncapsulatedContentInfo *encapContentInfo;
  234. };
  235. struct CMS_RevocationInfoChoice_st {
  236. int type;
  237. union {
  238. X509_CRL *crl;
  239. CMS_OtherRevocationInfoFormat *other;
  240. } d;
  241. };
  242. # define CMS_REVCHOICE_CRL 0
  243. # define CMS_REVCHOICE_OTHER 1
  244. struct CMS_OtherRevocationInfoFormat_st {
  245. ASN1_OBJECT *otherRevInfoFormat;
  246. ASN1_TYPE *otherRevInfo;
  247. };
  248. struct CMS_CertificateChoices {
  249. int type;
  250. union {
  251. X509 *certificate;
  252. ASN1_STRING *extendedCertificate; /* Obsolete */
  253. ASN1_STRING *v1AttrCert; /* Left encoded for now */
  254. ASN1_STRING *v2AttrCert; /* Left encoded for now */
  255. CMS_OtherCertificateFormat *other;
  256. } d;
  257. };
  258. # define CMS_CERTCHOICE_CERT 0
  259. # define CMS_CERTCHOICE_EXCERT 1
  260. # define CMS_CERTCHOICE_V1ACERT 2
  261. # define CMS_CERTCHOICE_V2ACERT 3
  262. # define CMS_CERTCHOICE_OTHER 4
  263. struct CMS_OtherCertificateFormat_st {
  264. ASN1_OBJECT *otherCertFormat;
  265. ASN1_TYPE *otherCert;
  266. };
  267. /*
  268. * This is also defined in pkcs7.h but we duplicate it to allow the CMS code
  269. * to be independent of PKCS#7
  270. */
  271. struct CMS_IssuerAndSerialNumber_st {
  272. X509_NAME *issuer;
  273. ASN1_INTEGER *serialNumber;
  274. };
  275. struct CMS_OtherKeyAttribute_st {
  276. ASN1_OBJECT *keyAttrId;
  277. ASN1_TYPE *keyAttr;
  278. };
  279. /* ESS structures */
  280. struct CMS_ReceiptRequest_st {
  281. ASN1_OCTET_STRING *signedContentIdentifier;
  282. CMS_ReceiptsFrom *receiptsFrom;
  283. STACK_OF(GENERAL_NAMES) *receiptsTo;
  284. };
  285. struct CMS_ReceiptsFrom_st {
  286. int type;
  287. union {
  288. int32_t allOrFirstTier;
  289. STACK_OF(GENERAL_NAMES) *receiptList;
  290. } d;
  291. };
  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(CMS_ContentInfo *cms);
  319. int cms_DigestedData_do_final(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(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(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(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(CMS_ContentInfo *cms,
  354. CMS_RecipientInfo *ri);
  355. /* PWRI routines */
  356. int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
  357. int en_de);
  358. /* SignerInfo routines */
  359. int CMS_si_check_attributes(const CMS_SignerInfo *si);
  360. DECLARE_ASN1_ITEM(CMS_CertificateChoices)
  361. DECLARE_ASN1_ITEM(CMS_DigestedData)
  362. DECLARE_ASN1_ITEM(CMS_EncryptedData)
  363. DECLARE_ASN1_ITEM(CMS_EnvelopedData)
  364. DECLARE_ASN1_ITEM(CMS_KEKRecipientInfo)
  365. DECLARE_ASN1_ITEM(CMS_KeyAgreeRecipientInfo)
  366. DECLARE_ASN1_ITEM(CMS_KeyTransRecipientInfo)
  367. DECLARE_ASN1_ITEM(CMS_OriginatorPublicKey)
  368. DECLARE_ASN1_ITEM(CMS_OtherKeyAttribute)
  369. DECLARE_ASN1_ITEM(CMS_Receipt)
  370. DECLARE_ASN1_ITEM(CMS_ReceiptRequest)
  371. DECLARE_ASN1_ITEM(CMS_RecipientEncryptedKey)
  372. DECLARE_ASN1_ITEM(CMS_RecipientKeyIdentifier)
  373. DECLARE_ASN1_ITEM(CMS_RevocationInfoChoice)
  374. DECLARE_ASN1_ITEM(CMS_SignedData)
  375. DECLARE_ASN1_ITEM(CMS_CompressedData)
  376. #endif