Coverage Report

Created: 2026-04-09 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/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
373M
#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
43.8M
{
107
43.8M
    if ((tag < 0) || (tag > 30))
108
5.88k
        return 0;
109
43.8M
    return tag2bit[tag];
110
43.8M
}
111
112
/* Macro to initialize and invalidate the cache */
113
114
#define asn1_tlc_clear(c)   \
115
345M
    do {                    \
116
345M
        if ((c) != NULL)    \
117
345M
            (c)->valid = 0; \
118
345M
    } while (0)
119
/* Version to avoid compiler warning about 'c' always non-NULL */
120
#define asn1_tlc_clear_nc(c) \
121
24.0M
    do {                     \
122
24.0M
        (c)->valid = 0;      \
123
24.0M
    } 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
26.8M
{
137
26.8M
    int rv;
138
139
26.8M
    if (pval == NULL || it == NULL) {
140
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
141
0
        return 0;
142
0
    }
143
26.8M
    rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0,
144
26.8M
        libctx, propq);
145
26.8M
    if (rv <= 0)
146
16.4M
        ASN1_item_ex_free(pval, it);
147
26.8M
    return rv;
148
26.8M
}
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.77M
{
154
2.77M
    return asn1_item_ex_d2i_intern(pval, in, len, it, tag, aclass, opt, ctx,
155
2.77M
        NULL, NULL);
156
2.77M
}
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
24.0M
{
163
24.0M
    ASN1_TLC c;
164
24.0M
    ASN1_VALUE *ptmpval = NULL;
165
166
24.0M
    if (pval == NULL)
167
21.2M
        pval = &ptmpval;
168
24.0M
    asn1_tlc_clear_nc(&c);
169
24.0M
    if (asn1_item_ex_d2i_intern(pval, in, len, it, -1, 0, 0, &c, libctx,
170
24.0M
            propq)
171
24.0M
        > 0)
172
7.96M
        return *pval;
173
16.1M
    return NULL;
174
24.0M
}
175
176
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
177
    const unsigned char **in, long len,
178
    const ASN1_ITEM *it)
179
22.5M
{
180
22.5M
    return ASN1_item_d2i_ex(pval, in, len, it, NULL, NULL);
181
22.5M
}
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
373M
{
194
373M
    const ASN1_TEMPLATE *tt, *errtt = NULL;
195
373M
    const ASN1_EXTERN_FUNCS *ef;
196
373M
    const ASN1_AUX *aux;
197
373M
    ASN1_aux_cb *asn1_cb;
198
373M
    const unsigned char *p = NULL, *q;
199
373M
    unsigned char oclass;
200
373M
    char seq_eoc, seq_nolen, cst, isopt;
201
373M
    long tmplen;
202
373M
    int i;
203
373M
    int otag;
204
373M
    int ret = 0;
205
373M
    ASN1_VALUE **pchptr;
206
207
373M
    if (pval == NULL || it == NULL) {
208
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
209
0
        return 0;
210
0
    }
211
373M
    if (len <= 0) {
212
80.5k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
213
80.5k
        return 0;
214
80.5k
    }
215
373M
    aux = it->funcs;
216
373M
    if (aux && aux->asn1_cb)
217
13.1M
        asn1_cb = aux->asn1_cb;
218
360M
    else
219
360M
        asn1_cb = 0;
220
221
373M
    if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
222
16
        ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_TOO_DEEP);
223
16
        goto err;
224
16
    }
225
226
373M
    switch (it->itype) {
227
277M
    case ASN1_ITYPE_PRIMITIVE:
228
277M
        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
144M
            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
144M
            return asn1_template_ex_d2i(pval, in, len, it->templates, opt, ctx,
241
144M
                depth, libctx, propq);
242
144M
        }
243
133M
        return asn1_d2i_ex_primitive(pval, in, len, it,
244
133M
            tag, aclass, opt, ctx);
245
246
22.0M
    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.0M
        if (tag != -1) {
252
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
253
0
            goto err;
254
0
        }
255
256
22.0M
        p = *in;
257
        /* Just read in tag and class */
258
22.0M
        ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
259
22.0M
            &p, len, -1, 0, 1, ctx);
260
22.0M
        if (!ret) {
261
21.8k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
262
21.8k
            goto err;
263
21.8k
        }
264
265
        /* Must be UNIVERSAL class */
266
22.0M
        if (oclass != V_ASN1_UNIVERSAL) {
267
            /* If OPTIONAL, assume this is OK */
268
152k
            if (opt)
269
118k
                return -1;
270
152k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
271
33.6k
            goto err;
272
152k
        }
273
274
        /* Check tag matches bit map */
275
21.8M
        if (!(ASN1_tag2bit(otag) & it->utype)) {
276
            /* If OPTIONAL, assume this is OK */
277
421k
            if (opt)
278
16.3k
                return -1;
279
421k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_WRONG_TAG);
280
404k
            goto err;
281
421k
        }
282
21.4M
        return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
283
284
5.54M
    case ASN1_ITYPE_EXTERN:
285
        /* Use new style d2i */
286
5.54M
        ef = it->funcs;
287
5.54M
        if (ef->asn1_ex_d2i_ex != NULL)
288
2.76M
            return ef->asn1_ex_d2i_ex(pval, in, len, it, tag, aclass, opt, ctx,
289
2.76M
                libctx, propq);
290
2.77M
        return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
291
292
6.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
6.10M
        if (tag != -1) {
298
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
299
0
            goto err;
300
0
        }
301
302
6.10M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
303
0
            goto auxerr;
304
6.10M
        if (*pval) {
305
            /* Free up and zero CHOICE value if initialised */
306
792k
            i = ossl_asn1_get_choice_selector(pval, it);
307
792k
            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
5.31M
        } 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
6.10M
        p = *in;
319
22.8M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
320
22.2M
            pchptr = ossl_asn1_get_field_ptr(pval, tt);
321
            /*
322
             * We mark field as OPTIONAL so its absence can be recognised.
323
             */
324
22.2M
            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth,
325
22.2M
                libctx, propq);
326
            /* If field not present, try the next one */
327
22.2M
            if (ret == -1)
328
16.7M
                continue;
329
            /* If positive return, read OK, break loop */
330
5.40M
            if (ret > 0)
331
4.39M
                break;
332
            /*
333
             * Must be an ASN1 parsing error.
334
             * Free up any partial choice value
335
             */
336
1.01M
            ossl_asn1_template_free(pchptr, tt);
337
1.01M
            errtt = tt;
338
1.01M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
339
1.01M
            goto err;
340
5.40M
        }
341
342
        /* Did we fall off the end without reading anything? */
343
5.09M
        if (i == it->tcount) {
344
            /* If OPTIONAL, this is OK */
345
695k
            if (opt) {
346
                /* Free and zero it */
347
31.7k
                ASN1_item_ex_free(pval, it);
348
31.7k
                return -1;
349
31.7k
            }
350
695k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
351
663k
            goto err;
352
695k
        }
353
354
4.39M
        ossl_asn1_set_choice_selector(pval, i, it);
355
356
4.39M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
357
0
            goto auxerr;
358
4.39M
        *in = p;
359
4.39M
        return 1;
360
361
861k
    case ASN1_ITYPE_NDEF_SEQUENCE:
362
61.8M
    case ASN1_ITYPE_SEQUENCE:
363
61.8M
        p = *in;
364
61.8M
        tmplen = len;
365
366
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
367
61.8M
        if (tag == -1) {
368
56.5M
            tag = V_ASN1_SEQUENCE;
369
56.5M
            aclass = V_ASN1_UNIVERSAL;
370
56.5M
        }
371
        /* Get SEQUENCE length and update len, p */
372
61.8M
        ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
373
61.8M
            &p, len, tag, aclass, opt, ctx);
374
61.8M
        if (!ret) {
375
4.41M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
376
4.41M
            goto err;
377
57.4M
        } else if (ret == -1)
378
5.76M
            return -1;
379
51.6M
        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
51.6M
        else
385
51.6M
            seq_nolen = seq_eoc;
386
51.6M
        if (!cst) {
387
119k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
388
119k
            goto err;
389
119k
        }
390
391
51.5M
        if (*pval == NULL
392
38.8M
            && !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
51.5M
        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
195M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
402
144M
            if (tt->flags & ASN1_TFLG_ADB_MASK) {
403
778k
                const ASN1_TEMPLATE *seqtt;
404
778k
                ASN1_VALUE **pseqval;
405
778k
                seqtt = ossl_asn1_do_adb(*pval, tt, 0);
406
778k
                if (seqtt == NULL)
407
628k
                    continue;
408
149k
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
409
149k
                ossl_asn1_template_free(pseqval, seqtt);
410
149k
            }
411
144M
        }
412
413
        /* Get each field entry */
414
150M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
415
115M
            const ASN1_TEMPLATE *seqtt;
416
115M
            ASN1_VALUE **pseqval;
417
115M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
418
115M
            if (seqtt == NULL)
419
0
                goto err;
420
115M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
421
            /* Have we ran out of data? */
422
115M
            if (!len)
423
2.79M
                break;
424
112M
            q = p;
425
112M
            if (asn1_check_eoc(&p, len)) {
426
3.73M
                if (!seq_eoc) {
427
31.4k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
428
31.4k
                    goto err;
429
31.4k
                }
430
3.70M
                len -= p - q;
431
3.70M
                seq_eoc = 0;
432
3.70M
                break;
433
3.73M
            }
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
108M
            if (i == (it->tcount - 1))
441
36.9M
                isopt = 0;
442
71.7M
            else
443
71.7M
                isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
444
            /*
445
             * attempt to read in field, allowing each to be OPTIONAL
446
             */
447
448
108M
            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
449
108M
                depth, libctx, propq);
450
108M
            if (!ret) {
451
9.78M
                errtt = seqtt;
452
9.78M
                goto err;
453
98.9M
            } else if (ret == -1) {
454
                /*
455
                 * OPTIONAL component absent. Free and zero the field.
456
                 */
457
10.2M
                ossl_asn1_template_free(pseqval, seqtt);
458
10.2M
                continue;
459
10.2M
            }
460
            /* Update length */
461
88.6M
            len -= p - q;
462
88.6M
        }
463
464
        /* Check for EOC if expecting one */
465
41.7M
        if (seq_eoc && !asn1_check_eoc(&p, len)) {
466
181k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
467
181k
            goto err;
468
181k
        }
469
        /* Check all data read */
470
41.5M
        if (!seq_nolen && len) {
471
31.1k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
472
31.1k
            goto err;
473
31.1k
        }
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
55.3M
        for (; i < it->tcount; tt++, i++) {
481
14.1M
            const ASN1_TEMPLATE *seqtt;
482
14.1M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
483
14.1M
            if (seqtt == NULL)
484
0
                goto err;
485
14.1M
            if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
486
13.8M
                ASN1_VALUE **pseqval;
487
13.8M
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
488
13.8M
                ossl_asn1_template_free(pseqval, seqtt);
489
13.8M
            } else {
490
280k
                errtt = seqtt;
491
280k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_FIELD_MISSING);
492
280k
                goto err;
493
280k
            }
494
14.1M
        }
495
        /* Save encoding */
496
41.2M
        if (!ossl_asn1_enc_save(pval, *in, p - *in, it))
497
0
            goto auxerr;
498
41.2M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
499
22.8k
            goto auxerr;
500
41.1M
        *in = p;
501
41.1M
        return 1;
502
503
0
    default:
504
0
        return 0;
505
373M
    }
506
22.8k
auxerr:
507
22.8k
    ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
508
17.0M
err:
509
17.0M
    if (errtt)
510
11.0M
        ERR_add_error_data(4, "Field=", errtt->field_name,
511
11.0M
            ", Type=", it->sname);
512
5.92M
    else
513
5.92M
        ERR_add_error_data(2, "Type=", it->sname);
514
17.0M
    return 0;
515
22.8k
}
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
274M
{
528
274M
    int flags, aclass;
529
274M
    int ret;
530
274M
    long len;
531
274M
    const unsigned char *p, *q;
532
274M
    char exp_eoc;
533
274M
    if (!val)
534
0
        return 0;
535
274M
    flags = tt->flags;
536
274M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
537
538
274M
    p = *in;
539
540
    /* Check if EXPLICIT tag expected */
541
274M
    if (flags & ASN1_TFLG_EXPTAG) {
542
7.59M
        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.59M
        ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
548
7.59M
            &p, inlen, tt->tag, aclass, opt, ctx);
549
7.59M
        q = p;
550
7.59M
        if (!ret) {
551
369k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
552
369k
            return 0;
553
7.22M
        } else if (ret == -1)
554
5.42M
            return -1;
555
1.79M
        if (!cst) {
556
6.49k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
557
6.49k
            return 0;
558
6.49k
        }
559
        /* We've found the field so it can't be OPTIONAL now */
560
1.79M
        ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth, libctx,
561
1.79M
            propq);
562
1.79M
        if (!ret) {
563
314k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
564
314k
            return 0;
565
314k
        }
566
        /* We read the field in OK so update length */
567
1.47M
        len -= p - q;
568
1.47M
        if (exp_eoc) {
569
            /* If NDEF we must have an EOC here */
570
450k
            if (!asn1_check_eoc(&p, len)) {
571
44.3k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
572
44.3k
                goto err;
573
44.3k
            }
574
1.02M
        } else {
575
            /*
576
             * Otherwise we must hit the EXPLICIT tag end or its an error
577
             */
578
1.02M
            if (len) {
579
4.23k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
580
4.23k
                goto err;
581
4.23k
            }
582
1.02M
        }
583
1.47M
    } else
584
267M
        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth,
585
267M
            libctx, propq);
586
587
1.42M
    *in = p;
588
1.42M
    return 1;
589
590
48.6k
err:
591
48.6k
    return 0;
592
274M
}
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
269M
{
600
269M
    int flags, aclass;
601
269M
    int ret;
602
269M
    ASN1_VALUE *tval;
603
269M
    const unsigned char *p, *q;
604
269M
    if (!val)
605
0
        return 0;
606
269M
    flags = tt->flags;
607
269M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
608
609
269M
    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
269M
    if (tt->flags & ASN1_TFLG_EMBED) {
616
12.9M
        tval = (ASN1_VALUE *)val;
617
12.9M
        val = &tval;
618
12.9M
    }
619
620
269M
    if (flags & ASN1_TFLG_SK_MASK) {
621
        /* SET OF, SEQUENCE OF */
622
147M
        int sktag, skaclass;
623
147M
        char sk_eoc;
624
        /* First work out expected inner tag value */
625
147M
        if (flags & ASN1_TFLG_IMPTAG) {
626
1.35M
            sktag = tt->tag;
627
1.35M
            skaclass = aclass;
628
146M
        } else {
629
146M
            skaclass = V_ASN1_UNIVERSAL;
630
146M
            if (flags & ASN1_TFLG_SET_OF)
631
139M
                sktag = V_ASN1_SET;
632
7.12M
            else
633
7.12M
                sktag = V_ASN1_SEQUENCE;
634
146M
        }
635
        /* Get the tag */
636
147M
        ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
637
147M
            &p, len, sktag, skaclass, opt, ctx);
638
147M
        if (!ret) {
639
1.32M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
640
1.32M
            return 0;
641
146M
        } else if (ret == -1)
642
794k
            return -1;
643
145M
        if (*val == NULL)
644
145M
            *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
645
696k
        else {
646
            /*
647
             * We've got a valid STACK: free up any items present
648
             */
649
696k
            STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
650
696k
            ASN1_VALUE *vtmp;
651
696k
            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
696k
        }
656
657
145M
        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
366M
        while (len > 0) {
664
229M
            ASN1_VALUE *skfield;
665
229M
            q = p;
666
            /* See if EOC found */
667
229M
            if (asn1_check_eoc(&p, len)) {
668
6.43M
                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.42M
                len -= p - q;
673
6.42M
                sk_eoc = 0;
674
6.42M
                break;
675
6.43M
            }
676
222M
            skfield = NULL;
677
222M
            if (asn1_item_embed_d2i(&skfield, &p, len,
678
222M
                    ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
679
222M
                    depth, libctx, propq)
680
222M
                <= 0) {
681
1.94M
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
682
                /* |skfield| may be partially allocated despite failure. */
683
1.94M
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
684
1.94M
                goto err;
685
1.94M
            }
686
220M
            len -= p - q;
687
220M
            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
220M
        }
693
143M
        if (sk_eoc) {
694
14.7k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
695
14.7k
            goto err;
696
14.7k
        }
697
143M
    } else if (flags & ASN1_TFLG_IMPTAG) {
698
        /* IMPLICIT tagging */
699
18.4M
        ret = asn1_item_embed_d2i(val, &p, len,
700
18.4M
            ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
701
18.4M
            ctx, depth, libctx, propq);
702
18.4M
        if (!ret) {
703
536k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
704
536k
            goto err;
705
17.9M
        } else if (ret == -1)
706
14.4M
            return -1;
707
102M
    } else {
708
        /* Nothing special */
709
102M
        ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
710
102M
            -1, 0, opt, ctx, depth, libctx, propq);
711
102M
        if (!ret) {
712
8.65M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
713
8.65M
            goto err;
714
94.1M
        } else if (ret == -1)
715
6.37M
            return -1;
716
102M
    }
717
718
235M
    *in = p;
719
235M
    return 1;
720
721
11.1M
err:
722
11.1M
    return 0;
723
269M
}
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
155M
{
730
155M
    int ret = 0, utype;
731
155M
    long plen;
732
155M
    char cst, inf, free_cont = 0;
733
155M
    const unsigned char *p;
734
155M
    BUF_MEM buf = { 0, NULL, 0, 0 };
735
155M
    const unsigned char *cont = NULL;
736
155M
    long len;
737
738
155M
    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
155M
    if (it->itype == ASN1_ITYPE_MSTRING) {
744
21.4M
        utype = tag;
745
21.4M
        tag = -1;
746
21.4M
    } else
747
133M
        utype = it->utype;
748
749
155M
    if (utype == V_ASN1_ANY) {
750
        /* If type is ANY need to figure out type from tag */
751
54.2M
        unsigned char oclass;
752
54.2M
        if (tag >= 0) {
753
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
754
0
            return 0;
755
0
        }
756
54.2M
        if (opt) {
757
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
758
0
            return 0;
759
0
        }
760
54.2M
        p = *in;
761
54.2M
        ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
762
54.2M
            &p, inlen, -1, 0, 0, ctx);
763
54.2M
        if (!ret) {
764
34.9k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
765
34.9k
            return 0;
766
34.9k
        }
767
54.2M
        if (oclass != V_ASN1_UNIVERSAL)
768
3.85M
            utype = V_ASN1_OTHER;
769
54.2M
    }
770
155M
    if (tag == -1) {
771
142M
        tag = utype;
772
142M
        aclass = V_ASN1_UNIVERSAL;
773
142M
    }
774
155M
    p = *in;
775
    /* Check header */
776
155M
    ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
777
155M
        &p, inlen, tag, aclass, opt, ctx);
778
155M
    if (!ret) {
779
6.92M
        ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
780
6.92M
        return 0;
781
148M
    } else if (ret == -1)
782
14.8M
        return -1;
783
133M
    ret = 0;
784
    /* SEQUENCE, SET and "OTHER" are left in encoded form */
785
133M
    if ((utype == V_ASN1_SEQUENCE)
786
129M
        || (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
24.7M
        if (utype == V_ASN1_OTHER) {
792
3.85M
            asn1_tlc_clear(ctx);
793
3.85M
        }
794
        /* SEQUENCE and SET must be constructed */
795
20.8M
        else if (!cst) {
796
16.0k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
797
16.0k
            return 0;
798
16.0k
        }
799
800
24.7M
        cont = *in;
801
        /* If indefinite length constructed find the real end */
802
24.7M
        if (inf) {
803
1.24M
            if (!asn1_find_end(&p, plen, inf))
804
112k
                goto err;
805
1.12M
            len = p - cont;
806
23.4M
        } else {
807
23.4M
            len = p - cont + plen;
808
23.4M
            p += plen;
809
23.4M
        }
810
108M
    } else if (cst) {
811
8.72M
        if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
812
8.71M
            || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
813
8.70M
            || utype == V_ASN1_ENUMERATED) {
814
28.3k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
815
28.3k
            return 0;
816
28.3k
        }
817
818
        /* Free any returned 'buf' content */
819
8.69M
        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
8.69M
        if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
827
69.3k
            goto err;
828
69.3k
        }
829
8.62M
        len = buf.length;
830
        /* Append a final null to string */
831
8.62M
        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
8.62M
        buf.data[len] = 0;
836
8.62M
        cont = (const unsigned char *)buf.data;
837
100M
    } else {
838
100M
        cont = p;
839
100M
        len = plen;
840
100M
        p += plen;
841
100M
    }
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
133M
    if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
846
837k
        goto err;
847
848
132M
    *in = p;
849
132M
    ret = 1;
850
133M
err:
851
133M
    if (free_cont)
852
487k
        OPENSSL_free(buf.data);
853
133M
    return ret;
854
132M
}
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
120M
{
861
120M
    ASN1_VALUE **opval = NULL;
862
120M
    ASN1_STRING *stmp;
863
120M
    ASN1_TYPE *typ = NULL;
864
120M
    int ret = 0;
865
120M
    const ASN1_PRIMITIVE_FUNCS *pf;
866
120M
    ASN1_INTEGER **tint;
867
120M
    pf = it->funcs;
868
869
120M
    if (pf && pf->prim_c2i)
870
3.30M
        return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
871
    /* If ANY type clear type and set pointer to internal value */
872
117M
    if (it->utype == V_ASN1_ANY) {
873
52.5M
        if (*pval == NULL) {
874
52.4M
            typ = ASN1_TYPE_new();
875
52.4M
            if (typ == NULL)
876
0
                goto err;
877
52.4M
            *pval = (ASN1_VALUE *)typ;
878
52.4M
        } else
879
119k
            typ = (ASN1_TYPE *)*pval;
880
881
52.5M
        if (utype != typ->type)
882
52.5M
            ASN1_TYPE_set(typ, utype, NULL);
883
52.5M
        opval = pval;
884
52.5M
        pval = &typ->value.asn1_value;
885
52.5M
    }
886
117M
    switch (utype) {
887
28.8M
    case V_ASN1_OBJECT:
888
28.8M
        if (!ossl_c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
889
49.9k
            goto err;
890
28.8M
        break;
891
892
28.8M
    case V_ASN1_NULL:
893
935k
        if (len) {
894
4.62k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
895
4.62k
            goto err;
896
4.62k
        }
897
930k
        *pval = (ASN1_VALUE *)1;
898
930k
        break;
899
900
3.56M
    case V_ASN1_BOOLEAN:
901
3.56M
        if (len != 1) {
902
8.26k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
903
8.26k
            goto err;
904
3.55M
        } else {
905
3.55M
            ASN1_BOOLEAN *tbool;
906
3.55M
            tbool = (ASN1_BOOLEAN *)pval;
907
3.55M
            *tbool = *cont;
908
3.55M
        }
909
3.55M
        break;
910
911
6.47M
    case V_ASN1_BIT_STRING:
912
6.47M
        if (!ossl_c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
913
48.8k
            goto err;
914
6.42M
        break;
915
916
10.8M
    case V_ASN1_INTEGER:
917
12.5M
    case V_ASN1_ENUMERATED:
918
12.5M
        tint = (ASN1_INTEGER **)pval;
919
12.5M
        if (!ossl_c2i_ASN1_INTEGER(tint, &cont, len))
920
523k
            goto err;
921
        /* Fixup type to match the expected form */
922
11.9M
        (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
923
11.9M
        break;
924
925
21.6M
    case V_ASN1_OCTET_STRING:
926
23.9M
    case V_ASN1_NUMERICSTRING:
927
27.4M
    case V_ASN1_PRINTABLESTRING:
928
28.1M
    case V_ASN1_T61STRING:
929
28.1M
    case V_ASN1_VIDEOTEXSTRING:
930
30.0M
    case V_ASN1_IA5STRING:
931
32.1M
    case V_ASN1_UTCTIME:
932
32.3M
    case V_ASN1_GENERALIZEDTIME:
933
32.8M
    case V_ASN1_GRAPHICSTRING:
934
32.8M
    case V_ASN1_VISIBLESTRING:
935
32.8M
    case V_ASN1_GENERALSTRING:
936
33.6M
    case V_ASN1_UNIVERSALSTRING:
937
34.5M
    case V_ASN1_BMPSTRING:
938
36.4M
    case V_ASN1_UTF8STRING:
939
40.2M
    case V_ASN1_OTHER:
940
56.4M
    case V_ASN1_SET:
941
59.8M
    case V_ASN1_SEQUENCE:
942
65.1M
    default:
943
65.1M
        if (utype == V_ASN1_BMPSTRING && (len & 1)) {
944
2.12k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
945
2.12k
            goto err;
946
2.12k
        }
947
65.1M
        if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
948
2.31k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
949
2.31k
            goto err;
950
2.31k
        }
951
65.1M
        if (utype == V_ASN1_GENERALIZEDTIME && (len < 15)) {
952
6.77k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_GENERALIZEDTIME_IS_TOO_SHORT);
953
6.77k
            goto err;
954
6.77k
        }
955
65.1M
        if (utype == V_ASN1_UTCTIME && (len < 13)) {
956
3.02k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UTCTIME_IS_TOO_SHORT);
957
3.02k
            goto err;
958
3.02k
        }
959
        /* All based on ASN1_STRING and handled the same */
960
65.1M
        if (*pval == NULL) {
961
44.6M
            stmp = ASN1_STRING_type_new(utype);
962
44.6M
            if (stmp == NULL) {
963
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
964
0
                goto err;
965
0
            }
966
44.6M
            *pval = (ASN1_VALUE *)stmp;
967
44.6M
        } else {
968
20.4M
            stmp = (ASN1_STRING *)*pval;
969
20.4M
            stmp->type = utype;
970
20.4M
        }
971
        /* If we've already allocated a buffer use it */
972
65.1M
        if (*free_cont) {
973
7.08M
            ASN1_STRING_set0(stmp, (unsigned char *)cont /* UGLY CAST! */, len);
974
7.08M
            *free_cont = 0;
975
58.0M
        } else {
976
58.0M
            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
58.0M
        }
983
65.1M
        break;
984
117M
    }
985
    /* If ASN1_ANY and NULL type fix up value */
986
116M
    if (typ && (utype == V_ASN1_NULL))
987
912k
        typ->value.ptr = NULL;
988
989
116M
    ret = 1;
990
117M
err:
991
117M
    if (!ret) {
992
649k
        ASN1_TYPE_free(typ);
993
649k
        if (opval)
994
36.4k
            *opval = NULL;
995
649k
    }
996
117M
    return ret;
997
116M
}
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.24M
{
1008
1.24M
    uint32_t expected_eoc;
1009
1.24M
    long plen;
1010
1.24M
    const unsigned char *p = *in, *q;
1011
    /* If not indefinite length constructed just add length */
1012
1.24M
    if (inf == 0) {
1013
0
        *in += len;
1014
0
        return 1;
1015
0
    }
1016
1.24M
    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
426M
    while (len > 0) {
1024
426M
        if (asn1_check_eoc(&p, len)) {
1025
40.0M
            expected_eoc--;
1026
40.0M
            if (expected_eoc == 0)
1027
1.12M
                break;
1028
38.9M
            len -= 2;
1029
38.9M
            continue;
1030
40.0M
        }
1031
386M
        q = p;
1032
        /* Just read in a header: only care about the length */
1033
386M
        if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1034
386M
                -1, 0, 0, NULL)) {
1035
70.5k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1036
70.5k
            return 0;
1037
70.5k
        }
1038
386M
        if (inf) {
1039
98.2M
            if (expected_eoc == UINT32_MAX) {
1040
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1041
0
                return 0;
1042
0
            }
1043
98.2M
            expected_eoc++;
1044
288M
        } else {
1045
288M
            p += plen;
1046
288M
        }
1047
386M
        len -= p - q;
1048
386M
    }
1049
1.16M
    if (expected_eoc) {
1050
41.6k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1051
41.6k
        return 0;
1052
41.6k
    }
1053
1.12M
    *in = p;
1054
1.12M
    return 1;
1055
1.16M
}
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
1.80M
#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
10.5M
{
1076
10.5M
    const unsigned char *p, *q;
1077
10.5M
    long plen;
1078
10.5M
    char cst, ininf;
1079
10.5M
    p = *in;
1080
10.5M
    inf &= 1;
1081
    /*
1082
     * If no buffer and not indefinite length constructed just pass over the
1083
     * encoded data
1084
     */
1085
10.5M
    if (!buf && !inf) {
1086
0
        *in += len;
1087
0
        return 1;
1088
0
    }
1089
58.4M
    while (len > 0) {
1090
49.0M
        q = p;
1091
        /* Check for EOC */
1092
49.0M
        if (asn1_check_eoc(&p, len)) {
1093
            /*
1094
             * EOC is illegal outside indefinite length constructed form
1095
             */
1096
1.08M
            if (!inf) {
1097
11.2k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
1098
11.2k
                return 0;
1099
11.2k
            }
1100
1.07M
            inf = 0;
1101
1.07M
            break;
1102
1.08M
        }
1103
1104
48.0M
        if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1105
48.0M
                len, tag, aclass, 0, NULL)) {
1106
40.4k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1107
40.4k
            return 0;
1108
40.4k
        }
1109
1110
        /* If indefinite length constructed update max length */
1111
47.9M
        if (cst) {
1112
1.80M
            if (depth >= ASN1_MAX_STRING_NEST) {
1113
2.25k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_ASN1_STRING);
1114
2.25k
                return 0;
1115
2.25k
            }
1116
1.80M
            if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1117
52.9k
                return 0;
1118
46.1M
        } else if (plen && !collect_data(buf, &p, plen))
1119
0
            return 0;
1120
47.9M
        len -= p - q;
1121
47.9M
    }
1122
10.3M
    if (inf) {
1123
15.4k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1124
15.4k
        return 0;
1125
15.4k
    }
1126
10.3M
    *in = p;
1127
10.3M
    return 1;
1128
10.3M
}
1129
1130
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1131
20.5M
{
1132
20.5M
    int len;
1133
20.5M
    if (buf) {
1134
20.5M
        len = buf->length;
1135
20.5M
        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
20.5M
        memcpy(buf->data + len, *p, plen);
1140
20.5M
    }
1141
20.5M
    *p += plen;
1142
20.5M
    return 1;
1143
20.5M
}
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
829M
{
1149
829M
    const unsigned char *p;
1150
1151
829M
    if (len < 2)
1152
192k
        return 0;
1153
829M
    p = *in;
1154
829M
    if (p[0] == '\0' && p[1] == '\0') {
1155
63.3M
        *in += 2;
1156
63.3M
        return 1;
1157
63.3M
    }
1158
765M
    return 0;
1159
829M
}
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
883M
{
1173
883M
    int i;
1174
883M
    int ptag, pclass;
1175
883M
    long plen;
1176
883M
    const unsigned char *p, *q;
1177
883M
    p = *in;
1178
883M
    q = p;
1179
1180
883M
    if (len <= 0) {
1181
504
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
1182
504
        goto err;
1183
504
    }
1184
883M
    if (ctx != NULL && ctx->valid) {
1185
101M
        i = ctx->ret;
1186
101M
        plen = ctx->plen;
1187
101M
        pclass = ctx->pclass;
1188
101M
        ptag = ctx->ptag;
1189
101M
        p += ctx->hdrlen;
1190
781M
    } else {
1191
781M
        i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1192
781M
        if (ctx != NULL) {
1193
346M
            ctx->ret = i;
1194
346M
            ctx->plen = plen;
1195
346M
            ctx->pclass = pclass;
1196
346M
            ctx->ptag = ptag;
1197
346M
            ctx->hdrlen = p - q;
1198
346M
            ctx->valid = 1;
1199
            /*
1200
             * If definite length, and no error, length + header can't exceed
1201
             * total amount of data available.
1202
             */
1203
346M
            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
346M
        }
1208
781M
    }
1209
1210
883M
    if ((i & 0x80) != 0) {
1211
1.12M
        ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER);
1212
1.12M
        goto err;
1213
1.12M
    }
1214
882M
    if (exptag >= 0) {
1215
367M
        if (exptag != ptag || expclass != pclass) {
1216
            /*
1217
             * If type is OPTIONAL, not an error: indicate missing type.
1218
             */
1219
38.9M
            if (opt != 0)
1220
26.8M
                return -1;
1221
38.9M
            ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG);
1222
12.0M
            goto err;
1223
38.9M
        }
1224
        /*
1225
         * We have a tag and class match: assume we are going to do something
1226
         * with it
1227
         */
1228
328M
        asn1_tlc_clear(ctx);
1229
328M
    }
1230
1231
843M
    if ((i & 1) != 0)
1232
133M
        plen = len - (p - q);
1233
1234
843M
    if (inf != NULL)
1235
767M
        *inf = i & 1;
1236
1237
843M
    if (cst != NULL)
1238
234M
        *cst = i & V_ASN1_CONSTRUCTED;
1239
1240
843M
    if (olen != NULL)
1241
767M
        *olen = plen;
1242
1243
843M
    if (oclass != NULL)
1244
76.2M
        *oclass = pclass;
1245
1246
843M
    if (otag != NULL)
1247
76.2M
        *otag = ptag;
1248
1249
843M
    *in = p;
1250
843M
    return 1;
1251
1252
13.1M
err:
1253
    asn1_tlc_clear(ctx);
1254
13.1M
    return 0;
1255
882M
}