Coverage Report

Created: 2022-08-24 06:30

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