Coverage Report

Created: 2026-04-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/asn1/tasn_dec.c
Line
Count
Source
1
/*
2
 * Copyright 2000-2025 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
389M
#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.9M
{
107
43.9M
    if ((tag < 0) || (tag > 30))
108
5.71k
        return 0;
109
43.9M
    return tag2bit[tag];
110
43.9M
}
111
112
/* Macro to initialize and invalidate the cache */
113
114
#define asn1_tlc_clear(c)   \
115
359M
    do {                    \
116
359M
        if ((c) != NULL)    \
117
359M
            (c)->valid = 0; \
118
359M
    } while (0)
119
/* Version to avoid compiler warning about 'c' always non-NULL */
120
#define asn1_tlc_clear_nc(c) \
121
24.2M
    do {                     \
122
24.2M
        (c)->valid = 0;      \
123
24.2M
    } 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
27.0M
{
137
27.0M
    int rv;
138
139
27.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
27.0M
    rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0,
144
27.0M
        libctx, propq);
145
27.0M
    if (rv <= 0)
146
16.6M
        ASN1_item_ex_free(pval, it);
147
27.0M
    return rv;
148
27.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.73M
{
154
2.73M
    return asn1_item_ex_d2i_intern(pval, in, len, it, tag, aclass, opt, ctx,
155
2.73M
        NULL, NULL);
156
2.73M
}
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.2M
{
163
24.2M
    ASN1_TLC c;
164
24.2M
    ASN1_VALUE *ptmpval = NULL;
165
166
24.2M
    if (pval == NULL)
167
21.4M
        pval = &ptmpval;
168
24.2M
    asn1_tlc_clear_nc(&c);
169
24.2M
    if (asn1_item_ex_d2i_intern(pval, in, len, it, -1, 0, 0, &c, libctx,
170
24.2M
            propq)
171
24.2M
        > 0)
172
7.92M
        return *pval;
173
16.3M
    return NULL;
174
24.2M
}
175
176
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
177
    const unsigned char **in, long len,
178
    const ASN1_ITEM *it)
179
22.7M
{
180
22.7M
    return ASN1_item_d2i_ex(pval, in, len, it, NULL, NULL);
181
22.7M
}
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
389M
{
194
389M
    const ASN1_TEMPLATE *tt, *errtt = NULL;
195
389M
    const ASN1_EXTERN_FUNCS *ef;
196
389M
    const ASN1_AUX *aux;
197
389M
    ASN1_aux_cb *asn1_cb;
198
389M
    const unsigned char *p = NULL, *q;
199
389M
    unsigned char oclass;
200
389M
    char seq_eoc, seq_nolen, cst, isopt;
201
389M
    long tmplen;
202
389M
    int i;
203
389M
    int otag;
204
389M
    int ret = 0;
205
389M
    ASN1_VALUE **pchptr;
206
207
389M
    if (pval == NULL || it == NULL) {
208
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
209
0
        return 0;
210
0
    }
211
389M
    if (len <= 0) {
212
79.8k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
213
79.8k
        return 0;
214
79.8k
    }
215
389M
    aux = it->funcs;
216
389M
    if (aux && aux->asn1_cb)
217
13.3M
        asn1_cb = aux->asn1_cb;
218
376M
    else
219
376M
        asn1_cb = 0;
220
221
389M
    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
389M
    switch (it->itype) {
227
292M
    case ASN1_ITYPE_PRIMITIVE:
228
292M
        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
147M
            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
147M
            return asn1_template_ex_d2i(pval, in, len, it->templates, opt, ctx,
241
147M
                depth, libctx, propq);
242
147M
        }
243
144M
        return asn1_d2i_ex_primitive(pval, in, len, it,
244
144M
            tag, aclass, opt, ctx);
245
246
22.1M
    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.1M
        if (tag != -1) {
252
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
253
0
            goto err;
254
0
        }
255
256
22.1M
        p = *in;
257
        /* Just read in tag and class */
258
22.1M
        ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
259
22.1M
            &p, len, -1, 0, 1, ctx);
260
22.1M
        if (!ret) {
261
22.4k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
262
22.4k
            goto err;
263
22.4k
        }
264
265
        /* Must be UNIVERSAL class */
266
22.0M
        if (oclass != V_ASN1_UNIVERSAL) {
267
            /* If OPTIONAL, assume this is OK */
268
151k
            if (opt)
269
117k
                return -1;
270
151k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
271
34.2k
            goto err;
272
151k
        }
273
274
        /* Check tag matches bit map */
275
21.9M
        if (!(ASN1_tag2bit(otag) & it->utype)) {
276
            /* If OPTIONAL, assume this is OK */
277
425k
            if (opt)
278
16.1k
                return -1;
279
425k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_WRONG_TAG);
280
409k
            goto err;
281
425k
        }
282
21.5M
        return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
283
284
5.45M
    case ASN1_ITYPE_EXTERN:
285
        /* Use new style d2i */
286
5.45M
        ef = it->funcs;
287
5.45M
        if (ef->asn1_ex_d2i_ex != NULL)
288
2.71M
            return ef->asn1_ex_d2i_ex(pval, in, len, it, tag, aclass, opt, ctx,
289
2.71M
                libctx, propq);
290
2.73M
        return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
291
292
7.07M
    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
7.07M
        if (tag != -1) {
298
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
299
0
            goto err;
300
0
        }
301
302
7.07M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
303
0
            goto auxerr;
304
7.07M
        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
6.27M
        } 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
7.07M
        p = *in;
319
25.4M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
320
24.7M
            pchptr = ossl_asn1_get_field_ptr(pval, tt);
321
            /*
322
             * We mark field as OPTIONAL so its absence can be recognised.
323
             */
324
24.7M
            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth,
325
24.7M
                libctx, propq);
326
            /* If field not present, try the next one */
327
24.7M
            if (ret == -1)
328
18.4M
                continue;
329
            /* If positive return, read OK, break loop */
330
6.36M
            if (ret > 0)
331
5.34M
                break;
332
            /*
333
             * Must be an ASN1 parsing error.
334
             * Free up any partial choice value
335
             */
336
1.02M
            ossl_asn1_template_free(pchptr, tt);
337
1.02M
            errtt = tt;
338
1.02M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
339
1.02M
            goto err;
340
6.36M
        }
341
342
        /* Did we fall off the end without reading anything? */
343
6.04M
        if (i == it->tcount) {
344
            /* If OPTIONAL, this is OK */
345
706k
            if (opt) {
346
                /* Free and zero it */
347
32.3k
                ASN1_item_ex_free(pval, it);
348
32.3k
                return -1;
349
32.3k
            }
350
706k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
351
674k
            goto err;
352
706k
        }
353
354
5.34M
        ossl_asn1_set_choice_selector(pval, i, it);
355
356
5.34M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
357
0
            goto auxerr;
358
5.34M
        *in = p;
359
5.34M
        return 1;
360
361
870k
    case ASN1_ITYPE_NDEF_SEQUENCE:
362
62.8M
    case ASN1_ITYPE_SEQUENCE:
363
62.8M
        p = *in;
364
62.8M
        tmplen = len;
365
366
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
367
62.8M
        if (tag == -1) {
368
56.7M
            tag = V_ASN1_SEQUENCE;
369
56.7M
            aclass = V_ASN1_UNIVERSAL;
370
56.7M
        }
371
        /* Get SEQUENCE length and update len, p */
372
62.8M
        ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
373
62.8M
            &p, len, tag, aclass, opt, ctx);
374
62.8M
        if (!ret) {
375
4.49M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
376
4.49M
            goto err;
377
58.3M
        } else if (ret == -1)
378
6.77M
            return -1;
379
51.5M
        if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
380
0
            len = tmplen - (long)(p - *in);
381
0
            seq_nolen = 1;
382
0
        }
383
        /* If indefinite we don't do a length check */
384
51.5M
        else
385
51.5M
            seq_nolen = seq_eoc;
386
51.5M
        if (!cst) {
387
120k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
388
120k
            goto err;
389
120k
        }
390
391
51.4M
        if (*pval == NULL
392
39.0M
            && !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.4M
        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
143M
            if (tt->flags & ASN1_TFLG_ADB_MASK) {
403
789k
                const ASN1_TEMPLATE *seqtt;
404
789k
                ASN1_VALUE **pseqval;
405
789k
                seqtt = ossl_asn1_do_adb(*pval, tt, 0);
406
789k
                if (seqtt == NULL)
407
638k
                    continue;
408
150k
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
409
150k
                ossl_asn1_template_free(pseqval, seqtt);
410
150k
            }
411
143M
        }
412
413
        /* Get each field entry */
414
149M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
415
114M
            const ASN1_TEMPLATE *seqtt;
416
114M
            ASN1_VALUE **pseqval;
417
114M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
418
114M
            if (seqtt == NULL)
419
0
                goto err;
420
114M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
421
            /* Have we ran out of data? */
422
114M
            if (!len)
423
2.76M
                break;
424
112M
            q = p;
425
112M
            if (asn1_check_eoc(&p, len)) {
426
3.64M
                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.61M
                len -= (long)(p - q);
431
3.61M
                seq_eoc = 0;
432
3.61M
                break;
433
3.64M
            }
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.8M
                isopt = 0;
442
71.5M
            else
443
71.5M
                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.91M
                errtt = seqtt;
452
9.91M
                goto err;
453
98.4M
            } else if (ret == -1) {
454
                /*
455
                 * OPTIONAL component absent. Free and zero the field.
456
                 */
457
10.1M
                ossl_asn1_template_free(pseqval, seqtt);
458
10.1M
                continue;
459
10.1M
            }
460
            /* Update length */
461
88.3M
            len -= (long)(p - q);
462
88.3M
        }
463
464
        /* Check for EOC if expecting one */
465
41.5M
        if (seq_eoc && !asn1_check_eoc(&p, len)) {
466
185k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
467
185k
            goto err;
468
185k
        }
469
        /* Check all data read */
470
41.3M
        if (!seq_nolen && len) {
471
31.4k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
472
31.4k
            goto err;
473
31.4k
        }
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
54.7M
        for (; i < it->tcount; tt++, i++) {
481
13.7M
            const ASN1_TEMPLATE *seqtt;
482
13.7M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
483
13.7M
            if (seqtt == NULL)
484
0
                goto err;
485
13.7M
            if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
486
13.5M
                ASN1_VALUE **pseqval;
487
13.5M
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
488
13.5M
                ossl_asn1_template_free(pseqval, seqtt);
489
13.5M
            } else {
490
284k
                errtt = seqtt;
491
284k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_FIELD_MISSING);
492
284k
                goto err;
493
284k
            }
494
13.7M
        }
495
        /* Save encoding */
496
40.9M
        if (!ossl_asn1_enc_save(pval, *in, (long)(p - *in), it))
497
0
            goto auxerr;
498
40.9M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
499
22.7k
            goto auxerr;
500
40.9M
        *in = p;
501
40.9M
        return 1;
502
503
0
    default:
504
0
        return 0;
505
389M
    }
506
22.7k
auxerr:
507
22.7k
    ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
508
17.2M
err:
509
17.2M
    if (errtt)
510
11.2M
        ERR_add_error_data(4, "Field=", errtt->field_name,
511
11.2M
            ", Type=", it->sname);
512
6.02M
    else
513
6.02M
        ERR_add_error_data(2, "Type=", it->sname);
514
17.2M
    return 0;
515
22.7k
}
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
280M
{
528
280M
    int flags, aclass;
529
280M
    int ret;
530
280M
    long len;
531
280M
    const unsigned char *p, *q;
532
280M
    char exp_eoc;
533
280M
    if (!val)
534
0
        return 0;
535
280M
    flags = tt->flags;
536
280M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
537
538
280M
    p = *in;
539
540
    /* Check if EXPLICIT tag expected */
541
280M
    if (flags & ASN1_TFLG_EXPTAG) {
542
7.63M
        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.63M
        ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
548
7.63M
            &p, inlen, tt->tag, aclass, opt, ctx);
549
7.63M
        q = p;
550
7.63M
        if (!ret) {
551
376k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
552
376k
            return 0;
553
7.26M
        } else if (ret == -1)
554
5.45M
            return -1;
555
1.80M
        if (!cst) {
556
6.56k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
557
6.56k
            return 0;
558
6.56k
        }
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
319k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
564
319k
            return 0;
565
319k
        }
566
        /* We read the field in OK so update length */
567
1.47M
        len -= (long)(p - q);
568
1.47M
        if (exp_eoc) {
569
            /* If NDEF we must have an EOC here */
570
452k
            if (!asn1_check_eoc(&p, len)) {
571
45.3k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
572
45.3k
                goto err;
573
45.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.25k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
580
4.25k
                goto err;
581
4.25k
            }
582
1.02M
        }
583
1.47M
    } else
584
272M
        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth,
585
272M
            libctx, propq);
586
587
1.42M
    *in = p;
588
1.42M
    return 1;
589
590
49.6k
err:
591
49.6k
    return 0;
592
280M
}
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
274M
{
600
274M
    int flags, aclass;
601
274M
    int ret;
602
274M
    ASN1_VALUE *tval;
603
274M
    const unsigned char *p, *q;
604
274M
    if (!val)
605
0
        return 0;
606
274M
    flags = tt->flags;
607
274M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
608
609
274M
    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
274M
    if (tt->flags & ASN1_TFLG_EMBED) {
616
12.8M
        tval = (ASN1_VALUE *)val;
617
12.8M
        val = &tval;
618
12.8M
    }
619
620
274M
    if (flags & ASN1_TFLG_SK_MASK) {
621
        /* SET OF, SEQUENCE OF */
622
151M
        int sktag, skaclass;
623
151M
        char sk_eoc;
624
        /* First work out expected inner tag value */
625
151M
        if (flags & ASN1_TFLG_IMPTAG) {
626
1.37M
            sktag = tt->tag;
627
1.37M
            skaclass = aclass;
628
149M
        } else {
629
149M
            skaclass = V_ASN1_UNIVERSAL;
630
149M
            if (flags & ASN1_TFLG_SET_OF)
631
142M
                sktag = V_ASN1_SET;
632
7.11M
            else
633
7.11M
                sktag = V_ASN1_SEQUENCE;
634
149M
        }
635
        /* Get the tag */
636
151M
        ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
637
151M
            &p, len, sktag, skaclass, opt, ctx);
638
151M
        if (!ret) {
639
1.35M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
640
1.35M
            return 0;
641
149M
        } else if (ret == -1)
642
802k
            return -1;
643
149M
        if (*val == NULL)
644
148M
            *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
645
680k
        else {
646
            /*
647
             * We've got a valid STACK: free up any items present
648
             */
649
680k
            STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
650
680k
            ASN1_VALUE *vtmp;
651
680k
            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
680k
        }
656
657
149M
        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
383M
        while (len > 0) {
664
243M
            ASN1_VALUE *skfield;
665
243M
            q = p;
666
            /* See if EOC found */
667
243M
            if (asn1_check_eoc(&p, len)) {
668
6.53M
                if (!sk_eoc) {
669
13.0k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
670
13.0k
                    goto err;
671
13.0k
                }
672
6.51M
                len -= (long)(p - q);
673
6.51M
                sk_eoc = 0;
674
6.51M
                break;
675
6.53M
            }
676
236M
            skfield = NULL;
677
236M
            if (asn1_item_embed_d2i(&skfield, &p, len,
678
236M
                    ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
679
236M
                    depth, libctx, propq)
680
236M
                <= 0) {
681
1.99M
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
682
                /* |skfield| may be partially allocated despite failure. */
683
1.99M
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
684
1.99M
                goto err;
685
1.99M
            }
686
234M
            len -= (long)(p - q);
687
234M
            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
234M
        }
693
147M
        if (sk_eoc) {
694
14.8k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
695
14.8k
            goto err;
696
14.8k
        }
697
147M
    } else if (flags & ASN1_TFLG_IMPTAG) {
698
        /* IMPLICIT tagging */
699
20.7M
        ret = asn1_item_embed_d2i(val, &p, len,
700
20.7M
            ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
701
20.7M
            ctx, depth, libctx, propq);
702
20.7M
        if (!ret) {
703
543k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
704
543k
            goto err;
705
20.2M
        } else if (ret == -1)
706
15.7M
            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.74M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
713
8.74M
            goto err;
714
94.0M
        } else if (ret == -1)
715
6.56M
            return -1;
716
102M
    }
717
718
238M
    *in = p;
719
238M
    return 1;
720
721
11.3M
err:
722
11.3M
    return 0;
723
274M
}
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
166M
{
730
166M
    int ret = 0, utype;
731
166M
    long plen;
732
166M
    char cst, inf, free_cont = 0;
733
166M
    const unsigned char *p;
734
166M
    BUF_MEM buf = { 0, NULL, 0, 0 };
735
166M
    const unsigned char *cont = NULL;
736
166M
    long len;
737
738
166M
    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
166M
    if (it->itype == ASN1_ITYPE_MSTRING) {
744
21.5M
        utype = tag;
745
21.5M
        tag = -1;
746
21.5M
    } else
747
144M
        utype = it->utype;
748
749
166M
    if (utype == V_ASN1_ANY) {
750
        /* If type is ANY need to figure out type from tag */
751
64.1M
        unsigned char oclass;
752
64.1M
        if (tag >= 0) {
753
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
754
0
            return 0;
755
0
        }
756
64.1M
        if (opt) {
757
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
758
0
            return 0;
759
0
        }
760
64.1M
        p = *in;
761
64.1M
        ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
762
64.1M
            &p, inlen, -1, 0, 0, ctx);
763
64.1M
        if (!ret) {
764
36.5k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
765
36.5k
            return 0;
766
36.5k
        }
767
64.1M
        if (oclass != V_ASN1_UNIVERSAL)
768
4.08M
            utype = V_ASN1_OTHER;
769
64.1M
    }
770
166M
    if (tag == -1) {
771
151M
        tag = utype;
772
151M
        aclass = V_ASN1_UNIVERSAL;
773
151M
    }
774
166M
    p = *in;
775
    /* Check header */
776
166M
    ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
777
166M
        &p, inlen, tag, aclass, opt, ctx);
778
166M
    if (!ret) {
779
7.01M
        ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
780
7.01M
        return 0;
781
159M
    } else if (ret == -1)
782
15.3M
        return -1;
783
144M
    ret = 0;
784
    /* SEQUENCE, SET and "OTHER" are left in encoded form */
785
144M
    if ((utype == V_ASN1_SEQUENCE)
786
139M
        || (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
25.0M
        if (utype == V_ASN1_OTHER) {
792
4.08M
            asn1_tlc_clear(ctx);
793
4.08M
        }
794
        /* SEQUENCE and SET must be constructed */
795
20.9M
        else if (!cst) {
796
16.8k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
797
16.8k
            return 0;
798
16.8k
        }
799
800
25.0M
        cont = *in;
801
        /* If indefinite length constructed find the real end */
802
25.0M
        if (inf) {
803
1.28M
            if (!asn1_find_end(&p, plen, inf))
804
114k
                goto err;
805
1.16M
            len = (long)(p - cont);
806
23.7M
        } else {
807
23.7M
            len = (long)(p - cont) + plen;
808
23.7M
            p += plen;
809
23.7M
        }
810
118M
    } else if (cst) {
811
8.86M
        if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
812
8.85M
            || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
813
8.83M
            || utype == V_ASN1_ENUMERATED) {
814
28.8k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
815
28.8k
            return 0;
816
28.8k
        }
817
818
        /* Free any returned 'buf' content */
819
8.83M
        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.83M
        if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
827
71.3k
            goto err;
828
71.3k
        }
829
8.76M
        len = (long)buf.length;
830
        /* Append a final null to string */
831
8.76M
        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.76M
        buf.data[len] = 0;
836
8.76M
        cont = (const unsigned char *)buf.data;
837
110M
    } else {
838
110M
        cont = p;
839
110M
        len = plen;
840
110M
        p += plen;
841
110M
    }
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
143M
    if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
846
850k
        goto err;
847
848
142M
    *in = p;
849
142M
    ret = 1;
850
144M
err:
851
144M
    if (free_cont)
852
470k
        OPENSSL_free(buf.data);
853
144M
    return ret;
854
142M
}
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
131M
{
861
131M
    ASN1_VALUE **opval = NULL;
862
131M
    ASN1_STRING *stmp;
863
131M
    ASN1_TYPE *typ = NULL;
864
131M
    int ret = 0;
865
131M
    const ASN1_PRIMITIVE_FUNCS *pf;
866
131M
    ASN1_INTEGER **tint;
867
131M
    pf = it->funcs;
868
869
131M
    if (pf && pf->prim_c2i)
870
3.25M
        return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
871
    /* If ANY type clear type and set pointer to internal value */
872
128M
    if (it->utype == V_ASN1_ANY) {
873
62.4M
        if (*pval == NULL) {
874
62.3M
            typ = ASN1_TYPE_new();
875
62.3M
            if (typ == NULL)
876
0
                goto err;
877
62.3M
            *pval = (ASN1_VALUE *)typ;
878
62.3M
        } else
879
120k
            typ = (ASN1_TYPE *)*pval;
880
881
62.4M
        if (utype != typ->type)
882
62.4M
            ASN1_TYPE_set(typ, utype, NULL);
883
62.4M
        opval = pval;
884
62.4M
        pval = &typ->value.asn1_value;
885
62.4M
    }
886
128M
    switch (utype) {
887
28.8M
    case V_ASN1_OBJECT:
888
28.8M
        if (!ossl_c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
889
52.0k
            goto err;
890
28.7M
        break;
891
892
28.7M
    case V_ASN1_NULL:
893
936k
        if (len) {
894
4.73k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
895
4.73k
            goto err;
896
4.73k
        }
897
932k
        *pval = (ASN1_VALUE *)1;
898
932k
        break;
899
900
8.10M
    case V_ASN1_BOOLEAN:
901
8.10M
        if (len != 1) {
902
8.44k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
903
8.44k
            goto err;
904
8.09M
        } else {
905
8.09M
            ASN1_BOOLEAN *tbool;
906
8.09M
            tbool = (ASN1_BOOLEAN *)pval;
907
8.09M
            *tbool = *cont;
908
8.09M
        }
909
8.09M
        break;
910
911
8.09M
    case V_ASN1_BIT_STRING:
912
6.44M
        if (!ossl_c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
913
49.5k
            goto err;
914
6.39M
        break;
915
916
10.3M
    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
526k
            goto err;
921
        /* Fixup type to match the expected form */
922
12.0M
        (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
923
12.0M
        break;
924
925
26.5M
    case V_ASN1_OCTET_STRING:
926
28.7M
    case V_ASN1_NUMERICSTRING:
927
32.5M
    case V_ASN1_PRINTABLESTRING:
928
33.1M
    case V_ASN1_T61STRING:
929
33.2M
    case V_ASN1_VIDEOTEXSTRING:
930
35.8M
    case V_ASN1_IA5STRING:
931
37.9M
    case V_ASN1_UTCTIME:
932
38.1M
    case V_ASN1_GENERALIZEDTIME:
933
38.6M
    case V_ASN1_GRAPHICSTRING:
934
38.6M
    case V_ASN1_VISIBLESTRING:
935
38.6M
    case V_ASN1_GENERALSTRING:
936
39.2M
    case V_ASN1_UNIVERSALSTRING:
937
40.0M
    case V_ASN1_BMPSTRING:
938
42.0M
    case V_ASN1_UTF8STRING:
939
46.0M
    case V_ASN1_OTHER:
940
62.3M
    case V_ASN1_SET:
941
65.7M
    case V_ASN1_SEQUENCE:
942
71.2M
    default:
943
71.2M
        if (utype == V_ASN1_BMPSTRING && (len & 1)) {
944
2.38k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
945
2.38k
            goto err;
946
2.38k
        }
947
71.2M
        if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
948
2.33k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
949
2.33k
            goto err;
950
2.33k
        }
951
71.1M
        if (utype == V_ASN1_GENERALIZEDTIME && (len < 15)) {
952
7.03k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_GENERALIZEDTIME_IS_TOO_SHORT);
953
7.03k
            goto err;
954
7.03k
        }
955
71.1M
        if (utype == V_ASN1_UTCTIME && (len < 13)) {
956
3.14k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UTCTIME_IS_TOO_SHORT);
957
3.14k
            goto err;
958
3.14k
        }
959
        /* All based on ASN1_STRING and handled the same */
960
71.1M
        if (*pval == NULL) {
961
50.6M
            stmp = ASN1_STRING_type_new(utype);
962
50.6M
            if (stmp == NULL) {
963
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
964
0
                goto err;
965
0
            }
966
50.6M
            *pval = (ASN1_VALUE *)stmp;
967
50.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
71.1M
        if (*free_cont) {
973
7.20M
            ASN1_STRING_set0(stmp, (unsigned char *)cont /* UGLY CAST! */, len);
974
7.20M
            *free_cont = 0;
975
63.9M
        } else {
976
63.9M
            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
63.9M
        }
983
71.1M
        break;
984
128M
    }
985
    /* If ASN1_ANY and NULL type fix up value */
986
127M
    if (typ && (utype == V_ASN1_NULL))
987
914k
        typ->value.ptr = NULL;
988
989
127M
    ret = 1;
990
128M
err:
991
128M
    if (!ret) {
992
655k
        ASN1_TYPE_free(typ);
993
655k
        if (opval)
994
37.3k
            *opval = NULL;
995
655k
    }
996
128M
    return ret;
997
127M
}
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.28M
{
1008
1.28M
    uint32_t expected_eoc;
1009
1.28M
    long plen;
1010
1.28M
    const unsigned char *p = *in, *q;
1011
    /* If not indefinite length constructed just add length */
1012
1.28M
    if (inf == 0) {
1013
0
        *in += len;
1014
0
        return 1;
1015
0
    }
1016
1.28M
    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
438M
    while (len > 0) {
1024
438M
        if (asn1_check_eoc(&p, len)) {
1025
41.9M
            expected_eoc--;
1026
41.9M
            if (expected_eoc == 0)
1027
1.16M
                break;
1028
40.7M
            len -= 2;
1029
40.7M
            continue;
1030
41.9M
        }
1031
396M
        q = p;
1032
        /* Just read in a header: only care about the length */
1033
396M
        if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1034
396M
                -1, 0, 0, NULL)) {
1035
72.0k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1036
72.0k
            return 0;
1037
72.0k
        }
1038
396M
        if (inf) {
1039
100M
            if (expected_eoc == UINT32_MAX) {
1040
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1041
0
                return 0;
1042
0
            }
1043
100M
            expected_eoc++;
1044
296M
        } else {
1045
296M
            p += plen;
1046
296M
        }
1047
396M
        len -= (long)(p - q);
1048
396M
    }
1049
1.21M
    if (expected_eoc) {
1050
42.1k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1051
42.1k
        return 0;
1052
42.1k
    }
1053
1.16M
    *in = p;
1054
1.16M
    return 1;
1055
1.21M
}
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.74M
#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
62.8M
    while (len > 0) {
1090
53.5M
        q = p;
1091
        /* Check for EOC */
1092
53.5M
        if (asn1_check_eoc(&p, len)) {
1093
            /*
1094
             * EOC is illegal outside indefinite length constructed form
1095
             */
1096
1.09M
            if (!inf) {
1097
11.8k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
1098
11.8k
                return 0;
1099
11.8k
            }
1100
1.08M
            inf = 0;
1101
1.08M
            break;
1102
1.09M
        }
1103
1104
52.4M
        if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1105
52.4M
                len, tag, aclass, 0, NULL)) {
1106
41.3k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1107
41.3k
            return 0;
1108
41.3k
        }
1109
1110
        /* If indefinite length constructed update max length */
1111
52.3M
        if (cst) {
1112
1.74M
            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.74M
            if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1117
54.2k
                return 0;
1118
50.6M
        } else if (plen && !collect_data(buf, &p, plen))
1119
0
            return 0;
1120
52.3M
        len -= (long)(p - q);
1121
52.3M
    }
1122
10.4M
    if (inf) {
1123
15.7k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1124
15.7k
        return 0;
1125
15.7k
    }
1126
10.4M
    *in = p;
1127
10.4M
    return 1;
1128
10.4M
}
1129
1130
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1131
20.6M
{
1132
20.6M
    long len;
1133
20.6M
    if (buf) {
1134
20.6M
        len = (long)buf->length;
1135
20.6M
        if (len + plen < 0) {
1136
            /* resulting buffer length will not fit into long */
1137
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_LENGTH_TOO_LONG);
1138
0
            return 0;
1139
0
        }
1140
20.6M
        if (!BUF_MEM_grow_clean(buf, len + plen)) {
1141
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
1142
0
            return 0;
1143
0
        }
1144
20.6M
        memcpy(buf->data + len, *p, plen);
1145
20.6M
    }
1146
20.6M
    *p += plen;
1147
20.6M
    return 1;
1148
20.6M
}
1149
1150
/* Check for ASN1 EOC and swallow it if found */
1151
1152
static int asn1_check_eoc(const unsigned char **in, long len)
1153
859M
{
1154
859M
    const unsigned char *p;
1155
1156
859M
    if (len < 2)
1157
197k
        return 0;
1158
859M
    p = *in;
1159
859M
    if (p[0] == '\0' && p[1] == '\0') {
1160
65.0M
        *in += 2;
1161
65.0M
        return 1;
1162
65.0M
    }
1163
794M
    return 0;
1164
859M
}
1165
1166
/*
1167
 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1168
 * length for indefinite length constructed form, we don't know the exact
1169
 * length but we can set an upper bound to the amount of data available minus
1170
 * the header length just read.
1171
 */
1172
1173
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1174
    char *inf, char *cst,
1175
    const unsigned char **in, long len,
1176
    int exptag, int expclass, char opt, ASN1_TLC *ctx)
1177
923M
{
1178
923M
    int i;
1179
923M
    int ptag, pclass;
1180
923M
    long plen;
1181
923M
    const unsigned char *p, *q;
1182
923M
    p = *in;
1183
923M
    q = p;
1184
1185
923M
    if (len <= 0) {
1186
504
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
1187
504
        goto err;
1188
504
    }
1189
923M
    if (ctx != NULL && ctx->valid) {
1190
113M
        i = ctx->ret;
1191
113M
        plen = ctx->plen;
1192
113M
        pclass = ctx->pclass;
1193
113M
        ptag = ctx->ptag;
1194
113M
        p += ctx->hdrlen;
1195
809M
    } else {
1196
809M
        i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1197
809M
        if (ctx != NULL) {
1198
360M
            ctx->ret = i;
1199
360M
            ctx->plen = plen;
1200
360M
            ctx->pclass = pclass;
1201
360M
            ctx->ptag = ptag;
1202
360M
            ctx->hdrlen = (int)(p - q);
1203
360M
            ctx->valid = 1;
1204
            /*
1205
             * If definite length, and no error, length + header can't exceed
1206
             * total amount of data available.
1207
             */
1208
360M
            if ((i & 0x81) == 0 && (plen + ctx->hdrlen) > len) {
1209
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
1210
0
                goto err;
1211
0
            }
1212
360M
        }
1213
809M
    }
1214
1215
923M
    if ((i & 0x80) != 0) {
1216
1.16M
        ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER);
1217
1.16M
        goto err;
1218
1.16M
    }
1219
922M
    if (exptag >= 0) {
1220
383M
        if (exptag != ptag || expclass != pclass) {
1221
            /*
1222
             * If type is OPTIONAL, not an error: indicate missing type.
1223
             */
1224
40.6M
            if (opt != 0)
1225
28.4M
                return -1;
1226
40.6M
            ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG);
1227
12.2M
            goto err;
1228
40.6M
        }
1229
        /*
1230
         * We have a tag and class match: assume we are going to do something
1231
         * with it
1232
         */
1233
342M
        asn1_tlc_clear(ctx);
1234
342M
    }
1235
1236
881M
    if ((i & 1) != 0)
1237
135M
        plen = len - (long)(p - q);
1238
1239
881M
    if (inf != NULL)
1240
795M
        *inf = i & 1;
1241
1242
881M
    if (cst != NULL)
1243
249M
        *cst = i & V_ASN1_CONSTRUCTED;
1244
1245
881M
    if (olen != NULL)
1246
795M
        *olen = plen;
1247
1248
881M
    if (oclass != NULL)
1249
86.2M
        *oclass = pclass;
1250
1251
881M
    if (otag != NULL)
1252
86.2M
        *otag = ptag;
1253
1254
881M
    *in = p;
1255
881M
    return 1;
1256
1257
13.4M
err:
1258
    asn1_tlc_clear(ctx);
1259
13.4M
    return 0;
1260
922M
}