1/* bio_ndef.c */ 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * project. 4 */ 5/* ==================================================================== 6 * Copyright (c) 2008 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 */ 54 55#include <openssl/asn1.h> 56#include <openssl/asn1t.h> 57#include <openssl/bio.h> 58#include <openssl/err.h> 59 60#define OPENSSL_SYSNAME_NETWARE 61#ifndef OPENSSL_SYSNAME_NETWARE 62#include <memory.h> 63#endif 64#include <stdio.h> 65 66/* Experimental NDEF ASN1 BIO support routines */ 67 68/* The usage is quite simple, initialize an ASN1 structure, 69 * get a BIO from it then any data written through the BIO 70 * will end up translated to approptiate 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 85typedef struct ndef_aux_st 86 { 87 /* ASN1 structure this BIO refers to */ 88 ASN1_VALUE *val; 89 const ASN1_ITEM *it; 90 /* Top of the BIO chain */ 91 BIO *ndef_bio; 92 /* Output BIO */ 93 BIO *out; 94 /* Boundary where content is inserted */ 95 unsigned char **boundary; 96 /* DER buffer start */ 97 unsigned char *derbuf; 98 } NDEF_SUPPORT; 99 100static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg); 101static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg); 102static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg); 103static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg); 104 105BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it) 106 { 107 NDEF_SUPPORT *ndef_aux = NULL; 108 BIO *asn_bio = NULL; 109 const ASN1_AUX *aux = it->funcs; 110 ASN1_STREAM_ARG sarg; 111 112 if (!aux || !aux->asn1_cb) 113 { 114 ASN1err(ASN1_F_BIO_NEW_NDEF, ASN1_R_STREAMING_NOT_SUPPORTED); 115 return NULL; 116 } 117 ndef_aux = OPENSSL_malloc(sizeof(NDEF_SUPPORT)); 118 asn_bio = BIO_new(BIO_f_asn1()); 119 120 /* ASN1 bio needs to be next to output BIO */ 121 122 out = BIO_push(asn_bio, out); 123 124 if (!ndef_aux || !asn_bio || !out) 125 goto err; 126 127 BIO_asn1_set_prefix(asn_bio, ndef_prefix, ndef_prefix_free); 128 BIO_asn1_set_suffix(asn_bio, ndef_suffix, ndef_suffix_free); 129 130 /* Now let callback prepend any digest, cipher etc BIOs 131 * ASN1 structure needs. 132 */ 133 134 sarg.out = out; 135 sarg.ndef_bio = NULL; 136 sarg.boundary = NULL; 137 138 if (aux->asn1_cb(ASN1_OP_STREAM_PRE, &val, it, &sarg) <= 0) 139 goto err; 140 141 ndef_aux->val = val; 142 ndef_aux->it = it; 143 ndef_aux->ndef_bio = sarg.ndef_bio; 144 ndef_aux->boundary = sarg.boundary; 145 ndef_aux->out = out; 146 147 BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, ndef_aux); 148 149 return sarg.ndef_bio; 150 151 err: 152 if (asn_bio) 153 BIO_free(asn_bio); 154 if (ndef_aux) 155 OPENSSL_free(ndef_aux); 156 return NULL; 157 } 158 159static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg) 160 { 161 NDEF_SUPPORT *ndef_aux; 162 unsigned char *p; 163 int derlen; 164 165 if (!parg) 166 return 0; 167 168 ndef_aux = *(NDEF_SUPPORT **)parg; 169 170 derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it); 171 p = OPENSSL_malloc(derlen); 172 ndef_aux->derbuf = p; 173 *pbuf = p; 174 derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it); 175 176 if (!*ndef_aux->boundary) 177 return 0; 178 179 *plen = *ndef_aux->boundary - *pbuf; 180 181 return 1; 182 } 183 184static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg) 185 { 186 NDEF_SUPPORT *ndef_aux; 187 188 if (!parg) 189 return 0; 190 191 ndef_aux = *(NDEF_SUPPORT **)parg; 192 193 if (ndef_aux->derbuf) 194 OPENSSL_free(ndef_aux->derbuf); 195 196 ndef_aux->derbuf = NULL; 197 *pbuf = NULL; 198 *plen = 0; 199 return 1; 200 } 201 202static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg) 203 { 204 NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg; 205 if (!ndef_prefix_free(b, pbuf, plen, parg)) 206 return 0; 207 OPENSSL_free(*pndef_aux); 208 *pndef_aux = NULL; 209 return 1; 210 } 211 212static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg) 213 { 214 NDEF_SUPPORT *ndef_aux; 215 unsigned char *p; 216 int derlen; 217 const ASN1_AUX *aux; 218 ASN1_STREAM_ARG sarg; 219 220 if (!parg) 221 return 0; 222 223 ndef_aux = *(NDEF_SUPPORT **)parg; 224 225 aux = ndef_aux->it->funcs; 226 227 /* Finalize structures */ 228 sarg.ndef_bio = ndef_aux->ndef_bio; 229 sarg.out = ndef_aux->out; 230 sarg.boundary = ndef_aux->boundary; 231 if (aux->asn1_cb(ASN1_OP_STREAM_POST, 232 &ndef_aux->val, ndef_aux->it, &sarg) <= 0) 233 return 0; 234 235 derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it); 236 p = OPENSSL_malloc(derlen); 237 ndef_aux->derbuf = p; 238 *pbuf = p; 239 derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it); 240 241 if (!*ndef_aux->boundary) 242 return 0; 243 *pbuf = *ndef_aux->boundary; 244 *plen = derlen - (*ndef_aux->boundary - ndef_aux->derbuf); 245 246 return 1; 247 } 248