| 1 |
/* |
| 2 |
* Copyright 2006-2025 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 |
* this file except in compliance with the License. You can obtain a copy |
| 6 |
* in the file LICENSE in the source distribution or at |
| 7 |
* https://www.openssl.org/source/license.html |
| 8 |
*/ |
| 9 |
|
| 10 |
/* |
| 11 |
* Experimental ASN1 BIO. When written through the data is converted to an |
| 12 |
* ASN1 string type: default is OCTET STRING. Additional functions can be |
| 13 |
* provided to add prefix and suffix data. |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <string.h> |
| 17 |
#include "internal/bio.h" |
| 18 |
#include <openssl/asn1.h> |
| 19 |
#include "internal/cryptlib.h" |
| 20 |
|
| 21 |
/* Must be large enough for biggest tag+length */ |
| 22 |
#define DEFAULT_ASN1_BUF_SIZE 20 |
| 23 |
|
| 24 |
typedef enum { |
| 25 |
ASN1_STATE_START, |
| 26 |
ASN1_STATE_PRE_COPY, |
| 27 |
ASN1_STATE_HEADER, |
| 28 |
ASN1_STATE_HEADER_COPY, |
| 29 |
ASN1_STATE_DATA_COPY, |
| 30 |
ASN1_STATE_POST_COPY, |
| 31 |
ASN1_STATE_DONE |
| 32 |
} asn1_bio_state_t; |
| 33 |
|
| 34 |
typedef struct BIO_ASN1_EX_FUNCS_st { |
| 35 |
asn1_ps_func *ex_func; |
| 36 |
asn1_ps_func *ex_free_func; |
| 37 |
} BIO_ASN1_EX_FUNCS; |
| 38 |
|
| 39 |
typedef struct BIO_ASN1_BUF_CTX_t { |
| 40 |
/* Internal state */ |
| 41 |
asn1_bio_state_t state; |
| 42 |
/* Internal buffer */ |
| 43 |
unsigned char *buf; |
| 44 |
/* Size of buffer */ |
| 45 |
int bufsize; |
| 46 |
/* Current position in buffer */ |
| 47 |
int bufpos; |
| 48 |
/* Current buffer length */ |
| 49 |
int buflen; |
| 50 |
/* Amount of data to copy */ |
| 51 |
int copylen; |
| 52 |
/* Class and tag to use */ |
| 53 |
int asn1_class, asn1_tag; |
| 54 |
asn1_ps_func *prefix, *prefix_free, *suffix, *suffix_free; |
| 55 |
/* Extra buffer for prefix and suffix data */ |
| 56 |
unsigned char *ex_buf; |
| 57 |
int ex_len; |
| 58 |
int ex_pos; |
| 59 |
void *ex_arg; |
| 60 |
} BIO_ASN1_BUF_CTX; |
| 61 |
|
| 62 |
static int asn1_bio_write(BIO *h, const char *buf, int num); |
| 63 |
static int asn1_bio_read(BIO *h, char *buf, int size); |
| 64 |
static int asn1_bio_puts(BIO *h, const char *str); |
| 65 |
static int asn1_bio_gets(BIO *h, char *str, int size); |
| 66 |
static long asn1_bio_ctrl(BIO *h, int cmd, long arg1, void *arg2); |
| 67 |
static int asn1_bio_new(BIO *h); |
| 68 |
static int asn1_bio_free(BIO *data); |
| 69 |
static long asn1_bio_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp); |
| 70 |
|
| 71 |
static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size); |
| 72 |
static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, |
| 73 |
asn1_ps_func *cleanup, asn1_bio_state_t next); |
| 74 |
static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, |
| 75 |
asn1_ps_func *setup, |
| 76 |
asn1_bio_state_t ex_state, |
| 77 |
asn1_bio_state_t other_state); |
| 78 |
|
| 79 |
static const BIO_METHOD methods_asn1 = { |
| 80 |
BIO_TYPE_ASN1, |
| 81 |
"asn1", |
| 82 |
bwrite_conv, |
| 83 |
asn1_bio_write, |
| 84 |
bread_conv, |
| 85 |
asn1_bio_read, |
| 86 |
asn1_bio_puts, |
| 87 |
asn1_bio_gets, |
| 88 |
asn1_bio_ctrl, |
| 89 |
asn1_bio_new, |
| 90 |
asn1_bio_free, |
| 91 |
asn1_bio_callback_ctrl, |
| 92 |
}; |
| 93 |
|
| 94 |
const BIO_METHOD *BIO_f_asn1(void) |
| 95 |
{ |
| 96 |
return &methods_asn1; |
| 97 |
} |
| 98 |
|
| 99 |
static int asn1_bio_new(BIO *b) |
| 100 |
{ |
| 101 |
BIO_ASN1_BUF_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
| 102 |
|
| 103 |
if (ctx == NULL) |
| 104 |
return 0; |
| 105 |
if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) { |
| 106 |
OPENSSL_free(ctx); |
| 107 |
return 0; |
| 108 |
} |
| 109 |
BIO_set_data(b, ctx); |
| 110 |
BIO_set_init(b, 1); |
| 111 |
|
| 112 |
return 1; |
| 113 |
} |
| 114 |
|
| 115 |
static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size) |
| 116 |
{ |
| 117 |
if (size <= 0) { |
| 118 |
ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_INVALID_ARGUMENT); |
| 119 |
return 0; |
| 120 |
} |
| 121 |
if ((ctx->buf = OPENSSL_malloc(size)) == NULL) |
| 122 |
return 0; |
| 123 |
ctx->bufsize = size; |
| 124 |
ctx->asn1_class = V_ASN1_UNIVERSAL; |
| 125 |
ctx->asn1_tag = V_ASN1_OCTET_STRING; |
| 126 |
ctx->state = ASN1_STATE_START; |
| 127 |
return 1; |
| 128 |
} |
| 129 |
|
| 130 |
static int asn1_bio_free(BIO *b) |
| 131 |
{ |
| 132 |
BIO_ASN1_BUF_CTX *ctx; |
| 133 |
|
| 134 |
if (b == NULL) |
| 135 |
return 0; |
| 136 |
|
| 137 |
ctx = BIO_get_data(b); |
| 138 |
if (ctx == NULL) |
| 139 |
return 0; |
| 140 |
|
| 141 |
if (ctx->prefix_free != NULL) |
| 142 |
ctx->prefix_free(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg); |
| 143 |
if (ctx->suffix_free != NULL) |
| 144 |
ctx->suffix_free(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg); |
| 145 |
|
| 146 |
OPENSSL_free(ctx->buf); |
| 147 |
OPENSSL_free(ctx); |
| 148 |
BIO_set_data(b, NULL); |
| 149 |
BIO_set_init(b, 0); |
| 150 |
|
| 151 |
return 1; |
| 152 |
} |
| 153 |
|
| 154 |
static int asn1_bio_write(BIO *b, const char *in, int inl) |
| 155 |
{ |
| 156 |
BIO_ASN1_BUF_CTX *ctx; |
| 157 |
int wrmax, wrlen, ret; |
| 158 |
unsigned char *p; |
| 159 |
BIO *next; |
| 160 |
|
| 161 |
ctx = BIO_get_data(b); |
| 162 |
next = BIO_next(b); |
| 163 |
if (in == NULL || inl < 0 || ctx == NULL || next == NULL) |
| 164 |
return 0; |
| 165 |
|
| 166 |
wrlen = 0; |
| 167 |
ret = -1; |
| 168 |
|
| 169 |
for (;;) { |
| 170 |
switch (ctx->state) { |
| 171 |
/* Setup prefix data, call it */ |
| 172 |
case ASN1_STATE_START: |
| 173 |
if (!asn1_bio_setup_ex(b, ctx, ctx->prefix, |
| 174 |
ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER)) |
| 175 |
return -1; |
| 176 |
break; |
| 177 |
|
| 178 |
/* Copy any pre data first */ |
| 179 |
case ASN1_STATE_PRE_COPY: |
| 180 |
|
| 181 |
ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free, |
| 182 |
ASN1_STATE_HEADER); |
| 183 |
|
| 184 |
if (ret <= 0) |
| 185 |
goto done; |
| 186 |
|
| 187 |
break; |
| 188 |
|
| 189 |
case ASN1_STATE_HEADER: |
| 190 |
ctx->buflen = ASN1_object_size(0, inl, ctx->asn1_tag) - inl; |
| 191 |
if (!ossl_assert(ctx->buflen <= ctx->bufsize)) |
| 192 |
return -1; |
| 193 |
p = ctx->buf; |
| 194 |
ASN1_put_object(&p, 0, inl, ctx->asn1_tag, ctx->asn1_class); |
| 195 |
ctx->copylen = inl; |
| 196 |
ctx->state = ASN1_STATE_HEADER_COPY; |
| 197 |
|
| 198 |
break; |
| 199 |
|
| 200 |
case ASN1_STATE_HEADER_COPY: |
| 201 |
ret = BIO_write(next, ctx->buf + ctx->bufpos, ctx->buflen); |
| 202 |
if (ret <= 0) |
| 203 |
goto done; |
| 204 |
|
| 205 |
ctx->buflen -= ret; |
| 206 |
if (ctx->buflen) |
| 207 |
ctx->bufpos += ret; |
| 208 |
else { |
| 209 |
ctx->bufpos = 0; |
| 210 |
ctx->state = ASN1_STATE_DATA_COPY; |
| 211 |
} |
| 212 |
|
| 213 |
break; |
| 214 |
|
| 215 |
case ASN1_STATE_DATA_COPY: |
| 216 |
|
| 217 |
if (inl > ctx->copylen) |
| 218 |
wrmax = ctx->copylen; |
| 219 |
else |
| 220 |
wrmax = inl; |
| 221 |
ret = BIO_write(next, in, wrmax); |
| 222 |
if (ret <= 0) |
| 223 |
goto done; |
| 224 |
wrlen += ret; |
| 225 |
ctx->copylen -= ret; |
| 226 |
in += ret; |
| 227 |
inl -= ret; |
| 228 |
|
| 229 |
if (ctx->copylen == 0) |
| 230 |
ctx->state = ASN1_STATE_HEADER; |
| 231 |
|
| 232 |
if (inl == 0) |
| 233 |
goto done; |
| 234 |
|
| 235 |
break; |
| 236 |
|
| 237 |
case ASN1_STATE_POST_COPY: |
| 238 |
case ASN1_STATE_DONE: |
| 239 |
BIO_clear_retry_flags(b); |
| 240 |
return 0; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
done: |
| 245 |
BIO_clear_retry_flags(b); |
| 246 |
BIO_copy_next_retry(b); |
| 247 |
|
| 248 |
return (wrlen > 0) ? wrlen : ret; |
| 249 |
} |
| 250 |
|
| 251 |
static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, |
| 252 |
asn1_ps_func *cleanup, asn1_bio_state_t next) |
| 253 |
{ |
| 254 |
int ret; |
| 255 |
|
| 256 |
if (ctx->ex_len <= 0) |
| 257 |
return 1; |
| 258 |
for (;;) { |
| 259 |
ret = BIO_write(BIO_next(b), ctx->ex_buf + ctx->ex_pos, ctx->ex_len); |
| 260 |
if (ret <= 0) |
| 261 |
break; |
| 262 |
ctx->ex_len -= ret; |
| 263 |
if (ctx->ex_len > 0) |
| 264 |
ctx->ex_pos += ret; |
| 265 |
else { |
| 266 |
if (cleanup) |
| 267 |
cleanup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg); |
| 268 |
ctx->state = next; |
| 269 |
ctx->ex_pos = 0; |
| 270 |
break; |
| 271 |
} |
| 272 |
} |
| 273 |
return ret; |
| 274 |
} |
| 275 |
|
| 276 |
static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, |
| 277 |
asn1_ps_func *setup, |
| 278 |
asn1_bio_state_t ex_state, |
| 279 |
asn1_bio_state_t other_state) |
| 280 |
{ |
| 281 |
if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) { |
| 282 |
BIO_clear_retry_flags(b); |
| 283 |
return 0; |
| 284 |
} |
| 285 |
if (ctx->ex_len > 0) |
| 286 |
ctx->state = ex_state; |
| 287 |
else |
| 288 |
ctx->state = other_state; |
| 289 |
return 1; |
| 290 |
} |
| 291 |
|
| 292 |
static int asn1_bio_read(BIO *b, char *in, int inl) |
| 293 |
{ |
| 294 |
BIO *next = BIO_next(b); |
| 295 |
if (next == NULL) |
| 296 |
return 0; |
| 297 |
return BIO_read(next, in, inl); |
| 298 |
} |
| 299 |
|
| 300 |
static int asn1_bio_puts(BIO *b, const char *str) |
| 301 |
{ |
| 302 |
size_t len = strlen(str); |
| 303 |
|
| 304 |
if (len > INT_MAX) |
| 305 |
return -1; |
| 306 |
|
| 307 |
return asn1_bio_write(b, str, (int)len); |
| 308 |
} |
| 309 |
|
| 310 |
static int asn1_bio_gets(BIO *b, char *str, int size) |
| 311 |
{ |
| 312 |
BIO *next = BIO_next(b); |
| 313 |
if (next == NULL) |
| 314 |
return 0; |
| 315 |
return BIO_gets(next, str, size); |
| 316 |
} |
| 317 |
|
| 318 |
static long asn1_bio_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) |
| 319 |
{ |
| 320 |
BIO *next = BIO_next(b); |
| 321 |
if (next == NULL) |
| 322 |
return 0; |
| 323 |
return BIO_callback_ctrl(next, cmd, fp); |
| 324 |
} |
| 325 |
|
| 326 |
static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2) |
| 327 |
{ |
| 328 |
BIO_ASN1_BUF_CTX *ctx; |
| 329 |
BIO_ASN1_EX_FUNCS *ex_func; |
| 330 |
long ret = 1; |
| 331 |
BIO *next; |
| 332 |
|
| 333 |
ctx = BIO_get_data(b); |
| 334 |
if (ctx == NULL) |
| 335 |
return 0; |
| 336 |
next = BIO_next(b); |
| 337 |
switch (cmd) { |
| 338 |
|
| 339 |
case BIO_C_SET_PREFIX: |
| 340 |
ex_func = arg2; |
| 341 |
ctx->prefix = ex_func->ex_func; |
| 342 |
ctx->prefix_free = ex_func->ex_free_func; |
| 343 |
break; |
| 344 |
|
| 345 |
case BIO_C_GET_PREFIX: |
| 346 |
ex_func = arg2; |
| 347 |
ex_func->ex_func = ctx->prefix; |
| 348 |
ex_func->ex_free_func = ctx->prefix_free; |
| 349 |
break; |
| 350 |
|
| 351 |
case BIO_C_SET_SUFFIX: |
| 352 |
ex_func = arg2; |
| 353 |
ctx->suffix = ex_func->ex_func; |
| 354 |
ctx->suffix_free = ex_func->ex_free_func; |
| 355 |
break; |
| 356 |
|
| 357 |
case BIO_C_GET_SUFFIX: |
| 358 |
ex_func = arg2; |
| 359 |
ex_func->ex_func = ctx->suffix; |
| 360 |
ex_func->ex_free_func = ctx->suffix_free; |
| 361 |
break; |
| 362 |
|
| 363 |
case BIO_C_SET_EX_ARG: |
| 364 |
ctx->ex_arg = arg2; |
| 365 |
break; |
| 366 |
|
| 367 |
case BIO_C_GET_EX_ARG: |
| 368 |
*(void **)arg2 = ctx->ex_arg; |
| 369 |
break; |
| 370 |
|
| 371 |
case BIO_CTRL_FLUSH: |
| 372 |
if (next == NULL) |
| 373 |
return 0; |
| 374 |
|
| 375 |
/* Call post function if possible */ |
| 376 |
if (ctx->state == ASN1_STATE_HEADER) { |
| 377 |
if (!asn1_bio_setup_ex(b, ctx, ctx->suffix, |
| 378 |
ASN1_STATE_POST_COPY, ASN1_STATE_DONE)) |
| 379 |
return 0; |
| 380 |
} |
| 381 |
|
| 382 |
if (ctx->state == ASN1_STATE_POST_COPY) { |
| 383 |
ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free, |
| 384 |
ASN1_STATE_DONE); |
| 385 |
if (ret <= 0) |
| 386 |
return ret; |
| 387 |
} |
| 388 |
|
| 389 |
if (ctx->state == ASN1_STATE_DONE) |
| 390 |
return BIO_ctrl(next, cmd, arg1, arg2); |
| 391 |
else { |
| 392 |
BIO_clear_retry_flags(b); |
| 393 |
return 0; |
| 394 |
} |
| 395 |
|
| 396 |
default: |
| 397 |
if (next == NULL) |
| 398 |
return 0; |
| 399 |
return BIO_ctrl(next, cmd, arg1, arg2); |
| 400 |
} |
| 401 |
|
| 402 |
return ret; |
| 403 |
} |
| 404 |
|
| 405 |
static int asn1_bio_set_ex(BIO *b, int cmd, |
| 406 |
asn1_ps_func *ex_func, asn1_ps_func *ex_free_func) |
| 407 |
{ |
| 408 |
BIO_ASN1_EX_FUNCS extmp; |
| 409 |
extmp.ex_func = ex_func; |
| 410 |
extmp.ex_free_func = ex_free_func; |
| 411 |
return BIO_ctrl(b, cmd, 0, &extmp); |
| 412 |
} |
| 413 |
|
| 414 |
static int asn1_bio_get_ex(BIO *b, int cmd, |
| 415 |
asn1_ps_func **ex_func, |
| 416 |
asn1_ps_func **ex_free_func) |
| 417 |
{ |
| 418 |
BIO_ASN1_EX_FUNCS extmp; |
| 419 |
int ret; |
| 420 |
ret = BIO_ctrl(b, cmd, 0, &extmp); |
| 421 |
if (ret > 0) { |
| 422 |
*ex_func = extmp.ex_func; |
| 423 |
*ex_free_func = extmp.ex_free_func; |
| 424 |
} |
| 425 |
return ret; |
| 426 |
} |
| 427 |
|
| 428 |
int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix, |
| 429 |
asn1_ps_func *prefix_free) |
| 430 |
{ |
| 431 |
return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free); |
| 432 |
} |
| 433 |
|
| 434 |
int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix, |
| 435 |
asn1_ps_func **pprefix_free) |
| 436 |
{ |
| 437 |
return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free); |
| 438 |
} |
| 439 |
|
| 440 |
int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix, |
| 441 |
asn1_ps_func *suffix_free) |
| 442 |
{ |
| 443 |
return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free); |
| 444 |
} |
| 445 |
|
| 446 |
int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix, |
| 447 |
asn1_ps_func **psuffix_free) |
| 448 |
{ |
| 449 |
return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free); |
| 450 |
} |