1 /* $OpenBSD: tasn_prn.c,v 1.24 2023/04/17 08:43:16 tb 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 <stddef.h> 60 61 #include <openssl/asn1.h> 62 #include <openssl/asn1t.h> 63 #include <openssl/buffer.h> 64 #include <openssl/err.h> 65 #include <openssl/objects.h> 66 #include <openssl/x509v3.h> 67 68 #include "asn1_local.h" 69 70 /* Print routines. 71 */ 72 73 /* ASN1_PCTX routines */ 74 75 static const ASN1_PCTX default_pctx = { 76 .flags = ASN1_PCTX_FLAGS_SHOW_ABSENT, 77 }; 78 79 ASN1_PCTX * 80 ASN1_PCTX_new(void) 81 { 82 ASN1_PCTX *p; 83 84 if ((p = calloc(1, sizeof(ASN1_PCTX))) == NULL) { 85 ASN1error(ERR_R_MALLOC_FAILURE); 86 return NULL; 87 } 88 89 return p; 90 } 91 92 void 93 ASN1_PCTX_free(ASN1_PCTX *p) 94 { 95 free(p); 96 } 97 98 unsigned long 99 ASN1_PCTX_get_flags(const ASN1_PCTX *p) 100 { 101 return p->flags; 102 } 103 104 void 105 ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags) 106 { 107 p->flags = flags; 108 } 109 110 unsigned long 111 ASN1_PCTX_get_nm_flags(const ASN1_PCTX *p) 112 { 113 return p->nm_flags; 114 } 115 116 void 117 ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags) 118 { 119 p->nm_flags = flags; 120 } 121 122 unsigned long 123 ASN1_PCTX_get_cert_flags(const ASN1_PCTX *p) 124 { 125 return p->cert_flags; 126 } 127 128 void 129 ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags) 130 { 131 p->cert_flags = flags; 132 } 133 134 unsigned long 135 ASN1_PCTX_get_oid_flags(const ASN1_PCTX *p) 136 { 137 return p->oid_flags; 138 } 139 140 void 141 ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags) 142 { 143 p->oid_flags = flags; 144 } 145 146 unsigned long 147 ASN1_PCTX_get_str_flags(const ASN1_PCTX *p) 148 { 149 return p->str_flags; 150 } 151 152 void 153 ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags) 154 { 155 p->str_flags = flags; 156 } 157 158 /* Main print routines */ 159 160 static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent, 161 const ASN1_ITEM *it, const char *fname, const char *sname, int nohdr, 162 const ASN1_PCTX *pctx); 163 164 int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent, 165 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx); 166 167 static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld, 168 const ASN1_ITEM *it, int indent, const char *fname, const char *sname, 169 const ASN1_PCTX *pctx); 170 171 static int asn1_print_fsname(BIO *out, int indent, const char *fname, 172 const char *sname, const ASN1_PCTX *pctx); 173 174 int 175 ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent, const ASN1_ITEM *it, 176 const ASN1_PCTX *pctx) 177 { 178 const char *sname; 179 180 if (pctx == NULL) 181 pctx = &default_pctx; 182 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME) 183 sname = NULL; 184 else 185 sname = it->sname; 186 return asn1_item_print_ctx(out, &ifld, indent, it, NULL, sname, 187 0, pctx); 188 } 189 190 static int 191 asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent, const ASN1_ITEM *it, 192 const char *fname, const char *sname, int nohdr, const ASN1_PCTX *pctx) 193 { 194 const ASN1_TEMPLATE *tt; 195 const ASN1_EXTERN_FUNCS *ef; 196 ASN1_VALUE **tmpfld; 197 const ASN1_AUX *aux = it->funcs; 198 ASN1_aux_cb *asn1_cb; 199 ASN1_PRINT_ARG parg; 200 int i; 201 202 if (aux && aux->asn1_cb) { 203 parg.out = out; 204 parg.indent = indent; 205 parg.pctx = pctx; 206 asn1_cb = aux->asn1_cb; 207 } else 208 asn1_cb = NULL; 209 210 if ((it->itype != ASN1_ITYPE_PRIMITIVE || 211 it->utype != V_ASN1_BOOLEAN) && *fld == NULL) { 212 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT) { 213 if (!nohdr && 214 !asn1_print_fsname(out, indent, fname, sname, pctx)) 215 return 0; 216 if (BIO_puts(out, "<ABSENT>\n") <= 0) 217 return 0; 218 } 219 return 1; 220 } 221 222 switch (it->itype) { 223 case ASN1_ITYPE_PRIMITIVE: 224 if (it->templates) { 225 if (!asn1_template_print_ctx(out, fld, indent, 226 it->templates, pctx)) 227 return 0; 228 } 229 /* fall thru */ 230 case ASN1_ITYPE_MSTRING: 231 if (!asn1_primitive_print(out, fld, it, 232 indent, fname, sname, pctx)) 233 return 0; 234 break; 235 236 case ASN1_ITYPE_EXTERN: 237 if (!nohdr && 238 !asn1_print_fsname(out, indent, fname, sname, pctx)) 239 return 0; 240 /* Use new style print routine if possible */ 241 ef = it->funcs; 242 if (ef && ef->asn1_ex_print) { 243 i = ef->asn1_ex_print(out, fld, indent, "", pctx); 244 if (!i) 245 return 0; 246 if ((i == 2) && (BIO_puts(out, "\n") <= 0)) 247 return 0; 248 return 1; 249 } else if (sname && 250 BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0) 251 return 0; 252 break; 253 254 case ASN1_ITYPE_CHOICE: 255 /* CHOICE type, get selector */ 256 i = asn1_get_choice_selector(fld, it); 257 /* This should never happen... */ 258 if ((i < 0) || (i >= it->tcount)) { 259 if (BIO_printf(out, 260 "ERROR: selector [%d] invalid\n", i) <= 0) 261 return 0; 262 return 1; 263 } 264 tt = it->templates + i; 265 tmpfld = asn1_get_field_ptr(fld, tt); 266 if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx)) 267 return 0; 268 break; 269 270 case ASN1_ITYPE_SEQUENCE: 271 case ASN1_ITYPE_NDEF_SEQUENCE: 272 if (!nohdr && 273 !asn1_print_fsname(out, indent, fname, sname, pctx)) 274 return 0; 275 if (fname || sname) { 276 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) { 277 if (BIO_puts(out, " {\n") <= 0) 278 return 0; 279 } else { 280 if (BIO_puts(out, "\n") <= 0) 281 return 0; 282 } 283 } 284 285 if (asn1_cb) { 286 i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg); 287 if (i == 0) 288 return 0; 289 if (i == 2) 290 return 1; 291 } 292 293 /* Print each field entry */ 294 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { 295 const ASN1_TEMPLATE *seqtt; 296 297 seqtt = asn1_do_adb(fld, tt, 1); 298 if (seqtt == NULL) 299 return 0; 300 tmpfld = asn1_get_field_ptr(fld, seqtt); 301 if (!asn1_template_print_ctx(out, tmpfld, indent + 2, 302 seqtt, pctx)) 303 return 0; 304 } 305 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) { 306 if (BIO_printf(out, "%*s}\n", indent, "") < 0) 307 return 0; 308 } 309 310 if (asn1_cb) { 311 i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg); 312 if (i == 0) 313 return 0; 314 } 315 break; 316 317 default: 318 BIO_printf(out, "Unprocessed type %d\n", it->itype); 319 return 0; 320 } 321 322 return 1; 323 } 324 325 int 326 asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent, 327 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx) 328 { 329 int i, flags; 330 const char *sname, *fname; 331 332 flags = tt->flags; 333 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME) 334 sname = tt->item->sname; 335 else 336 sname = NULL; 337 if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME) 338 fname = NULL; 339 else 340 fname = tt->field_name; 341 if (flags & ASN1_TFLG_SK_MASK) { 342 char *tname; 343 ASN1_VALUE *skitem; 344 STACK_OF(ASN1_VALUE) *stack; 345 346 /* SET OF, SEQUENCE OF */ 347 if (fname) { 348 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF) { 349 if (flags & ASN1_TFLG_SET_OF) 350 tname = "SET"; 351 else 352 tname = "SEQUENCE"; 353 if (BIO_printf(out, "%*s%s OF %s {\n", 354 indent, "", tname, tt->field_name) <= 0) 355 return 0; 356 } else if (BIO_printf(out, "%*s%s:\n", indent, "", 357 fname) <= 0) 358 return 0; 359 } 360 stack = (STACK_OF(ASN1_VALUE) *)*fld; 361 for (i = 0; i < sk_ASN1_VALUE_num(stack); i++) { 362 if ((i > 0) && (BIO_puts(out, "\n") <= 0)) 363 return 0; 364 skitem = sk_ASN1_VALUE_value(stack, i); 365 if (!asn1_item_print_ctx(out, &skitem, indent + 2, 366 tt->item, NULL, NULL, 1, pctx)) 367 return 0; 368 } 369 if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0) 370 return 0; 371 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) { 372 if (BIO_printf(out, "%*s}\n", indent, "") <= 0) 373 return 0; 374 } 375 return 1; 376 } 377 return asn1_item_print_ctx(out, fld, indent, tt->item, 378 fname, sname, 0, pctx); 379 } 380 381 static int 382 asn1_print_fsname(BIO *out, int indent, const char *fname, const char *sname, 383 const ASN1_PCTX *pctx) 384 { 385 static char spaces[] = " "; 386 const int nspaces = sizeof(spaces) - 1; 387 388 while (indent > nspaces) { 389 if (BIO_write(out, spaces, nspaces) != nspaces) 390 return 0; 391 indent -= nspaces; 392 } 393 if (BIO_write(out, spaces, indent) != indent) 394 return 0; 395 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME) 396 sname = NULL; 397 if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME) 398 fname = NULL; 399 if (!sname && !fname) 400 return 1; 401 if (fname) { 402 if (BIO_puts(out, fname) <= 0) 403 return 0; 404 } 405 if (sname) { 406 if (fname) { 407 if (BIO_printf(out, " (%s)", sname) <= 0) 408 return 0; 409 } else { 410 if (BIO_puts(out, sname) <= 0) 411 return 0; 412 } 413 } 414 if (BIO_write(out, ": ", 2) != 2) 415 return 0; 416 return 1; 417 } 418 419 static int 420 asn1_print_boolean_ctx(BIO *out, int boolval, const ASN1_PCTX *pctx) 421 { 422 const char *str; 423 switch (boolval) { 424 case -1: 425 str = "BOOL ABSENT"; 426 break; 427 428 case 0: 429 str = "FALSE"; 430 break; 431 432 default: 433 str = "TRUE"; 434 break; 435 436 } 437 438 if (BIO_puts(out, str) <= 0) 439 return 0; 440 return 1; 441 442 } 443 444 static int 445 asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str, const ASN1_PCTX *pctx) 446 { 447 char *s; 448 int ret = 1; 449 if ((s = i2s_ASN1_INTEGER(NULL, str)) == NULL) 450 return 0; 451 if (BIO_puts(out, s) <= 0) 452 ret = 0; 453 free(s); 454 return ret; 455 } 456 457 static int 458 asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid, const ASN1_PCTX *pctx) 459 { 460 char objbuf[80]; 461 const char *ln; 462 ln = OBJ_nid2ln(OBJ_obj2nid(oid)); 463 if (!ln) 464 ln = ""; 465 OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1); 466 if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0) 467 return 0; 468 return 1; 469 } 470 471 static int 472 asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent, 473 const ASN1_PCTX *pctx) 474 { 475 if (str->type == V_ASN1_BIT_STRING) { 476 if (BIO_printf(out, " (%ld unused bits)\n", 477 str->flags & 0x7) <= 0) 478 return 0; 479 } else if (BIO_puts(out, "\n") <= 0) 480 return 0; 481 if ((str->length > 0) && 482 BIO_dump_indent(out, (char *)str->data, str->length, 483 indent + 2) <= 0) 484 return 0; 485 return 1; 486 } 487 488 static int 489 asn1_primitive_print(BIO *out, ASN1_VALUE **fld, const ASN1_ITEM *it, 490 int indent, const char *fname, const char *sname, const ASN1_PCTX *pctx) 491 { 492 long utype; 493 ASN1_STRING *str; 494 int ret = 1, needlf = 1; 495 const char *pname; 496 497 if (!asn1_print_fsname(out, indent, fname, sname, pctx)) 498 return 0; 499 500 if (it != NULL && it->funcs != NULL) { 501 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; 502 503 if (pf->prim_print == NULL) 504 return 0; 505 506 return pf->prim_print(out, fld, it, indent, pctx); 507 } 508 if (it->itype == ASN1_ITYPE_MSTRING) { 509 str = (ASN1_STRING *)*fld; 510 utype = str->type & ~V_ASN1_NEG; 511 } else { 512 utype = it->utype; 513 if (utype == V_ASN1_BOOLEAN) 514 str = NULL; 515 else 516 str = (ASN1_STRING *)*fld; 517 } 518 if (utype == V_ASN1_ANY) { 519 ASN1_TYPE *atype = (ASN1_TYPE *)*fld; 520 utype = atype->type; 521 fld = &atype->value.asn1_value; 522 str = (ASN1_STRING *)*fld; 523 if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE) 524 pname = NULL; 525 else 526 pname = ASN1_tag2str(utype); 527 } else { 528 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE) 529 pname = ASN1_tag2str(utype); 530 else 531 pname = NULL; 532 } 533 534 if (utype == V_ASN1_NULL) { 535 if (BIO_puts(out, "NULL\n") <= 0) 536 return 0; 537 return 1; 538 } 539 540 if (pname) { 541 if (BIO_puts(out, pname) <= 0) 542 return 0; 543 if (BIO_puts(out, ":") <= 0) 544 return 0; 545 } 546 547 switch (utype) { 548 case V_ASN1_BOOLEAN: 549 { 550 int boolval = *(int *)fld; 551 if (boolval == -1) 552 boolval = it->size; 553 ret = asn1_print_boolean_ctx(out, boolval, pctx); 554 } 555 break; 556 557 case V_ASN1_INTEGER: 558 case V_ASN1_ENUMERATED: 559 ret = asn1_print_integer_ctx(out, str, pctx); 560 break; 561 562 case V_ASN1_UTCTIME: 563 ret = ASN1_UTCTIME_print(out, str); 564 break; 565 566 case V_ASN1_GENERALIZEDTIME: 567 ret = ASN1_GENERALIZEDTIME_print(out, str); 568 break; 569 570 case V_ASN1_OBJECT: 571 ret = asn1_print_oid_ctx(out, (const ASN1_OBJECT *)*fld, pctx); 572 break; 573 574 case V_ASN1_OCTET_STRING: 575 case V_ASN1_BIT_STRING: 576 ret = asn1_print_obstring_ctx(out, str, indent, pctx); 577 needlf = 0; 578 break; 579 580 case V_ASN1_SEQUENCE: 581 case V_ASN1_SET: 582 case V_ASN1_OTHER: 583 if (BIO_puts(out, "\n") <= 0) 584 return 0; 585 if (ASN1_parse_dump(out, str->data, str->length, 586 indent, 0) <= 0) 587 ret = 0; 588 needlf = 0; 589 break; 590 591 default: 592 ret = ASN1_STRING_print_ex(out, str, pctx->str_flags); 593 } 594 if (!ret) 595 return 0; 596 if (needlf && BIO_puts(out, "\n") <= 0) 597 return 0; 598 return 1; 599 } 600