Coverage Report

Created: 2023-06-08 06:40

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