Coverage Report

Created: 2025-12-10 06:24

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