Update smime utility to support streaming for -encrypt and -sign -nodetach
[openssl.git] / crypto / pkcs7 / bio_pk7.c
1 /* bio_pk7.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3  * project.
4  */
5 /* ====================================================================
6  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include <openssl/asn1.h>
60 #include <openssl/pkcs7.h>
61 #include <openssl/bio.h>
62
63 #include <memory.h>
64 #include <stdio.h>
65
66 /* Highly experiemental PKCS#7 BIO support routines */
67
68 /* The usage is quite simple, initialize a PKCS7 structure,
69  * get a BIO from it then any data written through the BIO
70  * will end up translated to PKCS#7 format on the fly.
71  * The data is streamed out and does *not* need to be
72  * all held in memory at once.
73  *
74  * When the BIO is flushed the output is finalized and any
75  * signatures etc written out.
76  *
77  * The BIO is a 'proper' BIO and can handle non blocking I/O
78  * correctly.
79  *
80  * The usage is simple. The implementation is *not*...
81  */
82
83 /* BIO support data stored in the ASN1 BIO ex_arg */
84
85 typedef struct pkcs7_aux_st
86         {
87         /* PKCS7 structure this BIO refers to */
88         PKCS7 *p7;
89         /* Top of the BIO chain */
90         BIO *p7bio;
91         /* Output BIO */
92         BIO *out;
93         /* Boundary where content is inserted */
94         unsigned char **boundary;
95         /* DER buffer start */
96         unsigned char *derbuf;
97         } PKCS7_SUPPORT;
98
99 static int pkcs7_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
100 static int pkcs7_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg);
101 static int pkcs7_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
102 static int pkcs7_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg);
103
104 BIO *BIO_new_PKCS7(BIO *out, PKCS7 *p7) 
105         {
106         PKCS7_SUPPORT *p7aux = NULL;
107         BIO *p7bio = NULL;
108         BIO *asn_bio = NULL;
109         unsigned char **boundary;
110         p7aux = OPENSSL_malloc(sizeof(PKCS7_SUPPORT));
111         asn_bio = BIO_new(BIO_f_asn1());
112
113         /* ASN1 bio needs to be next to output BIO */
114
115         out = BIO_push(asn_bio, out);
116
117         BIO_asn1_set_prefix(asn_bio, pkcs7_prefix, pkcs7_prefix_free);
118         BIO_asn1_set_suffix(asn_bio, pkcs7_suffix, pkcs7_suffix_free);
119
120         /* Now initialize BIO for PKCS#7 output */
121
122         p7bio = PKCS7_dataInit(p7, out);
123         PKCS7_stream(&boundary, p7);
124
125         p7aux->p7 = p7;
126         p7aux->p7bio = p7bio;
127         p7aux->boundary = boundary;
128         p7aux->out = out;
129
130         BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, p7aux);
131
132         return p7bio;
133
134         }
135
136 static int pkcs7_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
137         {
138         PKCS7_SUPPORT *p7aux;
139         unsigned char *p;
140         int derlen;
141
142         if (!parg)
143                 return 0;
144
145         p7aux = *(PKCS7_SUPPORT **)parg;
146
147         derlen = i2d_PKCS7_NDEF(p7aux->p7, NULL);
148         p = OPENSSL_malloc(derlen);
149         p7aux->derbuf = p;
150         *pbuf = p;
151         i2d_PKCS7_NDEF(p7aux->p7, &p);
152
153         if (!*p7aux->boundary)
154                 return 0;
155
156         *plen = *p7aux->boundary - *pbuf;
157
158         return 1;
159         }
160
161 static int pkcs7_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
162         {
163         PKCS7_SUPPORT *p7aux;
164
165         if (!parg)
166                 return 0;
167
168         p7aux = *(PKCS7_SUPPORT **)parg;
169
170         if (p7aux->derbuf)
171                 OPENSSL_free(p7aux->derbuf);
172
173         p7aux->derbuf = NULL;
174         *pbuf = NULL;
175         *plen = 0;
176         return 1;
177         }
178
179 static int pkcs7_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
180         {
181         PKCS7_SUPPORT **pp7aux = (PKCS7_SUPPORT **)parg;
182         if (!pkcs7_prefix_free(b, pbuf, plen, parg))
183                 return 0;
184         OPENSSL_free(*pp7aux);
185         *pp7aux = NULL;
186         return 1;
187         }
188
189 static int pkcs7_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
190         {
191         PKCS7_SUPPORT *p7aux;
192         unsigned char *p;
193         int derlen;
194
195         if (!parg)
196                 return 0;
197
198         p7aux = *(PKCS7_SUPPORT **)parg;
199
200         /* Finalize structures */
201         PKCS7_dataFinal(p7aux->p7, p7aux->p7bio);
202
203         derlen = i2d_PKCS7_NDEF(p7aux->p7, NULL);
204         p = OPENSSL_malloc(derlen);
205         p7aux->derbuf = p;
206         i2d_PKCS7_NDEF(p7aux->p7, &p);
207         if (!*p7aux->boundary)
208                 return 0;
209         *pbuf = *p7aux->boundary;
210         *plen = derlen - (*p7aux->boundary - p7aux->derbuf);
211
212         return 1;
213         }
214         
215