Spelling
[openssl.git] / include / openssl / pkcs7.h
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
10 #ifndef HEADER_PKCS7_H
11 # define HEADER_PKCS7_H
12
13 # include <openssl/asn1.h>
14 # include <openssl/bio.h>
15 # include <openssl/e_os2.h>
16
17 # include <openssl/symhacks.h>
18 # include <openssl/ossl_typ.h>
19
20 #ifdef  __cplusplus
21 extern "C" {
22 #endif
23
24 # ifdef OPENSSL_SYS_WIN32
25 /* Under Win32 these are defined in wincrypt.h */
26 #  undef PKCS7_ISSUER_AND_SERIAL
27 #  undef PKCS7_SIGNER_INFO
28 # endif
29
30 /*-
31 Encryption_ID           DES-CBC
32 Digest_ID               MD5
33 Digest_Encryption_ID    rsaEncryption
34 Key_Encryption_ID       rsaEncryption
35 */
36
37 typedef struct pkcs7_issuer_and_serial_st {
38     X509_NAME *issuer;
39     ASN1_INTEGER *serial;
40 } PKCS7_ISSUER_AND_SERIAL;
41
42 typedef struct pkcs7_signer_info_st {
43     ASN1_INTEGER *version;      /* version 1 */
44     PKCS7_ISSUER_AND_SERIAL *issuer_and_serial;
45     X509_ALGOR *digest_alg;
46     STACK_OF(X509_ATTRIBUTE) *auth_attr; /* [ 0 ] */
47     X509_ALGOR *digest_enc_alg;
48     ASN1_OCTET_STRING *enc_digest;
49     STACK_OF(X509_ATTRIBUTE) *unauth_attr; /* [ 1 ] */
50     /* The private key to sign with */
51     EVP_PKEY *pkey;
52 } PKCS7_SIGNER_INFO;
53
54 DEFINE_STACK_OF(PKCS7_SIGNER_INFO)
55
56 typedef struct pkcs7_recip_info_st {
57     ASN1_INTEGER *version;      /* version 0 */
58     PKCS7_ISSUER_AND_SERIAL *issuer_and_serial;
59     X509_ALGOR *key_enc_algor;
60     ASN1_OCTET_STRING *enc_key;
61     X509 *cert;                 /* get the pub-key from this */
62 } PKCS7_RECIP_INFO;
63
64 DEFINE_STACK_OF(PKCS7_RECIP_INFO)
65
66 typedef struct pkcs7_signed_st {
67     ASN1_INTEGER *version;      /* version 1 */
68     STACK_OF(X509_ALGOR) *md_algs; /* md used */
69     STACK_OF(X509) *cert;       /* [ 0 ] */
70     STACK_OF(X509_CRL) *crl;    /* [ 1 ] */
71     STACK_OF(PKCS7_SIGNER_INFO) *signer_info;
72     struct pkcs7_st *contents;
73 } PKCS7_SIGNED;
74 /*
75  * The above structure is very very similar to PKCS7_SIGN_ENVELOPE. How about
76  * merging the two
77  */
78
79 typedef struct pkcs7_enc_content_st {
80     ASN1_OBJECT *content_type;
81     X509_ALGOR *algorithm;
82     ASN1_OCTET_STRING *enc_data; /* [ 0 ] */
83     const EVP_CIPHER *cipher;
84 } PKCS7_ENC_CONTENT;
85
86 typedef struct pkcs7_enveloped_st {
87     ASN1_INTEGER *version;      /* version 0 */
88     STACK_OF(PKCS7_RECIP_INFO) *recipientinfo;
89     PKCS7_ENC_CONTENT *enc_data;
90 } PKCS7_ENVELOPE;
91
92 typedef struct pkcs7_signedandenveloped_st {
93     ASN1_INTEGER *version;      /* version 1 */
94     STACK_OF(X509_ALGOR) *md_algs; /* md used */
95     STACK_OF(X509) *cert;       /* [ 0 ] */
96     STACK_OF(X509_CRL) *crl;    /* [ 1 ] */
97     STACK_OF(PKCS7_SIGNER_INFO) *signer_info;
98     PKCS7_ENC_CONTENT *enc_data;
99     STACK_OF(PKCS7_RECIP_INFO) *recipientinfo;
100 } PKCS7_SIGN_ENVELOPE;
101
102 typedef struct pkcs7_digest_st {
103     ASN1_INTEGER *version;      /* version 0 */
104     X509_ALGOR *md;             /* md used */
105     struct pkcs7_st *contents;
106     ASN1_OCTET_STRING *digest;
107 } PKCS7_DIGEST;
108
109 typedef struct pkcs7_encrypted_st {
110     ASN1_INTEGER *version;      /* version 0 */
111     PKCS7_ENC_CONTENT *enc_data;
112 } PKCS7_ENCRYPT;
113
114 typedef struct pkcs7_st {
115     /*
116      * The following is non NULL if it contains ASN1 encoding of this
117      * structure
118      */
119     unsigned char *asn1;
120     long length;
121 # define PKCS7_S_HEADER  0
122 # define PKCS7_S_BODY    1
123 # define PKCS7_S_TAIL    2
124     int state;                  /* used during processing */
125     int detached;
126     ASN1_OBJECT *type;
127     /* content as defined by the type */
128     /*
129      * all encryption/message digests are applied to the 'contents', leaving
130      * out the 'type' field.
131      */
132     union {
133         char *ptr;
134         /* NID_pkcs7_data */
135         ASN1_OCTET_STRING *data;
136         /* NID_pkcs7_signed */
137         PKCS7_SIGNED *sign;
138         /* NID_pkcs7_enveloped */
139         PKCS7_ENVELOPE *enveloped;
140         /* NID_pkcs7_signedAndEnveloped */
141         PKCS7_SIGN_ENVELOPE *signed_and_enveloped;
142         /* NID_pkcs7_digest */
143         PKCS7_DIGEST *digest;
144         /* NID_pkcs7_encrypted */
145         PKCS7_ENCRYPT *encrypted;
146         /* Anything else */
147         ASN1_TYPE *other;
148     } d;
149 } PKCS7;
150
151 DEFINE_STACK_OF(PKCS7)
152
153 # define PKCS7_OP_SET_DETACHED_SIGNATURE 1
154 # define PKCS7_OP_GET_DETACHED_SIGNATURE 2
155
156 # define PKCS7_get_signed_attributes(si) ((si)->auth_attr)
157 # define PKCS7_get_attributes(si)        ((si)->unauth_attr)
158
159 # define PKCS7_type_is_signed(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_signed)
160 # define PKCS7_type_is_encrypted(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_encrypted)
161 # define PKCS7_type_is_enveloped(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_enveloped)
162 # define PKCS7_type_is_signedAndEnveloped(a) \
163                 (OBJ_obj2nid((a)->type) == NID_pkcs7_signedAndEnveloped)
164 # define PKCS7_type_is_data(a)   (OBJ_obj2nid((a)->type) == NID_pkcs7_data)
165 # define PKCS7_type_is_digest(a)   (OBJ_obj2nid((a)->type) == NID_pkcs7_digest)
166
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
172 # define PKCS7_is_detached(p7) (PKCS7_type_is_signed(p7) && PKCS7_get_detached(p7))
173
174 /* S/MIME related flags */
175
176 # define PKCS7_TEXT              0x1
177 # define PKCS7_NOCERTS           0x2
178 # define PKCS7_NOSIGS            0x4
179 # define PKCS7_NOCHAIN           0x8
180 # define PKCS7_NOINTERN          0x10
181 # define PKCS7_NOVERIFY          0x20
182 # define PKCS7_DETACHED          0x40
183 # define PKCS7_BINARY            0x80
184 # define PKCS7_NOATTR            0x100
185 # define PKCS7_NOSMIMECAP        0x200
186 # define PKCS7_NOOLDMIMETYPE     0x400
187 # define PKCS7_CRLFEOL           0x800
188 # define PKCS7_STREAM            0x1000
189 # define PKCS7_NOCRL             0x2000
190 # define PKCS7_PARTIAL           0x4000
191 # define PKCS7_REUSE_DIGEST      0x8000
192 # define PKCS7_NO_DUAL_CONTENT   0x10000
193
194 /* Flags: for compatibility with older code */
195
196 # define SMIME_TEXT      PKCS7_TEXT
197 # define SMIME_NOCERTS   PKCS7_NOCERTS
198 # define SMIME_NOSIGS    PKCS7_NOSIGS
199 # define SMIME_NOCHAIN   PKCS7_NOCHAIN
200 # define SMIME_NOINTERN  PKCS7_NOINTERN
201 # define SMIME_NOVERIFY  PKCS7_NOVERIFY
202 # define SMIME_DETACHED  PKCS7_DETACHED
203 # define SMIME_BINARY    PKCS7_BINARY
204 # define SMIME_NOATTR    PKCS7_NOATTR
205
206 /* CRLF ASCII canonicalisation */
207 # define SMIME_ASCIICRLF         0x80000
208
209 DECLARE_ASN1_FUNCTIONS(PKCS7_ISSUER_AND_SERIAL)
210
211 int PKCS7_ISSUER_AND_SERIAL_digest(PKCS7_ISSUER_AND_SERIAL *data,
212                                    const EVP_MD *type, unsigned char *md,
213                                    unsigned int *len);
214 # ifndef OPENSSL_NO_STDIO
215 PKCS7 *d2i_PKCS7_fp(FILE *fp, PKCS7 **p7);
216 int i2d_PKCS7_fp(FILE *fp, PKCS7 *p7);
217 # endif
218 PKCS7 *PKCS7_dup(PKCS7 *p7);
219 PKCS7 *d2i_PKCS7_bio(BIO *bp, PKCS7 **p7);
220 int i2d_PKCS7_bio(BIO *bp, PKCS7 *p7);
221 int i2d_PKCS7_bio_stream(BIO *out, PKCS7 *p7, BIO *in, int flags);
222 int PEM_write_bio_PKCS7_stream(BIO *out, PKCS7 *p7, BIO *in, int flags);
223
224 DECLARE_ASN1_FUNCTIONS(PKCS7_SIGNER_INFO)
225 DECLARE_ASN1_FUNCTIONS(PKCS7_RECIP_INFO)
226 DECLARE_ASN1_FUNCTIONS(PKCS7_SIGNED)
227 DECLARE_ASN1_FUNCTIONS(PKCS7_ENC_CONTENT)
228 DECLARE_ASN1_FUNCTIONS(PKCS7_ENVELOPE)
229 DECLARE_ASN1_FUNCTIONS(PKCS7_SIGN_ENVELOPE)
230 DECLARE_ASN1_FUNCTIONS(PKCS7_DIGEST)
231 DECLARE_ASN1_FUNCTIONS(PKCS7_ENCRYPT)
232 DECLARE_ASN1_FUNCTIONS(PKCS7)
233
234 DECLARE_ASN1_ITEM(PKCS7_ATTR_SIGN)
235 DECLARE_ASN1_ITEM(PKCS7_ATTR_VERIFY)
236
237 DECLARE_ASN1_NDEF_FUNCTION(PKCS7)
238 DECLARE_ASN1_PRINT_FUNCTION(PKCS7)
239
240 long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg);
241
242 int PKCS7_set_type(PKCS7 *p7, int type);
243 int PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other);
244 int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data);
245 int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey,
246                           const EVP_MD *dgst);
247 int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si);
248 int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *p7i);
249 int PKCS7_add_certificate(PKCS7 *p7, X509 *x509);
250 int PKCS7_add_crl(PKCS7 *p7, X509_CRL *x509);
251 int PKCS7_content_new(PKCS7 *p7, int nid);
252 int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx,
253                      BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si);
254 int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si,
255                           X509 *x509);
256
257 BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio);
258 int PKCS7_dataFinal(PKCS7 *p7, BIO *bio);
259 BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert);
260
261 PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509,
262                                        EVP_PKEY *pkey, const EVP_MD *dgst);
263 X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si);
264 int PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md);
265 STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7);
266
267 PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509);
268 void PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk,
269                                  X509_ALGOR **pdig, X509_ALGOR **psig);
270 void PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc);
271 int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri);
272 int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509);
273 int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher);
274 int PKCS7_stream(unsigned char ***boundary, PKCS7 *p7);
275
276 PKCS7_ISSUER_AND_SERIAL *PKCS7_get_issuer_and_serial(PKCS7 *p7, int idx);
277 ASN1_OCTET_STRING *PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE) *sk);
278 int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int type,
279                                void *data);
280 int PKCS7_add_attribute(PKCS7_SIGNER_INFO *p7si, int nid, int atrtype,
281                         void *value);
282 ASN1_TYPE *PKCS7_get_attribute(PKCS7_SIGNER_INFO *si, int nid);
283 ASN1_TYPE *PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO *si, int nid);
284 int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO *p7si,
285                                 STACK_OF(X509_ATTRIBUTE) *sk);
286 int PKCS7_set_attributes(PKCS7_SIGNER_INFO *p7si,
287                          STACK_OF(X509_ATTRIBUTE) *sk);
288
289 PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
290                   BIO *data, int flags);
291
292 PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7,
293                                          X509 *signcert, EVP_PKEY *pkey,
294                                          const EVP_MD *md, int flags);
295
296 int PKCS7_final(PKCS7 *p7, BIO *data, int flags);
297 int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
298                  BIO *indata, BIO *out, int flags);
299 STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs,
300                                    int flags);
301 PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
302                      int flags);
303 int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data,
304                   int flags);
305
306 int PKCS7_add_attrib_smimecap(PKCS7_SIGNER_INFO *si,
307                               STACK_OF(X509_ALGOR) *cap);
308 STACK_OF(X509_ALGOR) *PKCS7_get_smimecap(PKCS7_SIGNER_INFO *si);
309 int PKCS7_simple_smimecap(STACK_OF(X509_ALGOR) *sk, int nid, int arg);
310
311 int PKCS7_add_attrib_content_type(PKCS7_SIGNER_INFO *si, ASN1_OBJECT *coid);
312 int PKCS7_add0_attrib_signing_time(PKCS7_SIGNER_INFO *si, ASN1_TIME *t);
313 int PKCS7_add1_attrib_digest(PKCS7_SIGNER_INFO *si,
314                              const unsigned char *md, int mdlen);
315
316 int SMIME_write_PKCS7(BIO *bio, PKCS7 *p7, BIO *data, int flags);
317 PKCS7 *SMIME_read_PKCS7(BIO *bio, BIO **bcont);
318
319 BIO *BIO_new_PKCS7(BIO *out, PKCS7 *p7);
320
321 /* BEGIN ERROR CODES */
322 /*
323  * The following lines are auto generated by the script mkerr.pl. Any changes
324  * made after this point may be overwritten when the script is next run.
325  */
326
327 void ERR_load_PKCS7_strings(void);
328
329 /* Error codes for the PKCS7 functions. */
330
331 /* Function codes. */
332 # define PKCS7_F_DO_PKCS7_SIGNED_ATTRIB                   136
333 # define PKCS7_F_PKCS7_ADD0_ATTRIB_SIGNING_TIME           135
334 # define PKCS7_F_PKCS7_ADD_ATTRIB_SMIMECAP                118
335 # define PKCS7_F_PKCS7_ADD_CERTIFICATE                    100
336 # define PKCS7_F_PKCS7_ADD_CRL                            101
337 # define PKCS7_F_PKCS7_ADD_RECIPIENT_INFO                 102
338 # define PKCS7_F_PKCS7_ADD_SIGNATURE                      131
339 # define PKCS7_F_PKCS7_ADD_SIGNER                         103
340 # define PKCS7_F_PKCS7_BIO_ADD_DIGEST                     125
341 # define PKCS7_F_PKCS7_COPY_EXISTING_DIGEST               138
342 # define PKCS7_F_PKCS7_CTRL                               104
343 # define PKCS7_F_PKCS7_DATADECODE                         112
344 # define PKCS7_F_PKCS7_DATAFINAL                          128
345 # define PKCS7_F_PKCS7_DATAINIT                           105
346 # define PKCS7_F_PKCS7_DATAVERIFY                         107
347 # define PKCS7_F_PKCS7_DECRYPT                            114
348 # define PKCS7_F_PKCS7_DECRYPT_RINFO                      133
349 # define PKCS7_F_PKCS7_ENCODE_RINFO                       132
350 # define PKCS7_F_PKCS7_ENCRYPT                            115
351 # define PKCS7_F_PKCS7_FINAL                              134
352 # define PKCS7_F_PKCS7_FIND_DIGEST                        127
353 # define PKCS7_F_PKCS7_GET0_SIGNERS                       124
354 # define PKCS7_F_PKCS7_RECIP_INFO_SET                     130
355 # define PKCS7_F_PKCS7_SET_CIPHER                         108
356 # define PKCS7_F_PKCS7_SET_CONTENT                        109
357 # define PKCS7_F_PKCS7_SET_DIGEST                         126
358 # define PKCS7_F_PKCS7_SET_TYPE                           110
359 # define PKCS7_F_PKCS7_SIGN                               116
360 # define PKCS7_F_PKCS7_SIGNATUREVERIFY                    113
361 # define PKCS7_F_PKCS7_SIGNER_INFO_SET                    129
362 # define PKCS7_F_PKCS7_SIGNER_INFO_SIGN                   139
363 # define PKCS7_F_PKCS7_SIGN_ADD_SIGNER                    137
364 # define PKCS7_F_PKCS7_SIMPLE_SMIMECAP                    119
365 # define PKCS7_F_PKCS7_VERIFY                             117
366
367 /* Reason codes. */
368 # define PKCS7_R_CERTIFICATE_VERIFY_ERROR                 117
369 # define PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER          144
370 # define PKCS7_R_CIPHER_NOT_INITIALIZED                   116
371 # define PKCS7_R_CONTENT_AND_DATA_PRESENT                 118
372 # define PKCS7_R_CTRL_ERROR                               152
373 # define PKCS7_R_DECRYPT_ERROR                            119
374 # define PKCS7_R_DIGEST_FAILURE                           101
375 # define PKCS7_R_ENCRYPTION_CTRL_FAILURE                  149
376 # define PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE 150
377 # define PKCS7_R_ERROR_ADDING_RECIPIENT                   120
378 # define PKCS7_R_ERROR_SETTING_CIPHER                     121
379 # define PKCS7_R_INVALID_NULL_POINTER                     143
380 # define PKCS7_R_INVALID_SIGNED_DATA_TYPE                 155
381 # define PKCS7_R_NO_CONTENT                               122
382 # define PKCS7_R_NO_DEFAULT_DIGEST                        151
383 # define PKCS7_R_NO_MATCHING_DIGEST_TYPE_FOUND            154
384 # define PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE         115
385 # define PKCS7_R_NO_SIGNATURES_ON_DATA                    123
386 # define PKCS7_R_NO_SIGNERS                               142
387 # define PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE     104
388 # define PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR                124
389 # define PKCS7_R_PKCS7_ADD_SIGNER_ERROR                   153
390 # define PKCS7_R_PKCS7_DATASIGN                           145
391 # define PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE   127
392 # define PKCS7_R_SIGNATURE_FAILURE                        105
393 # define PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND             128
394 # define PKCS7_R_SIGNING_CTRL_FAILURE                     147
395 # define PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE  148
396 # define PKCS7_R_SMIME_TEXT_ERROR                         129
397 # define PKCS7_R_UNABLE_TO_FIND_CERTIFICATE               106
398 # define PKCS7_R_UNABLE_TO_FIND_MEM_BIO                   107
399 # define PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST            108
400 # define PKCS7_R_UNKNOWN_DIGEST_TYPE                      109
401 # define PKCS7_R_UNKNOWN_OPERATION                        110
402 # define PKCS7_R_UNSUPPORTED_CIPHER_TYPE                  111
403 # define PKCS7_R_UNSUPPORTED_CONTENT_TYPE                 112
404 # define PKCS7_R_WRONG_CONTENT_TYPE                       113
405 # define PKCS7_R_WRONG_PKCS7_TYPE                         114
406
407 # ifdef  __cplusplus
408 }
409 # endif
410 #endif