Coverage Report

Created: 2026-07-23 06:28

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