2
0

pkcs7.h.in 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * {- join("\n * ", @autowarntext) -}
  3. *
  4. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. {-
  12. use OpenSSL::stackhash qw(generate_stack_macros);
  13. -}
  14. #ifndef OPENSSL_PKCS7_H
  15. # define OPENSSL_PKCS7_H
  16. # pragma once
  17. # include <openssl/macros.h>
  18. # ifndef OPENSSL_NO_DEPRECATED_3_0
  19. # define HEADER_PKCS7_H
  20. # endif
  21. # include <openssl/asn1.h>
  22. # include <openssl/bio.h>
  23. # include <openssl/e_os2.h>
  24. # include <openssl/symhacks.h>
  25. # include <openssl/types.h>
  26. # include <openssl/pkcs7err.h>
  27. # ifndef OPENSSL_NO_STDIO
  28. # include <stdio.h>
  29. # endif
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /*-
  34. Encryption_ID DES-CBC
  35. Digest_ID MD5
  36. Digest_Encryption_ID rsaEncryption
  37. Key_Encryption_ID rsaEncryption
  38. */
  39. typedef struct PKCS7_CTX_st {
  40. OSSL_LIB_CTX *libctx;
  41. char *propq;
  42. } PKCS7_CTX;
  43. typedef struct pkcs7_issuer_and_serial_st {
  44. X509_NAME *issuer;
  45. ASN1_INTEGER *serial;
  46. } PKCS7_ISSUER_AND_SERIAL;
  47. typedef struct pkcs7_signer_info_st {
  48. ASN1_INTEGER *version; /* version 1 */
  49. PKCS7_ISSUER_AND_SERIAL *issuer_and_serial;
  50. X509_ALGOR *digest_alg;
  51. STACK_OF(X509_ATTRIBUTE) *auth_attr; /* [ 0 ] */
  52. X509_ALGOR *digest_enc_alg;
  53. ASN1_OCTET_STRING *enc_digest;
  54. STACK_OF(X509_ATTRIBUTE) *unauth_attr; /* [ 1 ] */
  55. /* The private key to sign with */
  56. EVP_PKEY *pkey;
  57. const PKCS7_CTX *ctx;
  58. } PKCS7_SIGNER_INFO;
  59. {-
  60. generate_stack_macros("PKCS7_SIGNER_INFO");
  61. -}
  62. typedef struct pkcs7_recip_info_st {
  63. ASN1_INTEGER *version; /* version 0 */
  64. PKCS7_ISSUER_AND_SERIAL *issuer_and_serial;
  65. X509_ALGOR *key_enc_algor;
  66. ASN1_OCTET_STRING *enc_key;
  67. X509 *cert; /* get the pub-key from this */
  68. const PKCS7_CTX *ctx;
  69. } PKCS7_RECIP_INFO;
  70. {-
  71. generate_stack_macros("PKCS7_RECIP_INFO");
  72. -}
  73. typedef struct pkcs7_signed_st {
  74. ASN1_INTEGER *version; /* version 1 */
  75. STACK_OF(X509_ALGOR) *md_algs; /* md used */
  76. STACK_OF(X509) *cert; /* [ 0 ] */
  77. STACK_OF(X509_CRL) *crl; /* [ 1 ] */
  78. STACK_OF(PKCS7_SIGNER_INFO) *signer_info;
  79. struct pkcs7_st *contents;
  80. } PKCS7_SIGNED;
  81. /*
  82. * The above structure is very very similar to PKCS7_SIGN_ENVELOPE. How about
  83. * merging the two
  84. */
  85. typedef struct pkcs7_enc_content_st {
  86. ASN1_OBJECT *content_type;
  87. X509_ALGOR *algorithm;
  88. ASN1_OCTET_STRING *enc_data; /* [ 0 ] */
  89. const EVP_CIPHER *cipher;
  90. const PKCS7_CTX *ctx;
  91. } PKCS7_ENC_CONTENT;
  92. typedef struct pkcs7_enveloped_st {
  93. ASN1_INTEGER *version; /* version 0 */
  94. STACK_OF(PKCS7_RECIP_INFO) *recipientinfo;
  95. PKCS7_ENC_CONTENT *enc_data;
  96. } PKCS7_ENVELOPE;
  97. typedef struct pkcs7_signedandenveloped_st {
  98. ASN1_INTEGER *version; /* version 1 */
  99. STACK_OF(X509_ALGOR) *md_algs; /* md used */
  100. STACK_OF(X509) *cert; /* [ 0 ] */
  101. STACK_OF(X509_CRL) *crl; /* [ 1 ] */
  102. STACK_OF(PKCS7_SIGNER_INFO) *signer_info;
  103. PKCS7_ENC_CONTENT *enc_data;
  104. STACK_OF(PKCS7_RECIP_INFO) *recipientinfo;
  105. } PKCS7_SIGN_ENVELOPE;
  106. typedef struct pkcs7_digest_st {
  107. ASN1_INTEGER *version; /* version 0 */
  108. X509_ALGOR *md; /* md used */
  109. struct pkcs7_st *contents;
  110. ASN1_OCTET_STRING *digest;
  111. } PKCS7_DIGEST;
  112. typedef struct pkcs7_encrypted_st {
  113. ASN1_INTEGER *version; /* version 0 */
  114. PKCS7_ENC_CONTENT *enc_data;
  115. } PKCS7_ENCRYPT;
  116. typedef struct pkcs7_st {
  117. /*
  118. * The following is non NULL if it contains ASN1 encoding of this
  119. * structure
  120. */
  121. unsigned char *asn1;
  122. long length;
  123. # define PKCS7_S_HEADER 0
  124. # define PKCS7_S_BODY 1
  125. # define PKCS7_S_TAIL 2
  126. int state; /* used during processing */
  127. int detached;
  128. ASN1_OBJECT *type;
  129. /* content as defined by the type */
  130. /*
  131. * all encryption/message digests are applied to the 'contents', leaving
  132. * out the 'type' field.
  133. */
  134. union {
  135. char *ptr;
  136. /* NID_pkcs7_data */
  137. ASN1_OCTET_STRING *data;
  138. /* NID_pkcs7_signed */
  139. PKCS7_SIGNED *sign;
  140. /* NID_pkcs7_enveloped */
  141. PKCS7_ENVELOPE *enveloped;
  142. /* NID_pkcs7_signedAndEnveloped */
  143. PKCS7_SIGN_ENVELOPE *signed_and_enveloped;
  144. /* NID_pkcs7_digest */
  145. PKCS7_DIGEST *digest;
  146. /* NID_pkcs7_encrypted */
  147. PKCS7_ENCRYPT *encrypted;
  148. /* Anything else */
  149. ASN1_TYPE *other;
  150. } d;
  151. PKCS7_CTX ctx;
  152. } PKCS7;
  153. {-
  154. generate_stack_macros("PKCS7");
  155. -}
  156. # define PKCS7_OP_SET_DETACHED_SIGNATURE 1
  157. # define PKCS7_OP_GET_DETACHED_SIGNATURE 2
  158. # define PKCS7_get_signed_attributes(si) ((si)->auth_attr)
  159. # define PKCS7_get_attributes(si) ((si)->unauth_attr)
  160. # define PKCS7_type_is_signed(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_signed)
  161. # define PKCS7_type_is_encrypted(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_encrypted)
  162. # define PKCS7_type_is_enveloped(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_enveloped)
  163. # define PKCS7_type_is_signedAndEnveloped(a) \
  164. (OBJ_obj2nid((a)->type) == NID_pkcs7_signedAndEnveloped)
  165. # define PKCS7_type_is_data(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_data)
  166. # define PKCS7_type_is_digest(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_digest)
  167. # define PKCS7_set_detached(p,v) \
  168. PKCS7_ctrl(p,PKCS7_OP_SET_DETACHED_SIGNATURE,v,NULL)
  169. # define PKCS7_get_detached(p) \
  170. PKCS7_ctrl(p,PKCS7_OP_GET_DETACHED_SIGNATURE,0,NULL)
  171. # define PKCS7_is_detached(p7) (PKCS7_type_is_signed(p7) && PKCS7_get_detached(p7))
  172. /* S/MIME related flags */
  173. # define PKCS7_TEXT 0x1
  174. # define PKCS7_NOCERTS 0x2
  175. # define PKCS7_NOSIGS 0x4
  176. # define PKCS7_NOCHAIN 0x8
  177. # define PKCS7_NOINTERN 0x10
  178. # define PKCS7_NOVERIFY 0x20
  179. # define PKCS7_DETACHED 0x40
  180. # define PKCS7_BINARY 0x80
  181. # define PKCS7_NOATTR 0x100
  182. # define PKCS7_NOSMIMECAP 0x200
  183. # define PKCS7_NOOLDMIMETYPE 0x400
  184. # define PKCS7_CRLFEOL 0x800
  185. # define PKCS7_STREAM 0x1000
  186. # define PKCS7_NOCRL 0x2000
  187. # define PKCS7_PARTIAL 0x4000
  188. # define PKCS7_REUSE_DIGEST 0x8000
  189. # define PKCS7_NO_DUAL_CONTENT 0x10000
  190. /* Flags: for compatibility with older code */
  191. # define SMIME_TEXT PKCS7_TEXT
  192. # define SMIME_NOCERTS PKCS7_NOCERTS
  193. # define SMIME_NOSIGS PKCS7_NOSIGS
  194. # define SMIME_NOCHAIN PKCS7_NOCHAIN
  195. # define SMIME_NOINTERN PKCS7_NOINTERN
  196. # define SMIME_NOVERIFY PKCS7_NOVERIFY
  197. # define SMIME_DETACHED PKCS7_DETACHED
  198. # define SMIME_BINARY PKCS7_BINARY
  199. # define SMIME_NOATTR PKCS7_NOATTR
  200. /* CRLF ASCII canonicalisation */
  201. # define SMIME_ASCIICRLF 0x80000
  202. DECLARE_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL)
  203. int PKCS7_ISSUER_AND_SERIAL_digest(PKCS7_ISSUER_AND_SERIAL *data,
  204. const EVP_MD *type, unsigned char *md,
  205. unsigned int *len);
  206. # ifndef OPENSSL_NO_STDIO
  207. PKCS7 *d2i_PKCS7_fp(FILE *fp, PKCS7 **p7);
  208. int i2d_PKCS7_fp(FILE *fp, const PKCS7 *p7);
  209. # endif
  210. DECLARE_ASN1_DUP_FUNCTION(PKCS7)
  211. PKCS7 *d2i_PKCS7_bio(BIO *bp, PKCS7 **p7);
  212. int i2d_PKCS7_bio(BIO *bp, const PKCS7 *p7);
  213. int i2d_PKCS7_bio_stream(BIO *out, PKCS7 *p7, BIO *in, int flags);
  214. int PEM_write_bio_PKCS7_stream(BIO *out, PKCS7 *p7, BIO *in, int flags);
  215. DECLARE_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO)
  216. DECLARE_ASN1_FUNCTIONS(PKCS7_RECIP_INFO)
  217. DECLARE_ASN1_FUNCTIONS(PKCS7_SIGNED)
  218. DECLARE_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT)
  219. DECLARE_ASN1_FUNCTIONS(PKCS7_ENVELOPE)
  220. DECLARE_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE)
  221. DECLARE_ASN1_FUNCTIONS(PKCS7_DIGEST)
  222. DECLARE_ASN1_FUNCTIONS(PKCS7_ENCRYPT)
  223. DECLARE_ASN1_FUNCTIONS(PKCS7)
  224. PKCS7 *PKCS7_new_ex(OSSL_LIB_CTX *libctx, const char *propq);
  225. DECLARE_ASN1_ITEM(PKCS7_ATTR_SIGN)
  226. DECLARE_ASN1_ITEM(PKCS7_ATTR_VERIFY)
  227. DECLARE_ASN1_NDEF_FUNCTION(PKCS7)
  228. DECLARE_ASN1_PRINT_FUNCTION(PKCS7)
  229. long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg);
  230. int PKCS7_type_is_other(PKCS7 *p7);
  231. int PKCS7_set_type(PKCS7 *p7, int type);
  232. int PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other);
  233. int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data);
  234. int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey,
  235. const EVP_MD *dgst);
  236. int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si);
  237. int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *p7i);
  238. int PKCS7_add_certificate(PKCS7 *p7, X509 *x509);
  239. int PKCS7_add_crl(PKCS7 *p7, X509_CRL *x509);
  240. int PKCS7_content_new(PKCS7 *p7, int nid);
  241. int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx,
  242. BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si);
  243. int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
  244. X509 *x509);
  245. BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio);
  246. int PKCS7_dataFinal(PKCS7 *p7, BIO *bio);
  247. BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert);
  248. PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509,
  249. EVP_PKEY *pkey, const EVP_MD *dgst);
  250. X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si);
  251. int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md);
  252. STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7);
  253. PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509);
  254. void PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk,
  255. X509_ALGOR **pdig, X509_ALGOR **psig);
  256. void PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc);
  257. int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri);
  258. int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509);
  259. int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher);
  260. int PKCS7_stream(unsigned char ***boundary, PKCS7 *p7);
  261. PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx);
  262. ASN1_OCTET_STRING *PKCS7_get_octet_string(PKCS7 *p7);
  263. ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk);
  264. int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int type,
  265. void *data);
  266. int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
  267. void *value);
  268. ASN1_TYPE *PKCS7_get_attribute(const PKCS7_SIGNER_INFO *si, int nid);
  269. ASN1_TYPE *PKCS7_get_signed_attribute(const PKCS7_SIGNER_INFO *si, int nid);
  270. int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
  271. STACK_OF(X509_ATTRIBUTE) *sk);
  272. int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,
  273. STACK_OF(X509_ATTRIBUTE) *sk);
  274. PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
  275. BIO *data, int flags);
  276. PKCS7 *PKCS7_sign_ex(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
  277. BIO *data, int flags, OSSL_LIB_CTX *libctx,
  278. const char *propq);
  279. PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7,
  280. X509 *signcert, EVP_PKEY *pkey,
  281. const EVP_MD *md, int flags);
  282. int PKCS7_final(PKCS7 *p7, BIO *data, int flags);
  283. int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
  284. BIO *indata, BIO *out, int flags);
  285. STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs,
  286. int flags);
  287. PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
  288. int flags);
  289. PKCS7 *PKCS7_encrypt_ex(STACK_OF(X509) *certs, BIO *in,
  290. const EVP_CIPHER *cipher, int flags,
  291. OSSL_LIB_CTX *libctx, const char *propq);
  292. int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data,
  293. int flags);
  294. int PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO *si,
  295. STACK_OF(X509_ALGOR) *cap);
  296. STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si);
  297. int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg);
  298. int PKCS7_add_attrib_content_type(PKCS7_SIGNER_INFO *si, ASN1_OBJECT *coid);
  299. int PKCS7_add0_attrib_signing_time(PKCS7_SIGNER_INFO *si, ASN1_TIME *t);
  300. int PKCS7_add1_attrib_digest(PKCS7_SIGNER_INFO *si,
  301. const unsigned char *md, int mdlen);
  302. int SMIME_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *data, int flags);
  303. PKCS7 *SMIME_read_PKCS7_ex(BIO *bio, BIO **bcont, PKCS7 **p7);
  304. PKCS7 *SMIME_read_PKCS7(BIO *bio, BIO **bcont);
  305. BIO *BIO_new_PKCS7(BIO *out, PKCS7 *p7);
  306. # ifdef __cplusplus
  307. }
  308. # endif
  309. #endif