1 /* $OpenBSD: tasn_dec.c,v 1.63 2022/05/10 18:40:06 jsing Exp $ */ 2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * project 2000. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 2000-2005 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 * 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). 56 * 57 */ 58 59 #include <limits.h> 60 #include <stddef.h> 61 #include <string.h> 62 63 #include <openssl/asn1.h> 64 #include <openssl/asn1t.h> 65 #include <openssl/buffer.h> 66 #include <openssl/err.h> 67 #include <openssl/objects.h> 68 69 #include "asn1_locl.h" 70 #include "bytestring.h" 71 72 /* Constructed types with a recursive definition (such as can be found in PKCS7) 73 * could eventually exceed the stack given malicious input with excessive 74 * recursion. Therefore we limit the stack depth. 75 */ 76 #define ASN1_MAX_CONSTRUCTED_NEST 30 77 78 static int asn1_check_eoc(const unsigned char **in, long len); 79 static int asn1_check_eoc_cbs(CBS *cbs); 80 static int asn1_find_end(CBS *cbs, size_t length, char indefinite); 81 82 static int asn1_collect(CBB *cbb, CBS *cbs, char indefinite, int expected_tag, 83 int expected_class, int depth); 84 85 static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, 86 long len, const ASN1_ITEM *it, int tag_number, int tag_class, char optional, 87 int depth); 88 static int asn1_template_ex_d2i_cbs(ASN1_VALUE **pval, CBS *cbs, 89 const ASN1_TEMPLATE *tt, char optional, int depth); 90 static int asn1_template_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, 91 long len, const ASN1_TEMPLATE *tt, char opt, int depth); 92 static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in, 93 long len, const ASN1_TEMPLATE *tt, char opt, int depth); 94 static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, CBS *cbs, 95 const ASN1_ITEM *it, int tag_number, int tag_class, char optional); 96 static int asn1_ex_c2i(ASN1_VALUE **pval, CBS *content, int utype, 97 const ASN1_ITEM *it); 98 99 static int asn1_check_tag_cbs(CBS *cbs, size_t *out_len, int *out_tag, 100 uint8_t *out_class, char *out_indefinite, char *out_constructed, 101 int expected_tag, int expected_class, char optional); 102 static int asn1_check_tag(long *out_len, int *out_tag, uint8_t *out_class, 103 char *out_indefinite, char *out_constructed, const unsigned char **in, 104 long len, int expected_tag, int expected_class, char optional); 105 106 ASN1_VALUE * 107 ASN1_item_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, 108 const ASN1_ITEM *it) 109 { 110 ASN1_VALUE *ptmpval = NULL; 111 112 if (pval == NULL) 113 pval = &ptmpval; 114 if (asn1_item_ex_d2i(pval, in, len, it, -1, 0, 0, 0) <= 0) 115 return NULL; 116 117 return *pval; 118 } 119 120 int 121 ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, 122 const ASN1_TEMPLATE *tt) 123 { 124 return asn1_template_ex_d2i(pval, in, len, tt, 0, 0); 125 } 126 127 static int 128 asn1_item_ex_d2i_choice(ASN1_VALUE **pval, const unsigned char **in, long len, 129 const ASN1_ITEM *it, int tag, int aclass, char opt, int depth) 130 { 131 const ASN1_TEMPLATE *tt, *errtt = NULL; 132 const ASN1_AUX *aux = it->funcs; 133 ASN1_aux_cb *asn1_cb = NULL; 134 ASN1_VALUE **pchptr; 135 const unsigned char *p = NULL; 136 int i; 137 int ret = 0; 138 139 if (it->itype != ASN1_ITYPE_CHOICE) 140 goto err; 141 142 if (aux && aux->asn1_cb) 143 asn1_cb = aux->asn1_cb; 144 145 /* 146 * It never makes sense for CHOICE types to have implicit 147 * tagging, so if tag != -1, then this looks like an error in 148 * the template. 149 */ 150 if (tag != -1) { 151 ASN1error(ASN1_R_BAD_TEMPLATE); 152 goto err; 153 } 154 155 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) 156 goto auxerr; 157 158 if (*pval) { 159 /* Free up and zero CHOICE value if initialised */ 160 i = asn1_get_choice_selector(pval, it); 161 if ((i >= 0) && (i < it->tcount)) { 162 tt = it->templates + i; 163 pchptr = asn1_get_field_ptr(pval, tt); 164 ASN1_template_free(pchptr, tt); 165 asn1_set_choice_selector(pval, -1, it); 166 } 167 } else if (!ASN1_item_ex_new(pval, it)) { 168 ASN1error(ERR_R_NESTED_ASN1_ERROR); 169 goto err; 170 } 171 /* CHOICE type, try each possibility in turn */ 172 p = *in; 173 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { 174 pchptr = asn1_get_field_ptr(pval, tt); 175 /* We mark field as OPTIONAL so its absence 176 * can be recognised. 177 */ 178 ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, 179 depth); 180 /* If field not present, try the next one */ 181 if (ret == -1) 182 continue; 183 /* If positive return, read OK, break loop */ 184 if (ret > 0) 185 break; 186 /* Otherwise must be an ASN1 parsing error */ 187 errtt = tt; 188 ASN1error(ERR_R_NESTED_ASN1_ERROR); 189 goto err; 190 } 191 192 /* Did we fall off the end without reading anything? */ 193 if (i == it->tcount) { 194 /* If OPTIONAL, this is OK */ 195 if (opt) { 196 /* Free and zero it */ 197 ASN1_item_ex_free(pval, it); 198 return -1; 199 } 200 ASN1error(ASN1_R_NO_MATCHING_CHOICE_TYPE); 201 goto err; 202 } 203 204 asn1_set_choice_selector(pval, i, it); 205 *in = p; 206 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) 207 goto auxerr; 208 return 1; 209 210 auxerr: 211 ASN1error(ASN1_R_AUX_ERROR); 212 err: 213 ASN1_item_ex_free(pval, it); 214 215 if (errtt) 216 ERR_asprintf_error_data("Field=%s, Type=%s", errtt->field_name, 217 it->sname); 218 else 219 ERR_asprintf_error_data("Type=%s", it->sname); 220 return 0; 221 } 222 223 static int 224 asn1_item_ex_d2i_choice_cbs(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it, 225 int tag_number, int tag_class, char optional, int depth) 226 { 227 const unsigned char *p; 228 int ret; 229 230 if (CBS_len(cbs) > LONG_MAX) 231 return 0; 232 233 p = CBS_data(cbs); 234 ret = asn1_item_ex_d2i_choice(pval, &p, (long)CBS_len(cbs), it, 235 tag_number, tag_class, optional, depth); 236 if (ret == 1) { 237 if (!CBS_skip(cbs, p - CBS_data(cbs))) 238 return 0; 239 } 240 return ret; 241 } 242 243 static int 244 asn1_item_ex_d2i_sequence(ASN1_VALUE **pval, const unsigned char **in, long len, 245 const ASN1_ITEM *it, int tag, int aclass, char opt, int depth) 246 { 247 const ASN1_TEMPLATE *tt, *errtt = NULL; 248 const ASN1_AUX *aux = it->funcs; 249 ASN1_aux_cb *asn1_cb = NULL; 250 char seq_eoc, seq_nolen, cst, isopt; 251 const unsigned char *p = NULL, *q; 252 long tmplen; 253 int i; 254 int ret = 0; 255 256 if (it->itype != ASN1_ITYPE_NDEF_SEQUENCE && 257 it->itype != ASN1_ITYPE_SEQUENCE) 258 goto err; 259 260 if (aux && aux->asn1_cb) 261 asn1_cb = aux->asn1_cb; 262 263 p = *in; 264 tmplen = len; 265 266 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */ 267 if (tag == -1) { 268 tag = V_ASN1_SEQUENCE; 269 aclass = V_ASN1_UNIVERSAL; 270 } 271 /* Get SEQUENCE length and update len, p */ 272 ret = asn1_check_tag(&len, NULL, NULL, &seq_eoc, &cst, &p, len, 273 tag, aclass, opt); 274 if (!ret) { 275 ASN1error(ERR_R_NESTED_ASN1_ERROR); 276 goto err; 277 } else if (ret == -1) 278 return -1; 279 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) { 280 len = tmplen - (p - *in); 281 seq_nolen = 1; 282 } 283 /* If indefinite we don't do a length check */ 284 else 285 seq_nolen = seq_eoc; 286 if (!cst) { 287 ASN1error(ASN1_R_SEQUENCE_NOT_CONSTRUCTED); 288 goto err; 289 } 290 291 if (!*pval && !ASN1_item_ex_new(pval, it)) { 292 ASN1error(ERR_R_NESTED_ASN1_ERROR); 293 goto err; 294 } 295 296 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) 297 goto auxerr; 298 299 /* Free up and zero any ADB found */ 300 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { 301 if (tt->flags & ASN1_TFLG_ADB_MASK) { 302 const ASN1_TEMPLATE *seqtt; 303 ASN1_VALUE **pseqval; 304 seqtt = asn1_do_adb(pval, tt, 1); 305 if (!seqtt) 306 goto err; 307 pseqval = asn1_get_field_ptr(pval, seqtt); 308 ASN1_template_free(pseqval, seqtt); 309 } 310 } 311 312 /* Get each field entry */ 313 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { 314 const ASN1_TEMPLATE *seqtt; 315 ASN1_VALUE **pseqval; 316 seqtt = asn1_do_adb(pval, tt, 1); 317 if (!seqtt) 318 goto err; 319 pseqval = asn1_get_field_ptr(pval, seqtt); 320 /* Have we ran out of data? */ 321 if (!len) 322 break; 323 q = p; 324 if (asn1_check_eoc(&p, len)) { 325 if (!seq_eoc) { 326 ASN1error(ASN1_R_UNEXPECTED_EOC); 327 goto err; 328 } 329 len -= p - q; 330 seq_eoc = 0; 331 q = p; 332 break; 333 } 334 /* This determines the OPTIONAL flag value. The field 335 * cannot be omitted if it is the last of a SEQUENCE 336 * and there is still data to be read. This isn't 337 * strictly necessary but it increases efficiency in 338 * some cases. 339 */ 340 if (i == (it->tcount - 1)) 341 isopt = 0; 342 else 343 isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL); 344 /* attempt to read in field, allowing each to be 345 * OPTIONAL */ 346 347 ret = asn1_template_ex_d2i(pseqval, &p, len, 348 seqtt, isopt, depth); 349 if (!ret) { 350 errtt = seqtt; 351 goto err; 352 } else if (ret == -1) { 353 /* OPTIONAL component absent. 354 * Free and zero the field. 355 */ 356 ASN1_template_free(pseqval, seqtt); 357 continue; 358 } 359 /* Update length */ 360 len -= p - q; 361 } 362 363 /* Check for EOC if expecting one */ 364 if (seq_eoc && !asn1_check_eoc(&p, len)) { 365 ASN1error(ASN1_R_MISSING_EOC); 366 goto err; 367 } 368 /* Check all data read */ 369 if (!seq_nolen && len) { 370 ASN1error(ASN1_R_SEQUENCE_LENGTH_MISMATCH); 371 goto err; 372 } 373 374 /* If we get here we've got no more data in the SEQUENCE, 375 * however we may not have read all fields so check all 376 * remaining are OPTIONAL and clear any that are. 377 */ 378 for (; i < it->tcount; tt++, i++) { 379 const ASN1_TEMPLATE *seqtt; 380 seqtt = asn1_do_adb(pval, tt, 1); 381 if (!seqtt) 382 goto err; 383 if (seqtt->flags & ASN1_TFLG_OPTIONAL) { 384 ASN1_VALUE **pseqval; 385 pseqval = asn1_get_field_ptr(pval, seqtt); 386 ASN1_template_free(pseqval, seqtt); 387 } else { 388 errtt = seqtt; 389 ASN1error(ASN1_R_FIELD_MISSING); 390 goto err; 391 } 392 } 393 /* Save encoding */ 394 if (!asn1_enc_save(pval, *in, p - *in, it)) { 395 ASN1error(ERR_R_MALLOC_FAILURE); 396 goto auxerr; 397 } 398 *in = p; 399 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) 400 goto auxerr; 401 return 1; 402 403 auxerr: 404 ASN1error(ASN1_R_AUX_ERROR); 405 err: 406 ASN1_item_ex_free(pval, it); 407 408 if (errtt) 409 ERR_asprintf_error_data("Field=%s, Type=%s", errtt->field_name, 410 it->sname); 411 else 412 ERR_asprintf_error_data("Type=%s", it->sname); 413 return 0; 414 } 415 416 static int 417 asn1_item_ex_d2i_sequence_cbs(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it, 418 int tag_number, int tag_class, char optional, int depth) 419 { 420 const unsigned char *p; 421 int ret; 422 423 if (CBS_len(cbs) > LONG_MAX) 424 return 0; 425 426 p = CBS_data(cbs); 427 ret = asn1_item_ex_d2i_sequence(pval, &p, (long)CBS_len(cbs), it, 428 tag_number, tag_class, optional, depth); 429 if (ret == 1) { 430 if (!CBS_skip(cbs, p - CBS_data(cbs))) 431 return 0; 432 } 433 return ret; 434 } 435 436 /* 437 * Decode an item, taking care of IMPLICIT tagging, if any. 438 * If 'opt' set and tag mismatch return -1 to handle OPTIONAL 439 */ 440 static int 441 asn1_item_ex_d2i_cbs(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it, 442 int tag_number, int tag_class, char optional, int depth) 443 { 444 const ASN1_EXTERN_FUNCS *ef = it->funcs; 445 const unsigned char *p = NULL; 446 ASN1_TLC ctx = { 0 }; 447 CBS cbs_mstring; 448 unsigned char oclass; 449 int otag; 450 int ret = 0; 451 452 if (pval == NULL) 453 return 0; 454 455 if (++depth > ASN1_MAX_CONSTRUCTED_NEST) { 456 ASN1error(ASN1_R_NESTED_TOO_DEEP); 457 goto err; 458 } 459 460 switch (it->itype) { 461 case ASN1_ITYPE_PRIMITIVE: 462 if (it->templates != NULL) { 463 /* 464 * Tagging or OPTIONAL is currently illegal on an item 465 * template because the flags can't get passed down. 466 * In practice this isn't a problem: we include the 467 * relevant flags from the item template in the 468 * template itself. 469 */ 470 if (tag_number != -1 || optional) { 471 ASN1error(ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE); 472 goto err; 473 } 474 return asn1_template_ex_d2i_cbs(pval, cbs, 475 it->templates, optional, depth); 476 } 477 return asn1_d2i_ex_primitive(pval, cbs, it, tag_number, 478 tag_class, optional); 479 480 case ASN1_ITYPE_MSTRING: 481 /* 482 * It never makes sense for multi-strings to have implicit 483 * tagging, so if tag != -1, then this looks like an error in 484 * the template. 485 */ 486 if (tag_number != -1) { 487 ASN1error(ASN1_R_BAD_TEMPLATE); 488 goto err; 489 } 490 491 /* XXX - avoid reading the header twice for MSTRING. */ 492 CBS_dup(cbs, &cbs_mstring); 493 if (asn1_check_tag_cbs(&cbs_mstring, NULL, &otag, &oclass, 494 NULL, NULL, -1, 0, 1) != 1) { 495 ASN1error(ERR_R_NESTED_ASN1_ERROR); 496 goto err; 497 } 498 499 /* Must be UNIVERSAL class */ 500 if (oclass != V_ASN1_UNIVERSAL) { 501 if (optional) 502 return -1; 503 ASN1error(ASN1_R_MSTRING_NOT_UNIVERSAL); 504 goto err; 505 } 506 /* Check tag matches bit map */ 507 if (!(ASN1_tag2bit(otag) & it->utype)) { 508 if (optional) 509 return -1; 510 ASN1error(ASN1_R_MSTRING_WRONG_TAG); 511 goto err; 512 } 513 return asn1_d2i_ex_primitive(pval, cbs, it, otag, 0, 0); 514 515 case ASN1_ITYPE_EXTERN: 516 if (CBS_len(cbs) > LONG_MAX) 517 return 0; 518 p = CBS_data(cbs); 519 if ((ret = ef->asn1_ex_d2i(pval, &p, (long)CBS_len(cbs), it, 520 tag_number, tag_class, optional, &ctx)) == 1) { 521 if (!CBS_skip(cbs, p - CBS_data(cbs))) 522 goto err; 523 } 524 return ret; 525 526 case ASN1_ITYPE_CHOICE: 527 return asn1_item_ex_d2i_choice_cbs(pval, cbs, it, tag_number, 528 tag_class, optional, depth); 529 530 case ASN1_ITYPE_NDEF_SEQUENCE: 531 case ASN1_ITYPE_SEQUENCE: 532 return asn1_item_ex_d2i_sequence_cbs(pval, cbs, it, tag_number, 533 tag_class, optional, depth); 534 535 default: 536 return 0; 537 } 538 539 err: 540 ASN1_item_ex_free(pval, it); 541 542 ERR_asprintf_error_data("Type=%s", it->sname); 543 544 return 0; 545 } 546 547 static int 548 asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long inlen, 549 const ASN1_ITEM *it, int tag_number, int tag_class, char optional, 550 int depth) 551 { 552 CBS cbs; 553 int ret; 554 555 if (inlen < 0) 556 return 0; 557 558 CBS_init(&cbs, *in, inlen); 559 560 ret = asn1_item_ex_d2i_cbs(pval, &cbs, it, tag_number, tag_class, 561 optional, depth); 562 563 if (ret == 1) 564 *in = CBS_data(&cbs); 565 566 return ret; 567 } 568 569 int 570 ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, 571 const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx) 572 { 573 return asn1_item_ex_d2i(pval, in, len, it, tag, aclass, opt, 0); 574 } 575 576 /* Templates are handled with two separate functions. 577 * One handles any EXPLICIT tag and the other handles the rest. 578 */ 579 580 static int 581 asn1_template_ex_d2i(ASN1_VALUE **val, const unsigned char **in, long inlen, 582 const ASN1_TEMPLATE *tt, char opt, int depth) 583 { 584 int flags, aclass; 585 int ret; 586 long len; 587 const unsigned char *p, *q; 588 char exp_eoc; 589 590 if (!val) 591 return 0; 592 flags = tt->flags; 593 aclass = flags & ASN1_TFLG_TAG_CLASS; 594 595 p = *in; 596 597 /* Check if EXPLICIT tag expected */ 598 if (flags & ASN1_TFLG_EXPTAG) { 599 char cst; 600 /* Need to work out amount of data available to the inner 601 * content and where it starts: so read in EXPLICIT header to 602 * get the info. 603 */ 604 ret = asn1_check_tag(&len, NULL, NULL, &exp_eoc, &cst, &p, 605 inlen, tt->tag, aclass, opt); 606 q = p; 607 if (!ret) { 608 ASN1error(ERR_R_NESTED_ASN1_ERROR); 609 return 0; 610 } else if (ret == -1) 611 return -1; 612 if (!cst) { 613 ASN1error(ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED); 614 return 0; 615 } 616 /* We've found the field so it can't be OPTIONAL now */ 617 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, depth); 618 if (!ret) { 619 ASN1error(ERR_R_NESTED_ASN1_ERROR); 620 return 0; 621 } 622 /* We read the field in OK so update length */ 623 len -= p - q; 624 if (exp_eoc) { 625 /* If NDEF we must have an EOC here */ 626 if (!asn1_check_eoc(&p, len)) { 627 ASN1error(ASN1_R_MISSING_EOC); 628 goto err; 629 } 630 } else { 631 /* Otherwise we must hit the EXPLICIT tag end or its 632 * an error */ 633 if (len) { 634 ASN1error(ASN1_R_EXPLICIT_LENGTH_MISMATCH); 635 goto err; 636 } 637 } 638 } else 639 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, depth); 640 641 *in = p; 642 return 1; 643 644 err: 645 ASN1_template_free(val, tt); 646 return 0; 647 } 648 649 static int 650 asn1_template_ex_d2i_cbs(ASN1_VALUE **pval, CBS *cbs, const ASN1_TEMPLATE *tt, 651 char optional, int depth) 652 { 653 const unsigned char *p; 654 int ret; 655 656 if (CBS_len(cbs) > LONG_MAX) 657 return 0; 658 659 p = CBS_data(cbs); 660 ret = asn1_template_ex_d2i(pval, &p, (long)CBS_len(cbs), tt, 661 optional, depth); 662 if (ret == 1) { 663 if (!CBS_skip(cbs, p - CBS_data(cbs))) 664 return 0; 665 } 666 return ret; 667 } 668 669 static int 670 asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in, long len, 671 const ASN1_TEMPLATE *tt, char opt, int depth) 672 { 673 int flags, aclass; 674 int ret; 675 const unsigned char *p, *q; 676 677 if (!val) 678 return 0; 679 flags = tt->flags; 680 aclass = flags & ASN1_TFLG_TAG_CLASS; 681 682 p = *in; 683 q = p; 684 685 if (flags & ASN1_TFLG_SK_MASK) { 686 /* SET OF, SEQUENCE OF */ 687 int sktag, skaclass; 688 char sk_eoc; 689 /* First work out expected inner tag value */ 690 if (flags & ASN1_TFLG_IMPTAG) { 691 sktag = tt->tag; 692 skaclass = aclass; 693 } else { 694 skaclass = V_ASN1_UNIVERSAL; 695 if (flags & ASN1_TFLG_SET_OF) 696 sktag = V_ASN1_SET; 697 else 698 sktag = V_ASN1_SEQUENCE; 699 } 700 /* Get the tag */ 701 ret = asn1_check_tag(&len, NULL, NULL, &sk_eoc, NULL, &p, len, 702 sktag, skaclass, opt); 703 if (!ret) { 704 ASN1error(ERR_R_NESTED_ASN1_ERROR); 705 return 0; 706 } else if (ret == -1) 707 return -1; 708 if (!*val) 709 *val = (ASN1_VALUE *)sk_new_null(); 710 else { 711 /* We've got a valid STACK: free up any items present */ 712 STACK_OF(ASN1_VALUE) *sktmp = 713 (STACK_OF(ASN1_VALUE) *)*val; 714 ASN1_VALUE *vtmp; 715 while (sk_ASN1_VALUE_num(sktmp) > 0) { 716 vtmp = sk_ASN1_VALUE_pop(sktmp); 717 ASN1_item_ex_free(&vtmp, 718 tt->item); 719 } 720 } 721 722 if (!*val) { 723 ASN1error(ERR_R_MALLOC_FAILURE); 724 goto err; 725 } 726 727 /* Read as many items as we can */ 728 while (len > 0) { 729 ASN1_VALUE *skfield; 730 q = p; 731 /* See if EOC found */ 732 if (asn1_check_eoc(&p, len)) { 733 if (!sk_eoc) { 734 ASN1error(ASN1_R_UNEXPECTED_EOC); 735 goto err; 736 } 737 len -= p - q; 738 sk_eoc = 0; 739 break; 740 } 741 skfield = NULL; 742 if (!asn1_item_ex_d2i(&skfield, &p, len, 743 tt->item, -1, 0, 0, depth)) { 744 ASN1error(ERR_R_NESTED_ASN1_ERROR); 745 goto err; 746 } 747 len -= p - q; 748 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, 749 skfield)) { 750 ASN1error(ERR_R_MALLOC_FAILURE); 751 goto err; 752 } 753 } 754 if (sk_eoc) { 755 ASN1error(ASN1_R_MISSING_EOC); 756 goto err; 757 } 758 } else if (flags & ASN1_TFLG_IMPTAG) { 759 /* IMPLICIT tagging */ 760 ret = asn1_item_ex_d2i(val, &p, len, 761 tt->item, tt->tag, aclass, opt, depth); 762 if (!ret) { 763 ASN1error(ERR_R_NESTED_ASN1_ERROR); 764 goto err; 765 } else if (ret == -1) 766 return -1; 767 } else { 768 /* Nothing special */ 769 ret = asn1_item_ex_d2i(val, &p, len, tt->item, -1, 0, 770 opt, depth); 771 if (!ret) { 772 ASN1error(ERR_R_NESTED_ASN1_ERROR); 773 goto err; 774 } else if (ret == -1) 775 return -1; 776 } 777 778 *in = p; 779 return 1; 780 781 err: 782 ASN1_template_free(val, tt); 783 return 0; 784 } 785 786 static int 787 asn1_d2i_ex_primitive(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it, 788 int tag_number, int tag_class, char optional) 789 { 790 CBS cbs_any, cbs_content, cbs_object, cbs_initial; 791 char constructed, indefinite; 792 uint8_t *data = NULL; 793 size_t data_len = 0; 794 int utype = it->utype; 795 unsigned char oclass; 796 size_t length; 797 CBB cbb; 798 int tag_ret; 799 int ret = 0; 800 801 memset(&cbb, 0, sizeof(cbb)); 802 803 CBS_dup(cbs, &cbs_initial); 804 CBS_init(&cbs_content, NULL, 0); 805 CBS_init(&cbs_object, CBS_data(cbs), CBS_len(cbs)); 806 807 if (pval == NULL) { 808 ASN1error(ASN1_R_ILLEGAL_NULL); 809 goto err; 810 } 811 812 if (it->itype == ASN1_ITYPE_MSTRING) { 813 utype = tag_number; 814 tag_number = -1; 815 } 816 817 if (utype == V_ASN1_ANY) { 818 /* Determine type from ASN.1 tag. */ 819 if (tag_number >= 0) { 820 ASN1error(ASN1_R_ILLEGAL_TAGGED_ANY); 821 goto err; 822 } 823 if (optional) { 824 ASN1error(ASN1_R_ILLEGAL_OPTIONAL_ANY); 825 goto err; 826 } 827 /* XXX - avoid reading the header twice for ANY. */ 828 CBS_dup(&cbs_object, &cbs_any); 829 tag_ret = asn1_check_tag_cbs(&cbs_any, NULL, &utype, &oclass, 830 NULL, NULL, -1, 0, 0); 831 if (tag_ret != 1) { 832 ASN1error(ERR_R_NESTED_ASN1_ERROR); 833 goto err; 834 } 835 if (oclass != V_ASN1_UNIVERSAL) 836 utype = V_ASN1_OTHER; 837 } 838 839 if (tag_number == -1) { 840 tag_number = utype; 841 tag_class = V_ASN1_UNIVERSAL; 842 } 843 844 tag_ret = asn1_check_tag_cbs(&cbs_object, &length, NULL, NULL, &indefinite, 845 &constructed, tag_number, tag_class, optional); 846 if (tag_ret == -1) { 847 ret = -1; 848 goto err; 849 } 850 if (tag_ret != 1) { 851 ASN1error(ERR_R_NESTED_ASN1_ERROR); 852 goto err; 853 } 854 855 /* SEQUENCE and SET must be constructed. */ 856 if ((utype == V_ASN1_SEQUENCE || utype == V_ASN1_SET) && !constructed) { 857 ASN1error(ASN1_R_TYPE_NOT_CONSTRUCTED); 858 goto err; 859 } 860 861 /* SEQUENCE, SET and "OTHER" are left in encoded form. */ 862 if (utype == V_ASN1_SEQUENCE || utype == V_ASN1_SET || 863 utype == V_ASN1_OTHER) { 864 if (!asn1_find_end(&cbs_object, length, indefinite)) 865 goto err; 866 if (!CBS_get_bytes(&cbs_initial, &cbs_content, 867 CBS_offset(&cbs_object))) 868 goto err; 869 } else if (constructed) { 870 /* 871 * Should really check the internal tags are correct but 872 * some things may get this wrong. The relevant specs 873 * say that constructed string types should be OCTET STRINGs 874 * internally irrespective of the type. So instead just check 875 * for UNIVERSAL class and ignore the tag. 876 */ 877 if (!CBB_init(&cbb, 0)) 878 goto err; 879 if (!asn1_collect(&cbb, &cbs_object, indefinite, -1, 880 V_ASN1_UNIVERSAL, 0)) 881 goto err; 882 if (!CBB_finish(&cbb, &data, &data_len)) 883 goto err; 884 885 CBS_init(&cbs_content, data, data_len); 886 } else { 887 if (!CBS_get_bytes(&cbs_object, &cbs_content, length)) 888 goto err; 889 } 890 891 if (!asn1_ex_c2i(pval, &cbs_content, utype, it)) 892 goto err; 893 894 if (!CBS_skip(cbs, CBS_offset(&cbs_object))) 895 goto err; 896 897 ret = 1; 898 899 err: 900 CBB_cleanup(&cbb); 901 freezero(data, data_len); 902 903 return ret; 904 } 905 906 static int 907 asn1_ex_c2i_primitive(ASN1_VALUE **pval, CBS *content, int utype, const ASN1_ITEM *it) 908 { 909 ASN1_STRING *stmp; 910 ASN1_INTEGER **tint; 911 ASN1_BOOLEAN *tbool; 912 uint8_t u8val; 913 int ret = 0; 914 915 if (it->funcs != NULL) 916 return 0; 917 918 if (CBS_len(content) > INT_MAX) 919 return 0; 920 921 switch (utype) { 922 case V_ASN1_OBJECT: 923 if (!c2i_ASN1_OBJECT_cbs((ASN1_OBJECT **)pval, content)) 924 goto err; 925 break; 926 927 case V_ASN1_NULL: 928 if (CBS_len(content) != 0) { 929 ASN1error(ASN1_R_NULL_IS_WRONG_LENGTH); 930 goto err; 931 } 932 *pval = (ASN1_VALUE *)1; 933 break; 934 935 case V_ASN1_BOOLEAN: 936 tbool = (ASN1_BOOLEAN *)pval; 937 if (CBS_len(content) != 1) { 938 ASN1error(ASN1_R_BOOLEAN_IS_WRONG_LENGTH); 939 goto err; 940 } 941 if (!CBS_get_u8(content, &u8val)) 942 goto err; 943 *tbool = u8val; 944 break; 945 946 case V_ASN1_BIT_STRING: 947 if (!c2i_ASN1_BIT_STRING_cbs((ASN1_BIT_STRING **)pval, content)) 948 goto err; 949 break; 950 951 case V_ASN1_INTEGER: 952 case V_ASN1_ENUMERATED: 953 tint = (ASN1_INTEGER **)pval; 954 if (!c2i_ASN1_INTEGER_cbs(tint, content)) 955 goto err; 956 /* Fixup type to match the expected form */ 957 (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG); 958 break; 959 960 case V_ASN1_OCTET_STRING: 961 case V_ASN1_NUMERICSTRING: 962 case V_ASN1_PRINTABLESTRING: 963 case V_ASN1_T61STRING: 964 case V_ASN1_VIDEOTEXSTRING: 965 case V_ASN1_IA5STRING: 966 case V_ASN1_UTCTIME: 967 case V_ASN1_GENERALIZEDTIME: 968 case V_ASN1_GRAPHICSTRING: 969 case V_ASN1_VISIBLESTRING: 970 case V_ASN1_GENERALSTRING: 971 case V_ASN1_UNIVERSALSTRING: 972 case V_ASN1_BMPSTRING: 973 case V_ASN1_UTF8STRING: 974 case V_ASN1_OTHER: 975 case V_ASN1_SET: 976 case V_ASN1_SEQUENCE: 977 default: 978 if (utype == V_ASN1_BMPSTRING && (CBS_len(content) & 1)) { 979 ASN1error(ASN1_R_BMPSTRING_IS_WRONG_LENGTH); 980 goto err; 981 } 982 if (utype == V_ASN1_UNIVERSALSTRING && (CBS_len(content) & 3)) { 983 ASN1error(ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH); 984 goto err; 985 } 986 /* All based on ASN1_STRING and handled the same way. */ 987 if (*pval == NULL) { 988 if ((stmp = ASN1_STRING_type_new(utype)) == NULL) { 989 ASN1error(ERR_R_MALLOC_FAILURE); 990 goto err; 991 } 992 *pval = (ASN1_VALUE *)stmp; 993 } else { 994 stmp = (ASN1_STRING *)*pval; 995 stmp->type = utype; 996 } 997 if (!ASN1_STRING_set(stmp, CBS_data(content), CBS_len(content))) { 998 ASN1_STRING_free(stmp); 999 *pval = NULL; 1000 goto err; 1001 } 1002 break; 1003 } 1004 1005 ret = 1; 1006 1007 err: 1008 return ret; 1009 } 1010 1011 static int 1012 asn1_ex_c2i_any(ASN1_VALUE **pval, CBS *content, int utype, const ASN1_ITEM *it) 1013 { 1014 ASN1_TYPE *atype; 1015 1016 if (it->utype != V_ASN1_ANY || it->funcs != NULL) 1017 return 0; 1018 1019 if (*pval != NULL) { 1020 ASN1_TYPE_free((ASN1_TYPE *)*pval); 1021 *pval = NULL; 1022 } 1023 1024 if ((atype = ASN1_TYPE_new()) == NULL) 1025 return 0; 1026 1027 if (!asn1_ex_c2i_primitive(&atype->value.asn1_value, content, utype, it)) { 1028 ASN1_TYPE_free(atype); 1029 return 0; 1030 } 1031 atype->type = utype; 1032 1033 /* Fix up value for ASN.1 NULL. */ 1034 if (atype->type == V_ASN1_NULL) 1035 atype->value.ptr = NULL; 1036 1037 *pval = (ASN1_VALUE *)atype; 1038 1039 return 1; 1040 } 1041 1042 static int 1043 asn1_ex_c2i(ASN1_VALUE **pval, CBS *content, int utype, const ASN1_ITEM *it) 1044 { 1045 if (CBS_len(content) > INT_MAX) 1046 return 0; 1047 1048 if (it->funcs != NULL) { 1049 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; 1050 char free_content = 0; 1051 1052 if (pf->prim_c2i == NULL) 1053 return 0; 1054 1055 return pf->prim_c2i(pval, CBS_data(content), CBS_len(content), 1056 utype, &free_content, it); 1057 } 1058 1059 if (it->utype == V_ASN1_ANY) 1060 return asn1_ex_c2i_any(pval, content, utype, it); 1061 1062 return asn1_ex_c2i_primitive(pval, content, utype, it); 1063 } 1064 1065 /* Find the end of an ASN.1 object. */ 1066 static int 1067 asn1_find_end(CBS *cbs, size_t length, char indefinite) 1068 { 1069 size_t eoc_count; 1070 1071 if (!indefinite) { 1072 if (!CBS_skip(cbs, length)) { 1073 ASN1error(ERR_R_NESTED_ASN1_ERROR); 1074 return 0; 1075 } 1076 return 1; 1077 } 1078 1079 eoc_count = 1; 1080 1081 while (CBS_len(cbs) > 0) { 1082 if (asn1_check_eoc_cbs(cbs)) { 1083 if (--eoc_count == 0) 1084 break; 1085 continue; 1086 } 1087 if (!asn1_check_tag_cbs(cbs, &length, NULL, NULL, 1088 &indefinite, NULL, -1, 0, 0)) { 1089 ASN1error(ERR_R_NESTED_ASN1_ERROR); 1090 return 0; 1091 } 1092 if (indefinite) { 1093 eoc_count++; 1094 continue; 1095 } 1096 if (!CBS_skip(cbs, length)) 1097 return 0; 1098 } 1099 1100 if (eoc_count > 0) { 1101 ASN1error(ASN1_R_MISSING_EOC); 1102 return 0; 1103 } 1104 1105 return 1; 1106 } 1107 1108 #ifndef ASN1_MAX_STRING_NEST 1109 /* This determines how many levels of recursion are permitted in ASN1 1110 * string types. If it is not limited stack overflows can occur. If set 1111 * to zero no recursion is allowed at all. Although zero should be adequate 1112 * examples exist that require a value of 1. So 5 should be more than enough. 1113 */ 1114 #define ASN1_MAX_STRING_NEST 5 1115 #endif 1116 1117 /* Collect the contents from a constructed ASN.1 object. */ 1118 static int 1119 asn1_collect(CBB *cbb, CBS *cbs, char indefinite, int expected_tag, 1120 int expected_class, int depth) 1121 { 1122 char constructed; 1123 size_t length; 1124 CBS content; 1125 int need_eoc; 1126 1127 if (depth > ASN1_MAX_STRING_NEST) { 1128 ASN1error(ASN1_R_NESTED_ASN1_STRING); 1129 return 0; 1130 } 1131 1132 need_eoc = indefinite; 1133 1134 while (CBS_len(cbs) > 0) { 1135 if (asn1_check_eoc_cbs(cbs)) { 1136 if (!need_eoc) { 1137 ASN1error(ASN1_R_UNEXPECTED_EOC); 1138 return 0; 1139 } 1140 return 1; 1141 } 1142 if (!asn1_check_tag_cbs(cbs, &length, NULL, NULL, &indefinite, 1143 &constructed, expected_tag, expected_class, 0)) { 1144 ASN1error(ERR_R_NESTED_ASN1_ERROR); 1145 return 0; 1146 } 1147 1148 if (constructed) { 1149 if (!asn1_collect(cbb, cbs, indefinite, expected_tag, 1150 expected_class, depth + 1)) 1151 return 0; 1152 continue; 1153 } 1154 1155 if (!CBS_get_bytes(cbs, &content, length)) { 1156 ASN1error(ERR_R_NESTED_ASN1_ERROR); 1157 return 0; 1158 } 1159 if (!CBB_add_bytes(cbb, CBS_data(&content), CBS_len(&content))) 1160 return 0; 1161 } 1162 1163 if (need_eoc) { 1164 ASN1error(ASN1_R_MISSING_EOC); 1165 return 0; 1166 } 1167 1168 return 1; 1169 } 1170 1171 static int 1172 asn1_check_eoc_cbs(CBS *cbs) 1173 { 1174 uint16_t eoc; 1175 1176 if (!CBS_peek_u16(cbs, &eoc)) 1177 return 0; 1178 if (eoc != 0) 1179 return 0; 1180 1181 return CBS_skip(cbs, 2); 1182 } 1183 1184 static int 1185 asn1_check_eoc(const unsigned char **in, long len) 1186 { 1187 const unsigned char *p; 1188 1189 if (len < 2) 1190 return 0; 1191 p = *in; 1192 if (!p[0] && !p[1]) { 1193 *in += 2; 1194 return 1; 1195 } 1196 return 0; 1197 } 1198 1199 static int 1200 asn1_check_tag_cbs(CBS *cbs, size_t *out_len, int *out_tag, uint8_t *out_class, 1201 char *out_indefinite, char *out_constructed, int expected_tag, 1202 int expected_class, char optional) 1203 { 1204 int constructed, indefinite; 1205 uint32_t tag_number; 1206 uint8_t tag_class; 1207 size_t length; 1208 1209 if (out_len != NULL) 1210 *out_len = 0; 1211 if (out_tag != NULL) 1212 *out_tag = 0; 1213 if (out_class != NULL) 1214 *out_class = 0; 1215 if (out_indefinite != NULL) 1216 *out_indefinite = 0; 1217 if (out_constructed != NULL) 1218 *out_constructed = 0; 1219 1220 if (!asn1_get_identifier_cbs(cbs, 0, &tag_class, &constructed, 1221 &tag_number)) { 1222 ASN1error(ASN1_R_BAD_OBJECT_HEADER); 1223 return 0; 1224 } 1225 if (expected_tag >= 0) { 1226 if (expected_tag != tag_number || 1227 expected_class != tag_class << 6) { 1228 /* Indicate missing type if this is OPTIONAL. */ 1229 if (optional) 1230 return -1; 1231 1232 ASN1error(ASN1_R_WRONG_TAG); 1233 return 0; 1234 } 1235 } 1236 if (!asn1_get_length_cbs(cbs, 0, &indefinite, &length)) { 1237 ASN1error(ASN1_R_BAD_OBJECT_HEADER); 1238 return 0; 1239 } 1240 1241 /* Indefinite length can only be used with constructed encoding. */ 1242 if (indefinite && !constructed) { 1243 ASN1error(ASN1_R_BAD_OBJECT_HEADER); 1244 return 0; 1245 } 1246 1247 if (!indefinite && CBS_len(cbs) < length) { 1248 ASN1error(ASN1_R_TOO_LONG); 1249 return 0; 1250 } 1251 1252 if (tag_number > INT_MAX) { 1253 ASN1error(ASN1_R_TOO_LONG); 1254 return 0; 1255 } 1256 1257 if (indefinite) 1258 length = CBS_len(cbs); 1259 1260 if (out_len != NULL) 1261 *out_len = length; 1262 if (out_tag != NULL) 1263 *out_tag = tag_number; 1264 if (out_class != NULL) 1265 *out_class = tag_class << 6; 1266 if (out_indefinite != NULL && indefinite) 1267 *out_indefinite = 1 << 0; 1268 if (out_constructed != NULL && constructed) 1269 *out_constructed = 1 << 5; 1270 1271 return 1; 1272 } 1273 1274 static int 1275 asn1_check_tag(long *out_len, int *out_tag, unsigned char *out_class, 1276 char *out_indefinite, char *out_constructed, const unsigned char **in, 1277 long len, int expected_tag, int expected_class, char optional) 1278 { 1279 size_t length; 1280 CBS cbs; 1281 int ret; 1282 1283 if (len < 0) 1284 return 0; 1285 1286 CBS_init(&cbs, *in, len); 1287 1288 ret = asn1_check_tag_cbs(&cbs, &length, out_tag, out_class, 1289 out_indefinite, out_constructed, expected_tag, expected_class, 1290 optional); 1291 1292 if (length > LONG_MAX) 1293 return 0; 1294 if (out_len != NULL) 1295 *out_len = (long)length; 1296 1297 if (ret == 1) 1298 *in = CBS_data(&cbs); 1299 1300 return ret; 1301 } 1302