Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/asn1/asn1_parse.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 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
3.57M
#define ASN1_PARSE_MAXDEPTH 128
18
#endif
19
20
#include <crypto/asn1.h>
21
22
static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
23
    int offset, int depth, int indent, int dump);
24
static int asn1_print_info(BIO *bp, long offset, int depth, int hl, long len,
25
    int tag, int xclass, int constructed, int indent)
26
1.89M
{
27
1.89M
    char str[128];
28
1.89M
    const char *p;
29
1.89M
    int pop_f_prefix = 0;
30
1.89M
    long saved_indent = -1;
31
1.89M
    int i = 0, n;
32
1.89M
    BIO *bio = NULL;
33
34
1.89M
    if (constructed & V_ASN1_CONSTRUCTED)
35
706k
        p = "cons: ";
36
1.18M
    else
37
1.18M
        p = "prim: ";
38
1.89M
    if (constructed != (V_ASN1_CONSTRUCTED | 1)) {
39
1.63M
        n = snprintf(str, sizeof(str), "%5ld:d=%-2d hl=%ld l=%4ld %s",
40
1.63M
            offset, depth, (long)hl, len, p);
41
1.63M
    } else {
42
257k
        n = snprintf(str, sizeof(str), "%5ld:d=%-2d hl=%ld l=inf  %s",
43
257k
            offset, depth, (long)hl, p);
44
257k
    }
45
1.89M
    if (n <= 0 || (size_t)n >= sizeof(str))
46
0
        goto err;
47
1.89M
    if (bp != NULL) {
48
1.89M
        if (BIO_set_prefix(bp, str) <= 0) {
49
1.89M
            if ((bio = BIO_new(BIO_f_prefix())) == NULL
50
1.89M
                || (bp = BIO_push(bio, bp)) == NULL)
51
0
                goto err;
52
1.89M
            pop_f_prefix = 1;
53
1.89M
        }
54
1.89M
        saved_indent = BIO_get_indent(bp);
55
1.89M
        if (BIO_set_prefix(bp, str) <= 0 || BIO_set_indent(bp, indent) <= 0)
56
0
            goto err;
57
1.89M
    }
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
1.89M
    p = str;
64
1.89M
    if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
65
12.0k
        snprintf(str, sizeof(str), "priv [ %d ] ", tag);
66
1.87M
    else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
67
145k
        snprintf(str, sizeof(str), "cont [ %d ]", tag);
68
1.73M
    else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
69
12.5k
        snprintf(str, sizeof(str), "appl [ %d ]", tag);
70
1.72M
    else if (tag > 30)
71
5.97k
        snprintf(str, sizeof(str), "<ASN1 %d>", tag);
72
1.71M
    else
73
1.71M
        p = ASN1_tag2str(tag);
74
75
1.89M
    i = (BIO_printf(bp, "%-18s", p) > 0);
76
1.89M
err:
77
1.89M
    if (saved_indent >= 0)
78
1.89M
        BIO_set_indent(bp, saved_indent);
79
1.89M
    if (pop_f_prefix)
80
1.89M
        BIO_pop(bp);
81
1.89M
    BIO_free(bio);
82
1.89M
    return i;
83
1.89M
}
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
609k
{
93
609k
    return asn1_parse2(bp, &pp, len, 0, 0, indent, dump);
94
609k
}
95
96
static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
97
    int offset, int depth, int indent, int dump)
98
3.57M
{
99
3.57M
    const unsigned char *p, *ep, *tot, *op, *opp;
100
3.57M
    long len;
101
3.57M
    int tag, xclass, ret = 0;
102
3.57M
    int nl, hl, j, r;
103
3.57M
    ASN1_OBJECT *o = NULL;
104
3.57M
    ASN1_OCTET_STRING *os = NULL;
105
3.57M
    ASN1_INTEGER *ai = NULL;
106
3.57M
    ASN1_ENUMERATED *ae = NULL;
107
    /* ASN1_BMPSTRING *bmp=NULL; */
108
3.57M
    int dump_indent, dump_cont = 0;
109
110
3.57M
    if (depth > ASN1_PARSE_MAXDEPTH) {
111
178
        BIO_puts(bp, "BAD RECURSION DEPTH\n");
112
178
        return 0;
113
178
    }
114
115
3.57M
    dump_indent = 6; /* Because we know BIO_dump_indent() */
116
3.57M
    p = *pp;
117
3.57M
    tot = p + length;
118
11.9M
    while (length > 0) {
119
10.4M
        op = p;
120
10.4M
        j = ASN1_get_object(&p, &len, &tag, &xclass, length);
121
10.4M
        if (j & 0x80) {
122
67.6k
            BIO_puts(bp, "Error in encoding\n");
123
67.6k
            goto end;
124
67.6k
        }
125
10.3M
        hl = (int)(p - op);
126
10.3M
        length -= hl;
127
        /*
128
         * if j == 0x21 it is a constructed indefinite length object
129
         */
130
10.3M
        if (!asn1_print_info(bp, (long)offset + (long)(op - *pp), depth,
131
10.3M
                hl, len, tag, xclass, j, (indent) ? depth : 0))
132
0
            goto end;
133
10.3M
        if (j & V_ASN1_CONSTRUCTED) {
134
3.65M
            const unsigned char *sp = p;
135
136
3.65M
            ep = p + len;
137
3.65M
            if (BIO_write(bp, "\n", 1) <= 0)
138
0
                goto end;
139
3.65M
            if (len > length) {
140
0
                BIO_printf(bp, "length is greater than %ld\n", length);
141
0
                goto end;
142
0
            }
143
3.65M
            if ((j == 0x21) && (len == 0)) {
144
1.74M
                for (;;) {
145
1.74M
                    r = asn1_parse2(bp, &p, (long)(tot - p),
146
1.74M
                        offset + (int)(p - *pp), depth + 1,
147
1.74M
                        indent, dump);
148
1.74M
                    if (r == 0)
149
111k
                        goto end;
150
1.63M
                    if ((r == 2) || (p >= tot)) {
151
1.63M
                        len = (long)(p - sp);
152
1.63M
                        break;
153
1.63M
                    }
154
1.63M
                }
155
1.91M
            } else {
156
1.91M
                long tmp = len;
157
158
3.05M
                while (p < ep) {
159
1.22M
                    sp = p;
160
1.22M
                    r = asn1_parse2(bp, &p, tmp,
161
1.22M
                        offset + (int)(p - *pp), depth + 1,
162
1.22M
                        indent, dump);
163
1.22M
                    if (r == 0)
164
81.7k
                        goto end;
165
1.13M
                    tmp -= (long)(p - sp);
166
1.13M
                }
167
1.91M
            }
168
6.72M
        } else if (xclass != 0) {
169
239k
            p += len;
170
239k
            if (BIO_write(bp, "\n", 1) <= 0)
171
0
                goto end;
172
6.48M
        } else {
173
6.48M
            nl = 0;
174
6.48M
            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
461k
                if (BIO_write(bp, ":", 1) <= 0)
176
0
                    goto end;
177
461k
                if ((len > 0) && BIO_write(bp, (const char *)p, (int)len) != (int)len)
178
0
                    goto end;
179
6.02M
            } else if (tag == V_ASN1_OBJECT) {
180
1.67M
                opp = op;
181
1.67M
                if (d2i_ASN1_OBJECT(&o, &opp, len + hl) != NULL) {
182
1.50M
                    if (BIO_write(bp, ":", 1) <= 0)
183
0
                        goto end;
184
1.50M
                    i2a_ASN1_OBJECT(bp, o);
185
1.50M
                } else {
186
177k
                    if (BIO_puts(bp, ":BAD OBJECT") <= 0)
187
0
                        goto end;
188
177k
                    dump_cont = 1;
189
177k
                }
190
4.34M
            } else if (tag == V_ASN1_BOOLEAN) {
191
175k
                if (len != 1) {
192
79.6k
                    if (BIO_puts(bp, ":BAD BOOLEAN") <= 0)
193
0
                        goto end;
194
79.6k
                    dump_cont = 1;
195
79.6k
                }
196
175k
                if (len > 0)
197
131k
                    BIO_printf(bp, ":%u", p[0]);
198
4.16M
            } else if (tag == V_ASN1_BMPSTRING) {
199
                /* do the BMP thang */
200
4.14M
            } else if (tag == V_ASN1_OCTET_STRING) {
201
777k
                int i, printable = 1;
202
203
777k
                opp = op;
204
777k
                os = d2i_ASN1_OCTET_STRING(NULL, &opp, len + hl);
205
777k
                if (os != NULL && os->length > 0) {
206
576k
                    opp = os->data;
207
                    /*
208
                     * testing whether the octet string is printable
209
                     */
210
1.30M
                    for (i = 0; i < os->length; i++) {
211
1.27M
                        if (((opp[i] < ' ') && (opp[i] != '\n') && (opp[i] != '\r') && (opp[i] != '\t')) || (opp[i] > '~')) {
212
544k
                            printable = 0;
213
544k
                            break;
214
544k
                        }
215
1.27M
                    }
216
576k
                    if (printable)
217
                    /* printable string */
218
32.1k
                    {
219
32.1k
                        if (BIO_write(bp, ":", 1) <= 0)
220
0
                            goto end;
221
32.1k
                        if (BIO_write(bp, (const char *)opp, os->length) <= 0)
222
0
                            goto end;
223
544k
                    } else if (!dump)
224
                    /*
225
                     * not printable => print octet string as hex dump
226
                     */
227
500k
                    {
228
500k
                        if (BIO_write(bp, "[HEX DUMP]:", 11) <= 0)
229
0
                            goto end;
230
17.1M
                        for (i = 0; i < os->length; i++) {
231
16.6M
                            if (BIO_printf(bp, "%02X", opp[i]) <= 0)
232
0
                                goto end;
233
16.6M
                        }
234
500k
                    } else
235
                    /* print the normal dump */
236
44.1k
                    {
237
44.1k
                        if (!nl) {
238
44.1k
                            if (BIO_write(bp, "\n", 1) <= 0)
239
0
                                goto end;
240
44.1k
                        }
241
44.1k
                        if (BIO_dump_indent(bp,
242
44.1k
                                (const char *)opp,
243
44.1k
                                ((dump == -1 || dump > os->length) ? os->length : dump),
244
44.1k
                                dump_indent)
245
44.1k
                            <= 0)
246
0
                            goto end;
247
44.1k
                        nl = 1;
248
44.1k
                    }
249
576k
                }
250
777k
                ASN1_OCTET_STRING_free(os);
251
777k
                os = NULL;
252
3.36M
            } else if (tag == V_ASN1_INTEGER) {
253
853k
                int i;
254
255
853k
                opp = op;
256
853k
                ai = d2i_ASN1_INTEGER(NULL, &opp, len + hl);
257
853k
                if (ai != NULL) {
258
681k
                    if (BIO_write(bp, ":", 1) <= 0)
259
0
                        goto end;
260
681k
                    if (ai->type == V_ASN1_NEG_INTEGER)
261
192k
                        if (BIO_write(bp, "-", 1) <= 0)
262
0
                            goto end;
263
30.8M
                    for (i = 0; i < ai->length; i++) {
264
30.1M
                        if (BIO_printf(bp, "%02X", ai->data[i]) <= 0)
265
0
                            goto end;
266
30.1M
                    }
267
681k
                    if (ai->length == 0) {
268
0
                        if (BIO_write(bp, "00", 2) <= 0)
269
0
                            goto end;
270
0
                    }
271
681k
                } else {
272
172k
                    if (BIO_puts(bp, ":BAD INTEGER") <= 0)
273
0
                        goto end;
274
172k
                    dump_cont = 1;
275
172k
                }
276
853k
                ASN1_INTEGER_free(ai);
277
853k
                ai = NULL;
278
2.51M
            } else if (tag == V_ASN1_ENUMERATED) {
279
103k
                int i;
280
281
103k
                opp = op;
282
103k
                ae = d2i_ASN1_ENUMERATED(NULL, &opp, len + hl);
283
103k
                if (ae != NULL) {
284
56.4k
                    if (BIO_write(bp, ":", 1) <= 0)
285
0
                        goto end;
286
56.4k
                    if (ae->type == V_ASN1_NEG_ENUMERATED)
287
11.7k
                        if (BIO_write(bp, "-", 1) <= 0)
288
0
                            goto end;
289
6.70M
                    for (i = 0; i < ae->length; i++) {
290
6.64M
                        if (BIO_printf(bp, "%02X", ae->data[i]) <= 0)
291
0
                            goto end;
292
6.64M
                    }
293
56.4k
                    if (ae->length == 0) {
294
0
                        if (BIO_write(bp, "00", 2) <= 0)
295
0
                            goto end;
296
0
                    }
297
56.4k
                } else {
298
46.7k
                    if (BIO_puts(bp, ":BAD ENUMERATED") <= 0)
299
0
                        goto end;
300
46.7k
                    dump_cont = 1;
301
46.7k
                }
302
103k
                ASN1_ENUMERATED_free(ae);
303
103k
                ae = NULL;
304
2.40M
            } else if (len > 0 && dump) {
305
67.1k
                if (!nl) {
306
67.1k
                    if (BIO_write(bp, "\n", 1) <= 0)
307
0
                        goto end;
308
67.1k
                }
309
67.1k
                if (BIO_dump_indent(bp, (const char *)p,
310
67.1k
                        ((dump == -1 || dump > len) ? len : dump),
311
67.1k
                        dump_indent)
312
67.1k
                    <= 0)
313
0
                    goto end;
314
67.1k
                nl = 1;
315
67.1k
            }
316
6.48M
            if (dump_cont) {
317
476k
                int i;
318
476k
                const unsigned char *tmp = op + hl;
319
476k
                if (BIO_puts(bp, ":[") <= 0)
320
0
                    goto end;
321
23.3M
                for (i = 0; i < len; i++) {
322
22.9M
                    if (BIO_printf(bp, "%02X", tmp[i]) <= 0)
323
0
                        goto end;
324
22.9M
                }
325
476k
                if (BIO_puts(bp, "]") <= 0)
326
0
                    goto end;
327
476k
                dump_cont = 0;
328
476k
            }
329
330
6.48M
            if (!nl) {
331
6.37M
                if (BIO_write(bp, "\n", 1) <= 0)
332
0
                    goto end;
333
6.37M
            }
334
6.48M
            p += len;
335
6.48M
            if ((tag == V_ASN1_EOC) && (xclass == 0)) {
336
1.82M
                ret = 2; /* End of sequence */
337
1.82M
                goto end;
338
1.82M
            }
339
6.48M
        }
340
8.36M
        length -= len;
341
8.36M
    }
342
1.48M
    ret = 1;
343
3.57M
end:
344
3.57M
    ASN1_OBJECT_free(o);
345
3.57M
    ASN1_OCTET_STRING_free(os);
346
3.57M
    ASN1_INTEGER_free(ai);
347
3.57M
    ASN1_ENUMERATED_free(ae);
348
3.57M
    *pp = p;
349
3.57M
    return ret;
350
1.48M
}
351
352
const char *ASN1_tag2str(int tag)
353
10.1M
{
354
10.1M
    static const char *const tag2str[] = {
355
        /* 0-4 */
356
10.1M
        "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
357
        /* 5-9 */
358
10.1M
        "NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL",
359
        /* 10-13 */
360
10.1M
        "ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>",
361
        /* 14-17 */
362
10.1M
        "<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET",
363
        /* 18-20 */
364
10.1M
        "NUMERICSTRING", "PRINTABLESTRING", "T61STRING",
365
        /* 21-24 */
366
10.1M
        "VIDEOTEXSTRING", "IA5STRING", "UTCTIME", "GENERALIZEDTIME",
367
        /* 25-27 */
368
10.1M
        "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING",
369
        /* 28-30 */
370
10.1M
        "UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING"
371
10.1M
    };
372
373
10.1M
    if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
374
3.48k
        tag &= ~0x100;
375
376
10.1M
    if (tag < 0 || tag > 30)
377
68.3k
        return "(unknown)";
378
10.1M
    return tag2str[tag];
379
10.1M
}