Netware support.
[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 #ifndef OPENSSL_SYSNAME_NETWARE
64 #include <memory.h>
65 #endif
66 #include <stdio.h>
67
68 /* Highly experiemental PKCS#7 BIO support routines */
69
70 /* The usage is quite simple, initialize a PKCS7 structure,
71  * get a BIO from it then any data written through the BIO
72  * will end up translated to PKCS#7 format on the fly.
73  * The data is streamed out and does *not* need to be
74  * all held in memory at once.
75  *
76  * When the BIO is flushed the output is finalized and any
77  * signatures etc written out.
78  *
79  * The BIO is a 'proper' BIO and can handle non blocking I/O
80  * correctly.
81  *
82  * The usage is simple. The implementation is *not*...
83  */
84
85 /* BIO support data stored in the ASN1 BIO ex_arg */
86
87 typedef struct pkcs7_aux_st
88         {
89         /* PKCS7 structure this BIO refers to */
90         PKCS7 *p7;
91         /* Top of the BIO chain */
92         BIO *p7bio;
93         /* Output BIO */
94         BIO *out;
95         /* Boundary where content is inserted */
96         unsigned char **boundary;
97         /* DER buffer start */
98         unsigned char *derbuf;
99         } PKCS7_SUPPORT;
100
101 static int pkcs7_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
102 static int pkcs7_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg);
103 static int pkcs7_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
104 static int pkcs7_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg);
105
106 BIO *BIO_new_PKCS7(BIO *out, PKCS7 *p7) 
107         {
108         PKCS7_SUPPORT *p7aux = NULL;
109         BIO *p7bio = NULL;
110         BIO *asn_bio = NULL;
111         unsigned char **boundary;
112         p7aux = OPENSSL_malloc(sizeof(PKCS7_SUPPORT));
113         asn_bio = BIO_new(BIO_f_asn1());
114
115         /* ASN1 bio needs to be next to output BIO */
116
117         out = BIO_push(asn_bio, out);
118
119         if (!p7aux || !asn_bio || !out)
120                 goto err;
121
122         BIO_asn1_set_prefix(asn_bio, pkcs7_prefix, pkcs7_prefix_free);
123         BIO_asn1_set_suffix(asn_bio, pkcs7_suffix, pkcs7_suffix_free);
124
125         /* Now initialize BIO for PKCS#7 output */
126
127         p7bio = PKCS7_dataInit(p7, out);
128         if (!p7bio || !PKCS7_stream(&boundary, p7))
129                 goto err;
130
131         p7aux->p7 = p7;
132         p7aux->p7bio = p7bio;
133         p7aux->boundary = boundary;
134         p7aux->out = out;
135
136         BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, p7aux);
137
138         return p7bio;
139
140         err:
141         if (p7bio)
142                 BIO_free(p7bio);
143         if (asn_bio)
144                 BIO_free(asn_bio);
145         if (p7aux)
146                 OPENSSL_free(p7aux);
147         return NULL;
148         }
149
150 static int pkcs7_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
151         {
152         PKCS7_SUPPORT *p7aux;
153         unsigned char *p;
154         int derlen;
155
156         if (!parg)
157                 return 0;
158
159         p7aux = *(PKCS7_SUPPORT **)parg;
160
161         derlen = i2d_PKCS7_NDEF(p7aux->p7, NULL);
162         p = OPENSSL_malloc(derlen);
163         p7aux->derbuf = p;
164         *pbuf = p;
165         i2d_PKCS7_NDEF(p7aux->p7, &p);
166
167         if (!*p7aux->boundary)
168                 return 0;
169
170         *plen = *p7aux->boundary - *pbuf;
171
172         return 1;
173         }
174
175 static int pkcs7_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
176         {
177         PKCS7_SUPPORT *p7aux;
178
179         if (!parg)
180                 return 0;
181
182         p7aux = *(PKCS7_SUPPORT **)parg;
183
184         if (p7aux->derbuf)
185                 OPENSSL_free(p7aux->derbuf);
186
187         p7aux->derbuf = NULL;
188         *pbuf = NULL;
189         *plen = 0;
190         return 1;
191         }
192
193 static int pkcs7_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
194         {
195         PKCS7_SUPPORT **pp7aux = (PKCS7_SUPPORT **)parg;
196         if (!pkcs7_prefix_free(b, pbuf, plen, parg))
197                 return 0;
198         OPENSSL_free(*pp7aux);
199         *pp7aux = NULL;
200         return 1;
201         }
202
203 static int pkcs7_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
204         {
205         PKCS7_SUPPORT *p7aux;
206         unsigned char *p;
207         int derlen;
208
209         if (!parg)
210                 return 0;
211
212         p7aux = *(PKCS7_SUPPORT **)parg;
213
214         /* Finalize structures */
215         PKCS7_dataFinal(p7aux->p7, p7aux->p7bio);
216
217         derlen = i2d_PKCS7_NDEF(p7aux->p7, NULL);
218         p = OPENSSL_malloc(derlen);
219         p7aux->derbuf = p;
220         i2d_PKCS7_NDEF(p7aux->p7, &p);
221         if (!*p7aux->boundary)
222                 return 0;
223         *pbuf = *p7aux->boundary;
224         *plen = derlen - (*p7aux->boundary - p7aux->derbuf);
225
226         return 1;
227         }
228         
229