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>
66 /* Highly experiemental PKCS#7 BIO support routines */
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.
74 * When the BIO is flushed the output is finalized and any
75 * signatures etc written out.
77 * The BIO is a 'proper' BIO and can handle non blocking I/O
80 * The usage is simple. The implementation is *not*...
83 /* BIO support data stored in the ASN1 BIO ex_arg */
85 typedef struct pkcs7_aux_st
87 /* PKCS7 structure this BIO refers to */
89 /* Top of the BIO chain */
93 /* Boundary where content is inserted */
94 unsigned char **boundary;
95 /* DER buffer start */
96 unsigned char *derbuf;
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);
103 BIO *BIO_new_PKCS7(BIO *out, PKCS7 *p7)
105 PKCS7_SUPPORT *p7aux = NULL;
108 unsigned char **boundary;
109 p7aux = OPENSSL_malloc(sizeof(PKCS7_SUPPORT));
110 asn_bio = BIO_new(BIO_f_asn1());
112 /* ASN1 bio needs to be next to output BIO */
114 out = BIO_push(asn_bio, out);
116 BIO_asn1_set_prefix(asn_bio, pkcs7_prefix, pkcs7_psfix_free);
117 BIO_asn1_set_suffix(asn_bio, pkcs7_suffix, pkcs7_psfix_free);
119 /* Now initialize BIO for PKCS#7 output */
121 p7bio = PKCS7_dataInit(p7, out);
122 PKCS7_stream(&boundary, p7);
125 p7aux->p7bio = p7bio;
126 p7aux->boundary = boundary;
129 BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, p7aux);
136 static int pkcs7_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
138 PKCS7_SUPPORT *p7aux;
145 p7aux = *(PKCS7_SUPPORT **)parg;
147 derlen = i2d_PKCS7_NDEF(p7aux->p7, NULL);
148 p = OPENSSL_malloc(derlen);
151 i2d_PKCS7_NDEF(p7aux->p7, &p);
153 *plen = *p7aux->boundary - *pbuf;
158 static int pkcs7_psfix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
160 PKCS7_SUPPORT *p7aux;
165 p7aux = *(PKCS7_SUPPORT **)parg;
168 OPENSSL_free(p7aux->derbuf);
170 p7aux->derbuf = NULL;
176 static int pkcs7_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
178 PKCS7_SUPPORT *p7aux;
185 p7aux = *(PKCS7_SUPPORT **)parg;
187 /* Finalize structures */
188 PKCS7_dataFinal(p7aux->p7, p7aux->p7bio);
190 derlen = i2d_PKCS7_NDEF(p7aux->p7, NULL);
191 p = OPENSSL_malloc(derlen);
193 i2d_PKCS7_NDEF(p7aux->p7, &p);
194 *pbuf = *p7aux->boundary;
195 *plen = derlen - (*p7aux->boundary - p7aux->derbuf);