Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/asn1/tasn_dec.c
Line
Count
Source
1
/*
2
 * Copyright 2000-2026 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 <stddef.h>
11
#include <string.h>
12
#include <openssl/asn1.h>
13
#include <openssl/asn1t.h>
14
#include <openssl/objects.h>
15
#include <openssl/buffer.h>
16
#include <openssl/err.h>
17
#include "crypto/asn1.h"
18
#include "internal/numbers.h"
19
#include "asn1_local.h"
20
21
/*
22
 * Constructed types with a recursive definition (such as can be found in PKCS7)
23
 * could eventually exceed the stack given malicious input with excessive
24
 * recursion. Therefore we limit the stack depth. This is the maximum number of
25
 * recursive invocations of asn1_item_embed_d2i().
26
 */
27
370M
#define ASN1_MAX_CONSTRUCTED_NEST 30
28
29
static int asn1_check_eoc(const unsigned char **in, long len);
30
static int asn1_find_end(const unsigned char **in, long len, char inf);
31
32
static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
33
    char inf, int tag, int aclass, int depth);
34
35
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
36
37
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
38
    char *inf, char *cst,
39
    const unsigned char **in, long len,
40
    int exptag, int expclass, char opt, ASN1_TLC *ctx);
41
42
static int asn1_template_ex_d2i(ASN1_VALUE **pval,
43
    const unsigned char **in, long len,
44
    const ASN1_TEMPLATE *tt, char opt,
45
    ASN1_TLC *ctx, int depth, OSSL_LIB_CTX *libctx,
46
    const char *propq);
47
static int asn1_template_noexp_d2i(ASN1_VALUE **val,
48
    const unsigned char **in, long len,
49
    const ASN1_TEMPLATE *tt, char opt,
50
    ASN1_TLC *ctx, int depth,
51
    OSSL_LIB_CTX *libctx, const char *propq);
52
static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
53
    const unsigned char **in, long len,
54
    const ASN1_ITEM *it,
55
    int tag, int aclass, char opt,
56
    ASN1_TLC *ctx);
57
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, long len,
58
    int utype, char *free_cont, const ASN1_ITEM *it);
59
60
/* Table to convert tags to bit values, used for MSTRING type */
61
static const unsigned long tag2bit[32] = {
62
    /* tags  0 -  3 */
63
    0,
64
    0,
65
    0,
66
    B_ASN1_BIT_STRING,
67
    /* tags  4- 7 */
68
    B_ASN1_OCTET_STRING,
69
    0,
70
    0,
71
    B_ASN1_UNKNOWN,
72
    /* tags  8-11 */
73
    B_ASN1_UNKNOWN,
74
    B_ASN1_UNKNOWN,
75
    0,
76
    B_ASN1_UNKNOWN,
77
    /* tags 12-15 */
78
    B_ASN1_UTF8STRING,
79
    B_ASN1_UNKNOWN,
80
    B_ASN1_UNKNOWN,
81
    B_ASN1_UNKNOWN,
82
    /* tags 16-19 */
83
    B_ASN1_SEQUENCE,
84
    0,
85
    B_ASN1_NUMERICSTRING,
86
    B_ASN1_PRINTABLESTRING,
87
    /* tags 20-22 */
88
    B_ASN1_T61STRING,
89
    B_ASN1_VIDEOTEXSTRING,
90
    B_ASN1_IA5STRING,
91
    /* tags 23-24 */
92
    B_ASN1_UTCTIME,
93
    B_ASN1_GENERALIZEDTIME,
94
    /* tags 25-27 */
95
    B_ASN1_GRAPHICSTRING,
96
    B_ASN1_ISO64STRING,
97
    B_ASN1_GENERALSTRING,
98
    /* tags 28-31 */
99
    B_ASN1_UNIVERSALSTRING,
100
    B_ASN1_UNKNOWN,
101
    B_ASN1_BMPSTRING,
102
    B_ASN1_UNKNOWN,
103
};
104
105
unsigned long ASN1_tag2bit(int tag)
106
35.2M
{
107
35.2M
    if ((tag < 0) || (tag > 30))
108
6.28k
        return 0;
109
35.2M
    return tag2bit[tag];
110
35.2M
}
111
112
/* Macro to initialize and invalidate the cache */
113
114
#define asn1_tlc_clear(c)   \
115
340M
    do {                    \
116
340M
        if ((c) != NULL)    \
117
340M
            (c)->valid = 0; \
118
340M
    } while (0)
119
/* Version to avoid compiler warning about 'c' always non-NULL */
120
#define asn1_tlc_clear_nc(c) \
121
23.3M
    do {                     \
122
23.3M
        (c)->valid = 0;      \
123
23.3M
    } while (0)
124
125
/*
126
 * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
127
 * function. 'in' points to a buffer to read the data from, in future we
128
 * will have more advanced versions that can input data a piece at a time and
129
 * this will simply be a special case.
130
 */
131
132
static int asn1_item_ex_d2i_intern(ASN1_VALUE **pval, const unsigned char **in,
133
    long len, const ASN1_ITEM *it, int tag,
134
    int aclass, char opt, ASN1_TLC *ctx,
135
    OSSL_LIB_CTX *libctx, const char *propq)
136
26.0M
{
137
26.0M
    int rv;
138
139
26.0M
    if (pval == NULL || it == NULL) {
140
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
141
0
        return 0;
142
0
    }
143
26.0M
    rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0,
144
26.0M
        libctx, propq);
145
26.0M
    if (rv <= 0)
146
16.1M
        ASN1_item_ex_free(pval, it);
147
26.0M
    return rv;
148
26.0M
}
149
150
int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
151
    const ASN1_ITEM *it,
152
    int tag, int aclass, char opt, ASN1_TLC *ctx)
153
2.66M
{
154
2.66M
    return asn1_item_ex_d2i_intern(pval, in, len, it, tag, aclass, opt, ctx,
155
2.66M
        NULL, NULL);
156
2.66M
}
157
158
ASN1_VALUE *ASN1_item_d2i_ex(ASN1_VALUE **pval,
159
    const unsigned char **in, long len,
160
    const ASN1_ITEM *it, OSSL_LIB_CTX *libctx,
161
    const char *propq)
162
23.3M
{
163
23.3M
    ASN1_TLC c;
164
23.3M
    ASN1_VALUE *ptmpval = NULL;
165
166
23.3M
    if (pval == NULL)
167
20.6M
        pval = &ptmpval;
168
23.3M
    asn1_tlc_clear_nc(&c);
169
23.3M
    if (asn1_item_ex_d2i_intern(pval, in, len, it, -1, 0, 0, &c, libctx,
170
23.3M
            propq)
171
23.3M
        > 0)
172
7.47M
        return *pval;
173
15.8M
    return NULL;
174
23.3M
}
175
176
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
177
    const unsigned char **in, long len,
178
    const ASN1_ITEM *it)
179
21.8M
{
180
21.8M
    return ASN1_item_d2i_ex(pval, in, len, it, NULL, NULL);
181
21.8M
}
182
183
/*
184
 * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
185
 * tag mismatch return -1 to handle OPTIONAL
186
 */
187
188
int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
189
    long len, const ASN1_ITEM *it,
190
    int tag, int aclass, char opt, ASN1_TLC *ctx,
191
    int depth, OSSL_LIB_CTX *libctx,
192
    const char *propq)
193
370M
{
194
370M
    const ASN1_TEMPLATE *tt, *errtt = NULL;
195
370M
    const ASN1_EXTERN_FUNCS *ef;
196
370M
    const ASN1_AUX *aux;
197
370M
    ASN1_aux_cb *asn1_cb;
198
370M
    const unsigned char *p = NULL, *q;
199
370M
    unsigned char oclass;
200
370M
    char seq_eoc, seq_nolen, cst, isopt;
201
370M
    long tmplen;
202
370M
    int i;
203
370M
    int otag;
204
370M
    int ret = 0;
205
370M
    ASN1_VALUE **pchptr;
206
207
370M
    if (pval == NULL || it == NULL) {
208
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
209
0
        return 0;
210
0
    }
211
370M
    if (len <= 0) {
212
67.1k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
213
67.1k
        return 0;
214
67.1k
    }
215
370M
    aux = it->funcs;
216
370M
    if (aux && aux->asn1_cb)
217
12.2M
        asn1_cb = aux->asn1_cb;
218
358M
    else
219
358M
        asn1_cb = 0;
220
221
370M
    if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
222
33
        ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_TOO_DEEP);
223
33
        goto err;
224
33
    }
225
226
370M
    switch (it->itype) {
227
284M
    case ASN1_ITYPE_PRIMITIVE:
228
284M
        if (it->templates) {
229
            /*
230
             * tagging or OPTIONAL is currently illegal on an item template
231
             * because the flags can't get passed down. In practice this
232
             * isn't a problem: we include the relevant flags from the item
233
             * template in the template itself.
234
             */
235
161M
            if ((tag != -1) || opt) {
236
0
                ERR_raise(ERR_LIB_ASN1,
237
0
                    ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
238
0
                goto err;
239
0
            }
240
161M
            return asn1_template_ex_d2i(pval, in, len, it->templates, opt, ctx,
241
161M
                depth, libctx, propq);
242
161M
        }
243
123M
        return asn1_d2i_ex_primitive(pval, in, len, it,
244
123M
            tag, aclass, opt, ctx);
245
246
18.1M
    case ASN1_ITYPE_MSTRING:
247
        /*
248
         * It never makes sense for multi-strings to have implicit tagging, so
249
         * if tag != -1, then this looks like an error in the template.
250
         */
251
18.1M
        if (tag != -1) {
252
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
253
0
            goto err;
254
0
        }
255
256
18.1M
        p = *in;
257
        /* Just read in tag and class */
258
18.1M
        ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
259
18.1M
            &p, len, -1, 0, 1, ctx);
260
18.1M
        if (!ret) {
261
25.9k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
262
25.9k
            goto err;
263
25.9k
        }
264
265
        /* Must be UNIVERSAL class */
266
18.1M
        if (oclass != V_ASN1_UNIVERSAL) {
267
            /* If OPTIONAL, assume this is OK */
268
135k
            if (opt)
269
103k
                return -1;
270
135k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
271
31.6k
            goto err;
272
135k
        }
273
274
        /* Check tag matches bit map */
275
18.0M
        if (!(ASN1_tag2bit(otag) & it->utype)) {
276
            /* If OPTIONAL, assume this is OK */
277
398k
            if (opt)
278
15.1k
                return -1;
279
398k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_WRONG_TAG);
280
383k
            goto err;
281
398k
        }
282
17.6M
        return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
283
284
5.29M
    case ASN1_ITYPE_EXTERN:
285
        /* Use new style d2i */
286
5.29M
        ef = it->funcs;
287
5.29M
        if (ef->asn1_ex_d2i_ex != NULL)
288
2.63M
            return ef->asn1_ex_d2i_ex(pval, in, len, it, tag, aclass, opt, ctx,
289
2.63M
                libctx, propq);
290
2.66M
        return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
291
292
5.76M
    case ASN1_ITYPE_CHOICE:
293
        /*
294
         * It never makes sense for CHOICE types to have implicit tagging, so
295
         * if tag != -1, then this looks like an error in the template.
296
         */
297
5.76M
        if (tag != -1) {
298
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
299
0
            goto err;
300
0
        }
301
302
5.76M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
303
0
            goto auxerr;
304
5.76M
        if (*pval) {
305
            /* Free up and zero CHOICE value if initialised */
306
891k
            i = ossl_asn1_get_choice_selector(pval, it);
307
891k
            if ((i >= 0) && (i < it->tcount)) {
308
0
                tt = it->templates + i;
309
0
                pchptr = ossl_asn1_get_field_ptr(pval, tt);
310
0
                ossl_asn1_template_free(pchptr, tt);
311
0
                ossl_asn1_set_choice_selector(pval, -1, it);
312
0
            }
313
4.87M
        } else if (!ossl_asn1_item_ex_new_intern(pval, it, libctx, propq)) {
314
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
315
0
            goto err;
316
0
        }
317
        /* CHOICE type, try each possibility in turn */
318
5.76M
        p = *in;
319
27.8M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
320
27.1M
            pchptr = ossl_asn1_get_field_ptr(pval, tt);
321
            /*
322
             * We mark field as OPTIONAL so its absence can be recognised.
323
             */
324
27.1M
            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth,
325
27.1M
                libctx, propq);
326
            /* If field not present, try the next one */
327
27.1M
            if (ret == -1)
328
22.0M
                continue;
329
            /* If positive return, read OK, break loop */
330
5.08M
            if (ret > 0)
331
4.11M
                break;
332
            /*
333
             * Must be an ASN1 parsing error.
334
             * Free up any partial choice value
335
             */
336
970k
            ossl_asn1_template_free(pchptr, tt);
337
970k
            errtt = tt;
338
970k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
339
970k
            goto err;
340
5.08M
        }
341
342
        /* Did we fall off the end without reading anything? */
343
4.79M
        if (i == it->tcount) {
344
            /* If OPTIONAL, this is OK */
345
681k
            if (opt) {
346
                /* Free and zero it */
347
26.4k
                ASN1_item_ex_free(pval, it);
348
26.4k
                return -1;
349
26.4k
            }
350
681k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
351
655k
            goto err;
352
681k
        }
353
354
4.11M
        ossl_asn1_set_choice_selector(pval, i, it);
355
356
4.11M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
357
0
            goto auxerr;
358
4.11M
        *in = p;
359
4.11M
        return 1;
360
361
877k
    case ASN1_ITYPE_NDEF_SEQUENCE:
362
57.4M
    case ASN1_ITYPE_SEQUENCE:
363
57.4M
        p = *in;
364
57.4M
        tmplen = len;
365
366
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
367
57.4M
        if (tag == -1) {
368
51.2M
            tag = V_ASN1_SEQUENCE;
369
51.2M
            aclass = V_ASN1_UNIVERSAL;
370
51.2M
        }
371
        /* Get SEQUENCE length and update len, p */
372
57.4M
        ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
373
57.4M
            &p, len, tag, aclass, opt, ctx);
374
57.4M
        if (!ret) {
375
4.55M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
376
4.55M
            goto err;
377
52.8M
        } else if (ret == -1)
378
6.31M
            return -1;
379
46.5M
        if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
380
0
            len = tmplen - (long)(p - *in);
381
0
            seq_nolen = 1;
382
0
        }
383
        /* If indefinite we don't do a length check */
384
46.5M
        else
385
46.5M
            seq_nolen = seq_eoc;
386
46.5M
        if (!cst) {
387
121k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
388
121k
            goto err;
389
121k
        }
390
391
46.4M
        if (*pval == NULL
392
34.3M
            && !ossl_asn1_item_ex_new_intern(pval, it, libctx, propq)) {
393
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
394
0
            goto err;
395
0
        }
396
397
46.4M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
398
0
            goto auxerr;
399
400
        /* Free up and zero any ADB found */
401
179M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
402
132M
            if (tt->flags & ASN1_TFLG_ADB_MASK) {
403
718k
                const ASN1_TEMPLATE *seqtt;
404
718k
                ASN1_VALUE **pseqval;
405
718k
                seqtt = ossl_asn1_do_adb(*pval, tt, 0);
406
718k
                if (seqtt == NULL)
407
551k
                    continue;
408
166k
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
409
166k
                ossl_asn1_template_free(pseqval, seqtt);
410
166k
            }
411
132M
        }
412
413
        /* Get each field entry */
414
135M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
415
104M
            const ASN1_TEMPLATE *seqtt;
416
104M
            ASN1_VALUE **pseqval;
417
104M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
418
104M
            if (seqtt == NULL)
419
0
                goto err;
420
104M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
421
            /* Have we ran out of data? */
422
104M
            if (!len)
423
2.61M
                break;
424
101M
            q = p;
425
101M
            if (asn1_check_eoc(&p, len)) {
426
3.48M
                if (!seq_eoc) {
427
26.5k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
428
26.5k
                    goto err;
429
26.5k
                }
430
3.46M
                len -= (long)(p - q);
431
3.46M
                seq_eoc = 0;
432
3.46M
                break;
433
3.48M
            }
434
            /*
435
             * This determines the OPTIONAL flag value. The field cannot be
436
             * omitted if it is the last of a SEQUENCE and there is still
437
             * data to be read. This isn't strictly necessary but it
438
             * increases efficiency in some cases.
439
             */
440
98.4M
            if (i == (it->tcount - 1))
441
32.4M
                isopt = 0;
442
65.9M
            else
443
65.9M
                isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
444
            /*
445
             * attempt to read in field, allowing each to be OPTIONAL
446
             */
447
448
98.4M
            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
449
98.4M
                depth, libctx, propq);
450
98.4M
            if (!ret) {
451
9.52M
                errtt = seqtt;
452
9.52M
                goto err;
453
88.8M
            } else if (ret == -1) {
454
                /*
455
                 * OPTIONAL component absent. Free and zero the field.
456
                 */
457
9.80M
                ossl_asn1_template_free(pseqval, seqtt);
458
9.80M
                continue;
459
9.80M
            }
460
            /* Update length */
461
79.0M
            len -= (long)(p - q);
462
79.0M
        }
463
464
        /* Check for EOC if expecting one */
465
36.8M
        if (seq_eoc && !asn1_check_eoc(&p, len)) {
466
174k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
467
174k
            goto err;
468
174k
        }
469
        /* Check all data read */
470
36.6M
        if (!seq_nolen && len) {
471
31.0k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
472
31.0k
            goto err;
473
31.0k
        }
474
475
        /*
476
         * If we get here we've got no more data in the SEQUENCE, however we
477
         * may not have read all fields so check all remaining are OPTIONAL
478
         * and clear any that are.
479
         */
480
49.4M
        for (; i < it->tcount; tt++, i++) {
481
13.1M
            const ASN1_TEMPLATE *seqtt;
482
13.1M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
483
13.1M
            if (seqtt == NULL)
484
0
                goto err;
485
13.1M
            if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
486
12.8M
                ASN1_VALUE **pseqval;
487
12.8M
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
488
12.8M
                ossl_asn1_template_free(pseqval, seqtt);
489
12.8M
            } else {
490
285k
                errtt = seqtt;
491
285k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_FIELD_MISSING);
492
285k
                goto err;
493
285k
            }
494
13.1M
        }
495
        /* Save encoding */
496
36.3M
        if (!ossl_asn1_enc_save(pval, *in, (long)(p - *in), it))
497
0
            goto auxerr;
498
36.3M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
499
36.4k
            goto auxerr;
500
36.3M
        *in = p;
501
36.3M
        return 1;
502
503
0
    default:
504
0
        return 0;
505
370M
    }
506
36.4k
auxerr:
507
36.4k
    ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
508
16.8M
err:
509
16.8M
    if (errtt)
510
10.7M
        ERR_add_error_data(4, "Field=", errtt->field_name,
511
10.7M
            ", Type=", it->sname);
512
6.03M
    else
513
6.03M
        ERR_add_error_data(2, "Type=", it->sname);
514
16.8M
    return 0;
515
36.4k
}
516
517
/*
518
 * Templates are handled with two separate functions. One handles any
519
 * EXPLICIT tag and the other handles the rest.
520
 */
521
522
static int asn1_template_ex_d2i(ASN1_VALUE **val,
523
    const unsigned char **in, long inlen,
524
    const ASN1_TEMPLATE *tt, char opt,
525
    ASN1_TLC *ctx, int depth,
526
    OSSL_LIB_CTX *libctx, const char *propq)
527
286M
{
528
286M
    int flags, aclass;
529
286M
    int ret;
530
286M
    long len;
531
286M
    const unsigned char *p, *q;
532
286M
    char exp_eoc;
533
286M
    if (!val)
534
0
        return 0;
535
286M
    flags = tt->flags;
536
286M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
537
538
286M
    p = *in;
539
540
    /* Check if EXPLICIT tag expected */
541
286M
    if (flags & ASN1_TFLG_EXPTAG) {
542
8.86M
        char cst;
543
        /*
544
         * Need to work out amount of data available to the inner content and
545
         * where it starts: so read in EXPLICIT header to get the info.
546
         */
547
8.86M
        ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
548
8.86M
            &p, inlen, tt->tag, aclass, opt, ctx);
549
8.86M
        q = p;
550
8.86M
        if (!ret) {
551
346k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
552
346k
            return 0;
553
8.52M
        } else if (ret == -1)
554
6.65M
            return -1;
555
1.87M
        if (!cst) {
556
6.78k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
557
6.78k
            return 0;
558
6.78k
        }
559
        /* We've found the field so it can't be OPTIONAL now */
560
1.86M
        ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth, libctx,
561
1.86M
            propq);
562
1.86M
        if (!ret) {
563
302k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
564
302k
            return 0;
565
302k
        }
566
        /* We read the field in OK so update length */
567
1.56M
        len -= (long)(p - q);
568
1.56M
        if (exp_eoc) {
569
            /* If NDEF we must have an EOC here */
570
458k
            if (!asn1_check_eoc(&p, len)) {
571
40.0k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
572
40.0k
                goto err;
573
40.0k
            }
574
1.10M
        } else {
575
            /*
576
             * Otherwise we must hit the EXPLICIT tag end or its an error
577
             */
578
1.10M
            if (len) {
579
3.86k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
580
3.86k
                goto err;
581
3.86k
            }
582
1.10M
        }
583
1.56M
    } else
584
277M
        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth,
585
277M
            libctx, propq);
586
587
1.51M
    *in = p;
588
1.51M
    return 1;
589
590
43.9k
err:
591
43.9k
    return 0;
592
286M
}
593
594
static int asn1_template_noexp_d2i(ASN1_VALUE **val,
595
    const unsigned char **in, long len,
596
    const ASN1_TEMPLATE *tt, char opt,
597
    ASN1_TLC *ctx, int depth,
598
    OSSL_LIB_CTX *libctx, const char *propq)
599
279M
{
600
279M
    int flags, aclass;
601
279M
    int ret;
602
279M
    ASN1_VALUE *tval;
603
279M
    const unsigned char *p, *q;
604
279M
    if (!val)
605
0
        return 0;
606
279M
    flags = tt->flags;
607
279M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
608
609
279M
    p = *in;
610
611
    /*
612
     * If field is embedded then val needs fixing so it is a pointer to
613
     * a pointer to a field.
614
     */
615
279M
    if (tt->flags & ASN1_TFLG_EMBED) {
616
12.3M
        tval = (ASN1_VALUE *)val;
617
12.3M
        val = &tval;
618
12.3M
    }
619
620
279M
    if (flags & ASN1_TFLG_SK_MASK) {
621
        /* SET OF, SEQUENCE OF */
622
164M
        int sktag, skaclass;
623
164M
        char sk_eoc;
624
        /* First work out expected inner tag value */
625
164M
        if (flags & ASN1_TFLG_IMPTAG) {
626
1.32M
            sktag = tt->tag;
627
1.32M
            skaclass = aclass;
628
163M
        } else {
629
163M
            skaclass = V_ASN1_UNIVERSAL;
630
163M
            if (flags & ASN1_TFLG_SET_OF)
631
156M
                sktag = V_ASN1_SET;
632
6.70M
            else
633
6.70M
                sktag = V_ASN1_SEQUENCE;
634
163M
        }
635
        /* Get the tag */
636
164M
        ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
637
164M
            &p, len, sktag, skaclass, opt, ctx);
638
164M
        if (!ret) {
639
1.32M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
640
1.32M
            return 0;
641
163M
        } else if (ret == -1)
642
775k
            return -1;
643
162M
        if (*val == NULL)
644
161M
            *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
645
798k
        else {
646
            /*
647
             * We've got a valid STACK: free up any items present
648
             */
649
798k
            STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
650
798k
            ASN1_VALUE *vtmp;
651
798k
            while (sk_ASN1_VALUE_num(sktmp) > 0) {
652
0
                vtmp = sk_ASN1_VALUE_pop(sktmp);
653
0
                ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
654
0
            }
655
798k
        }
656
657
162M
        if (*val == NULL) {
658
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
659
0
            goto err;
660
0
        }
661
662
        /* Read as many items as we can */
663
387M
        while (len > 0) {
664
231M
            ASN1_VALUE *skfield;
665
231M
            q = p;
666
            /* See if EOC found */
667
231M
            if (asn1_check_eoc(&p, len)) {
668
4.65M
                if (!sk_eoc) {
669
9.83k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
670
9.83k
                    goto err;
671
9.83k
                }
672
4.64M
                len -= (long)(p - q);
673
4.64M
                sk_eoc = 0;
674
4.64M
                break;
675
4.65M
            }
676
227M
            skfield = NULL;
677
227M
            if (asn1_item_embed_d2i(&skfield, &p, len,
678
227M
                    ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
679
227M
                    depth, libctx, propq)
680
227M
                <= 0) {
681
2.01M
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
682
                /* |skfield| may be partially allocated despite failure. */
683
2.01M
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
684
2.01M
                goto err;
685
2.01M
            }
686
225M
            len -= (long)(p - q);
687
225M
            if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
688
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
689
0
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
690
0
                goto err;
691
0
            }
692
225M
        }
693
160M
        if (sk_eoc) {
694
15.6k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
695
15.6k
            goto err;
696
15.6k
        }
697
160M
    } else if (flags & ASN1_TFLG_IMPTAG) {
698
        /* IMPLICIT tagging */
699
22.5M
        ret = asn1_item_embed_d2i(val, &p, len,
700
22.5M
            ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
701
22.5M
            ctx, depth, libctx, propq);
702
22.5M
        if (!ret) {
703
521k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
704
521k
            goto err;
705
21.9M
        } else if (ret == -1)
706
18.6M
            return -1;
707
92.3M
    } else {
708
        /* Nothing special */
709
92.3M
        ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
710
92.3M
            -1, 0, opt, ctx, depth, libctx, propq);
711
92.3M
        if (!ret) {
712
8.41M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
713
8.41M
            goto err;
714
83.9M
        } else if (ret == -1)
715
5.75M
            return -1;
716
92.3M
    }
717
718
242M
    *in = p;
719
242M
    return 1;
720
721
10.9M
err:
722
10.9M
    return 0;
723
279M
}
724
725
static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
726
    const unsigned char **in, long inlen,
727
    const ASN1_ITEM *it,
728
    int tag, int aclass, char opt, ASN1_TLC *ctx)
729
140M
{
730
140M
    int ret = 0, utype;
731
140M
    long plen;
732
140M
    char cst, inf, free_cont = 0;
733
140M
    const unsigned char *p;
734
140M
    BUF_MEM buf = { 0, NULL, 0, 0 };
735
140M
    const unsigned char *cont = NULL;
736
140M
    long len;
737
738
140M
    if (pval == NULL) {
739
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NULL);
740
0
        return 0; /* Should never happen */
741
0
    }
742
743
140M
    if (it->itype == ASN1_ITYPE_MSTRING) {
744
17.6M
        utype = tag;
745
17.6M
        tag = -1;
746
17.6M
    } else
747
123M
        utype = it->utype;
748
749
140M
    if (utype == V_ASN1_ANY) {
750
        /* If type is ANY need to figure out type from tag */
751
45.4M
        unsigned char oclass;
752
45.4M
        if (tag >= 0) {
753
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
754
0
            return 0;
755
0
        }
756
45.4M
        if (opt) {
757
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
758
0
            return 0;
759
0
        }
760
45.4M
        p = *in;
761
45.4M
        ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
762
45.4M
            &p, inlen, -1, 0, 0, ctx);
763
45.4M
        if (!ret) {
764
37.5k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
765
37.5k
            return 0;
766
37.5k
        }
767
45.4M
        if (oclass != V_ASN1_UNIVERSAL)
768
3.54M
            utype = V_ASN1_OTHER;
769
45.4M
    }
770
140M
    if (tag == -1) {
771
124M
        tag = utype;
772
124M
        aclass = V_ASN1_UNIVERSAL;
773
124M
    }
774
140M
    p = *in;
775
    /* Check header */
776
140M
    ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
777
140M
        &p, inlen, tag, aclass, opt, ctx);
778
140M
    if (!ret) {
779
6.58M
        ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
780
6.58M
        return 0;
781
133M
    } else if (ret == -1)
782
17.9M
        return -1;
783
116M
    ret = 0;
784
    /* SEQUENCE, SET and "OTHER" are left in encoded form */
785
116M
    if ((utype == V_ASN1_SEQUENCE)
786
111M
        || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
787
        /*
788
         * Clear context cache for type OTHER because the auto clear when we
789
         * have a exact match won't work
790
         */
791
22.9M
        if (utype == V_ASN1_OTHER) {
792
3.54M
            asn1_tlc_clear(ctx);
793
3.54M
        }
794
        /* SEQUENCE and SET must be constructed */
795
19.4M
        else if (!cst) {
796
13.7k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
797
13.7k
            return 0;
798
13.7k
        }
799
800
22.9M
        cont = *in;
801
        /* If indefinite length constructed find the real end */
802
22.9M
        if (inf) {
803
1.31M
            if (!asn1_find_end(&p, plen, inf))
804
103k
                goto err;
805
1.21M
            len = (long)(p - cont);
806
21.6M
        } else {
807
21.6M
            len = (long)(p - cont) + plen;
808
21.6M
            p += plen;
809
21.6M
        }
810
93.0M
    } else if (cst) {
811
13.1M
        if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
812
13.1M
            || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
813
13.1M
            || utype == V_ASN1_ENUMERATED) {
814
25.2k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
815
25.2k
            return 0;
816
25.2k
        }
817
818
        /* Free any returned 'buf' content */
819
13.0M
        free_cont = 1;
820
        /*
821
         * Should really check the internal tags are correct but some things
822
         * may get this wrong. The relevant specs say that constructed string
823
         * types should be OCTET STRINGs internally irrespective of the type.
824
         * So instead just check for UNIVERSAL class and ignore the tag.
825
         */
826
13.0M
        if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
827
72.7k
            goto err;
828
72.7k
        }
829
13.0M
        len = (long)buf.length;
830
        /* Append a final null to string */
831
13.0M
        if (!BUF_MEM_grow_clean(&buf, len + 1)) {
832
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
833
0
            goto err;
834
0
        }
835
13.0M
        buf.data[len] = 0;
836
13.0M
        cont = (const unsigned char *)buf.data;
837
79.9M
    } else {
838
79.9M
        cont = p;
839
79.9M
        len = plen;
840
79.9M
        p += plen;
841
79.9M
    }
842
843
    /* We now have content length and type: translate into a structure */
844
    /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
845
115M
    if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
846
883k
        goto err;
847
848
114M
    *in = p;
849
114M
    ret = 1;
850
115M
err:
851
115M
    if (free_cont)
852
609k
        OPENSSL_free(buf.data);
853
115M
    return ret;
854
114M
}
855
856
/* Translate ASN1 content octets into a structure */
857
858
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, long len,
859
    int utype, char *free_cont, const ASN1_ITEM *it)
860
115M
{
861
115M
    ASN1_VALUE **opval = NULL;
862
115M
    ASN1_STRING *stmp;
863
115M
    ASN1_TYPE *typ = NULL;
864
115M
    int ret = 0;
865
115M
    int ilen = (int)len;
866
115M
    const ASN1_PRIMITIVE_FUNCS *pf;
867
115M
    ASN1_INTEGER **tint;
868
115M
    pf = it->funcs;
869
870
115M
    if (pf && pf->prim_c2i) {
871
3.47M
        if (len == (long)ilen)
872
3.47M
            return pf->prim_c2i(pval, cont, ilen, utype, free_cont, it);
873
3.47M
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
874
0
        return 0;
875
3.47M
    }
876
    /* If ANY type clear type and set pointer to internal value */
877
112M
    if (it->utype == V_ASN1_ANY) {
878
45.3M
        if (*pval == NULL) {
879
45.1M
            typ = ASN1_TYPE_new();
880
45.1M
            if (typ == NULL)
881
0
                goto err;
882
45.1M
            *pval = (ASN1_VALUE *)typ;
883
45.1M
        } else
884
118k
            typ = (ASN1_TYPE *)*pval;
885
886
45.3M
        if (utype != typ->type)
887
45.3M
            ASN1_TYPE_set(typ, utype, NULL);
888
45.3M
        opval = pval;
889
45.3M
        pval = &typ->value.asn1_value;
890
45.3M
    }
891
112M
    switch (utype) {
892
29.9M
    case V_ASN1_OBJECT:
893
29.9M
        if (len != (long)ilen
894
29.9M
            || !ossl_c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, ilen))
895
65.5k
            goto err;
896
29.9M
        break;
897
898
29.9M
    case V_ASN1_NULL:
899
1.08M
        if (len) {
900
4.76k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
901
4.76k
            goto err;
902
4.76k
        }
903
1.07M
        *pval = (ASN1_VALUE *)1;
904
1.07M
        break;
905
906
3.15M
    case V_ASN1_BOOLEAN:
907
3.15M
        if (len != 1) {
908
9.82k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
909
9.82k
            goto err;
910
3.14M
        } else {
911
3.14M
            ASN1_BOOLEAN *tbool;
912
3.14M
            tbool = (ASN1_BOOLEAN *)pval;
913
3.14M
            *tbool = *cont;
914
3.14M
        }
915
3.14M
        break;
916
917
5.99M
    case V_ASN1_BIT_STRING:
918
5.99M
        if (!ossl_c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
919
45.5k
            goto err;
920
5.95M
        break;
921
922
15.2M
    case V_ASN1_INTEGER:
923
16.0M
    case V_ASN1_ENUMERATED:
924
16.0M
        tint = (ASN1_INTEGER **)pval;
925
16.0M
        if (!ossl_c2i_ASN1_INTEGER(tint, &cont, len))
926
590k
            goto err;
927
        /* Fixup type to match the expected form */
928
15.4M
        (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
929
15.4M
        break;
930
931
8.24M
    case V_ASN1_OCTET_STRING:
932
14.4M
    case V_ASN1_NUMERICSTRING:
933
17.6M
    case V_ASN1_PRINTABLESTRING:
934
19.4M
    case V_ASN1_T61STRING:
935
19.5M
    case V_ASN1_VIDEOTEXSTRING:
936
20.8M
    case V_ASN1_IA5STRING:
937
23.1M
    case V_ASN1_UTCTIME:
938
23.3M
    case V_ASN1_GENERALIZEDTIME:
939
24.2M
    case V_ASN1_GRAPHICSTRING:
940
24.3M
    case V_ASN1_VISIBLESTRING:
941
24.3M
    case V_ASN1_GENERALSTRING:
942
25.5M
    case V_ASN1_UNIVERSALSTRING:
943
26.7M
    case V_ASN1_BMPSTRING:
944
28.8M
    case V_ASN1_UTF8STRING:
945
32.3M
    case V_ASN1_OTHER:
946
47.5M
    case V_ASN1_SET:
947
51.7M
    case V_ASN1_SEQUENCE:
948
56.0M
    default:
949
56.0M
        if (len != (long)ilen) {
950
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
951
0
            goto err;
952
0
        }
953
56.0M
        if (utype == V_ASN1_BMPSTRING && (len & 1)) {
954
2.42k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
955
2.42k
            goto err;
956
2.42k
        }
957
56.0M
        if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
958
3.19k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
959
3.19k
            goto err;
960
3.19k
        }
961
56.0M
        if (utype == V_ASN1_GENERALIZEDTIME && (len < 15)) {
962
6.80k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_GENERALIZEDTIME_IS_TOO_SHORT);
963
6.80k
            goto err;
964
6.80k
        }
965
56.0M
        if (utype == V_ASN1_UTCTIME && (len < 13)) {
966
3.19k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UTCTIME_IS_TOO_SHORT);
967
3.19k
            goto err;
968
3.19k
        }
969
        /* All based on ASN1_STRING and handled the same */
970
56.0M
        if (*pval == NULL) {
971
35.2M
            stmp = ASN1_STRING_type_new(utype);
972
35.2M
            if (stmp == NULL) {
973
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
974
0
                goto err;
975
0
            }
976
35.2M
            *pval = (ASN1_VALUE *)stmp;
977
35.2M
        } else {
978
20.8M
            stmp = (ASN1_STRING *)*pval;
979
20.8M
            stmp->type = utype;
980
20.8M
        }
981
        /* If we've already allocated a buffer use it */
982
56.0M
        if (*free_cont) {
983
12.4M
            ASN1_STRING_set0(stmp, (unsigned char *)cont /* UGLY CAST! */, ilen);
984
12.4M
            *free_cont = 0;
985
43.5M
        } else {
986
43.5M
            if (!ASN1_STRING_set(stmp, cont, ilen)) {
987
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
988
0
                ASN1_STRING_free(stmp);
989
0
                *pval = NULL;
990
0
                goto err;
991
0
            }
992
43.5M
        }
993
56.0M
        break;
994
112M
    }
995
    /* If ASN1_ANY and NULL type fix up value */
996
111M
    if (typ && (utype == V_ASN1_NULL))
997
1.06M
        typ->value.ptr = NULL;
998
999
111M
    ret = 1;
1000
112M
err:
1001
112M
    if (!ret) {
1002
731k
        ASN1_TYPE_free(typ);
1003
731k
        if (opval)
1004
42.0k
            *opval = NULL;
1005
731k
    }
1006
112M
    return ret;
1007
111M
}
1008
1009
/*
1010
 * This function finds the end of an ASN1 structure when passed its maximum
1011
 * length, whether it is indefinite length and a pointer to the content. This
1012
 * is more efficient than calling asn1_collect because it does not recurse on
1013
 * each indefinite length header.
1014
 */
1015
1016
static int asn1_find_end(const unsigned char **in, long len, char inf)
1017
1.31M
{
1018
1.31M
    uint32_t expected_eoc;
1019
1.31M
    long plen;
1020
1.31M
    const unsigned char *p = *in, *q;
1021
    /* If not indefinite length constructed just add length */
1022
1.31M
    if (inf == 0) {
1023
0
        *in += len;
1024
0
        return 1;
1025
0
    }
1026
1.31M
    expected_eoc = 1;
1027
    /*
1028
     * Indefinite length constructed form. Find the end when enough EOCs are
1029
     * found. If more indefinite length constructed headers are encountered
1030
     * increment the expected eoc count otherwise just skip to the end of the
1031
     * data.
1032
     */
1033
408M
    while (len > 0) {
1034
408M
        if (asn1_check_eoc(&p, len)) {
1035
30.3M
            expected_eoc--;
1036
30.3M
            if (expected_eoc == 0)
1037
1.21M
                break;
1038
29.1M
            len -= 2;
1039
29.1M
            continue;
1040
30.3M
        }
1041
377M
        q = p;
1042
        /* Just read in a header: only care about the length */
1043
377M
        if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1044
377M
                -1, 0, 0, NULL)) {
1045
65.9k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1046
65.9k
            return 0;
1047
65.9k
        }
1048
377M
        if (inf) {
1049
101M
            if (expected_eoc == UINT32_MAX) {
1050
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1051
0
                return 0;
1052
0
            }
1053
101M
            expected_eoc++;
1054
276M
        } else {
1055
276M
            p += plen;
1056
276M
        }
1057
377M
        len -= (long)(p - q);
1058
377M
    }
1059
1.25M
    if (expected_eoc) {
1060
37.6k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1061
37.6k
        return 0;
1062
37.6k
    }
1063
1.21M
    *in = p;
1064
1.21M
    return 1;
1065
1.25M
}
1066
1067
/*
1068
 * This function collects the asn1 data from a constructed string type into
1069
 * a buffer. The values of 'in' and 'len' should refer to the contents of the
1070
 * constructed type and 'inf' should be set if it is indefinite length.
1071
 */
1072
1073
#ifndef ASN1_MAX_STRING_NEST
1074
/*
1075
 * This determines how many levels of recursion are permitted in ASN1 string
1076
 * types. If it is not limited stack overflows can occur. If set to zero no
1077
 * recursion is allowed at all. Although zero should be adequate examples
1078
 * exist that require a value of 1. So 5 should be more than enough.
1079
 */
1080
6.32M
#define ASN1_MAX_STRING_NEST 5
1081
#endif
1082
1083
static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1084
    char inf, int tag, int aclass, int depth)
1085
19.4M
{
1086
19.4M
    const unsigned char *p, *q;
1087
19.4M
    long plen;
1088
19.4M
    char cst, ininf;
1089
19.4M
    p = *in;
1090
19.4M
    inf &= 1;
1091
    /*
1092
     * If no buffer and not indefinite length constructed just pass over the
1093
     * encoded data
1094
     */
1095
19.4M
    if (!buf && !inf) {
1096
0
        *in += len;
1097
0
        return 1;
1098
0
    }
1099
65.5M
    while (len > 0) {
1100
47.7M
        q = p;
1101
        /* Check for EOC */
1102
47.7M
        if (asn1_check_eoc(&p, len)) {
1103
            /*
1104
             * EOC is illegal outside indefinite length constructed form
1105
             */
1106
1.58M
            if (!inf) {
1107
13.5k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
1108
13.5k
                return 0;
1109
13.5k
            }
1110
1.57M
            inf = 0;
1111
1.57M
            break;
1112
1.58M
        }
1113
1114
46.2M
        if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1115
46.2M
                len, tag, aclass, 0, NULL)) {
1116
42.5k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1117
42.5k
            return 0;
1118
42.5k
        }
1119
1120
        /* If indefinite length constructed update max length */
1121
46.1M
        if (cst) {
1122
6.32M
            if (depth >= ASN1_MAX_STRING_NEST) {
1123
3.12k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_ASN1_STRING);
1124
3.12k
                return 0;
1125
3.12k
            }
1126
6.32M
            if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1127
64.0k
                return 0;
1128
39.8M
        } else if (plen && !collect_data(buf, &p, plen))
1129
0
            return 0;
1130
46.0M
        len -= (long)(p - q);
1131
46.0M
    }
1132
19.2M
    if (inf) {
1133
13.5k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1134
13.5k
        return 0;
1135
13.5k
    }
1136
19.2M
    *in = p;
1137
19.2M
    return 1;
1138
19.2M
}
1139
1140
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1141
17.4M
{
1142
17.4M
    long len;
1143
17.4M
    if (buf) {
1144
17.4M
        len = (long)buf->length;
1145
17.4M
        if (len + plen < 0) {
1146
            /* resulting buffer length will not fit into long */
1147
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_LENGTH_TOO_LONG);
1148
0
            return 0;
1149
0
        }
1150
17.4M
        if (!BUF_MEM_grow_clean(buf, len + plen)) {
1151
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
1152
0
            return 0;
1153
0
        }
1154
17.4M
        memcpy(buf->data + len, *p, plen);
1155
17.4M
    }
1156
17.4M
    *p += plen;
1157
17.4M
    return 1;
1158
17.4M
}
1159
1160
/* Check for ASN1 EOC and swallow it if found */
1161
1162
static int asn1_check_eoc(const unsigned char **in, long len)
1163
800M
{
1164
800M
    const unsigned char *p;
1165
1166
800M
    if (len < 2)
1167
195k
        return 0;
1168
800M
    p = *in;
1169
800M
    if (p[0] == '\0' && p[1] == '\0') {
1170
50.4M
        *in += 2;
1171
50.4M
        return 1;
1172
50.4M
    }
1173
749M
    return 0;
1174
800M
}
1175
1176
/*
1177
 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1178
 * length for indefinite length constructed form, we don't know the exact
1179
 * length but we can set an upper bound to the amount of data available minus
1180
 * the header length just read.
1181
 */
1182
1183
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1184
    char *inf, char *cst,
1185
    const unsigned char **in, long len,
1186
    int exptag, int expclass, char opt, ASN1_TLC *ctx)
1187
859M
{
1188
859M
    int i;
1189
859M
    int ptag, pclass;
1190
859M
    long plen;
1191
859M
    const unsigned char *p, *q;
1192
859M
    p = *in;
1193
859M
    q = p;
1194
1195
859M
    if (len <= 0) {
1196
1.05k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
1197
1.05k
        goto err;
1198
1.05k
    }
1199
859M
    if (ctx != NULL && ctx->valid) {
1200
94.1M
        i = ctx->ret;
1201
94.1M
        plen = ctx->plen;
1202
94.1M
        pclass = ctx->pclass;
1203
94.1M
        ptag = ctx->ptag;
1204
94.1M
        p += ctx->hdrlen;
1205
765M
    } else {
1206
765M
        i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1207
765M
        if (ctx != NULL) {
1208
340M
            ctx->ret = i;
1209
340M
            ctx->plen = plen;
1210
340M
            ctx->pclass = pclass;
1211
340M
            ctx->ptag = ptag;
1212
340M
            ctx->hdrlen = (int)(p - q);
1213
340M
            ctx->valid = 1;
1214
            /*
1215
             * If definite length, and no error, length + header can't exceed
1216
             * total amount of data available.
1217
             */
1218
340M
            if ((i & 0x81) == 0 && (plen + ctx->hdrlen) > len) {
1219
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
1220
0
                goto err;
1221
0
            }
1222
340M
        }
1223
765M
    }
1224
1225
859M
    if ((i & 0x80) != 0) {
1226
1.10M
        ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER);
1227
1.10M
        goto err;
1228
1.10M
    }
1229
858M
    if (exptag >= 0) {
1230
367M
        if (exptag != ptag || expclass != pclass) {
1231
            /*
1232
             * If type is OPTIONAL, not an error: indicate missing type.
1233
             */
1234
43.5M
            if (opt != 0)
1235
31.7M
                return -1;
1236
43.5M
            ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG);
1237
11.8M
            goto err;
1238
43.5M
        }
1239
        /*
1240
         * We have a tag and class match: assume we are going to do something
1241
         * with it
1242
         */
1243
323M
        asn1_tlc_clear(ctx);
1244
323M
    }
1245
1246
814M
    if ((i & 1) != 0)
1247
133M
        plen = len - (long)(p - q);
1248
1249
814M
    if (inf != NULL)
1250
751M
        *inf = i & 1;
1251
1252
814M
    if (cst != NULL)
1253
210M
        *cst = i & V_ASN1_CONSTRUCTED;
1254
1255
814M
    if (olen != NULL)
1256
751M
        *olen = plen;
1257
1258
814M
    if (oclass != NULL)
1259
63.5M
        *oclass = pclass;
1260
1261
814M
    if (otag != NULL)
1262
63.5M
        *otag = ptag;
1263
1264
814M
    *in = p;
1265
814M
    return 1;
1266
1267
12.9M
err:
1268
    asn1_tlc_clear(ctx);
1269
12.9M
    return 0;
1270
858M
}