bio_pk7.c 6.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
/* bio_pk7.c */
/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
 * project.
 */
/* ====================================================================
 * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    licensing@OpenSSL.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This product includes cryptographic software written by Eric Young
 * (eay@cryptsoft.com).  This product includes software written by Tim
 * Hudson (tjh@cryptsoft.com).
 *
 */

#include <openssl/asn1.h>
#include <openssl/pkcs7.h>
#include <openssl/bio.h>

#include <memory.h>
#include <stdio.h>

/* Highly experiemental PKCS#7 BIO support routines */

/* The usage is quite simple, initialize a PKCS7 structure,
 * get a BIO from it then any data written through the BIO
 * will end up translated to PKCS#7 format on the fly.
 * The data is streamed out and does *not* need to be
 * all held in memory at once.
 *
 * When the BIO is flushed the output is finalized and any
 * signatures etc written out.
 *
 * The BIO is a 'proper' BIO and can handle non blocking I/O
 * correctly.
 *
 * The usage is simple. The implementation is *not*...
 */

/* BIO support data stored in the ASN1 BIO ex_arg */

typedef struct pkcs7_aux_st
	{
	/* PKCS7 structure this BIO refers to */
	PKCS7 *p7;
	/* Top of the BIO chain */
	BIO *p7bio;
	/* Output BIO */
	BIO *out;
	/* Boundary where content is inserted */
	unsigned char **boundary;
	/* DER buffer start */
	unsigned char *derbuf;
	} PKCS7_SUPPORT;

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 105 106 107 108 109 110 111 112 113 114 115 116

BIO *BIO_new_PKCS7(BIO *out, PKCS7 *p7) 
	{
	PKCS7_SUPPORT *p7aux = NULL;
	BIO *p7bio = NULL;
	BIO *asn_bio = NULL;
	unsigned char **boundary;
	p7aux = OPENSSL_malloc(sizeof(PKCS7_SUPPORT));
	asn_bio = BIO_new(BIO_f_asn1());

	/* ASN1 bio needs to be next to output BIO */

	out = BIO_push(asn_bio, out);

117 118
	BIO_asn1_set_prefix(asn_bio, pkcs7_prefix, pkcs7_prefix_free);
	BIO_asn1_set_suffix(asn_bio, pkcs7_suffix, pkcs7_suffix_free);
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

	/* Now initialize BIO for PKCS#7 output */

	p7bio = PKCS7_dataInit(p7, out);
	PKCS7_stream(&boundary, p7);

	p7aux->p7 = p7;
	p7aux->p7bio = p7bio;
	p7aux->boundary = boundary;
	p7aux->out = out;

	BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, p7aux);

	return p7bio;

	}

static int pkcs7_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
	{
	PKCS7_SUPPORT *p7aux;
	unsigned char *p;
	int derlen;

	if (!parg)
		return 0;

	p7aux = *(PKCS7_SUPPORT **)parg;

	derlen = i2d_PKCS7_NDEF(p7aux->p7, NULL);
	p = OPENSSL_malloc(derlen);
	p7aux->derbuf = p;
	*pbuf = p;
	i2d_PKCS7_NDEF(p7aux->p7, &p);

153 154 155
	if (!*p7aux->boundary)
		return 0;

156 157 158 159 160
	*plen = *p7aux->boundary - *pbuf;

	return 1;
	}

161
static int pkcs7_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	{
	PKCS7_SUPPORT *p7aux;

	if (!parg)
		return 0;

	p7aux = *(PKCS7_SUPPORT **)parg;

	if (p7aux->derbuf)
		OPENSSL_free(p7aux->derbuf);

	p7aux->derbuf = NULL;
	*pbuf = NULL;
	*plen = 0;
	return 1;
	}

179 180 181 182 183 184 185 186 187 188
static int pkcs7_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
	{
	PKCS7_SUPPORT **pp7aux = (PKCS7_SUPPORT **)parg;
	if (!pkcs7_prefix_free(b, pbuf, plen, parg))
		return 0;
	OPENSSL_free(*pp7aux);
	*pp7aux = NULL;
	return 1;
	}

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
static int pkcs7_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
	{
	PKCS7_SUPPORT *p7aux;
	unsigned char *p;
	int derlen;

	if (!parg)
		return 0;

	p7aux = *(PKCS7_SUPPORT **)parg;

	/* Finalize structures */
	PKCS7_dataFinal(p7aux->p7, p7aux->p7bio);

	derlen = i2d_PKCS7_NDEF(p7aux->p7, NULL);
	p = OPENSSL_malloc(derlen);
	p7aux->derbuf = p;
	i2d_PKCS7_NDEF(p7aux->p7, &p);
207 208
	if (!*p7aux->boundary)
		return 0;
209 210 211 212 213 214 215
	*pbuf = *p7aux->boundary;
	*plen = derlen - (*p7aux->boundary - p7aux->derbuf);

	return 1;
	}