Coverage Report

Created: 2023-06-08 06:41

/src/openssl111/crypto/asn1/tasn_dec.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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 "internal/numbers.h"
18
#include "asn1_local.h"
19
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
15.6M
#define ASN1_MAX_CONSTRUCTED_NEST 30
28
29
static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
30
                               long len, const ASN1_ITEM *it,
31
                               int tag, int aclass, char opt, ASN1_TLC *ctx,
32
                               int depth);
33
34
static int asn1_check_eoc(const unsigned char **in, long len);
35
static int asn1_find_end(const unsigned char **in, long len, char inf);
36
37
static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
38
                        char inf, int tag, int aclass, int depth);
39
40
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
41
42
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
43
                           char *inf, char *cst,
44
                           const unsigned char **in, long len,
45
                           int exptag, int expclass, char opt, ASN1_TLC *ctx);
46
47
static int asn1_template_ex_d2i(ASN1_VALUE **pval,
48
                                const unsigned char **in, long len,
49
                                const ASN1_TEMPLATE *tt, char opt,
50
                                ASN1_TLC *ctx, int depth);
51
static int asn1_template_noexp_d2i(ASN1_VALUE **val,
52
                                   const unsigned char **in, long len,
53
                                   const ASN1_TEMPLATE *tt, char opt,
54
                                   ASN1_TLC *ctx, int depth);
55
static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
56
                                 const unsigned char **in, long len,
57
                                 const ASN1_ITEM *it,
58
                                 int tag, int aclass, char opt,
59
                                 ASN1_TLC *ctx);
60
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
61
                       int utype, char *free_cont, const ASN1_ITEM *it);
62
63
/* Table to convert tags to bit values, used for MSTRING type */
64
static const unsigned long tag2bit[32] = {
65
    /* tags  0 -  3 */
66
    0, 0, 0, B_ASN1_BIT_STRING,
67
    /* tags  4- 7 */
68
    B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,
69
    /* tags  8-11 */
70
    B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
71
    /* tags 12-15 */
72
    B_ASN1_UTF8STRING, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
73
    /* tags 16-19 */
74
    B_ASN1_SEQUENCE, 0, B_ASN1_NUMERICSTRING, B_ASN1_PRINTABLESTRING,
75
    /* tags 20-22 */
76
    B_ASN1_T61STRING, B_ASN1_VIDEOTEXSTRING, B_ASN1_IA5STRING,
77
    /* tags 23-24 */
78
    B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME,
79
    /* tags 25-27 */
80
    B_ASN1_GRAPHICSTRING, B_ASN1_ISO64STRING, B_ASN1_GENERALSTRING,
81
    /* tags 28-31 */
82
    B_ASN1_UNIVERSALSTRING, B_ASN1_UNKNOWN, B_ASN1_BMPSTRING, B_ASN1_UNKNOWN,
83
};
84
85
unsigned long ASN1_tag2bit(int tag)
86
1.36M
{
87
1.36M
    if ((tag < 0) || (tag > 30))
88
1.14k
        return 0;
89
1.36M
    return tag2bit[tag];
90
1.36M
}
91
92
/* Macro to initialize and invalidate the cache */
93
94
10.6M
#define asn1_tlc_clear(c)       if (c) (c)->valid = 0
95
/* Version to avoid compiler warning about 'c' always non-NULL */
96
2.60M
#define asn1_tlc_clear_nc(c)    (c)->valid = 0
97
98
/*
99
 * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
100
 * function. 'in' points to a buffer to read the data from, in future we
101
 * will have more advanced versions that can input data a piece at a time and
102
 * this will simply be a special case.
103
 */
104
105
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
106
                          const unsigned char **in, long len,
107
                          const ASN1_ITEM *it)
108
2.60M
{
109
2.60M
    ASN1_TLC c;
110
2.60M
    ASN1_VALUE *ptmpval = NULL;
111
2.60M
    if (!pval)
112
2.60M
        pval = &ptmpval;
113
2.60M
    asn1_tlc_clear_nc(&c);
114
2.60M
    if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
115
470k
        return *pval;
116
2.13M
    return NULL;
117
2.60M
}
118
119
int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
120
                     const ASN1_ITEM *it,
121
                     int tag, int aclass, char opt, ASN1_TLC *ctx)
122
2.74M
{
123
2.74M
    int rv;
124
2.74M
    rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0);
125
2.74M
    if (rv <= 0)
126
2.17M
        ASN1_item_ex_free(pval, it);
127
2.74M
    return rv;
128
2.74M
}
129
130
/*
131
 * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
132
 * tag mismatch return -1 to handle OPTIONAL
133
 */
134
135
static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
136
                               long len, const ASN1_ITEM *it,
137
                               int tag, int aclass, char opt, ASN1_TLC *ctx,
138
                               int depth)
139
15.6M
{
140
15.6M
    const ASN1_TEMPLATE *tt, *errtt = NULL;
141
15.6M
    const ASN1_EXTERN_FUNCS *ef;
142
15.6M
    const ASN1_AUX *aux = it->funcs;
143
15.6M
    ASN1_aux_cb *asn1_cb;
144
15.6M
    const unsigned char *p = NULL, *q;
145
15.6M
    unsigned char oclass;
146
15.6M
    char seq_eoc, seq_nolen, cst, isopt;
147
15.6M
    long tmplen;
148
15.6M
    int i;
149
15.6M
    int otag;
150
15.6M
    int ret = 0;
151
15.6M
    ASN1_VALUE **pchptr;
152
15.6M
    if (!pval)
153
0
        return 0;
154
15.6M
    if (aux && aux->asn1_cb)
155
2.35M
        asn1_cb = aux->asn1_cb;
156
13.3M
    else
157
13.3M
        asn1_cb = 0;
158
159
15.6M
    if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
160
1
        ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NESTED_TOO_DEEP);
161
1
        goto err;
162
1
    }
163
164
15.6M
    switch (it->itype) {
165
9.05M
    case ASN1_ITYPE_PRIMITIVE:
166
9.05M
        if (it->templates) {
167
            /*
168
             * tagging or OPTIONAL is currently illegal on an item template
169
             * because the flags can't get passed down. In practice this
170
             * isn't a problem: we include the relevant flags from the item
171
             * template in the template itself.
172
             */
173
1.22M
            if ((tag != -1) || opt) {
174
0
                ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I,
175
0
                        ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
176
0
                goto err;
177
0
            }
178
1.22M
            return asn1_template_ex_d2i(pval, in, len,
179
1.22M
                                        it->templates, opt, ctx, depth);
180
1.22M
        }
181
7.83M
        return asn1_d2i_ex_primitive(pval, in, len, it,
182
7.83M
                                     tag, aclass, opt, ctx);
183
184
685k
    case ASN1_ITYPE_MSTRING:
185
        /*
186
         * It never makes sense for multi-strings to have implicit tagging, so
187
         * if tag != -1, then this looks like an error in the template.
188
         */
189
685k
        if (tag != -1) {
190
0
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_BAD_TEMPLATE);
191
0
            goto err;
192
0
        }
193
194
685k
        p = *in;
195
        /* Just read in tag and class */
196
685k
        ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
197
685k
                              &p, len, -1, 0, 1, ctx);
198
685k
        if (!ret) {
199
967
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
200
967
            goto err;
201
967
        }
202
203
        /* Must be UNIVERSAL class */
204
684k
        if (oclass != V_ASN1_UNIVERSAL) {
205
            /* If OPTIONAL, assume this is OK */
206
18.4k
            if (opt)
207
12.1k
                return -1;
208
6.28k
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);
209
6.28k
            goto err;
210
18.4k
        }
211
212
        /* Check tag matches bit map */
213
666k
        if (!(ASN1_tag2bit(otag) & it->utype)) {
214
            /* If OPTIONAL, assume this is OK */
215
70.2k
            if (opt)
216
1.31k
                return -1;
217
68.9k
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_WRONG_TAG);
218
68.9k
            goto err;
219
70.2k
        }
220
595k
        return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
221
222
142k
    case ASN1_ITYPE_EXTERN:
223
        /* Use new style d2i */
224
142k
        ef = it->funcs;
225
142k
        return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
226
227
1.59M
    case ASN1_ITYPE_CHOICE:
228
        /*
229
         * It never makes sense for CHOICE types to have implicit tagging, so
230
         * if tag != -1, then this looks like an error in the template.
231
         */
232
1.59M
        if (tag != -1) {
233
0
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_BAD_TEMPLATE);
234
0
            goto err;
235
0
        }
236
237
1.59M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
238
0
            goto auxerr;
239
1.59M
        if (*pval) {
240
            /* Free up and zero CHOICE value if initialised */
241
28.1k
            i = asn1_get_choice_selector(pval, it);
242
28.1k
            if ((i >= 0) && (i < it->tcount)) {
243
0
                tt = it->templates + i;
244
0
                pchptr = asn1_get_field_ptr(pval, tt);
245
0
                asn1_template_free(pchptr, tt);
246
0
                asn1_set_choice_selector(pval, -1, it);
247
0
            }
248
1.56M
        } else if (!ASN1_item_ex_new(pval, it)) {
249
0
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
250
0
            goto err;
251
0
        }
252
        /* CHOICE type, try each possibility in turn */
253
1.59M
        p = *in;
254
4.98M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
255
4.87M
            pchptr = asn1_get_field_ptr(pval, tt);
256
            /*
257
             * We mark field as OPTIONAL so its absence can be recognised.
258
             */
259
4.87M
            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth);
260
            /* If field not present, try the next one */
261
4.87M
            if (ret == -1)
262
3.38M
                continue;
263
            /* If positive return, read OK, break loop */
264
1.48M
            if (ret > 0)
265
1.39M
                break;
266
            /*
267
             * Must be an ASN1 parsing error.
268
             * Free up any partial choice value
269
             */
270
91.7k
            asn1_template_free(pchptr, tt);
271
91.7k
            errtt = tt;
272
91.7k
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
273
91.7k
            goto err;
274
1.48M
        }
275
276
        /* Did we fall off the end without reading anything? */
277
1.50M
        if (i == it->tcount) {
278
            /* If OPTIONAL, this is OK */
279
111k
            if (opt) {
280
                /* Free and zero it */
281
0
                ASN1_item_ex_free(pval, it);
282
0
                return -1;
283
0
            }
284
111k
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);
285
111k
            goto err;
286
111k
        }
287
288
1.39M
        asn1_set_choice_selector(pval, i, it);
289
290
1.39M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
291
0
            goto auxerr;
292
1.39M
        *in = p;
293
1.39M
        return 1;
294
295
129k
    case ASN1_ITYPE_NDEF_SEQUENCE:
296
4.21M
    case ASN1_ITYPE_SEQUENCE:
297
4.21M
        p = *in;
298
4.21M
        tmplen = len;
299
300
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
301
4.21M
        if (tag == -1) {
302
4.07M
            tag = V_ASN1_SEQUENCE;
303
4.07M
            aclass = V_ASN1_UNIVERSAL;
304
4.07M
        }
305
        /* Get SEQUENCE length and update len, p */
306
4.21M
        ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
307
4.21M
                              &p, len, tag, aclass, opt, ctx);
308
4.21M
        if (!ret) {
309
545k
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
310
545k
            goto err;
311
3.66M
        } else if (ret == -1)
312
1.48M
            return -1;
313
2.18M
        if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
314
0
            len = tmplen - (p - *in);
315
0
            seq_nolen = 1;
316
0
        }
317
        /* If indefinite we don't do a length check */
318
2.18M
        else
319
2.18M
            seq_nolen = seq_eoc;
320
2.18M
        if (!cst) {
321
31.0k
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
322
31.0k
            goto err;
323
31.0k
        }
324
325
2.15M
        if (!*pval && !ASN1_item_ex_new(pval, it)) {
326
0
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
327
0
            goto err;
328
0
        }
329
330
2.15M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
331
0
            goto auxerr;
332
333
        /* Free up and zero any ADB found */
334
8.77M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
335
6.61M
            if (tt->flags & ASN1_TFLG_ADB_MASK) {
336
71.3k
                const ASN1_TEMPLATE *seqtt;
337
71.3k
                ASN1_VALUE **pseqval;
338
71.3k
                seqtt = asn1_do_adb(pval, tt, 0);
339
71.3k
                if (seqtt == NULL)
340
51.0k
                    continue;
341
20.3k
                pseqval = asn1_get_field_ptr(pval, seqtt);
342
20.3k
                asn1_template_free(pseqval, seqtt);
343
20.3k
            }
344
6.61M
        }
345
346
        /* Get each field entry */
347
5.11M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
348
4.28M
            const ASN1_TEMPLATE *seqtt;
349
4.28M
            ASN1_VALUE **pseqval;
350
4.28M
            seqtt = asn1_do_adb(pval, tt, 1);
351
4.28M
            if (seqtt == NULL)
352
0
                goto err;
353
4.28M
            pseqval = asn1_get_field_ptr(pval, seqtt);
354
            /* Have we ran out of data? */
355
4.28M
            if (!len)
356
66.3k
                break;
357
4.21M
            q = p;
358
4.21M
            if (asn1_check_eoc(&p, len)) {
359
147k
                if (!seq_eoc) {
360
766
                    ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_UNEXPECTED_EOC);
361
766
                    goto err;
362
766
                }
363
147k
                len -= p - q;
364
147k
                seq_eoc = 0;
365
147k
                q = p;
366
147k
                break;
367
147k
            }
368
            /*
369
             * This determines the OPTIONAL flag value. The field cannot be
370
             * omitted if it is the last of a SEQUENCE and there is still
371
             * data to be read. This isn't strictly necessary but it
372
             * increases efficiency in some cases.
373
             */
374
4.06M
            if (i == (it->tcount - 1))
375
1.05M
                isopt = 0;
376
3.01M
            else
377
3.01M
                isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
378
            /*
379
             * attempt to read in field, allowing each to be OPTIONAL
380
             */
381
382
4.06M
            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
383
4.06M
                                       depth);
384
4.06M
            if (!ret) {
385
1.10M
                errtt = seqtt;
386
1.10M
                goto err;
387
2.96M
            } else if (ret == -1) {
388
                /*
389
                 * OPTIONAL component absent. Free and zero the field.
390
                 */
391
497k
                asn1_template_free(pseqval, seqtt);
392
497k
                continue;
393
497k
            }
394
            /* Update length */
395
2.46M
            len -= p - q;
396
2.46M
        }
397
398
        /* Check for EOC if expecting one */
399
1.04M
        if (seq_eoc && !asn1_check_eoc(&p, len)) {
400
21.0k
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MISSING_EOC);
401
21.0k
            goto err;
402
21.0k
        }
403
        /* Check all data read */
404
1.02M
        if (!seq_nolen && len) {
405
655
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
406
655
            goto err;
407
655
        }
408
409
        /*
410
         * If we get here we've got no more data in the SEQUENCE, however we
411
         * may not have read all fields so check all remaining are OPTIONAL
412
         * and clear any that are.
413
         */
414
1.35M
        for (; i < it->tcount; tt++, i++) {
415
348k
            const ASN1_TEMPLATE *seqtt;
416
348k
            seqtt = asn1_do_adb(pval, tt, 1);
417
348k
            if (seqtt == NULL)
418
0
                goto err;
419
348k
            if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
420
328k
                ASN1_VALUE **pseqval;
421
328k
                pseqval = asn1_get_field_ptr(pval, seqtt);
422
328k
                asn1_template_free(pseqval, seqtt);
423
328k
            } else {
424
20.0k
                errtt = seqtt;
425
20.0k
                ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_FIELD_MISSING);
426
20.0k
                goto err;
427
20.0k
            }
428
348k
        }
429
        /* Save encoding */
430
1.00M
        if (!asn1_enc_save(pval, *in, p - *in, it))
431
0
            goto auxerr;
432
1.00M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
433
103
            goto auxerr;
434
1.00M
        *in = p;
435
1.00M
        return 1;
436
437
0
    default:
438
0
        return 0;
439
15.6M
    }
440
103
 auxerr:
441
103
    ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_AUX_ERROR);
442
2.00M
 err:
443
2.00M
    if (errtt)
444
1.21M
        ERR_add_error_data(4, "Field=", errtt->field_name,
445
1.21M
                           ", Type=", it->sname);
446
787k
    else
447
787k
        ERR_add_error_data(2, "Type=", it->sname);
448
2.00M
    return 0;
449
103
}
450
451
/*
452
 * Templates are handled with two separate functions. One handles any
453
 * EXPLICIT tag and the other handles the rest.
454
 */
455
456
static int asn1_template_ex_d2i(ASN1_VALUE **val,
457
                                const unsigned char **in, long inlen,
458
                                const ASN1_TEMPLATE *tt, char opt,
459
                                ASN1_TLC *ctx, int depth)
460
10.1M
{
461
10.1M
    int flags, aclass;
462
10.1M
    int ret;
463
10.1M
    long len;
464
10.1M
    const unsigned char *p, *q;
465
10.1M
    char exp_eoc;
466
10.1M
    if (!val)
467
0
        return 0;
468
10.1M
    flags = tt->flags;
469
10.1M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
470
471
10.1M
    p = *in;
472
473
    /* Check if EXPLICIT tag expected */
474
10.1M
    if (flags & ASN1_TFLG_EXPTAG) {
475
391k
        char cst;
476
        /*
477
         * Need to work out amount of data available to the inner content and
478
         * where it starts: so read in EXPLICIT header to get the info.
479
         */
480
391k
        ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
481
391k
                              &p, inlen, tt->tag, aclass, opt, ctx);
482
391k
        q = p;
483
391k
        if (!ret) {
484
52.8k
            ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
485
52.8k
            return 0;
486
338k
        } else if (ret == -1)
487
293k
            return -1;
488
45.1k
        if (!cst) {
489
462
            ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
490
462
                    ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
491
462
            return 0;
492
462
        }
493
        /* We've found the field so it can't be OPTIONAL now */
494
44.6k
        ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth);
495
44.6k
        if (!ret) {
496
14.6k
            ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
497
14.6k
            return 0;
498
14.6k
        }
499
        /* We read the field in OK so update length */
500
29.9k
        len -= p - q;
501
29.9k
        if (exp_eoc) {
502
            /* If NDEF we must have an EOC here */
503
22.7k
            if (!asn1_check_eoc(&p, len)) {
504
1.08k
                ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ASN1_R_MISSING_EOC);
505
1.08k
                goto err;
506
1.08k
            }
507
22.7k
        } else {
508
            /*
509
             * Otherwise we must hit the EXPLICIT tag end or its an error
510
             */
511
7.22k
            if (len) {
512
195
                ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
513
195
                        ASN1_R_EXPLICIT_LENGTH_MISMATCH);
514
195
                goto err;
515
195
            }
516
7.22k
        }
517
29.9k
    } else
518
9.77M
        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth);
519
520
28.7k
    *in = p;
521
28.7k
    return 1;
522
523
1.27k
 err:
524
1.27k
    return 0;
525
10.1M
}
526
527
static int asn1_template_noexp_d2i(ASN1_VALUE **val,
528
                                   const unsigned char **in, long len,
529
                                   const ASN1_TEMPLATE *tt, char opt,
530
                                   ASN1_TLC *ctx, int depth)
531
9.81M
{
532
9.81M
    int flags, aclass;
533
9.81M
    int ret;
534
9.81M
    ASN1_VALUE *tval;
535
9.81M
    const unsigned char *p, *q;
536
9.81M
    if (!val)
537
0
        return 0;
538
9.81M
    flags = tt->flags;
539
9.81M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
540
541
9.81M
    p = *in;
542
9.81M
    q = p;
543
544
    /*
545
     * If field is embedded then val needs fixing so it is a pointer to
546
     * a pointer to a field.
547
     */
548
9.81M
    if (tt->flags & ASN1_TFLG_EMBED) {
549
581k
        tval = (ASN1_VALUE *)val;
550
581k
        val = &tval;
551
581k
    }
552
553
9.81M
    if (flags & ASN1_TFLG_SK_MASK) {
554
        /* SET OF, SEQUENCE OF */
555
1.46M
        int sktag, skaclass;
556
1.46M
        char sk_eoc;
557
        /* First work out expected inner tag value */
558
1.46M
        if (flags & ASN1_TFLG_IMPTAG) {
559
123k
            sktag = tt->tag;
560
123k
            skaclass = aclass;
561
1.34M
        } else {
562
1.34M
            skaclass = V_ASN1_UNIVERSAL;
563
1.34M
            if (flags & ASN1_TFLG_SET_OF)
564
927k
                sktag = V_ASN1_SET;
565
414k
            else
566
414k
                sktag = V_ASN1_SEQUENCE;
567
1.34M
        }
568
        /* Get the tag */
569
1.46M
        ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
570
1.46M
                              &p, len, sktag, skaclass, opt, ctx);
571
1.46M
        if (!ret) {
572
185k
            ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
573
185k
            return 0;
574
1.28M
        } else if (ret == -1)
575
90.4k
            return -1;
576
1.18M
        if (!*val)
577
1.17M
            *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
578
12.8k
        else {
579
            /*
580
             * We've got a valid STACK: free up any items present
581
             */
582
12.8k
            STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
583
12.8k
            ASN1_VALUE *vtmp;
584
12.8k
            while (sk_ASN1_VALUE_num(sktmp) > 0) {
585
0
                vtmp = sk_ASN1_VALUE_pop(sktmp);
586
0
                ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
587
0
            }
588
12.8k
        }
589
590
1.18M
        if (!*val) {
591
0
            ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
592
0
            goto err;
593
0
        }
594
595
        /* Read as many items as we can */
596
5.62M
        while (len > 0) {
597
4.63M
            ASN1_VALUE *skfield;
598
4.63M
            q = p;
599
            /* See if EOC found */
600
4.63M
            if (asn1_check_eoc(&p, len)) {
601
43.8k
                if (!sk_eoc) {
602
451
                    ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
603
451
                            ASN1_R_UNEXPECTED_EOC);
604
451
                    goto err;
605
451
                }
606
43.3k
                len -= p - q;
607
43.3k
                sk_eoc = 0;
608
43.3k
                break;
609
43.8k
            }
610
4.59M
            skfield = NULL;
611
4.59M
            if (!asn1_item_embed_d2i(&skfield, &p, len,
612
4.59M
                                     ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
613
4.59M
                                     depth)) {
614
163k
                ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
615
163k
                        ERR_R_NESTED_ASN1_ERROR);
616
                /* |skfield| may be partially allocated despite failure. */
617
163k
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
618
163k
                goto err;
619
163k
            }
620
4.43M
            len -= p - q;
621
4.43M
            if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
622
0
                ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_MALLOC_FAILURE);
623
0
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
624
0
                goto err;
625
0
            }
626
4.43M
        }
627
1.02M
        if (sk_eoc) {
628
1.45k
            ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
629
1.45k
            goto err;
630
1.45k
        }
631
8.35M
    } else if (flags & ASN1_TFLG_IMPTAG) {
632
        /* IMPLICIT tagging */
633
3.30M
        ret = asn1_item_embed_d2i(val, &p, len,
634
3.30M
                                  ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
635
3.30M
                                  ctx, depth);
636
3.30M
        if (!ret) {
637
58.4k
            ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
638
58.4k
            goto err;
639
3.24M
        } else if (ret == -1)
640
1.90M
            return -1;
641
5.04M
    } else {
642
        /* Nothing special */
643
5.04M
        ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
644
5.04M
                                  -1, 0, opt, ctx, depth);
645
5.04M
        if (!ret) {
646
972k
            ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
647
972k
            goto err;
648
4.07M
        } else if (ret == -1)
649
1.59M
            return -1;
650
5.04M
    }
651
652
4.84M
    *in = p;
653
4.84M
    return 1;
654
655
1.19M
 err:
656
1.19M
    return 0;
657
9.81M
}
658
659
static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
660
                                 const unsigned char **in, long inlen,
661
                                 const ASN1_ITEM *it,
662
                                 int tag, int aclass, char opt, ASN1_TLC *ctx)
663
8.43M
{
664
8.43M
    int ret = 0, utype;
665
8.43M
    long plen;
666
8.43M
    char cst, inf, free_cont = 0;
667
8.43M
    const unsigned char *p;
668
8.43M
    BUF_MEM buf = { 0, NULL, 0, 0 };
669
8.43M
    const unsigned char *cont = NULL;
670
8.43M
    long len;
671
8.43M
    if (!pval) {
672
0
        ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
673
0
        return 0;               /* Should never happen */
674
0
    }
675
676
8.43M
    if (it->itype == ASN1_ITYPE_MSTRING) {
677
595k
        utype = tag;
678
595k
        tag = -1;
679
595k
    } else
680
7.83M
        utype = it->utype;
681
682
8.43M
    if (utype == V_ASN1_ANY) {
683
        /* If type is ANY need to figure out type from tag */
684
1.66M
        unsigned char oclass;
685
1.66M
        if (tag >= 0) {
686
0
            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
687
0
            return 0;
688
0
        }
689
1.66M
        if (opt) {
690
0
            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
691
0
                    ASN1_R_ILLEGAL_OPTIONAL_ANY);
692
0
            return 0;
693
0
        }
694
1.66M
        p = *in;
695
1.66M
        ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
696
1.66M
                              &p, inlen, -1, 0, 0, ctx);
697
1.66M
        if (!ret) {
698
715
            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
699
715
            return 0;
700
715
        }
701
1.66M
        if (oclass != V_ASN1_UNIVERSAL)
702
1.13M
            utype = V_ASN1_OTHER;
703
1.66M
    }
704
8.43M
    if (tag == -1) {
705
5.26M
        tag = utype;
706
5.26M
        aclass = V_ASN1_UNIVERSAL;
707
5.26M
    }
708
8.43M
    p = *in;
709
    /* Check header */
710
8.43M
    ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
711
8.43M
                          &p, inlen, tag, aclass, opt, ctx);
712
8.43M
    if (!ret) {
713
1.01M
        ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
714
1.01M
        return 0;
715
7.41M
    } else if (ret == -1)
716
2.00M
        return -1;
717
5.40M
    ret = 0;
718
    /* SEQUENCE, SET and "OTHER" are left in encoded form */
719
5.40M
    if ((utype == V_ASN1_SEQUENCE)
720
5.40M
        || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
721
        /*
722
         * Clear context cache for type OTHER because the auto clear when we
723
         * have a exact match won't work
724
         */
725
2.61M
        if (utype == V_ASN1_OTHER) {
726
1.13M
            asn1_tlc_clear(ctx);
727
1.13M
        }
728
        /* SEQUENCE and SET must be constructed */
729
1.47M
        else if (!cst) {
730
1.84k
            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
731
1.84k
                    ASN1_R_TYPE_NOT_CONSTRUCTED);
732
1.84k
            return 0;
733
1.84k
        }
734
735
2.61M
        cont = *in;
736
        /* If indefinite length constructed find the real end */
737
2.61M
        if (inf) {
738
100k
            if (!asn1_find_end(&p, plen, inf))
739
23.0k
                goto err;
740
77.6k
            len = p - cont;
741
2.51M
        } else {
742
2.51M
            len = p - cont + plen;
743
2.51M
            p += plen;
744
2.51M
        }
745
2.79M
    } else if (cst) {
746
253k
        if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
747
253k
            || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
748
253k
            || utype == V_ASN1_ENUMERATED) {
749
1.34k
            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_PRIMITIVE);
750
1.34k
            return 0;
751
1.34k
        }
752
753
        /* Free any returned 'buf' content */
754
252k
        free_cont = 1;
755
        /*
756
         * Should really check the internal tags are correct but some things
757
         * may get this wrong. The relevant specs say that constructed string
758
         * types should be OCTET STRINGs internally irrespective of the type.
759
         * So instead just check for UNIVERSAL class and ignore the tag.
760
         */
761
252k
        if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
762
1.57k
            goto err;
763
1.57k
        }
764
250k
        len = buf.length;
765
        /* Append a final null to string */
766
250k
        if (!BUF_MEM_grow_clean(&buf, len + 1)) {
767
0
            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_MALLOC_FAILURE);
768
0
            goto err;
769
0
        }
770
250k
        buf.data[len] = 0;
771
250k
        cont = (const unsigned char *)buf.data;
772
2.54M
    } else {
773
2.54M
        cont = p;
774
2.54M
        len = plen;
775
2.54M
        p += plen;
776
2.54M
    }
777
778
    /* We now have content length and type: translate into a structure */
779
    /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
780
5.38M
    if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
781
43.7k
        goto err;
782
783
5.33M
    *in = p;
784
5.33M
    ret = 1;
785
5.40M
 err:
786
5.40M
    if (free_cont)
787
1.88k
        OPENSSL_free(buf.data);
788
5.40M
    return ret;
789
5.33M
}
790
791
/* Translate ASN1 content octets into a structure */
792
793
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
794
                       int utype, char *free_cont, const ASN1_ITEM *it)
795
5.38M
{
796
5.38M
    ASN1_VALUE **opval = NULL;
797
5.38M
    ASN1_STRING *stmp;
798
5.38M
    ASN1_TYPE *typ = NULL;
799
5.38M
    int ret = 0;
800
5.38M
    const ASN1_PRIMITIVE_FUNCS *pf;
801
5.38M
    ASN1_INTEGER **tint;
802
5.38M
    pf = it->funcs;
803
804
5.38M
    if (pf && pf->prim_c2i)
805
227k
        return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
806
    /* If ANY type clear type and set pointer to internal value */
807
5.15M
    if (it->utype == V_ASN1_ANY) {
808
1.64M
        if (!*pval) {
809
1.63M
            typ = ASN1_TYPE_new();
810
1.63M
            if (typ == NULL)
811
0
                goto err;
812
1.63M
            *pval = (ASN1_VALUE *)typ;
813
1.63M
        } else
814
10.3k
            typ = (ASN1_TYPE *)*pval;
815
816
1.64M
        if (utype != typ->type)
817
1.64M
            ASN1_TYPE_set(typ, utype, NULL);
818
1.64M
        opval = pval;
819
1.64M
        pval = &typ->value.asn1_value;
820
1.64M
    }
821
5.15M
    switch (utype) {
822
821k
    case V_ASN1_OBJECT:
823
821k
        if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
824
762
            goto err;
825
820k
        break;
826
827
820k
    case V_ASN1_NULL:
828
287k
        if (len) {
829
46
            ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_NULL_IS_WRONG_LENGTH);
830
46
            goto err;
831
46
        }
832
287k
        *pval = (ASN1_VALUE *)1;
833
287k
        break;
834
835
9.81k
    case V_ASN1_BOOLEAN:
836
9.81k
        if (len != 1) {
837
136
            ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
838
136
            goto err;
839
9.67k
        } else {
840
9.67k
            ASN1_BOOLEAN *tbool;
841
9.67k
            tbool = (ASN1_BOOLEAN *)pval;
842
9.67k
            *tbool = *cont;
843
9.67k
        }
844
9.67k
        break;
845
846
104k
    case V_ASN1_BIT_STRING:
847
104k
        if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
848
366
            goto err;
849
103k
        break;
850
851
488k
    case V_ASN1_INTEGER:
852
500k
    case V_ASN1_ENUMERATED:
853
500k
        tint = (ASN1_INTEGER **)pval;
854
500k
        if (!c2i_ASN1_INTEGER(tint, &cont, len))
855
33.8k
            goto err;
856
        /* Fixup type to match the expected form */
857
466k
        (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
858
466k
        break;
859
860
221k
    case V_ASN1_OCTET_STRING:
861
222k
    case V_ASN1_NUMERICSTRING:
862
227k
    case V_ASN1_PRINTABLESTRING:
863
236k
    case V_ASN1_T61STRING:
864
237k
    case V_ASN1_VIDEOTEXSTRING:
865
378k
    case V_ASN1_IA5STRING:
866
422k
    case V_ASN1_UTCTIME:
867
481k
    case V_ASN1_GENERALIZEDTIME:
868
481k
    case V_ASN1_GRAPHICSTRING:
869
482k
    case V_ASN1_VISIBLESTRING:
870
482k
    case V_ASN1_GENERALSTRING:
871
488k
    case V_ASN1_UNIVERSALSTRING:
872
716k
    case V_ASN1_BMPSTRING:
873
727k
    case V_ASN1_UTF8STRING:
874
1.85M
    case V_ASN1_OTHER:
875
1.86M
    case V_ASN1_SET:
876
3.31M
    case V_ASN1_SEQUENCE:
877
3.43M
    default:
878
3.43M
        if (utype == V_ASN1_BMPSTRING && (len & 1)) {
879
69
            ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
880
69
            goto err;
881
69
        }
882
3.43M
        if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
883
20
            ASN1err(ASN1_F_ASN1_EX_C2I,
884
20
                    ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
885
20
            goto err;
886
20
        }
887
        /* All based on ASN1_STRING and handled the same */
888
3.43M
        if (!*pval) {
889
2.77M
            stmp = ASN1_STRING_type_new(utype);
890
2.77M
            if (stmp == NULL) {
891
0
                ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
892
0
                goto err;
893
0
            }
894
2.77M
            *pval = (ASN1_VALUE *)stmp;
895
2.77M
        } else {
896
658k
            stmp = (ASN1_STRING *)*pval;
897
658k
            stmp->type = utype;
898
658k
        }
899
        /* If we've already allocated a buffer use it */
900
3.43M
        if (*free_cont) {
901
250k
            OPENSSL_free(stmp->data);
902
250k
            stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
903
250k
            stmp->length = len;
904
250k
            *free_cont = 0;
905
3.18M
        } else {
906
3.18M
            if (!ASN1_STRING_set(stmp, cont, len)) {
907
0
                ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
908
0
                ASN1_STRING_free(stmp);
909
0
                *pval = NULL;
910
0
                goto err;
911
0
            }
912
3.18M
        }
913
3.43M
        break;
914
5.15M
    }
915
    /* If ASN1_ANY and NULL type fix up value */
916
5.11M
    if (typ && (utype == V_ASN1_NULL))
917
287k
        typ->value.ptr = NULL;
918
919
5.11M
    ret = 1;
920
5.15M
 err:
921
5.15M
    if (!ret) {
922
35.2k
        ASN1_TYPE_free(typ);
923
35.2k
        if (opval)
924
1.97k
            *opval = NULL;
925
35.2k
    }
926
5.15M
    return ret;
927
5.11M
}
928
929
/*
930
 * This function finds the end of an ASN1 structure when passed its maximum
931
 * length, whether it is indefinite length and a pointer to the content. This
932
 * is more efficient than calling asn1_collect because it does not recurse on
933
 * each indefinite length header.
934
 */
935
936
static int asn1_find_end(const unsigned char **in, long len, char inf)
937
100k
{
938
100k
    uint32_t expected_eoc;
939
100k
    long plen;
940
100k
    const unsigned char *p = *in, *q;
941
    /* If not indefinite length constructed just add length */
942
100k
    if (inf == 0) {
943
0
        *in += len;
944
0
        return 1;
945
0
    }
946
100k
    expected_eoc = 1;
947
    /*
948
     * Indefinite length constructed form. Find the end when enough EOCs are
949
     * found. If more indefinite length constructed headers are encountered
950
     * increment the expected eoc count otherwise just skip to the end of the
951
     * data.
952
     */
953
70.4M
    while (len > 0) {
954
70.4M
        if (asn1_check_eoc(&p, len)) {
955
4.07M
            expected_eoc--;
956
4.07M
            if (expected_eoc == 0)
957
77.6k
                break;
958
3.99M
            len -= 2;
959
3.99M
            continue;
960
4.07M
        }
961
66.3M
        q = p;
962
        /* Just read in a header: only care about the length */
963
66.3M
        if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
964
66.3M
                             -1, 0, 0, NULL)) {
965
14.0k
            ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
966
14.0k
            return 0;
967
14.0k
        }
968
66.3M
        if (inf) {
969
12.0M
            if (expected_eoc == UINT32_MAX) {
970
0
                ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
971
0
                return 0;
972
0
            }
973
12.0M
            expected_eoc++;
974
54.2M
        } else {
975
54.2M
            p += plen;
976
54.2M
        }
977
66.3M
        len -= p - q;
978
66.3M
    }
979
86.6k
    if (expected_eoc) {
980
8.99k
        ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
981
8.99k
        return 0;
982
8.99k
    }
983
77.6k
    *in = p;
984
77.6k
    return 1;
985
86.6k
}
986
987
/*
988
 * This function collects the asn1 data from a constructed string type into
989
 * a buffer. The values of 'in' and 'len' should refer to the contents of the
990
 * constructed type and 'inf' should be set if it is indefinite length.
991
 */
992
993
#ifndef ASN1_MAX_STRING_NEST
994
/*
995
 * This determines how many levels of recursion are permitted in ASN1 string
996
 * types. If it is not limited stack overflows can occur. If set to zero no
997
 * recursion is allowed at all. Although zero should be adequate examples
998
 * exist that require a value of 1. So 5 should be more than enough.
999
 */
1000
15.2k
# define ASN1_MAX_STRING_NEST 5
1001
#endif
1002
1003
static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1004
                        char inf, int tag, int aclass, int depth)
1005
267k
{
1006
267k
    const unsigned char *p, *q;
1007
267k
    long plen;
1008
267k
    char cst, ininf;
1009
267k
    p = *in;
1010
267k
    inf &= 1;
1011
    /*
1012
     * If no buffer and not indefinite length constructed just pass over the
1013
     * encoded data
1014
     */
1015
267k
    if (!buf && !inf) {
1016
0
        *in += len;
1017
0
        return 1;
1018
0
    }
1019
888k
    while (len > 0) {
1020
637k
        q = p;
1021
        /* Check for EOC */
1022
637k
        if (asn1_check_eoc(&p, len)) {
1023
            /*
1024
             * EOC is illegal outside indefinite length constructed form
1025
             */
1026
14.3k
            if (!inf) {
1027
229
                ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_UNEXPECTED_EOC);
1028
229
                return 0;
1029
229
            }
1030
14.1k
            inf = 0;
1031
14.1k
            break;
1032
14.3k
        }
1033
1034
623k
        if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1035
623k
                             len, tag, aclass, 0, NULL)) {
1036
698
            ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1037
698
            return 0;
1038
698
        }
1039
1040
        /* If indefinite length constructed update max length */
1041
622k
        if (cst) {
1042
15.2k
            if (depth >= ASN1_MAX_STRING_NEST) {
1043
103
                ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
1044
103
                return 0;
1045
103
            }
1046
15.1k
            if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1047
1.23k
                return 0;
1048
607k
        } else if (plen && !collect_data(buf, &p, plen))
1049
0
            return 0;
1050
621k
        len -= p - q;
1051
621k
    }
1052
265k
    if (inf) {
1053
542
        ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1054
542
        return 0;
1055
542
    }
1056
264k
    *in = p;
1057
264k
    return 1;
1058
265k
}
1059
1060
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1061
590k
{
1062
590k
    int len;
1063
590k
    if (buf) {
1064
590k
        len = buf->length;
1065
590k
        if (!BUF_MEM_grow_clean(buf, len + plen)) {
1066
0
            ASN1err(ASN1_F_COLLECT_DATA, ERR_R_MALLOC_FAILURE);
1067
0
            return 0;
1068
0
        }
1069
590k
        memcpy(buf->data + len, *p, plen);
1070
590k
    }
1071
590k
    *p += plen;
1072
590k
    return 1;
1073
590k
}
1074
1075
/* Check for ASN1 EOC and swallow it if found */
1076
1077
static int asn1_check_eoc(const unsigned char **in, long len)
1078
80.2M
{
1079
80.2M
    const unsigned char *p;
1080
80.2M
    if (len < 2)
1081
9.26k
        return 0;
1082
80.2M
    p = *in;
1083
80.2M
    if (!p[0] && !p[1]) {
1084
4.58M
        *in += 2;
1085
4.58M
        return 1;
1086
4.58M
    }
1087
75.6M
    return 0;
1088
80.2M
}
1089
1090
/*
1091
 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1092
 * length for indefinite length constructed form, we don't know the exact
1093
 * length but we can set an upper bound to the amount of data available minus
1094
 * the header length just read.
1095
 */
1096
1097
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1098
                           char *inf, char *cst,
1099
                           const unsigned char **in, long len,
1100
                           int exptag, int expclass, char opt, ASN1_TLC *ctx)
1101
83.8M
{
1102
83.8M
    int i;
1103
83.8M
    int ptag, pclass;
1104
83.8M
    long plen;
1105
83.8M
    const unsigned char *p, *q;
1106
83.8M
    p = *in;
1107
83.8M
    q = p;
1108
1109
83.8M
    if (ctx && ctx->valid) {
1110
6.03M
        i = ctx->ret;
1111
6.03M
        plen = ctx->plen;
1112
6.03M
        pclass = ctx->pclass;
1113
6.03M
        ptag = ctx->ptag;
1114
6.03M
        p += ctx->hdrlen;
1115
77.8M
    } else {
1116
77.8M
        i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1117
77.8M
        if (ctx) {
1118
10.8M
            ctx->ret = i;
1119
10.8M
            ctx->plen = plen;
1120
10.8M
            ctx->pclass = pclass;
1121
10.8M
            ctx->ptag = ptag;
1122
10.8M
            ctx->hdrlen = p - q;
1123
10.8M
            ctx->valid = 1;
1124
            /*
1125
             * If definite length, and no error, length + header can't exceed
1126
             * total amount of data available.
1127
             */
1128
10.8M
            if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
1129
0
                ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_TOO_LONG);
1130
0
                asn1_tlc_clear(ctx);
1131
0
                return 0;
1132
0
            }
1133
10.8M
        }
1134
77.8M
    }
1135
1136
83.8M
    if (i & 0x80) {
1137
71.0k
        ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1138
71.0k
        asn1_tlc_clear(ctx);
1139
71.0k
        return 0;
1140
71.0k
    }
1141
83.7M
    if (exptag >= 0) {
1142
13.3M
        if ((exptag != ptag) || (expclass != pclass)) {
1143
            /*
1144
             * If type is OPTIONAL, not an error: indicate missing type.
1145
             */
1146
5.61M
            if (opt)
1147
3.87M
                return -1;
1148
1.74M
            asn1_tlc_clear(ctx);
1149
1.74M
            ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1150
1.74M
            return 0;
1151
5.61M
        }
1152
        /*
1153
         * We have a tag and class match: assume we are going to do something
1154
         * with it
1155
         */
1156
7.69M
        asn1_tlc_clear(ctx);
1157
7.69M
    }
1158
1159
78.1M
    if (i & 1)
1160
13.9M
        plen = len - (p - q);
1161
1162
78.1M
    if (inf)
1163
75.8M
        *inf = i & 1;
1164
1165
78.1M
    if (cst)
1166
8.26M
        *cst = i & V_ASN1_CONSTRUCTED;
1167
1168
78.1M
    if (olen)
1169
75.8M
        *olen = plen;
1170
1171
78.1M
    if (oclass)
1172
2.35M
        *oclass = pclass;
1173
1174
78.1M
    if (otag)
1175
2.35M
        *otag = ptag;
1176
1177
78.1M
    *in = p;
1178
78.1M
    return 1;
1179
83.7M
}