Coverage Report

Created: 2026-09-12 06:55

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