Coverage Report

Created: 2026-07-12 07:21

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