Coverage Report

Created: 2026-03-09 06:55

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