cms_lcl.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /* crypto/cms/cms_lcl.h */
  2. /*
  3. * Written by Dr Stephen N Henson ([email protected]) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2008-2019 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * [email protected].
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. */
  54. #ifndef HEADER_CMS_LCL_H
  55. # define HEADER_CMS_LCL_H
  56. #ifdef __cplusplus
  57. extern "C" {
  58. #endif
  59. # include <openssl/x509.h>
  60. /*
  61. * Cryptographic message syntax (CMS) structures: taken from RFC3852
  62. */
  63. /* Forward references */
  64. typedef struct CMS_IssuerAndSerialNumber_st CMS_IssuerAndSerialNumber;
  65. typedef struct CMS_EncapsulatedContentInfo_st CMS_EncapsulatedContentInfo;
  66. typedef struct CMS_SignerIdentifier_st CMS_SignerIdentifier;
  67. typedef struct CMS_SignedData_st CMS_SignedData;
  68. typedef struct CMS_OtherRevocationInfoFormat_st CMS_OtherRevocationInfoFormat;
  69. typedef struct CMS_OriginatorInfo_st CMS_OriginatorInfo;
  70. typedef struct CMS_EncryptedContentInfo_st CMS_EncryptedContentInfo;
  71. typedef struct CMS_EnvelopedData_st CMS_EnvelopedData;
  72. typedef struct CMS_DigestedData_st CMS_DigestedData;
  73. typedef struct CMS_EncryptedData_st CMS_EncryptedData;
  74. typedef struct CMS_AuthenticatedData_st CMS_AuthenticatedData;
  75. typedef struct CMS_CompressedData_st CMS_CompressedData;
  76. typedef struct CMS_OtherCertificateFormat_st CMS_OtherCertificateFormat;
  77. typedef struct CMS_KeyTransRecipientInfo_st CMS_KeyTransRecipientInfo;
  78. typedef struct CMS_OriginatorPublicKey_st CMS_OriginatorPublicKey;
  79. typedef struct CMS_OriginatorIdentifierOrKey_st CMS_OriginatorIdentifierOrKey;
  80. typedef struct CMS_KeyAgreeRecipientInfo_st CMS_KeyAgreeRecipientInfo;
  81. typedef struct CMS_RecipientKeyIdentifier_st CMS_RecipientKeyIdentifier;
  82. typedef struct CMS_KeyAgreeRecipientIdentifier_st
  83. CMS_KeyAgreeRecipientIdentifier;
  84. typedef struct CMS_KEKIdentifier_st CMS_KEKIdentifier;
  85. typedef struct CMS_KEKRecipientInfo_st CMS_KEKRecipientInfo;
  86. typedef struct CMS_PasswordRecipientInfo_st CMS_PasswordRecipientInfo;
  87. typedef struct CMS_OtherRecipientInfo_st CMS_OtherRecipientInfo;
  88. typedef struct CMS_ReceiptsFrom_st CMS_ReceiptsFrom;
  89. struct CMS_ContentInfo_st {
  90. ASN1_OBJECT *contentType;
  91. union {
  92. ASN1_OCTET_STRING *data;
  93. CMS_SignedData *signedData;
  94. CMS_EnvelopedData *envelopedData;
  95. CMS_DigestedData *digestedData;
  96. CMS_EncryptedData *encryptedData;
  97. CMS_AuthenticatedData *authenticatedData;
  98. CMS_CompressedData *compressedData;
  99. ASN1_TYPE *other;
  100. /* Other types ... */
  101. void *otherData;
  102. } d;
  103. };
  104. struct CMS_SignedData_st {
  105. long version;
  106. STACK_OF(X509_ALGOR) *digestAlgorithms;
  107. CMS_EncapsulatedContentInfo *encapContentInfo;
  108. STACK_OF(CMS_CertificateChoices) *certificates;
  109. STACK_OF(CMS_RevocationInfoChoice) *crls;
  110. STACK_OF(CMS_SignerInfo) *signerInfos;
  111. };
  112. struct CMS_EncapsulatedContentInfo_st {
  113. ASN1_OBJECT *eContentType;
  114. ASN1_OCTET_STRING *eContent;
  115. /* Set to 1 if incomplete structure only part set up */
  116. int partial;
  117. };
  118. struct CMS_SignerInfo_st {
  119. long version;
  120. CMS_SignerIdentifier *sid;
  121. X509_ALGOR *digestAlgorithm;
  122. STACK_OF(X509_ATTRIBUTE) *signedAttrs;
  123. X509_ALGOR *signatureAlgorithm;
  124. ASN1_OCTET_STRING *signature;
  125. STACK_OF(X509_ATTRIBUTE) *unsignedAttrs;
  126. /* Signing certificate and key */
  127. X509 *signer;
  128. EVP_PKEY *pkey;
  129. /* Digest and public key context for alternative parameters */
  130. EVP_MD_CTX mctx;
  131. EVP_PKEY_CTX *pctx;
  132. };
  133. struct CMS_SignerIdentifier_st {
  134. int type;
  135. union {
  136. CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
  137. ASN1_OCTET_STRING *subjectKeyIdentifier;
  138. } d;
  139. };
  140. struct CMS_EnvelopedData_st {
  141. long version;
  142. CMS_OriginatorInfo *originatorInfo;
  143. STACK_OF(CMS_RecipientInfo) *recipientInfos;
  144. CMS_EncryptedContentInfo *encryptedContentInfo;
  145. STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
  146. };
  147. struct CMS_OriginatorInfo_st {
  148. STACK_OF(CMS_CertificateChoices) *certificates;
  149. STACK_OF(CMS_RevocationInfoChoice) *crls;
  150. };
  151. struct CMS_EncryptedContentInfo_st {
  152. ASN1_OBJECT *contentType;
  153. X509_ALGOR *contentEncryptionAlgorithm;
  154. ASN1_OCTET_STRING *encryptedContent;
  155. /* Content encryption algorithm and key */
  156. const EVP_CIPHER *cipher;
  157. unsigned char *key;
  158. size_t keylen;
  159. /* Set to 1 if we are debugging decrypt and don't fake keys for MMA */
  160. int debug;
  161. /* Set to 1 if we have no cert and need extra safety measures for MMA */
  162. int havenocert;
  163. };
  164. struct CMS_RecipientInfo_st {
  165. int type;
  166. union {
  167. CMS_KeyTransRecipientInfo *ktri;
  168. CMS_KeyAgreeRecipientInfo *kari;
  169. CMS_KEKRecipientInfo *kekri;
  170. CMS_PasswordRecipientInfo *pwri;
  171. CMS_OtherRecipientInfo *ori;
  172. } d;
  173. };
  174. typedef CMS_SignerIdentifier CMS_RecipientIdentifier;
  175. struct CMS_KeyTransRecipientInfo_st {
  176. long version;
  177. CMS_RecipientIdentifier *rid;
  178. X509_ALGOR *keyEncryptionAlgorithm;
  179. ASN1_OCTET_STRING *encryptedKey;
  180. /* Recipient Key and cert */
  181. X509 *recip;
  182. EVP_PKEY *pkey;
  183. /* Public key context for this operation */
  184. EVP_PKEY_CTX *pctx;
  185. };
  186. struct CMS_KeyAgreeRecipientInfo_st {
  187. long version;
  188. CMS_OriginatorIdentifierOrKey *originator;
  189. ASN1_OCTET_STRING *ukm;
  190. X509_ALGOR *keyEncryptionAlgorithm;
  191. STACK_OF(CMS_RecipientEncryptedKey) *recipientEncryptedKeys;
  192. /* Public key context associated with current operation */
  193. EVP_PKEY_CTX *pctx;
  194. /* Cipher context for CEK wrapping */
  195. EVP_CIPHER_CTX ctx;
  196. };
  197. struct CMS_OriginatorIdentifierOrKey_st {
  198. int type;
  199. union {
  200. CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
  201. ASN1_OCTET_STRING *subjectKeyIdentifier;
  202. CMS_OriginatorPublicKey *originatorKey;
  203. } d;
  204. };
  205. struct CMS_OriginatorPublicKey_st {
  206. X509_ALGOR *algorithm;
  207. ASN1_BIT_STRING *publicKey;
  208. };
  209. struct CMS_RecipientEncryptedKey_st {
  210. CMS_KeyAgreeRecipientIdentifier *rid;
  211. ASN1_OCTET_STRING *encryptedKey;
  212. /* Public key associated with this recipient */
  213. EVP_PKEY *pkey;
  214. };
  215. struct CMS_KeyAgreeRecipientIdentifier_st {
  216. int type;
  217. union {
  218. CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
  219. CMS_RecipientKeyIdentifier *rKeyId;
  220. } d;
  221. };
  222. struct CMS_RecipientKeyIdentifier_st {
  223. ASN1_OCTET_STRING *subjectKeyIdentifier;
  224. ASN1_GENERALIZEDTIME *date;
  225. CMS_OtherKeyAttribute *other;
  226. };
  227. struct CMS_KEKRecipientInfo_st {
  228. long version;
  229. CMS_KEKIdentifier *kekid;
  230. X509_ALGOR *keyEncryptionAlgorithm;
  231. ASN1_OCTET_STRING *encryptedKey;
  232. /* Extra info: symmetric key to use */
  233. unsigned char *key;
  234. size_t keylen;
  235. };
  236. struct CMS_KEKIdentifier_st {
  237. ASN1_OCTET_STRING *keyIdentifier;
  238. ASN1_GENERALIZEDTIME *date;
  239. CMS_OtherKeyAttribute *other;
  240. };
  241. struct CMS_PasswordRecipientInfo_st {
  242. long version;
  243. X509_ALGOR *keyDerivationAlgorithm;
  244. X509_ALGOR *keyEncryptionAlgorithm;
  245. ASN1_OCTET_STRING *encryptedKey;
  246. /* Extra info: password to use */
  247. unsigned char *pass;
  248. size_t passlen;
  249. };
  250. struct CMS_OtherRecipientInfo_st {
  251. ASN1_OBJECT *oriType;
  252. ASN1_TYPE *oriValue;
  253. };
  254. struct CMS_DigestedData_st {
  255. long version;
  256. X509_ALGOR *digestAlgorithm;
  257. CMS_EncapsulatedContentInfo *encapContentInfo;
  258. ASN1_OCTET_STRING *digest;
  259. };
  260. struct CMS_EncryptedData_st {
  261. long version;
  262. CMS_EncryptedContentInfo *encryptedContentInfo;
  263. STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
  264. };
  265. struct CMS_AuthenticatedData_st {
  266. long version;
  267. CMS_OriginatorInfo *originatorInfo;
  268. STACK_OF(CMS_RecipientInfo) *recipientInfos;
  269. X509_ALGOR *macAlgorithm;
  270. X509_ALGOR *digestAlgorithm;
  271. CMS_EncapsulatedContentInfo *encapContentInfo;
  272. STACK_OF(X509_ATTRIBUTE) *authAttrs;
  273. ASN1_OCTET_STRING *mac;
  274. STACK_OF(X509_ATTRIBUTE) *unauthAttrs;
  275. };
  276. struct CMS_CompressedData_st {
  277. long version;
  278. X509_ALGOR *compressionAlgorithm;
  279. STACK_OF(CMS_RecipientInfo) *recipientInfos;
  280. CMS_EncapsulatedContentInfo *encapContentInfo;
  281. };
  282. struct CMS_RevocationInfoChoice_st {
  283. int type;
  284. union {
  285. X509_CRL *crl;
  286. CMS_OtherRevocationInfoFormat *other;
  287. } d;
  288. };
  289. # define CMS_REVCHOICE_CRL 0
  290. # define CMS_REVCHOICE_OTHER 1
  291. struct CMS_OtherRevocationInfoFormat_st {
  292. ASN1_OBJECT *otherRevInfoFormat;
  293. ASN1_TYPE *otherRevInfo;
  294. };
  295. struct CMS_CertificateChoices {
  296. int type;
  297. union {
  298. X509 *certificate;
  299. ASN1_STRING *extendedCertificate; /* Obsolete */
  300. ASN1_STRING *v1AttrCert; /* Left encoded for now */
  301. ASN1_STRING *v2AttrCert; /* Left encoded for now */
  302. CMS_OtherCertificateFormat *other;
  303. } d;
  304. };
  305. # define CMS_CERTCHOICE_CERT 0
  306. # define CMS_CERTCHOICE_EXCERT 1
  307. # define CMS_CERTCHOICE_V1ACERT 2
  308. # define CMS_CERTCHOICE_V2ACERT 3
  309. # define CMS_CERTCHOICE_OTHER 4
  310. struct CMS_OtherCertificateFormat_st {
  311. ASN1_OBJECT *otherCertFormat;
  312. ASN1_TYPE *otherCert;
  313. };
  314. /*
  315. * This is also defined in pkcs7.h but we duplicate it to allow the CMS code
  316. * to be independent of PKCS#7
  317. */
  318. struct CMS_IssuerAndSerialNumber_st {
  319. X509_NAME *issuer;
  320. ASN1_INTEGER *serialNumber;
  321. };
  322. struct CMS_OtherKeyAttribute_st {
  323. ASN1_OBJECT *keyAttrId;
  324. ASN1_TYPE *keyAttr;
  325. };
  326. /* ESS structures */
  327. # ifdef HEADER_X509V3_H
  328. struct CMS_ReceiptRequest_st {
  329. ASN1_OCTET_STRING *signedContentIdentifier;
  330. CMS_ReceiptsFrom *receiptsFrom;
  331. STACK_OF(GENERAL_NAMES) *receiptsTo;
  332. };
  333. struct CMS_ReceiptsFrom_st {
  334. int type;
  335. union {
  336. long allOrFirstTier;
  337. STACK_OF(GENERAL_NAMES) *receiptList;
  338. } d;
  339. };
  340. # endif
  341. struct CMS_Receipt_st {
  342. long version;
  343. ASN1_OBJECT *contentType;
  344. ASN1_OCTET_STRING *signedContentIdentifier;
  345. ASN1_OCTET_STRING *originatorSignatureValue;
  346. };
  347. DECLARE_ASN1_FUNCTIONS(CMS_ContentInfo)
  348. DECLARE_ASN1_ITEM(CMS_SignerInfo)
  349. DECLARE_ASN1_ITEM(CMS_IssuerAndSerialNumber)
  350. DECLARE_ASN1_ITEM(CMS_Attributes_Sign)
  351. DECLARE_ASN1_ITEM(CMS_Attributes_Verify)
  352. DECLARE_ASN1_ITEM(CMS_RecipientInfo)
  353. DECLARE_ASN1_ITEM(CMS_PasswordRecipientInfo)
  354. DECLARE_ASN1_ALLOC_FUNCTIONS(CMS_IssuerAndSerialNumber)
  355. # define CMS_SIGNERINFO_ISSUER_SERIAL 0
  356. # define CMS_SIGNERINFO_KEYIDENTIFIER 1
  357. # define CMS_RECIPINFO_ISSUER_SERIAL 0
  358. # define CMS_RECIPINFO_KEYIDENTIFIER 1
  359. # define CMS_REK_ISSUER_SERIAL 0
  360. # define CMS_REK_KEYIDENTIFIER 1
  361. # define CMS_OIK_ISSUER_SERIAL 0
  362. # define CMS_OIK_KEYIDENTIFIER 1
  363. # define CMS_OIK_PUBKEY 2
  364. BIO *cms_content_bio(CMS_ContentInfo *cms);
  365. CMS_ContentInfo *cms_Data_create(void);
  366. CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md);
  367. BIO *cms_DigestedData_init_bio(CMS_ContentInfo *cms);
  368. int cms_DigestedData_do_final(CMS_ContentInfo *cms, BIO *chain, int verify);
  369. BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms);
  370. int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain);
  371. int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert,
  372. int type);
  373. int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
  374. ASN1_OCTET_STRING **keyid,
  375. X509_NAME **issuer,
  376. ASN1_INTEGER **sno);
  377. int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert);
  378. CMS_ContentInfo *cms_CompressedData_create(int comp_nid);
  379. BIO *cms_CompressedData_init_bio(CMS_ContentInfo *cms);
  380. void cms_DigestAlgorithm_set(X509_ALGOR *alg, const EVP_MD *md);
  381. BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm);
  382. int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
  383. X509_ALGOR *mdalg);
  384. int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert);
  385. int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert);
  386. int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert);
  387. int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert);
  388. BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec);
  389. BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms);
  390. int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
  391. const EVP_CIPHER *cipher,
  392. const unsigned char *key, size_t keylen);
  393. int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms);
  394. int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src);
  395. ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si);
  396. BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms);
  397. CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms);
  398. int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd);
  399. int cms_pkey_get_ri_type(EVP_PKEY *pk);
  400. /* KARI routines */
  401. int cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip,
  402. EVP_PKEY *pk, unsigned int flags);
  403. int cms_RecipientInfo_kari_encrypt(CMS_ContentInfo *cms,
  404. CMS_RecipientInfo *ri);
  405. /* PWRI routines */
  406. int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
  407. int en_de);
  408. #ifdef __cplusplus
  409. }
  410. #endif
  411. #endif