Coverage Report

Created: 2025-08-28 07:07

/src/openssl30/crypto/asn1/tasn_prn.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2000-2021 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
#include <stddef.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/asn1.h>
13
#include <openssl/asn1t.h>
14
#include <openssl/objects.h>
15
#include <openssl/buffer.h>
16
#include <openssl/err.h>
17
#include <openssl/x509v3.h>
18
#include "crypto/asn1.h"
19
#include "asn1_local.h"
20
21
/*
22
 * Print routines.
23
 */
24
25
/* ASN1_PCTX routines */
26
27
static ASN1_PCTX default_pctx = {
28
    ASN1_PCTX_FLAGS_SHOW_ABSENT, /* flags */
29
    0,                          /* nm_flags */
30
    0,                          /* cert_flags */
31
    0,                          /* oid_flags */
32
    0                           /* str_flags */
33
};
34
35
ASN1_PCTX *ASN1_PCTX_new(void)
36
22
{
37
22
    ASN1_PCTX *ret;
38
39
22
    ret = OPENSSL_zalloc(sizeof(*ret));
40
22
    if (ret == NULL) {
41
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
42
0
        return NULL;
43
0
    }
44
22
    return ret;
45
22
}
46
47
void ASN1_PCTX_free(ASN1_PCTX *p)
48
0
{
49
0
    OPENSSL_free(p);
50
0
}
51
52
unsigned long ASN1_PCTX_get_flags(const ASN1_PCTX *p)
53
0
{
54
0
    return p->flags;
55
0
}
56
57
void ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags)
58
22
{
59
22
    p->flags = flags;
60
22
}
61
62
unsigned long ASN1_PCTX_get_nm_flags(const ASN1_PCTX *p)
63
0
{
64
0
    return p->nm_flags;
65
0
}
66
67
void ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags)
68
0
{
69
0
    p->nm_flags = flags;
70
0
}
71
72
unsigned long ASN1_PCTX_get_cert_flags(const ASN1_PCTX *p)
73
0
{
74
0
    return p->cert_flags;
75
0
}
76
77
void ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags)
78
0
{
79
0
    p->cert_flags = flags;
80
0
}
81
82
unsigned long ASN1_PCTX_get_oid_flags(const ASN1_PCTX *p)
83
0
{
84
0
    return p->oid_flags;
85
0
}
86
87
void ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags)
88
0
{
89
0
    p->oid_flags = flags;
90
0
}
91
92
unsigned long ASN1_PCTX_get_str_flags(const ASN1_PCTX *p)
93
0
{
94
0
    return p->str_flags;
95
0
}
96
97
void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags)
98
22
{
99
22
    p->str_flags = flags;
100
22
}
101
102
/* Main print routines */
103
104
static int asn1_item_print_ctx(BIO *out, const ASN1_VALUE **fld, int indent,
105
                               const ASN1_ITEM *it,
106
                               const char *fname, const char *sname,
107
                               int nohdr, const ASN1_PCTX *pctx);
108
109
static int asn1_template_print_ctx(BIO *out, const ASN1_VALUE **fld, int indent,
110
                            const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx);
111
112
static int asn1_primitive_print(BIO *out, const ASN1_VALUE **fld,
113
                                const ASN1_ITEM *it, int indent,
114
                                const char *fname, const char *sname,
115
                                const ASN1_PCTX *pctx);
116
117
static int asn1_print_fsname(BIO *out, int indent,
118
                             const char *fname, const char *sname,
119
                             const ASN1_PCTX *pctx);
120
121
int ASN1_item_print(BIO *out, const ASN1_VALUE *ifld, int indent,
122
                    const ASN1_ITEM *it, const ASN1_PCTX *pctx)
123
353k
{
124
353k
    const char *sname;
125
353k
    if (pctx == NULL)
126
37.8k
        pctx = &default_pctx;
127
353k
    if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
128
0
        sname = NULL;
129
353k
    else
130
353k
        sname = it->sname;
131
353k
    return asn1_item_print_ctx(out, &ifld, indent, it, NULL, sname, 0, pctx);
132
353k
}
133
134
static int asn1_item_print_ctx(BIO *out, const ASN1_VALUE **fld, int indent,
135
                               const ASN1_ITEM *it,
136
                               const char *fname, const char *sname,
137
                               int nohdr, const ASN1_PCTX *pctx)
138
6.16M
{
139
6.16M
    const ASN1_TEMPLATE *tt;
140
6.16M
    const ASN1_EXTERN_FUNCS *ef;
141
6.16M
    const ASN1_VALUE **tmpfld;
142
6.16M
    const ASN1_AUX *aux = it->funcs;
143
6.16M
    ASN1_aux_const_cb *asn1_cb = NULL;
144
6.16M
    ASN1_PRINT_ARG parg;
145
6.16M
    int i;
146
6.16M
    if (aux != NULL) {
147
664k
        parg.out = out;
148
664k
        parg.indent = indent;
149
664k
        parg.pctx = pctx;
150
664k
        asn1_cb = ((aux->flags & ASN1_AFLG_CONST_CB) != 0) ? aux->asn1_const_cb
151
664k
            : (ASN1_aux_const_cb *)aux->asn1_cb; /* backward compatibility */
152
664k
    }
153
154
6.16M
   if (((it->itype != ASN1_ITYPE_PRIMITIVE)
155
6.16M
       || (it->utype != V_ASN1_BOOLEAN)) && *fld == NULL) {
156
1.56M
        if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT) {
157
1.56M
            if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
158
0
                return 0;
159
1.56M
            if (BIO_puts(out, "<ABSENT>\n") <= 0)
160
0
                return 0;
161
1.56M
        }
162
1.56M
        return 1;
163
1.56M
    }
164
165
4.59M
    switch (it->itype) {
166
3.48M
    case ASN1_ITYPE_PRIMITIVE:
167
3.48M
        if (it->templates) {
168
94.0k
            if (!asn1_template_print_ctx(out, fld, indent,
169
94.0k
                                         it->templates, pctx))
170
5.29k
                return 0;
171
88.7k
            break;
172
94.0k
        }
173
        /* fall through */
174
3.44M
    case ASN1_ITYPE_MSTRING:
175
3.44M
        if (!asn1_primitive_print(out, fld, it, indent, fname, sname, pctx))
176
35.6k
            return 0;
177
3.40M
        break;
178
179
3.40M
    case ASN1_ITYPE_EXTERN:
180
124k
        if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
181
0
            return 0;
182
        /* Use new style print routine if possible */
183
124k
        ef = it->funcs;
184
124k
        if (ef && ef->asn1_ex_print) {
185
124k
            i = ef->asn1_ex_print(out, fld, indent, "", pctx);
186
124k
            if (!i)
187
1.10k
                return 0;
188
123k
            if ((i == 2) && (BIO_puts(out, "\n") <= 0))
189
0
                return 0;
190
123k
            return 1;
191
123k
        } else if (sname &&
192
0
                   BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
193
0
            return 0;
194
0
        break;
195
196
204k
    case ASN1_ITYPE_CHOICE:
197
        /* CHOICE type, get selector */
198
204k
        i = ossl_asn1_get_choice_selector_const(fld, it);
199
        /* This should never happen... */
200
204k
        if ((i < 0) || (i >= it->tcount)) {
201
0
            if (BIO_printf(out, "ERROR: selector [%d] invalid\n", i) <= 0)
202
0
                return 0;
203
0
            return 1;
204
0
        }
205
204k
        tt = it->templates + i;
206
204k
        tmpfld = ossl_asn1_get_const_field_ptr(fld, tt);
207
204k
        if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
208
12.6k
            return 0;
209
192k
        break;
210
211
717k
    case ASN1_ITYPE_SEQUENCE:
212
731k
    case ASN1_ITYPE_NDEF_SEQUENCE:
213
731k
        if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
214
0
            return 0;
215
731k
        if (fname || sname) {
216
454k
            if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
217
78.3k
                if (BIO_puts(out, " {\n") <= 0)
218
0
                    return 0;
219
376k
            } else {
220
376k
                if (BIO_puts(out, "\n") <= 0)
221
0
                    return 0;
222
376k
            }
223
454k
        }
224
225
731k
        if (asn1_cb) {
226
82.6k
            i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg);
227
82.6k
            if (i == 0)
228
0
                return 0;
229
82.6k
            if (i == 2)
230
0
                return 1;
231
82.6k
        }
232
233
        /* Print each field entry */
234
3.80M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
235
3.11M
            const ASN1_TEMPLATE *seqtt;
236
3.11M
            seqtt = ossl_asn1_do_adb(*fld, tt, 1);
237
3.11M
            if (!seqtt)
238
0
                return 0;
239
3.11M
            tmpfld = ossl_asn1_get_const_field_ptr(fld, seqtt);
240
3.11M
            if (!asn1_template_print_ctx(out, tmpfld,
241
3.11M
                                         indent + 2, seqtt, pctx))
242
48.9k
                return 0;
243
3.11M
        }
244
682k
        if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
245
167k
            if (BIO_printf(out, "%*s}\n", indent, "") < 0)
246
0
                return 0;
247
167k
        }
248
249
682k
        if (asn1_cb) {
250
57.9k
            i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg);
251
57.9k
            if (i == 0)
252
0
                return 0;
253
57.9k
        }
254
682k
        break;
255
256
682k
    default:
257
0
        BIO_printf(out, "Unprocessed type %d\n", it->itype);
258
0
        return 0;
259
4.59M
    }
260
261
4.36M
    return 1;
262
4.59M
}
263
264
static int asn1_template_print_ctx(BIO *out, const ASN1_VALUE **fld, int indent,
265
                            const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
266
3.41M
{
267
3.41M
    int i, flags;
268
3.41M
    const char *sname, *fname;
269
3.41M
    const ASN1_VALUE *tfld;
270
3.41M
    flags = tt->flags;
271
3.41M
    if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
272
603k
        sname = ASN1_ITEM_ptr(tt->item)->sname;
273
2.81M
    else
274
2.81M
        sname = NULL;
275
3.41M
    if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
276
0
        fname = NULL;
277
3.41M
    else
278
3.41M
        fname = tt->field_name;
279
280
    /*
281
     * If field is embedded then fld needs fixing so it is a pointer to
282
     * a pointer to a field.
283
     */
284
3.41M
    if (flags & ASN1_TFLG_EMBED) {
285
55.8k
        tfld = (const ASN1_VALUE *)fld;
286
55.8k
        fld = &tfld;
287
55.8k
    }
288
289
3.41M
    if (flags & ASN1_TFLG_SK_MASK) {
290
414k
        char *tname;
291
414k
        const ASN1_VALUE *skitem;
292
414k
        STACK_OF(const_ASN1_VALUE) *stack;
293
294
        /* SET OF, SEQUENCE OF */
295
414k
        if (fname) {
296
414k
            if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF) {
297
121k
                if (flags & ASN1_TFLG_SET_OF)
298
20.9k
                    tname = "SET";
299
100k
                else
300
100k
                    tname = "SEQUENCE";
301
121k
                if (BIO_printf(out, "%*s%s OF %s {\n",
302
121k
                               indent, "", tname, tt->field_name) <= 0)
303
0
                    return 0;
304
293k
            } else if (BIO_printf(out, "%*s%s:\n", indent, "", fname) <= 0)
305
0
                return 0;
306
414k
        }
307
414k
        stack = (STACK_OF(const_ASN1_VALUE) *)*fld;
308
3.21M
        for (i = 0; i < sk_const_ASN1_VALUE_num(stack); i++) {
309
2.80M
            if ((i > 0) && (BIO_puts(out, "\n") <= 0))
310
0
                return 0;
311
312
2.80M
            skitem = sk_const_ASN1_VALUE_value(stack, i);
313
2.80M
            if (!asn1_item_print_ctx(out, &skitem, indent + 2,
314
2.80M
                                     ASN1_ITEM_ptr(tt->item), NULL, NULL, 1,
315
2.80M
                                     pctx))
316
8.76k
                return 0;
317
2.80M
        }
318
406k
        if (i == 0 && BIO_printf(out, "%*s<%s>\n", indent + 2, "",
319
340k
                                 stack == NULL ? "ABSENT" : "EMPTY") <= 0)
320
0
            return 0;
321
406k
        if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
322
117k
            if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
323
0
                return 0;
324
117k
        }
325
406k
        return 1;
326
406k
    }
327
3.00M
    return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
328
3.00M
                               fname, sname, 0, pctx);
329
3.41M
}
330
331
static int asn1_print_fsname(BIO *out, int indent,
332
                             const char *fname, const char *sname,
333
                             const ASN1_PCTX *pctx)
334
5.58M
{
335
5.58M
    static const char spaces[] = "                    ";
336
5.58M
    static const int nspaces = sizeof(spaces) - 1;
337
338
5.64M
    while (indent > nspaces) {
339
58.1k
        if (BIO_write(out, spaces, nspaces) != nspaces)
340
0
            return 0;
341
58.1k
        indent -= nspaces;
342
58.1k
    }
343
5.58M
    if (BIO_write(out, spaces, indent) != indent)
344
0
        return 0;
345
5.58M
    if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
346
0
        sname = NULL;
347
5.58M
    if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
348
0
        fname = NULL;
349
5.58M
    if (!sname && !fname)
350
2.47M
        return 1;
351
3.11M
    if (fname) {
352
2.80M
        if (BIO_puts(out, fname) <= 0)
353
0
            return 0;
354
2.80M
    }
355
3.11M
    if (sname) {
356
777k
        if (fname) {
357
473k
            if (BIO_printf(out, " (%s)", sname) <= 0)
358
0
                return 0;
359
473k
        } else {
360
304k
            if (BIO_puts(out, sname) <= 0)
361
0
                return 0;
362
304k
        }
363
777k
    }
364
3.11M
    if (BIO_write(out, ": ", 2) != 2)
365
0
        return 0;
366
3.11M
    return 1;
367
3.11M
}
368
369
static int asn1_print_boolean(BIO *out, int boolval)
370
28.2k
{
371
28.2k
    const char *str;
372
28.2k
    switch (boolval) {
373
4.99k
    case -1:
374
4.99k
        str = "BOOL ABSENT";
375
4.99k
        break;
376
377
11.7k
    case 0:
378
11.7k
        str = "FALSE";
379
11.7k
        break;
380
381
11.4k
    default:
382
11.4k
        str = "TRUE";
383
11.4k
        break;
384
385
28.2k
    }
386
387
28.2k
    if (BIO_puts(out, str) <= 0)
388
0
        return 0;
389
28.2k
    return 1;
390
391
28.2k
}
392
393
static int asn1_print_integer(BIO *out, const ASN1_INTEGER *str)
394
2.34M
{
395
2.34M
    char *s;
396
2.34M
    int ret = 1;
397
2.34M
    s = i2s_ASN1_INTEGER(NULL, str);
398
2.34M
    if (s == NULL)
399
406
        return 0;
400
2.34M
    if (BIO_puts(out, s) <= 0)
401
0
        ret = 0;
402
2.34M
    OPENSSL_free(s);
403
2.34M
    return ret;
404
2.34M
}
405
406
static int asn1_print_oid(BIO *out, const ASN1_OBJECT *oid)
407
263k
{
408
263k
    char objbuf[80];
409
263k
    const char *ln;
410
263k
    ln = OBJ_nid2ln(OBJ_obj2nid(oid));
411
263k
    if (!ln)
412
0
        ln = "";
413
263k
    OBJ_obj2txt(objbuf, sizeof(objbuf), oid, 1);
414
263k
    if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
415
0
        return 0;
416
263k
    return 1;
417
263k
}
418
419
static int asn1_print_obstring(BIO *out, const ASN1_STRING *str, int indent)
420
197k
{
421
197k
    if (str->type == V_ASN1_BIT_STRING) {
422
94.4k
        if (BIO_printf(out, " (%ld unused bits)\n", str->flags & 0x7) <= 0)
423
0
            return 0;
424
103k
    } else if (BIO_puts(out, "\n") <= 0)
425
0
        return 0;
426
197k
    if ((str->length > 0)
427
197k
        && BIO_dump_indent(out, (const char *)str->data, str->length,
428
116k
                           indent + 2) <= 0)
429
0
        return 0;
430
197k
    return 1;
431
197k
}
432
433
static int asn1_primitive_print(BIO *out, const ASN1_VALUE **fld,
434
                                const ASN1_ITEM *it, int indent,
435
                                const char *fname, const char *sname,
436
                                const ASN1_PCTX *pctx)
437
3.44M
{
438
3.44M
    long utype;
439
3.44M
    ASN1_STRING *str;
440
3.44M
    int ret = 1, needlf = 1;
441
3.44M
    const char *pname;
442
3.44M
    const ASN1_PRIMITIVE_FUNCS *pf;
443
3.44M
    pf = it->funcs;
444
3.44M
    if (!asn1_print_fsname(out, indent, fname, sname, pctx))
445
0
        return 0;
446
3.44M
    if (pf && pf->prim_print)
447
129k
        return pf->prim_print(out, fld, it, indent, pctx);
448
3.31M
    if (it->itype == ASN1_ITYPE_MSTRING) {
449
51.9k
        str = (ASN1_STRING *)*fld;
450
51.9k
        utype = str->type & ~V_ASN1_NEG;
451
3.25M
    } else {
452
3.25M
        utype = it->utype;
453
3.25M
        if (utype == V_ASN1_BOOLEAN)
454
21.0k
            str = NULL;
455
3.23M
        else
456
3.23M
            str = (ASN1_STRING *)*fld;
457
3.25M
    }
458
3.31M
    if (utype == V_ASN1_ANY) {
459
458k
        const ASN1_TYPE *atype = (const ASN1_TYPE *)*fld;
460
458k
        utype = atype->type;
461
458k
        fld = (const ASN1_VALUE **)&atype->value.asn1_value; /* actually is const */
462
458k
        str = (ASN1_STRING *)*fld;
463
458k
        if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
464
0
            pname = NULL;
465
458k
        else
466
458k
            pname = ASN1_tag2str(utype);
467
2.85M
    } else {
468
2.85M
        if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
469
284k
            pname = ASN1_tag2str(utype);
470
2.56M
        else
471
2.56M
            pname = NULL;
472
2.85M
    }
473
474
3.31M
    if (utype == V_ASN1_NULL) {
475
19.7k
        if (BIO_puts(out, "NULL\n") <= 0)
476
0
            return 0;
477
19.7k
        return 1;
478
19.7k
    }
479
480
3.29M
    if (pname) {
481
723k
        if (BIO_puts(out, pname) <= 0)
482
0
            return 0;
483
723k
        if (BIO_puts(out, ":") <= 0)
484
0
            return 0;
485
723k
    }
486
487
3.29M
    switch (utype) {
488
28.2k
    case V_ASN1_BOOLEAN:
489
28.2k
        {
490
28.2k
            int boolval = *(int *)fld;
491
28.2k
            if (boolval == -1)
492
4.99k
                boolval = it->size;
493
28.2k
            ret = asn1_print_boolean(out, boolval);
494
28.2k
        }
495
28.2k
        break;
496
497
2.34M
    case V_ASN1_INTEGER:
498
2.34M
    case V_ASN1_ENUMERATED:
499
2.34M
        ret = asn1_print_integer(out, str);
500
2.34M
        break;
501
502
13.6k
    case V_ASN1_UTCTIME:
503
13.6k
        ret = ASN1_UTCTIME_print(out, str);
504
13.6k
        break;
505
506
12.9k
    case V_ASN1_GENERALIZEDTIME:
507
12.9k
        ret = ASN1_GENERALIZEDTIME_print(out, str);
508
12.9k
        break;
509
510
263k
    case V_ASN1_OBJECT:
511
263k
        ret = asn1_print_oid(out, (const ASN1_OBJECT *)*fld);
512
263k
        break;
513
514
103k
    case V_ASN1_OCTET_STRING:
515
197k
    case V_ASN1_BIT_STRING:
516
197k
        ret = asn1_print_obstring(out, str, indent);
517
197k
        needlf = 0;
518
197k
        break;
519
520
264k
    case V_ASN1_SEQUENCE:
521
295k
    case V_ASN1_SET:
522
358k
    case V_ASN1_OTHER:
523
358k
        if (BIO_puts(out, "\n") <= 0)
524
0
            return 0;
525
358k
        if (ASN1_parse_dump(out, str->data, str->length, indent, 0) <= 0)
526
16.8k
            ret = 0;
527
358k
        needlf = 0;
528
358k
        break;
529
530
69.9k
    default:
531
69.9k
        ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
532
533
3.29M
    }
534
3.29M
    if (!ret)
535
35.6k
        return 0;
536
3.25M
    if (needlf && BIO_puts(out, "\n") <= 0)
537
0
        return 0;
538
3.25M
    return 1;
539
3.25M
}