Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/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
291M
#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
31.5M
{
107
31.5M
    if ((tag < 0) || (tag > 30))
108
4.88k
        return 0;
109
31.5M
    return tag2bit[tag];
110
31.5M
}
111
112
/* Macro to initialize and invalidate the cache */
113
114
#define asn1_tlc_clear(c)   \
115
271M
    do {                    \
116
271M
        if ((c) != NULL)    \
117
271M
            (c)->valid = 0; \
118
271M
    } while (0)
119
/* Version to avoid compiler warning about 'c' always non-NULL */
120
#define asn1_tlc_clear_nc(c) \
121
18.0M
    do {                     \
122
18.0M
        (c)->valid = 0;      \
123
18.0M
    } 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
20.1M
{
137
20.1M
    int rv;
138
139
20.1M
    if (pval == NULL || it == NULL) {
140
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
141
0
        return 0;
142
0
    }
143
20.1M
    rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0,
144
20.1M
        libctx, propq);
145
20.1M
    if (rv <= 0)
146
12.4M
        ASN1_item_ex_free(pval, it);
147
20.1M
    return rv;
148
20.1M
}
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.10M
{
154
2.10M
    return asn1_item_ex_d2i_intern(pval, in, len, it, tag, aclass, opt, ctx,
155
2.10M
        NULL, NULL);
156
2.10M
}
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
18.0M
{
163
18.0M
    ASN1_TLC c;
164
18.0M
    ASN1_VALUE *ptmpval = NULL;
165
166
18.0M
    if (pval == NULL)
167
15.9M
        pval = &ptmpval;
168
18.0M
    asn1_tlc_clear_nc(&c);
169
18.0M
    if (asn1_item_ex_d2i_intern(pval, in, len, it, -1, 0, 0, &c, libctx,
170
18.0M
            propq)
171
18.0M
        > 0)
172
5.87M
        return *pval;
173
12.1M
    return NULL;
174
18.0M
}
175
176
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
177
    const unsigned char **in, long len,
178
    const ASN1_ITEM *it)
179
16.8M
{
180
16.8M
    return ASN1_item_d2i_ex(pval, in, len, it, NULL, NULL);
181
16.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
291M
{
194
291M
    const ASN1_TEMPLATE *tt, *errtt = NULL;
195
291M
    const ASN1_EXTERN_FUNCS *ef;
196
291M
    const ASN1_AUX *aux;
197
291M
    ASN1_aux_cb *asn1_cb;
198
291M
    const unsigned char *p = NULL, *q;
199
291M
    unsigned char oclass;
200
291M
    char seq_eoc, seq_nolen, cst, isopt;
201
291M
    long tmplen;
202
291M
    int i;
203
291M
    int otag;
204
291M
    int ret = 0;
205
291M
    ASN1_VALUE **pchptr;
206
207
291M
    if (pval == NULL || it == NULL) {
208
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
209
0
        return 0;
210
0
    }
211
291M
    if (len <= 0) {
212
59.3k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
213
59.3k
        return 0;
214
59.3k
    }
215
291M
    aux = it->funcs;
216
291M
    if (aux && aux->asn1_cb)
217
9.53M
        asn1_cb = aux->asn1_cb;
218
282M
    else
219
282M
        asn1_cb = 0;
220
221
291M
    if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
222
17
        ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_TOO_DEEP);
223
17
        goto err;
224
17
    }
225
226
291M
    switch (it->itype) {
227
221M
    case ASN1_ITYPE_PRIMITIVE:
228
221M
        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
127M
            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
127M
            return asn1_template_ex_d2i(pval, in, len, it->templates, opt, ctx,
241
127M
                depth, libctx, propq);
242
127M
        }
243
94.1M
        return asn1_d2i_ex_primitive(pval, in, len, it,
244
94.1M
            tag, aclass, opt, ctx);
245
246
16.0M
    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
16.0M
        if (tag != -1) {
252
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
253
0
            goto err;
254
0
        }
255
256
16.0M
        p = *in;
257
        /* Just read in tag and class */
258
16.0M
        ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
259
16.0M
            &p, len, -1, 0, 1, ctx);
260
16.0M
        if (!ret) {
261
17.2k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
262
17.2k
            goto err;
263
17.2k
        }
264
265
        /* Must be UNIVERSAL class */
266
16.0M
        if (oclass != V_ASN1_UNIVERSAL) {
267
            /* If OPTIONAL, assume this is OK */
268
109k
            if (opt)
269
84.3k
                return -1;
270
109k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
271
24.7k
            goto err;
272
109k
        }
273
274
        /* Check tag matches bit map */
275
15.9M
        if (!(ASN1_tag2bit(otag) & it->utype)) {
276
            /* If OPTIONAL, assume this is OK */
277
310k
            if (opt)
278
12.2k
                return -1;
279
310k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_WRONG_TAG);
280
298k
            goto err;
281
310k
        }
282
15.6M
        return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
283
284
4.21M
    case ASN1_ITYPE_EXTERN:
285
        /* Use new style d2i */
286
4.21M
        ef = it->funcs;
287
4.21M
        if (ef->asn1_ex_d2i_ex != NULL)
288
2.10M
            return ef->asn1_ex_d2i_ex(pval, in, len, it, tag, aclass, opt, ctx,
289
2.10M
                libctx, propq);
290
2.10M
        return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
291
292
4.03M
    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
4.03M
        if (tag != -1) {
298
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
299
0
            goto err;
300
0
        }
301
302
4.03M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
303
0
            goto auxerr;
304
4.03M
        if (*pval) {
305
            /* Free up and zero CHOICE value if initialised */
306
687k
            i = ossl_asn1_get_choice_selector(pval, it);
307
687k
            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
3.34M
        } 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
4.03M
        p = *in;
319
17.3M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
320
16.8M
            pchptr = ossl_asn1_get_field_ptr(pval, tt);
321
            /*
322
             * We mark field as OPTIONAL so its absence can be recognised.
323
             */
324
16.8M
            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth,
325
16.8M
                libctx, propq);
326
            /* If field not present, try the next one */
327
16.8M
            if (ret == -1)
328
13.3M
                continue;
329
            /* If positive return, read OK, break loop */
330
3.50M
            if (ret > 0)
331
2.75M
                break;
332
            /*
333
             * Must be an ASN1 parsing error.
334
             * Free up any partial choice value
335
             */
336
750k
            ossl_asn1_template_free(pchptr, tt);
337
750k
            errtt = tt;
338
750k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
339
750k
            goto err;
340
3.50M
        }
341
342
        /* Did we fall off the end without reading anything? */
343
3.28M
        if (i == it->tcount) {
344
            /* If OPTIONAL, this is OK */
345
527k
            if (opt) {
346
                /* Free and zero it */
347
25.6k
                ASN1_item_ex_free(pval, it);
348
25.6k
                return -1;
349
25.6k
            }
350
527k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
351
501k
            goto err;
352
527k
        }
353
354
2.75M
        ossl_asn1_set_choice_selector(pval, i, it);
355
356
2.75M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
357
0
            goto auxerr;
358
2.75M
        *in = p;
359
2.75M
        return 1;
360
361
665k
    case ASN1_ITYPE_NDEF_SEQUENCE:
362
46.4M
    case ASN1_ITYPE_SEQUENCE:
363
46.4M
        p = *in;
364
46.4M
        tmplen = len;
365
366
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
367
46.4M
        if (tag == -1) {
368
42.2M
            tag = V_ASN1_SEQUENCE;
369
42.2M
            aclass = V_ASN1_UNIVERSAL;
370
42.2M
        }
371
        /* Get SEQUENCE length and update len, p */
372
46.4M
        ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
373
46.4M
            &p, len, tag, aclass, opt, ctx);
374
46.4M
        if (!ret) {
375
3.44M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
376
3.44M
            goto err;
377
42.9M
        } else if (ret == -1)
378
4.15M
            return -1;
379
38.8M
        if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
380
0
            len = tmplen - (p - *in);
381
0
            seq_nolen = 1;
382
0
        }
383
        /* If indefinite we don't do a length check */
384
38.8M
        else
385
38.8M
            seq_nolen = seq_eoc;
386
38.8M
        if (!cst) {
387
97.4k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
388
97.4k
            goto err;
389
97.4k
        }
390
391
38.7M
        if (*pval == NULL
392
29.0M
            && !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
38.7M
        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
148M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
402
109M
            if (tt->flags & ASN1_TFLG_ADB_MASK) {
403
622k
                const ASN1_TEMPLATE *seqtt;
404
622k
                ASN1_VALUE **pseqval;
405
622k
                seqtt = ossl_asn1_do_adb(*pval, tt, 0);
406
622k
                if (seqtt == NULL)
407
496k
                    continue;
408
125k
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
409
125k
                ossl_asn1_template_free(pseqval, seqtt);
410
125k
            }
411
109M
        }
412
413
        /* Get each field entry */
414
113M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
415
87.1M
            const ASN1_TEMPLATE *seqtt;
416
87.1M
            ASN1_VALUE **pseqval;
417
87.1M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
418
87.1M
            if (seqtt == NULL)
419
0
                goto err;
420
87.1M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
421
            /* Have we ran out of data? */
422
87.1M
            if (!len)
423
2.16M
                break;
424
84.9M
            q = p;
425
84.9M
            if (asn1_check_eoc(&p, len)) {
426
2.92M
                if (!seq_eoc) {
427
22.0k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
428
22.0k
                    goto err;
429
22.0k
                }
430
2.89M
                len -= p - q;
431
2.89M
                seq_eoc = 0;
432
2.89M
                break;
433
2.92M
            }
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
82.0M
            if (i == (it->tcount - 1))
441
27.6M
                isopt = 0;
442
54.4M
            else
443
54.4M
                isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
444
            /*
445
             * attempt to read in field, allowing each to be OPTIONAL
446
             */
447
448
82.0M
            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
449
82.0M
                depth, libctx, propq);
450
82.0M
            if (!ret) {
451
7.30M
                errtt = seqtt;
452
7.30M
                goto err;
453
74.7M
            } else if (ret == -1) {
454
                /*
455
                 * OPTIONAL component absent. Free and zero the field.
456
                 */
457
8.01M
                ossl_asn1_template_free(pseqval, seqtt);
458
8.01M
                continue;
459
8.01M
            }
460
            /* Update length */
461
66.7M
            len -= p - q;
462
66.7M
        }
463
464
        /* Check for EOC if expecting one */
465
31.4M
        if (seq_eoc && !asn1_check_eoc(&p, len)) {
466
136k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
467
136k
            goto err;
468
136k
        }
469
        /* Check all data read */
470
31.2M
        if (!seq_nolen && len) {
471
24.1k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
472
24.1k
            goto err;
473
24.1k
        }
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
42.3M
        for (; i < it->tcount; tt++, i++) {
481
11.2M
            const ASN1_TEMPLATE *seqtt;
482
11.2M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
483
11.2M
            if (seqtt == NULL)
484
0
                goto err;
485
11.2M
            if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
486
11.0M
                ASN1_VALUE **pseqval;
487
11.0M
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
488
11.0M
                ossl_asn1_template_free(pseqval, seqtt);
489
11.0M
            } else {
490
221k
                errtt = seqtt;
491
221k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_FIELD_MISSING);
492
221k
                goto err;
493
221k
            }
494
11.2M
        }
495
        /* Save encoding */
496
31.0M
        if (!ossl_asn1_enc_save(pval, *in, p - *in, it))
497
0
            goto auxerr;
498
31.0M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
499
22.9k
            goto auxerr;
500
30.9M
        *in = p;
501
30.9M
        return 1;
502
503
0
    default:
504
0
        return 0;
505
291M
    }
506
22.9k
auxerr:
507
22.9k
    ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
508
12.8M
err:
509
12.8M
    if (errtt)
510
8.27M
        ERR_add_error_data(4, "Field=", errtt->field_name,
511
8.27M
            ", Type=", it->sname);
512
4.59M
    else
513
4.59M
        ERR_add_error_data(2, "Type=", it->sname);
514
12.8M
    return 0;
515
22.9k
}
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
225M
{
528
225M
    int flags, aclass;
529
225M
    int ret;
530
225M
    long len;
531
225M
    const unsigned char *p, *q;
532
225M
    char exp_eoc;
533
225M
    if (!val)
534
0
        return 0;
535
225M
    flags = tt->flags;
536
225M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
537
538
225M
    p = *in;
539
540
    /* Check if EXPLICIT tag expected */
541
225M
    if (flags & ASN1_TFLG_EXPTAG) {
542
6.51M
        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
6.51M
        ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
548
6.51M
            &p, inlen, tt->tag, aclass, opt, ctx);
549
6.51M
        q = p;
550
6.51M
        if (!ret) {
551
268k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
552
268k
            return 0;
553
6.24M
        } else if (ret == -1)
554
4.80M
            return -1;
555
1.44M
        if (!cst) {
556
5.44k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
557
5.44k
            return 0;
558
5.44k
        }
559
        /* We've found the field so it can't be OPTIONAL now */
560
1.43M
        ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth, libctx,
561
1.43M
            propq);
562
1.43M
        if (!ret) {
563
230k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
564
230k
            return 0;
565
230k
        }
566
        /* We read the field in OK so update length */
567
1.20M
        len -= p - q;
568
1.20M
        if (exp_eoc) {
569
            /* If NDEF we must have an EOC here */
570
368k
            if (!asn1_check_eoc(&p, len)) {
571
33.8k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
572
33.8k
                goto err;
573
33.8k
            }
574
837k
        } else {
575
            /*
576
             * Otherwise we must hit the EXPLICIT tag end or its an error
577
             */
578
837k
            if (len) {
579
3.45k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
580
3.45k
                goto err;
581
3.45k
            }
582
837k
        }
583
1.20M
    } else
584
219M
        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth,
585
219M
            libctx, propq);
586
587
1.16M
    *in = p;
588
1.16M
    return 1;
589
590
37.2k
err:
591
37.2k
    return 0;
592
225M
}
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
220M
{
600
220M
    int flags, aclass;
601
220M
    int ret;
602
220M
    ASN1_VALUE *tval;
603
220M
    const unsigned char *p, *q;
604
220M
    if (!val)
605
0
        return 0;
606
220M
    flags = tt->flags;
607
220M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
608
609
220M
    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
220M
    if (tt->flags & ASN1_TFLG_EMBED) {
616
9.78M
        tval = (ASN1_VALUE *)val;
617
9.78M
        val = &tval;
618
9.78M
    }
619
620
220M
    if (flags & ASN1_TFLG_SK_MASK) {
621
        /* SET OF, SEQUENCE OF */
622
129M
        int sktag, skaclass;
623
129M
        char sk_eoc;
624
        /* First work out expected inner tag value */
625
129M
        if (flags & ASN1_TFLG_IMPTAG) {
626
1.00M
            sktag = tt->tag;
627
1.00M
            skaclass = aclass;
628
128M
        } else {
629
128M
            skaclass = V_ASN1_UNIVERSAL;
630
128M
            if (flags & ASN1_TFLG_SET_OF)
631
123M
                sktag = V_ASN1_SET;
632
5.24M
            else
633
5.24M
                sktag = V_ASN1_SEQUENCE;
634
128M
        }
635
        /* Get the tag */
636
129M
        ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
637
129M
            &p, len, sktag, skaclass, opt, ctx);
638
129M
        if (!ret) {
639
1.02M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
640
1.02M
            return 0;
641
128M
        } else if (ret == -1)
642
596k
            return -1;
643
128M
        if (*val == NULL)
644
127M
            *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
645
652k
        else {
646
            /*
647
             * We've got a valid STACK: free up any items present
648
             */
649
652k
            STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
650
652k
            ASN1_VALUE *vtmp;
651
652k
            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
652k
        }
656
657
128M
        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
305M
        while (len > 0) {
664
183M
            ASN1_VALUE *skfield;
665
183M
            q = p;
666
            /* See if EOC found */
667
183M
            if (asn1_check_eoc(&p, len)) {
668
4.22M
                if (!sk_eoc) {
669
8.72k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
670
8.72k
                    goto err;
671
8.72k
                }
672
4.22M
                len -= p - q;
673
4.22M
                sk_eoc = 0;
674
4.22M
                break;
675
4.22M
            }
676
178M
            skfield = NULL;
677
178M
            if (asn1_item_embed_d2i(&skfield, &p, len,
678
178M
                    ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
679
178M
                    depth, libctx, propq)
680
178M
                <= 0) {
681
1.51M
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
682
                /* |skfield| may be partially allocated despite failure. */
683
1.51M
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
684
1.51M
                goto err;
685
1.51M
            }
686
177M
            len -= p - q;
687
177M
            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
177M
        }
693
126M
        if (sk_eoc) {
694
12.6k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
695
12.6k
            goto err;
696
12.6k
        }
697
126M
    } else if (flags & ASN1_TFLG_IMPTAG) {
698
        /* IMPLICIT tagging */
699
14.0M
        ret = asn1_item_embed_d2i(val, &p, len,
700
14.0M
            ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
701
14.0M
            ctx, depth, libctx, propq);
702
14.0M
        if (!ret) {
703
394k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
704
394k
            goto err;
705
13.6M
        } else if (ret == -1)
706
11.4M
            return -1;
707
76.8M
    } else {
708
        /* Nothing special */
709
76.8M
        ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
710
76.8M
            -1, 0, opt, ctx, depth, libctx, propq);
711
76.8M
        if (!ret) {
712
6.45M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
713
6.45M
            goto err;
714
70.3M
        } else if (ret == -1)
715
4.51M
            return -1;
716
76.8M
    }
717
718
194M
    *in = p;
719
194M
    return 1;
720
721
8.38M
err:
722
8.38M
    return 0;
723
220M
}
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
109M
{
730
109M
    int ret = 0, utype;
731
109M
    long plen;
732
109M
    char cst, inf, free_cont = 0;
733
109M
    const unsigned char *p;
734
109M
    BUF_MEM buf = { 0, NULL, 0, 0 };
735
109M
    const unsigned char *cont = NULL;
736
109M
    long len;
737
738
109M
    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
109M
    if (it->itype == ASN1_ITYPE_MSTRING) {
744
15.6M
        utype = tag;
745
15.6M
        tag = -1;
746
15.6M
    } else
747
94.1M
        utype = it->utype;
748
749
109M
    if (utype == V_ASN1_ANY) {
750
        /* If type is ANY need to figure out type from tag */
751
35.7M
        unsigned char oclass;
752
35.7M
        if (tag >= 0) {
753
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
754
0
            return 0;
755
0
        }
756
35.7M
        if (opt) {
757
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
758
0
            return 0;
759
0
        }
760
35.7M
        p = *in;
761
35.7M
        ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
762
35.7M
            &p, inlen, -1, 0, 0, ctx);
763
35.7M
        if (!ret) {
764
30.7k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
765
30.7k
            return 0;
766
30.7k
        }
767
35.6M
        if (oclass != V_ASN1_UNIVERSAL)
768
3.15M
            utype = V_ASN1_OTHER;
769
35.6M
    }
770
109M
    if (tag == -1) {
771
99.8M
        tag = utype;
772
99.8M
        aclass = V_ASN1_UNIVERSAL;
773
99.8M
    }
774
109M
    p = *in;
775
    /* Check header */
776
109M
    ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
777
109M
        &p, inlen, tag, aclass, opt, ctx);
778
109M
    if (!ret) {
779
5.05M
        ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
780
5.05M
        return 0;
781
104M
    } else if (ret == -1)
782
11.7M
        return -1;
783
92.9M
    ret = 0;
784
    /* SEQUENCE, SET and "OTHER" are left in encoded form */
785
92.9M
    if ((utype == V_ASN1_SEQUENCE)
786
89.5M
        || (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
18.5M
        if (utype == V_ASN1_OTHER) {
792
3.15M
            asn1_tlc_clear(ctx);
793
3.15M
        }
794
        /* SEQUENCE and SET must be constructed */
795
15.4M
        else if (!cst) {
796
12.9k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
797
12.9k
            return 0;
798
12.9k
        }
799
800
18.5M
        cont = *in;
801
        /* If indefinite length constructed find the real end */
802
18.5M
        if (inf) {
803
1.24M
            if (!asn1_find_end(&p, plen, inf))
804
79.7k
                goto err;
805
1.16M
            len = p - cont;
806
17.3M
        } else {
807
17.3M
            len = p - cont + plen;
808
17.3M
            p += plen;
809
17.3M
        }
810
74.4M
    } else if (cst) {
811
10.0M
        if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
812
10.0M
            || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
813
10.0M
            || utype == V_ASN1_ENUMERATED) {
814
22.0k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
815
22.0k
            return 0;
816
22.0k
        }
817
818
        /* Free any returned 'buf' content */
819
10.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
10.0M
        if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
827
52.9k
            goto err;
828
52.9k
        }
829
9.95M
        len = buf.length;
830
        /* Append a final null to string */
831
9.95M
        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
9.95M
        buf.data[len] = 0;
836
9.95M
        cont = (const unsigned char *)buf.data;
837
64.3M
    } else {
838
64.3M
        cont = p;
839
64.3M
        len = plen;
840
64.3M
        p += plen;
841
64.3M
    }
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
92.8M
    if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
846
687k
        goto err;
847
848
92.1M
    *in = p;
849
92.1M
    ret = 1;
850
92.9M
err:
851
92.9M
    if (free_cont)
852
267k
        OPENSSL_free(buf.data);
853
92.9M
    return ret;
854
92.1M
}
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
92.8M
{
861
92.8M
    ASN1_VALUE **opval = NULL;
862
92.8M
    ASN1_STRING *stmp;
863
92.8M
    ASN1_TYPE *typ = NULL;
864
92.8M
    int ret = 0;
865
92.8M
    int ilen = (int)len;
866
92.8M
    const ASN1_PRIMITIVE_FUNCS *pf;
867
92.8M
    ASN1_INTEGER **tint;
868
92.8M
    pf = it->funcs;
869
870
92.8M
    if (pf && pf->prim_c2i) {
871
2.89M
        if (len == (long)ilen)
872
2.89M
            return pf->prim_c2i(pval, cont, ilen, utype, free_cont, it);
873
2.89M
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
874
0
        return 0;
875
2.89M
    }
876
    /* If ANY type clear type and set pointer to internal value */
877
89.9M
    if (it->utype == V_ASN1_ANY) {
878
35.6M
        if (*pval == NULL) {
879
35.5M
            typ = ASN1_TYPE_new();
880
35.5M
            if (typ == NULL)
881
0
                goto err;
882
35.5M
            *pval = (ASN1_VALUE *)typ;
883
35.5M
        } else
884
97.9k
            typ = (ASN1_TYPE *)*pval;
885
886
35.6M
        if (utype != typ->type)
887
35.6M
            ASN1_TYPE_set(typ, utype, NULL);
888
35.6M
        opval = pval;
889
35.6M
        pval = &typ->value.asn1_value;
890
35.6M
    }
891
89.9M
    switch (utype) {
892
24.8M
    case V_ASN1_OBJECT:
893
24.8M
        if (len != (long)ilen
894
24.8M
            || !ossl_c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, ilen))
895
43.5k
            goto err;
896
24.8M
        break;
897
898
24.8M
    case V_ASN1_NULL:
899
790k
        if (len) {
900
4.02k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
901
4.02k
            goto err;
902
4.02k
        }
903
786k
        *pval = (ASN1_VALUE *)1;
904
786k
        break;
905
906
2.46M
    case V_ASN1_BOOLEAN:
907
2.46M
        if (len != 1) {
908
9.11k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
909
9.11k
            goto err;
910
2.45M
        } else {
911
2.45M
            ASN1_BOOLEAN *tbool;
912
2.45M
            tbool = (ASN1_BOOLEAN *)pval;
913
2.45M
            *tbool = *cont;
914
2.45M
        }
915
2.45M
        break;
916
917
5.25M
    case V_ASN1_BIT_STRING:
918
5.25M
        if (!ossl_c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
919
35.4k
            goto err;
920
5.22M
        break;
921
922
9.51M
    case V_ASN1_INTEGER:
923
10.9M
    case V_ASN1_ENUMERATED:
924
10.9M
        tint = (ASN1_INTEGER **)pval;
925
10.9M
        if (!ossl_c2i_ASN1_INTEGER(tint, &cont, len))
926
478k
            goto err;
927
        /* Fixup type to match the expected form */
928
10.4M
        (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
929
10.4M
        break;
930
931
6.76M
    case V_ASN1_OCTET_STRING:
932
11.3M
    case V_ASN1_NUMERICSTRING:
933
14.2M
    case V_ASN1_PRINTABLESTRING:
934
15.2M
    case V_ASN1_T61STRING:
935
15.2M
    case V_ASN1_VIDEOTEXSTRING:
936
16.4M
    case V_ASN1_IA5STRING:
937
18.2M
    case V_ASN1_UTCTIME:
938
18.4M
    case V_ASN1_GENERALIZEDTIME:
939
19.3M
    case V_ASN1_GRAPHICSTRING:
940
19.3M
    case V_ASN1_VISIBLESTRING:
941
19.3M
    case V_ASN1_GENERALSTRING:
942
20.3M
    case V_ASN1_UNIVERSALSTRING:
943
21.5M
    case V_ASN1_BMPSTRING:
944
23.1M
    case V_ASN1_UTF8STRING:
945
26.3M
    case V_ASN1_OTHER:
946
38.2M
    case V_ASN1_SET:
947
41.6M
    case V_ASN1_SEQUENCE:
948
45.5M
    default:
949
45.5M
        if (len != (long)ilen) {
950
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
951
0
            goto err;
952
0
        }
953
45.5M
        if (utype == V_ASN1_BMPSTRING && (len & 1)) {
954
1.94k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
955
1.94k
            goto err;
956
1.94k
        }
957
45.5M
        if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
958
2.59k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
959
2.59k
            goto err;
960
2.59k
        }
961
45.5M
        if (utype == V_ASN1_GENERALIZEDTIME && (len < 15)) {
962
6.09k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_GENERALIZEDTIME_IS_TOO_SHORT);
963
6.09k
            goto err;
964
6.09k
        }
965
45.5M
        if (utype == V_ASN1_UTCTIME && (len < 13)) {
966
2.55k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UTCTIME_IS_TOO_SHORT);
967
2.55k
            goto err;
968
2.55k
        }
969
        /* All based on ASN1_STRING and handled the same */
970
45.5M
        if (*pval == NULL) {
971
27.7M
            stmp = ASN1_STRING_type_new(utype);
972
27.7M
            if (stmp == NULL) {
973
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
974
0
                goto err;
975
0
            }
976
27.7M
            *pval = (ASN1_VALUE *)stmp;
977
27.7M
        } else {
978
17.7M
            stmp = (ASN1_STRING *)*pval;
979
17.7M
            stmp->type = utype;
980
17.7M
        }
981
        /* If we've already allocated a buffer use it */
982
45.5M
        if (*free_cont) {
983
9.74M
            ASN1_STRING_set0(stmp, (unsigned char *)cont /* UGLY CAST! */, ilen);
984
9.74M
            *free_cont = 0;
985
35.8M
        } else {
986
35.8M
            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
35.8M
        }
993
45.5M
        break;
994
89.9M
    }
995
    /* If ASN1_ANY and NULL type fix up value */
996
89.3M
    if (typ && (utype == V_ASN1_NULL))
997
769k
        typ->value.ptr = NULL;
998
999
89.3M
    ret = 1;
1000
89.9M
err:
1001
89.9M
    if (!ret) {
1002
584k
        ASN1_TYPE_free(typ);
1003
584k
        if (opval)
1004
30.7k
            *opval = NULL;
1005
584k
    }
1006
89.9M
    return ret;
1007
89.3M
}
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.24M
{
1018
1.24M
    uint32_t expected_eoc;
1019
1.24M
    long plen;
1020
1.24M
    const unsigned char *p = *in, *q;
1021
    /* If not indefinite length constructed just add length */
1022
1.24M
    if (inf == 0) {
1023
0
        *in += len;
1024
0
        return 1;
1025
0
    }
1026
1.24M
    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
240M
    while (len > 0) {
1034
240M
        if (asn1_check_eoc(&p, len)) {
1035
21.5M
            expected_eoc--;
1036
21.5M
            if (expected_eoc == 0)
1037
1.16M
                break;
1038
20.3M
            len -= 2;
1039
20.3M
            continue;
1040
21.5M
        }
1041
219M
        q = p;
1042
        /* Just read in a header: only care about the length */
1043
219M
        if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1044
219M
                -1, 0, 0, NULL)) {
1045
51.0k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1046
51.0k
            return 0;
1047
51.0k
        }
1048
219M
        if (inf) {
1049
52.1M
            if (expected_eoc == UINT32_MAX) {
1050
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1051
0
                return 0;
1052
0
            }
1053
52.1M
            expected_eoc++;
1054
167M
        } else {
1055
167M
            p += plen;
1056
167M
        }
1057
219M
        len -= p - q;
1058
219M
    }
1059
1.19M
    if (expected_eoc) {
1060
28.6k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1061
28.6k
        return 0;
1062
28.6k
    }
1063
1.16M
    *in = p;
1064
1.16M
    return 1;
1065
1.19M
}
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
7.34M
#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
17.3M
{
1086
17.3M
    const unsigned char *p, *q;
1087
17.3M
    long plen;
1088
17.3M
    char cst, ininf;
1089
17.3M
    p = *in;
1090
17.3M
    inf &= 1;
1091
    /*
1092
     * If no buffer and not indefinite length constructed just pass over the
1093
     * encoded data
1094
     */
1095
17.3M
    if (!buf && !inf) {
1096
0
        *in += len;
1097
0
        return 1;
1098
0
    }
1099
53.0M
    while (len > 0) {
1100
37.2M
        q = p;
1101
        /* Check for EOC */
1102
37.2M
        if (asn1_check_eoc(&p, len)) {
1103
            /*
1104
             * EOC is illegal outside indefinite length constructed form
1105
             */
1106
1.42M
            if (!inf) {
1107
7.92k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
1108
7.92k
                return 0;
1109
7.92k
            }
1110
1.41M
            inf = 0;
1111
1.41M
            break;
1112
1.42M
        }
1113
1114
35.8M
        if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1115
35.8M
                len, tag, aclass, 0, NULL)) {
1116
30.4k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1117
30.4k
            return 0;
1118
30.4k
        }
1119
1120
        /* If indefinite length constructed update max length */
1121
35.7M
        if (cst) {
1122
7.34M
            if (depth >= ASN1_MAX_STRING_NEST) {
1123
2.79k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_ASN1_STRING);
1124
2.79k
                return 0;
1125
2.79k
            }
1126
7.34M
            if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1127
48.6k
                return 0;
1128
28.4M
        } else if (plen && !collect_data(buf, &p, plen))
1129
0
            return 0;
1130
35.7M
        len -= p - q;
1131
35.7M
    }
1132
17.2M
    if (inf) {
1133
11.7k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1134
11.7k
        return 0;
1135
11.7k
    }
1136
17.2M
    *in = p;
1137
17.2M
    return 1;
1138
17.2M
}
1139
1140
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1141
11.4M
{
1142
11.4M
    int len;
1143
11.4M
    if (buf) {
1144
11.4M
        len = buf->length;
1145
11.4M
        if (!BUF_MEM_grow_clean(buf, len + plen)) {
1146
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
1147
0
            return 0;
1148
0
        }
1149
11.4M
        memcpy(buf->data + len, *p, plen);
1150
11.4M
    }
1151
11.4M
    *p += plen;
1152
11.4M
    return 1;
1153
11.4M
}
1154
1155
/* Check for ASN1 EOC and swallow it if found */
1156
1157
static int asn1_check_eoc(const unsigned char **in, long len)
1158
555M
{
1159
555M
    const unsigned char *p;
1160
1161
555M
    if (len < 2)
1162
148k
        return 0;
1163
555M
    p = *in;
1164
555M
    if (p[0] == '\0' && p[1] == '\0') {
1165
39.2M
        *in += 2;
1166
39.2M
        return 1;
1167
39.2M
    }
1168
515M
    return 0;
1169
555M
}
1170
1171
/*
1172
 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1173
 * length for indefinite length constructed form, we don't know the exact
1174
 * length but we can set an upper bound to the amount of data available minus
1175
 * the header length just read.
1176
 */
1177
1178
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1179
    char *inf, char *cst,
1180
    const unsigned char **in, long len,
1181
    int exptag, int expclass, char opt, ASN1_TLC *ctx)
1182
599M
{
1183
599M
    int i;
1184
599M
    int ptag, pclass;
1185
599M
    long plen;
1186
599M
    const unsigned char *p, *q;
1187
599M
    p = *in;
1188
599M
    q = p;
1189
1190
599M
    if (len <= 0) {
1191
985
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
1192
985
        goto err;
1193
985
    }
1194
599M
    if (ctx != NULL && ctx->valid) {
1195
72.1M
        i = ctx->ret;
1196
72.1M
        plen = ctx->plen;
1197
72.1M
        pclass = ctx->pclass;
1198
72.1M
        ptag = ctx->ptag;
1199
72.1M
        p += ctx->hdrlen;
1200
527M
    } else {
1201
527M
        i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1202
527M
        if (ctx != NULL) {
1203
272M
            ctx->ret = i;
1204
272M
            ctx->plen = plen;
1205
272M
            ctx->pclass = pclass;
1206
272M
            ctx->ptag = ptag;
1207
272M
            ctx->hdrlen = p - q;
1208
272M
            ctx->valid = 1;
1209
            /*
1210
             * If definite length, and no error, length + header can't exceed
1211
             * total amount of data available.
1212
             */
1213
272M
            if ((i & 0x81) == 0 && (plen + ctx->hdrlen) > len) {
1214
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
1215
0
                goto err;
1216
0
            }
1217
272M
        }
1218
527M
    }
1219
1220
599M
    if ((i & 0x80) != 0) {
1221
839k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER);
1222
839k
        goto err;
1223
839k
    }
1224
598M
    if (exptag >= 0) {
1225
288M
        if (exptag != ptag || expclass != pclass) {
1226
            /*
1227
             * If type is OPTIONAL, not an error: indicate missing type.
1228
             */
1229
30.3M
            if (opt != 0)
1230
21.2M
                return -1;
1231
30.3M
            ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG);
1232
9.08M
            goto err;
1233
30.3M
        }
1234
        /*
1235
         * We have a tag and class match: assume we are going to do something
1236
         * with it
1237
         */
1238
258M
        asn1_tlc_clear(ctx);
1239
258M
    }
1240
1241
568M
    if ((i & 1) != 0)
1242
79.1M
        plen = len - (p - q);
1243
1244
568M
    if (inf != NULL)
1245
516M
        *inf = i & 1;
1246
1247
568M
    if (cst != NULL)
1248
169M
        *cst = i & V_ASN1_CONSTRUCTED;
1249
1250
568M
    if (olen != NULL)
1251
516M
        *olen = plen;
1252
1253
568M
    if (oclass != NULL)
1254
51.7M
        *oclass = pclass;
1255
1256
568M
    if (otag != NULL)
1257
51.7M
        *otag = ptag;
1258
1259
568M
    *in = p;
1260
568M
    return 1;
1261
1262
9.92M
err:
1263
    asn1_tlc_clear(ctx);
1264
9.92M
    return 0;
1265
598M
}