2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #ifndef OPENSSL_PKCS7_H
11 # define OPENSSL_PKCS7_H
14 # include <openssl/macros.h>
15 # ifndef OPENSSL_NO_DEPRECATED_3_0
16 # define HEADER_PKCS7_H
19 # include <openssl/asn1.h>
20 # include <openssl/bio.h>
21 # include <openssl/e_os2.h>
23 # include <openssl/symhacks.h>
24 # include <openssl/types.h>
25 # include <openssl/pkcs7err.h>
35 Digest_Encryption_ID rsaEncryption
36 Key_Encryption_ID rsaEncryption
39 typedef struct PKCS7_CTX_st
{
44 typedef struct pkcs7_issuer_and_serial_st
{
47 } PKCS7_ISSUER_AND_SERIAL
;
49 typedef struct pkcs7_signer_info_st
{
50 ASN1_INTEGER
*version
; /* version 1 */
51 PKCS7_ISSUER_AND_SERIAL
*issuer_and_serial
;
52 X509_ALGOR
*digest_alg
;
53 STACK_OF(X509_ATTRIBUTE
) *auth_attr
; /* [ 0 ] */
54 X509_ALGOR
*digest_enc_alg
;
55 ASN1_OCTET_STRING
*enc_digest
;
56 STACK_OF(X509_ATTRIBUTE
) *unauth_attr
; /* [ 1 ] */
57 /* The private key to sign with */
61 DEFINE_OR_DECLARE_STACK_OF(PKCS7_SIGNER_INFO
)
63 typedef struct pkcs7_recip_info_st
{
64 ASN1_INTEGER
*version
; /* version 0 */
65 PKCS7_ISSUER_AND_SERIAL
*issuer_and_serial
;
66 X509_ALGOR
*key_enc_algor
;
67 ASN1_OCTET_STRING
*enc_key
;
68 X509
*cert
; /* get the pub-key from this */
71 DEFINE_OR_DECLARE_STACK_OF(PKCS7_RECIP_INFO
)
74 typedef struct pkcs7_signed_st
{
75 ASN1_INTEGER
*version
; /* version 1 */
76 STACK_OF(X509_ALGOR
) *md_algs
; /* md used */
77 STACK_OF(X509
) *cert
; /* [ 0 ] */
78 STACK_OF(X509_CRL
) *crl
; /* [ 1 ] */
79 STACK_OF(PKCS7_SIGNER_INFO
) *signer_info
;
80 struct pkcs7_st
*contents
;
83 * The above structure is very very similar to PKCS7_SIGN_ENVELOPE. How about
87 typedef struct pkcs7_enc_content_st
{
88 ASN1_OBJECT
*content_type
;
89 X509_ALGOR
*algorithm
;
90 ASN1_OCTET_STRING
*enc_data
; /* [ 0 ] */
91 const EVP_CIPHER
*cipher
;
95 typedef struct pkcs7_enveloped_st
{
96 ASN1_INTEGER
*version
; /* version 0 */
97 STACK_OF(PKCS7_RECIP_INFO
) *recipientinfo
;
98 PKCS7_ENC_CONTENT
*enc_data
;
101 typedef struct pkcs7_signedandenveloped_st
{
102 ASN1_INTEGER
*version
; /* version 1 */
103 STACK_OF(X509_ALGOR
) *md_algs
; /* md used */
104 STACK_OF(X509
) *cert
; /* [ 0 ] */
105 STACK_OF(X509_CRL
) *crl
; /* [ 1 ] */
106 STACK_OF(PKCS7_SIGNER_INFO
) *signer_info
;
107 PKCS7_ENC_CONTENT
*enc_data
;
108 STACK_OF(PKCS7_RECIP_INFO
) *recipientinfo
;
109 } PKCS7_SIGN_ENVELOPE
;
111 typedef struct pkcs7_digest_st
{
112 ASN1_INTEGER
*version
; /* version 0 */
113 X509_ALGOR
*md
; /* md used */
114 struct pkcs7_st
*contents
;
115 ASN1_OCTET_STRING
*digest
;
118 typedef struct pkcs7_encrypted_st
{
119 ASN1_INTEGER
*version
; /* version 0 */
120 PKCS7_ENC_CONTENT
*enc_data
;
123 typedef struct pkcs7_st
{
125 * The following is non NULL if it contains ASN1 encoding of this
130 # define PKCS7_S_HEADER 0
131 # define PKCS7_S_BODY 1
132 # define PKCS7_S_TAIL 2
133 int state
; /* used during processing */
136 /* content as defined by the type */
138 * all encryption/message digests are applied to the 'contents', leaving
139 * out the 'type' field.
144 ASN1_OCTET_STRING
*data
;
145 /* NID_pkcs7_signed */
147 /* NID_pkcs7_enveloped */
148 PKCS7_ENVELOPE
*enveloped
;
149 /* NID_pkcs7_signedAndEnveloped */
150 PKCS7_SIGN_ENVELOPE
*signed_and_enveloped
;
151 /* NID_pkcs7_digest */
152 PKCS7_DIGEST
*digest
;
153 /* NID_pkcs7_encrypted */
154 PKCS7_ENCRYPT
*encrypted
;
160 DEFINE_OR_DECLARE_STACK_OF(PKCS7
)
163 # define PKCS7_OP_SET_DETACHED_SIGNATURE 1
164 # define PKCS7_OP_GET_DETACHED_SIGNATURE 2
166 # define PKCS7_get_signed_attributes(si) ((si)->auth_attr)
167 # define PKCS7_get_attributes(si) ((si)->unauth_attr)
169 # define PKCS7_type_is_signed(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_signed)
170 # define PKCS7_type_is_encrypted(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_encrypted)
171 # define PKCS7_type_is_enveloped(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_enveloped)
172 # define PKCS7_type_is_signedAndEnveloped(a) \
173 (OBJ_obj2nid((a)->type) == NID_pkcs7_signedAndEnveloped)
174 # define PKCS7_type_is_data(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_data)
175 # define PKCS7_type_is_digest(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_digest)
177 # define PKCS7_set_detached(p,v) \
178 PKCS7_ctrl(p,PKCS7_OP_SET_DETACHED_SIGNATURE,v,NULL)
179 # define PKCS7_get_detached(p) \
180 PKCS7_ctrl(p,PKCS7_OP_GET_DETACHED_SIGNATURE,0,NULL)
182 # define PKCS7_is_detached(p7) (PKCS7_type_is_signed(p7) && PKCS7_get_detached(p7))
184 /* S/MIME related flags */
186 # define PKCS7_TEXT 0x1
187 # define PKCS7_NOCERTS 0x2
188 # define PKCS7_NOSIGS 0x4
189 # define PKCS7_NOCHAIN 0x8
190 # define PKCS7_NOINTERN 0x10
191 # define PKCS7_NOVERIFY 0x20
192 # define PKCS7_DETACHED 0x40
193 # define PKCS7_BINARY 0x80
194 # define PKCS7_NOATTR 0x100
195 # define PKCS7_NOSMIMECAP 0x200
196 # define PKCS7_NOOLDMIMETYPE 0x400
197 # define PKCS7_CRLFEOL 0x800
198 # define PKCS7_STREAM 0x1000
199 # define PKCS7_NOCRL 0x2000
200 # define PKCS7_PARTIAL 0x4000
201 # define PKCS7_REUSE_DIGEST 0x8000
202 # define PKCS7_NO_DUAL_CONTENT 0x10000
204 /* Flags: for compatibility with older code */
206 # define SMIME_TEXT PKCS7_TEXT
207 # define SMIME_NOCERTS PKCS7_NOCERTS
208 # define SMIME_NOSIGS PKCS7_NOSIGS
209 # define SMIME_NOCHAIN PKCS7_NOCHAIN
210 # define SMIME_NOINTERN PKCS7_NOINTERN
211 # define SMIME_NOVERIFY PKCS7_NOVERIFY
212 # define SMIME_DETACHED PKCS7_DETACHED
213 # define SMIME_BINARY PKCS7_BINARY
214 # define SMIME_NOATTR PKCS7_NOATTR
216 /* CRLF ASCII canonicalisation */
217 # define SMIME_ASCIICRLF 0x80000
219 DECLARE_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL
)
221 int PKCS7_ISSUER_AND_SERIAL_digest(PKCS7_ISSUER_AND_SERIAL
*data
,
222 const EVP_MD
*type
, unsigned char *md
,
224 # ifndef OPENSSL_NO_STDIO
225 PKCS7
*d2i_PKCS7_fp(FILE *fp
, PKCS7
**p7
);
226 int i2d_PKCS7_fp(FILE *fp
, const PKCS7
*p7
);
228 DECLARE_ASN1_DUP_FUNCTION(PKCS7
)
229 PKCS7
*d2i_PKCS7_bio(BIO
*bp
, PKCS7
**p7
);
230 int i2d_PKCS7_bio(BIO
*bp
, const PKCS7
*p7
);
231 int i2d_PKCS7_bio_stream(BIO
*out
, PKCS7
*p7
, BIO
*in
, int flags
);
232 int PEM_write_bio_PKCS7_stream(BIO
*out
, PKCS7
*p7
, BIO
*in
, int flags
);
234 DECLARE_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO
)
235 DECLARE_ASN1_FUNCTIONS(PKCS7_RECIP_INFO
)
236 DECLARE_ASN1_FUNCTIONS(PKCS7_SIGNED
)
237 DECLARE_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT
)
238 DECLARE_ASN1_FUNCTIONS(PKCS7_ENVELOPE
)
239 DECLARE_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE
)
240 DECLARE_ASN1_FUNCTIONS(PKCS7_DIGEST
)
241 DECLARE_ASN1_FUNCTIONS(PKCS7_ENCRYPT
)
242 DECLARE_ASN1_FUNCTIONS(PKCS7
)
243 PKCS7
*PKCS7_new_with_libctx(OPENSSL_CTX
*libctx
, const char *propq
);
245 DECLARE_ASN1_ITEM(PKCS7_ATTR_SIGN
)
246 DECLARE_ASN1_ITEM(PKCS7_ATTR_VERIFY
)
248 DECLARE_ASN1_NDEF_FUNCTION(PKCS7
)
249 DECLARE_ASN1_PRINT_FUNCTION(PKCS7
)
251 long PKCS7_ctrl(PKCS7
*p7
, int cmd
, long larg
, char *parg
);
253 int PKCS7_set_type(PKCS7
*p7
, int type
);
254 int PKCS7_set0_type_other(PKCS7
*p7
, int type
, ASN1_TYPE
*other
);
255 int PKCS7_set_content(PKCS7
*p7
, PKCS7
*p7_data
);
256 int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO
*p7i
, X509
*x509
, EVP_PKEY
*pkey
,
258 int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO
*si
);
259 int PKCS7_add_signer(PKCS7
*p7
, PKCS7_SIGNER_INFO
*p7i
);
260 int PKCS7_add_certificate(PKCS7
*p7
, X509
*x509
);
261 int PKCS7_add_crl(PKCS7
*p7
, X509_CRL
*x509
);
262 int PKCS7_content_new(PKCS7
*p7
, int nid
);
263 int PKCS7_dataVerify(X509_STORE
*cert_store
, X509_STORE_CTX
*ctx
,
264 BIO
*bio
, PKCS7
*p7
, PKCS7_SIGNER_INFO
*si
);
265 int PKCS7_signatureVerify(BIO
*bio
, PKCS7
*p7
, PKCS7_SIGNER_INFO
*si
,
268 BIO
*PKCS7_dataInit(PKCS7
*p7
, BIO
*bio
);
269 int PKCS7_dataFinal(PKCS7
*p7
, BIO
*bio
);
270 BIO
*PKCS7_dataDecode(PKCS7
*p7
, EVP_PKEY
*pkey
, BIO
*in_bio
, X509
*pcert
);
272 PKCS7_SIGNER_INFO
*PKCS7_add_signature(PKCS7
*p7
, X509
*x509
,
273 EVP_PKEY
*pkey
, const EVP_MD
*dgst
);
274 X509
*PKCS7_cert_from_signer_info(PKCS7
*p7
, PKCS7_SIGNER_INFO
*si
);
275 int PKCS7_set_digest(PKCS7
*p7
, const EVP_MD
*md
);
276 STACK_OF(PKCS7_SIGNER_INFO
) *PKCS7_get_signer_info(PKCS7
*p7
);
278 PKCS7_RECIP_INFO
*PKCS7_add_recipient(PKCS7
*p7
, X509
*x509
);
279 void PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO
*si
, EVP_PKEY
**pk
,
280 X509_ALGOR
**pdig
, X509_ALGOR
**psig
);
281 void PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO
*ri
, X509_ALGOR
**penc
);
282 int PKCS7_add_recipient_info(PKCS7
*p7
, PKCS7_RECIP_INFO
*ri
);
283 int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO
*p7i
, X509
*x509
);
284 int PKCS7_set_cipher(PKCS7
*p7
, const EVP_CIPHER
*cipher
);
285 int PKCS7_stream(unsigned char ***boundary
, PKCS7
*p7
);
287 PKCS7_ISSUER_AND_SERIAL
*PKCS7_get_issuer_and_serial(PKCS7
*p7
, int idx
);
288 ASN1_OCTET_STRING
*PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE
) *sk
);
289 int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO
*p7si
, int nid
, int type
,
291 int PKCS7_add_attribute(PKCS7_SIGNER_INFO
*p7si
, int nid
, int atrtype
,
293 ASN1_TYPE
*PKCS7_get_attribute(PKCS7_SIGNER_INFO
*si
, int nid
);
294 ASN1_TYPE
*PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO
*si
, int nid
);
295 int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO
*p7si
,
296 STACK_OF(X509_ATTRIBUTE
) *sk
);
297 int PKCS7_set_attributes(PKCS7_SIGNER_INFO
*p7si
,
298 STACK_OF(X509_ATTRIBUTE
) *sk
);
300 PKCS7
*PKCS7_sign(X509
*signcert
, EVP_PKEY
*pkey
, STACK_OF(X509
) *certs
,
301 BIO
*data
, int flags
);
302 PKCS7
*PKCS7_sign_with_libctx(X509
*signcert
, EVP_PKEY
*pkey
,
303 STACK_OF(X509
) *certs
, BIO
*data
, int flags
,
304 OPENSSL_CTX
*libctx
, const char *propq
);
306 PKCS7_SIGNER_INFO
*PKCS7_sign_add_signer(PKCS7
*p7
,
307 X509
*signcert
, EVP_PKEY
*pkey
,
308 const EVP_MD
*md
, int flags
);
310 int PKCS7_final(PKCS7
*p7
, BIO
*data
, int flags
);
311 int PKCS7_verify(PKCS7
*p7
, STACK_OF(X509
) *certs
, X509_STORE
*store
,
312 BIO
*indata
, BIO
*out
, int flags
);
313 STACK_OF(X509
) *PKCS7_get0_signers(PKCS7
*p7
, STACK_OF(X509
) *certs
,
315 PKCS7
*PKCS7_encrypt(STACK_OF(X509
) *certs
, BIO
*in
, const EVP_CIPHER
*cipher
,
317 PKCS7
*PKCS7_encrypt_with_libctx(STACK_OF(X509
) *certs
, BIO
*in
,
318 const EVP_CIPHER
*cipher
, int flags
,
319 OPENSSL_CTX
*libctx
, const char *propq
);
320 int PKCS7_decrypt(PKCS7
*p7
, EVP_PKEY
*pkey
, X509
*cert
, BIO
*data
,
323 int PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO
*si
,
324 STACK_OF(X509_ALGOR
) *cap
);
325 STACK_OF(X509_ALGOR
) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO
*si
);
326 int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR
) *sk
, int nid
, int arg
);
328 int PKCS7_add_attrib_content_type(PKCS7_SIGNER_INFO
*si
, ASN1_OBJECT
*coid
);
329 int PKCS7_add0_attrib_signing_time(PKCS7_SIGNER_INFO
*si
, ASN1_TIME
*t
);
330 int PKCS7_add1_attrib_digest(PKCS7_SIGNER_INFO
*si
,
331 const unsigned char *md
, int mdlen
);
333 int SMIME_write_PKCS7(BIO
*bio
, PKCS7
*p7
, BIO
*data
, int flags
);
334 PKCS7
*SMIME_read_PKCS7_ex(BIO
*bio
, BIO
**bcont
, PKCS7
**p7
);
335 PKCS7
*SMIME_read_PKCS7(BIO
*bio
, BIO
**bcont
);
337 BIO
*BIO_new_PKCS7(BIO
*out
, PKCS7
*p7
);