pkcs7.h 11 KB

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