Coverage Report

Created: 2023-09-25 06:41

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