Coverage Report

Created: 2023-06-08 06:43

/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
2
{
37
2
    ASN1_PCTX *ret;
38
39
2
    ret = OPENSSL_zalloc(sizeof(*ret));
40
2
    if (ret == NULL) {
41
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
42
0
        return NULL;
43
0
    }
44
2
    return ret;
45
2
}
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
2
{
59
2
    p->flags = flags;
60
2
}
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
2
{
99
2
    p->str_flags = flags;
100
2
}
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
44.4k
{
124
44.4k
    const char *sname;
125
44.4k
    if (pctx == NULL)
126
0
        pctx = &default_pctx;
127
44.4k
    if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
128
0
        sname = NULL;
129
44.4k
    else
130
44.4k
        sname = it->sname;
131
44.4k
    return asn1_item_print_ctx(out, &ifld, indent, it, NULL, sname, 0, pctx);
132
44.4k
}
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
783k
{
139
783k
    const ASN1_TEMPLATE *tt;
140
783k
    const ASN1_EXTERN_FUNCS *ef;
141
783k
    const ASN1_VALUE **tmpfld;
142
783k
    const ASN1_AUX *aux = it->funcs;
143
783k
    ASN1_aux_const_cb *asn1_cb = NULL;
144
783k
    ASN1_PRINT_ARG parg;
145
783k
    int i;
146
783k
    if (aux != NULL) {
147
48.2k
        parg.out = out;
148
48.2k
        parg.indent = indent;
149
48.2k
        parg.pctx = pctx;
150
48.2k
        asn1_cb = ((aux->flags & ASN1_AFLG_CONST_CB) != 0) ? aux->asn1_const_cb
151
48.2k
            : (ASN1_aux_const_cb *)aux->asn1_cb; /* backward compatibility */
152
48.2k
    }
153
154
783k
   if (((it->itype != ASN1_ITYPE_PRIMITIVE)
155
783k
       || (it->utype != V_ASN1_BOOLEAN)) && *fld == NULL) {
156
15.3k
        if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT) {
157
15.3k
            if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
158
0
                return 0;
159
15.3k
            if (BIO_puts(out, "<ABSENT>\n") <= 0)
160
0
                return 0;
161
15.3k
        }
162
15.3k
        return 1;
163
15.3k
    }
164
165
767k
    switch (it->itype) {
166
696k
    case ASN1_ITYPE_PRIMITIVE:
167
696k
        if (it->templates) {
168
8.66k
            if (!asn1_template_print_ctx(out, fld, indent,
169
8.66k
                                         it->templates, pctx))
170
569
                return 0;
171
8.10k
            break;
172
8.66k
        }
173
        /* fall through */
174
698k
    case ASN1_ITYPE_MSTRING:
175
698k
        if (!asn1_primitive_print(out, fld, it, indent, fname, sname, pctx))
176
4.19k
            return 0;
177
693k
        break;
178
179
693k
    case ASN1_ITYPE_EXTERN:
180
3.90k
        if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
181
0
            return 0;
182
        /* Use new style print routine if possible */
183
3.90k
        ef = it->funcs;
184
3.90k
        if (ef && ef->asn1_ex_print) {
185
3.90k
            i = ef->asn1_ex_print(out, fld, indent, "", pctx);
186
3.90k
            if (!i)
187
39
                return 0;
188
3.86k
            if ((i == 2) && (BIO_puts(out, "\n") <= 0))
189
0
                return 0;
190
3.86k
            return 1;
191
3.86k
        } else if (sname &&
192
0
                   BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
193
0
            return 0;
194
0
        break;
195
196
13.8k
    case ASN1_ITYPE_CHOICE:
197
        /* CHOICE type, get selector */
198
13.8k
        i = ossl_asn1_get_choice_selector_const(fld, it);
199
        /* This should never happen... */
200
13.8k
        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
13.8k
        tt = it->templates + i;
206
13.8k
        tmpfld = ossl_asn1_get_const_field_ptr(fld, tt);
207
13.8k
        if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
208
76
            return 0;
209
13.7k
        break;
210
211
39.7k
    case ASN1_ITYPE_SEQUENCE:
212
43.3k
    case ASN1_ITYPE_NDEF_SEQUENCE:
213
43.3k
        if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
214
0
            return 0;
215
43.3k
        if (fname || sname) {
216
16.2k
            if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
217
16.2k
                if (BIO_puts(out, " {\n") <= 0)
218
0
                    return 0;
219
16.2k
            } else {
220
0
                if (BIO_puts(out, "\n") <= 0)
221
0
                    return 0;
222
0
            }
223
16.2k
        }
224
225
43.3k
        if (asn1_cb) {
226
5.97k
            i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg);
227
5.97k
            if (i == 0)
228
0
                return 0;
229
5.97k
            if (i == 2)
230
0
                return 1;
231
5.97k
        }
232
233
        /* Print each field entry */
234
160k
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
235
119k
            const ASN1_TEMPLATE *seqtt;
236
119k
            seqtt = ossl_asn1_do_adb(*fld, tt, 1);
237
119k
            if (!seqtt)
238
0
                return 0;
239
119k
            tmpfld = ossl_asn1_get_const_field_ptr(fld, seqtt);
240
119k
            if (!asn1_template_print_ctx(out, tmpfld,
241
119k
                                         indent + 2, seqtt, pctx))
242
2.24k
                return 0;
243
119k
        }
244
41.1k
        if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
245
41.1k
            if (BIO_printf(out, "%*s}\n", indent, "") < 0)
246
0
                return 0;
247
41.1k
        }
248
249
41.1k
        if (asn1_cb) {
250
4.98k
            i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg);
251
4.98k
            if (i == 0)
252
0
                return 0;
253
4.98k
        }
254
41.1k
        break;
255
256
41.1k
    default:
257
0
        BIO_printf(out, "Unprocessed type %d\n", it->itype);
258
0
        return 0;
259
767k
    }
260
261
756k
    return 1;
262
767k
}
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
141k
{
267
141k
    int i, flags;
268
141k
    const char *sname, *fname;
269
141k
    const ASN1_VALUE *tfld;
270
141k
    flags = tt->flags;
271
141k
    if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
272
141k
        sname = ASN1_ITEM_ptr(tt->item)->sname;
273
0
    else
274
0
        sname = NULL;
275
141k
    if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
276
0
        fname = NULL;
277
141k
    else
278
141k
        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
141k
    if (flags & ASN1_TFLG_EMBED) {
285
10.6k
        tfld = (const ASN1_VALUE *)fld;
286
10.6k
        fld = &tfld;
287
10.6k
    }
288
289
141k
    if (flags & ASN1_TFLG_SK_MASK) {
290
23.8k
        char *tname;
291
23.8k
        const ASN1_VALUE *skitem;
292
23.8k
        STACK_OF(const_ASN1_VALUE) *stack;
293
294
        /* SET OF, SEQUENCE OF */
295
23.8k
        if (fname) {
296
23.8k
            if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF) {
297
23.8k
                if (flags & ASN1_TFLG_SET_OF)
298
3.66k
                    tname = "SET";
299
20.2k
                else
300
20.2k
                    tname = "SEQUENCE";
301
23.8k
                if (BIO_printf(out, "%*s%s OF %s {\n",
302
23.8k
                               indent, "", tname, tt->field_name) <= 0)
303
0
                    return 0;
304
23.8k
            } else if (BIO_printf(out, "%*s%s:\n", indent, "", fname) <= 0)
305
0
                return 0;
306
23.8k
        }
307
23.8k
        stack = (STACK_OF(const_ASN1_VALUE) *)*fld;
308
644k
        for (i = 0; i < sk_const_ASN1_VALUE_num(stack); i++) {
309
620k
            if ((i > 0) && (BIO_puts(out, "\n") <= 0))
310
0
                return 0;
311
312
620k
            skitem = sk_const_ASN1_VALUE_value(stack, i);
313
620k
            if (!asn1_item_print_ctx(out, &skitem, indent + 2,
314
620k
                                     ASN1_ITEM_ptr(tt->item), NULL, NULL, 1,
315
620k
                                     pctx))
316
750
                return 0;
317
620k
        }
318
23.1k
        if (i == 0 && BIO_printf(out, "%*s<%s>\n", indent + 2, "",
319
13.8k
                                 stack == NULL ? "ABSENT" : "EMPTY") <= 0)
320
0
            return 0;
321
23.1k
        if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
322
23.1k
            if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
323
0
                return 0;
324
23.1k
        }
325
23.1k
        return 1;
326
23.1k
    }
327
118k
    return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
328
118k
                               fname, sname, 0, pctx);
329
141k
}
330
331
static int asn1_print_fsname(BIO *out, int indent,
332
                             const char *fname, const char *sname,
333
                             const ASN1_PCTX *pctx)
334
733k
{
335
733k
    static const char spaces[] = "                    ";
336
733k
    static const int nspaces = sizeof(spaces) - 1;
337
338
734k
    while (indent > nspaces) {
339
1.18k
        if (BIO_write(out, spaces, nspaces) != nspaces)
340
0
            return 0;
341
1.18k
        indent -= nspaces;
342
1.18k
    }
343
733k
    if (BIO_write(out, spaces, indent) != indent)
344
0
        return 0;
345
733k
    if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
346
0
        sname = NULL;
347
733k
    if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
348
0
        fname = NULL;
349
733k
    if (!sname && !fname)
350
582k
        return 1;
351
151k
    if (fname) {
352
116k
        if (BIO_puts(out, fname) <= 0)
353
0
            return 0;
354
116k
    }
355
151k
    if (sname) {
356
151k
        if (fname) {
357
116k
            if (BIO_printf(out, " (%s)", sname) <= 0)
358
0
                return 0;
359
116k
        } else {
360
35.1k
            if (BIO_puts(out, sname) <= 0)
361
0
                return 0;
362
35.1k
        }
363
151k
    }
364
151k
    if (BIO_write(out, ": ", 2) != 2)
365
0
        return 0;
366
151k
    return 1;
367
151k
}
368
369
static int asn1_print_boolean(BIO *out, int boolval)
370
4.35k
{
371
4.35k
    const char *str;
372
4.35k
    switch (boolval) {
373
1.38k
    case -1:
374
1.38k
        str = "BOOL ABSENT";
375
1.38k
        break;
376
377
1.49k
    case 0:
378
1.49k
        str = "FALSE";
379
1.49k
        break;
380
381
1.48k
    default:
382
1.48k
        str = "TRUE";
383
1.48k
        break;
384
385
4.35k
    }
386
387
4.35k
    if (BIO_puts(out, str) <= 0)
388
0
        return 0;
389
4.35k
    return 1;
390
391
4.35k
}
392
393
static int asn1_print_integer(BIO *out, const ASN1_INTEGER *str)
394
19.7k
{
395
19.7k
    char *s;
396
19.7k
    int ret = 1;
397
19.7k
    s = i2s_ASN1_INTEGER(NULL, str);
398
19.7k
    if (s == NULL)
399
70
        return 0;
400
19.7k
    if (BIO_puts(out, s) <= 0)
401
0
        ret = 0;
402
19.7k
    OPENSSL_free(s);
403
19.7k
    return ret;
404
19.7k
}
405
406
static int asn1_print_oid(BIO *out, const ASN1_OBJECT *oid)
407
24.5k
{
408
24.5k
    char objbuf[80];
409
24.5k
    const char *ln;
410
24.5k
    ln = OBJ_nid2ln(OBJ_obj2nid(oid));
411
24.5k
    if (!ln)
412
0
        ln = "";
413
24.5k
    OBJ_obj2txt(objbuf, sizeof(objbuf), oid, 1);
414
24.5k
    if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
415
0
        return 0;
416
24.5k
    return 1;
417
24.5k
}
418
419
static int asn1_print_obstring(BIO *out, const ASN1_STRING *str, int indent)
420
16.4k
{
421
16.4k
    if (str->type == V_ASN1_BIT_STRING) {
422
4.43k
        if (BIO_printf(out, " (%ld unused bits)\n", str->flags & 0x7) <= 0)
423
0
            return 0;
424
12.0k
    } else if (BIO_puts(out, "\n") <= 0)
425
0
        return 0;
426
16.4k
    if ((str->length > 0)
427
16.4k
        && BIO_dump_indent(out, (const char *)str->data, str->length,
428
9.12k
                           indent + 2) <= 0)
429
0
        return 0;
430
16.4k
    return 1;
431
16.4k
}
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
698k
{
438
698k
    long utype;
439
698k
    ASN1_STRING *str;
440
698k
    int ret = 1, needlf = 1;
441
698k
    const char *pname;
442
698k
    const ASN1_PRIMITIVE_FUNCS *pf;
443
698k
    pf = it->funcs;
444
698k
    if (!asn1_print_fsname(out, indent, fname, sname, pctx))
445
0
        return 0;
446
698k
    if (pf && pf->prim_print)
447
34.5k
        return pf->prim_print(out, fld, it, indent, pctx);
448
663k
    if (it->itype == ASN1_ITYPE_MSTRING) {
449
10.6k
        str = (ASN1_STRING *)*fld;
450
10.6k
        utype = str->type & ~V_ASN1_NEG;
451
652k
    } else {
452
652k
        utype = it->utype;
453
652k
        if (utype == V_ASN1_BOOLEAN)
454
3.83k
            str = NULL;
455
649k
        else
456
649k
            str = (ASN1_STRING *)*fld;
457
652k
    }
458
663k
    if (utype == V_ASN1_ANY) {
459
597k
        const ASN1_TYPE *atype = (const ASN1_TYPE *)*fld;
460
597k
        utype = atype->type;
461
597k
        fld = (const ASN1_VALUE **)&atype->value.asn1_value; /* actually is const */
462
597k
        str = (ASN1_STRING *)*fld;
463
597k
        if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
464
0
            pname = NULL;
465
597k
        else
466
597k
            pname = ASN1_tag2str(utype);
467
597k
    } else {
468
66.4k
        if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
469
66.4k
            pname = ASN1_tag2str(utype);
470
0
        else
471
0
            pname = NULL;
472
66.4k
    }
473
474
663k
    if (utype == V_ASN1_NULL) {
475
464k
        if (BIO_puts(out, "NULL\n") <= 0)
476
0
            return 0;
477
464k
        return 1;
478
464k
    }
479
480
198k
    if (pname) {
481
198k
        if (BIO_puts(out, pname) <= 0)
482
0
            return 0;
483
198k
        if (BIO_puts(out, ":") <= 0)
484
0
            return 0;
485
198k
    }
486
487
198k
    switch (utype) {
488
4.35k
    case V_ASN1_BOOLEAN:
489
4.35k
        {
490
4.35k
            int boolval = *(int *)fld;
491
4.35k
            if (boolval == -1)
492
1.38k
                boolval = it->size;
493
4.35k
            ret = asn1_print_boolean(out, boolval);
494
4.35k
        }
495
4.35k
        break;
496
497
19.7k
    case V_ASN1_INTEGER:
498
19.7k
    case V_ASN1_ENUMERATED:
499
19.7k
        ret = asn1_print_integer(out, str);
500
19.7k
        break;
501
502
3.91k
    case V_ASN1_UTCTIME:
503
3.91k
        ret = ASN1_UTCTIME_print(out, str);
504
3.91k
        break;
505
506
2.21k
    case V_ASN1_GENERALIZEDTIME:
507
2.21k
        ret = ASN1_GENERALIZEDTIME_print(out, str);
508
2.21k
        break;
509
510
24.5k
    case V_ASN1_OBJECT:
511
24.5k
        ret = asn1_print_oid(out, (const ASN1_OBJECT *)*fld);
512
24.5k
        break;
513
514
12.0k
    case V_ASN1_OCTET_STRING:
515
16.4k
    case V_ASN1_BIT_STRING:
516
16.4k
        ret = asn1_print_obstring(out, str, indent);
517
16.4k
        needlf = 0;
518
16.4k
        break;
519
520
35.0k
    case V_ASN1_SEQUENCE:
521
40.6k
    case V_ASN1_SET:
522
84.9k
    case V_ASN1_OTHER:
523
84.9k
        if (BIO_puts(out, "\n") <= 0)
524
0
            return 0;
525
84.9k
        if (ASN1_parse_dump(out, str->data, str->length, indent, 0) <= 0)
526
3.17k
            ret = 0;
527
84.9k
        needlf = 0;
528
84.9k
        break;
529
530
42.7k
    default:
531
42.7k
        ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
532
533
198k
    }
534
198k
    if (!ret)
535
4.19k
        return 0;
536
194k
    if (needlf && BIO_puts(out, "\n") <= 0)
537
0
        return 0;
538
194k
    return 1;
539
194k
}