Coverage Report

Created: 2026-07-12 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/asn1/tasn_dec.c
Line
Count
Source
1
/*
2
 * Copyright 2000-2026 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
383M
#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, long 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.7M
{
107
43.7M
    if ((tag < 0) || (tag > 30))
108
5.83k
        return 0;
109
43.7M
    return tag2bit[tag];
110
43.7M
}
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
21.2M
    do {                     \
122
21.2M
        (c)->valid = 0;      \
123
21.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
23.8M
{
137
23.8M
    int rv;
138
139
23.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
23.8M
    rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0,
144
23.8M
        libctx, propq);
145
23.8M
    if (rv <= 0)
146
14.2M
        ASN1_item_ex_free(pval, it);
147
23.8M
    return rv;
148
23.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.64M
{
154
2.64M
    return asn1_item_ex_d2i_intern(pval, in, len, it, tag, aclass, opt, ctx,
155
2.64M
        NULL, NULL);
156
2.64M
}
157
158
ASN1_VALUE *ASN1_item_d2i_ex(ASN1_VALUE **pval,
159
    const unsigned char **in, long len,
160
    const ASN1_ITEM *it, OSSL_LIB_CTX *libctx,
161
    const char *propq)
162
21.2M
{
163
21.2M
    ASN1_TLC c;
164
21.2M
    ASN1_VALUE *ptmpval = NULL;
165
166
21.2M
    if (pval == NULL)
167
18.4M
        pval = &ptmpval;
168
21.2M
    asn1_tlc_clear_nc(&c);
169
21.2M
    if (asn1_item_ex_d2i_intern(pval, in, len, it, -1, 0, 0, &c, libctx,
170
21.2M
            propq)
171
21.2M
        > 0)
172
7.26M
        return *pval;
173
13.9M
    return NULL;
174
21.2M
}
175
176
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
177
    const unsigned char **in, long len,
178
    const ASN1_ITEM *it)
179
19.7M
{
180
19.7M
    return ASN1_item_d2i_ex(pval, in, len, it, NULL, NULL);
181
19.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
383M
{
194
383M
    const ASN1_TEMPLATE *tt, *errtt = NULL;
195
383M
    const ASN1_EXTERN_FUNCS *ef;
196
383M
    const ASN1_AUX *aux;
197
383M
    ASN1_aux_cb *asn1_cb;
198
383M
    const unsigned char *p = NULL, *q;
199
383M
    unsigned char oclass;
200
383M
    char seq_eoc, seq_nolen, cst, isopt;
201
383M
    long tmplen;
202
383M
    int i;
203
383M
    int otag;
204
383M
    int ret = 0;
205
383M
    ASN1_VALUE **pchptr;
206
207
383M
    if (pval == NULL || it == NULL) {
208
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
209
0
        return 0;
210
0
    }
211
383M
    if (len <= 0) {
212
83.2k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
213
83.2k
        return 0;
214
83.2k
    }
215
383M
    aux = it->funcs;
216
383M
    if (aux && aux->asn1_cb)
217
12.1M
        asn1_cb = aux->asn1_cb;
218
371M
    else
219
371M
        asn1_cb = 0;
220
221
383M
    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
383M
    switch (it->itype) {
227
293M
    case ASN1_ITYPE_PRIMITIVE:
228
293M
        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
168M
            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
168M
            return asn1_template_ex_d2i(pval, in, len, it->templates, opt, ctx,
241
168M
                depth, libctx, propq);
242
168M
        }
243
124M
        return asn1_d2i_ex_primitive(pval, in, len, it,
244
124M
            tag, aclass, opt, ctx);
245
246
21.8M
    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
21.8M
        if (tag != -1) {
252
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
253
0
            goto err;
254
0
        }
255
256
21.8M
        p = *in;
257
        /* Just read in tag and class */
258
21.8M
        ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
259
21.8M
            &p, len, -1, 0, 1, ctx);
260
21.8M
        if (!ret) {
261
20.2k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
262
20.2k
            goto err;
263
20.2k
        }
264
265
        /* Must be UNIVERSAL class */
266
21.7M
        if (oclass != V_ASN1_UNIVERSAL) {
267
            /* If OPTIONAL, assume this is OK */
268
153k
            if (opt)
269
124k
                return -1;
270
153k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
271
29.3k
            goto err;
272
153k
        }
273
274
        /* Check tag matches bit map */
275
21.6M
        if (!(ASN1_tag2bit(otag) & it->utype)) {
276
            /* If OPTIONAL, assume this is OK */
277
361k
            if (opt)
278
17.7k
                return -1;
279
361k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_WRONG_TAG);
280
344k
            goto err;
281
361k
        }
282
21.2M
        return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
283
284
5.34M
    case ASN1_ITYPE_EXTERN:
285
        /* Use new style d2i */
286
5.34M
        ef = it->funcs;
287
5.34M
        if (ef->asn1_ex_d2i_ex != NULL)
288
2.69M
            return ef->asn1_ex_d2i_ex(pval, in, len, it, tag, aclass, opt, ctx,
289
2.69M
                libctx, propq);
290
2.64M
        return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
291
292
5.09M
    case ASN1_ITYPE_CHOICE:
293
        /*
294
         * It never makes sense for CHOICE types to have implicit tagging, so
295
         * if tag != -1, then this looks like an error in the template.
296
         */
297
5.09M
        if (tag != -1) {
298
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
299
0
            goto err;
300
0
        }
301
302
5.09M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
303
0
            goto auxerr;
304
5.09M
        if (*pval) {
305
            /* Free up and zero CHOICE value if initialised */
306
817k
            i = ossl_asn1_get_choice_selector(pval, it);
307
817k
            if ((i >= 0) && (i < it->tcount)) {
308
0
                tt = it->templates + i;
309
0
                pchptr = ossl_asn1_get_field_ptr(pval, tt);
310
0
                ossl_asn1_template_free(pchptr, tt);
311
0
                ossl_asn1_set_choice_selector(pval, -1, it);
312
0
            }
313
4.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
5.09M
        p = *in;
319
19.8M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
320
19.2M
            pchptr = ossl_asn1_get_field_ptr(pval, tt);
321
            /*
322
             * We mark field as OPTIONAL so its absence can be recognised.
323
             */
324
19.2M
            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth,
325
19.2M
                libctx, propq);
326
            /* If field not present, try the next one */
327
19.2M
            if (ret == -1)
328
14.7M
                continue;
329
            /* If positive return, read OK, break loop */
330
4.49M
            if (ret > 0)
331
3.56M
                break;
332
            /*
333
             * Must be an ASN1 parsing error.
334
             * Free up any partial choice value
335
             */
336
928k
            ossl_asn1_template_free(pchptr, tt);
337
928k
            errtt = tt;
338
928k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
339
928k
            goto err;
340
4.49M
        }
341
342
        /* Did we fall off the end without reading anything? */
343
4.16M
        if (i == it->tcount) {
344
            /* If OPTIONAL, this is OK */
345
598k
            if (opt) {
346
                /* Free and zero it */
347
32.6k
                ASN1_item_ex_free(pval, it);
348
32.6k
                return -1;
349
32.6k
            }
350
598k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
351
565k
            goto err;
352
598k
        }
353
354
3.56M
        ossl_asn1_set_choice_selector(pval, i, it);
355
356
3.56M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
357
0
            goto auxerr;
358
3.56M
        *in = p;
359
3.56M
        return 1;
360
361
742k
    case ASN1_ITYPE_NDEF_SEQUENCE:
362
57.9M
    case ASN1_ITYPE_SEQUENCE:
363
57.9M
        p = *in;
364
57.9M
        tmplen = len;
365
366
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
367
57.9M
        if (tag == -1) {
368
53.5M
            tag = V_ASN1_SEQUENCE;
369
53.5M
            aclass = V_ASN1_UNIVERSAL;
370
53.5M
        }
371
        /* Get SEQUENCE length and update len, p */
372
57.9M
        ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
373
57.9M
            &p, len, tag, aclass, opt, ctx);
374
57.9M
        if (!ret) {
375
3.81M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
376
3.81M
            goto err;
377
54.1M
        } else if (ret == -1)
378
4.74M
            return -1;
379
49.4M
        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
49.4M
        else
385
49.4M
            seq_nolen = seq_eoc;
386
49.4M
        if (!cst) {
387
100k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
388
100k
            goto err;
389
100k
        }
390
391
49.3M
        if (*pval == NULL
392
37.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
49.3M
        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
185M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
402
136M
            if (tt->flags & ASN1_TFLG_ADB_MASK) {
403
676k
                const ASN1_TEMPLATE *seqtt;
404
676k
                ASN1_VALUE **pseqval;
405
676k
                seqtt = ossl_asn1_do_adb(*pval, tt, 0);
406
676k
                if (seqtt == NULL)
407
548k
                    continue;
408
128k
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
409
128k
                ossl_asn1_template_free(pseqval, seqtt);
410
128k
            }
411
136M
        }
412
413
        /* Get each field entry */
414
145M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
415
111M
            const ASN1_TEMPLATE *seqtt;
416
111M
            ASN1_VALUE **pseqval;
417
111M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
418
111M
            if (seqtt == NULL)
419
0
                goto err;
420
111M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
421
            /* Have we ran out of data? */
422
111M
            if (!len)
423
2.58M
                break;
424
108M
            q = p;
425
108M
            if (asn1_check_eoc(&p, len)) {
426
3.58M
                if (!seq_eoc) {
427
28.3k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
428
28.3k
                    goto err;
429
28.3k
                }
430
3.55M
                len -= p - q;
431
3.55M
                seq_eoc = 0;
432
3.55M
                break;
433
3.58M
            }
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
105M
            if (i == (it->tcount - 1))
441
36.1M
                isopt = 0;
442
68.9M
            else
443
68.9M
                isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
444
            /*
445
             * attempt to read in field, allowing each to be OPTIONAL
446
             */
447
448
105M
            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
449
105M
                depth, libctx, propq);
450
105M
            if (!ret) {
451
8.51M
                errtt = seqtt;
452
8.51M
                goto err;
453
96.6M
            } else if (ret == -1) {
454
                /*
455
                 * OPTIONAL component absent. Free and zero the field.
456
                 */
457
9.76M
                ossl_asn1_template_free(pseqval, seqtt);
458
9.76M
                continue;
459
9.76M
            }
460
            /* Update length */
461
86.8M
            len -= p - q;
462
86.8M
        }
463
464
        /* Check for EOC if expecting one */
465
40.7M
        if (seq_eoc && !asn1_check_eoc(&p, len)) {
466
183k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
467
183k
            goto err;
468
183k
        }
469
        /* Check all data read */
470
40.5M
        if (!seq_nolen && len) {
471
28.6k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
472
28.6k
            goto err;
473
28.6k
        }
474
475
        /*
476
         * If we get here we've got no more data in the SEQUENCE, however we
477
         * may not have read all fields so check all remaining are OPTIONAL
478
         * and clear any that are.
479
         */
480
53.0M
        for (; i < it->tcount; tt++, i++) {
481
12.6M
            const ASN1_TEMPLATE *seqtt;
482
12.6M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
483
12.6M
            if (seqtt == NULL)
484
0
                goto err;
485
12.6M
            if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
486
12.4M
                ASN1_VALUE **pseqval;
487
12.4M
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
488
12.4M
                ossl_asn1_template_free(pseqval, seqtt);
489
12.4M
            } else {
490
255k
                errtt = seqtt;
491
255k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_FIELD_MISSING);
492
255k
                goto err;
493
255k
            }
494
12.6M
        }
495
        /* Save encoding */
496
40.3M
        if (!ossl_asn1_enc_save(pval, *in, p - *in, it))
497
0
            goto auxerr;
498
40.3M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
499
20.3k
            goto auxerr;
500
40.2M
        *in = p;
501
40.2M
        return 1;
502
503
0
    default:
504
0
        return 0;
505
383M
    }
506
20.3k
auxerr:
507
20.3k
    ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
508
14.8M
err:
509
14.8M
    if (errtt)
510
9.69M
        ERR_add_error_data(4, "Field=", errtt->field_name,
511
9.69M
            ", Type=", it->sname);
512
5.13M
    else
513
5.13M
        ERR_add_error_data(2, "Type=", it->sname);
514
14.8M
    return 0;
515
20.3k
}
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
293M
{
528
293M
    int flags, aclass;
529
293M
    int ret;
530
293M
    long len;
531
293M
    const unsigned char *p, *q;
532
293M
    char exp_eoc;
533
293M
    if (!val)
534
0
        return 0;
535
293M
    flags = tt->flags;
536
293M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
537
538
293M
    p = *in;
539
540
    /* Check if EXPLICIT tag expected */
541
293M
    if (flags & ASN1_TFLG_EXPTAG) {
542
7.41M
        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.41M
        ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
548
7.41M
            &p, inlen, tt->tag, aclass, opt, ctx);
549
7.41M
        q = p;
550
7.41M
        if (!ret) {
551
313k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
552
313k
            return 0;
553
7.09M
        } else if (ret == -1)
554
5.35M
            return -1;
555
1.74M
        if (!cst) {
556
6.06k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
557
6.06k
            return 0;
558
6.06k
        }
559
        /* We've found the field so it can't be OPTIONAL now */
560
1.73M
        ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth, libctx,
561
1.73M
            propq);
562
1.73M
        if (!ret) {
563
306k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
564
306k
            return 0;
565
306k
        }
566
        /* We read the field in OK so update length */
567
1.43M
        len -= p - q;
568
1.43M
        if (exp_eoc) {
569
            /* If NDEF we must have an EOC here */
570
463k
            if (!asn1_check_eoc(&p, len)) {
571
43.9k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
572
43.9k
                goto err;
573
43.9k
            }
574
969k
        } else {
575
            /*
576
             * Otherwise we must hit the EXPLICIT tag end or its an error
577
             */
578
969k
            if (len) {
579
4.81k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
580
4.81k
                goto err;
581
4.81k
            }
582
969k
        }
583
1.43M
    } else
584
285M
        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth,
585
285M
            libctx, propq);
586
587
1.38M
    *in = p;
588
1.38M
    return 1;
589
590
48.7k
err:
591
48.7k
    return 0;
592
293M
}
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
287M
{
600
287M
    int flags, aclass;
601
287M
    int ret;
602
287M
    ASN1_VALUE *tval;
603
287M
    const unsigned char *p, *q;
604
287M
    if (!val)
605
0
        return 0;
606
287M
    flags = tt->flags;
607
287M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
608
609
287M
    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
287M
    if (tt->flags & ASN1_TFLG_EMBED) {
616
12.3M
        tval = (ASN1_VALUE *)val;
617
12.3M
        val = &tval;
618
12.3M
    }
619
620
287M
    if (flags & ASN1_TFLG_SK_MASK) {
621
        /* SET OF, SEQUENCE OF */
622
172M
        int sktag, skaclass;
623
172M
        char sk_eoc;
624
        /* First work out expected inner tag value */
625
172M
        if (flags & ASN1_TFLG_IMPTAG) {
626
1.23M
            sktag = tt->tag;
627
1.23M
            skaclass = aclass;
628
170M
        } else {
629
170M
            skaclass = V_ASN1_UNIVERSAL;
630
170M
            if (flags & ASN1_TFLG_SET_OF)
631
164M
                sktag = V_ASN1_SET;
632
6.48M
            else
633
6.48M
                sktag = V_ASN1_SEQUENCE;
634
170M
        }
635
        /* Get the tag */
636
172M
        ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
637
172M
            &p, len, sktag, skaclass, opt, ctx);
638
172M
        if (!ret) {
639
1.13M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
640
1.13M
            return 0;
641
171M
        } else if (ret == -1)
642
736k
            return -1;
643
170M
        if (*val == NULL)
644
169M
            *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
645
701k
        else {
646
            /*
647
             * We've got a valid STACK: free up any items present
648
             */
649
701k
            STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
650
701k
            ASN1_VALUE *vtmp;
651
701k
            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
701k
        }
656
657
170M
        if (*val == NULL) {
658
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
659
0
            goto err;
660
0
        }
661
662
        /* Read as many items as we can */
663
410M
        while (len > 0) {
664
248M
            ASN1_VALUE *skfield;
665
248M
            q = p;
666
            /* See if EOC found */
667
248M
            if (asn1_check_eoc(&p, len)) {
668
6.17M
                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.15M
                len -= p - q;
673
6.15M
                sk_eoc = 0;
674
6.15M
                break;
675
6.17M
            }
676
241M
            skfield = NULL;
677
241M
            if (asn1_item_embed_d2i(&skfield, &p, len,
678
241M
                    ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
679
241M
                    depth, libctx, propq)
680
241M
                <= 0) {
681
1.72M
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
682
                /* |skfield| may be partially allocated despite failure. */
683
1.72M
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
684
1.72M
                goto err;
685
1.72M
            }
686
240M
            len -= p - q;
687
240M
            if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
688
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
689
0
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
690
0
                goto err;
691
0
            }
692
240M
        }
693
168M
        if (sk_eoc) {
694
15.0k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
695
15.0k
            goto err;
696
15.0k
        }
697
168M
    } else if (flags & ASN1_TFLG_IMPTAG) {
698
        /* IMPLICIT tagging */
699
15.7M
        ret = asn1_item_embed_d2i(val, &p, len,
700
15.7M
            ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
701
15.7M
            ctx, depth, libctx, propq);
702
15.7M
        if (!ret) {
703
475k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
704
475k
            goto err;
705
15.2M
        } else if (ret == -1)
706
12.5M
            return -1;
707
99.4M
    } else {
708
        /* Nothing special */
709
99.4M
        ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
710
99.4M
            -1, 0, opt, ctx, depth, libctx, propq);
711
99.4M
        if (!ret) {
712
7.53M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
713
7.53M
            goto err;
714
91.8M
        } else if (ret == -1)
715
5.91M
            return -1;
716
99.4M
    }
717
718
257M
    *in = p;
719
257M
    return 1;
720
721
9.76M
err:
722
9.76M
    return 0;
723
287M
}
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
145M
{
730
145M
    int ret = 0, utype;
731
145M
    long plen;
732
145M
    char cst, inf, free_cont = 0;
733
145M
    const unsigned char *p;
734
145M
    BUF_MEM buf = { 0, NULL, 0, 0 };
735
145M
    const unsigned char *cont = NULL;
736
145M
    long len;
737
738
145M
    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
145M
    if (it->itype == ASN1_ITYPE_MSTRING) {
744
21.2M
        utype = tag;
745
21.2M
        tag = -1;
746
21.2M
    } else
747
124M
        utype = it->utype;
748
749
145M
    if (utype == V_ASN1_ANY) {
750
        /* If type is ANY need to figure out type from tag */
751
50.7M
        unsigned char oclass;
752
50.7M
        if (tag >= 0) {
753
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
754
0
            return 0;
755
0
        }
756
50.7M
        if (opt) {
757
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
758
0
            return 0;
759
0
        }
760
50.7M
        p = *in;
761
50.7M
        ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
762
50.7M
            &p, inlen, -1, 0, 0, ctx);
763
50.7M
        if (!ret) {
764
35.4k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
765
35.4k
            return 0;
766
35.4k
        }
767
50.7M
        if (oclass != V_ASN1_UNIVERSAL)
768
4.18M
            utype = V_ASN1_OTHER;
769
50.7M
    }
770
145M
    if (tag == -1) {
771
134M
        tag = utype;
772
134M
        aclass = V_ASN1_UNIVERSAL;
773
134M
    }
774
145M
    p = *in;
775
    /* Check header */
776
145M
    ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
777
145M
        &p, inlen, tag, aclass, opt, ctx);
778
145M
    if (!ret) {
779
5.86M
        ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
780
5.86M
        return 0;
781
139M
    } else if (ret == -1)
782
13.5M
        return -1;
783
126M
    ret = 0;
784
    /* SEQUENCE, SET and "OTHER" are left in encoded form */
785
126M
    if ((utype == V_ASN1_SEQUENCE)
786
122M
        || (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
23.4M
        if (utype == V_ASN1_OTHER) {
792
4.18M
            asn1_tlc_clear(ctx);
793
4.18M
        }
794
        /* SEQUENCE and SET must be constructed */
795
19.2M
        else if (!cst) {
796
13.8k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
797
13.8k
            return 0;
798
13.8k
        }
799
800
23.4M
        cont = *in;
801
        /* If indefinite length constructed find the real end */
802
23.4M
        if (inf) {
803
1.33M
            if (!asn1_find_end(&p, plen, inf))
804
104k
                goto err;
805
1.23M
            len = p - cont;
806
22.1M
        } else {
807
22.1M
            len = p - cont + plen;
808
22.1M
            p += plen;
809
22.1M
        }
810
102M
    } else if (cst) {
811
8.76M
        if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
812
8.75M
            || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
813
8.74M
            || utype == V_ASN1_ENUMERATED) {
814
27.0k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
815
27.0k
            return 0;
816
27.0k
        }
817
818
        /* Free any returned 'buf' content */
819
8.74M
        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.74M
        if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
827
65.5k
            goto err;
828
65.5k
        }
829
8.67M
        len = buf.length;
830
        /* Append a final null to string */
831
8.67M
        if (!BUF_MEM_grow_clean(&buf, len + 1)) {
832
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
833
0
            goto err;
834
0
        }
835
8.67M
        buf.data[len] = 0;
836
8.67M
        cont = (const unsigned char *)buf.data;
837
94.1M
    } else {
838
94.1M
        cont = p;
839
94.1M
        len = plen;
840
94.1M
        p += plen;
841
94.1M
    }
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
126M
    if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
846
810k
        goto err;
847
848
125M
    *in = p;
849
125M
    ret = 1;
850
126M
err:
851
126M
    if (free_cont)
852
421k
        OPENSSL_free(buf.data);
853
126M
    return ret;
854
125M
}
855
856
/* Translate ASN1 content octets into a structure */
857
858
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, long len,
859
    int utype, char *free_cont, const ASN1_ITEM *it)
860
13.8M
{
861
13.8M
    ASN1_VALUE **opval = NULL;
862
13.8M
    ASN1_STRING *stmp;
863
13.8M
    ASN1_TYPE *typ = NULL;
864
13.8M
    int ret = 0;
865
13.8M
    int ilen = (int)len;
866
13.8M
    const ASN1_PRIMITIVE_FUNCS *pf;
867
13.8M
    ASN1_INTEGER **tint;
868
13.8M
    pf = it->funcs;
869
870
13.8M
    if (pf && pf->prim_c2i) {
871
539k
        if (len == (long)ilen)
872
539k
            return pf->prim_c2i(pval, cont, ilen, utype, free_cont, it);
873
539k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
874
0
        return 0;
875
539k
    }
876
    /* If ANY type clear type and set pointer to internal value */
877
13.2M
    if (it->utype == V_ASN1_ANY) {
878
1.66M
        if (*pval == NULL) {
879
1.63M
            typ = ASN1_TYPE_new();
880
1.63M
            if (typ == NULL)
881
0
                goto err;
882
1.63M
            *pval = (ASN1_VALUE *)typ;
883
1.63M
        } else
884
23.1k
            typ = (ASN1_TYPE *)*pval;
885
886
1.66M
        if (utype != typ->type)
887
1.66M
            ASN1_TYPE_set(typ, utype, NULL);
888
1.66M
        opval = pval;
889
1.66M
        pval = &typ->value.asn1_value;
890
1.66M
    }
891
13.2M
    switch (utype) {
892
4.99M
    case V_ASN1_OBJECT:
893
4.99M
        if (len != (long)ilen
894
4.99M
            || !ossl_c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, ilen))
895
6.23k
            goto err;
896
4.98M
        break;
897
898
4.98M
    case V_ASN1_NULL:
899
78.1k
        if (len) {
900
526
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
901
526
            goto err;
902
526
        }
903
77.6k
        *pval = (ASN1_VALUE *)1;
904
77.6k
        break;
905
906
56.7k
    case V_ASN1_BOOLEAN:
907
56.7k
        if (len != 1) {
908
961
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
909
961
            goto err;
910
55.7k
        } else {
911
55.7k
            ASN1_BOOLEAN *tbool;
912
55.7k
            tbool = (ASN1_BOOLEAN *)pval;
913
55.7k
            *tbool = *cont;
914
55.7k
        }
915
55.7k
        break;
916
917
1.47M
    case V_ASN1_BIT_STRING:
918
1.47M
        if (!ossl_c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
919
7.55k
            goto err;
920
1.46M
        break;
921
922
1.46M
    case V_ASN1_INTEGER:
923
1.39M
    case V_ASN1_ENUMERATED:
924
1.39M
        tint = (ASN1_INTEGER **)pval;
925
1.39M
        if (!ossl_c2i_ASN1_INTEGER(tint, &cont, len))
926
57.5k
            goto err;
927
        /* Fixup type to match the expected form */
928
1.33M
        (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
929
1.33M
        break;
930
931
755k
    case V_ASN1_OCTET_STRING:
932
875k
    case V_ASN1_NUMERICSTRING:
933
1.68M
    case V_ASN1_PRINTABLESTRING:
934
1.81M
    case V_ASN1_T61STRING:
935
1.81M
    case V_ASN1_VIDEOTEXSTRING:
936
2.19M
    case V_ASN1_IA5STRING:
937
2.47M
    case V_ASN1_UTCTIME:
938
2.63M
    case V_ASN1_GENERALIZEDTIME:
939
2.66M
    case V_ASN1_GRAPHICSTRING:
940
2.66M
    case V_ASN1_VISIBLESTRING:
941
2.66M
    case V_ASN1_GENERALSTRING:
942
2.84M
    case V_ASN1_UNIVERSALSTRING:
943
2.94M
    case V_ASN1_BMPSTRING:
944
3.08M
    case V_ASN1_UTF8STRING:
945
3.15M
    case V_ASN1_OTHER:
946
3.90M
    case V_ASN1_SET:
947
4.63M
    case V_ASN1_SEQUENCE:
948
5.28M
    default:
949
5.28M
        if (len != (long)ilen) {
950
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
951
0
            goto err;
952
0
        }
953
5.28M
        if (utype == V_ASN1_BMPSTRING && (len & 1)) {
954
540
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
955
540
            goto err;
956
540
        }
957
5.28M
        if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
958
640
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
959
640
            goto err;
960
640
        }
961
        /* All based on ASN1_STRING and handled the same */
962
5.28M
        if (*pval == NULL) {
963
1.93M
            stmp = ASN1_STRING_type_new(utype);
964
1.93M
            if (stmp == NULL) {
965
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
966
0
                goto err;
967
0
            }
968
1.93M
            *pval = (ASN1_VALUE *)stmp;
969
3.35M
        } else {
970
3.35M
            stmp = (ASN1_STRING *)*pval;
971
3.35M
            stmp->type = utype;
972
3.35M
        }
973
        /* If we've already allocated a buffer use it */
974
5.28M
        if (*free_cont) {
975
1.16M
            OPENSSL_free(stmp->data);
976
1.16M
            stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
977
1.16M
            stmp->length = ilen;
978
1.16M
            *free_cont = 0;
979
4.11M
        } else {
980
4.11M
            if (!ASN1_STRING_set(stmp, cont, ilen)) {
981
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
982
0
                ASN1_STRING_free(stmp);
983
0
                *pval = NULL;
984
0
                goto err;
985
0
            }
986
4.11M
        }
987
5.28M
        break;
988
13.2M
    }
989
    /* If ASN1_ANY and NULL type fix up value */
990
13.2M
    if (typ && (utype == V_ASN1_NULL))
991
75.0k
        typ->value.ptr = NULL;
992
993
13.2M
    ret = 1;
994
13.2M
err:
995
13.2M
    if (!ret) {
996
73.9k
        ASN1_TYPE_free(typ);
997
73.9k
        if (opval)
998
3.45k
            *opval = NULL;
999
73.9k
    }
1000
13.2M
    return ret;
1001
13.2M
}
1002
1003
/*
1004
 * This function finds the end of an ASN1 structure when passed its maximum
1005
 * length, whether it is indefinite length and a pointer to the content. This
1006
 * is more efficient than calling asn1_collect because it does not recurse on
1007
 * each indefinite length header.
1008
 */
1009
1010
static int asn1_find_end(const unsigned char **in, long len, char inf)
1011
1.33M
{
1012
1.33M
    uint32_t expected_eoc;
1013
1.33M
    long plen;
1014
1.33M
    const unsigned char *p = *in, *q;
1015
    /* If not indefinite length constructed just add length */
1016
1.33M
    if (inf == 0) {
1017
0
        *in += len;
1018
0
        return 1;
1019
0
    }
1020
1.33M
    expected_eoc = 1;
1021
    /*
1022
     * Indefinite length constructed form. Find the end when enough EOCs are
1023
     * found. If more indefinite length constructed headers are encountered
1024
     * increment the expected eoc count otherwise just skip to the end of the
1025
     * data.
1026
     */
1027
344M
    while (len > 0) {
1028
344M
        if (asn1_check_eoc(&p, len)) {
1029
34.9M
            expected_eoc--;
1030
34.9M
            if (expected_eoc == 0)
1031
1.23M
                break;
1032
33.7M
            len -= 2;
1033
33.7M
            continue;
1034
34.9M
        }
1035
309M
        q = p;
1036
        /* Just read in a header: only care about the length */
1037
309M
        if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1038
309M
                -1, 0, 0, NULL)) {
1039
68.7k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1040
68.7k
            return 0;
1041
68.7k
        }
1042
309M
        if (inf) {
1043
70.0M
            if (expected_eoc == UINT32_MAX) {
1044
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1045
0
                return 0;
1046
0
            }
1047
70.0M
            expected_eoc++;
1048
239M
        } else {
1049
239M
            p += plen;
1050
239M
        }
1051
309M
        len -= p - q;
1052
309M
    }
1053
1.26M
    if (expected_eoc) {
1054
35.8k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1055
35.8k
        return 0;
1056
35.8k
    }
1057
1.23M
    *in = p;
1058
1.23M
    return 1;
1059
1.26M
}
1060
1061
/*
1062
 * This function collects the asn1 data from a constructed string type into
1063
 * a buffer. The values of 'in' and 'len' should refer to the contents of the
1064
 * constructed type and 'inf' should be set if it is indefinite length.
1065
 */
1066
1067
#ifndef ASN1_MAX_STRING_NEST
1068
/*
1069
 * This determines how many levels of recursion are permitted in ASN1 string
1070
 * types. If it is not limited stack overflows can occur. If set to zero no
1071
 * recursion is allowed at all. Although zero should be adequate examples
1072
 * exist that require a value of 1. So 5 should be more than enough.
1073
 */
1074
2.48M
#define ASN1_MAX_STRING_NEST 5
1075
#endif
1076
1077
static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1078
    char inf, int tag, int aclass, int depth)
1079
11.2M
{
1080
11.2M
    const unsigned char *p, *q;
1081
11.2M
    long plen;
1082
11.2M
    char cst, ininf;
1083
11.2M
    p = *in;
1084
11.2M
    inf &= 1;
1085
    /*
1086
     * If no buffer and not indefinite length constructed just pass over the
1087
     * encoded data
1088
     */
1089
11.2M
    if (!buf && !inf) {
1090
0
        *in += len;
1091
0
        return 1;
1092
0
    }
1093
46.0M
    while (len > 0) {
1094
35.9M
        q = p;
1095
        /* Check for EOC */
1096
35.9M
        if (asn1_check_eoc(&p, len)) {
1097
            /*
1098
             * EOC is illegal outside indefinite length constructed form
1099
             */
1100
1.03M
            if (!inf) {
1101
9.97k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
1102
9.97k
                return 0;
1103
9.97k
            }
1104
1.02M
            inf = 0;
1105
1.02M
            break;
1106
1.03M
        }
1107
1108
34.9M
        if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1109
34.9M
                len, tag, aclass, 0, NULL)) {
1110
37.6k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1111
37.6k
            return 0;
1112
37.6k
        }
1113
1114
        /* If indefinite length constructed update max length */
1115
34.9M
        if (cst) {
1116
2.48M
            if (depth >= ASN1_MAX_STRING_NEST) {
1117
3.39k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_ASN1_STRING);
1118
3.39k
                return 0;
1119
3.39k
            }
1120
2.47M
            if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1121
58.8k
                return 0;
1122
32.4M
        } else if (plen && !collect_data(buf, &p, plen))
1123
0
            return 0;
1124
34.8M
        len -= p - q;
1125
34.8M
    }
1126
11.1M
    if (inf) {
1127
14.6k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1128
14.6k
        return 0;
1129
14.6k
    }
1130
11.0M
    *in = p;
1131
11.0M
    return 1;
1132
11.1M
}
1133
1134
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1135
17.1M
{
1136
17.1M
    int len;
1137
17.1M
    if (buf) {
1138
17.1M
        len = buf->length;
1139
17.1M
        if (!BUF_MEM_grow_clean(buf, len + plen)) {
1140
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
1141
0
            return 0;
1142
0
        }
1143
17.1M
        memcpy(buf->data + len, *p, plen);
1144
17.1M
    }
1145
17.1M
    *p += plen;
1146
17.1M
    return 1;
1147
17.1M
}
1148
1149
/* Check for ASN1 EOC and swallow it if found */
1150
1151
static int asn1_check_eoc(const unsigned char **in, long len)
1152
748M
{
1153
748M
    const unsigned char *p;
1154
1155
748M
    if (len < 2)
1156
194k
        return 0;
1157
748M
    p = *in;
1158
748M
    if (p[0] == '\0' && p[1] == '\0') {
1159
57.3M
        *in += 2;
1160
57.3M
        return 1;
1161
57.3M
    }
1162
691M
    return 0;
1163
748M
}
1164
1165
/*
1166
 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1167
 * length for indefinite length constructed form, we don't know the exact
1168
 * length but we can set an upper bound to the amount of data available minus
1169
 * the header length just read.
1170
 */
1171
1172
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1173
    char *inf, char *cst,
1174
    const unsigned char **in, long len,
1175
    int exptag, int expclass, char opt, ASN1_TLC *ctx)
1176
800M
{
1177
800M
    int i;
1178
800M
    int ptag, pclass;
1179
800M
    long plen;
1180
800M
    const unsigned char *p, *q;
1181
800M
    p = *in;
1182
800M
    q = p;
1183
1184
800M
    if (len <= 0) {
1185
995
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
1186
995
        goto err;
1187
995
    }
1188
800M
    if (ctx != NULL && ctx->valid) {
1189
95.9M
        i = ctx->ret;
1190
95.9M
        plen = ctx->plen;
1191
95.9M
        pclass = ctx->pclass;
1192
95.9M
        ptag = ctx->ptag;
1193
95.9M
        p += ctx->hdrlen;
1194
704M
    } else {
1195
704M
        i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1196
704M
        if (ctx != NULL) {
1197
360M
            ctx->ret = i;
1198
360M
            ctx->plen = plen;
1199
360M
            ctx->pclass = pclass;
1200
360M
            ctx->ptag = ptag;
1201
360M
            ctx->hdrlen = p - q;
1202
360M
            ctx->valid = 1;
1203
            /*
1204
             * If definite length, and no error, length + header can't exceed
1205
             * total amount of data available.
1206
             */
1207
360M
            if ((i & 0x81) == 0 && (plen + ctx->hdrlen) > len) {
1208
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
1209
0
                goto err;
1210
0
            }
1211
360M
        }
1212
704M
    }
1213
1214
800M
    if ((i & 0x80) != 0) {
1215
966k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER);
1216
966k
        goto err;
1217
966k
    }
1218
799M
    if (exptag >= 0) {
1219
378M
        if (exptag != ptag || expclass != pclass) {
1220
            /*
1221
             * If type is OPTIONAL, not an error: indicate missing type.
1222
             */
1223
34.6M
            if (opt != 0)
1224
24.3M
                return -1;
1225
34.6M
            ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG);
1226
10.3M
            goto err;
1227
34.6M
        }
1228
        /*
1229
         * We have a tag and class match: assume we are going to do something
1230
         * with it
1231
         */
1232
343M
        asn1_tlc_clear(ctx);
1233
343M
    }
1234
1235
764M
    if ((i & 1) != 0)
1236
103M
        plen = len - (p - q);
1237
1238
764M
    if (inf != NULL)
1239
692M
        *inf = i & 1;
1240
1241
764M
    if (cst != NULL)
1242
212M
        *cst = i & V_ASN1_CONSTRUCTED;
1243
1244
764M
    if (olen != NULL)
1245
692M
        *olen = plen;
1246
1247
764M
    if (oclass != NULL)
1248
72.5M
        *oclass = pclass;
1249
1250
764M
    if (otag != NULL)
1251
72.5M
        *otag = ptag;
1252
1253
764M
    *in = p;
1254
764M
    return 1;
1255
1256
11.2M
err:
1257
    asn1_tlc_clear(ctx);
1258
11.2M
    return 0;
1259
799M
}