]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/pkcs7/bio_pk7.c
Experimental streaming PKCS#7 support.
[thirdparty/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_psfix_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
103 BIO *BIO_new_PKCS7(BIO *out, PKCS7 *p7)
104 {
105 PKCS7_SUPPORT *p7aux = NULL;
106 BIO *p7bio = NULL;
107 BIO *asn_bio = NULL;
108 unsigned char **boundary;
109 p7aux = OPENSSL_malloc(sizeof(PKCS7_SUPPORT));
110 asn_bio = BIO_new(BIO_f_asn1());
111
112 /* ASN1 bio needs to be next to output BIO */
113
114 out = BIO_push(asn_bio, out);
115
116 BIO_asn1_set_prefix(asn_bio, pkcs7_prefix, pkcs7_psfix_free);
117 BIO_asn1_set_suffix(asn_bio, pkcs7_suffix, pkcs7_psfix_free);
118
119 /* Now initialize BIO for PKCS#7 output */
120
121 p7bio = PKCS7_dataInit(p7, out);
122 PKCS7_stream(&boundary, p7);
123
124 p7aux->p7 = p7;
125 p7aux->p7bio = p7bio;
126 p7aux->boundary = boundary;
127 p7aux->out = out;
128
129 BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, p7aux);
130
131 return p7bio;
132
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 *plen = *p7aux->boundary - *pbuf;
154
155 return 1;
156 }
157
158 static int pkcs7_psfix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
159 {
160 PKCS7_SUPPORT *p7aux;
161
162 if (!parg)
163 return 0;
164
165 p7aux = *(PKCS7_SUPPORT **)parg;
166
167 if (p7aux->derbuf)
168 OPENSSL_free(p7aux->derbuf);
169
170 p7aux->derbuf = NULL;
171 *pbuf = NULL;
172 *plen = 0;
173 return 1;
174 }
175
176 static int pkcs7_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
177 {
178 PKCS7_SUPPORT *p7aux;
179 unsigned char *p;
180 int derlen;
181
182 if (!parg)
183 return 0;
184
185 p7aux = *(PKCS7_SUPPORT **)parg;
186
187 /* Finalize structures */
188 PKCS7_dataFinal(p7aux->p7, p7aux->p7bio);
189
190 derlen = i2d_PKCS7_NDEF(p7aux->p7, NULL);
191 p = OPENSSL_malloc(derlen);
192 p7aux->derbuf = p;
193 i2d_PKCS7_NDEF(p7aux->p7, &p);
194 *pbuf = *p7aux->boundary;
195 *plen = derlen - (*p7aux->boundary - p7aux->derbuf);
196
197 return 1;
198 }
199
200