cms_lcl.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* crypto/cms/cms_lcl.h */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2008 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. * licensing@OpenSSL.org.
  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. };
  162. struct CMS_RecipientInfo_st {
  163. int type;
  164. union {
  165. CMS_KeyTransRecipientInfo *ktri;
  166. CMS_KeyAgreeRecipientInfo *kari;
  167. CMS_KEKRecipientInfo *kekri;
  168. CMS_PasswordRecipientInfo *pwri;
  169. CMS_OtherRecipientInfo *ori;
  170. } d;
  171. };
  172. typedef CMS_SignerIdentifier CMS_RecipientIdentifier;
  173. struct CMS_KeyTransRecipientInfo_st {
  174. long version;
  175. CMS_RecipientIdentifier *rid;
  176. X509_ALGOR *keyEncryptionAlgorithm;
  177. ASN1_OCTET_STRING *encryptedKey;
  178. /* Recipient Key and cert */
  179. X509 *recip;
  180. EVP_PKEY *pkey;
  181. /* Public key context for this operation */
  182. EVP_PKEY_CTX *pctx;
  183. };
  184. struct CMS_KeyAgreeRecipientInfo_st {
  185. long version;
  186. CMS_OriginatorIdentifierOrKey *originator;
  187. ASN1_OCTET_STRING *ukm;
  188. X509_ALGOR *keyEncryptionAlgorithm;
  189. STACK_OF(CMS_RecipientEncryptedKey) *recipientEncryptedKeys;
  190. /* Public key context associated with current operation */
  191. EVP_PKEY_CTX *pctx;
  192. /* Cipher context for CEK wrapping */
  193. EVP_CIPHER_CTX ctx;
  194. };
  195. struct CMS_OriginatorIdentifierOrKey_st {
  196. int type;
  197. union {
  198. CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
  199. ASN1_OCTET_STRING *subjectKeyIdentifier;
  200. CMS_OriginatorPublicKey *originatorKey;
  201. } d;
  202. };
  203. struct CMS_OriginatorPublicKey_st {
  204. X509_ALGOR *algorithm;
  205. ASN1_BIT_STRING *publicKey;
  206. };
  207. struct CMS_RecipientEncryptedKey_st {
  208. CMS_KeyAgreeRecipientIdentifier *rid;
  209. ASN1_OCTET_STRING *encryptedKey;
  210. /* Public key associated with this recipient */
  211. EVP_PKEY *pkey;
  212. };
  213. struct CMS_KeyAgreeRecipientIdentifier_st {
  214. int type;
  215. union {
  216. CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
  217. CMS_RecipientKeyIdentifier *rKeyId;
  218. } d;
  219. };
  220. struct CMS_RecipientKeyIdentifier_st {
  221. ASN1_OCTET_STRING *subjectKeyIdentifier;
  222. ASN1_GENERALIZEDTIME *date;
  223. CMS_OtherKeyAttribute *other;
  224. };
  225. struct CMS_KEKRecipientInfo_st {
  226. long version;
  227. CMS_KEKIdentifier *kekid;
  228. X509_ALGOR *keyEncryptionAlgorithm;
  229. ASN1_OCTET_STRING *encryptedKey;
  230. /* Extra info: symmetric key to use */
  231. unsigned char *key;
  232. size_t keylen;
  233. };
  234. struct CMS_KEKIdentifier_st {
  235. ASN1_OCTET_STRING *keyIdentifier;
  236. ASN1_GENERALIZEDTIME *date;
  237. CMS_OtherKeyAttribute *other;
  238. };
  239. struct CMS_PasswordRecipientInfo_st {
  240. long version;
  241. X509_ALGOR *keyDerivationAlgorithm;
  242. X509_ALGOR *keyEncryptionAlgorithm;
  243. ASN1_OCTET_STRING *encryptedKey;
  244. /* Extra info: password to use */
  245. unsigned char *pass;
  246. size_t passlen;
  247. };
  248. struct CMS_OtherRecipientInfo_st {
  249. ASN1_OBJECT *oriType;
  250. ASN1_TYPE *oriValue;
  251. };
  252. struct CMS_DigestedData_st {
  253. long version;
  254. X509_ALGOR *digestAlgorithm;
  255. CMS_EncapsulatedContentInfo *encapContentInfo;
  256. ASN1_OCTET_STRING *digest;
  257. };
  258. struct CMS_EncryptedData_st {
  259. long version;
  260. CMS_EncryptedContentInfo *encryptedContentInfo;
  261. STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
  262. };
  263. struct CMS_AuthenticatedData_st {
  264. long version;
  265. CMS_OriginatorInfo *originatorInfo;
  266. STACK_OF(CMS_RecipientInfo) *recipientInfos;
  267. X509_ALGOR *macAlgorithm;
  268. X509_ALGOR *digestAlgorithm;
  269. CMS_EncapsulatedContentInfo *encapContentInfo;
  270. STACK_OF(X509_ATTRIBUTE) *authAttrs;
  271. ASN1_OCTET_STRING *mac;
  272. STACK_OF(X509_ATTRIBUTE) *unauthAttrs;
  273. };
  274. struct CMS_CompressedData_st {
  275. long version;
  276. X509_ALGOR *compressionAlgorithm;
  277. STACK_OF(CMS_RecipientInfo) *recipientInfos;
  278. CMS_EncapsulatedContentInfo *encapContentInfo;
  279. };
  280. struct CMS_RevocationInfoChoice_st {
  281. int type;
  282. union {
  283. X509_CRL *crl;
  284. CMS_OtherRevocationInfoFormat *other;
  285. } d;
  286. };
  287. # define CMS_REVCHOICE_CRL 0
  288. # define CMS_REVCHOICE_OTHER 1
  289. struct CMS_OtherRevocationInfoFormat_st {
  290. ASN1_OBJECT *otherRevInfoFormat;
  291. ASN1_TYPE *otherRevInfo;
  292. };
  293. struct CMS_CertificateChoices {
  294. int type;
  295. union {
  296. X509 *certificate;
  297. ASN1_STRING *extendedCertificate; /* Obsolete */
  298. ASN1_STRING *v1AttrCert; /* Left encoded for now */
  299. ASN1_STRING *v2AttrCert; /* Left encoded for now */
  300. CMS_OtherCertificateFormat *other;
  301. } d;
  302. };
  303. # define CMS_CERTCHOICE_CERT 0
  304. # define CMS_CERTCHOICE_EXCERT 1
  305. # define CMS_CERTCHOICE_V1ACERT 2
  306. # define CMS_CERTCHOICE_V2ACERT 3
  307. # define CMS_CERTCHOICE_OTHER 4
  308. struct CMS_OtherCertificateFormat_st {
  309. ASN1_OBJECT *otherCertFormat;
  310. ASN1_TYPE *otherCert;
  311. };
  312. /*
  313. * This is also defined in pkcs7.h but we duplicate it to allow the CMS code
  314. * to be independent of PKCS#7
  315. */
  316. struct CMS_IssuerAndSerialNumber_st {
  317. X509_NAME *issuer;
  318. ASN1_INTEGER *serialNumber;
  319. };
  320. struct CMS_OtherKeyAttribute_st {
  321. ASN1_OBJECT *keyAttrId;
  322. ASN1_TYPE *keyAttr;
  323. };
  324. /* ESS structures */
  325. # ifdef HEADER_X509V3_H
  326. struct CMS_ReceiptRequest_st {
  327. ASN1_OCTET_STRING *signedContentIdentifier;
  328. CMS_ReceiptsFrom *receiptsFrom;
  329. STACK_OF(GENERAL_NAMES) *receiptsTo;
  330. };
  331. struct CMS_ReceiptsFrom_st {
  332. int type;
  333. union {
  334. long allOrFirstTier;
  335. STACK_OF(GENERAL_NAMES) *receiptList;
  336. } d;
  337. };
  338. # endif
  339. struct CMS_Receipt_st {
  340. long version;
  341. ASN1_OBJECT *contentType;
  342. ASN1_OCTET_STRING *signedContentIdentifier;
  343. ASN1_OCTET_STRING *originatorSignatureValue;
  344. };
  345. DECLARE_ASN1_FUNCTIONS(CMS_ContentInfo)
  346. DECLARE_ASN1_ITEM(CMS_SignerInfo)
  347. DECLARE_ASN1_ITEM(CMS_IssuerAndSerialNumber)
  348. DECLARE_ASN1_ITEM(CMS_Attributes_Sign)
  349. DECLARE_ASN1_ITEM(CMS_Attributes_Verify)
  350. DECLARE_ASN1_ITEM(CMS_RecipientInfo)
  351. DECLARE_ASN1_ITEM(CMS_PasswordRecipientInfo)
  352. DECLARE_ASN1_ALLOC_FUNCTIONS(CMS_IssuerAndSerialNumber)
  353. # define CMS_SIGNERINFO_ISSUER_SERIAL 0
  354. # define CMS_SIGNERINFO_KEYIDENTIFIER 1
  355. # define CMS_RECIPINFO_ISSUER_SERIAL 0
  356. # define CMS_RECIPINFO_KEYIDENTIFIER 1
  357. # define CMS_REK_ISSUER_SERIAL 0
  358. # define CMS_REK_KEYIDENTIFIER 1
  359. # define CMS_OIK_ISSUER_SERIAL 0
  360. # define CMS_OIK_KEYIDENTIFIER 1
  361. # define CMS_OIK_PUBKEY 2
  362. BIO *cms_content_bio(CMS_ContentInfo *cms);
  363. CMS_ContentInfo *cms_Data_create(void);
  364. CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md);
  365. BIO *cms_DigestedData_init_bio(CMS_ContentInfo *cms);
  366. int cms_DigestedData_do_final(CMS_ContentInfo *cms, BIO *chain, int verify);
  367. BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms);
  368. int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain);
  369. int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert,
  370. int type);
  371. int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
  372. ASN1_OCTET_STRING **keyid,
  373. X509_NAME **issuer,
  374. ASN1_INTEGER **sno);
  375. int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert);
  376. CMS_ContentInfo *cms_CompressedData_create(int comp_nid);
  377. BIO *cms_CompressedData_init_bio(CMS_ContentInfo *cms);
  378. void cms_DigestAlgorithm_set(X509_ALGOR *alg, const EVP_MD *md);
  379. BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm);
  380. int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
  381. X509_ALGOR *mdalg);
  382. int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert);
  383. int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert);
  384. int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert);
  385. int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert);
  386. BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec);
  387. BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms);
  388. int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
  389. const EVP_CIPHER *cipher,
  390. const unsigned char *key, size_t keylen);
  391. int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms);
  392. int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src);
  393. ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si);
  394. BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms);
  395. CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms);
  396. int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd);
  397. int cms_pkey_get_ri_type(EVP_PKEY *pk);
  398. /* KARI routines */
  399. int cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip,
  400. EVP_PKEY *pk, unsigned int flags);
  401. int cms_RecipientInfo_kari_encrypt(CMS_ContentInfo *cms,
  402. CMS_RecipientInfo *ri);
  403. /* PWRI routines */
  404. int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
  405. int en_de);
  406. #ifdef __cplusplus
  407. }
  408. #endif
  409. #endif