Coverage Report

Created: 2025-12-31 06:58

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