2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
59 #include <openssl/asn1.h>
60 #include <openssl/pkcs7.h>
61 #include <openssl/bio.h>
63 #ifndef OPENSSL_SYSNAME_NETWARE
68 /* Highly experiemental PKCS#7 BIO support routines */
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.
76 * When the BIO is flushed the output is finalized and any
77 * signatures etc written out.
79 * The BIO is a 'proper' BIO and can handle non blocking I/O
82 * The usage is simple. The implementation is *not*...
85 /* BIO support data stored in the ASN1 BIO ex_arg */
87 typedef struct pkcs7_aux_st
89 /* PKCS7 structure this BIO refers to */
91 /* Top of the BIO chain */
95 /* Boundary where content is inserted */
96 unsigned char **boundary;
97 /* DER buffer start */
98 unsigned char *derbuf;
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);
106 BIO *BIO_new_PKCS7(BIO *out, PKCS7 *p7)
108 PKCS7_SUPPORT *p7aux = NULL;
111 unsigned char **boundary;
112 p7aux = OPENSSL_malloc(sizeof(PKCS7_SUPPORT));
113 asn_bio = BIO_new(BIO_f_asn1());
115 /* ASN1 bio needs to be next to output BIO */
117 out = BIO_push(asn_bio, out);
119 if (!p7aux || !asn_bio || !out)
122 BIO_asn1_set_prefix(asn_bio, pkcs7_prefix, pkcs7_prefix_free);
123 BIO_asn1_set_suffix(asn_bio, pkcs7_suffix, pkcs7_suffix_free);
125 /* Now initialize BIO for PKCS#7 output */
127 p7bio = PKCS7_dataInit(p7, out);
128 if (!p7bio || !PKCS7_stream(&boundary, p7))
132 p7aux->p7bio = p7bio;
133 p7aux->boundary = boundary;
136 BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, p7aux);
150 static int pkcs7_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
152 PKCS7_SUPPORT *p7aux;
159 p7aux = *(PKCS7_SUPPORT **)parg;
161 derlen = i2d_PKCS7_NDEF(p7aux->p7, NULL);
162 p = OPENSSL_malloc(derlen);
165 i2d_PKCS7_NDEF(p7aux->p7, &p);
167 if (!*p7aux->boundary)
170 *plen = *p7aux->boundary - *pbuf;
175 static int pkcs7_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
177 PKCS7_SUPPORT *p7aux;
182 p7aux = *(PKCS7_SUPPORT **)parg;
185 OPENSSL_free(p7aux->derbuf);
187 p7aux->derbuf = NULL;
193 static int pkcs7_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
195 PKCS7_SUPPORT **pp7aux = (PKCS7_SUPPORT **)parg;
196 if (!pkcs7_prefix_free(b, pbuf, plen, parg))
198 OPENSSL_free(*pp7aux);
203 static int pkcs7_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
205 PKCS7_SUPPORT *p7aux;
212 p7aux = *(PKCS7_SUPPORT **)parg;
214 /* Finalize structures */
215 PKCS7_dataFinal(p7aux->p7, p7aux->p7bio);
217 derlen = i2d_PKCS7_NDEF(p7aux->p7, NULL);
218 p = OPENSSL_malloc(derlen);
220 i2d_PKCS7_NDEF(p7aux->p7, &p);
221 if (!*p7aux->boundary)
223 *pbuf = *p7aux->boundary;
224 *plen = derlen - (*p7aux->boundary - p7aux->derbuf);