Coverage Report

Created: 2018-08-29 13:53

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