Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/crypto/asn1/asn1_parse.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2023 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 <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/buffer.h>
13
#include <openssl/objects.h>
14
#include <openssl/asn1.h>
15
16
#ifndef ASN1_PARSE_MAXDEPTH
17
5.13M
#define ASN1_PARSE_MAXDEPTH 128
18
#endif
19
20
static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
21
    int offset, int depth, int indent, int dump);
22
static int asn1_print_info(BIO *bp, long offset, int depth, int hl, long len,
23
    int tag, int xclass, int constructed, int indent)
24
14.9M
{
25
14.9M
    char str[128];
26
14.9M
    const char *p;
27
14.9M
    int pop_f_prefix = 0;
28
14.9M
    long saved_indent = -1;
29
14.9M
    int i = 0;
30
14.9M
    BIO *bio = NULL;
31
32
14.9M
    if (constructed & V_ASN1_CONSTRUCTED)
33
5.46M
        p = "cons: ";
34
9.51M
    else
35
9.51M
        p = "prim: ";
36
14.9M
    if (constructed != (V_ASN1_CONSTRUCTED | 1)) {
37
12.6M
        if (BIO_snprintf(str, sizeof(str), "%5ld:d=%-2d hl=%ld l=%4ld %s",
38
12.6M
                offset, depth, (long)hl, len, p)
39
12.6M
            <= 0)
40
0
            goto err;
41
12.6M
    } else {
42
2.37M
        if (BIO_snprintf(str, sizeof(str), "%5ld:d=%-2d hl=%ld l=inf  %s",
43
2.37M
                offset, depth, (long)hl, p)
44
2.37M
            <= 0)
45
0
            goto err;
46
2.37M
    }
47
14.9M
    if (bp != NULL) {
48
14.9M
        if (BIO_set_prefix(bp, str) <= 0) {
49
14.9M
            if ((bio = BIO_new(BIO_f_prefix())) == NULL
50
14.9M
                || (bp = BIO_push(bio, bp)) == NULL)
51
0
                goto err;
52
14.9M
            pop_f_prefix = 1;
53
14.9M
        }
54
14.9M
        saved_indent = BIO_get_indent(bp);
55
14.9M
        if (BIO_set_prefix(bp, str) <= 0 || BIO_set_indent(bp, indent) <= 0)
56
0
            goto err;
57
14.9M
    }
58
59
    /*
60
     * BIO_set_prefix made a copy of |str|, so we can safely use it for
61
     * something else, ASN.1 tag printout.
62
     */
63
14.9M
    p = str;
64
14.9M
    if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
65
79.4k
        BIO_snprintf(str, sizeof(str), "priv [ %d ] ", tag);
66
14.9M
    else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
67
1.06M
        BIO_snprintf(str, sizeof(str), "cont [ %d ]", tag);
68
13.8M
    else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
69
138k
        BIO_snprintf(str, sizeof(str), "appl [ %d ]", tag);
70
13.6M
    else if (tag > 30)
71
33.1k
        BIO_snprintf(str, sizeof(str), "<ASN1 %d>", tag);
72
13.6M
    else
73
13.6M
        p = ASN1_tag2str(tag);
74
75
14.9M
    i = (BIO_printf(bp, "%-18s", p) > 0);
76
14.9M
err:
77
14.9M
    if (saved_indent >= 0)
78
14.9M
        BIO_set_indent(bp, saved_indent);
79
14.9M
    if (pop_f_prefix)
80
14.9M
        BIO_pop(bp);
81
14.9M
    BIO_free(bio);
82
14.9M
    return i;
83
14.9M
}
84
85
int ASN1_parse(BIO *bp, const unsigned char *pp, long len, int indent)
86
0
{
87
0
    return asn1_parse2(bp, &pp, len, 0, 0, indent, 0);
88
0
}
89
90
int ASN1_parse_dump(BIO *bp, const unsigned char *pp, long len, int indent,
91
    int dump)
92
805k
{
93
805k
    return asn1_parse2(bp, &pp, len, 0, 0, indent, dump);
94
805k
}
95
96
static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
97
    int offset, int depth, int indent, int dump)
98
5.13M
{
99
5.13M
    const unsigned char *p, *ep, *tot, *op, *opp;
100
5.13M
    long len;
101
5.13M
    int tag, xclass, ret = 0;
102
5.13M
    int nl, hl, j, r;
103
5.13M
    ASN1_OBJECT *o = NULL;
104
5.13M
    ASN1_OCTET_STRING *os = NULL;
105
5.13M
    ASN1_INTEGER *ai = NULL;
106
5.13M
    ASN1_ENUMERATED *ae = NULL;
107
    /* ASN1_BMPSTRING *bmp=NULL; */
108
5.13M
    int dump_indent, dump_cont = 0;
109
110
5.13M
    if (depth > ASN1_PARSE_MAXDEPTH) {
111
303
        BIO_puts(bp, "BAD RECURSION DEPTH\n");
112
303
        return 0;
113
303
    }
114
115
5.13M
    dump_indent = 6; /* Because we know BIO_dump_indent() */
116
5.13M
    p = *pp;
117
5.13M
    tot = p + length;
118
17.0M
    while (length > 0) {
119
15.1M
        op = p;
120
15.1M
        j = ASN1_get_object(&p, &len, &tag, &xclass, length);
121
15.1M
        if (j & 0x80) {
122
155k
            BIO_puts(bp, "Error in encoding\n");
123
155k
            goto end;
124
155k
        }
125
14.9M
        hl = (p - op);
126
14.9M
        length -= hl;
127
        /*
128
         * if j == 0x21 it is a constructed indefinite length object
129
         */
130
14.9M
        if (!asn1_print_info(bp, (long)offset + (long)(op - *pp), depth,
131
14.9M
                hl, len, tag, xclass, j, (indent) ? depth : 0))
132
0
            goto end;
133
14.9M
        if (j & V_ASN1_CONSTRUCTED) {
134
5.46M
            const unsigned char *sp = p;
135
136
5.46M
            ep = p + len;
137
5.46M
            if (BIO_write(bp, "\n", 1) <= 0)
138
0
                goto end;
139
5.46M
            if (len > length) {
140
0
                BIO_printf(bp, "length is greater than %ld\n", length);
141
0
                goto end;
142
0
            }
143
5.46M
            if ((j == 0x21) && (len == 0)) {
144
2.37M
                for (;;) {
145
2.37M
                    r = asn1_parse2(bp, &p, (long)(tot - p),
146
2.37M
                        offset + (p - *pp), depth + 1,
147
2.37M
                        indent, dump);
148
2.37M
                    if (r == 0)
149
155k
                        goto end;
150
2.21M
                    if ((r == 2) || (p >= tot)) {
151
2.21M
                        len = p - sp;
152
2.21M
                        break;
153
2.21M
                    }
154
2.21M
                }
155
3.09M
            } else {
156
3.09M
                long tmp = len;
157
158
4.79M
                while (p < ep) {
159
1.95M
                    sp = p;
160
1.95M
                    r = asn1_parse2(bp, &p, tmp,
161
1.95M
                        offset + (p - *pp), depth + 1,
162
1.95M
                        indent, dump);
163
1.95M
                    if (r == 0)
164
255k
                        goto end;
165
1.69M
                    tmp -= p - sp;
166
1.69M
                }
167
3.09M
            }
168
9.51M
        } else if (xclass != 0) {
169
348k
            p += len;
170
348k
            if (BIO_write(bp, "\n", 1) <= 0)
171
0
                goto end;
172
9.16M
        } else {
173
9.16M
            nl = 0;
174
9.16M
            if ((tag == V_ASN1_PRINTABLESTRING) || (tag == V_ASN1_T61STRING) || (tag == V_ASN1_IA5STRING) || (tag == V_ASN1_VISIBLESTRING) || (tag == V_ASN1_NUMERICSTRING) || (tag == V_ASN1_UTF8STRING) || (tag == V_ASN1_UTCTIME) || (tag == V_ASN1_GENERALIZEDTIME)) {
175
537k
                if (BIO_write(bp, ":", 1) <= 0)
176
0
                    goto end;
177
537k
                if ((len > 0) && BIO_write(bp, (const char *)p, (int)len) != (int)len)
178
0
                    goto end;
179
8.62M
            } else if (tag == V_ASN1_OBJECT) {
180
2.12M
                opp = op;
181
2.12M
                if (d2i_ASN1_OBJECT(&o, &opp, len + hl) != NULL) {
182
1.97M
                    if (BIO_write(bp, ":", 1) <= 0)
183
0
                        goto end;
184
1.97M
                    i2a_ASN1_OBJECT(bp, o);
185
1.97M
                } else {
186
156k
                    if (BIO_puts(bp, ":BAD OBJECT") <= 0)
187
0
                        goto end;
188
156k
                    dump_cont = 1;
189
156k
                }
190
6.50M
            } else if (tag == V_ASN1_BOOLEAN) {
191
251k
                if (len != 1) {
192
91.0k
                    if (BIO_puts(bp, ":BAD BOOLEAN") <= 0)
193
0
                        goto end;
194
91.0k
                    dump_cont = 1;
195
91.0k
                }
196
251k
                if (len > 0)
197
194k
                    BIO_printf(bp, ":%u", p[0]);
198
6.25M
            } else if (tag == V_ASN1_BMPSTRING) {
199
                /* do the BMP thang */
200
6.17M
            } else if (tag == V_ASN1_OCTET_STRING) {
201
1.02M
                int i, printable = 1;
202
203
1.02M
                opp = op;
204
1.02M
                os = d2i_ASN1_OCTET_STRING(NULL, &opp, len + hl);
205
1.02M
                if (os != NULL && os->length > 0) {
206
715k
                    opp = os->data;
207
                    /*
208
                     * testing whether the octet string is printable
209
                     */
210
1.81M
                    for (i = 0; i < os->length; i++) {
211
1.76M
                        if (((opp[i] < ' ') && (opp[i] != '\n') && (opp[i] != '\r') && (opp[i] != '\t')) || (opp[i] > '~')) {
212
669k
                            printable = 0;
213
669k
                            break;
214
669k
                        }
215
1.76M
                    }
216
715k
                    if (printable)
217
                    /* printable string */
218
46.0k
                    {
219
46.0k
                        if (BIO_write(bp, ":", 1) <= 0)
220
0
                            goto end;
221
46.0k
                        if (BIO_write(bp, (const char *)opp, os->length) <= 0)
222
0
                            goto end;
223
669k
                    } else if (!dump)
224
                    /*
225
                     * not printable => print octet string as hex dump
226
                     */
227
616k
                    {
228
616k
                        if (BIO_write(bp, "[HEX DUMP]:", 11) <= 0)
229
0
                            goto end;
230
25.0M
                        for (i = 0; i < os->length; i++) {
231
24.4M
                            if (BIO_printf(bp, "%02X", opp[i]) <= 0)
232
0
                                goto end;
233
24.4M
                        }
234
616k
                    } else
235
                    /* print the normal dump */
236
52.3k
                    {
237
52.3k
                        if (!nl) {
238
52.3k
                            if (BIO_write(bp, "\n", 1) <= 0)
239
0
                                goto end;
240
52.3k
                        }
241
52.3k
                        if (BIO_dump_indent(bp,
242
52.3k
                                (const char *)opp,
243
52.3k
                                ((dump == -1 || dump > os->length) ? os->length : dump),
244
52.3k
                                dump_indent)
245
52.3k
                            <= 0)
246
0
                            goto end;
247
52.3k
                        nl = 1;
248
52.3k
                    }
249
715k
                }
250
1.02M
                ASN1_OCTET_STRING_free(os);
251
1.02M
                os = NULL;
252
5.15M
            } else if (tag == V_ASN1_INTEGER) {
253
1.31M
                int i;
254
255
1.31M
                opp = op;
256
1.31M
                ai = d2i_ASN1_INTEGER(NULL, &opp, len + hl);
257
1.31M
                if (ai != NULL) {
258
1.07M
                    if (BIO_write(bp, ":", 1) <= 0)
259
0
                        goto end;
260
1.07M
                    if (ai->type == V_ASN1_NEG_INTEGER)
261
270k
                        if (BIO_write(bp, "-", 1) <= 0)
262
0
                            goto end;
263
37.9M
                    for (i = 0; i < ai->length; i++) {
264
36.8M
                        if (BIO_printf(bp, "%02X", ai->data[i]) <= 0)
265
0
                            goto end;
266
36.8M
                    }
267
1.07M
                    if (ai->length == 0) {
268
0
                        if (BIO_write(bp, "00", 2) <= 0)
269
0
                            goto end;
270
0
                    }
271
1.07M
                } else {
272
247k
                    if (BIO_puts(bp, ":BAD INTEGER") <= 0)
273
0
                        goto end;
274
247k
                    dump_cont = 1;
275
247k
                }
276
1.31M
                ASN1_INTEGER_free(ai);
277
1.31M
                ai = NULL;
278
3.83M
            } else if (tag == V_ASN1_ENUMERATED) {
279
130k
                int i;
280
281
130k
                opp = op;
282
130k
                ae = d2i_ASN1_ENUMERATED(NULL, &opp, len + hl);
283
130k
                if (ae != NULL) {
284
90.2k
                    if (BIO_write(bp, ":", 1) <= 0)
285
0
                        goto end;
286
90.2k
                    if (ae->type == V_ASN1_NEG_ENUMERATED)
287
20.0k
                        if (BIO_write(bp, "-", 1) <= 0)
288
0
                            goto end;
289
9.25M
                    for (i = 0; i < ae->length; i++) {
290
9.16M
                        if (BIO_printf(bp, "%02X", ae->data[i]) <= 0)
291
0
                            goto end;
292
9.16M
                    }
293
90.2k
                    if (ae->length == 0) {
294
0
                        if (BIO_write(bp, "00", 2) <= 0)
295
0
                            goto end;
296
0
                    }
297
90.2k
                } else {
298
39.8k
                    if (BIO_puts(bp, ":BAD ENUMERATED") <= 0)
299
0
                        goto end;
300
39.8k
                    dump_cont = 1;
301
39.8k
                }
302
130k
                ASN1_ENUMERATED_free(ae);
303
130k
                ae = NULL;
304
3.70M
            } else if (len > 0 && dump) {
305
100k
                if (!nl) {
306
100k
                    if (BIO_write(bp, "\n", 1) <= 0)
307
0
                        goto end;
308
100k
                }
309
100k
                if (BIO_dump_indent(bp, (const char *)p,
310
100k
                        ((dump == -1 || dump > len) ? len : dump),
311
100k
                        dump_indent)
312
100k
                    <= 0)
313
0
                    goto end;
314
100k
                nl = 1;
315
100k
            }
316
9.16M
            if (dump_cont) {
317
534k
                int i;
318
534k
                const unsigned char *tmp = op + hl;
319
534k
                if (BIO_puts(bp, ":[") <= 0)
320
0
                    goto end;
321
30.6M
                for (i = 0; i < len; i++) {
322
30.1M
                    if (BIO_printf(bp, "%02X", tmp[i]) <= 0)
323
0
                        goto end;
324
30.1M
                }
325
534k
                if (BIO_puts(bp, "]") <= 0)
326
0
                    goto end;
327
534k
                dump_cont = 0;
328
534k
            }
329
330
9.16M
            if (!nl) {
331
9.01M
                if (BIO_write(bp, "\n", 1) <= 0)
332
0
                    goto end;
333
9.01M
            }
334
9.16M
            p += len;
335
9.16M
            if ((tag == V_ASN1_EOC) && (xclass == 0)) {
336
2.61M
                ret = 2; /* End of sequence */
337
2.61M
                goto end;
338
2.61M
            }
339
9.16M
        }
340
11.9M
        length -= len;
341
11.9M
    }
342
1.95M
    ret = 1;
343
5.13M
end:
344
5.13M
    ASN1_OBJECT_free(o);
345
5.13M
    ASN1_OCTET_STRING_free(os);
346
5.13M
    ASN1_INTEGER_free(ai);
347
5.13M
    ASN1_ENUMERATED_free(ae);
348
5.13M
    *pp = p;
349
5.13M
    return ret;
350
1.95M
}
351
352
const char *ASN1_tag2str(int tag)
353
14.5M
{
354
14.5M
    static const char *const tag2str[] = {
355
        /* 0-4 */
356
14.5M
        "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
357
        /* 5-9 */
358
14.5M
        "NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL",
359
        /* 10-13 */
360
14.5M
        "ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>",
361
        /* 15-17 */
362
14.5M
        "<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET",
363
        /* 18-20 */
364
14.5M
        "NUMERICSTRING", "PRINTABLESTRING", "T61STRING",
365
        /* 21-24 */
366
14.5M
        "VIDEOTEXSTRING", "IA5STRING", "UTCTIME", "GENERALIZEDTIME",
367
        /* 25-27 */
368
14.5M
        "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING",
369
        /* 28-30 */
370
14.5M
        "UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING"
371
14.5M
    };
372
373
14.5M
    if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
374
4.50k
        tag &= ~0x100;
375
376
14.5M
    if (tag < 0 || tag > 30)
377
91.5k
        return "(unknown)";
378
14.4M
    return tag2str[tag];
379
14.5M
}