Coverage Report

Created: 2026-10-28 06:28

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
5.45M
#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
12.7M
{
27
12.7M
    char str[128];
28
12.7M
    const char *p;
29
12.7M
    int pop_f_prefix = 0;
30
12.7M
    long saved_indent = -1;
31
12.7M
    int i = 0;
32
12.7M
    BIO *bio = NULL;
33
34
12.7M
    if (constructed & V_ASN1_CONSTRUCTED)
35
4.56M
        p = "cons: ";
36
8.17M
    else
37
8.17M
        p = "prim: ";
38
12.7M
    if (constructed != (V_ASN1_CONSTRUCTED | 1)) {
39
10.3M
        if (BIO_snprintf(str, sizeof(str), "%5ld:d=%-2d hl=%ld l=%4ld %s",
40
10.3M
                offset, depth, (long)hl, len, p)
41
10.3M
            <= 0)
42
0
            goto err;
43
10.3M
    } else {
44
2.38M
        if (BIO_snprintf(str, sizeof(str), "%5ld:d=%-2d hl=%ld l=inf  %s",
45
2.38M
                offset, depth, (long)hl, p)
46
2.38M
            <= 0)
47
0
            goto err;
48
2.38M
    }
49
12.7M
    if (bp != NULL) {
50
12.7M
        if (BIO_set_prefix(bp, str) <= 0) {
51
12.7M
            if ((bio = BIO_new(BIO_f_prefix())) == NULL
52
12.7M
                || (bp = BIO_push(bio, bp)) == NULL)
53
0
                goto err;
54
12.7M
            pop_f_prefix = 1;
55
12.7M
        }
56
12.7M
        saved_indent = BIO_get_indent(bp);
57
12.7M
        if (BIO_set_prefix(bp, str) <= 0 || BIO_set_indent(bp, indent) <= 0)
58
0
            goto err;
59
12.7M
    }
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
12.7M
    p = str;
66
12.7M
    if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
67
64.3k
        BIO_snprintf(str, sizeof(str), "priv [ %d ] ", tag);
68
12.6M
    else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
69
803k
        BIO_snprintf(str, sizeof(str), "cont [ %d ]", tag);
70
11.8M
    else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
71
83.8k
        BIO_snprintf(str, sizeof(str), "appl [ %d ]", tag);
72
11.7M
    else if (tag > 30)
73
31.9k
        BIO_snprintf(str, sizeof(str), "<ASN1 %d>", tag);
74
11.7M
    else
75
11.7M
        p = ASN1_tag2str(tag);
76
77
12.7M
    i = (BIO_printf(bp, "%-18s", p) > 0);
78
12.7M
err:
79
12.7M
    if (saved_indent >= 0)
80
12.7M
        BIO_set_indent(bp, saved_indent);
81
12.7M
    if (pop_f_prefix)
82
12.7M
        BIO_pop(bp);
83
12.7M
    BIO_free(bio);
84
12.7M
    return i;
85
12.7M
}
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
790k
{
95
790k
    return asn1_parse2(bp, &pp, len, 0, 0, indent, dump);
96
790k
}
97
98
static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
99
    int offset, int depth, int indent, int dump)
100
5.45M
{
101
5.45M
    const unsigned char *p, *ep, *tot, *op, *opp;
102
5.45M
    long len;
103
5.45M
    int tag, xclass, ret = 0;
104
5.45M
    int nl, hl, j, r;
105
5.45M
    ASN1_OBJECT *o = NULL;
106
5.45M
    ASN1_OCTET_STRING *os = NULL;
107
5.45M
    ASN1_INTEGER *ai = NULL;
108
5.45M
    ASN1_ENUMERATED *ae = NULL;
109
    /* ASN1_BMPSTRING *bmp=NULL; */
110
5.45M
    int dump_indent, dump_cont = 0;
111
112
5.45M
    if (depth > ASN1_PARSE_MAXDEPTH) {
113
246
        BIO_puts(bp, "BAD RECURSION DEPTH\n");
114
246
        return 0;
115
246
    }
116
117
5.45M
    dump_indent = 6; /* Because we know BIO_dump_indent() */
118
5.45M
    p = *pp;
119
5.45M
    tot = p + length;
120
18.8M
    while (length > 0) {
121
16.8M
        op = p;
122
16.8M
        j = ASN1_get_object(&p, &len, &tag, &xclass, length);
123
16.8M
        if (j & 0x80) {
124
106k
            BIO_puts(bp, "Error in encoding\n");
125
106k
            goto end;
126
106k
        }
127
16.7M
        hl = (int)(p - op);
128
16.7M
        length -= hl;
129
        /*
130
         * if j == 0x21 it is a constructed indefinite length object
131
         */
132
16.7M
        if (!asn1_print_info(bp, (long)offset + (long)(op - *pp), depth,
133
16.7M
                hl, len, tag, xclass, j, (indent) ? depth : 0))
134
0
            goto end;
135
16.7M
        if (j & V_ASN1_CONSTRUCTED) {
136
5.91M
            const unsigned char *sp = p;
137
138
5.91M
            ep = p + len;
139
5.91M
            if (BIO_write(bp, "\n", 1) <= 0)
140
0
                goto end;
141
5.91M
            if (len > length) {
142
0
                BIO_printf(bp, "length is greater than %ld\n", length);
143
0
                goto end;
144
0
            }
145
5.91M
            if ((j == 0x21) && (len == 0)) {
146
2.92M
                for (;;) {
147
2.92M
                    r = asn1_parse2(bp, &p, (long)(tot - p),
148
2.92M
                        offset + (int)(p - *pp), depth + 1,
149
2.92M
                        indent, dump);
150
2.92M
                    if (r == 0)
151
147k
                        goto end;
152
2.77M
                    if ((r == 2) || (p >= tot)) {
153
2.77M
                        len = (long)(p - sp);
154
2.77M
                        break;
155
2.77M
                    }
156
2.77M
                }
157
2.98M
            } else {
158
2.98M
                long tmp = len;
159
160
4.60M
                while (p < ep) {
161
1.74M
                    sp = p;
162
1.74M
                    r = asn1_parse2(bp, &p, tmp,
163
1.74M
                        offset + (int)(p - *pp), depth + 1,
164
1.74M
                        indent, dump);
165
1.74M
                    if (r == 0)
166
127k
                        goto end;
167
1.61M
                    tmp -= (long)(p - sp);
168
1.61M
                }
169
2.98M
            }
170
10.7M
        } else if (xclass != 0) {
171
438k
            p += len;
172
438k
            if (BIO_write(bp, "\n", 1) <= 0)
173
0
                goto end;
174
10.3M
        } else {
175
10.3M
            nl = 0;
176
10.3M
            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
898k
                if (BIO_write(bp, ":", 1) <= 0)
178
0
                    goto end;
179
898k
                if ((len > 0) && BIO_write(bp, (const char *)p, (int)len) != (int)len)
180
0
                    goto end;
181
9.45M
            } else if (tag == V_ASN1_OBJECT) {
182
2.62M
                opp = op;
183
2.62M
                if (d2i_ASN1_OBJECT(&o, &opp, len + hl) != NULL) {
184
2.32M
                    if (BIO_write(bp, ":", 1) <= 0)
185
0
                        goto end;
186
2.32M
                    i2a_ASN1_OBJECT(bp, o);
187
2.32M
                } else {
188
297k
                    if (BIO_puts(bp, ":BAD OBJECT") <= 0)
189
0
                        goto end;
190
297k
                    dump_cont = 1;
191
297k
                }
192
6.82M
            } else if (tag == V_ASN1_BOOLEAN) {
193
318k
                if (len != 1) {
194
116k
                    if (BIO_puts(bp, ":BAD BOOLEAN") <= 0)
195
0
                        goto end;
196
116k
                    dump_cont = 1;
197
116k
                }
198
318k
                if (len > 0)
199
260k
                    BIO_printf(bp, ":%u", p[0]);
200
6.51M
            } else if (tag == V_ASN1_BMPSTRING) {
201
                /* do the BMP thang */
202
6.48M
            } else if (tag == V_ASN1_OCTET_STRING) {
203
948k
                int i, printable = 1;
204
205
948k
                opp = op;
206
948k
                os = d2i_ASN1_OCTET_STRING(NULL, &opp, len + hl);
207
948k
                if (os != NULL && os->length > 0) {
208
701k
                    opp = os->data;
209
                    /*
210
                     * testing whether the octet string is printable
211
                     */
212
1.57M
                    for (i = 0; i < os->length; i++) {
213
1.52M
                        if (((opp[i] < ' ') && (opp[i] != '\n') && (opp[i] != '\r') && (opp[i] != '\t')) || (opp[i] > '~')) {
214
646k
                            printable = 0;
215
646k
                            break;
216
646k
                        }
217
1.52M
                    }
218
701k
                    if (printable)
219
                    /* printable string */
220
54.9k
                    {
221
54.9k
                        if (BIO_write(bp, ":", 1) <= 0)
222
0
                            goto end;
223
54.9k
                        if (BIO_write(bp, (const char *)opp, os->length) <= 0)
224
0
                            goto end;
225
646k
                    } else if (!dump)
226
                    /*
227
                     * not printable => print octet string as hex dump
228
                     */
229
588k
                    {
230
588k
                        if (BIO_write(bp, "[HEX DUMP]:", 11) <= 0)
231
0
                            goto end;
232
24.0M
                        for (i = 0; i < os->length; i++) {
233
23.4M
                            if (BIO_printf(bp, "%02X", opp[i]) <= 0)
234
0
                                goto end;
235
23.4M
                        }
236
588k
                    } else
237
                    /* print the normal dump */
238
57.4k
                    {
239
57.4k
                        if (!nl) {
240
57.4k
                            if (BIO_write(bp, "\n", 1) <= 0)
241
0
                                goto end;
242
57.4k
                        }
243
57.4k
                        if (BIO_dump_indent(bp,
244
57.4k
                                (const char *)opp,
245
57.4k
                                ((dump == -1 || dump > os->length) ? os->length : dump),
246
57.4k
                                dump_indent)
247
57.4k
                            <= 0)
248
0
                            goto end;
249
57.4k
                        nl = 1;
250
57.4k
                    }
251
701k
                }
252
948k
                ASN1_OCTET_STRING_free(os);
253
948k
                os = NULL;
254
5.53M
            } else if (tag == V_ASN1_INTEGER) {
255
1.24M
                int i;
256
257
1.24M
                opp = op;
258
1.24M
                ai = d2i_ASN1_INTEGER(NULL, &opp, len + hl);
259
1.24M
                if (ai != NULL) {
260
1.04M
                    if (BIO_write(bp, ":", 1) <= 0)
261
0
                        goto end;
262
1.04M
                    if (ai->type == V_ASN1_NEG_INTEGER)
263
258k
                        if (BIO_write(bp, "-", 1) <= 0)
264
0
                            goto end;
265
38.6M
                    for (i = 0; i < ai->length; i++) {
266
37.6M
                        if (BIO_printf(bp, "%02X", ai->data[i]) <= 0)
267
0
                            goto end;
268
37.6M
                    }
269
1.04M
                    if (ai->length == 0) {
270
0
                        if (BIO_write(bp, "00", 2) <= 0)
271
0
                            goto end;
272
0
                    }
273
1.04M
                } else {
274
193k
                    if (BIO_puts(bp, ":BAD INTEGER") <= 0)
275
0
                        goto end;
276
193k
                    dump_cont = 1;
277
193k
                }
278
1.24M
                ASN1_INTEGER_free(ai);
279
1.24M
                ai = NULL;
280
4.29M
            } else if (tag == V_ASN1_ENUMERATED) {
281
159k
                int i;
282
283
159k
                opp = op;
284
159k
                ae = d2i_ASN1_ENUMERATED(NULL, &opp, len + hl);
285
159k
                if (ae != NULL) {
286
89.9k
                    if (BIO_write(bp, ":", 1) <= 0)
287
0
                        goto end;
288
89.9k
                    if (ae->type == V_ASN1_NEG_ENUMERATED)
289
13.7k
                        if (BIO_write(bp, "-", 1) <= 0)
290
0
                            goto end;
291
7.38M
                    for (i = 0; i < ae->length; i++) {
292
7.29M
                        if (BIO_printf(bp, "%02X", ae->data[i]) <= 0)
293
0
                            goto end;
294
7.29M
                    }
295
89.9k
                    if (ae->length == 0) {
296
0
                        if (BIO_write(bp, "00", 2) <= 0)
297
0
                            goto end;
298
0
                    }
299
89.9k
                } else {
300
70.0k
                    if (BIO_puts(bp, ":BAD ENUMERATED") <= 0)
301
0
                        goto end;
302
70.0k
                    dump_cont = 1;
303
70.0k
                }
304
159k
                ASN1_ENUMERATED_free(ae);
305
159k
                ae = NULL;
306
4.13M
            } else if (len > 0 && dump) {
307
106k
                if (!nl) {
308
106k
                    if (BIO_write(bp, "\n", 1) <= 0)
309
0
                        goto end;
310
106k
                }
311
106k
                if (BIO_dump_indent(bp, (const char *)p,
312
106k
                        ((dump == -1 || dump > len) ? len : dump),
313
106k
                        dump_indent)
314
106k
                    <= 0)
315
0
                    goto end;
316
106k
                nl = 1;
317
106k
            }
318
10.3M
            if (dump_cont) {
319
678k
                int i;
320
678k
                const unsigned char *tmp = op + hl;
321
678k
                if (BIO_puts(bp, ":[") <= 0)
322
0
                    goto end;
323
31.6M
                for (i = 0; i < len; i++) {
324
30.9M
                    if (BIO_printf(bp, "%02X", tmp[i]) <= 0)
325
0
                        goto end;
326
30.9M
                }
327
678k
                if (BIO_puts(bp, "]") <= 0)
328
0
                    goto end;
329
678k
                dump_cont = 0;
330
678k
            }
331
332
10.3M
            if (!nl) {
333
10.1M
                if (BIO_write(bp, "\n", 1) <= 0)
334
0
                    goto end;
335
10.1M
            }
336
10.3M
            p += len;
337
10.3M
            if ((tag == V_ASN1_EOC) && (xclass == 0)) {
338
3.00M
                ret = 2; /* End of sequence */
339
3.00M
                goto end;
340
3.00M
            }
341
10.3M
        }
342
13.4M
        length -= len;
343
13.4M
    }
344
2.07M
    ret = 1;
345
5.45M
end:
346
5.45M
    ASN1_OBJECT_free(o);
347
5.45M
    ASN1_OCTET_STRING_free(os);
348
5.45M
    ASN1_INTEGER_free(ai);
349
5.45M
    ASN1_ENUMERATED_free(ae);
350
5.45M
    *pp = p;
351
5.45M
    return ret;
352
2.07M
}
353
354
const char *ASN1_tag2str(int tag)
355
16.0M
{
356
16.0M
    static const char *const tag2str[] = {
357
        /* 0-4 */
358
16.0M
        "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
359
        /* 5-9 */
360
16.0M
        "NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL",
361
        /* 10-13 */
362
16.0M
        "ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>",
363
        /* 14-17 */
364
16.0M
        "<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET",
365
        /* 18-20 */
366
16.0M
        "NUMERICSTRING", "PRINTABLESTRING", "T61STRING",
367
        /* 21-24 */
368
16.0M
        "VIDEOTEXSTRING", "IA5STRING", "UTCTIME", "GENERALIZEDTIME",
369
        /* 25-27 */
370
16.0M
        "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING",
371
        /* 28-30 */
372
16.0M
        "UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING"
373
16.0M
    };
374
375
16.0M
    if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
376
3.05k
        tag &= ~0x100;
377
378
16.0M
    if (tag < 0 || tag > 30)
379
84.0k
        return "(unknown)";
380
15.9M
    return tag2str[tag];
381
16.0M
}