Coverage Report

Created: 2026-07-12 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/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
383M
#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
43.7M
{
107
43.7M
    if ((tag < 0) || (tag > 30))
108
5.83k
        return 0;
109
43.7M
    return tag2bit[tag];
110
43.7M
}
111
112
/* Macro to initialize and invalidate the cache */
113
114
#define asn1_tlc_clear(c)   \
115
359M
    do {                    \
116
359M
        if ((c) != NULL)    \
117
359M
            (c)->valid = 0; \
118
359M
    } while (0)
119
/* Version to avoid compiler warning about 'c' always non-NULL */
120
#define asn1_tlc_clear_nc(c) \
121
21.2M
    do {                     \
122
21.2M
        (c)->valid = 0;      \
123
21.2M
    } 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
23.8M
{
137
23.8M
    int rv;
138
139
23.8M
    if (pval == NULL || it == NULL) {
140
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
141
0
        return 0;
142
0
    }
143
23.8M
    rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0,
144
23.8M
        libctx, propq);
145
23.8M
    if (rv <= 0)
146
14.2M
        ASN1_item_ex_free(pval, it);
147
23.8M
    return rv;
148
23.8M
}
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.64M
{
154
2.64M
    return asn1_item_ex_d2i_intern(pval, in, len, it, tag, aclass, opt, ctx,
155
2.64M
        NULL, NULL);
156
2.64M
}
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
21.2M
{
163
21.2M
    ASN1_TLC c;
164
21.2M
    ASN1_VALUE *ptmpval = NULL;
165
166
21.2M
    if (pval == NULL)
167
18.4M
        pval = &ptmpval;
168
21.2M
    asn1_tlc_clear_nc(&c);
169
21.2M
    if (asn1_item_ex_d2i_intern(pval, in, len, it, -1, 0, 0, &c, libctx,
170
21.2M
            propq)
171
21.2M
        > 0)
172
7.26M
        return *pval;
173
13.9M
    return NULL;
174
21.2M
}
175
176
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
177
    const unsigned char **in, long len,
178
    const ASN1_ITEM *it)
179
19.7M
{
180
19.7M
    return ASN1_item_d2i_ex(pval, in, len, it, NULL, NULL);
181
19.7M
}
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
383M
{
194
383M
    const ASN1_TEMPLATE *tt, *errtt = NULL;
195
383M
    const ASN1_EXTERN_FUNCS *ef;
196
383M
    const ASN1_AUX *aux;
197
383M
    ASN1_aux_cb *asn1_cb;
198
383M
    const unsigned char *p = NULL, *q;
199
383M
    unsigned char oclass;
200
383M
    char seq_eoc, seq_nolen, cst, isopt;
201
383M
    long tmplen;
202
383M
    int i;
203
383M
    int otag;
204
383M
    int ret = 0;
205
383M
    ASN1_VALUE **pchptr;
206
207
383M
    if (pval == NULL || it == NULL) {
208
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
209
0
        return 0;
210
0
    }
211
383M
    if (len <= 0) {
212
83.2k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
213
83.2k
        return 0;
214
83.2k
    }
215
383M
    aux = it->funcs;
216
383M
    if (aux && aux->asn1_cb)
217
12.1M
        asn1_cb = aux->asn1_cb;
218
371M
    else
219
371M
        asn1_cb = 0;
220
221
383M
    if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
222
15
        ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_TOO_DEEP);
223
15
        goto err;
224
15
    }
225
226
383M
    switch (it->itype) {
227
293M
    case ASN1_ITYPE_PRIMITIVE:
228
293M
        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
168M
            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
168M
            return asn1_template_ex_d2i(pval, in, len, it->templates, opt, ctx,
241
168M
                depth, libctx, propq);
242
168M
        }
243
124M
        return asn1_d2i_ex_primitive(pval, in, len, it,
244
124M
            tag, aclass, opt, ctx);
245
246
21.8M
    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
21.8M
        if (tag != -1) {
252
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
253
0
            goto err;
254
0
        }
255
256
21.8M
        p = *in;
257
        /* Just read in tag and class */
258
21.8M
        ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
259
21.8M
            &p, len, -1, 0, 1, ctx);
260
21.8M
        if (!ret) {
261
20.2k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
262
20.2k
            goto err;
263
20.2k
        }
264
265
        /* Must be UNIVERSAL class */
266
21.7M
        if (oclass != V_ASN1_UNIVERSAL) {
267
            /* If OPTIONAL, assume this is OK */
268
153k
            if (opt)
269
124k
                return -1;
270
153k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
271
29.3k
            goto err;
272
153k
        }
273
274
        /* Check tag matches bit map */
275
21.6M
        if (!(ASN1_tag2bit(otag) & it->utype)) {
276
            /* If OPTIONAL, assume this is OK */
277
361k
            if (opt)
278
17.7k
                return -1;
279
361k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_WRONG_TAG);
280
344k
            goto err;
281
361k
        }
282
21.2M
        return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
283
284
5.34M
    case ASN1_ITYPE_EXTERN:
285
        /* Use new style d2i */
286
5.34M
        ef = it->funcs;
287
5.34M
        if (ef->asn1_ex_d2i_ex != NULL)
288
2.69M
            return ef->asn1_ex_d2i_ex(pval, in, len, it, tag, aclass, opt, ctx,
289
2.69M
                libctx, propq);
290
2.64M
        return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
291
292
5.09M
    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.09M
        if (tag != -1) {
298
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
299
0
            goto err;
300
0
        }
301
302
5.09M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
303
0
            goto auxerr;
304
5.09M
        if (*pval) {
305
            /* Free up and zero CHOICE value if initialised */
306
817k
            i = ossl_asn1_get_choice_selector(pval, it);
307
817k
            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.27M
        } 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.09M
        p = *in;
319
19.8M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
320
19.2M
            pchptr = ossl_asn1_get_field_ptr(pval, tt);
321
            /*
322
             * We mark field as OPTIONAL so its absence can be recognised.
323
             */
324
19.2M
            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth,
325
19.2M
                libctx, propq);
326
            /* If field not present, try the next one */
327
19.2M
            if (ret == -1)
328
14.7M
                continue;
329
            /* If positive return, read OK, break loop */
330
4.49M
            if (ret > 0)
331
3.56M
                break;
332
            /*
333
             * Must be an ASN1 parsing error.
334
             * Free up any partial choice value
335
             */
336
928k
            ossl_asn1_template_free(pchptr, tt);
337
928k
            errtt = tt;
338
928k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
339
928k
            goto err;
340
4.49M
        }
341
342
        /* Did we fall off the end without reading anything? */
343
4.16M
        if (i == it->tcount) {
344
            /* If OPTIONAL, this is OK */
345
598k
            if (opt) {
346
                /* Free and zero it */
347
32.6k
                ASN1_item_ex_free(pval, it);
348
32.6k
                return -1;
349
32.6k
            }
350
598k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
351
565k
            goto err;
352
598k
        }
353
354
3.56M
        ossl_asn1_set_choice_selector(pval, i, it);
355
356
3.56M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
357
0
            goto auxerr;
358
3.56M
        *in = p;
359
3.56M
        return 1;
360
361
742k
    case ASN1_ITYPE_NDEF_SEQUENCE:
362
57.9M
    case ASN1_ITYPE_SEQUENCE:
363
57.9M
        p = *in;
364
57.9M
        tmplen = len;
365
366
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
367
57.9M
        if (tag == -1) {
368
53.5M
            tag = V_ASN1_SEQUENCE;
369
53.5M
            aclass = V_ASN1_UNIVERSAL;
370
53.5M
        }
371
        /* Get SEQUENCE length and update len, p */
372
57.9M
        ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
373
57.9M
            &p, len, tag, aclass, opt, ctx);
374
57.9M
        if (!ret) {
375
3.81M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
376
3.81M
            goto err;
377
54.1M
        } else if (ret == -1)
378
4.74M
            return -1;
379
49.4M
        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
49.4M
        else
385
49.4M
            seq_nolen = seq_eoc;
386
49.4M
        if (!cst) {
387
100k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
388
100k
            goto err;
389
100k
        }
390
391
49.3M
        if (*pval == NULL
392
37.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
49.3M
        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
185M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
402
136M
            if (tt->flags & ASN1_TFLG_ADB_MASK) {
403
676k
                const ASN1_TEMPLATE *seqtt;
404
676k
                ASN1_VALUE **pseqval;
405
676k
                seqtt = ossl_asn1_do_adb(*pval, tt, 0);
406
676k
                if (seqtt == NULL)
407
548k
                    continue;
408
128k
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
409
128k
                ossl_asn1_template_free(pseqval, seqtt);
410
128k
            }
411
136M
        }
412
413
        /* Get each field entry */
414
145M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
415
111M
            const ASN1_TEMPLATE *seqtt;
416
111M
            ASN1_VALUE **pseqval;
417
111M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
418
111M
            if (seqtt == NULL)
419
0
                goto err;
420
111M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
421
            /* Have we ran out of data? */
422
111M
            if (!len)
423
2.58M
                break;
424
108M
            q = p;
425
108M
            if (asn1_check_eoc(&p, len)) {
426
3.58M
                if (!seq_eoc) {
427
28.3k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
428
28.3k
                    goto err;
429
28.3k
                }
430
3.55M
                len -= p - q;
431
3.55M
                seq_eoc = 0;
432
3.55M
                break;
433
3.58M
            }
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
105M
            if (i == (it->tcount - 1))
441
36.1M
                isopt = 0;
442
68.9M
            else
443
68.9M
                isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
444
            /*
445
             * attempt to read in field, allowing each to be OPTIONAL
446
             */
447
448
105M
            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
449
105M
                depth, libctx, propq);
450
105M
            if (!ret) {
451
8.51M
                errtt = seqtt;
452
8.51M
                goto err;
453
96.6M
            } else if (ret == -1) {
454
                /*
455
                 * OPTIONAL component absent. Free and zero the field.
456
                 */
457
9.76M
                ossl_asn1_template_free(pseqval, seqtt);
458
9.76M
                continue;
459
9.76M
            }
460
            /* Update length */
461
86.8M
            len -= p - q;
462
86.8M
        }
463
464
        /* Check for EOC if expecting one */
465
40.7M
        if (seq_eoc && !asn1_check_eoc(&p, len)) {
466
183k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
467
183k
            goto err;
468
183k
        }
469
        /* Check all data read */
470
40.5M
        if (!seq_nolen && len) {
471
28.6k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
472
28.6k
            goto err;
473
28.6k
        }
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
53.0M
        for (; i < it->tcount; tt++, i++) {
481
12.6M
            const ASN1_TEMPLATE *seqtt;
482
12.6M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
483
12.6M
            if (seqtt == NULL)
484
0
                goto err;
485
12.6M
            if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
486
12.4M
                ASN1_VALUE **pseqval;
487
12.4M
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
488
12.4M
                ossl_asn1_template_free(pseqval, seqtt);
489
12.4M
            } else {
490
255k
                errtt = seqtt;
491
255k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_FIELD_MISSING);
492
255k
                goto err;
493
255k
            }
494
12.6M
        }
495
        /* Save encoding */
496
40.3M
        if (!ossl_asn1_enc_save(pval, *in, p - *in, it))
497
0
            goto auxerr;
498
40.3M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
499
20.3k
            goto auxerr;
500
40.2M
        *in = p;
501
40.2M
        return 1;
502
503
0
    default:
504
0
        return 0;
505
383M
    }
506
20.3k
auxerr:
507
20.3k
    ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
508
14.8M
err:
509
14.8M
    if (errtt)
510
9.69M
        ERR_add_error_data(4, "Field=", errtt->field_name,
511
9.69M
            ", Type=", it->sname);
512
5.13M
    else
513
5.13M
        ERR_add_error_data(2, "Type=", it->sname);
514
14.8M
    return 0;
515
20.3k
}
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
293M
{
528
293M
    int flags, aclass;
529
293M
    int ret;
530
293M
    long len;
531
293M
    const unsigned char *p, *q;
532
293M
    char exp_eoc;
533
293M
    if (!val)
534
0
        return 0;
535
293M
    flags = tt->flags;
536
293M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
537
538
293M
    p = *in;
539
540
    /* Check if EXPLICIT tag expected */
541
293M
    if (flags & ASN1_TFLG_EXPTAG) {
542
7.41M
        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
7.41M
        ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
548
7.41M
            &p, inlen, tt->tag, aclass, opt, ctx);
549
7.41M
        q = p;
550
7.41M
        if (!ret) {
551
313k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
552
313k
            return 0;
553
7.09M
        } else if (ret == -1)
554
5.35M
            return -1;
555
1.74M
        if (!cst) {
556
6.06k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
557
6.06k
            return 0;
558
6.06k
        }
559
        /* We've found the field so it can't be OPTIONAL now */
560
1.73M
        ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth, libctx,
561
1.73M
            propq);
562
1.73M
        if (!ret) {
563
306k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
564
306k
            return 0;
565
306k
        }
566
        /* We read the field in OK so update length */
567
1.43M
        len -= p - q;
568
1.43M
        if (exp_eoc) {
569
            /* If NDEF we must have an EOC here */
570
463k
            if (!asn1_check_eoc(&p, len)) {
571
43.9k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
572
43.9k
                goto err;
573
43.9k
            }
574
969k
        } else {
575
            /*
576
             * Otherwise we must hit the EXPLICIT tag end or its an error
577
             */
578
969k
            if (len) {
579
4.81k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
580
4.81k
                goto err;
581
4.81k
            }
582
969k
        }
583
1.43M
    } else
584
285M
        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth,
585
285M
            libctx, propq);
586
587
1.38M
    *in = p;
588
1.38M
    return 1;
589
590
48.7k
err:
591
48.7k
    return 0;
592
293M
}
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
287M
{
600
287M
    int flags, aclass;
601
287M
    int ret;
602
287M
    ASN1_VALUE *tval;
603
287M
    const unsigned char *p, *q;
604
287M
    if (!val)
605
0
        return 0;
606
287M
    flags = tt->flags;
607
287M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
608
609
287M
    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
287M
    if (tt->flags & ASN1_TFLG_EMBED) {
616
12.3M
        tval = (ASN1_VALUE *)val;
617
12.3M
        val = &tval;
618
12.3M
    }
619
620
287M
    if (flags & ASN1_TFLG_SK_MASK) {
621
        /* SET OF, SEQUENCE OF */
622
172M
        int sktag, skaclass;
623
172M
        char sk_eoc;
624
        /* First work out expected inner tag value */
625
172M
        if (flags & ASN1_TFLG_IMPTAG) {
626
1.23M
            sktag = tt->tag;
627
1.23M
            skaclass = aclass;
628
170M
        } else {
629
170M
            skaclass = V_ASN1_UNIVERSAL;
630
170M
            if (flags & ASN1_TFLG_SET_OF)
631
164M
                sktag = V_ASN1_SET;
632
6.48M
            else
633
6.48M
                sktag = V_ASN1_SEQUENCE;
634
170M
        }
635
        /* Get the tag */
636
172M
        ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
637
172M
            &p, len, sktag, skaclass, opt, ctx);
638
172M
        if (!ret) {
639
1.13M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
640
1.13M
            return 0;
641
171M
        } else if (ret == -1)
642
736k
            return -1;
643
170M
        if (*val == NULL)
644
169M
            *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
645
701k
        else {
646
            /*
647
             * We've got a valid STACK: free up any items present
648
             */
649
701k
            STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
650
701k
            ASN1_VALUE *vtmp;
651
701k
            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
701k
        }
656
657
170M
        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
410M
        while (len > 0) {
664
248M
            ASN1_VALUE *skfield;
665
248M
            q = p;
666
            /* See if EOC found */
667
248M
            if (asn1_check_eoc(&p, len)) {
668
6.17M
                if (!sk_eoc) {
669
12.5k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
670
12.5k
                    goto err;
671
12.5k
                }
672
6.15M
                len -= p - q;
673
6.15M
                sk_eoc = 0;
674
6.15M
                break;
675
6.17M
            }
676
241M
            skfield = NULL;
677
241M
            if (asn1_item_embed_d2i(&skfield, &p, len,
678
241M
                    ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
679
241M
                    depth, libctx, propq)
680
241M
                <= 0) {
681
1.72M
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
682
                /* |skfield| may be partially allocated despite failure. */
683
1.72M
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
684
1.72M
                goto err;
685
1.72M
            }
686
240M
            len -= p - q;
687
240M
            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
240M
        }
693
168M
        if (sk_eoc) {
694
15.0k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
695
15.0k
            goto err;
696
15.0k
        }
697
168M
    } else if (flags & ASN1_TFLG_IMPTAG) {
698
        /* IMPLICIT tagging */
699
15.7M
        ret = asn1_item_embed_d2i(val, &p, len,
700
15.7M
            ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
701
15.7M
            ctx, depth, libctx, propq);
702
15.7M
        if (!ret) {
703
475k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
704
475k
            goto err;
705
15.2M
        } else if (ret == -1)
706
12.5M
            return -1;
707
99.4M
    } else {
708
        /* Nothing special */
709
99.4M
        ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
710
99.4M
            -1, 0, opt, ctx, depth, libctx, propq);
711
99.4M
        if (!ret) {
712
7.53M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
713
7.53M
            goto err;
714
91.8M
        } else if (ret == -1)
715
5.91M
            return -1;
716
99.4M
    }
717
718
257M
    *in = p;
719
257M
    return 1;
720
721
9.76M
err:
722
9.76M
    return 0;
723
287M
}
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
145M
{
730
145M
    int ret = 0, utype;
731
145M
    long plen;
732
145M
    char cst, inf, free_cont = 0;
733
145M
    const unsigned char *p;
734
145M
    BUF_MEM buf = { 0, NULL, 0, 0 };
735
145M
    const unsigned char *cont = NULL;
736
145M
    long len;
737
738
145M
    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
145M
    if (it->itype == ASN1_ITYPE_MSTRING) {
744
21.2M
        utype = tag;
745
21.2M
        tag = -1;
746
21.2M
    } else
747
124M
        utype = it->utype;
748
749
145M
    if (utype == V_ASN1_ANY) {
750
        /* If type is ANY need to figure out type from tag */
751
50.7M
        unsigned char oclass;
752
50.7M
        if (tag >= 0) {
753
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
754
0
            return 0;
755
0
        }
756
50.7M
        if (opt) {
757
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
758
0
            return 0;
759
0
        }
760
50.7M
        p = *in;
761
50.7M
        ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
762
50.7M
            &p, inlen, -1, 0, 0, ctx);
763
50.7M
        if (!ret) {
764
35.4k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
765
35.4k
            return 0;
766
35.4k
        }
767
50.7M
        if (oclass != V_ASN1_UNIVERSAL)
768
4.18M
            utype = V_ASN1_OTHER;
769
50.7M
    }
770
145M
    if (tag == -1) {
771
134M
        tag = utype;
772
134M
        aclass = V_ASN1_UNIVERSAL;
773
134M
    }
774
145M
    p = *in;
775
    /* Check header */
776
145M
    ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
777
145M
        &p, inlen, tag, aclass, opt, ctx);
778
145M
    if (!ret) {
779
5.86M
        ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
780
5.86M
        return 0;
781
139M
    } else if (ret == -1)
782
13.5M
        return -1;
783
126M
    ret = 0;
784
    /* SEQUENCE, SET and "OTHER" are left in encoded form */
785
126M
    if ((utype == V_ASN1_SEQUENCE)
786
122M
        || (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
23.4M
        if (utype == V_ASN1_OTHER) {
792
4.18M
            asn1_tlc_clear(ctx);
793
4.18M
        }
794
        /* SEQUENCE and SET must be constructed */
795
19.2M
        else if (!cst) {
796
13.8k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
797
13.8k
            return 0;
798
13.8k
        }
799
800
23.4M
        cont = *in;
801
        /* If indefinite length constructed find the real end */
802
23.4M
        if (inf) {
803
1.33M
            if (!asn1_find_end(&p, plen, inf))
804
104k
                goto err;
805
1.23M
            len = p - cont;
806
22.1M
        } else {
807
22.1M
            len = p - cont + plen;
808
22.1M
            p += plen;
809
22.1M
        }
810
102M
    } else if (cst) {
811
8.76M
        if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
812
8.75M
            || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
813
8.74M
            || utype == V_ASN1_ENUMERATED) {
814
27.0k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
815
27.0k
            return 0;
816
27.0k
        }
817
818
        /* Free any returned 'buf' content */
819
8.74M
        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
8.74M
        if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
827
65.5k
            goto err;
828
65.5k
        }
829
8.67M
        len = buf.length;
830
        /* Append a final null to string */
831
8.67M
        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
8.67M
        buf.data[len] = 0;
836
8.67M
        cont = (const unsigned char *)buf.data;
837
94.1M
    } else {
838
94.1M
        cont = p;
839
94.1M
        len = plen;
840
94.1M
        p += plen;
841
94.1M
    }
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
126M
    if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
846
810k
        goto err;
847
848
125M
    *in = p;
849
125M
    ret = 1;
850
126M
err:
851
126M
    if (free_cont)
852
421k
        OPENSSL_free(buf.data);
853
126M
    return ret;
854
125M
}
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
85.1M
{
861
85.1M
    ASN1_VALUE **opval = NULL;
862
85.1M
    ASN1_STRING *stmp;
863
85.1M
    ASN1_TYPE *typ = NULL;
864
85.1M
    int ret = 0;
865
85.1M
    int ilen = (int)len;
866
85.1M
    const ASN1_PRIMITIVE_FUNCS *pf;
867
85.1M
    ASN1_INTEGER **tint;
868
85.1M
    pf = it->funcs;
869
870
85.1M
    if (pf && pf->prim_c2i) {
871
2.50M
        if (len == (long)ilen)
872
2.50M
            return pf->prim_c2i(pval, cont, ilen, utype, free_cont, it);
873
2.50M
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
874
0
        return 0;
875
2.50M
    }
876
    /* If ANY type clear type and set pointer to internal value */
877
82.6M
    if (it->utype == V_ASN1_ANY) {
878
35.0M
        if (*pval == NULL) {
879
34.9M
            typ = ASN1_TYPE_new();
880
34.9M
            if (typ == NULL)
881
0
                goto err;
882
34.9M
            *pval = (ASN1_VALUE *)typ;
883
34.9M
        } else
884
79.7k
            typ = (ASN1_TYPE *)*pval;
885
886
35.0M
        if (utype != typ->type)
887
35.0M
            ASN1_TYPE_set(typ, utype, NULL);
888
35.0M
        opval = pval;
889
35.0M
        pval = &typ->value.asn1_value;
890
35.0M
    }
891
82.6M
    switch (utype) {
892
21.8M
    case V_ASN1_OBJECT:
893
21.8M
        if (len != (long)ilen
894
21.8M
            || !ossl_c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, ilen))
895
40.4k
            goto err;
896
21.7M
        break;
897
898
21.7M
    case V_ASN1_NULL:
899
628k
        if (len) {
900
3.97k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
901
3.97k
            goto err;
902
3.97k
        }
903
624k
        *pval = (ASN1_VALUE *)1;
904
624k
        break;
905
906
7.76M
    case V_ASN1_BOOLEAN:
907
7.76M
        if (len != 1) {
908
6.55k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
909
6.55k
            goto err;
910
7.75M
        } else {
911
7.75M
            ASN1_BOOLEAN *tbool;
912
7.75M
            tbool = (ASN1_BOOLEAN *)pval;
913
7.75M
            *tbool = *cont;
914
7.75M
        }
915
7.75M
        break;
916
917
7.75M
    case V_ASN1_BIT_STRING:
918
4.39M
        if (!ossl_c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
919
33.6k
            goto err;
920
4.36M
        break;
921
922
7.23M
    case V_ASN1_INTEGER:
923
9.17M
    case V_ASN1_ENUMERATED:
924
9.17M
        tint = (ASN1_INTEGER **)pval;
925
9.17M
        if (!ossl_c2i_ASN1_INTEGER(tint, &cont, len))
926
399k
            goto err;
927
        /* Fixup type to match the expected form */
928
8.77M
        (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
929
8.77M
        break;
930
931
6.16M
    case V_ASN1_OCTET_STRING:
932
8.50M
    case V_ASN1_NUMERICSTRING:
933
10.7M
    case V_ASN1_PRINTABLESTRING:
934
11.4M
    case V_ASN1_T61STRING:
935
11.4M
    case V_ASN1_VIDEOTEXSTRING:
936
12.5M
    case V_ASN1_IA5STRING:
937
14.1M
    case V_ASN1_UTCTIME:
938
14.2M
    case V_ASN1_GENERALIZEDTIME:
939
14.6M
    case V_ASN1_GRAPHICSTRING:
940
14.6M
    case V_ASN1_VISIBLESTRING:
941
14.6M
    case V_ASN1_GENERALSTRING:
942
15.4M
    case V_ASN1_UNIVERSALSTRING:
943
16.4M
    case V_ASN1_BMPSTRING:
944
18.0M
    case V_ASN1_UTF8STRING:
945
22.0M
    case V_ASN1_OTHER:
946
31.9M
    case V_ASN1_SET:
947
34.5M
    case V_ASN1_SEQUENCE:
948
38.8M
    default:
949
38.8M
        if (len != (long)ilen) {
950
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
951
0
            goto err;
952
0
        }
953
38.8M
        if (utype == V_ASN1_BMPSTRING && (len & 1)) {
954
1.82k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
955
1.82k
            goto err;
956
1.82k
        }
957
38.8M
        if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
958
2.24k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
959
2.24k
            goto err;
960
2.24k
        }
961
38.8M
        if (utype == V_ASN1_GENERALIZEDTIME && (len < 15)) {
962
5.38k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_GENERALIZEDTIME_IS_TOO_SHORT);
963
5.38k
            goto err;
964
5.38k
        }
965
38.8M
        if (utype == V_ASN1_UTCTIME && (len < 13)) {
966
2.15k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UTCTIME_IS_TOO_SHORT);
967
2.15k
            goto err;
968
2.15k
        }
969
        /* All based on ASN1_STRING and handled the same */
970
38.8M
        if (*pval == NULL) {
971
22.9M
            stmp = ASN1_STRING_type_new(utype);
972
22.9M
            if (stmp == NULL) {
973
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
974
0
                goto err;
975
0
            }
976
22.9M
            *pval = (ASN1_VALUE *)stmp;
977
22.9M
        } else {
978
15.9M
            stmp = (ASN1_STRING *)*pval;
979
15.9M
            stmp->type = utype;
980
15.9M
        }
981
        /* If we've already allocated a buffer use it */
982
38.8M
        if (*free_cont) {
983
6.35M
            ASN1_STRING_set0(stmp, (unsigned char *)cont /* UGLY CAST! */, ilen);
984
6.35M
            *free_cont = 0;
985
32.4M
        } else {
986
32.4M
            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
32.4M
        }
993
38.8M
        break;
994
82.6M
    }
995
    /* If ASN1_ANY and NULL type fix up value */
996
82.1M
    if (typ && (utype == V_ASN1_NULL))
997
608k
        typ->value.ptr = NULL;
998
999
82.1M
    ret = 1;
1000
82.6M
err:
1001
82.6M
    if (!ret) {
1002
495k
        ASN1_TYPE_free(typ);
1003
495k
        if (opval)
1004
27.5k
            *opval = NULL;
1005
495k
    }
1006
82.6M
    return ret;
1007
82.1M
}
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.33M
{
1018
1.33M
    uint32_t expected_eoc;
1019
1.33M
    long plen;
1020
1.33M
    const unsigned char *p = *in, *q;
1021
    /* If not indefinite length constructed just add length */
1022
1.33M
    if (inf == 0) {
1023
0
        *in += len;
1024
0
        return 1;
1025
0
    }
1026
1.33M
    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
344M
    while (len > 0) {
1034
344M
        if (asn1_check_eoc(&p, len)) {
1035
34.9M
            expected_eoc--;
1036
34.9M
            if (expected_eoc == 0)
1037
1.23M
                break;
1038
33.7M
            len -= 2;
1039
33.7M
            continue;
1040
34.9M
        }
1041
309M
        q = p;
1042
        /* Just read in a header: only care about the length */
1043
309M
        if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1044
309M
                -1, 0, 0, NULL)) {
1045
68.7k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1046
68.7k
            return 0;
1047
68.7k
        }
1048
309M
        if (inf) {
1049
70.0M
            if (expected_eoc == UINT32_MAX) {
1050
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1051
0
                return 0;
1052
0
            }
1053
70.0M
            expected_eoc++;
1054
239M
        } else {
1055
239M
            p += plen;
1056
239M
        }
1057
309M
        len -= p - q;
1058
309M
    }
1059
1.26M
    if (expected_eoc) {
1060
35.8k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1061
35.8k
        return 0;
1062
35.8k
    }
1063
1.23M
    *in = p;
1064
1.23M
    return 1;
1065
1.26M
}
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
2.48M
#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
11.2M
{
1086
11.2M
    const unsigned char *p, *q;
1087
11.2M
    long plen;
1088
11.2M
    char cst, ininf;
1089
11.2M
    p = *in;
1090
11.2M
    inf &= 1;
1091
    /*
1092
     * If no buffer and not indefinite length constructed just pass over the
1093
     * encoded data
1094
     */
1095
11.2M
    if (!buf && !inf) {
1096
0
        *in += len;
1097
0
        return 1;
1098
0
    }
1099
46.0M
    while (len > 0) {
1100
35.9M
        q = p;
1101
        /* Check for EOC */
1102
35.9M
        if (asn1_check_eoc(&p, len)) {
1103
            /*
1104
             * EOC is illegal outside indefinite length constructed form
1105
             */
1106
1.03M
            if (!inf) {
1107
9.97k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
1108
9.97k
                return 0;
1109
9.97k
            }
1110
1.02M
            inf = 0;
1111
1.02M
            break;
1112
1.03M
        }
1113
1114
34.9M
        if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1115
34.9M
                len, tag, aclass, 0, NULL)) {
1116
37.6k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1117
37.6k
            return 0;
1118
37.6k
        }
1119
1120
        /* If indefinite length constructed update max length */
1121
34.9M
        if (cst) {
1122
2.48M
            if (depth >= ASN1_MAX_STRING_NEST) {
1123
3.39k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_ASN1_STRING);
1124
3.39k
                return 0;
1125
3.39k
            }
1126
2.47M
            if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1127
58.8k
                return 0;
1128
32.4M
        } else if (plen && !collect_data(buf, &p, plen))
1129
0
            return 0;
1130
34.8M
        len -= p - q;
1131
34.8M
    }
1132
11.1M
    if (inf) {
1133
14.6k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1134
14.6k
        return 0;
1135
14.6k
    }
1136
11.0M
    *in = p;
1137
11.0M
    return 1;
1138
11.1M
}
1139
1140
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1141
17.1M
{
1142
17.1M
    int len;
1143
17.1M
    if (buf) {
1144
17.1M
        len = buf->length;
1145
17.1M
        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
17.1M
        memcpy(buf->data + len, *p, plen);
1150
17.1M
    }
1151
17.1M
    *p += plen;
1152
17.1M
    return 1;
1153
17.1M
}
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
748M
{
1159
748M
    const unsigned char *p;
1160
1161
748M
    if (len < 2)
1162
194k
        return 0;
1163
748M
    p = *in;
1164
748M
    if (p[0] == '\0' && p[1] == '\0') {
1165
57.3M
        *in += 2;
1166
57.3M
        return 1;
1167
57.3M
    }
1168
691M
    return 0;
1169
748M
}
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
800M
{
1183
800M
    int i;
1184
800M
    int ptag, pclass;
1185
800M
    long plen;
1186
800M
    const unsigned char *p, *q;
1187
800M
    p = *in;
1188
800M
    q = p;
1189
1190
800M
    if (len <= 0) {
1191
995
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
1192
995
        goto err;
1193
995
    }
1194
800M
    if (ctx != NULL && ctx->valid) {
1195
95.9M
        i = ctx->ret;
1196
95.9M
        plen = ctx->plen;
1197
95.9M
        pclass = ctx->pclass;
1198
95.9M
        ptag = ctx->ptag;
1199
95.9M
        p += ctx->hdrlen;
1200
704M
    } else {
1201
704M
        i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1202
704M
        if (ctx != NULL) {
1203
360M
            ctx->ret = i;
1204
360M
            ctx->plen = plen;
1205
360M
            ctx->pclass = pclass;
1206
360M
            ctx->ptag = ptag;
1207
360M
            ctx->hdrlen = p - q;
1208
360M
            ctx->valid = 1;
1209
            /*
1210
             * If definite length, and no error, length + header can't exceed
1211
             * total amount of data available.
1212
             */
1213
360M
            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
360M
        }
1218
704M
    }
1219
1220
800M
    if ((i & 0x80) != 0) {
1221
966k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER);
1222
966k
        goto err;
1223
966k
    }
1224
799M
    if (exptag >= 0) {
1225
378M
        if (exptag != ptag || expclass != pclass) {
1226
            /*
1227
             * If type is OPTIONAL, not an error: indicate missing type.
1228
             */
1229
34.6M
            if (opt != 0)
1230
24.3M
                return -1;
1231
34.6M
            ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG);
1232
10.3M
            goto err;
1233
34.6M
        }
1234
        /*
1235
         * We have a tag and class match: assume we are going to do something
1236
         * with it
1237
         */
1238
343M
        asn1_tlc_clear(ctx);
1239
343M
    }
1240
1241
764M
    if ((i & 1) != 0)
1242
103M
        plen = len - (p - q);
1243
1244
764M
    if (inf != NULL)
1245
692M
        *inf = i & 1;
1246
1247
764M
    if (cst != NULL)
1248
212M
        *cst = i & V_ASN1_CONSTRUCTED;
1249
1250
764M
    if (olen != NULL)
1251
692M
        *olen = plen;
1252
1253
764M
    if (oclass != NULL)
1254
72.5M
        *oclass = pclass;
1255
1256
764M
    if (otag != NULL)
1257
72.5M
        *otag = ptag;
1258
1259
764M
    *in = p;
1260
764M
    return 1;
1261
1262
11.2M
err:
1263
    asn1_tlc_clear(ctx);
1264
11.2M
    return 0;
1265
799M
}