cms_lcl.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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_OtherKeyAttribute_st CMS_OtherKeyAttribute;
  82. typedef struct CMS_RecipientKeyIdentifier_st CMS_RecipientKeyIdentifier;
  83. typedef struct CMS_KeyAgreeRecipientIdentifier_st
  84. CMS_KeyAgreeRecipientIdentifier;
  85. typedef struct CMS_RecipientEncryptedKey_st CMS_RecipientEncryptedKey;
  86. typedef struct CMS_KEKIdentifier_st CMS_KEKIdentifier;
  87. typedef struct CMS_KEKRecipientInfo_st CMS_KEKRecipientInfo;
  88. typedef struct CMS_PasswordRecipientInfo_st CMS_PasswordRecipientInfo;
  89. typedef struct CMS_OtherRecipientInfo_st CMS_OtherRecipientInfo;
  90. typedef struct CMS_ReceiptsFrom_st CMS_ReceiptsFrom;
  91. struct CMS_ContentInfo_st {
  92. ASN1_OBJECT *contentType;
  93. union {
  94. ASN1_OCTET_STRING *data;
  95. CMS_SignedData *signedData;
  96. CMS_EnvelopedData *envelopedData;
  97. CMS_DigestedData *digestedData;
  98. CMS_EncryptedData *encryptedData;
  99. CMS_AuthenticatedData *authenticatedData;
  100. CMS_CompressedData *compressedData;
  101. ASN1_TYPE *other;
  102. /* Other types ... */
  103. void *otherData;
  104. } d;
  105. };
  106. struct CMS_SignedData_st {
  107. long version;
  108. STACK_OF(X509_ALGOR) *digestAlgorithms;
  109. CMS_EncapsulatedContentInfo *encapContentInfo;
  110. STACK_OF(CMS_CertificateChoices) *certificates;
  111. STACK_OF(CMS_RevocationInfoChoice) *crls;
  112. STACK_OF(CMS_SignerInfo) *signerInfos;
  113. };
  114. struct CMS_EncapsulatedContentInfo_st {
  115. ASN1_OBJECT *eContentType;
  116. ASN1_OCTET_STRING *eContent;
  117. /* Set to 1 if incomplete structure only part set up */
  118. int partial;
  119. };
  120. struct CMS_SignerInfo_st {
  121. long version;
  122. CMS_SignerIdentifier *sid;
  123. X509_ALGOR *digestAlgorithm;
  124. STACK_OF(X509_ATTRIBUTE) *signedAttrs;
  125. X509_ALGOR *signatureAlgorithm;
  126. ASN1_OCTET_STRING *signature;
  127. STACK_OF(X509_ATTRIBUTE) *unsignedAttrs;
  128. /* Signing certificate and key */
  129. X509 *signer;
  130. EVP_PKEY *pkey;
  131. };
  132. struct CMS_SignerIdentifier_st {
  133. int type;
  134. union {
  135. CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
  136. ASN1_OCTET_STRING *subjectKeyIdentifier;
  137. } d;
  138. };
  139. struct CMS_EnvelopedData_st {
  140. long version;
  141. CMS_OriginatorInfo *originatorInfo;
  142. STACK_OF(CMS_RecipientInfo) *recipientInfos;
  143. CMS_EncryptedContentInfo *encryptedContentInfo;
  144. STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
  145. };
  146. struct CMS_OriginatorInfo_st {
  147. STACK_OF(CMS_CertificateChoices) *certificates;
  148. STACK_OF(CMS_RevocationInfoChoice) *crls;
  149. };
  150. struct CMS_EncryptedContentInfo_st {
  151. ASN1_OBJECT *contentType;
  152. X509_ALGOR *contentEncryptionAlgorithm;
  153. ASN1_OCTET_STRING *encryptedContent;
  154. /* Content encryption algorithm and key */
  155. const EVP_CIPHER *cipher;
  156. unsigned char *key;
  157. size_t keylen;
  158. /* Set to 1 if we are debugging decrypt and don't fake keys for MMA */
  159. int debug;
  160. };
  161. struct CMS_RecipientInfo_st {
  162. int type;
  163. union {
  164. CMS_KeyTransRecipientInfo *ktri;
  165. CMS_KeyAgreeRecipientInfo *kari;
  166. CMS_KEKRecipientInfo *kekri;
  167. CMS_PasswordRecipientInfo *pwri;
  168. CMS_OtherRecipientInfo *ori;
  169. } d;
  170. };
  171. typedef CMS_SignerIdentifier CMS_RecipientIdentifier;
  172. struct CMS_KeyTransRecipientInfo_st {
  173. long version;
  174. CMS_RecipientIdentifier *rid;
  175. X509_ALGOR *keyEncryptionAlgorithm;
  176. ASN1_OCTET_STRING *encryptedKey;
  177. /* Recipient Key and cert */
  178. X509 *recip;
  179. EVP_PKEY *pkey;
  180. };
  181. struct CMS_KeyAgreeRecipientInfo_st {
  182. long version;
  183. CMS_OriginatorIdentifierOrKey *originator;
  184. ASN1_OCTET_STRING *ukm;
  185. X509_ALGOR *keyEncryptionAlgorithm;
  186. STACK_OF(CMS_RecipientEncryptedKey) *recipientEncryptedKeys;
  187. };
  188. struct CMS_OriginatorIdentifierOrKey_st {
  189. int type;
  190. union {
  191. CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
  192. ASN1_OCTET_STRING *subjectKeyIdentifier;
  193. CMS_OriginatorPublicKey *originatorKey;
  194. } d;
  195. };
  196. struct CMS_OriginatorPublicKey_st {
  197. X509_ALGOR *algorithm;
  198. ASN1_BIT_STRING *publicKey;
  199. };
  200. struct CMS_RecipientEncryptedKey_st {
  201. CMS_KeyAgreeRecipientIdentifier *rid;
  202. ASN1_OCTET_STRING *encryptedKey;
  203. };
  204. struct CMS_KeyAgreeRecipientIdentifier_st {
  205. int type;
  206. union {
  207. CMS_IssuerAndSerialNumber *issuerAndSerialNumber;
  208. CMS_RecipientKeyIdentifier *rKeyId;
  209. } d;
  210. };
  211. struct CMS_RecipientKeyIdentifier_st {
  212. ASN1_OCTET_STRING *subjectKeyIdentifier;
  213. ASN1_GENERALIZEDTIME *date;
  214. CMS_OtherKeyAttribute *other;
  215. };
  216. struct CMS_KEKRecipientInfo_st {
  217. long version;
  218. CMS_KEKIdentifier *kekid;
  219. X509_ALGOR *keyEncryptionAlgorithm;
  220. ASN1_OCTET_STRING *encryptedKey;
  221. /* Extra info: symmetric key to use */
  222. unsigned char *key;
  223. size_t keylen;
  224. };
  225. struct CMS_KEKIdentifier_st {
  226. ASN1_OCTET_STRING *keyIdentifier;
  227. ASN1_GENERALIZEDTIME *date;
  228. CMS_OtherKeyAttribute *other;
  229. };
  230. struct CMS_PasswordRecipientInfo_st {
  231. long version;
  232. X509_ALGOR *keyDerivationAlgorithm;
  233. X509_ALGOR *keyEncryptionAlgorithm;
  234. ASN1_OCTET_STRING *encryptedKey;
  235. };
  236. struct CMS_OtherRecipientInfo_st {
  237. ASN1_OBJECT *oriType;
  238. ASN1_TYPE *oriValue;
  239. };
  240. struct CMS_DigestedData_st {
  241. long version;
  242. X509_ALGOR *digestAlgorithm;
  243. CMS_EncapsulatedContentInfo *encapContentInfo;
  244. ASN1_OCTET_STRING *digest;
  245. };
  246. struct CMS_EncryptedData_st {
  247. long version;
  248. CMS_EncryptedContentInfo *encryptedContentInfo;
  249. STACK_OF(X509_ATTRIBUTE) *unprotectedAttrs;
  250. };
  251. struct CMS_AuthenticatedData_st {
  252. long version;
  253. CMS_OriginatorInfo *originatorInfo;
  254. STACK_OF(CMS_RecipientInfo) *recipientInfos;
  255. X509_ALGOR *macAlgorithm;
  256. X509_ALGOR *digestAlgorithm;
  257. CMS_EncapsulatedContentInfo *encapContentInfo;
  258. STACK_OF(X509_ATTRIBUTE) *authAttrs;
  259. ASN1_OCTET_STRING *mac;
  260. STACK_OF(X509_ATTRIBUTE) *unauthAttrs;
  261. };
  262. struct CMS_CompressedData_st {
  263. long version;
  264. X509_ALGOR *compressionAlgorithm;
  265. STACK_OF(CMS_RecipientInfo) *recipientInfos;
  266. CMS_EncapsulatedContentInfo *encapContentInfo;
  267. };
  268. struct CMS_RevocationInfoChoice_st {
  269. int type;
  270. union {
  271. X509_CRL *crl;
  272. CMS_OtherRevocationInfoFormat *other;
  273. } d;
  274. };
  275. # define CMS_REVCHOICE_CRL 0
  276. # define CMS_REVCHOICE_OTHER 1
  277. struct CMS_OtherRevocationInfoFormat_st {
  278. ASN1_OBJECT *otherRevInfoFormat;
  279. ASN1_TYPE *otherRevInfo;
  280. };
  281. struct CMS_CertificateChoices {
  282. int type;
  283. union {
  284. X509 *certificate;
  285. ASN1_STRING *extendedCertificate; /* Obsolete */
  286. ASN1_STRING *v1AttrCert; /* Left encoded for now */
  287. ASN1_STRING *v2AttrCert; /* Left encoded for now */
  288. CMS_OtherCertificateFormat *other;
  289. } d;
  290. };
  291. # define CMS_CERTCHOICE_CERT 0
  292. # define CMS_CERTCHOICE_EXCERT 1
  293. # define CMS_CERTCHOICE_V1ACERT 2
  294. # define CMS_CERTCHOICE_V2ACERT 3
  295. # define CMS_CERTCHOICE_OTHER 4
  296. struct CMS_OtherCertificateFormat_st {
  297. ASN1_OBJECT *otherCertFormat;
  298. ASN1_TYPE *otherCert;
  299. };
  300. /*
  301. * This is also defined in pkcs7.h but we duplicate it to allow the CMS code
  302. * to be independent of PKCS#7
  303. */
  304. struct CMS_IssuerAndSerialNumber_st {
  305. X509_NAME *issuer;
  306. ASN1_INTEGER *serialNumber;
  307. };
  308. struct CMS_OtherKeyAttribute_st {
  309. ASN1_OBJECT *keyAttrId;
  310. ASN1_TYPE *keyAttr;
  311. };
  312. /* ESS structures */
  313. # ifdef HEADER_X509V3_H
  314. struct CMS_ReceiptRequest_st {
  315. ASN1_OCTET_STRING *signedContentIdentifier;
  316. CMS_ReceiptsFrom *receiptsFrom;
  317. STACK_OF(GENERAL_NAMES) *receiptsTo;
  318. };
  319. struct CMS_ReceiptsFrom_st {
  320. int type;
  321. union {
  322. long allOrFirstTier;
  323. STACK_OF(GENERAL_NAMES) *receiptList;
  324. } d;
  325. };
  326. # endif
  327. struct CMS_Receipt_st {
  328. long version;
  329. ASN1_OBJECT *contentType;
  330. ASN1_OCTET_STRING *signedContentIdentifier;
  331. ASN1_OCTET_STRING *originatorSignatureValue;
  332. };
  333. DECLARE_ASN1_ITEM(CMS_SignerInfo)
  334. DECLARE_ASN1_ITEM(CMS_IssuerAndSerialNumber)
  335. DECLARE_ASN1_ITEM(CMS_Attributes_Sign)
  336. DECLARE_ASN1_ITEM(CMS_Attributes_Verify)
  337. DECLARE_ASN1_ALLOC_FUNCTIONS(CMS_IssuerAndSerialNumber)
  338. # define CMS_SIGNERINFO_ISSUER_SERIAL 0
  339. # define CMS_SIGNERINFO_KEYIDENTIFIER 1
  340. # define CMS_RECIPINFO_ISSUER_SERIAL 0
  341. # define CMS_RECIPINFO_KEYIDENTIFIER 1
  342. BIO *cms_content_bio(CMS_ContentInfo *cms);
  343. CMS_ContentInfo *cms_Data_create(void);
  344. CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md);
  345. BIO *cms_DigestedData_init_bio(CMS_ContentInfo *cms);
  346. int cms_DigestedData_do_final(CMS_ContentInfo *cms, BIO *chain, int verify);
  347. BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms);
  348. int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain);
  349. int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert,
  350. int type);
  351. int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
  352. ASN1_OCTET_STRING **keyid,
  353. X509_NAME **issuer,
  354. ASN1_INTEGER **sno);
  355. int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert);
  356. CMS_ContentInfo *cms_CompressedData_create(int comp_nid);
  357. BIO *cms_CompressedData_init_bio(CMS_ContentInfo *cms);
  358. void cms_DigestAlgorithm_set(X509_ALGOR *alg, const EVP_MD *md);
  359. BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm);
  360. int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
  361. X509_ALGOR *mdalg);
  362. BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec);
  363. BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms);
  364. int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
  365. const EVP_CIPHER *cipher,
  366. const unsigned char *key, size_t keylen);
  367. int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms);
  368. int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src);
  369. ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si);
  370. BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms);
  371. #ifdef __cplusplus
  372. }
  373. #endif
  374. #endif