Coverage Report

Created: 2026-04-09 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/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
        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
389k
{
124
389k
    const char *sname;
125
389k
    if (pctx == NULL)
126
39.7k
        pctx = &default_pctx;
127
389k
    if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
128
0
        sname = NULL;
129
389k
    else
130
389k
        sname = it->sname;
131
389k
    return asn1_item_print_ctx(out, &ifld, indent, it, NULL, sname, 0, pctx);
132
389k
}
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
9.70M
{
139
9.70M
    const ASN1_TEMPLATE *tt;
140
9.70M
    const ASN1_EXTERN_FUNCS *ef;
141
9.70M
    const ASN1_VALUE **tmpfld;
142
9.70M
    const ASN1_AUX *aux = it->funcs;
143
9.70M
    ASN1_aux_const_cb *asn1_cb = NULL;
144
9.70M
    ASN1_PRINT_ARG parg;
145
9.70M
    int i;
146
9.70M
    if (aux != NULL) {
147
1.22M
        parg.out = out;
148
1.22M
        parg.indent = indent;
149
1.22M
        parg.pctx = pctx;
150
1.22M
        asn1_cb = ((aux->flags & ASN1_AFLG_CONST_CB) != 0) ? aux->asn1_const_cb
151
1.22M
                                                           : (ASN1_aux_const_cb *)aux->asn1_cb; /* backward compatibility */
152
1.22M
    }
153
154
9.70M
    if (((it->itype != ASN1_ITYPE_PRIMITIVE)
155
6.55M
            || (it->utype != V_ASN1_BOOLEAN))
156
9.67M
        && *fld == NULL) {
157
3.38M
        if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT) {
158
3.38M
            if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
159
0
                return 0;
160
3.38M
            if (BIO_puts(out, "<ABSENT>\n") <= 0)
161
0
                return 0;
162
3.38M
        }
163
3.38M
        return 1;
164
3.38M
    }
165
166
6.31M
    switch (it->itype) {
167
4.68M
    case ASN1_ITYPE_PRIMITIVE:
168
4.68M
        if (it->templates) {
169
109k
            if (!asn1_template_print_ctx(out, fld, indent,
170
109k
                    it->templates, pctx))
171
5.10k
                return 0;
172
104k
            break;
173
109k
        }
174
        /* fall through */
175
4.63M
    case ASN1_ITYPE_MSTRING:
176
4.63M
        if (!asn1_primitive_print(out, fld, it, indent, fname, sname, pctx))
177
36.1k
            return 0;
178
4.59M
        break;
179
180
4.59M
    case ASN1_ITYPE_EXTERN:
181
146k
        if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
182
0
            return 0;
183
        /* Use new style print routine if possible */
184
146k
        ef = it->funcs;
185
146k
        if (ef && ef->asn1_ex_print) {
186
146k
            i = ef->asn1_ex_print(out, fld, indent, "", pctx);
187
146k
            if (!i)
188
914
                return 0;
189
145k
            if ((i == 2) && (BIO_puts(out, "\n") <= 0))
190
0
                return 0;
191
145k
            return 1;
192
145k
        } else if (sname && BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
193
0
            return 0;
194
0
        break;
195
196
211k
    case ASN1_ITYPE_CHOICE:
197
        /* CHOICE type, get selector */
198
211k
        i = ossl_asn1_get_choice_selector_const(fld, it);
199
        /* This should never happen... */
200
211k
        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
211k
        tt = it->templates + i;
206
211k
        tmpfld = ossl_asn1_get_const_field_ptr(fld, tt);
207
211k
        if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
208
12.9k
            return 0;
209
199k
        break;
210
211
1.18M
    case ASN1_ITYPE_SEQUENCE:
212
1.20M
    case ASN1_ITYPE_NDEF_SEQUENCE:
213
1.20M
        if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
214
0
            return 0;
215
1.20M
        if (fname || sname) {
216
697k
            if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
217
99.9k
                if (BIO_puts(out, " {\n") <= 0)
218
0
                    return 0;
219
597k
            } else {
220
597k
                if (BIO_puts(out, "\n") <= 0)
221
0
                    return 0;
222
597k
            }
223
697k
        }
224
225
1.20M
        if (asn1_cb) {
226
92.2k
            i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg);
227
92.2k
            if (i == 0)
228
0
                return 0;
229
92.2k
            if (i == 2)
230
0
                return 1;
231
92.2k
        }
232
233
        /* Print each field entry */
234
6.71M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
235
5.56M
            const ASN1_TEMPLATE *seqtt;
236
5.56M
            seqtt = ossl_asn1_do_adb(*fld, tt, 1);
237
5.56M
            if (!seqtt)
238
0
                return 0;
239
5.56M
            tmpfld = ossl_asn1_get_const_field_ptr(fld, seqtt);
240
5.56M
            if (!asn1_template_print_ctx(out, tmpfld,
241
5.56M
                    indent + 2, seqtt, pctx))
242
52.3k
                return 0;
243
5.56M
        }
244
1.15M
        if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
245
197k
            if (BIO_printf(out, "%*s}\n", indent, "") < 0)
246
0
                return 0;
247
197k
        }
248
249
1.15M
        if (asn1_cb) {
250
66.2k
            i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg);
251
66.2k
            if (i == 0)
252
0
                return 0;
253
66.2k
        }
254
1.15M
        break;
255
256
1.15M
    default:
257
0
        BIO_printf(out, "Unprocessed type %d\n", it->itype);
258
0
        return 0;
259
6.31M
    }
260
261
6.05M
    return 1;
262
6.31M
}
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
5.88M
{
267
5.88M
    int i, flags;
268
5.88M
    const char *sname, *fname;
269
5.88M
    const ASN1_VALUE *tfld;
270
5.88M
    flags = tt->flags;
271
5.88M
    if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
272
693k
        sname = ASN1_ITEM_ptr(tt->item)->sname;
273
5.18M
    else
274
5.18M
        sname = NULL;
275
5.88M
    if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
276
0
        fname = NULL;
277
5.88M
    else
278
5.88M
        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
5.88M
    if (flags & ASN1_TFLG_EMBED) {
285
68.8k
        tfld = (const ASN1_VALUE *)fld;
286
68.8k
        fld = &tfld;
287
68.8k
    }
288
289
5.88M
    if (flags & ASN1_TFLG_SK_MASK) {
290
626k
        char *tname;
291
626k
        const ASN1_VALUE *skitem;
292
626k
        STACK_OF(const_ASN1_VALUE) *stack;
293
294
        /* SET OF, SEQUENCE OF */
295
626k
        if (fname) {
296
626k
            if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF) {
297
136k
                if (flags & ASN1_TFLG_SET_OF)
298
25.1k
                    tname = "SET";
299
111k
                else
300
111k
                    tname = "SEQUENCE";
301
136k
                if (BIO_printf(out, "%*s%s OF %s {\n",
302
136k
                        indent, "", tname, tt->field_name)
303
136k
                    <= 0)
304
0
                    return 0;
305
489k
            } else if (BIO_printf(out, "%*s%s:\n", indent, "", fname) <= 0)
306
0
                return 0;
307
626k
        }
308
626k
        stack = (STACK_OF(const_ASN1_VALUE) *)*fld;
309
4.67M
        for (i = 0; i < sk_const_ASN1_VALUE_num(stack); i++) {
310
4.05M
            if ((i > 0) && (BIO_puts(out, "\n") <= 0))
311
0
                return 0;
312
313
4.05M
            skitem = sk_const_ASN1_VALUE_value(stack, i);
314
4.05M
            if (!asn1_item_print_ctx(out, &skitem, indent + 2,
315
4.05M
                    ASN1_ITEM_ptr(tt->item), NULL, NULL, 1,
316
4.05M
                    pctx))
317
8.82k
                return 0;
318
4.05M
        }
319
617k
        if (i == 0 && BIO_printf(out, "%*s<%s>\n", indent + 2, "", stack == NULL ? "ABSENT" : "EMPTY") <= 0)
320
0
            return 0;
321
617k
        if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
322
132k
            if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
323
0
                return 0;
324
132k
        }
325
617k
        return 1;
326
617k
    }
327
5.25M
    return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
328
5.25M
        fname, sname, 0, pctx);
329
5.88M
}
330
331
static int asn1_print_fsname(BIO *out, int indent,
332
    const char *fname, const char *sname,
333
    const ASN1_PCTX *pctx)
334
8.86M
{
335
8.86M
    static const char spaces[] = "                    ";
336
8.86M
    static const int nspaces = sizeof(spaces) - 1;
337
338
8.91M
    while (indent > nspaces) {
339
45.3k
        if (BIO_write(out, spaces, nspaces) != nspaces)
340
0
            return 0;
341
45.3k
        indent -= nspaces;
342
45.3k
    }
343
8.86M
    if (BIO_write(out, spaces, indent) != indent)
344
0
        return 0;
345
8.86M
    if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
346
0
        sname = NULL;
347
8.86M
    if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
348
0
        fname = NULL;
349
8.86M
    if (!sname && !fname)
350
3.48M
        return 1;
351
5.38M
    if (fname) {
352
5.04M
        if (BIO_puts(out, fname) <= 0)
353
0
            return 0;
354
5.04M
    }
355
5.38M
    if (sname) {
356
884k
        if (fname) {
357
547k
            if (BIO_printf(out, " (%s)", sname) <= 0)
358
0
                return 0;
359
547k
        } else {
360
336k
            if (BIO_puts(out, sname) <= 0)
361
0
                return 0;
362
336k
        }
363
884k
    }
364
5.38M
    if (BIO_write(out, ": ", 2) != 2)
365
0
        return 0;
366
5.38M
    return 1;
367
5.38M
}
368
369
static int asn1_print_boolean(BIO *out, int boolval)
370
27.8k
{
371
27.8k
    const char *str;
372
27.8k
    switch (boolval) {
373
4.25k
    case -1:
374
4.25k
        str = "BOOL ABSENT";
375
4.25k
        break;
376
377
12.2k
    case 0:
378
12.2k
        str = "FALSE";
379
12.2k
        break;
380
381
11.3k
    default:
382
11.3k
        str = "TRUE";
383
11.3k
        break;
384
27.8k
    }
385
386
27.8k
    if (BIO_puts(out, str) <= 0)
387
0
        return 0;
388
27.8k
    return 1;
389
27.8k
}
390
391
static int asn1_print_integer(BIO *out, const ASN1_INTEGER *str)
392
3.31M
{
393
3.31M
    char *s;
394
3.31M
    int ret = 1;
395
3.31M
    s = i2s_ASN1_INTEGER(NULL, str);
396
3.31M
    if (s == NULL)
397
465
        return 0;
398
3.31M
    if (BIO_puts(out, s) <= 0)
399
0
        ret = 0;
400
3.31M
    OPENSSL_free(s);
401
3.31M
    return ret;
402
3.31M
}
403
404
static int asn1_print_oid(BIO *out, const ASN1_OBJECT *oid)
405
366k
{
406
366k
    char objbuf[80];
407
366k
    const char *ln;
408
366k
    ln = OBJ_nid2ln(OBJ_obj2nid(oid));
409
366k
    if (!ln)
410
0
        ln = "";
411
366k
    OBJ_obj2txt(objbuf, sizeof(objbuf), oid, 1);
412
366k
    if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
413
0
        return 0;
414
366k
    return 1;
415
366k
}
416
417
static int asn1_print_obstring(BIO *out, const ASN1_STRING *str, int indent)
418
230k
{
419
230k
    if (str->type == V_ASN1_BIT_STRING) {
420
116k
        if (BIO_printf(out, " (%ld unused bits)\n", str->flags & 0x7) <= 0)
421
0
            return 0;
422
116k
    } else if (BIO_puts(out, "\n") <= 0)
423
0
        return 0;
424
230k
    if ((str->length > 0)
425
138k
        && BIO_dump_indent(out, (const char *)str->data, str->length,
426
138k
               indent + 2)
427
138k
            <= 0)
428
0
        return 0;
429
230k
    return 1;
430
230k
}
431
432
static int asn1_primitive_print(BIO *out, const ASN1_VALUE **fld,
433
    const ASN1_ITEM *it, int indent,
434
    const char *fname, const char *sname,
435
    const ASN1_PCTX *pctx)
436
4.63M
{
437
4.63M
    long utype;
438
4.63M
    ASN1_STRING *str;
439
4.63M
    int ret = 1, needlf = 1;
440
4.63M
    const char *pname;
441
4.63M
    const ASN1_PRIMITIVE_FUNCS *pf;
442
4.63M
    pf = it->funcs;
443
4.63M
    if (!asn1_print_fsname(out, indent, fname, sname, pctx))
444
0
        return 0;
445
4.63M
    if (pf && pf->prim_print)
446
121k
        return pf->prim_print(out, fld, it, indent, pctx);
447
4.51M
    if (it->itype == ASN1_ITYPE_MSTRING) {
448
58.1k
        str = (ASN1_STRING *)*fld;
449
58.1k
        utype = str->type & ~V_ASN1_NEG;
450
4.45M
    } else {
451
4.45M
        utype = it->utype;
452
4.45M
        if (utype == V_ASN1_BOOLEAN)
453
22.6k
            str = NULL;
454
4.43M
        else
455
4.43M
            str = (ASN1_STRING *)*fld;
456
4.45M
    }
457
4.51M
    if (utype == V_ASN1_ANY) {
458
560k
        const ASN1_TYPE *atype = (const ASN1_TYPE *)*fld;
459
560k
        utype = atype->type;
460
560k
        fld = (const ASN1_VALUE **)&atype->value.asn1_value; /* actually is const */
461
560k
        str = (ASN1_STRING *)*fld;
462
560k
        if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
463
0
            pname = NULL;
464
560k
        else
465
560k
            pname = ASN1_tag2str(utype);
466
3.95M
    } else {
467
3.95M
        if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
468
331k
            pname = ASN1_tag2str(utype);
469
3.62M
        else
470
3.62M
            pname = NULL;
471
3.95M
    }
472
473
4.51M
    if (utype == V_ASN1_NULL) {
474
27.6k
        if (BIO_puts(out, "NULL\n") <= 0)
475
0
            return 0;
476
27.6k
        return 1;
477
27.6k
    }
478
479
4.48M
    if (pname) {
480
866k
        if (BIO_puts(out, pname) <= 0)
481
0
            return 0;
482
866k
        if (BIO_puts(out, ":") <= 0)
483
0
            return 0;
484
866k
    }
485
486
4.48M
    switch (utype) {
487
27.8k
    case V_ASN1_BOOLEAN: {
488
27.8k
        int boolval = *(int *)fld;
489
27.8k
        if (boolval == -1)
490
4.25k
            boolval = it->size;
491
27.8k
        ret = asn1_print_boolean(out, boolval);
492
27.8k
    } break;
493
494
3.31M
    case V_ASN1_INTEGER:
495
3.31M
    case V_ASN1_ENUMERATED:
496
3.31M
        ret = asn1_print_integer(out, str);
497
3.31M
        break;
498
499
17.7k
    case V_ASN1_UTCTIME:
500
17.7k
        ret = ASN1_UTCTIME_print(out, str);
501
17.7k
        break;
502
503
17.4k
    case V_ASN1_GENERALIZEDTIME:
504
17.4k
        ret = ASN1_GENERALIZEDTIME_print(out, str);
505
17.4k
        break;
506
507
366k
    case V_ASN1_OBJECT:
508
366k
        ret = asn1_print_oid(out, (const ASN1_OBJECT *)*fld);
509
366k
        break;
510
511
114k
    case V_ASN1_OCTET_STRING:
512
230k
    case V_ASN1_BIT_STRING:
513
230k
        ret = asn1_print_obstring(out, str, indent);
514
230k
        needlf = 0;
515
230k
        break;
516
517
297k
    case V_ASN1_SEQUENCE:
518
353k
    case V_ASN1_SET:
519
434k
    case V_ASN1_OTHER:
520
434k
        if (BIO_puts(out, "\n") <= 0)
521
0
            return 0;
522
434k
        if (ASN1_parse_dump(out, str->data, str->length, indent, 0) <= 0)
523
15.9k
            ret = 0;
524
434k
        needlf = 0;
525
434k
        break;
526
527
73.8k
    default:
528
73.8k
        ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
529
4.48M
    }
530
4.48M
    if (!ret)
531
36.1k
        return 0;
532
4.45M
    if (needlf && BIO_puts(out, "\n") <= 0)
533
0
        return 0;
534
4.45M
    return 1;
535
4.45M
}