Coverage Report

Created: 2023-06-08 06:41

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