Coverage Report

Created: 2023-09-25 06:43

/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
12.5M
#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
1.75M
{
89
1.75M
    if ((tag < 0) || (tag > 30))
90
443
        return 0;
91
1.75M
    return tag2bit[tag];
92
1.75M
}
93
94
/* Macro to initialize and invalidate the cache */
95
96
11.2M
#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
3.13M
#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
3.46M
{
112
3.46M
    int rv;
113
114
3.46M
    if (pval == NULL || it == NULL) {
115
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
116
0
        return 0;
117
0
    }
118
3.46M
    rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0,
119
3.46M
                             libctx, propq);
120
3.46M
    if (rv <= 0)
121
2.51M
        ASN1_item_ex_free(pval, it);
122
3.46M
    return rv;
123
3.46M
}
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
327k
{
129
327k
    return asn1_item_ex_d2i_intern(pval, in, len, it, tag, aclass, opt, ctx,
130
327k
                                   NULL, NULL);
131
327k
}
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
3.13M
{
138
3.13M
    ASN1_TLC c;
139
3.13M
    ASN1_VALUE *ptmpval = NULL;
140
141
3.13M
    if (pval == NULL)
142
2.99M
        pval = &ptmpval;
143
3.13M
    asn1_tlc_clear_nc(&c);
144
3.13M
    if (asn1_item_ex_d2i_intern(pval, in, len, it, -1, 0, 0, &c, libctx,
145
3.13M
                                propq) > 0)
146
683k
        return *pval;
147
2.45M
    return NULL;
148
3.13M
}
149
150
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
151
                          const unsigned char **in, long len,
152
                          const ASN1_ITEM *it)
153
3.04M
{
154
3.04M
    return ASN1_item_d2i_ex(pval, in, len, it, NULL, NULL);
155
3.04M
}
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
12.5M
{
168
12.5M
    const ASN1_TEMPLATE *tt, *errtt = NULL;
169
12.5M
    const ASN1_EXTERN_FUNCS *ef;
170
12.5M
    const ASN1_AUX *aux;
171
12.5M
    ASN1_aux_cb *asn1_cb;
172
12.5M
    const unsigned char *p = NULL, *q;
173
12.5M
    unsigned char oclass;
174
12.5M
    char seq_eoc, seq_nolen, cst, isopt;
175
12.5M
    long tmplen;
176
12.5M
    int i;
177
12.5M
    int otag;
178
12.5M
    int ret = 0;
179
12.5M
    ASN1_VALUE **pchptr;
180
181
12.5M
    if (pval == NULL || it == NULL) {
182
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
183
0
        return 0;
184
0
    }
185
12.5M
    if (len <= 0) {
186
1.69k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
187
1.69k
        return 0;
188
1.69k
    }
189
12.5M
    aux = it->funcs;
190
12.5M
    if (aux && aux->asn1_cb)
191
1.44M
        asn1_cb = aux->asn1_cb;
192
11.0M
    else
193
11.0M
        asn1_cb = 0;
194
195
12.5M
    if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
196
1
        ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_TOO_DEEP);
197
1
        goto err;
198
1
    }
199
200
12.5M
    switch (it->itype) {
201
7.14M
    case ASN1_ITYPE_PRIMITIVE:
202
7.14M
        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
1.81M
            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
1.81M
            return asn1_template_ex_d2i(pval, in, len, it->templates, opt, ctx,
215
1.81M
                                        depth, libctx, propq);
216
1.81M
        }
217
5.33M
        return asn1_d2i_ex_primitive(pval, in, len, it,
218
5.33M
                                     tag, aclass, opt, ctx);
219
220
862k
    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
862k
        if (tag != -1) {
226
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
227
0
            goto err;
228
0
        }
229
230
862k
        p = *in;
231
        /* Just read in tag and class */
232
862k
        ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
233
862k
                              &p, len, -1, 0, 1, ctx);
234
862k
        if (!ret) {
235
1.52k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
236
1.52k
            goto err;
237
1.52k
        }
238
239
        /* Must be UNIVERSAL class */
240
860k
        if (oclass != V_ASN1_UNIVERSAL) {
241
            /* If OPTIONAL, assume this is OK */
242
17.0k
            if (opt)
243
10.9k
                return -1;
244
17.0k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
245
6.04k
            goto err;
246
17.0k
        }
247
248
        /* Check tag matches bit map */
249
843k
        if (!(ASN1_tag2bit(otag) & it->utype)) {
250
            /* If OPTIONAL, assume this is OK */
251
68.5k
            if (opt)
252
1.35k
                return -1;
253
68.5k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_WRONG_TAG);
254
67.1k
            goto err;
255
68.5k
        }
256
775k
        return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
257
258
327k
    case ASN1_ITYPE_EXTERN:
259
        /* Use new style d2i */
260
327k
        ef = it->funcs;
261
327k
        if (ef->asn1_ex_d2i_ex != NULL)
262
141k
            return ef->asn1_ex_d2i_ex(pval, in, len, it, tag, aclass, opt, ctx,
263
141k
                                      libctx, propq);
264
185k
        return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
265
266
299k
    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
299k
        if (tag != -1) {
272
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
273
0
            goto err;
274
0
        }
275
276
299k
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
277
0
            goto auxerr;
278
299k
        if (*pval) {
279
            /* Free up and zero CHOICE value if initialised */
280
29.7k
            i = ossl_asn1_get_choice_selector(pval, it);
281
29.7k
            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
269k
        } 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
299k
        p = *in;
293
1.00M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
294
895k
            pchptr = ossl_asn1_get_field_ptr(pval, tt);
295
            /*
296
             * We mark field as OPTIONAL so its absence can be recognised.
297
             */
298
895k
            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth,
299
895k
                                       libctx, propq);
300
            /* If field not present, try the next one */
301
895k
            if (ret == -1)
302
703k
                continue;
303
            /* If positive return, read OK, break loop */
304
192k
            if (ret > 0)
305
101k
                break;
306
            /*
307
             * Must be an ASN1 parsing error.
308
             * Free up any partial choice value
309
             */
310
90.8k
            ossl_asn1_template_free(pchptr, tt);
311
90.8k
            errtt = tt;
312
90.8k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
313
90.8k
            goto err;
314
192k
        }
315
316
        /* Did we fall off the end without reading anything? */
317
208k
        if (i == it->tcount) {
318
            /* If OPTIONAL, this is OK */
319
106k
            if (opt) {
320
                /* Free and zero it */
321
3
                ASN1_item_ex_free(pval, it);
322
3
                return -1;
323
3
            }
324
106k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
325
106k
            goto err;
326
106k
        }
327
328
101k
        ossl_asn1_set_choice_selector(pval, i, it);
329
330
101k
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
331
0
            goto auxerr;
332
101k
        *in = p;
333
101k
        return 1;
334
335
123k
    case ASN1_ITYPE_NDEF_SEQUENCE:
336
3.89M
    case ASN1_ITYPE_SEQUENCE:
337
3.89M
        p = *in;
338
3.89M
        tmplen = len;
339
340
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
341
3.89M
        if (tag == -1) {
342
3.73M
            tag = V_ASN1_SEQUENCE;
343
3.73M
            aclass = V_ASN1_UNIVERSAL;
344
3.73M
        }
345
        /* Get SEQUENCE length and update len, p */
346
3.89M
        ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
347
3.89M
                              &p, len, tag, aclass, opt, ctx);
348
3.89M
        if (!ret) {
349
636k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
350
636k
            goto err;
351
3.25M
        } else if (ret == -1)
352
187k
            return -1;
353
3.06M
        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
3.06M
        else
359
3.06M
            seq_nolen = seq_eoc;
360
3.06M
        if (!cst) {
361
25.2k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
362
25.2k
            goto err;
363
25.2k
        }
364
365
3.04M
        if (*pval == NULL
366
3.04M
                && !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
3.04M
        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
12.4M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
376
9.40M
            if (tt->flags & ASN1_TFLG_ADB_MASK) {
377
71.5k
                const ASN1_TEMPLATE *seqtt;
378
71.5k
                ASN1_VALUE **pseqval;
379
71.5k
                seqtt = ossl_asn1_do_adb(*pval, tt, 0);
380
71.5k
                if (seqtt == NULL)
381
51.8k
                    continue;
382
19.7k
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
383
19.7k
                ossl_asn1_template_free(pseqval, seqtt);
384
19.7k
            }
385
9.40M
        }
386
387
        /* Get each field entry */
388
7.26M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
389
6.00M
            const ASN1_TEMPLATE *seqtt;
390
6.00M
            ASN1_VALUE **pseqval;
391
6.00M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
392
6.00M
            if (seqtt == NULL)
393
0
                goto err;
394
6.00M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
395
            /* Have we ran out of data? */
396
6.00M
            if (!len)
397
109k
                break;
398
5.89M
            q = p;
399
5.89M
            if (asn1_check_eoc(&p, len)) {
400
232k
                if (!seq_eoc) {
401
1.15k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
402
1.15k
                    goto err;
403
1.15k
                }
404
230k
                len -= p - q;
405
230k
                seq_eoc = 0;
406
230k
                break;
407
232k
            }
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
5.66M
            if (i == (it->tcount - 1))
415
1.48M
                isopt = 0;
416
4.17M
            else
417
4.17M
                isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
418
            /*
419
             * attempt to read in field, allowing each to be OPTIONAL
420
             */
421
422
5.66M
            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
423
5.66M
                                       depth, libctx, propq);
424
5.66M
            if (!ret) {
425
1.44M
                errtt = seqtt;
426
1.44M
                goto err;
427
4.22M
            } else if (ret == -1) {
428
                /*
429
                 * OPTIONAL component absent. Free and zero the field.
430
                 */
431
524k
                ossl_asn1_template_free(pseqval, seqtt);
432
524k
                continue;
433
524k
            }
434
            /* Update length */
435
3.69M
            len -= p - q;
436
3.69M
        }
437
438
        /* Check for EOC if expecting one */
439
1.60M
        if (seq_eoc && !asn1_check_eoc(&p, len)) {
440
16.9k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
441
16.9k
            goto err;
442
16.9k
        }
443
        /* Check all data read */
444
1.58M
        if (!seq_nolen && len) {
445
767
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
446
767
            goto err;
447
767
        }
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
2.06M
        for (; i < it->tcount; tt++, i++) {
455
515k
            const ASN1_TEMPLATE *seqtt;
456
515k
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
457
515k
            if (seqtt == NULL)
458
0
                goto err;
459
515k
            if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
460
481k
                ASN1_VALUE **pseqval;
461
481k
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
462
481k
                ossl_asn1_template_free(pseqval, seqtt);
463
481k
            } else {
464
34.4k
                errtt = seqtt;
465
34.4k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_FIELD_MISSING);
466
34.4k
                goto err;
467
34.4k
            }
468
515k
        }
469
        /* Save encoding */
470
1.54M
        if (!ossl_asn1_enc_save(pval, *in, p - *in, it))
471
0
            goto auxerr;
472
1.54M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
473
78
            goto auxerr;
474
1.54M
        *in = p;
475
1.54M
        return 1;
476
477
0
    default:
478
0
        return 0;
479
12.5M
    }
480
78
 auxerr:
481
78
    ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
482
2.42M
 err:
483
2.42M
    if (errtt)
484
1.56M
        ERR_add_error_data(4, "Field=", errtt->field_name,
485
1.56M
                           ", Type=", it->sname);
486
861k
    else
487
861k
        ERR_add_error_data(2, "Type=", it->sname);
488
2.42M
    return 0;
489
78
}
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
8.37M
{
502
8.37M
    int flags, aclass;
503
8.37M
    int ret;
504
8.37M
    long len;
505
8.37M
    const unsigned char *p, *q;
506
8.37M
    char exp_eoc;
507
8.37M
    if (!val)
508
0
        return 0;
509
8.37M
    flags = tt->flags;
510
8.37M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
511
512
8.37M
    p = *in;
513
514
    /* Check if EXPLICIT tag expected */
515
8.37M
    if (flags & ASN1_TFLG_EXPTAG) {
516
419k
        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
419k
        ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
522
419k
                              &p, inlen, tt->tag, aclass, opt, ctx);
523
419k
        q = p;
524
419k
        if (!ret) {
525
52.4k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
526
52.4k
            return 0;
527
366k
        } else if (ret == -1)
528
317k
            return -1;
529
49.7k
        if (!cst) {
530
575
            ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
531
575
            return 0;
532
575
        }
533
        /* We've found the field so it can't be OPTIONAL now */
534
49.1k
        ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth, libctx,
535
49.1k
                                      propq);
536
49.1k
        if (!ret) {
537
16.9k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
538
16.9k
            return 0;
539
16.9k
        }
540
        /* We read the field in OK so update length */
541
32.1k
        len -= p - q;
542
32.1k
        if (exp_eoc) {
543
            /* If NDEF we must have an EOC here */
544
23.4k
            if (!asn1_check_eoc(&p, len)) {
545
1.23k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
546
1.23k
                goto err;
547
1.23k
            }
548
23.4k
        } else {
549
            /*
550
             * Otherwise we must hit the EXPLICIT tag end or its an error
551
             */
552
8.73k
            if (len) {
553
281
                ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
554
281
                goto err;
555
281
            }
556
8.73k
        }
557
32.1k
    } else
558
7.95M
        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth,
559
7.95M
                                       libctx, propq);
560
561
30.6k
    *in = p;
562
30.6k
    return 1;
563
564
1.51k
 err:
565
1.51k
    return 0;
566
8.37M
}
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
8.00M
{
574
8.00M
    int flags, aclass;
575
8.00M
    int ret;
576
8.00M
    ASN1_VALUE *tval;
577
8.00M
    const unsigned char *p, *q;
578
8.00M
    if (!val)
579
0
        return 0;
580
8.00M
    flags = tt->flags;
581
8.00M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
582
583
8.00M
    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
8.00M
    if (tt->flags & ASN1_TFLG_EMBED) {
590
800k
        tval = (ASN1_VALUE *)val;
591
800k
        val = &tval;
592
800k
    }
593
594
8.00M
    if (flags & ASN1_TFLG_SK_MASK) {
595
        /* SET OF, SEQUENCE OF */
596
2.05M
        int sktag, skaclass;
597
2.05M
        char sk_eoc;
598
        /* First work out expected inner tag value */
599
2.05M
        if (flags & ASN1_TFLG_IMPTAG) {
600
119k
            sktag = tt->tag;
601
119k
            skaclass = aclass;
602
1.93M
        } else {
603
1.93M
            skaclass = V_ASN1_UNIVERSAL;
604
1.93M
            if (flags & ASN1_TFLG_SET_OF)
605
1.47M
                sktag = V_ASN1_SET;
606
458k
            else
607
458k
                sktag = V_ASN1_SEQUENCE;
608
1.93M
        }
609
        /* Get the tag */
610
2.05M
        ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
611
2.05M
                              &p, len, sktag, skaclass, opt, ctx);
612
2.05M
        if (!ret) {
613
182k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
614
182k
            return 0;
615
1.87M
        } else if (ret == -1)
616
85.1k
            return -1;
617
1.78M
        if (*val == NULL)
618
1.77M
            *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
619
15.2k
        else {
620
            /*
621
             * We've got a valid STACK: free up any items present
622
             */
623
15.2k
            STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
624
15.2k
            ASN1_VALUE *vtmp;
625
15.2k
            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
15.2k
        }
630
631
1.78M
        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
4.73M
        while (len > 0) {
638
3.15M
            ASN1_VALUE *skfield;
639
3.15M
            q = p;
640
            /* See if EOC found */
641
3.15M
            if (asn1_check_eoc(&p, len)) {
642
39.5k
                if (!sk_eoc) {
643
568
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
644
568
                    goto err;
645
568
                }
646
38.9k
                len -= p - q;
647
38.9k
                sk_eoc = 0;
648
38.9k
                break;
649
39.5k
            }
650
3.11M
            skfield = NULL;
651
3.11M
            if (asn1_item_embed_d2i(&skfield, &p, len,
652
3.11M
                                     ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
653
3.11M
                                     depth, libctx, propq) <= 0) {
654
168k
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
655
                /* |skfield| may be partially allocated despite failure. */
656
168k
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
657
168k
                goto err;
658
168k
            }
659
2.94M
            len -= p - q;
660
2.94M
            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
2.94M
        }
666
1.61M
        if (sk_eoc) {
667
1.38k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
668
1.38k
            goto err;
669
1.38k
        }
670
5.94M
    } else if (flags & ASN1_TFLG_IMPTAG) {
671
        /* IMPLICIT tagging */
672
634k
        ret = asn1_item_embed_d2i(val, &p, len,
673
634k
                                  ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
674
634k
                                  ctx, depth, libctx, propq);
675
634k
        if (!ret) {
676
58.1k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
677
58.1k
            goto err;
678
576k
        } else if (ret == -1)
679
542k
            return -1;
680
5.31M
    } else {
681
        /* Nothing special */
682
5.31M
        ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
683
5.31M
                                  -1, 0, opt, ctx, depth, libctx, propq);
684
5.31M
        if (!ret) {
685
1.29M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
686
1.29M
            goto err;
687
4.01M
        } else if (ret == -1)
688
282k
            return -1;
689
5.31M
    }
690
691
5.38M
    *in = p;
692
5.38M
    return 1;
693
694
1.52M
 err:
695
1.52M
    return 0;
696
8.00M
}
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
6.10M
{
703
6.10M
    int ret = 0, utype;
704
6.10M
    long plen;
705
6.10M
    char cst, inf, free_cont = 0;
706
6.10M
    const unsigned char *p;
707
6.10M
    BUF_MEM buf = { 0, NULL, 0, 0 };
708
6.10M
    const unsigned char *cont = NULL;
709
6.10M
    long len;
710
711
6.10M
    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
6.10M
    if (it->itype == ASN1_ITYPE_MSTRING) {
717
775k
        utype = tag;
718
775k
        tag = -1;
719
775k
    } else
720
5.33M
        utype = it->utype;
721
722
6.10M
    if (utype == V_ASN1_ANY) {
723
        /* If type is ANY need to figure out type from tag */
724
901k
        unsigned char oclass;
725
901k
        if (tag >= 0) {
726
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
727
0
            return 0;
728
0
        }
729
901k
        if (opt) {
730
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
731
0
            return 0;
732
0
        }
733
901k
        p = *in;
734
901k
        ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
735
901k
                              &p, inlen, -1, 0, 0, ctx);
736
901k
        if (!ret) {
737
1.26k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
738
1.26k
            return 0;
739
1.26k
        }
740
900k
        if (oclass != V_ASN1_UNIVERSAL)
741
78.1k
            utype = V_ASN1_OTHER;
742
900k
    }
743
6.10M
    if (tag == -1) {
744
5.62M
        tag = utype;
745
5.62M
        aclass = V_ASN1_UNIVERSAL;
746
5.62M
    }
747
6.10M
    p = *in;
748
    /* Check header */
749
6.10M
    ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
750
6.10M
                          &p, inlen, tag, aclass, opt, ctx);
751
6.10M
    if (!ret) {
752
1.23M
        ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
753
1.23M
        return 0;
754
4.86M
    } else if (ret == -1)
755
625k
        return -1;
756
4.24M
    ret = 0;
757
    /* SEQUENCE, SET and "OTHER" are left in encoded form */
758
4.24M
    if ((utype == V_ASN1_SEQUENCE)
759
4.24M
        || (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
365k
        if (utype == V_ASN1_OTHER) {
765
78.1k
            asn1_tlc_clear(ctx);
766
78.1k
        }
767
        /* SEQUENCE and SET must be constructed */
768
287k
        else if (!cst) {
769
1.03k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
770
1.03k
            return 0;
771
1.03k
        }
772
773
364k
        cont = *in;
774
        /* If indefinite length constructed find the real end */
775
364k
        if (inf) {
776
111k
            if (!asn1_find_end(&p, plen, inf))
777
27.2k
                goto err;
778
83.9k
            len = p - cont;
779
253k
        } else {
780
253k
            len = p - cont + plen;
781
253k
            p += plen;
782
253k
        }
783
3.87M
    } else if (cst) {
784
180k
        if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
785
180k
            || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
786
180k
            || utype == V_ASN1_ENUMERATED) {
787
1.65k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
788
1.65k
            return 0;
789
1.65k
        }
790
791
        /* Free any returned 'buf' content */
792
179k
        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
179k
        if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
800
1.80k
            goto err;
801
1.80k
        }
802
177k
        len = buf.length;
803
        /* Append a final null to string */
804
177k
        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
177k
        buf.data[len] = 0;
809
177k
        cont = (const unsigned char *)buf.data;
810
3.69M
    } else {
811
3.69M
        cont = p;
812
3.69M
        len = plen;
813
3.69M
        p += plen;
814
3.69M
    }
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
4.20M
    if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
819
41.1k
        goto err;
820
821
4.16M
    *in = p;
822
4.16M
    ret = 1;
823
4.23M
 err:
824
4.23M
    if (free_cont)
825
2.49k
        OPENSSL_free(buf.data);
826
4.23M
    return ret;
827
4.16M
}
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
4.20M
{
834
4.20M
    ASN1_VALUE **opval = NULL;
835
4.20M
    ASN1_STRING *stmp;
836
4.20M
    ASN1_TYPE *typ = NULL;
837
4.20M
    int ret = 0;
838
4.20M
    const ASN1_PRIMITIVE_FUNCS *pf;
839
4.20M
    ASN1_INTEGER **tint;
840
4.20M
    pf = it->funcs;
841
842
4.20M
    if (pf && pf->prim_c2i)
843
280k
        return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
844
    /* If ANY type clear type and set pointer to internal value */
845
3.92M
    if (it->utype == V_ASN1_ANY) {
846
878k
        if (*pval == NULL) {
847
868k
            typ = ASN1_TYPE_new();
848
868k
            if (typ == NULL)
849
0
                goto err;
850
868k
            *pval = (ASN1_VALUE *)typ;
851
868k
        } else
852
10.2k
            typ = (ASN1_TYPE *)*pval;
853
854
878k
        if (utype != typ->type)
855
878k
            ASN1_TYPE_set(typ, utype, NULL);
856
878k
        opval = pval;
857
878k
        pval = &typ->value.asn1_value;
858
878k
    }
859
3.92M
    switch (utype) {
860
1.21M
    case V_ASN1_OBJECT:
861
1.21M
        if (!ossl_c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
862
979
            goto err;
863
1.21M
        break;
864
865
1.21M
    case V_ASN1_NULL:
866
401k
        if (len) {
867
86
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
868
86
            goto err;
869
86
        }
870
401k
        *pval = (ASN1_VALUE *)1;
871
401k
        break;
872
873
10.6k
    case V_ASN1_BOOLEAN:
874
10.6k
        if (len != 1) {
875
190
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
876
190
            goto err;
877
10.4k
        } else {
878
10.4k
            ASN1_BOOLEAN *tbool;
879
10.4k
            tbool = (ASN1_BOOLEAN *)pval;
880
10.4k
            *tbool = *cont;
881
10.4k
        }
882
10.4k
        break;
883
884
510k
    case V_ASN1_BIT_STRING:
885
510k
        if (!ossl_c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
886
450
            goto err;
887
509k
        break;
888
889
653k
    case V_ASN1_INTEGER:
890
672k
    case V_ASN1_ENUMERATED:
891
672k
        tint = (ASN1_INTEGER **)pval;
892
672k
        if (!ossl_c2i_ASN1_INTEGER(tint, &cont, len))
893
28.0k
            goto err;
894
        /* Fixup type to match the expected form */
895
644k
        (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
896
644k
        break;
897
898
229k
    case V_ASN1_OCTET_STRING:
899
229k
    case V_ASN1_NUMERICSTRING:
900
236k
    case V_ASN1_PRINTABLESTRING:
901
246k
    case V_ASN1_T61STRING:
902
246k
    case V_ASN1_VIDEOTEXSTRING:
903
348k
    case V_ASN1_IA5STRING:
904
419k
    case V_ASN1_UTCTIME:
905
495k
    case V_ASN1_GENERALIZEDTIME:
906
496k
    case V_ASN1_GRAPHICSTRING:
907
497k
    case V_ASN1_VISIBLESTRING:
908
497k
    case V_ASN1_GENERALSTRING:
909
515k
    case V_ASN1_UNIVERSALSTRING:
910
673k
    case V_ASN1_BMPSTRING:
911
686k
    case V_ASN1_UTF8STRING:
912
756k
    case V_ASN1_OTHER:
913
870k
    case V_ASN1_SET:
914
1.02M
    case V_ASN1_SEQUENCE:
915
1.12M
    default:
916
1.12M
        if (utype == V_ASN1_BMPSTRING && (len & 1)) {
917
77
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
918
77
            goto err;
919
77
        }
920
1.12M
        if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
921
71
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
922
71
            goto err;
923
71
        }
924
        /* All based on ASN1_STRING and handled the same */
925
1.12M
        if (*pval == NULL) {
926
524k
            stmp = ASN1_STRING_type_new(utype);
927
524k
            if (stmp == NULL) {
928
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
929
0
                goto err;
930
0
            }
931
524k
            *pval = (ASN1_VALUE *)stmp;
932
596k
        } else {
933
596k
            stmp = (ASN1_STRING *)*pval;
934
596k
            stmp->type = utype;
935
596k
        }
936
        /* If we've already allocated a buffer use it */
937
1.12M
        if (*free_cont) {
938
176k
            OPENSSL_free(stmp->data);
939
176k
            stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
940
176k
            stmp->length = len;
941
176k
            *free_cont = 0;
942
943k
        } else {
943
943k
            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
943k
        }
950
1.12M
        break;
951
3.92M
    }
952
    /* If ASN1_ANY and NULL type fix up value */
953
3.89M
    if (typ && (utype == V_ASN1_NULL))
954
401k
        typ->value.ptr = NULL;
955
956
3.89M
    ret = 1;
957
3.92M
 err:
958
3.92M
    if (!ret) {
959
29.9k
        ASN1_TYPE_free(typ);
960
29.9k
        if (opval)
961
1.34k
            *opval = NULL;
962
29.9k
    }
963
3.92M
    return ret;
964
3.89M
}
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
111k
{
975
111k
    uint32_t expected_eoc;
976
111k
    long plen;
977
111k
    const unsigned char *p = *in, *q;
978
    /* If not indefinite length constructed just add length */
979
111k
    if (inf == 0) {
980
0
        *in += len;
981
0
        return 1;
982
0
    }
983
111k
    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
56.9M
    while (len > 0) {
991
56.8M
        if (asn1_check_eoc(&p, len)) {
992
5.87M
            expected_eoc--;
993
5.87M
            if (expected_eoc == 0)
994
83.9k
                break;
995
5.79M
            len -= 2;
996
5.79M
            continue;
997
5.87M
        }
998
51.0M
        q = p;
999
        /* Just read in a header: only care about the length */
1000
51.0M
        if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1001
51.0M
                             -1, 0, 0, NULL)) {
1002
18.6k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1003
18.6k
            return 0;
1004
18.6k
        }
1005
51.0M
        if (inf) {
1006
13.6M
            if (expected_eoc == UINT32_MAX) {
1007
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1008
0
                return 0;
1009
0
            }
1010
13.6M
            expected_eoc++;
1011
37.3M
        } else {
1012
37.3M
            p += plen;
1013
37.3M
        }
1014
51.0M
        len -= p - q;
1015
51.0M
    }
1016
92.4k
    if (expected_eoc) {
1017
8.52k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1018
8.52k
        return 0;
1019
8.52k
    }
1020
83.9k
    *in = p;
1021
83.9k
    return 1;
1022
92.4k
}
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
38.6k
# 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
217k
{
1043
217k
    const unsigned char *p, *q;
1044
217k
    long plen;
1045
217k
    char cst, ininf;
1046
217k
    p = *in;
1047
217k
    inf &= 1;
1048
    /*
1049
     * If no buffer and not indefinite length constructed just pass over the
1050
     * encoded data
1051
     */
1052
217k
    if (!buf && !inf) {
1053
0
        *in += len;
1054
0
        return 1;
1055
0
    }
1056
888k
    while (len > 0) {
1057
700k
        q = p;
1058
        /* Check for EOC */
1059
700k
        if (asn1_check_eoc(&p, len)) {
1060
            /*
1061
             * EOC is illegal outside indefinite length constructed form
1062
             */
1063
26.8k
            if (!inf) {
1064
229
                ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
1065
229
                return 0;
1066
229
            }
1067
26.5k
            inf = 0;
1068
26.5k
            break;
1069
26.8k
        }
1070
1071
673k
        if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1072
673k
                             len, tag, aclass, 0, NULL)) {
1073
986
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1074
986
            return 0;
1075
986
        }
1076
1077
        /* If indefinite length constructed update max length */
1078
672k
        if (cst) {
1079
38.6k
            if (depth >= ASN1_MAX_STRING_NEST) {
1080
147
                ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_ASN1_STRING);
1081
147
                return 0;
1082
147
            }
1083
38.4k
            if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1084
1.55k
                return 0;
1085
634k
        } else if (plen && !collect_data(buf, &p, plen))
1086
0
            return 0;
1087
671k
        len -= p - q;
1088
671k
    }
1089
214k
    if (inf) {
1090
439
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1091
439
        return 0;
1092
439
    }
1093
214k
    *in = p;
1094
214k
    return 1;
1095
214k
}
1096
1097
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1098
596k
{
1099
596k
    int len;
1100
596k
    if (buf) {
1101
596k
        len = buf->length;
1102
596k
        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
596k
        memcpy(buf->data + len, *p, plen);
1107
596k
    }
1108
596k
    *p += plen;
1109
596k
    return 1;
1110
596k
}
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
67.3M
{
1116
67.3M
    const unsigned char *p;
1117
1118
67.3M
    if (len < 2)
1119
11.5k
        return 0;
1120
67.3M
    p = *in;
1121
67.3M
    if (p[0] == '\0' && p[1] == '\0') {
1122
6.88M
        *in += 2;
1123
6.88M
        return 1;
1124
6.88M
    }
1125
60.4M
    return 0;
1126
67.3M
}
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
65.9M
{
1140
65.9M
    int i;
1141
65.9M
    int ptag, pclass;
1142
65.9M
    long plen;
1143
65.9M
    const unsigned char *p, *q;
1144
65.9M
    p = *in;
1145
65.9M
    q = p;
1146
1147
65.9M
    if (len <= 0) {
1148
12
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
1149
12
        goto err;
1150
12
    }
1151
65.9M
    if (ctx != NULL && ctx->valid) {
1152
2.79M
        i = ctx->ret;
1153
2.79M
        plen = ctx->plen;
1154
2.79M
        pclass = ctx->pclass;
1155
2.79M
        ptag = ctx->ptag;
1156
2.79M
        p += ctx->hdrlen;
1157
63.1M
    } else {
1158
63.1M
        i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1159
63.1M
        if (ctx != NULL) {
1160
11.4M
            ctx->ret = i;
1161
11.4M
            ctx->plen = plen;
1162
11.4M
            ctx->pclass = pclass;
1163
11.4M
            ctx->ptag = ptag;
1164
11.4M
            ctx->hdrlen = p - q;
1165
11.4M
            ctx->valid = 1;
1166
            /*
1167
             * If definite length, and no error, length + header can't exceed
1168
             * total amount of data available.
1169
             */
1170
11.4M
            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
11.4M
        }
1175
63.1M
    }
1176
1177
65.9M
    if ((i & 0x80) != 0) {
1178
105k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER);
1179
105k
        goto err;
1180
105k
    }
1181
65.8M
    if (exptag >= 0) {
1182
12.3M
        if (exptag != ptag || expclass != pclass) {
1183
            /*
1184
             * If type is OPTIONAL, not an error: indicate missing type.
1185
             */
1186
3.24M
            if (opt != 0)
1187
1.21M
                return -1;
1188
3.24M
            ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG);
1189
2.02M
            goto err;
1190
3.24M
        }
1191
        /*
1192
         * We have a tag and class match: assume we are going to do something
1193
         * with it
1194
         */
1195
9.06M
        asn1_tlc_clear(ctx);
1196
9.06M
    }
1197
1198
62.5M
    if ((i & 1) != 0)
1199
16.2M
        plen = len - (p - q);
1200
1201
62.5M
    if (inf != NULL)
1202
60.8M
        *inf = i & 1;
1203
1204
62.5M
    if (cst != NULL)
1205
8.03M
        *cst = i & V_ASN1_CONSTRUCTED;
1206
1207
62.5M
    if (olen != NULL)
1208
60.8M
        *olen = plen;
1209
1210
62.5M
    if (oclass != NULL)
1211
1.76M
        *oclass = pclass;
1212
1213
62.5M
    if (otag != NULL)
1214
1.76M
        *otag = ptag;
1215
1216
62.5M
    *in = p;
1217
62.5M
    return 1;
1218
1219
2.13M
 err:
1220
2.13M
    asn1_tlc_clear(ctx);
1221
2.13M
    return 0;
1222
65.8M
}