Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/asn1/tasn_prn.c
Line
Count
Source
1
/*
2
 * Copyright 2000-2026 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
24
{
37
24
    ASN1_PCTX *ret;
38
39
24
    ret = OPENSSL_zalloc(sizeof(*ret));
40
24
    if (ret == NULL)
41
0
        return NULL;
42
24
    return ret;
43
24
}
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
24
{
57
24
    p->flags = flags;
58
24
}
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
24
{
97
24
    p->str_flags = flags;
98
24
}
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
365k
{
122
365k
    const char *sname;
123
365k
    if (pctx == NULL)
124
37.7k
        pctx = &default_pctx;
125
365k
    if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
126
0
        sname = NULL;
127
365k
    else
128
365k
        sname = it->sname;
129
365k
    return asn1_item_print_ctx(out, &ifld, indent, it, NULL, sname, 0, pctx);
130
365k
}
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
2.41M
{
137
2.41M
    const ASN1_TEMPLATE *tt;
138
2.41M
    const ASN1_EXTERN_FUNCS *ef;
139
2.41M
    const ASN1_VALUE **tmpfld;
140
2.41M
    const ASN1_AUX *aux = it->funcs;
141
2.41M
    ASN1_PRINT_ARG parg;
142
2.41M
    int i;
143
2.41M
    if (aux != NULL) {
144
204k
        parg.out = out;
145
204k
        parg.indent = indent;
146
204k
        parg.pctx = pctx;
147
204k
    }
148
149
2.41M
    if (((it->itype != ASN1_ITYPE_PRIMITIVE)
150
1.82M
            || (it->utype != V_ASN1_BOOLEAN))
151
2.41M
        && *fld == NULL) {
152
568k
        if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT) {
153
568k
            if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
154
0
                return 0;
155
568k
            if (BIO_puts(out, "<ABSENT>\n") <= 0)
156
0
                return 0;
157
568k
        }
158
568k
        return 1;
159
568k
    }
160
161
1.84M
    switch (it->itype) {
162
1.47M
    case ASN1_ITYPE_PRIMITIVE:
163
1.47M
        if (it->templates) {
164
33.0k
            if (!asn1_template_print_ctx(out, fld, indent,
165
33.0k
                    it->templates, pctx))
166
1.83k
                return 0;
167
31.2k
            break;
168
33.0k
        }
169
        /* fall through */
170
1.46M
    case ASN1_ITYPE_MSTRING:
171
1.46M
        if (!asn1_primitive_print(out, fld, it, indent, fname, sname, pctx))
172
11.5k
            return 0;
173
1.45M
        break;
174
175
1.45M
    case ASN1_ITYPE_EXTERN:
176
43.3k
        if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
177
0
            return 0;
178
        /* Use new style print routine if possible */
179
43.3k
        ef = it->funcs;
180
43.3k
        if (ef && ef->asn1_ex_print) {
181
43.3k
            i = ef->asn1_ex_print(out, fld, indent, "", pctx);
182
43.3k
            if (!i)
183
231
                return 0;
184
43.1k
            if ((i == 2) && (BIO_puts(out, "\n") <= 0))
185
0
                return 0;
186
43.1k
            return 1;
187
43.1k
        } else if (sname && BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
188
0
            return 0;
189
0
        break;
190
191
70.2k
    case ASN1_ITYPE_CHOICE:
192
        /* CHOICE type, get selector */
193
70.2k
        i = ossl_asn1_get_choice_selector_const(fld, it);
194
        /* This should never happen... */
195
70.2k
        if ((i < 0) || (i >= it->tcount)) {
196
0
            if (BIO_printf(out, "ERROR: selector [%d] invalid\n", i) <= 0)
197
0
                return 0;
198
0
            return 1;
199
0
        }
200
70.2k
        tt = it->templates + i;
201
70.2k
        tmpfld = ossl_asn1_get_const_field_ptr(fld, tt);
202
70.2k
        if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
203
4.25k
            return 0;
204
65.9k
        break;
205
206
236k
    case ASN1_ITYPE_SEQUENCE:
207
240k
    case ASN1_ITYPE_NDEF_SEQUENCE:
208
240k
        if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
209
0
            return 0;
210
240k
        if (fname || sname) {
211
150k
            if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
212
27.2k
                if (BIO_puts(out, " {\n") <= 0)
213
0
                    return 0;
214
123k
            } else {
215
123k
                if (BIO_puts(out, "\n") <= 0)
216
0
                    return 0;
217
123k
            }
218
150k
        }
219
220
240k
        i = ossl_asn1_call_aux_cb(aux, ASN1_OP_PRINT_PRE, fld, it, &parg);
221
240k
        if (i == 0)
222
0
            return 0;
223
240k
        if (i == 2)
224
0
            return 1;
225
226
        /* Print each field entry */
227
1.29M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
228
1.07M
            const ASN1_TEMPLATE *seqtt;
229
1.07M
            seqtt = ossl_asn1_do_adb(*fld, tt, 1);
230
1.07M
            if (!seqtt)
231
0
                return 0;
232
1.07M
            tmpfld = ossl_asn1_get_const_field_ptr(fld, seqtt);
233
1.07M
            if (!asn1_template_print_ctx(out, tmpfld,
234
1.07M
                    indent + 2, seqtt, pctx))
235
16.0k
                return 0;
236
1.07M
        }
237
224k
        if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
238
46.5k
            if (BIO_printf(out, "%*s}\n", indent, "") < 0)
239
0
                return 0;
240
46.5k
        }
241
242
224k
        i = ossl_asn1_call_aux_cb(aux, ASN1_OP_PRINT_POST, fld, it, &parg);
243
224k
        if (i == 0)
244
0
            return 0;
245
224k
        break;
246
247
224k
    default:
248
0
        BIO_printf(out, "Unprocessed type %d\n", it->itype);
249
0
        return 0;
250
1.84M
    }
251
252
1.77M
    return 1;
253
1.84M
}
254
255
static int asn1_template_print_ctx(BIO *out, const ASN1_VALUE **fld, int indent,
256
    const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
257
4.70M
{
258
4.70M
    int i, flags;
259
4.70M
    const char *sname, *fname;
260
4.70M
    const ASN1_VALUE *tfld;
261
4.70M
    flags = tt->flags;
262
4.70M
    if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
263
637k
        sname = ASN1_ITEM_ptr(tt->item)->sname;
264
4.06M
    else
265
4.06M
        sname = NULL;
266
4.70M
    if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
267
0
        fname = NULL;
268
4.70M
    else
269
4.70M
        fname = tt->field_name;
270
271
    /*
272
     * If field is embedded then fld needs fixing so it is a pointer to
273
     * a pointer to a field.
274
     */
275
4.70M
    if (flags & ASN1_TFLG_EMBED) {
276
62.4k
        tfld = (const ASN1_VALUE *)fld;
277
62.4k
        fld = &tfld;
278
62.4k
    }
279
280
4.70M
    if (flags & ASN1_TFLG_SK_MASK) {
281
537k
        char *tname;
282
537k
        const ASN1_VALUE *skitem;
283
537k
        STACK_OF(const_ASN1_VALUE) *stack;
284
285
        /* SET OF, SEQUENCE OF */
286
537k
        if (fname) {
287
537k
            if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF) {
288
132k
                if (flags & ASN1_TFLG_SET_OF)
289
25.1k
                    tname = "SET";
290
107k
                else
291
107k
                    tname = "SEQUENCE";
292
132k
                if (BIO_printf(out, "%*s%s OF %s {\n",
293
132k
                        indent, "", tname, tt->field_name)
294
132k
                    <= 0)
295
0
                    return 0;
296
404k
            } else if (BIO_printf(out, "%*s%s:\n", indent, "", fname) <= 0)
297
0
                return 0;
298
537k
        }
299
537k
        stack = (STACK_OF(const_ASN1_VALUE) *)*fld;
300
4.18M
        for (i = 0; i < sk_const_ASN1_VALUE_num(stack); i++) {
301
3.65M
            if ((i > 0) && (BIO_puts(out, "\n") <= 0))
302
0
                return 0;
303
304
3.65M
            skitem = sk_const_ASN1_VALUE_value(stack, i);
305
3.65M
            if (!asn1_item_print_ctx(out, &skitem, indent + 2,
306
3.65M
                    ASN1_ITEM_ptr(tt->item), NULL, NULL, 1,
307
3.65M
                    pctx))
308
9.36k
                return 0;
309
3.65M
        }
310
528k
        if (i == 0 && BIO_printf(out, "%*s<%s>\n", indent + 2, "", stack == NULL ? "ABSENT" : "EMPTY") <= 0)
311
0
            return 0;
312
528k
        if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
313
128k
            if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
314
0
                return 0;
315
128k
        }
316
528k
        return 1;
317
528k
    }
318
4.16M
    return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
319
4.16M
        fname, sname, 0, pctx);
320
4.70M
}
321
322
static int asn1_print_fsname(BIO *out, int indent,
323
    const char *fname, const char *sname,
324
    const ASN1_PCTX *pctx)
325
7.45M
{
326
7.45M
    static const char spaces[] = "                    ";
327
7.45M
    static const int nspaces = sizeof(spaces) - 1;
328
329
7.50M
    while (indent > nspaces) {
330
47.8k
        if (BIO_write(out, spaces, nspaces) != nspaces)
331
0
            return 0;
332
47.8k
        indent -= nspaces;
333
47.8k
    }
334
7.45M
    if (BIO_write(out, spaces, indent) != indent)
335
0
        return 0;
336
7.45M
    if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
337
0
        sname = NULL;
338
7.45M
    if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
339
0
        fname = NULL;
340
7.45M
    if (!sname && !fname)
341
3.19M
        return 1;
342
4.26M
    if (fname) {
343
3.95M
        if (BIO_puts(out, fname) <= 0)
344
0
            return 0;
345
3.95M
    }
346
4.26M
    if (sname) {
347
810k
        if (fname) {
348
495k
            if (BIO_printf(out, " (%s)", sname) <= 0)
349
0
                return 0;
350
495k
        } else {
351
314k
            if (BIO_puts(out, sname) <= 0)
352
0
                return 0;
353
314k
        }
354
810k
    }
355
4.26M
    if (BIO_write(out, ": ", 2) != 2)
356
0
        return 0;
357
4.26M
    return 1;
358
4.26M
}
359
360
static int asn1_print_boolean(BIO *out, int boolval)
361
24.4k
{
362
24.4k
    const char *str;
363
24.4k
    switch (boolval) {
364
0
    case -1:
365
0
        str = "BOOL ABSENT";
366
0
        break;
367
368
14.7k
    case 0:
369
14.7k
        str = "FALSE";
370
14.7k
        break;
371
372
9.66k
    default:
373
9.66k
        str = "TRUE";
374
9.66k
        break;
375
24.4k
    }
376
377
24.4k
    if (BIO_puts(out, str) <= 0)
378
0
        return 0;
379
24.4k
    return 1;
380
24.4k
}
381
382
static int asn1_print_integer(BIO *out, const ASN1_INTEGER *str)
383
3.07M
{
384
3.07M
    char *s;
385
3.07M
    int ret = 1;
386
3.07M
    s = i2s_ASN1_INTEGER(NULL, str);
387
3.07M
    if (s == NULL)
388
423
        return 0;
389
3.07M
    if (BIO_puts(out, s) <= 0)
390
0
        ret = 0;
391
3.07M
    OPENSSL_free(s);
392
3.07M
    return ret;
393
3.07M
}
394
395
static int asn1_print_oid(BIO *out, const ASN1_OBJECT *oid)
396
314k
{
397
314k
    char objbuf[80];
398
314k
    const char *ln;
399
314k
    ln = OBJ_nid2ln(OBJ_obj2nid(oid));
400
314k
    if (!ln)
401
0
        ln = "";
402
314k
    OBJ_obj2txt(objbuf, sizeof(objbuf), oid, 1);
403
314k
    if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
404
0
        return 0;
405
314k
    return 1;
406
314k
}
407
408
static int asn1_print_obstring(BIO *out, const ASN1_STRING *str, int indent)
409
214k
{
410
214k
    if (str->type == V_ASN1_BIT_STRING) {
411
97.8k
        if (BIO_printf(out, " (%ld unused bits)\n", str->flags & 0x7) <= 0)
412
0
            return 0;
413
116k
    } else if (BIO_puts(out, "\n") <= 0)
414
0
        return 0;
415
214k
    if ((str->length > 0)
416
126k
        && BIO_dump_indent(out, (const char *)str->data, str->length,
417
126k
               indent + 2)
418
126k
            <= 0)
419
0
        return 0;
420
214k
    return 1;
421
214k
}
422
423
static int asn1_primitive_print(BIO *out, const ASN1_VALUE **fld,
424
    const ASN1_ITEM *it, int indent,
425
    const char *fname, const char *sname,
426
    const ASN1_PCTX *pctx)
427
4.25M
{
428
4.25M
    long utype;
429
4.25M
    ASN1_STRING *str;
430
4.25M
    int ret = 1, needlf = 1;
431
4.25M
    const char *pname;
432
4.25M
    const ASN1_PRIMITIVE_FUNCS *pf;
433
4.25M
    pf = it->funcs;
434
4.25M
    if (!asn1_print_fsname(out, indent, fname, sname, pctx))
435
0
        return 0;
436
4.25M
    if (pf && pf->prim_print)
437
90.0k
        return pf->prim_print(out, fld, it, indent, pctx);
438
4.16M
    if (it->itype == ASN1_ITYPE_MSTRING) {
439
54.9k
        str = (ASN1_STRING *)*fld;
440
54.9k
        utype = str->type & ~V_ASN1_NEG;
441
4.10M
    } else {
442
4.10M
        utype = it->utype;
443
4.10M
        if (utype == V_ASN1_BOOLEAN)
444
19.3k
            str = NULL;
445
4.08M
        else
446
4.08M
            str = (ASN1_STRING *)*fld;
447
4.10M
    }
448
4.16M
    if (utype == V_ASN1_ANY) {
449
466k
        const ASN1_TYPE *atype = (const ASN1_TYPE *)*fld;
450
466k
        utype = atype->type;
451
466k
        fld = (const ASN1_VALUE **)&atype->value.asn1_value; /* actually is const */
452
466k
        str = (ASN1_STRING *)*fld;
453
466k
        if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
454
0
            pname = NULL;
455
466k
        else
456
466k
            pname = ASN1_tag2str(utype);
457
3.69M
    } else {
458
3.69M
        if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
459
310k
            pname = ASN1_tag2str(utype);
460
3.38M
        else
461
3.38M
            pname = NULL;
462
3.69M
    }
463
464
4.16M
    if (utype == V_ASN1_NULL) {
465
22.1k
        if (BIO_puts(out, "NULL\n") <= 0)
466
0
            return 0;
467
22.1k
        return 1;
468
22.1k
    }
469
470
4.13M
    if (pname) {
471
756k
        if (BIO_puts(out, pname) <= 0)
472
0
            return 0;
473
756k
        if (BIO_puts(out, ":") <= 0)
474
0
            return 0;
475
756k
    }
476
477
4.13M
    switch (utype) {
478
24.4k
    case V_ASN1_BOOLEAN: {
479
24.4k
        int boolval = *(int *)fld;
480
24.4k
        if (boolval == -1)
481
0
            boolval = it->size;
482
24.4k
        ret = asn1_print_boolean(out, boolval);
483
24.4k
    } break;
484
485
3.07M
    case V_ASN1_INTEGER:
486
3.07M
    case V_ASN1_ENUMERATED:
487
3.07M
        ret = asn1_print_integer(out, str);
488
3.07M
        break;
489
490
18.7k
    case V_ASN1_UTCTIME:
491
18.7k
        ret = ASN1_UTCTIME_print(out, str);
492
18.7k
        break;
493
494
14.8k
    case V_ASN1_GENERALIZEDTIME:
495
14.8k
        ret = ASN1_GENERALIZEDTIME_print(out, str);
496
14.8k
        break;
497
498
314k
    case V_ASN1_OBJECT:
499
314k
        ret = asn1_print_oid(out, (const ASN1_OBJECT *)*fld);
500
314k
        break;
501
502
116k
    case V_ASN1_OCTET_STRING:
503
214k
    case V_ASN1_BIT_STRING:
504
214k
        ret = asn1_print_obstring(out, str, indent);
505
214k
        needlf = 0;
506
214k
        break;
507
508
293k
    case V_ASN1_SEQUENCE:
509
308k
    case V_ASN1_SET:
510
376k
    case V_ASN1_OTHER:
511
376k
        if (BIO_puts(out, "\n") <= 0)
512
0
            return 0;
513
376k
        if (ASN1_parse_dump(out, str->data, str->length, indent, 0) <= 0)
514
17.4k
            ret = 0;
515
376k
        needlf = 0;
516
376k
        break;
517
518
101k
    default:
519
101k
        ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
520
4.13M
    }
521
4.13M
    if (!ret)
522
36.9k
        return 0;
523
4.10M
    if (needlf && BIO_puts(out, "\n") <= 0)
524
0
        return 0;
525
4.10M
    return 1;
526
4.10M
}