2
0

pkcs7.h 11 KB

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