Coverage Report

Created: 2023-09-25 06:45

/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
924k
#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
4.56M
{
27
4.56M
    static const char fmt[] = "%-18s";
28
4.56M
    char str[128];
29
4.56M
    const char *p;
30
31
4.56M
    if (constructed & V_ASN1_CONSTRUCTED)
32
2.13M
        p = "cons: ";
33
2.43M
    else
34
2.43M
        p = "prim: ";
35
4.56M
    if (BIO_write(bp, p, 6) < 6)
36
0
        goto err;
37
4.56M
    BIO_indent(bp, indent, 128);
38
39
4.56M
    p = str;
40
4.56M
    if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
41
2.80k
        BIO_snprintf(str, sizeof(str), "priv [ %d ] ", tag);
42
4.56M
    else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
43
1.44M
        BIO_snprintf(str, sizeof(str), "cont [ %d ]", tag);
44
3.12M
    else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
45
349k
        BIO_snprintf(str, sizeof(str), "appl [ %d ]", tag);
46
2.77M
    else if (tag > 30)
47
2.69k
        BIO_snprintf(str, sizeof(str), "<ASN1 %d>", tag);
48
2.76M
    else
49
2.76M
        p = ASN1_tag2str(tag);
50
51
4.56M
    if (BIO_printf(bp, fmt, p) <= 0)
52
0
        goto err;
53
4.56M
    return 1;
54
0
 err:
55
0
    return 0;
56
4.56M
}
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
736k
{
66
736k
    return asn1_parse2(bp, &pp, len, 0, 0, indent, dump);
67
736k
}
68
69
static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
70
                       int offset, int depth, int indent, int dump)
71
924k
{
72
924k
    const unsigned char *p, *ep, *tot, *op, *opp;
73
924k
    long len;
74
924k
    int tag, xclass, ret = 0;
75
924k
    int nl, hl, j, r;
76
924k
    ASN1_OBJECT *o = NULL;
77
924k
    ASN1_OCTET_STRING *os = NULL;
78
924k
    ASN1_INTEGER *ai = NULL;
79
924k
    ASN1_ENUMERATED *ae = NULL;
80
    /* ASN1_BMPSTRING *bmp=NULL; */
81
924k
    int dump_indent, dump_cont = 0;
82
83
924k
    if (depth > ASN1_PARSE_MAXDEPTH) {
84
29
        BIO_puts(bp, "BAD RECURSION DEPTH\n");
85
29
        return 0;
86
29
    }
87
88
924k
    dump_indent = 6;            /* Because we know BIO_dump_indent() */
89
924k
    p = *pp;
90
924k
    tot = p + length;
91
5.02M
    while (length > 0) {
92
4.56M
        op = p;
93
4.56M
        j = ASN1_get_object(&p, &len, &tag, &xclass, length);
94
4.56M
        if (j & 0x80) {
95
2.74k
            if (BIO_write(bp, "Error in encoding\n", 18) <= 0)
96
0
                goto end;
97
2.74k
            ret = 0;
98
2.74k
            goto end;
99
2.74k
        }
100
4.56M
        hl = (p - op);
101
4.56M
        length -= hl;
102
        /*
103
         * if j == 0x21 it is a constructed indefinite length object
104
         */
105
4.56M
        if (BIO_printf(bp, "%5ld:", (long)offset + (long)(op - *pp))
106
4.56M
            <= 0)
107
0
            goto end;
108
109
4.56M
        if (j != (V_ASN1_CONSTRUCTED | 1)) {
110
4.10M
            if (BIO_printf(bp, "d=%-2d hl=%ld l=%4ld ",
111
4.10M
                           depth, (long)hl, len) <= 0)
112
0
                goto end;
113
4.10M
        } else {
114
463k
            if (BIO_printf(bp, "d=%-2d hl=%ld l=inf  ", depth, (long)hl) <= 0)
115
0
                goto end;
116
463k
        }
117
4.56M
        if (!asn1_print_info(bp, tag, xclass, j, (indent) ? depth : 0))
118
0
            goto end;
119
4.56M
        if (j & V_ASN1_CONSTRUCTED) {
120
2.13M
            const unsigned char *sp = p;
121
122
2.13M
            ep = p + len;
123
2.13M
            if (BIO_write(bp, "\n", 1) <= 0)
124
0
                goto end;
125
2.13M
            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
2.13M
            if ((j == 0x21) && (len == 0)) {
131
463k
                for (;;) {
132
463k
                    r = asn1_parse2(bp, &p, (long)(tot - p),
133
463k
                                    offset + (p - *pp), depth + 1,
134
463k
                                    indent, dump);
135
463k
                    if (r == 0) {
136
16.4k
                        ret = 0;
137
16.4k
                        goto end;
138
16.4k
                    }
139
447k
                    if ((r == 2) || (p >= tot)) {
140
447k
                        len = p - sp;
141
447k
                        break;
142
447k
                    }
143
447k
                }
144
1.66M
            } else {
145
1.66M
                long tmp = len;
146
147
1.81M
                while (p < ep) {
148
150k
                    sp = p;
149
150k
                    r = asn1_parse2(bp, &p, tmp,
150
150k
                                    offset + (p - *pp), depth + 1,
151
150k
                                    indent, dump);
152
150k
                    if (r == 0) {
153
3.28k
                        ret = 0;
154
3.28k
                        goto end;
155
3.28k
                    }
156
147k
                    tmp -= p - sp;
157
147k
                }
158
1.66M
            }
159
2.43M
        } else if (xclass != 0) {
160
405k
            p += len;
161
405k
            if (BIO_write(bp, "\n", 1) <= 0)
162
0
                goto end;
163
2.02M
        } else {
164
2.02M
            nl = 0;
165
2.02M
            if ((tag == V_ASN1_PRINTABLESTRING) ||
166
2.02M
                (tag == V_ASN1_T61STRING) ||
167
2.02M
                (tag == V_ASN1_IA5STRING) ||
168
2.02M
                (tag == V_ASN1_VISIBLESTRING) ||
169
2.02M
                (tag == V_ASN1_NUMERICSTRING) ||
170
2.02M
                (tag == V_ASN1_UTF8STRING) ||
171
2.02M
                (tag == V_ASN1_UTCTIME) || (tag == V_ASN1_GENERALIZEDTIME)) {
172
99.7k
                if (BIO_write(bp, ":", 1) <= 0)
173
0
                    goto end;
174
99.7k
                if ((len > 0) && BIO_write(bp, (const char *)p, (int)len)
175
78.8k
                    != (int)len)
176
0
                    goto end;
177
1.92M
            } else if (tag == V_ASN1_OBJECT) {
178
432k
                opp = op;
179
432k
                if (d2i_ASN1_OBJECT(&o, &opp, len + hl) != NULL) {
180
414k
                    if (BIO_write(bp, ":", 1) <= 0)
181
0
                        goto end;
182
414k
                    i2a_ASN1_OBJECT(bp, o);
183
414k
                } else {
184
18.7k
                    if (BIO_puts(bp, ":BAD OBJECT") <= 0)
185
0
                        goto end;
186
18.7k
                    dump_cont = 1;
187
18.7k
                }
188
1.49M
            } else if (tag == V_ASN1_BOOLEAN) {
189
33.6k
                if (len != 1) {
190
13.9k
                    if (BIO_puts(bp, ":BAD BOOLEAN") <= 0)
191
0
                        goto end;
192
13.9k
                    dump_cont = 1;
193
13.9k
                }
194
33.6k
                if (len > 0)
195
32.5k
                    BIO_printf(bp, ":%u", p[0]);
196
1.46M
            } else if (tag == V_ASN1_BMPSTRING) {
197
                /* do the BMP thang */
198
1.45M
            } else if (tag == V_ASN1_OCTET_STRING) {
199
138k
                int i, printable = 1;
200
201
138k
                opp = op;
202
138k
                os = d2i_ASN1_OCTET_STRING(NULL, &opp, len + hl);
203
138k
                if (os != NULL && os->length > 0) {
204
118k
                    opp = os->data;
205
                    /*
206
                     * testing whether the octet string is printable
207
                     */
208
220k
                    for (i = 0; i < os->length; i++) {
209
214k
                        if (((opp[i] < ' ') &&
210
214k
                             (opp[i] != '\n') &&
211
214k
                             (opp[i] != '\r') &&
212
214k
                             (opp[i] != '\t')) || (opp[i] > '~')) {
213
112k
                            printable = 0;
214
112k
                            break;
215
112k
                        }
216
214k
                    }
217
118k
                    if (printable)
218
                        /* printable string */
219
5.31k
                    {
220
5.31k
                        if (BIO_write(bp, ":", 1) <= 0)
221
0
                            goto end;
222
5.31k
                        if (BIO_write(bp, (const char *)opp, os->length) <= 0)
223
0
                            goto end;
224
112k
                    } else if (!dump)
225
                        /*
226
                         * not printable => print octet string as hex dump
227
                         */
228
112k
                    {
229
112k
                        if (BIO_write(bp, "[HEX DUMP]:", 11) <= 0)
230
0
                            goto end;
231
7.85M
                        for (i = 0; i < os->length; i++) {
232
7.74M
                            if (BIO_printf(bp, "%02X", opp[i]) <= 0)
233
0
                                goto end;
234
7.74M
                        }
235
112k
                    } 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
118k
                }
252
138k
                ASN1_OCTET_STRING_free(os);
253
138k
                os = NULL;
254
1.32M
            } else if (tag == V_ASN1_INTEGER) {
255
292k
                int i;
256
257
292k
                opp = op;
258
292k
                ai = d2i_ASN1_INTEGER(NULL, &opp, len + hl);
259
292k
                if (ai != NULL) {
260
273k
                    if (BIO_write(bp, ":", 1) <= 0)
261
0
                        goto end;
262
273k
                    if (ai->type == V_ASN1_NEG_INTEGER)
263
81.5k
                        if (BIO_write(bp, "-", 1) <= 0)
264
0
                            goto end;
265
9.71M
                    for (i = 0; i < ai->length; i++) {
266
9.44M
                        if (BIO_printf(bp, "%02X", ai->data[i]) <= 0)
267
0
                            goto end;
268
9.44M
                    }
269
273k
                    if (ai->length == 0) {
270
0
                        if (BIO_write(bp, "00", 2) <= 0)
271
0
                            goto end;
272
0
                    }
273
273k
                } else {
274
19.7k
                    if (BIO_puts(bp, ":BAD INTEGER") <= 0)
275
0
                        goto end;
276
19.7k
                    dump_cont = 1;
277
19.7k
                }
278
292k
                ASN1_INTEGER_free(ai);
279
292k
                ai = NULL;
280
1.02M
            } else if (tag == V_ASN1_ENUMERATED) {
281
8.65k
                int i;
282
283
8.65k
                opp = op;
284
8.65k
                ae = d2i_ASN1_ENUMERATED(NULL, &opp, len + hl);
285
8.65k
                if (ae != NULL) {
286
6.19k
                    if (BIO_write(bp, ":", 1) <= 0)
287
0
                        goto end;
288
6.19k
                    if (ae->type == V_ASN1_NEG_ENUMERATED)
289
2.68k
                        if (BIO_write(bp, "-", 1) <= 0)
290
0
                            goto end;
291
1.24M
                    for (i = 0; i < ae->length; i++) {
292
1.23M
                        if (BIO_printf(bp, "%02X", ae->data[i]) <= 0)
293
0
                            goto end;
294
1.23M
                    }
295
6.19k
                    if (ae->length == 0) {
296
0
                        if (BIO_write(bp, "00", 2) <= 0)
297
0
                            goto end;
298
0
                    }
299
6.19k
                } else {
300
2.46k
                    if (BIO_puts(bp, ":BAD ENUMERATED") <= 0)
301
0
                        goto end;
302
2.46k
                    dump_cont = 1;
303
2.46k
                }
304
8.65k
                ASN1_ENUMERATED_free(ae);
305
8.65k
                ae = NULL;
306
1.01M
            } 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
2.02M
            if (dump_cont) {
318
54.9k
                int i;
319
54.9k
                const unsigned char *tmp = op + hl;
320
54.9k
                if (BIO_puts(bp, ":[") <= 0)
321
0
                    goto end;
322
27.2M
                for (i = 0; i < len; i++) {
323
27.2M
                    if (BIO_printf(bp, "%02X", tmp[i]) <= 0)
324
0
                        goto end;
325
27.2M
                }
326
54.9k
                if (BIO_puts(bp, "]") <= 0)
327
0
                    goto end;
328
54.9k
                dump_cont = 0;
329
54.9k
            }
330
331
2.02M
            if (!nl) {
332
2.02M
                if (BIO_write(bp, "\n", 1) <= 0)
333
0
                    goto end;
334
2.02M
            }
335
2.02M
            p += len;
336
2.02M
            if ((tag == V_ASN1_EOC) && (xclass == 0)) {
337
447k
                ret = 2;        /* End of sequence */
338
447k
                goto end;
339
447k
            }
340
2.02M
        }
341
4.09M
        length -= len;
342
4.09M
    }
343
454k
    ret = 1;
344
924k
 end:
345
924k
    ASN1_OBJECT_free(o);
346
924k
    ASN1_OCTET_STRING_free(os);
347
924k
    ASN1_INTEGER_free(ai);
348
924k
    ASN1_ENUMERATED_free(ae);
349
924k
    *pp = p;
350
924k
    return ret;
351
454k
}
352
353
const char *ASN1_tag2str(int tag)
354
18.9M
{
355
18.9M
    static const char *const tag2str[] = {
356
        /* 0-4 */
357
18.9M
        "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
358
        /* 5-9 */
359
18.9M
        "NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL",
360
        /* 10-13 */
361
18.9M
        "ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>",
362
        /* 15-17 */
363
18.9M
        "<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET",
364
        /* 18-20 */
365
18.9M
        "NUMERICSTRING", "PRINTABLESTRING", "T61STRING",
366
        /* 21-24 */
367
18.9M
        "VIDEOTEXSTRING", "IA5STRING", "UTCTIME", "GENERALIZEDTIME",
368
        /* 25-27 */
369
18.9M
        "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING",
370
        /* 28-30 */
371
18.9M
        "UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING"
372
18.9M
    };
373
374
18.9M
    if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
375
2.48k
        tag &= ~0x100;
376
377
18.9M
    if (tag < 0 || tag > 30)
378
230k
        return "(unknown)";
379
18.6M
    return tag2str[tag];
380
18.9M
}