| File: | src/lib/libcrypto/asn1/bio_ndef.c |
| Warning: | line 185, column 7 Although the value stored to 'derlen' is used in the enclosing expression, the value is never actually read from 'derlen' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* $OpenBSD: bio_ndef.c,v 1.24 2023/07/28 09:58:30 tb Exp $ */ |
| 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 <stdio.h> |
| 56 | |
| 57 | #include <openssl/asn1.h> |
| 58 | #include <openssl/asn1t.h> |
| 59 | #include <openssl/bio.h> |
| 60 | #include <openssl/err.h> |
| 61 | |
| 62 | #include "asn1_local.h" |
| 63 | |
| 64 | int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix, asn1_ps_func *prefix_free); |
| 65 | int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix, asn1_ps_func *suffix_free); |
| 66 | |
| 67 | /* Experimental NDEF ASN1 BIO support routines */ |
| 68 | |
| 69 | /* The usage is quite simple, initialize an ASN1 structure, |
| 70 | * get a BIO from it then any data written through the BIO |
| 71 | * will end up translated to approptiate format on the fly. |
| 72 | * The data is streamed out and does *not* need to be |
| 73 | * all held in memory at once. |
| 74 | * |
| 75 | * When the BIO is flushed the output is finalized and any |
| 76 | * signatures etc written out. |
| 77 | * |
| 78 | * The BIO is a 'proper' BIO and can handle non blocking I/O |
| 79 | * correctly. |
| 80 | * |
| 81 | * The usage is simple. The implementation is *not*... |
| 82 | */ |
| 83 | |
| 84 | /* BIO support data stored in the ASN1 BIO ex_arg */ |
| 85 | |
| 86 | typedef struct ndef_aux_st { |
| 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 | |
| 100 | static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg); |
| 101 | static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg); |
| 102 | static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg); |
| 103 | static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg); |
| 104 | |
| 105 | BIO * |
| 106 | BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it) |
| 107 | { |
| 108 | NDEF_SUPPORT *ndef_aux = NULL((void *)0); |
| 109 | BIO *asn_bio = NULL((void *)0), *pop_bio = NULL((void *)0); |
| 110 | const ASN1_AUX *aux = it->funcs; |
| 111 | ASN1_STREAM_ARG sarg; |
| 112 | |
| 113 | if (aux == NULL((void *)0) || aux->asn1_cb == NULL((void *)0)) { |
| 114 | ASN1error(ASN1_R_STREAMING_NOT_SUPPORTED)ERR_put_error(13,(0xfff),(202),"/usr/src/lib/libcrypto/asn1/bio_ndef.c" ,114); |
| 115 | goto err; |
| 116 | } |
| 117 | |
| 118 | if ((asn_bio = BIO_new(BIO_f_asn1())) == NULL((void *)0)) |
| 119 | goto err; |
| 120 | |
| 121 | if (BIO_push(asn_bio, out) == NULL((void *)0)) |
| 122 | goto err; |
| 123 | pop_bio = asn_bio; |
| 124 | |
| 125 | /* |
| 126 | * Set up prefix and suffix handlers first. This ensures that ndef_aux |
| 127 | * is freed as part of asn_bio once it is the asn_bio's ex_arg. |
| 128 | */ |
| 129 | if (BIO_asn1_set_prefix(asn_bio, ndef_prefix, ndef_prefix_free) <= 0) |
| 130 | goto err; |
| 131 | if (BIO_asn1_set_suffix(asn_bio, ndef_suffix, ndef_suffix_free) <= 0) |
| 132 | goto err; |
| 133 | |
| 134 | /* |
| 135 | * Allocate early to avoid the tricky cleanup after the asn1_cb(). |
| 136 | * Ownership of ndef_aux is transferred to asn_bio in BIO_ctrl(). |
| 137 | * Keep a reference to populate it after callback success. |
| 138 | */ |
| 139 | if ((ndef_aux = calloc(1, sizeof(*ndef_aux))) == NULL((void *)0)) |
| 140 | goto err; |
| 141 | if (BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG153, 0, ndef_aux) <= 0) { |
| 142 | free(ndef_aux); |
| 143 | goto err; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * The callback prepends BIOs to the chain starting at asn_bio for |
| 148 | * digest, cipher, etc. The resulting chain starts at sarg.ndef_bio. |
| 149 | */ |
| 150 | |
| 151 | sarg.out = asn_bio; |
| 152 | sarg.ndef_bio = NULL((void *)0); |
| 153 | sarg.boundary = NULL((void *)0); |
| 154 | |
| 155 | if (aux->asn1_cb(ASN1_OP_STREAM_PRE10, &val, it, &sarg) <= 0) |
| 156 | goto err; |
| 157 | |
| 158 | ndef_aux->val = val; |
| 159 | ndef_aux->it = it; |
| 160 | ndef_aux->ndef_bio = sarg.ndef_bio; |
| 161 | ndef_aux->boundary = sarg.boundary; |
| 162 | ndef_aux->out = asn_bio; |
| 163 | |
| 164 | return sarg.ndef_bio; |
| 165 | |
| 166 | err: |
| 167 | BIO_pop(pop_bio); |
| 168 | BIO_free(asn_bio); |
| 169 | |
| 170 | return NULL((void *)0); |
| 171 | } |
| 172 | |
| 173 | static int |
| 174 | ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg) |
| 175 | { |
| 176 | NDEF_SUPPORT *ndef_aux; |
| 177 | unsigned char *p = NULL((void *)0); |
| 178 | int derlen; |
| 179 | |
| 180 | if (!parg) |
| 181 | return 0; |
| 182 | |
| 183 | ndef_aux = *(NDEF_SUPPORT **)parg; |
| 184 | |
| 185 | if ((derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it)) <= 0) |
Although the value stored to 'derlen' is used in the enclosing expression, the value is never actually read from 'derlen' | |
| 186 | return 0; |
| 187 | |
| 188 | ndef_aux->derbuf = p; |
| 189 | *pbuf = p; |
| 190 | |
| 191 | if (*ndef_aux->boundary == NULL((void *)0)) |
| 192 | return 0; |
| 193 | |
| 194 | *plen = *ndef_aux->boundary - *pbuf; |
| 195 | |
| 196 | return 1; |
| 197 | } |
| 198 | |
| 199 | static int |
| 200 | ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg) |
| 201 | { |
| 202 | NDEF_SUPPORT **pndef_aux = parg; |
| 203 | |
| 204 | if (pndef_aux == NULL((void *)0) || *pndef_aux == NULL((void *)0)) |
| 205 | return 0; |
| 206 | |
| 207 | free((*pndef_aux)->derbuf); |
| 208 | (*pndef_aux)->derbuf = NULL((void *)0); |
| 209 | |
| 210 | *pbuf = NULL((void *)0); |
| 211 | *plen = 0; |
| 212 | |
| 213 | return 1; |
| 214 | } |
| 215 | |
| 216 | static int |
| 217 | ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg) |
| 218 | { |
| 219 | NDEF_SUPPORT **pndef_aux = parg; |
| 220 | |
| 221 | /* Ensure ndef_prefix_free() won't fail, so we won't leak *pndef_aux. */ |
| 222 | if (pndef_aux == NULL((void *)0) || *pndef_aux == NULL((void *)0)) |
| 223 | return 0; |
| 224 | if (!ndef_prefix_free(b, pbuf, plen, parg)) |
| 225 | return 0; |
| 226 | |
| 227 | free(*pndef_aux); |
| 228 | *pndef_aux = NULL((void *)0); |
| 229 | |
| 230 | return 1; |
| 231 | } |
| 232 | |
| 233 | static int |
| 234 | ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg) |
| 235 | { |
| 236 | NDEF_SUPPORT *ndef_aux; |
| 237 | unsigned char *p = NULL((void *)0); |
| 238 | int derlen; |
| 239 | const ASN1_AUX *aux; |
| 240 | ASN1_STREAM_ARG sarg; |
| 241 | |
| 242 | if (!parg) |
| 243 | return 0; |
| 244 | |
| 245 | ndef_aux = *(NDEF_SUPPORT **)parg; |
| 246 | |
| 247 | aux = ndef_aux->it->funcs; |
| 248 | |
| 249 | /* Finalize structures */ |
| 250 | sarg.ndef_bio = ndef_aux->ndef_bio; |
| 251 | sarg.out = ndef_aux->out; |
| 252 | sarg.boundary = ndef_aux->boundary; |
| 253 | if (aux->asn1_cb(ASN1_OP_STREAM_POST11, |
| 254 | &ndef_aux->val, ndef_aux->it, &sarg) <= 0) |
| 255 | return 0; |
| 256 | |
| 257 | if ((derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it)) <= 0) |
| 258 | return 0; |
| 259 | |
| 260 | ndef_aux->derbuf = p; |
| 261 | *pbuf = p; |
| 262 | |
| 263 | if (*ndef_aux->boundary == NULL((void *)0)) |
| 264 | return 0; |
| 265 | |
| 266 | *pbuf = *ndef_aux->boundary; |
| 267 | *plen = derlen - (*ndef_aux->boundary - ndef_aux->derbuf); |
| 268 | |
| 269 | return 1; |
| 270 | } |