This commit was generated by cvs2svn to track changes on a CVS vendor
[openssl.git] / crypto / pkcs7 / pkcs7.h
1 /* crypto/pkcs7/pkcs7.h */
2 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #ifndef HEADER_PKCS7_H
60 #define HEADER_PKCS7_H
61
62 #ifdef  __cplusplus
63 extern "C" {
64 #endif
65
66 #include "bio.h"
67 #include "x509.h"
68
69 /*
70 Encryption_ID           DES-CBC
71 Digest_ID               MD5
72 Digest_Encryption_ID    rsaEncryption
73 Key_Encryption_ID       rsaEncryption
74 */
75
76 typedef struct pkcs7_issuer_and_serial_st
77         {
78         X509_NAME *issuer;
79         ASN1_INTEGER *serial;
80         } PKCS7_ISSUER_AND_SERIAL;
81
82 typedef struct pkcs7_signer_info_st
83         {
84         ASN1_INTEGER                    *version;       /* version 1 */
85         PKCS7_ISSUER_AND_SERIAL         *issuer_and_serial;
86         X509_ALGOR                      *digest_alg;
87         STACK /* X509_ATTRIBUTE */      *auth_attr;     /* [ 0 ] */
88         X509_ALGOR                      *digest_enc_alg;
89         ASN1_OCTET_STRING               *enc_digest;
90         STACK /* X509_ATTRIBUTE */      *unauth_attr;   /* [ 1 ] */
91
92         /* The private key to sign with */
93         EVP_PKEY                        *pkey;
94         } PKCS7_SIGNER_INFO;
95
96 typedef struct pkcs7_recip_info_st
97         {
98         ASN1_INTEGER                    *version;       /* version 0 */
99         PKCS7_ISSUER_AND_SERIAL         *issuer_and_serial;
100         X509_ALGOR                      *key_enc_algor;
101         ASN1_OCTET_STRING               *enc_key;
102         } PKCS7_RECIP_INFO;
103
104 typedef struct pkcs7_signed_st
105         {
106         ASN1_INTEGER                    *version;       /* version 1 */
107         STACK /* X509_ALGOR's */        *md_algs;       /* md used */
108         struct pkcs7_st                 *contents;
109         STACK /* X509 */                *cert;          /* [ 0 ] */
110         STACK /* X509_CRL */            *crl;           /* [ 1 ] */
111         STACK /* PKCS7_SIGNER_INFO */   *signer_info;
112         } PKCS7_SIGNED;
113 /* The above structure is very very similar to PKCS7_SIGN_ENVELOPE.
114  * How about merging the two */
115
116 typedef struct pkcs7_enc_content_st
117         {
118         ASN1_OBJECT                     *content_type;
119         X509_ALGOR                      *algorithm;
120         ASN1_OCTET_STRING               *enc_data;      /* [ 0 ] */
121         } PKCS7_ENC_CONTENT;
122
123 typedef struct pkcs7_enveloped_st
124         {
125         ASN1_INTEGER                    *version;       /* version 0 */
126         STACK /* PKCS7_RECIP_INFO */    *recipientinfo;
127         PKCS7_ENC_CONTENT               *enc_data;
128         } PKCS7_ENVELOPE;
129         
130 typedef struct pkcs7_signedandenveloped_st
131         {
132         ASN1_INTEGER                    *version;       /* version 1 */
133         STACK /* PKCS7_RECIP_INFO */    *recipientinfo;
134         STACK /* X509_ALGOR's */        *md_algs;       /* md used */
135         PKCS7_ENC_CONTENT               *enc_data;
136         STACK /* X509 */                *cert;          /* [ 0 ] */
137         STACK /* X509_CRL */            *crl;           /* [ 1 ] */
138         STACK /* PKCS7_SIGNER_INFO */   *signer_info;
139         } PKCS7_SIGN_ENVELOPE;
140
141 typedef struct pkcs7_digest_st
142         {
143         ASN1_INTEGER                    *version;       /* version 0 */
144         X509_ALGOR                      *md;            /* md used */
145         struct pkcs7_st                 *contents;
146         ASN1_OCTET_STRING               *digest;
147         } PKCS7_DIGEST;
148
149 typedef struct pkcs7_encrypted_st
150         {
151         ASN1_INTEGER                    *version;       /* version 0 */
152         PKCS7_ENC_CONTENT               *enc_data;
153         } PKCS7_ENCRYPT;
154
155 typedef struct pkcs7_st
156         {
157         /* The following is non NULL if it contains ASN1 encoding of
158          * this structure */
159         unsigned char *asn1;
160         long length;
161
162 #define PKCS7_S_HEADER  0
163 #define PKCS7_S_BODY    1
164 #define PKCS7_S_TAIL    2
165         int state; /* used during processing */
166
167         int detached;
168
169         ASN1_OBJECT *type;
170         /* content as defined by the type */
171         /* all encryption/message digests are applied to the 'contents',
172          * leaving out the 'type' field. */
173         union   {
174                 char *ptr;
175
176                 /* NID_pkcs7_data */
177                 ASN1_OCTET_STRING *data;
178
179                 /* NID_pkcs7_signed */
180                 PKCS7_SIGNED *sign;
181
182                 /* NID_pkcs7_enveloped */
183                 PKCS7_ENVELOPE *enveloped;
184
185                 /* NID_pkcs7_signedAndEnveloped */
186                 PKCS7_SIGN_ENVELOPE *signed_and_enveloped;
187
188                 /* NID_pkcs7_digest */
189                 PKCS7_DIGEST *digest;
190
191                 /* NID_pkcs7_encrypted */
192                 PKCS7_ENCRYPT *encrypted;
193                 } d;
194         } PKCS7;
195
196 #define PKCS7_OP_SET_DETACHED_SIGNATURE 1
197 #define PKCS7_OP_GET_DETACHED_SIGNATURE 2
198
199 #define PKCS7_type_is_signed(a) (OBJ_obj2nid((a)->type) == NID_pkcs7_signed)
200 #define PKCS7_type_is_data(a)   (OBJ_obj2nid((a)->type) == NID_pkcs7_data)
201
202 #define PKCS7_set_detached(p,v) \
203                 PKCS7_ctrl(p,PKCS7_OP_SET_DETACHED_SIGNATURE,v,NULL)
204 #define PKCS7_get_detached(p) \
205                 PKCS7_ctrl(p,PKCS7_OP_GET_DETACHED_SIGNATURE,0,NULL)
206
207 #ifndef NOPROTO
208 PKCS7_ISSUER_AND_SERIAL *PKCS7_ISSUER_AND_SERIAL_new(void );
209 void                    PKCS7_ISSUER_AND_SERIAL_free(
210                                 PKCS7_ISSUER_AND_SERIAL *a);
211 int                     i2d_PKCS7_ISSUER_AND_SERIAL(
212                                 PKCS7_ISSUER_AND_SERIAL *a,unsigned char **pp);
213 PKCS7_ISSUER_AND_SERIAL *d2i_PKCS7_ISSUER_AND_SERIAL(
214                                 PKCS7_ISSUER_AND_SERIAL **a,
215                                 unsigned char **pp, long length);
216
217 int PKCS7_ISSUER_AND_SERIAL_digest(PKCS7_ISSUER_AND_SERIAL *data,EVP_MD *type,
218         unsigned char *md,unsigned int *len);
219
220 PKCS7_SIGNER_INFO       *PKCS7_SIGNER_INFO_new(void);
221 void                    PKCS7_SIGNER_INFO_free(PKCS7_SIGNER_INFO *a);
222 int                     i2d_PKCS7_SIGNER_INFO(PKCS7_SIGNER_INFO *a,
223                                 unsigned char **pp);
224 PKCS7_SIGNER_INFO       *d2i_PKCS7_SIGNER_INFO(PKCS7_SIGNER_INFO **a,
225                                 unsigned char **pp,long length);
226
227 PKCS7_RECIP_INFO        *PKCS7_RECIP_INFO_new(void);
228 void                    PKCS7_RECIP_INFO_free(PKCS7_RECIP_INFO *a);
229 int                     i2d_PKCS7_RECIP_INFO(PKCS7_RECIP_INFO *a,
230                                 unsigned char **pp);
231 PKCS7_RECIP_INFO        *d2i_PKCS7_RECIP_INFO(PKCS7_RECIP_INFO **a,
232                                 unsigned char **pp,long length);
233
234 PKCS7_SIGNED            *PKCS7_SIGNED_new(void);
235 void                    PKCS7_SIGNED_free(PKCS7_SIGNED *a);
236 int                     i2d_PKCS7_SIGNED(PKCS7_SIGNED *a,
237                                 unsigned char **pp);
238 PKCS7_SIGNED            *d2i_PKCS7_SIGNED(PKCS7_SIGNED **a,
239                                 unsigned char **pp,long length);
240
241 PKCS7_ENC_CONTENT       *PKCS7_ENC_CONTENT_new(void);
242 void                    PKCS7_ENC_CONTENT_free(PKCS7_ENC_CONTENT *a);
243 int                     i2d_PKCS7_ENC_CONTENT(PKCS7_ENC_CONTENT *a,
244                                 unsigned char **pp);
245 PKCS7_ENC_CONTENT       *d2i_PKCS7_ENC_CONTENT(PKCS7_ENC_CONTENT **a,
246                                 unsigned char **pp,long length);
247
248 PKCS7_ENVELOPE          *PKCS7_ENVELOPE_new(void);
249 void                    PKCS7_ENVELOPE_free(PKCS7_ENVELOPE *a);
250 int                     i2d_PKCS7_ENVELOPE(PKCS7_ENVELOPE *a,
251                                 unsigned char **pp);
252 PKCS7_ENVELOPE          *d2i_PKCS7_ENVELOPE(PKCS7_ENVELOPE **a,
253                                 unsigned char **pp,long length);
254
255 PKCS7_SIGN_ENVELOPE     *PKCS7_SIGN_ENVELOPE_new(void);
256 void                    PKCS7_SIGN_ENVELOPE_free(PKCS7_SIGN_ENVELOPE *a);
257 int                     i2d_PKCS7_SIGN_ENVELOPE(PKCS7_SIGN_ENVELOPE *a,
258                                 unsigned char **pp);
259 PKCS7_SIGN_ENVELOPE     *d2i_PKCS7_SIGN_ENVELOPE(PKCS7_SIGN_ENVELOPE **a,
260                                 unsigned char **pp,long length);
261
262 PKCS7_DIGEST            *PKCS7_DIGEST_new(void);
263 void                    PKCS7_DIGEST_free(PKCS7_DIGEST *a);
264 int                     i2d_PKCS7_DIGEST(PKCS7_DIGEST *a,
265                                 unsigned char **pp);
266 PKCS7_DIGEST            *d2i_PKCS7_DIGEST(PKCS7_DIGEST **a,
267                                 unsigned char **pp,long length);
268
269 PKCS7_ENCRYPT           *PKCS7_ENCRYPT_new(void);
270 void                    PKCS7_ENCRYPT_free(PKCS7_ENCRYPT *a);
271 int                     i2d_PKCS7_ENCRYPT(PKCS7_ENCRYPT *a,
272                                 unsigned char **pp);
273 PKCS7_ENCRYPT           *d2i_PKCS7_ENCRYPT(PKCS7_ENCRYPT **a,
274                                 unsigned char **pp,long length);
275
276 PKCS7                   *PKCS7_new(void);
277 void                    PKCS7_free(PKCS7 *a);
278 void                    PKCS7_content_free(PKCS7 *a);
279 int                     i2d_PKCS7(PKCS7 *a,
280                                 unsigned char **pp);
281 PKCS7                   *d2i_PKCS7(PKCS7 **a,
282                                 unsigned char **pp,long length);
283
284 void ERR_load_PKCS7_strings(void);
285
286 #ifndef WIN16
287 PKCS7 *d2i_PKCS7_fp(FILE *fp,PKCS7 *p7);
288 int i2d_PKCS7_fp(FILE *fp,PKCS7 *p7);
289 #endif
290 PKCS7 *PKCS7_dup(PKCS7 *p7);
291 PKCS7 *d2i_PKCS7_bio(BIO *bp,PKCS7 *p7);
292 int i2d_PKCS7_bio(BIO *bp,PKCS7 *p7);
293
294 long PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg);
295
296 int PKCS7_set_type(PKCS7 *p7, int type);
297 int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data);
298 int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey,
299         EVP_MD *dgst);
300 int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *p7i);
301 int PKCS7_add_certificate(PKCS7 *p7, X509 *x509);
302 int PKCS7_add_crl(PKCS7 *p7, X509_CRL *x509);
303 int PKCS7_content_new(PKCS7 *p7, int nid);
304 int PKCS7_dataSign(PKCS7 *p7, BIO *bio);
305 int PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx,
306         BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si); 
307
308 BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio);
309 /*int PKCS7_DataFinal(PKCS7 *p7, BIO *bio); */
310
311 PKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509,
312         EVP_PKEY *pkey, EVP_MD *dgst);
313 X509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si);
314 STACK *PKCS7_get_signer_info(PKCS7 *p7);
315
316 #else
317
318 PKCS7_ISSUER_AND_SERIAL *PKCS7_ISSUER_AND_SERIAL_new();
319 void                    PKCS7_ISSUER_AND_SERIAL_free();
320 int                     i2d_PKCS7_ISSUER_AND_SERIAL();
321 PKCS7_ISSUER_AND_SERIAL *d2i_PKCS7_ISSUER_AND_SERIAL();
322
323 int                     PKCS7_ISSUER_AND_SERIAL_digest();
324
325 PKCS7_SIGNER_INFO       *PKCS7_SIGNER_INFO_new();
326 void                    PKCS7_SIGNER_INFO_free();
327 int                     i2d_PKCS7_SIGNER_INFO();
328 PKCS7_SIGNER_INFO       *d2i_PKCS7_SIGNER_INFO();
329 PKCS7_RECIP_INFO        *PKCS7_RECIP_INFO_new();
330 void                    PKCS7_RECIP_INFO_free();
331 int                     i2d_PKCS7_RECIP_INFO();
332 PKCS7_RECIP_INFO        *d2i_PKCS7_RECIP_INFO();
333 PKCS7_SIGNED            *PKCS7_SIGNED_new();
334 void                    PKCS7_SIGNED_free();
335 int                     i2d_PKCS7_SIGNED();
336 PKCS7_SIGNED            *d2i_PKCS7_SIGNED();
337 PKCS7_ENC_CONTENT       *PKCS7_ENC_CONTENT_new();
338 void                    PKCS7_ENC_CONTENT_free();
339 int                     i2d_PKCS7_ENC_CONTENT();
340 PKCS7_ENC_CONTENT       *d2i_PKCS7_ENC_CONTENT();
341 PKCS7_ENVELOPE          *PKCS7_ENVELOPE_new();
342 void                    PKCS7_ENVELOPE_free();
343 int                     i2d_PKCS7_ENVELOPE();
344 PKCS7_ENVELOPE          *d2i_PKCS7_ENVELOPE();
345 PKCS7_SIGN_ENVELOPE     *PKCS7_SIGN_ENVELOPE_new();
346 void                    PKCS7_SIGN_ENVELOPE_free();
347 int                     i2d_PKCS7_SIGN_ENVELOPE();
348 PKCS7_SIGN_ENVELOPE     *d2i_PKCS7_SIGN_ENVELOPE();
349 PKCS7_DIGEST            *PKCS7_DIGEST_new();
350 void                    PKCS7_DIGEST_free();
351 int                     i2d_PKCS7_DIGEST();
352 PKCS7_DIGEST            *d2i_PKCS7_DIGEST();
353 PKCS7_ENCRYPT           *PKCS7_ENCRYPT_new();
354 void                    PKCS7_ENCRYPT_free();
355 int                     i2d_PKCS7_ENCRYPT();
356 PKCS7_ENCRYPT           *d2i_PKCS7_ENCRYPT();
357 PKCS7                   *PKCS7_new();
358 void                    PKCS7_free();
359 void                    PKCS7_content_free();
360 int                     i2d_PKCS7();
361 PKCS7                   *d2i_PKCS7();
362
363 void ERR_load_PKCS7_strings();
364
365 #ifndef WIN16
366 PKCS7 *d2i_PKCS7_fp();
367 int i2d_PKCS7_fp();
368 #endif
369 PKCS7 *PKCS7_dup();
370 PKCS7 *d2i_PKCS7_bio();
371 int i2d_PKCS7_bio();
372 long PKCS7_ctrl();
373 int PKCS7_set_type();
374 int PKCS7_set_content();
375 int PKCS7_SIGNER_INFO_set();
376 int PKCS7_add_signer();
377 int PKCS7_add_certificate();
378 int PKCS7_add_crl();
379 int PKCS7_content_new();
380 int PKCS7_dataSign();
381 int PKCS7_dataVerify();
382 BIO *PKCS7_dataInit();
383 PKCS7_SIGNER_INFO *PKCS7_add_signature();
384 X509 *PKCS7_cert_from_signer_info();
385 STACK *PKCS7_get_signer_info();
386
387 #endif
388
389 /* BEGIN ERROR CODES */
390 /* Error codes for the PKCS7 functions. */
391
392 /* Function codes. */
393 #define PKCS7_F_PKCS7_ADD_SIGNER                         100
394 #define PKCS7_F_PKCS7_CTRL                               101
395 #define PKCS7_F_PKCS7_DATAFINAL                          102
396 #define PKCS7_F_PKCS7_DATAINIT                           103
397 #define PKCS7_F_PKCS7_DATAVERIFY                         104
398 #define PKCS7_F_PKCS7_SET_CONTENT                        105
399 #define PKCS7_F_PKCS7_SET_TYPE                           106
400
401 /* Reason codes. */
402 #define PKCS7_R_INTERNAL_ERROR                           100
403 #define PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE     101
404 #define PKCS7_R_SIGNATURE_FAILURE                        102
405 #define PKCS7_R_UNABLE_TO_FIND_CERTIFICATE               103
406 #define PKCS7_R_UNABLE_TO_FIND_MEM_BIO                   104
407 #define PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST            105
408 #define PKCS7_R_UNKNOWN_DIGEST_TYPE                      106
409 #define PKCS7_R_UNSUPPORTED_CONTENT_TYPE                 107
410 #define PKCS7_R_WRONG_CONTENT_TYPE                       108
411  
412 #ifdef  __cplusplus
413 }
414 #endif
415 #endif
416