Coverage Report

Created: 2023-09-25 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
218k
#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
37.9k
{
87
37.9k
    if ((tag < 0) || (tag > 30))
88
323
        return 0;
89
37.6k
    return tag2bit[tag];
90
37.9k
}
91
92
/* Macro to initialize and invalidate the cache */
93
94
203k
#define asn1_tlc_clear(c)       if (c) (c)->valid = 0
95
/* Version to avoid compiler warning about 'c' always non-NULL */
96
14.4k
#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
14.4k
{
109
14.4k
    ASN1_TLC c;
110
14.4k
    ASN1_VALUE *ptmpval = NULL;
111
14.4k
    if (!pval)
112
13.8k
        pval = &ptmpval;
113
14.4k
    asn1_tlc_clear_nc(&c);
114
14.4k
    if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
115
10.0k
        return *pval;
116
4.45k
    return NULL;
117
14.4k
}
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
22.8k
{
123
22.8k
    int rv;
124
22.8k
    rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0);
125
22.8k
    if (rv <= 0)
126
4.64k
        ASN1_item_ex_free(pval, it);
127
22.8k
    return rv;
128
22.8k
}
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
218k
{
140
218k
    const ASN1_TEMPLATE *tt, *errtt = NULL;
141
218k
    const ASN1_EXTERN_FUNCS *ef;
142
218k
    const ASN1_AUX *aux = it->funcs;
143
218k
    ASN1_aux_cb *asn1_cb;
144
218k
    const unsigned char *p = NULL, *q;
145
218k
    unsigned char oclass;
146
218k
    char seq_eoc, seq_nolen, cst, isopt;
147
218k
    long tmplen;
148
218k
    int i;
149
218k
    int otag;
150
218k
    int ret = 0;
151
218k
    ASN1_VALUE **pchptr;
152
218k
    if (!pval)
153
0
        return 0;
154
218k
    if (aux && aux->asn1_cb)
155
18.1k
        asn1_cb = aux->asn1_cb;
156
200k
    else
157
200k
        asn1_cb = 0;
158
159
218k
    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
164
218k
    switch (it->itype) {
165
126k
    case ASN1_ITYPE_PRIMITIVE:
166
126k
        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
26.3k
            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
26.3k
            return asn1_template_ex_d2i(pval, in, len,
179
26.3k
                                        it->templates, opt, ctx, depth);
180
26.3k
        }
181
99.6k
        return asn1_d2i_ex_primitive(pval, in, len, it,
182
99.6k
                                     tag, aclass, opt, ctx);
183
184
23.0k
    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
23.0k
        if (tag != -1) {
190
0
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_BAD_TEMPLATE);
191
0
            goto err;
192
0
        }
193
194
23.0k
        p = *in;
195
        /* Just read in tag and class */
196
23.0k
        ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
197
23.0k
                              &p, len, -1, 0, 1, ctx);
198
23.0k
        if (!ret) {
199
12
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
200
12
            goto err;
201
12
        }
202
203
        /* Must be UNIVERSAL class */
204
23.0k
        if (oclass != V_ASN1_UNIVERSAL) {
205
            /* If OPTIONAL, assume this is OK */
206
4
            if (opt)
207
0
                return -1;
208
4
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_NOT_UNIVERSAL);
209
4
            goto err;
210
4
        }
211
212
        /* Check tag matches bit map */
213
23.0k
        if (!(ASN1_tag2bit(otag) & it->utype)) {
214
            /* If OPTIONAL, assume this is OK */
215
44
            if (opt)
216
0
                return -1;
217
44
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MSTRING_WRONG_TAG);
218
44
            goto err;
219
44
        }
220
22.9k
        return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
221
222
8.37k
    case ASN1_ITYPE_EXTERN:
223
        /* Use new style d2i */
224
8.37k
        ef = it->funcs;
225
8.37k
        return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
226
227
994
    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
994
        if (tag != -1) {
233
0
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_BAD_TEMPLATE);
234
0
            goto err;
235
0
        }
236
237
994
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
238
0
            goto auxerr;
239
994
        if (*pval) {
240
            /* Free up and zero CHOICE value if initialised */
241
40
            i = asn1_get_choice_selector(pval, it);
242
40
            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
954
        } 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
994
        p = *in;
254
2.89k
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
255
2.84k
            pchptr = asn1_get_field_ptr(pval, tt);
256
            /*
257
             * We mark field as OPTIONAL so its absence can be recognised.
258
             */
259
2.84k
            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth);
260
            /* If field not present, try the next one */
261
2.84k
            if (ret == -1)
262
1.90k
                continue;
263
            /* If positive return, read OK, break loop */
264
941
            if (ret > 0)
265
221
                break;
266
            /*
267
             * Must be an ASN1 parsing error.
268
             * Free up any partial choice value
269
             */
270
720
            asn1_template_free(pchptr, tt);
271
720
            errtt = tt;
272
720
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
273
720
            goto err;
274
941
        }
275
276
        /* Did we fall off the end without reading anything? */
277
274
        if (i == it->tcount) {
278
            /* If OPTIONAL, this is OK */
279
53
            if (opt) {
280
                /* Free and zero it */
281
0
                ASN1_item_ex_free(pval, it);
282
0
                return -1;
283
0
            }
284
53
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_NO_MATCHING_CHOICE_TYPE);
285
53
            goto err;
286
53
        }
287
288
221
        asn1_set_choice_selector(pval, i, it);
289
290
221
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
291
0
            goto auxerr;
292
221
        *in = p;
293
221
        return 1;
294
295
0
    case ASN1_ITYPE_NDEF_SEQUENCE:
296
60.1k
    case ASN1_ITYPE_SEQUENCE:
297
60.1k
        p = *in;
298
60.1k
        tmplen = len;
299
300
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
301
60.1k
        if (tag == -1) {
302
59.7k
            tag = V_ASN1_SEQUENCE;
303
59.7k
            aclass = V_ASN1_UNIVERSAL;
304
59.7k
        }
305
        /* Get SEQUENCE length and update len, p */
306
60.1k
        ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
307
60.1k
                              &p, len, tag, aclass, opt, ctx);
308
60.1k
        if (!ret) {
309
1.21k
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ERR_R_NESTED_ASN1_ERROR);
310
1.21k
            goto err;
311
58.9k
        } else if (ret == -1)
312
395
            return -1;
313
58.5k
        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
58.5k
        else
319
58.5k
            seq_nolen = seq_eoc;
320
58.5k
        if (!cst) {
321
23
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
322
23
            goto err;
323
23
        }
324
325
58.5k
        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
58.5k
        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
228k
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
335
169k
            if (tt->flags & ASN1_TFLG_ADB_MASK) {
336
194
                const ASN1_TEMPLATE *seqtt;
337
194
                ASN1_VALUE **pseqval;
338
194
                seqtt = asn1_do_adb(pval, tt, 0);
339
194
                if (seqtt == NULL)
340
194
                    continue;
341
0
                pseqval = asn1_get_field_ptr(pval, seqtt);
342
0
                asn1_template_free(pseqval, seqtt);
343
0
            }
344
169k
        }
345
346
        /* Get each field entry */
347
208k
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
348
158k
            const ASN1_TEMPLATE *seqtt;
349
158k
            ASN1_VALUE **pseqval;
350
158k
            seqtt = asn1_do_adb(pval, tt, 1);
351
158k
            if (seqtt == NULL)
352
0
                goto err;
353
158k
            pseqval = asn1_get_field_ptr(pval, seqtt);
354
            /* Have we ran out of data? */
355
158k
            if (!len)
356
3.72k
                break;
357
154k
            q = p;
358
154k
            if (asn1_check_eoc(&p, len)) {
359
252
                if (!seq_eoc) {
360
191
                    ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_UNEXPECTED_EOC);
361
191
                    goto err;
362
191
                }
363
61
                len -= p - q;
364
61
                seq_eoc = 0;
365
61
                q = p;
366
61
                break;
367
252
            }
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
154k
            if (i == (it->tcount - 1))
375
51.4k
                isopt = 0;
376
102k
            else
377
102k
                isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
378
            /*
379
             * attempt to read in field, allowing each to be OPTIONAL
380
             */
381
382
154k
            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
383
154k
                                       depth);
384
154k
            if (!ret) {
385
4.06k
                errtt = seqtt;
386
4.06k
                goto err;
387
150k
            } else if (ret == -1) {
388
                /*
389
                 * OPTIONAL component absent. Free and zero the field.
390
                 */
391
16.0k
                asn1_template_free(pseqval, seqtt);
392
16.0k
                continue;
393
16.0k
            }
394
            /* Update length */
395
134k
            len -= p - q;
396
134k
        }
397
398
        /* Check for EOC if expecting one */
399
54.3k
        if (seq_eoc && !asn1_check_eoc(&p, len)) {
400
141
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_MISSING_EOC);
401
141
            goto err;
402
141
        }
403
        /* Check all data read */
404
54.1k
        if (!seq_nolen && len) {
405
46
            ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
406
46
            goto err;
407
46
        }
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
58.9k
        for (; i < it->tcount; tt++, i++) {
415
4.99k
            const ASN1_TEMPLATE *seqtt;
416
4.99k
            seqtt = asn1_do_adb(pval, tt, 1);
417
4.99k
            if (seqtt == NULL)
418
0
                goto err;
419
4.99k
            if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
420
4.86k
                ASN1_VALUE **pseqval;
421
4.86k
                pseqval = asn1_get_field_ptr(pval, seqtt);
422
4.86k
                asn1_template_free(pseqval, seqtt);
423
4.86k
            } else {
424
127
                errtt = seqtt;
425
127
                ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_FIELD_MISSING);
426
127
                goto err;
427
127
            }
428
4.99k
        }
429
        /* Save encoding */
430
53.9k
        if (!asn1_enc_save(pval, *in, p - *in, it))
431
0
            goto auxerr;
432
53.9k
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
433
0
            goto auxerr;
434
53.9k
        *in = p;
435
53.9k
        return 1;
436
437
0
    default:
438
0
        return 0;
439
218k
    }
440
0
 auxerr:
441
0
    ASN1err(ASN1_F_ASN1_ITEM_EMBED_D2I, ASN1_R_AUX_ERROR);
442
6.63k
 err:
443
6.63k
    if (errtt)
444
4.91k
        ERR_add_error_data(4, "Field=", errtt->field_name,
445
4.91k
                           ", Type=", it->sname);
446
1.72k
    else
447
1.72k
        ERR_add_error_data(2, "Type=", it->sname);
448
6.63k
    return 0;
449
0
}
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
183k
{
461
183k
    int flags, aclass;
462
183k
    int ret;
463
183k
    long len;
464
183k
    const unsigned char *p, *q;
465
183k
    char exp_eoc;
466
183k
    if (!val)
467
0
        return 0;
468
183k
    flags = tt->flags;
469
183k
    aclass = flags & ASN1_TFLG_TAG_CLASS;
470
471
183k
    p = *in;
472
473
    /* Check if EXPLICIT tag expected */
474
183k
    if (flags & ASN1_TFLG_EXPTAG) {
475
8.21k
        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
8.21k
        ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
481
8.21k
                              &p, inlen, tt->tag, aclass, opt, ctx);
482
8.21k
        q = p;
483
8.21k
        if (!ret) {
484
170
            ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
485
170
            return 0;
486
8.04k
        } else if (ret == -1)
487
478
            return -1;
488
7.56k
        if (!cst) {
489
23
            ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
490
23
                    ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
491
23
            return 0;
492
23
        }
493
        /* We've found the field so it can't be OPTIONAL now */
494
7.54k
        ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth);
495
7.54k
        if (!ret) {
496
150
            ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
497
150
            return 0;
498
150
        }
499
        /* We read the field in OK so update length */
500
7.39k
        len -= p - q;
501
7.39k
        if (exp_eoc) {
502
            /* If NDEF we must have an EOC here */
503
14
            if (!asn1_check_eoc(&p, len)) {
504
13
                ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, ASN1_R_MISSING_EOC);
505
13
                goto err;
506
13
            }
507
7.37k
        } else {
508
            /*
509
             * Otherwise we must hit the EXPLICIT tag end or its an error
510
             */
511
7.37k
            if (len) {
512
6
                ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
513
6
                        ASN1_R_EXPLICIT_LENGTH_MISMATCH);
514
6
                goto err;
515
6
            }
516
7.37k
        }
517
7.39k
    } else
518
175k
        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth);
519
520
7.37k
    *in = p;
521
7.37k
    return 1;
522
523
19
 err:
524
19
    return 0;
525
183k
}
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
182k
{
532
182k
    int flags, aclass;
533
182k
    int ret;
534
182k
    ASN1_VALUE *tval;
535
182k
    const unsigned char *p, *q;
536
182k
    if (!val)
537
0
        return 0;
538
182k
    flags = tt->flags;
539
182k
    aclass = flags & ASN1_TFLG_TAG_CLASS;
540
541
182k
    p = *in;
542
182k
    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
182k
    if (tt->flags & ASN1_TFLG_EMBED) {
549
33.3k
        tval = (ASN1_VALUE *)val;
550
33.3k
        val = &tval;
551
33.3k
    }
552
553
182k
    if (flags & ASN1_TFLG_SK_MASK) {
554
        /* SET OF, SEQUENCE OF */
555
30.0k
        int sktag, skaclass;
556
30.0k
        char sk_eoc;
557
        /* First work out expected inner tag value */
558
30.0k
        if (flags & ASN1_TFLG_IMPTAG) {
559
333
            sktag = tt->tag;
560
333
            skaclass = aclass;
561
29.6k
        } else {
562
29.6k
            skaclass = V_ASN1_UNIVERSAL;
563
29.6k
            if (flags & ASN1_TFLG_SET_OF)
564
17.3k
                sktag = V_ASN1_SET;
565
12.2k
            else
566
12.2k
                sktag = V_ASN1_SEQUENCE;
567
29.6k
        }
568
        /* Get the tag */
569
30.0k
        ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
570
30.0k
                              &p, len, sktag, skaclass, opt, ctx);
571
30.0k
        if (!ret) {
572
267
            ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
573
267
            return 0;
574
29.7k
        } else if (ret == -1)
575
95
            return -1;
576
29.6k
        if (!*val)
577
29.6k
            *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
578
0
        else {
579
            /*
580
             * We've got a valid STACK: free up any items present
581
             */
582
0
            STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
583
0
            ASN1_VALUE *vtmp;
584
0
            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
0
        }
589
590
29.6k
        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
71.8k
        while (len > 0) {
597
45.4k
            ASN1_VALUE *skfield;
598
45.4k
            q = p;
599
            /* See if EOC found */
600
45.4k
            if (asn1_check_eoc(&p, len)) {
601
2.55k
                if (!sk_eoc) {
602
21
                    ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
603
21
                            ASN1_R_UNEXPECTED_EOC);
604
21
                    goto err;
605
21
                }
606
2.53k
                len -= p - q;
607
2.53k
                sk_eoc = 0;
608
2.53k
                break;
609
2.55k
            }
610
42.8k
            skfield = NULL;
611
42.8k
            if (!asn1_item_embed_d2i(&skfield, &p, len,
612
42.8k
                                     ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
613
42.8k
                                     depth)) {
614
642
                ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
615
642
                        ERR_R_NESTED_ASN1_ERROR);
616
                /* |skfield| may be partially allocated despite failure. */
617
642
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
618
642
                goto err;
619
642
            }
620
42.2k
            len -= p - q;
621
42.2k
            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
42.2k
        }
627
28.9k
        if (sk_eoc) {
628
3
            ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC);
629
3
            goto err;
630
3
        }
631
152k
    } else if (flags & ASN1_TFLG_IMPTAG) {
632
        /* IMPLICIT tagging */
633
9.34k
        ret = asn1_item_embed_d2i(val, &p, len,
634
9.34k
                                  ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
635
9.34k
                                  ctx, depth);
636
9.34k
        if (!ret) {
637
420
            ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
638
420
            goto err;
639
8.92k
        } else if (ret == -1)
640
8.24k
            return -1;
641
143k
    } else {
642
        /* Nothing special */
643
143k
        ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
644
143k
                                  -1, 0, opt, ctx, depth);
645
143k
        if (!ret) {
646
3.91k
            ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ERR_R_NESTED_ASN1_ERROR);
647
3.91k
            goto err;
648
139k
        } else if (ret == -1)
649
9.15k
            return -1;
650
143k
    }
651
652
160k
    *in = p;
653
160k
    return 1;
654
655
4.99k
 err:
656
4.99k
    return 0;
657
182k
}
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
122k
{
664
122k
    int ret = 0, utype;
665
122k
    long plen;
666
122k
    char cst, inf, free_cont = 0;
667
122k
    const unsigned char *p;
668
122k
    BUF_MEM buf = { 0, NULL, 0, 0 };
669
122k
    const unsigned char *cont = NULL;
670
122k
    long len;
671
122k
    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
122k
    if (it->itype == ASN1_ITYPE_MSTRING) {
677
22.9k
        utype = tag;
678
22.9k
        tag = -1;
679
22.9k
    } else
680
99.6k
        utype = it->utype;
681
682
122k
    if (utype == V_ASN1_ANY) {
683
        /* If type is ANY need to figure out type from tag */
684
9.41k
        unsigned char oclass;
685
9.41k
        if (tag >= 0) {
686
0
            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_TAGGED_ANY);
687
0
            return 0;
688
0
        }
689
9.41k
        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
9.41k
        p = *in;
695
9.41k
        ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
696
9.41k
                              &p, inlen, -1, 0, 0, ctx);
697
9.41k
        if (!ret) {
698
41
            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
699
41
            return 0;
700
41
        }
701
9.37k
        if (oclass != V_ASN1_UNIVERSAL)
702
856
            utype = V_ASN1_OTHER;
703
9.37k
    }
704
122k
    if (tag == -1) {
705
113k
        tag = utype;
706
113k
        aclass = V_ASN1_UNIVERSAL;
707
113k
    }
708
122k
    p = *in;
709
    /* Check header */
710
122k
    ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
711
122k
                          &p, inlen, tag, aclass, opt, ctx);
712
122k
    if (!ret) {
713
1.32k
        ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
714
1.32k
        return 0;
715
121k
    } else if (ret == -1)
716
17.0k
        return -1;
717
104k
    ret = 0;
718
    /* SEQUENCE, SET and "OTHER" are left in encoded form */
719
104k
    if ((utype == V_ASN1_SEQUENCE)
720
104k
        || (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
1.93k
        if (utype == V_ASN1_OTHER) {
726
856
            asn1_tlc_clear(ctx);
727
856
        }
728
        /* SEQUENCE and SET must be constructed */
729
1.07k
        else if (!cst) {
730
7
            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
731
7
                    ASN1_R_TYPE_NOT_CONSTRUCTED);
732
7
            return 0;
733
7
        }
734
735
1.92k
        cont = *in;
736
        /* If indefinite length constructed find the real end */
737
1.92k
        if (inf) {
738
142
            if (!asn1_find_end(&p, plen, inf))
739
47
                goto err;
740
95
            len = p - cont;
741
1.78k
        } else {
742
1.78k
            len = p - cont + plen;
743
1.78k
            p += plen;
744
1.78k
        }
745
102k
    } else if (cst) {
746
1.28k
        if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
747
1.28k
            || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
748
1.28k
            || utype == V_ASN1_ENUMERATED) {
749
56
            ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_TYPE_NOT_PRIMITIVE);
750
56
            return 0;
751
56
        }
752
753
        /* Free any returned 'buf' content */
754
1.22k
        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
1.22k
        if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
762
94
            goto err;
763
94
        }
764
1.13k
        len = buf.length;
765
        /* Append a final null to string */
766
1.13k
        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
1.13k
        buf.data[len] = 0;
771
1.13k
        cont = (const unsigned char *)buf.data;
772
101k
    } else {
773
101k
        cont = p;
774
101k
        len = plen;
775
101k
        p += plen;
776
101k
    }
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
104k
    if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
781
364
        goto err;
782
783
103k
    *in = p;
784
103k
    ret = 1;
785
104k
 err:
786
104k
    if (free_cont)
787
478
        OPENSSL_free(buf.data);
788
104k
    return ret;
789
103k
}
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
104k
{
796
104k
    ASN1_VALUE **opval = NULL;
797
104k
    ASN1_STRING *stmp;
798
104k
    ASN1_TYPE *typ = NULL;
799
104k
    int ret = 0;
800
104k
    const ASN1_PRIMITIVE_FUNCS *pf;
801
104k
    ASN1_INTEGER **tint;
802
104k
    pf = it->funcs;
803
804
104k
    if (pf && pf->prim_c2i)
805
6.54k
        return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
806
    /* If ANY type clear type and set pointer to internal value */
807
97.5k
    if (it->utype == V_ASN1_ANY) {
808
9.24k
        if (!*pval) {
809
9.24k
            typ = ASN1_TYPE_new();
810
9.24k
            if (typ == NULL)
811
0
                goto err;
812
9.24k
            *pval = (ASN1_VALUE *)typ;
813
9.24k
        } else
814
0
            typ = (ASN1_TYPE *)*pval;
815
816
9.24k
        if (utype != typ->type)
817
9.24k
            ASN1_TYPE_set(typ, utype, NULL);
818
9.24k
        opval = pval;
819
9.24k
        pval = &typ->value.asn1_value;
820
9.24k
    }
821
97.5k
    switch (utype) {
822
37.3k
    case V_ASN1_OBJECT:
823
37.3k
        if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
824
70
            goto err;
825
37.3k
        break;
826
827
37.3k
    case V_ASN1_NULL:
828
5.35k
        if (len) {
829
8
            ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_NULL_IS_WRONG_LENGTH);
830
8
            goto err;
831
8
        }
832
5.34k
        *pval = (ASN1_VALUE *)1;
833
5.34k
        break;
834
835
1.10k
    case V_ASN1_BOOLEAN:
836
1.10k
        if (len != 1) {
837
11
            ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
838
11
            goto err;
839
1.09k
        } else {
840
1.09k
            ASN1_BOOLEAN *tbool;
841
1.09k
            tbool = (ASN1_BOOLEAN *)pval;
842
1.09k
            *tbool = *cont;
843
1.09k
        }
844
1.09k
        break;
845
846
8.42k
    case V_ASN1_BIT_STRING:
847
8.42k
        if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
848
83
            goto err;
849
8.33k
        break;
850
851
9.12k
    case V_ASN1_INTEGER:
852
9.51k
    case V_ASN1_ENUMERATED:
853
9.51k
        tint = (ASN1_INTEGER **)pval;
854
9.51k
        if (!c2i_ASN1_INTEGER(tint, &cont, len))
855
31
            goto err;
856
        /* Fixup type to match the expected form */
857
9.48k
        (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
858
9.48k
        break;
859
860
10.4k
    case V_ASN1_OCTET_STRING:
861
10.5k
    case V_ASN1_NUMERICSTRING:
862
18.8k
    case V_ASN1_PRINTABLESTRING:
863
19.0k
    case V_ASN1_T61STRING:
864
19.0k
    case V_ASN1_VIDEOTEXSTRING:
865
19.2k
    case V_ASN1_IA5STRING:
866
26.8k
    case V_ASN1_UTCTIME:
867
26.8k
    case V_ASN1_GENERALIZEDTIME:
868
26.8k
    case V_ASN1_GRAPHICSTRING:
869
26.9k
    case V_ASN1_VISIBLESTRING:
870
26.9k
    case V_ASN1_GENERALSTRING:
871
27.1k
    case V_ASN1_UNIVERSALSTRING:
872
27.2k
    case V_ASN1_BMPSTRING:
873
32.7k
    case V_ASN1_UTF8STRING:
874
33.6k
    case V_ASN1_OTHER:
875
33.6k
    case V_ASN1_SET:
876
34.6k
    case V_ASN1_SEQUENCE:
877
35.7k
    default:
878
35.7k
        if (utype == V_ASN1_BMPSTRING && (len & 1)) {
879
7
            ASN1err(ASN1_F_ASN1_EX_C2I, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
880
7
            goto err;
881
7
        }
882
35.7k
        if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
883
8
            ASN1err(ASN1_F_ASN1_EX_C2I,
884
8
                    ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
885
8
            goto err;
886
8
        }
887
        /* All based on ASN1_STRING and handled the same */
888
35.7k
        if (!*pval) {
889
4.02k
            stmp = ASN1_STRING_type_new(utype);
890
4.02k
            if (stmp == NULL) {
891
0
                ASN1err(ASN1_F_ASN1_EX_C2I, ERR_R_MALLOC_FAILURE);
892
0
                goto err;
893
0
            }
894
4.02k
            *pval = (ASN1_VALUE *)stmp;
895
31.7k
        } else {
896
31.7k
            stmp = (ASN1_STRING *)*pval;
897
31.7k
            stmp->type = utype;
898
31.7k
        }
899
        /* If we've already allocated a buffer use it */
900
35.7k
        if (*free_cont) {
901
749
            OPENSSL_free(stmp->data);
902
749
            stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
903
749
            stmp->length = len;
904
749
            *free_cont = 0;
905
34.9k
        } else {
906
34.9k
            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
34.9k
        }
913
35.7k
        break;
914
97.5k
    }
915
    /* If ASN1_ANY and NULL type fix up value */
916
97.3k
    if (typ && (utype == V_ASN1_NULL))
917
5.34k
        typ->value.ptr = NULL;
918
919
97.3k
    ret = 1;
920
97.5k
 err:
921
97.5k
    if (!ret) {
922
218
        ASN1_TYPE_free(typ);
923
218
        if (opval)
924
47
            *opval = NULL;
925
218
    }
926
97.5k
    return ret;
927
97.3k
}
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
142
{
938
142
    uint32_t expected_eoc;
939
142
    long plen;
940
142
    const unsigned char *p = *in, *q;
941
    /* If not indefinite length constructed just add length */
942
142
    if (inf == 0) {
943
0
        *in += len;
944
0
        return 1;
945
0
    }
946
142
    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
1.67k
    while (len > 0) {
954
1.65k
        if (asn1_check_eoc(&p, len)) {
955
588
            expected_eoc--;
956
588
            if (expected_eoc == 0)
957
95
                break;
958
493
            len -= 2;
959
493
            continue;
960
588
        }
961
1.07k
        q = p;
962
        /* Just read in a header: only care about the length */
963
1.07k
        if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
964
1.07k
                             -1, 0, 0, NULL)) {
965
26
            ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
966
26
            return 0;
967
26
        }
968
1.04k
        if (inf) {
969
569
            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
569
            expected_eoc++;
974
569
        } else {
975
475
            p += plen;
976
475
        }
977
1.04k
        len -= p - q;
978
1.04k
    }
979
116
    if (expected_eoc) {
980
21
        ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
981
21
        return 0;
982
21
    }
983
95
    *in = p;
984
95
    return 1;
985
116
}
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
1.38k
# 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
2.60k
{
1006
2.60k
    const unsigned char *p, *q;
1007
2.60k
    long plen;
1008
2.60k
    char cst, ininf;
1009
2.60k
    p = *in;
1010
2.60k
    inf &= 1;
1011
    /*
1012
     * If no buffer and not indefinite length constructed just pass over the
1013
     * encoded data
1014
     */
1015
2.60k
    if (!buf && !inf) {
1016
0
        *in += len;
1017
0
        return 1;
1018
0
    }
1019
7.44k
    while (len > 0) {
1020
5.55k
        q = p;
1021
        /* Check for EOC */
1022
5.55k
        if (asn1_check_eoc(&p, len)) {
1023
            /*
1024
             * EOC is illegal outside indefinite length constructed form
1025
             */
1026
538
            if (!inf) {
1027
8
                ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_UNEXPECTED_EOC);
1028
8
                return 0;
1029
8
            }
1030
530
            inf = 0;
1031
530
            break;
1032
538
        }
1033
1034
5.01k
        if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1035
5.01k
                             len, tag, aclass, 0, NULL)) {
1036
62
            ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1037
62
            return 0;
1038
62
        }
1039
1040
        /* If indefinite length constructed update max length */
1041
4.95k
        if (cst) {
1042
1.38k
            if (depth >= ASN1_MAX_STRING_NEST) {
1043
3
                ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_NESTED_ASN1_STRING);
1044
3
                return 0;
1045
3
            }
1046
1.38k
            if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1047
115
                return 0;
1048
3.56k
        } else if (plen && !collect_data(buf, &p, plen))
1049
0
            return 0;
1050
4.83k
        len -= p - q;
1051
4.83k
    }
1052
2.42k
    if (inf) {
1053
21
        ASN1err(ASN1_F_ASN1_COLLECT, ASN1_R_MISSING_EOC);
1054
21
        return 0;
1055
21
    }
1056
2.40k
    *in = p;
1057
2.40k
    return 1;
1058
2.42k
}
1059
1060
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1061
3.18k
{
1062
3.18k
    int len;
1063
3.18k
    if (buf) {
1064
3.18k
        len = buf->length;
1065
3.18k
        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
3.18k
        memcpy(buf->data + len, *p, plen);
1070
3.18k
    }
1071
3.18k
    *p += plen;
1072
3.18k
    return 1;
1073
3.18k
}
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
207k
{
1079
207k
    const unsigned char *p;
1080
207k
    if (len < 2)
1081
174
        return 0;
1082
207k
    p = *in;
1083
207k
    if (!p[0] && !p[1]) {
1084
4.20k
        *in += 2;
1085
4.20k
        return 1;
1086
4.20k
    }
1087
203k
    return 0;
1088
207k
}
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
259k
{
1102
259k
    int i;
1103
259k
    int ptag, pclass;
1104
259k
    long plen;
1105
259k
    const unsigned char *p, *q;
1106
259k
    p = *in;
1107
259k
    q = p;
1108
1109
259k
    if (ctx && ctx->valid) {
1110
50.2k
        i = ctx->ret;
1111
50.2k
        plen = ctx->plen;
1112
50.2k
        pclass = ctx->pclass;
1113
50.2k
        ptag = ctx->ptag;
1114
50.2k
        p += ctx->hdrlen;
1115
209k
    } else {
1116
209k
        i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1117
209k
        if (ctx) {
1118
203k
            ctx->ret = i;
1119
203k
            ctx->plen = plen;
1120
203k
            ctx->pclass = pclass;
1121
203k
            ctx->ptag = ptag;
1122
203k
            ctx->hdrlen = p - q;
1123
203k
            ctx->valid = 1;
1124
            /*
1125
             * If definite length, and no error, length + header can't exceed
1126
             * total amount of data available.
1127
             */
1128
203k
            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
203k
        }
1134
209k
    }
1135
1136
259k
    if (i & 0x80) {
1137
1.89k
        ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_BAD_OBJECT_HEADER);
1138
1.89k
        asn1_tlc_clear(ctx);
1139
1.89k
        return 0;
1140
1.89k
    }
1141
257k
    if (exptag >= 0) {
1142
218k
        if ((exptag != ptag) || (expclass != pclass)) {
1143
            /*
1144
             * If type is OPTIONAL, not an error: indicate missing type.
1145
             */
1146
19.1k
            if (opt)
1147
17.9k
                return -1;
1148
1.21k
            asn1_tlc_clear(ctx);
1149
1.21k
            ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1150
1.21k
            return 0;
1151
19.1k
        }
1152
        /*
1153
         * We have a tag and class match: assume we are going to do something
1154
         * with it
1155
         */
1156
199k
        asn1_tlc_clear(ctx);
1157
199k
    }
1158
1159
238k
    if (i & 1)
1160
5.86k
        plen = len - (p - q);
1161
1162
238k
    if (inf)
1163
206k
        *inf = i & 1;
1164
1165
238k
    if (cst)
1166
175k
        *cst = i & V_ASN1_CONSTRUCTED;
1167
1168
238k
    if (olen)
1169
206k
        *olen = plen;
1170
1171
238k
    if (oclass)
1172
32.4k
        *oclass = pclass;
1173
1174
238k
    if (otag)
1175
32.4k
        *otag = ptag;
1176
1177
238k
    *in = p;
1178
238k
    return 1;
1179
257k
}