Coverage Report

Created: 2024-07-27 06:39

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