Coverage Report

Created: 2026-05-24 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/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
4.77M
#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
13.9M
{
25
13.9M
    char str[128];
26
13.9M
    const char *p;
27
13.9M
    int pop_f_prefix = 0;
28
13.9M
    long saved_indent = -1;
29
13.9M
    int i = 0;
30
13.9M
    BIO *bio = NULL;
31
32
13.9M
    if (constructed & V_ASN1_CONSTRUCTED)
33
5.00M
        p = "cons: ";
34
8.96M
    else
35
8.96M
        p = "prim: ";
36
13.9M
    if (constructed != (V_ASN1_CONSTRUCTED | 1)) {
37
11.6M
        if (BIO_snprintf(str, sizeof(str), "%5ld:d=%-2d hl=%ld l=%4ld %s",
38
11.6M
                offset, depth, (long)hl, len, p)
39
11.6M
            <= 0)
40
0
            goto err;
41
11.6M
    } else {
42
2.29M
        if (BIO_snprintf(str, sizeof(str), "%5ld:d=%-2d hl=%ld l=inf  %s",
43
2.29M
                offset, depth, (long)hl, p)
44
2.29M
            <= 0)
45
0
            goto err;
46
2.29M
    }
47
13.9M
    if (bp != NULL) {
48
13.9M
        if (BIO_set_prefix(bp, str) <= 0) {
49
13.9M
            if ((bio = BIO_new(BIO_f_prefix())) == NULL
50
13.9M
                || (bp = BIO_push(bio, bp)) == NULL)
51
0
                goto err;
52
13.9M
            pop_f_prefix = 1;
53
13.9M
        }
54
13.9M
        saved_indent = BIO_get_indent(bp);
55
13.9M
        if (BIO_set_prefix(bp, str) <= 0 || BIO_set_indent(bp, indent) <= 0)
56
0
            goto err;
57
13.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
13.9M
    p = str;
64
13.9M
    if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
65
86.7k
        BIO_snprintf(str, sizeof(str), "priv [ %d ] ", tag);
66
13.8M
    else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
67
906k
        BIO_snprintf(str, sizeof(str), "cont [ %d ]", tag);
68
12.9M
    else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
69
131k
        BIO_snprintf(str, sizeof(str), "appl [ %d ]", tag);
70
12.8M
    else if (tag > 30)
71
25.1k
        BIO_snprintf(str, sizeof(str), "<ASN1 %d>", tag);
72
12.8M
    else
73
12.8M
        p = ASN1_tag2str(tag);
74
75
13.9M
    i = (BIO_printf(bp, "%-18s", p) > 0);
76
13.9M
err:
77
13.9M
    if (saved_indent >= 0)
78
13.9M
        BIO_set_indent(bp, saved_indent);
79
13.9M
    if (pop_f_prefix)
80
13.9M
        BIO_pop(bp);
81
13.9M
    BIO_free(bio);
82
13.9M
    return i;
83
13.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
745k
{
93
745k
    return asn1_parse2(bp, &pp, len, 0, 0, indent, dump);
94
745k
}
95
96
static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
97
    int offset, int depth, int indent, int dump)
98
4.77M
{
99
4.77M
    const unsigned char *p, *ep, *tot, *op, *opp;
100
4.77M
    long len;
101
4.77M
    int tag, xclass, ret = 0;
102
4.77M
    int nl, hl, j, r;
103
4.77M
    ASN1_OBJECT *o = NULL;
104
4.77M
    ASN1_OCTET_STRING *os = NULL;
105
4.77M
    ASN1_INTEGER *ai = NULL;
106
4.77M
    ASN1_ENUMERATED *ae = NULL;
107
    /* ASN1_BMPSTRING *bmp=NULL; */
108
4.77M
    int dump_indent, dump_cont = 0;
109
110
4.77M
    if (depth > ASN1_PARSE_MAXDEPTH) {
111
242
        BIO_puts(bp, "BAD RECURSION DEPTH\n");
112
242
        return 0;
113
242
    }
114
115
4.77M
    dump_indent = 6; /* Because we know BIO_dump_indent() */
116
4.77M
    p = *pp;
117
4.77M
    tot = p + length;
118
15.9M
    while (length > 0) {
119
14.0M
        op = p;
120
14.0M
        j = ASN1_get_object(&p, &len, &tag, &xclass, length);
121
14.0M
        if (j & 0x80) {
122
76.6k
            BIO_puts(bp, "Error in encoding\n");
123
76.6k
            goto end;
124
76.6k
        }
125
13.9M
        hl = (p - op);
126
13.9M
        length -= hl;
127
        /*
128
         * if j == 0x21 it is a constructed indefinite length object
129
         */
130
13.9M
        if (!asn1_print_info(bp, (long)offset + (long)(op - *pp), depth,
131
13.9M
                hl, len, tag, xclass, j, (indent) ? depth : 0))
132
0
            goto end;
133
13.9M
        if (j & V_ASN1_CONSTRUCTED) {
134
5.00M
            const unsigned char *sp = p;
135
136
5.00M
            ep = p + len;
137
5.00M
            if (BIO_write(bp, "\n", 1) <= 0)
138
0
                goto end;
139
5.00M
            if (len > length) {
140
0
                BIO_printf(bp, "length is greater than %ld\n", length);
141
0
                goto end;
142
0
            }
143
5.00M
            if ((j == 0x21) && (len == 0)) {
144
2.29M
                for (;;) {
145
2.29M
                    r = asn1_parse2(bp, &p, (long)(tot - p),
146
2.29M
                        offset + (p - *pp), depth + 1,
147
2.29M
                        indent, dump);
148
2.29M
                    if (r == 0)
149
131k
                        goto end;
150
2.16M
                    if ((r == 2) || (p >= tot)) {
151
2.16M
                        len = p - sp;
152
2.16M
                        break;
153
2.16M
                    }
154
2.16M
                }
155
2.71M
            } else {
156
2.71M
                long tmp = len;
157
158
4.35M
                while (p < ep) {
159
1.73M
                    sp = p;
160
1.73M
                    r = asn1_parse2(bp, &p, tmp,
161
1.73M
                        offset + (p - *pp), depth + 1,
162
1.73M
                        indent, dump);
163
1.73M
                    if (r == 0)
164
92.5k
                        goto end;
165
1.64M
                    tmp -= p - sp;
166
1.64M
                }
167
2.71M
            }
168
8.96M
        } else if (xclass != 0) {
169
348k
            p += len;
170
348k
            if (BIO_write(bp, "\n", 1) <= 0)
171
0
                goto end;
172
8.61M
        } else {
173
8.61M
            nl = 0;
174
8.61M
            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
572k
                if (BIO_write(bp, ":", 1) <= 0)
176
0
                    goto end;
177
572k
                if ((len > 0) && BIO_write(bp, (const char *)p, (int)len) != (int)len)
178
0
                    goto end;
179
8.04M
            } 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.95M
                    if (BIO_write(bp, ":", 1) <= 0)
183
0
                        goto end;
184
1.95M
                    i2a_ASN1_OBJECT(bp, o);
185
1.95M
                } else {
186
170k
                    if (BIO_puts(bp, ":BAD OBJECT") <= 0)
187
0
                        goto end;
188
170k
                    dump_cont = 1;
189
170k
                }
190
5.91M
            } else if (tag == V_ASN1_BOOLEAN) {
191
244k
                if (len != 1) {
192
84.9k
                    if (BIO_puts(bp, ":BAD BOOLEAN") <= 0)
193
0
                        goto end;
194
84.9k
                    dump_cont = 1;
195
84.9k
                }
196
244k
                if (len > 0)
197
192k
                    BIO_printf(bp, ":%u", p[0]);
198
5.67M
            } else if (tag == V_ASN1_BMPSTRING) {
199
                /* do the BMP thang */
200
5.59M
            } else if (tag == V_ASN1_OCTET_STRING) {
201
999k
                int i, printable = 1;
202
203
999k
                opp = op;
204
999k
                os = d2i_ASN1_OCTET_STRING(NULL, &opp, len + hl);
205
999k
                if (os != NULL && os->length > 0) {
206
730k
                    opp = os->data;
207
                    /*
208
                     * testing whether the octet string is printable
209
                     */
210
1.59M
                    for (i = 0; i < os->length; i++) {
211
1.56M
                        if (((opp[i] < ' ') && (opp[i] != '\n') && (opp[i] != '\r') && (opp[i] != '\t')) || (opp[i] > '~')) {
212
692k
                            printable = 0;
213
692k
                            break;
214
692k
                        }
215
1.56M
                    }
216
730k
                    if (printable)
217
                    /* printable string */
218
37.8k
                    {
219
37.8k
                        if (BIO_write(bp, ":", 1) <= 0)
220
0
                            goto end;
221
37.8k
                        if (BIO_write(bp, (const char *)opp, os->length) <= 0)
222
0
                            goto end;
223
692k
                    } else if (!dump)
224
                    /*
225
                     * not printable => print octet string as hex dump
226
                     */
227
641k
                    {
228
641k
                        if (BIO_write(bp, "[HEX DUMP]:", 11) <= 0)
229
0
                            goto end;
230
23.9M
                        for (i = 0; i < os->length; i++) {
231
23.3M
                            if (BIO_printf(bp, "%02X", opp[i]) <= 0)
232
0
                                goto end;
233
23.3M
                        }
234
641k
                    } else
235
                    /* print the normal dump */
236
50.2k
                    {
237
50.2k
                        if (!nl) {
238
50.2k
                            if (BIO_write(bp, "\n", 1) <= 0)
239
0
                                goto end;
240
50.2k
                        }
241
50.2k
                        if (BIO_dump_indent(bp,
242
50.2k
                                (const char *)opp,
243
50.2k
                                ((dump == -1 || dump > os->length) ? os->length : dump),
244
50.2k
                                dump_indent)
245
50.2k
                            <= 0)
246
0
                            goto end;
247
50.2k
                        nl = 1;
248
50.2k
                    }
249
730k
                }
250
999k
                ASN1_OCTET_STRING_free(os);
251
999k
                os = NULL;
252
4.60M
            } else if (tag == V_ASN1_INTEGER) {
253
1.18M
                int i;
254
255
1.18M
                opp = op;
256
1.18M
                ai = d2i_ASN1_INTEGER(NULL, &opp, len + hl);
257
1.18M
                if (ai != NULL) {
258
960k
                    if (BIO_write(bp, ":", 1) <= 0)
259
0
                        goto end;
260
960k
                    if (ai->type == V_ASN1_NEG_INTEGER)
261
284k
                        if (BIO_write(bp, "-", 1) <= 0)
262
0
                            goto end;
263
43.7M
                    for (i = 0; i < ai->length; i++) {
264
42.8M
                        if (BIO_printf(bp, "%02X", ai->data[i]) <= 0)
265
0
                            goto end;
266
42.8M
                    }
267
960k
                    if (ai->length == 0) {
268
0
                        if (BIO_write(bp, "00", 2) <= 0)
269
0
                            goto end;
270
0
                    }
271
960k
                } else {
272
225k
                    if (BIO_puts(bp, ":BAD INTEGER") <= 0)
273
0
                        goto end;
274
225k
                    dump_cont = 1;
275
225k
                }
276
1.18M
                ASN1_INTEGER_free(ai);
277
1.18M
                ai = NULL;
278
3.41M
            } else if (tag == V_ASN1_ENUMERATED) {
279
132k
                int i;
280
281
132k
                opp = op;
282
132k
                ae = d2i_ASN1_ENUMERATED(NULL, &opp, len + hl);
283
132k
                if (ae != NULL) {
284
80.3k
                    if (BIO_write(bp, ":", 1) <= 0)
285
0
                        goto end;
286
80.3k
                    if (ae->type == V_ASN1_NEG_ENUMERATED)
287
19.8k
                        if (BIO_write(bp, "-", 1) <= 0)
288
0
                            goto end;
289
8.97M
                    for (i = 0; i < ae->length; i++) {
290
8.89M
                        if (BIO_printf(bp, "%02X", ae->data[i]) <= 0)
291
0
                            goto end;
292
8.89M
                    }
293
80.3k
                    if (ae->length == 0) {
294
0
                        if (BIO_write(bp, "00", 2) <= 0)
295
0
                            goto end;
296
0
                    }
297
80.3k
                } else {
298
52.2k
                    if (BIO_puts(bp, ":BAD ENUMERATED") <= 0)
299
0
                        goto end;
300
52.2k
                    dump_cont = 1;
301
52.2k
                }
302
132k
                ASN1_ENUMERATED_free(ae);
303
132k
                ae = NULL;
304
3.28M
            } else if (len > 0 && dump) {
305
73.2k
                if (!nl) {
306
73.2k
                    if (BIO_write(bp, "\n", 1) <= 0)
307
0
                        goto end;
308
73.2k
                }
309
73.2k
                if (BIO_dump_indent(bp, (const char *)p,
310
73.2k
                        ((dump == -1 || dump > len) ? len : dump),
311
73.2k
                        dump_indent)
312
73.2k
                    <= 0)
313
0
                    goto end;
314
73.2k
                nl = 1;
315
73.2k
            }
316
8.61M
            if (dump_cont) {
317
533k
                int i;
318
533k
                const unsigned char *tmp = op + hl;
319
533k
                if (BIO_puts(bp, ":[") <= 0)
320
0
                    goto end;
321
29.7M
                for (i = 0; i < len; i++) {
322
29.2M
                    if (BIO_printf(bp, "%02X", tmp[i]) <= 0)
323
0
                        goto end;
324
29.2M
                }
325
533k
                if (BIO_puts(bp, "]") <= 0)
326
0
                    goto end;
327
533k
                dump_cont = 0;
328
533k
            }
329
330
8.61M
            if (!nl) {
331
8.49M
                if (BIO_write(bp, "\n", 1) <= 0)
332
0
                    goto end;
333
8.49M
            }
334
8.61M
            p += len;
335
8.61M
            if ((tag == V_ASN1_EOC) && (xclass == 0)) {
336
2.52M
                ret = 2; /* End of sequence */
337
2.52M
                goto end;
338
2.52M
            }
339
8.61M
        }
340
11.2M
        length -= len;
341
11.2M
    }
342
1.94M
    ret = 1;
343
4.77M
end:
344
4.77M
    ASN1_OBJECT_free(o);
345
4.77M
    ASN1_OCTET_STRING_free(os);
346
4.77M
    ASN1_INTEGER_free(ai);
347
4.77M
    ASN1_ENUMERATED_free(ae);
348
4.77M
    *pp = p;
349
4.77M
    return ret;
350
1.94M
}
351
352
const char *ASN1_tag2str(int tag)
353
13.7M
{
354
13.7M
    static const char *const tag2str[] = {
355
        /* 0-4 */
356
13.7M
        "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
357
        /* 5-9 */
358
13.7M
        "NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL",
359
        /* 10-13 */
360
13.7M
        "ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>",
361
        /* 15-17 */
362
13.7M
        "<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET",
363
        /* 18-20 */
364
13.7M
        "NUMERICSTRING", "PRINTABLESTRING", "T61STRING",
365
        /* 21-24 */
366
13.7M
        "VIDEOTEXSTRING", "IA5STRING", "UTCTIME", "GENERALIZEDTIME",
367
        /* 25-27 */
368
13.7M
        "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING",
369
        /* 28-30 */
370
13.7M
        "UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING"
371
13.7M
    };
372
373
13.7M
    if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
374
5.23k
        tag &= ~0x100;
375
376
13.7M
    if (tag < 0 || tag > 30)
377
102k
        return "(unknown)";
378
13.6M
    return tag2str[tag];
379
13.7M
}