Coverage Report

Created: 2026-07-16 06:59

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