Coverage Report

Created: 2023-06-08 06:40

/src/openssl111/crypto/asn1/asn1_par.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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
8.81k
#define ASN1_PARSE_MAXDEPTH 128
18
#endif
19
20
static int asn1_print_info(BIO *bp, int tag, int xclass, int constructed,
21
                           int indent);
22
static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
23
                       int offset, int depth, int indent, int dump);
24
static int asn1_print_info(BIO *bp, int tag, int xclass, int constructed,
25
                           int indent)
26
101k
{
27
101k
    static const char fmt[] = "%-18s";
28
101k
    char str[128];
29
101k
    const char *p;
30
31
101k
    if (constructed & V_ASN1_CONSTRUCTED)
32
17.9k
        p = "cons: ";
33
83.3k
    else
34
83.3k
        p = "prim: ";
35
101k
    if (BIO_write(bp, p, 6) < 6)
36
0
        goto err;
37
101k
    BIO_indent(bp, indent, 128);
38
39
101k
    p = str;
40
101k
    if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
41
1.24k
        BIO_snprintf(str, sizeof(str), "priv [ %d ] ", tag);
42
100k
    else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
43
2.74k
        BIO_snprintf(str, sizeof(str), "cont [ %d ]", tag);
44
97.3k
    else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
45
1.43k
        BIO_snprintf(str, sizeof(str), "appl [ %d ]", tag);
46
95.8k
    else if (tag > 30)
47
449
        BIO_snprintf(str, sizeof(str), "<ASN1 %d>", tag);
48
95.4k
    else
49
95.4k
        p = ASN1_tag2str(tag);
50
51
101k
    if (BIO_printf(bp, fmt, p) <= 0)
52
0
        goto err;
53
101k
    return 1;
54
0
 err:
55
0
    return 0;
56
101k
}
57
58
int ASN1_parse(BIO *bp, const unsigned char *pp, long len, int indent)
59
0
{
60
0
    return asn1_parse2(bp, &pp, len, 0, 0, indent, 0);
61
0
}
62
63
int ASN1_parse_dump(BIO *bp, const unsigned char *pp, long len, int indent,
64
                    int dump)
65
2.61k
{
66
2.61k
    return asn1_parse2(bp, &pp, len, 0, 0, indent, dump);
67
2.61k
}
68
69
static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
70
                       int offset, int depth, int indent, int dump)
71
8.81k
{
72
8.81k
    const unsigned char *p, *ep, *tot, *op, *opp;
73
8.81k
    long len;
74
8.81k
    int tag, xclass, ret = 0;
75
8.81k
    int nl, hl, j, r;
76
8.81k
    ASN1_OBJECT *o = NULL;
77
8.81k
    ASN1_OCTET_STRING *os = NULL;
78
8.81k
    ASN1_INTEGER *ai = NULL;
79
8.81k
    ASN1_ENUMERATED *ae = NULL;
80
    /* ASN1_BMPSTRING *bmp=NULL; */
81
8.81k
    int dump_indent, dump_cont = 0;
82
83
8.81k
    if (depth > ASN1_PARSE_MAXDEPTH) {
84
1
        BIO_puts(bp, "BAD RECURSION DEPTH\n");
85
1
        return 0;
86
1
    }
87
88
8.81k
    dump_indent = 6;            /* Because we know BIO_dump_indent() */
89
8.81k
    p = *pp;
90
8.81k
    tot = p + length;
91
107k
    while (length > 0) {
92
101k
        op = p;
93
101k
        j = ASN1_get_object(&p, &len, &tag, &xclass, length);
94
101k
        if (j & 0x80) {
95
482
            if (BIO_write(bp, "Error in encoding\n", 18) <= 0)
96
0
                goto end;
97
482
            ret = 0;
98
482
            goto end;
99
482
        }
100
101k
        hl = (p - op);
101
101k
        length -= hl;
102
        /*
103
         * if j == 0x21 it is a constructed indefinite length object
104
         */
105
101k
        if (BIO_printf(bp, "%5ld:", (long)offset + (long)(op - *pp))
106
101k
            <= 0)
107
0
            goto end;
108
109
101k
        if (j != (V_ASN1_CONSTRUCTED | 1)) {
110
96.4k
            if (BIO_printf(bp, "d=%-2d hl=%ld l=%4ld ",
111
96.4k
                           depth, (long)hl, len) <= 0)
112
0
                goto end;
113
96.4k
        } else {
114
4.87k
            if (BIO_printf(bp, "d=%-2d hl=%ld l=inf  ", depth, (long)hl) <= 0)
115
0
                goto end;
116
4.87k
        }
117
101k
        if (!asn1_print_info(bp, tag, xclass, j, (indent) ? depth : 0))
118
0
            goto end;
119
101k
        if (j & V_ASN1_CONSTRUCTED) {
120
17.9k
            const unsigned char *sp = p;
121
122
17.9k
            ep = p + len;
123
17.9k
            if (BIO_write(bp, "\n", 1) <= 0)
124
0
                goto end;
125
17.9k
            if (len > length) {
126
0
                BIO_printf(bp, "length is greater than %ld\n", length);
127
0
                ret = 0;
128
0
                goto end;
129
0
            }
130
17.9k
            if ((j == 0x21) && (len == 0)) {
131
4.87k
                for (;;) {
132
4.87k
                    r = asn1_parse2(bp, &p, (long)(tot - p),
133
4.87k
                                    offset + (p - *pp), depth + 1,
134
4.87k
                                    indent, dump);
135
4.87k
                    if (r == 0) {
136
799
                        ret = 0;
137
799
                        goto end;
138
799
                    }
139
4.07k
                    if ((r == 2) || (p >= tot)) {
140
4.07k
                        len = p - sp;
141
4.07k
                        break;
142
4.07k
                    }
143
4.07k
                }
144
13.0k
            } else {
145
13.0k
                long tmp = len;
146
147
14.3k
                while (p < ep) {
148
1.32k
                    sp = p;
149
1.32k
                    r = asn1_parse2(bp, &p, tmp,
150
1.32k
                                    offset + (p - *pp), depth + 1,
151
1.32k
                                    indent, dump);
152
1.32k
                    if (r == 0) {
153
101
                        ret = 0;
154
101
                        goto end;
155
101
                    }
156
1.22k
                    tmp -= p - sp;
157
1.22k
                }
158
13.0k
            }
159
83.3k
        } else if (xclass != 0) {
160
2.99k
            p += len;
161
2.99k
            if (BIO_write(bp, "\n", 1) <= 0)
162
0
                goto end;
163
80.3k
        } else {
164
80.3k
            nl = 0;
165
80.3k
            if ((tag == V_ASN1_PRINTABLESTRING) ||
166
80.3k
                (tag == V_ASN1_T61STRING) ||
167
80.3k
                (tag == V_ASN1_IA5STRING) ||
168
80.3k
                (tag == V_ASN1_VISIBLESTRING) ||
169
80.3k
                (tag == V_ASN1_NUMERICSTRING) ||
170
80.3k
                (tag == V_ASN1_UTF8STRING) ||
171
80.3k
                (tag == V_ASN1_UTCTIME) || (tag == V_ASN1_GENERALIZEDTIME)) {
172
2.57k
                if (BIO_write(bp, ":", 1) <= 0)
173
0
                    goto end;
174
2.57k
                if ((len > 0) && BIO_write(bp, (const char *)p, (int)len)
175
713
                    != (int)len)
176
0
                    goto end;
177
77.7k
            } else if (tag == V_ASN1_OBJECT) {
178
21.1k
                opp = op;
179
21.1k
                if (d2i_ASN1_OBJECT(&o, &opp, len + hl) != NULL) {
180
8.93k
                    if (BIO_write(bp, ":", 1) <= 0)
181
0
                        goto end;
182
8.93k
                    i2a_ASN1_OBJECT(bp, o);
183
12.2k
                } else {
184
12.2k
                    if (BIO_puts(bp, ":BAD OBJECT") <= 0)
185
0
                        goto end;
186
12.2k
                    dump_cont = 1;
187
12.2k
                }
188
56.6k
            } else if (tag == V_ASN1_BOOLEAN) {
189
5.41k
                if (len != 1) {
190
2.38k
                    if (BIO_puts(bp, ":BAD BOOLEAN") <= 0)
191
0
                        goto end;
192
2.38k
                    dump_cont = 1;
193
2.38k
                }
194
5.41k
                if (len > 0)
195
4.91k
                    BIO_printf(bp, ":%u", p[0]);
196
51.2k
            } else if (tag == V_ASN1_BMPSTRING) {
197
                /* do the BMP thang */
198
50.9k
            } else if (tag == V_ASN1_OCTET_STRING) {
199
17.4k
                int i, printable = 1;
200
201
17.4k
                opp = op;
202
17.4k
                os = d2i_ASN1_OCTET_STRING(NULL, &opp, len + hl);
203
17.4k
                if (os != NULL && os->length > 0) {
204
2.44k
                    opp = os->data;
205
                    /*
206
                     * testing whether the octet string is printable
207
                     */
208
5.63k
                    for (i = 0; i < os->length; i++) {
209
4.92k
                        if (((opp[i] < ' ') &&
210
4.92k
                             (opp[i] != '\n') &&
211
4.92k
                             (opp[i] != '\r') &&
212
4.92k
                             (opp[i] != '\t')) || (opp[i] > '~')) {
213
1.73k
                            printable = 0;
214
1.73k
                            break;
215
1.73k
                        }
216
4.92k
                    }
217
2.44k
                    if (printable)
218
                        /* printable string */
219
714
                    {
220
714
                        if (BIO_write(bp, ":", 1) <= 0)
221
0
                            goto end;
222
714
                        if (BIO_write(bp, (const char *)opp, os->length) <= 0)
223
0
                            goto end;
224
1.73k
                    } else if (!dump)
225
                        /*
226
                         * not printable => print octet string as hex dump
227
                         */
228
1.73k
                    {
229
1.73k
                        if (BIO_write(bp, "[HEX DUMP]:", 11) <= 0)
230
0
                            goto end;
231
1.12M
                        for (i = 0; i < os->length; i++) {
232
1.12M
                            if (BIO_printf(bp, "%02X", opp[i]) <= 0)
233
0
                                goto end;
234
1.12M
                        }
235
1.73k
                    } else
236
                        /* print the normal dump */
237
0
                    {
238
0
                        if (!nl) {
239
0
                            if (BIO_write(bp, "\n", 1) <= 0)
240
0
                                goto end;
241
0
                        }
242
0
                        if (BIO_dump_indent(bp,
243
0
                                            (const char *)opp,
244
0
                                            ((dump == -1 || dump >
245
0
                                              os->
246
0
                                              length) ? os->length : dump),
247
0
                                            dump_indent) <= 0)
248
0
                            goto end;
249
0
                        nl = 1;
250
0
                    }
251
2.44k
                }
252
17.4k
                ASN1_OCTET_STRING_free(os);
253
17.4k
                os = NULL;
254
33.4k
            } else if (tag == V_ASN1_INTEGER) {
255
29.2k
                int i;
256
257
29.2k
                opp = op;
258
29.2k
                ai = d2i_ASN1_INTEGER(NULL, &opp, len + hl);
259
29.2k
                if (ai != NULL) {
260
26.8k
                    if (BIO_write(bp, ":", 1) <= 0)
261
0
                        goto end;
262
26.8k
                    if (ai->type == V_ASN1_NEG_INTEGER)
263
23.8k
                        if (BIO_write(bp, "-", 1) <= 0)
264
0
                            goto end;
265
2.71M
                    for (i = 0; i < ai->length; i++) {
266
2.68M
                        if (BIO_printf(bp, "%02X", ai->data[i]) <= 0)
267
0
                            goto end;
268
2.68M
                    }
269
26.8k
                    if (ai->length == 0) {
270
0
                        if (BIO_write(bp, "00", 2) <= 0)
271
0
                            goto end;
272
0
                    }
273
26.8k
                } else {
274
2.42k
                    if (BIO_puts(bp, ":BAD INTEGER") <= 0)
275
0
                        goto end;
276
2.42k
                    dump_cont = 1;
277
2.42k
                }
278
29.2k
                ASN1_INTEGER_free(ai);
279
29.2k
                ai = NULL;
280
29.2k
            } else if (tag == V_ASN1_ENUMERATED) {
281
1.65k
                int i;
282
283
1.65k
                opp = op;
284
1.65k
                ae = d2i_ASN1_ENUMERATED(NULL, &opp, len + hl);
285
1.65k
                if (ae != NULL) {
286
853
                    if (BIO_write(bp, ":", 1) <= 0)
287
0
                        goto end;
288
853
                    if (ae->type == V_ASN1_NEG_ENUMERATED)
289
265
                        if (BIO_write(bp, "-", 1) <= 0)
290
0
                            goto end;
291
586k
                    for (i = 0; i < ae->length; i++) {
292
585k
                        if (BIO_printf(bp, "%02X", ae->data[i]) <= 0)
293
0
                            goto end;
294
585k
                    }
295
853
                    if (ae->length == 0) {
296
0
                        if (BIO_write(bp, "00", 2) <= 0)
297
0
                            goto end;
298
0
                    }
299
853
                } else {
300
797
                    if (BIO_puts(bp, ":BAD ENUMERATED") <= 0)
301
0
                        goto end;
302
797
                    dump_cont = 1;
303
797
                }
304
1.65k
                ASN1_ENUMERATED_free(ae);
305
1.65k
                ae = NULL;
306
2.57k
            } else if (len > 0 && dump) {
307
0
                if (!nl) {
308
0
                    if (BIO_write(bp, "\n", 1) <= 0)
309
0
                        goto end;
310
0
                }
311
0
                if (BIO_dump_indent(bp, (const char *)p,
312
0
                                    ((dump == -1 || dump > len) ? len : dump),
313
0
                                    dump_indent) <= 0)
314
0
                    goto end;
315
0
                nl = 1;
316
0
            }
317
80.3k
            if (dump_cont) {
318
17.8k
                int i;
319
17.8k
                const unsigned char *tmp = op + hl;
320
17.8k
                if (BIO_puts(bp, ":[") <= 0)
321
0
                    goto end;
322
3.95M
                for (i = 0; i < len; i++) {
323
3.93M
                    if (BIO_printf(bp, "%02X", tmp[i]) <= 0)
324
0
                        goto end;
325
3.93M
                }
326
17.8k
                if (BIO_puts(bp, "]") <= 0)
327
0
                    goto end;
328
17.8k
                dump_cont = 0;
329
17.8k
            }
330
331
80.3k
            if (!nl) {
332
80.3k
                if (BIO_write(bp, "\n", 1) <= 0)
333
0
                    goto end;
334
80.3k
            }
335
80.3k
            p += len;
336
80.3k
            if ((tag == V_ASN1_EOC) && (xclass == 0)) {
337
1.33k
                ret = 2;        /* End of sequence */
338
1.33k
                goto end;
339
1.33k
            }
340
80.3k
        }
341
99.0k
        length -= len;
342
99.0k
    }
343
6.10k
    ret = 1;
344
8.81k
 end:
345
8.81k
    ASN1_OBJECT_free(o);
346
8.81k
    ASN1_OCTET_STRING_free(os);
347
8.81k
    ASN1_INTEGER_free(ai);
348
8.81k
    ASN1_ENUMERATED_free(ae);
349
8.81k
    *pp = p;
350
8.81k
    return ret;
351
6.10k
}
352
353
const char *ASN1_tag2str(int tag)
354
95.4k
{
355
95.4k
    static const char *const tag2str[] = {
356
        /* 0-4 */
357
95.4k
        "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
358
        /* 5-9 */
359
95.4k
        "NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL",
360
        /* 10-13 */
361
95.4k
        "ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>",
362
        /* 15-17 */
363
95.4k
        "<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET",
364
        /* 18-20 */
365
95.4k
        "NUMERICSTRING", "PRINTABLESTRING", "T61STRING",
366
        /* 21-24 */
367
95.4k
        "VIDEOTEXSTRING", "IA5STRING", "UTCTIME", "GENERALIZEDTIME",
368
        /* 25-27 */
369
95.4k
        "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING",
370
        /* 28-30 */
371
95.4k
        "UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING"
372
95.4k
    };
373
374
95.4k
    if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
375
0
        tag &= ~0x100;
376
377
95.4k
    if (tag < 0 || tag > 30)
378
0
        return "(unknown)";
379
95.4k
    return tag2str[tag];
380
95.4k
}