Coverage Report

Created: 2026-04-01 06:39

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