cms_lcl.h 13 KB

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