Coverage Report

Created: 2025-08-28 07:07

/src/openssl33/crypto/asn1/asn1_parse.c
Line
Count
Source (jump to first uncovered line)
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
3.22M
#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
9.49M
{
25
9.49M
    char str[128];
26
9.49M
    const char *p;
27
9.49M
    int pop_f_prefix = 0;
28
9.49M
    long saved_indent = -1;
29
9.49M
    int i = 0;
30
9.49M
    BIO *bio = NULL;
31
32
9.49M
    if (constructed & V_ASN1_CONSTRUCTED)
33
3.34M
        p = "cons: ";
34
6.14M
    else
35
6.14M
        p = "prim: ";
36
9.49M
    if (constructed != (V_ASN1_CONSTRUCTED | 1)) {
37
7.96M
        if (BIO_snprintf(str, sizeof(str), "%5ld:d=%-2d hl=%ld l=%4ld %s",
38
7.96M
                         offset, depth, (long)hl, len, p) <= 0)
39
0
            goto err;
40
7.96M
    } else {
41
1.53M
        if (BIO_snprintf(str, sizeof(str), "%5ld:d=%-2d hl=%ld l=inf  %s",
42
1.53M
                         offset, depth, (long)hl, p) <= 0)
43
0
            goto err;
44
1.53M
    }
45
9.49M
    if (bp != NULL) {
46
9.49M
        if (BIO_set_prefix(bp, str) <= 0) {
47
9.49M
            if ((bio = BIO_new(BIO_f_prefix())) == NULL
48
9.49M
                    || (bp = BIO_push(bio, bp)) == NULL)
49
0
                goto err;
50
9.49M
            pop_f_prefix = 1;
51
9.49M
        }
52
9.49M
        saved_indent = BIO_get_indent(bp);
53
9.49M
        if (BIO_set_prefix(bp, str) <= 0 || BIO_set_indent(bp, indent) <= 0)
54
0
            goto err;
55
9.49M
    }
56
57
    /*
58
     * BIO_set_prefix made a copy of |str|, so we can safely use it for
59
     * something else, ASN.1 tag printout.
60
     */
61
9.49M
    p = str;
62
9.49M
    if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
63
43.6k
        BIO_snprintf(str, sizeof(str), "priv [ %d ] ", tag);
64
9.45M
    else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
65
642k
        BIO_snprintf(str, sizeof(str), "cont [ %d ]", tag);
66
8.80M
    else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
67
95.5k
        BIO_snprintf(str, sizeof(str), "appl [ %d ]", tag);
68
8.71M
    else if (tag > 30)
69
27.3k
        BIO_snprintf(str, sizeof(str), "<ASN1 %d>", tag);
70
8.68M
    else
71
8.68M
        p = ASN1_tag2str(tag);
72
73
9.49M
    i = (BIO_printf(bp, "%-18s", p) > 0);
74
9.49M
 err:
75
9.49M
    if (saved_indent >= 0)
76
9.49M
        BIO_set_indent(bp, saved_indent);
77
9.49M
    if (pop_f_prefix)
78
9.49M
        BIO_pop(bp);
79
9.49M
    BIO_free(bio);
80
9.49M
    return i;
81
9.49M
}
82
83
int ASN1_parse(BIO *bp, const unsigned char *pp, long len, int indent)
84
0
{
85
0
    return asn1_parse2(bp, &pp, len, 0, 0, indent, 0);
86
0
}
87
88
int ASN1_parse_dump(BIO *bp, const unsigned char *pp, long len, int indent,
89
                    int dump)
90
501k
{
91
501k
    return asn1_parse2(bp, &pp, len, 0, 0, indent, dump);
92
501k
}
93
94
static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
95
                       int offset, int depth, int indent, int dump)
96
3.22M
{
97
3.22M
    const unsigned char *p, *ep, *tot, *op, *opp;
98
3.22M
    long len;
99
3.22M
    int tag, xclass, ret = 0;
100
3.22M
    int nl, hl, j, r;
101
3.22M
    ASN1_OBJECT *o = NULL;
102
3.22M
    ASN1_OCTET_STRING *os = NULL;
103
3.22M
    ASN1_INTEGER *ai = NULL;
104
3.22M
    ASN1_ENUMERATED *ae = NULL;
105
    /* ASN1_BMPSTRING *bmp=NULL; */
106
3.22M
    int dump_indent, dump_cont = 0;
107
108
3.22M
    if (depth > ASN1_PARSE_MAXDEPTH) {
109
213
        BIO_puts(bp, "BAD RECURSION DEPTH\n");
110
213
        return 0;
111
213
    }
112
113
3.22M
    dump_indent = 6;            /* Because we know BIO_dump_indent() */
114
3.22M
    p = *pp;
115
3.22M
    tot = p + length;
116
10.7M
    while (length > 0) {
117
9.56M
        op = p;
118
9.56M
        j = ASN1_get_object(&p, &len, &tag, &xclass, length);
119
9.56M
        if (j & 0x80) {
120
70.6k
            BIO_puts(bp, "Error in encoding\n");
121
70.6k
            goto end;
122
70.6k
        }
123
9.49M
        hl = (p - op);
124
9.49M
        length -= hl;
125
        /*
126
         * if j == 0x21 it is a constructed indefinite length object
127
         */
128
9.49M
        if (!asn1_print_info(bp, (long)offset + (long)(op - *pp), depth,
129
9.49M
                             hl, len, tag, xclass, j, (indent) ? depth : 0))
130
0
            goto end;
131
9.49M
        if (j & V_ASN1_CONSTRUCTED) {
132
3.34M
            const unsigned char *sp = p;
133
134
3.34M
            ep = p + len;
135
3.34M
            if (BIO_write(bp, "\n", 1) <= 0)
136
0
                goto end;
137
3.34M
            if (len > length) {
138
0
                BIO_printf(bp, "length is greater than %ld\n", length);
139
0
                goto end;
140
0
            }
141
3.34M
            if ((j == 0x21) && (len == 0)) {
142
1.53M
                for (;;) {
143
1.53M
                    r = asn1_parse2(bp, &p, (long)(tot - p),
144
1.53M
                                    offset + (p - *pp), depth + 1,
145
1.53M
                                    indent, dump);
146
1.53M
                    if (r == 0)
147
126k
                        goto end;
148
1.40M
                    if ((r == 2) || (p >= tot)) {
149
1.40M
                        len = p - sp;
150
1.40M
                        break;
151
1.40M
                    }
152
1.40M
                }
153
1.81M
            } else {
154
1.81M
                long tmp = len;
155
156
2.91M
                while (p < ep) {
157
1.18M
                    sp = p;
158
1.18M
                    r = asn1_parse2(bp, &p, tmp,
159
1.18M
                                    offset + (p - *pp), depth + 1,
160
1.18M
                                    indent, dump);
161
1.18M
                    if (r == 0)
162
84.7k
                        goto end;
163
1.10M
                    tmp -= p - sp;
164
1.10M
                }
165
1.81M
            }
166
6.14M
        } else if (xclass != 0) {
167
202k
            p += len;
168
202k
            if (BIO_write(bp, "\n", 1) <= 0)
169
0
                goto end;
170
5.94M
        } else {
171
5.94M
            nl = 0;
172
5.94M
            if ((tag == V_ASN1_PRINTABLESTRING) ||
173
5.94M
                (tag == V_ASN1_T61STRING) ||
174
5.94M
                (tag == V_ASN1_IA5STRING) ||
175
5.94M
                (tag == V_ASN1_VISIBLESTRING) ||
176
5.94M
                (tag == V_ASN1_NUMERICSTRING) ||
177
5.94M
                (tag == V_ASN1_UTF8STRING) ||
178
5.94M
                (tag == V_ASN1_UTCTIME) || (tag == V_ASN1_GENERALIZEDTIME)) {
179
281k
                if (BIO_write(bp, ":", 1) <= 0)
180
0
                    goto end;
181
281k
                if ((len > 0) && BIO_write(bp, (const char *)p, (int)len)
182
175k
                    != (int)len)
183
0
                    goto end;
184
5.66M
            } else if (tag == V_ASN1_OBJECT) {
185
1.42M
                opp = op;
186
1.42M
                if (d2i_ASN1_OBJECT(&o, &opp, len + hl) != NULL) {
187
1.31M
                    if (BIO_write(bp, ":", 1) <= 0)
188
0
                        goto end;
189
1.31M
                    i2a_ASN1_OBJECT(bp, o);
190
1.31M
                } else {
191
105k
                    if (BIO_puts(bp, ":BAD OBJECT") <= 0)
192
0
                        goto end;
193
105k
                    dump_cont = 1;
194
105k
                }
195
4.24M
            } else if (tag == V_ASN1_BOOLEAN) {
196
219k
                if (len != 1) {
197
75.7k
                    if (BIO_puts(bp, ":BAD BOOLEAN") <= 0)
198
0
                        goto end;
199
75.7k
                    dump_cont = 1;
200
75.7k
                }
201
219k
                if (len > 0)
202
171k
                    BIO_printf(bp, ":%u", p[0]);
203
4.02M
            } else if (tag == V_ASN1_BMPSTRING) {
204
                /* do the BMP thang */
205
3.96M
            } else if (tag == V_ASN1_OCTET_STRING) {
206
705k
                int i, printable = 1;
207
208
705k
                opp = op;
209
705k
                os = d2i_ASN1_OCTET_STRING(NULL, &opp, len + hl);
210
705k
                if (os != NULL && os->length > 0) {
211
498k
                    opp = os->data;
212
                    /*
213
                     * testing whether the octet string is printable
214
                     */
215
1.61M
                    for (i = 0; i < os->length; i++) {
216
1.59M
                        if (((opp[i] < ' ') &&
217
1.59M
                             (opp[i] != '\n') &&
218
1.59M
                             (opp[i] != '\r') &&
219
1.59M
                             (opp[i] != '\t')) || (opp[i] > '~')) {
220
474k
                            printable = 0;
221
474k
                            break;
222
474k
                        }
223
1.59M
                    }
224
498k
                    if (printable)
225
                        /* printable string */
226
24.2k
                    {
227
24.2k
                        if (BIO_write(bp, ":", 1) <= 0)
228
0
                            goto end;
229
24.2k
                        if (BIO_write(bp, (const char *)opp, os->length) <= 0)
230
0
                            goto end;
231
474k
                    } else if (!dump)
232
                        /*
233
                         * not printable => print octet string as hex dump
234
                         */
235
458k
                    {
236
458k
                        if (BIO_write(bp, "[HEX DUMP]:", 11) <= 0)
237
0
                            goto end;
238
19.4M
                        for (i = 0; i < os->length; i++) {
239
18.9M
                            if (BIO_printf(bp, "%02X", opp[i]) <= 0)
240
0
                                goto end;
241
18.9M
                        }
242
458k
                    } else
243
                        /* print the normal dump */
244
15.7k
                    {
245
15.7k
                        if (!nl) {
246
15.7k
                            if (BIO_write(bp, "\n", 1) <= 0)
247
0
                                goto end;
248
15.7k
                        }
249
15.7k
                        if (BIO_dump_indent(bp,
250
15.7k
                                            (const char *)opp,
251
15.7k
                                            ((dump == -1 || dump >
252
15.7k
                                              os->
253
15.7k
                                              length) ? os->length : dump),
254
15.7k
                                            dump_indent) <= 0)
255
0
                            goto end;
256
15.7k
                        nl = 1;
257
15.7k
                    }
258
498k
                }
259
705k
                ASN1_OCTET_STRING_free(os);
260
705k
                os = NULL;
261
3.26M
            } else if (tag == V_ASN1_INTEGER) {
262
1.06M
                int i;
263
264
1.06M
                opp = op;
265
1.06M
                ai = d2i_ASN1_INTEGER(NULL, &opp, len + hl);
266
1.06M
                if (ai != NULL) {
267
836k
                    if (BIO_write(bp, ":", 1) <= 0)
268
0
                        goto end;
269
836k
                    if (ai->type == V_ASN1_NEG_INTEGER)
270
240k
                        if (BIO_write(bp, "-", 1) <= 0)
271
0
                            goto end;
272
30.1M
                    for (i = 0; i < ai->length; i++) {
273
29.3M
                        if (BIO_printf(bp, "%02X", ai->data[i]) <= 0)
274
0
                            goto end;
275
29.3M
                    }
276
836k
                    if (ai->length == 0) {
277
0
                        if (BIO_write(bp, "00", 2) <= 0)
278
0
                            goto end;
279
0
                    }
280
836k
                } else {
281
224k
                    if (BIO_puts(bp, ":BAD INTEGER") <= 0)
282
0
                        goto end;
283
224k
                    dump_cont = 1;
284
224k
                }
285
1.06M
                ASN1_INTEGER_free(ai);
286
1.06M
                ai = NULL;
287
2.20M
            } else if (tag == V_ASN1_ENUMERATED) {
288
78.6k
                int i;
289
290
78.6k
                opp = op;
291
78.6k
                ae = d2i_ASN1_ENUMERATED(NULL, &opp, len + hl);
292
78.6k
                if (ae != NULL) {
293
53.9k
                    if (BIO_write(bp, ":", 1) <= 0)
294
0
                        goto end;
295
53.9k
                    if (ae->type == V_ASN1_NEG_ENUMERATED)
296
16.8k
                        if (BIO_write(bp, "-", 1) <= 0)
297
0
                            goto end;
298
7.55M
                    for (i = 0; i < ae->length; i++) {
299
7.50M
                        if (BIO_printf(bp, "%02X", ae->data[i]) <= 0)
300
0
                            goto end;
301
7.50M
                    }
302
53.9k
                    if (ae->length == 0) {
303
0
                        if (BIO_write(bp, "00", 2) <= 0)
304
0
                            goto end;
305
0
                    }
306
53.9k
                } else {
307
24.6k
                    if (BIO_puts(bp, ":BAD ENUMERATED") <= 0)
308
0
                        goto end;
309
24.6k
                    dump_cont = 1;
310
24.6k
                }
311
78.6k
                ASN1_ENUMERATED_free(ae);
312
78.6k
                ae = NULL;
313
2.12M
            } else if (len > 0 && dump) {
314
40.3k
                if (!nl) {
315
40.3k
                    if (BIO_write(bp, "\n", 1) <= 0)
316
0
                        goto end;
317
40.3k
                }
318
40.3k
                if (BIO_dump_indent(bp, (const char *)p,
319
40.3k
                                    ((dump == -1 || dump > len) ? len : dump),
320
40.3k
                                    dump_indent) <= 0)
321
0
                    goto end;
322
40.3k
                nl = 1;
323
40.3k
            }
324
5.94M
            if (dump_cont) {
325
429k
                int i;
326
429k
                const unsigned char *tmp = op + hl;
327
429k
                if (BIO_puts(bp, ":[") <= 0)
328
0
                    goto end;
329
31.2M
                for (i = 0; i < len; i++) {
330
30.8M
                    if (BIO_printf(bp, "%02X", tmp[i]) <= 0)
331
0
                        goto end;
332
30.8M
                }
333
429k
                if (BIO_puts(bp, "]") <= 0)
334
0
                    goto end;
335
429k
                dump_cont = 0;
336
429k
            }
337
338
5.94M
            if (!nl) {
339
5.88M
                if (BIO_write(bp, "\n", 1) <= 0)
340
0
                    goto end;
341
5.88M
            }
342
5.94M
            p += len;
343
5.94M
            if ((tag == V_ASN1_EOC) && (xclass == 0)) {
344
1.72M
                ret = 2;        /* End of sequence */
345
1.72M
                goto end;
346
1.72M
            }
347
5.94M
        }
348
7.55M
        length -= len;
349
7.55M
    }
350
1.21M
    ret = 1;
351
3.22M
 end:
352
3.22M
    ASN1_OBJECT_free(o);
353
3.22M
    ASN1_OCTET_STRING_free(os);
354
3.22M
    ASN1_INTEGER_free(ai);
355
3.22M
    ASN1_ENUMERATED_free(ae);
356
3.22M
    *pp = p;
357
3.22M
    return ret;
358
1.21M
}
359
360
const char *ASN1_tag2str(int tag)
361
9.44M
{
362
9.44M
    static const char *const tag2str[] = {
363
        /* 0-4 */
364
9.44M
        "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
365
        /* 5-9 */
366
9.44M
        "NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL",
367
        /* 10-13 */
368
9.44M
        "ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>",
369
        /* 15-17 */
370
9.44M
        "<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET",
371
        /* 18-20 */
372
9.44M
        "NUMERICSTRING", "PRINTABLESTRING", "T61STRING",
373
        /* 21-24 */
374
9.44M
        "VIDEOTEXSTRING", "IA5STRING", "UTCTIME", "GENERALIZEDTIME",
375
        /* 25-27 */
376
9.44M
        "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING",
377
        /* 28-30 */
378
9.44M
        "UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING"
379
9.44M
    };
380
381
9.44M
    if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
382
3.23k
        tag &= ~0x100;
383
384
9.44M
    if (tag < 0 || tag > 30)
385
74.2k
        return "(unknown)";
386
9.37M
    return tag2str[tag];
387
9.44M
}