Coverage Report

Created: 2026-05-24 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/asn1/tasn_dec.c
Line
Count
Source
1
/*
2
 * Copyright 2000-2024 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stddef.h>
11
#include <string.h>
12
#include <openssl/asn1.h>
13
#include <openssl/asn1t.h>
14
#include <openssl/objects.h>
15
#include <openssl/buffer.h>
16
#include <openssl/err.h>
17
#include "crypto/asn1.h"
18
#include "internal/numbers.h"
19
#include "asn1_local.h"
20
21
/*
22
 * Constructed types with a recursive definition (such as can be found in PKCS7)
23
 * could eventually exceed the stack given malicious input with excessive
24
 * recursion. Therefore we limit the stack depth. This is the maximum number of
25
 * recursive invocations of asn1_item_embed_d2i().
26
 */
27
408M
#define ASN1_MAX_CONSTRUCTED_NEST 30
28
29
static int asn1_check_eoc(const unsigned char **in, long len);
30
static int asn1_find_end(const unsigned char **in, long len, char inf);
31
32
static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
33
    char inf, int tag, int aclass, int depth);
34
35
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
36
37
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
38
    char *inf, char *cst,
39
    const unsigned char **in, long len,
40
    int exptag, int expclass, char opt, ASN1_TLC *ctx);
41
42
static int asn1_template_ex_d2i(ASN1_VALUE **pval,
43
    const unsigned char **in, long len,
44
    const ASN1_TEMPLATE *tt, char opt,
45
    ASN1_TLC *ctx, int depth, OSSL_LIB_CTX *libctx,
46
    const char *propq);
47
static int asn1_template_noexp_d2i(ASN1_VALUE **val,
48
    const unsigned char **in, long len,
49
    const ASN1_TEMPLATE *tt, char opt,
50
    ASN1_TLC *ctx, int depth,
51
    OSSL_LIB_CTX *libctx, const char *propq);
52
static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
53
    const unsigned char **in, long len,
54
    const ASN1_ITEM *it,
55
    int tag, int aclass, char opt,
56
    ASN1_TLC *ctx);
57
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
58
    int utype, char *free_cont, const ASN1_ITEM *it);
59
60
/* Table to convert tags to bit values, used for MSTRING type */
61
static const unsigned long tag2bit[32] = {
62
    /* tags  0 -  3 */
63
    0,
64
    0,
65
    0,
66
    B_ASN1_BIT_STRING,
67
    /* tags  4- 7 */
68
    B_ASN1_OCTET_STRING,
69
    0,
70
    0,
71
    B_ASN1_UNKNOWN,
72
    /* tags  8-11 */
73
    B_ASN1_UNKNOWN,
74
    B_ASN1_UNKNOWN,
75
    0,
76
    B_ASN1_UNKNOWN,
77
    /* tags 12-15 */
78
    B_ASN1_UTF8STRING,
79
    B_ASN1_UNKNOWN,
80
    B_ASN1_UNKNOWN,
81
    B_ASN1_UNKNOWN,
82
    /* tags 16-19 */
83
    B_ASN1_SEQUENCE,
84
    0,
85
    B_ASN1_NUMERICSTRING,
86
    B_ASN1_PRINTABLESTRING,
87
    /* tags 20-22 */
88
    B_ASN1_T61STRING,
89
    B_ASN1_VIDEOTEXSTRING,
90
    B_ASN1_IA5STRING,
91
    /* tags 23-24 */
92
    B_ASN1_UTCTIME,
93
    B_ASN1_GENERALIZEDTIME,
94
    /* tags 25-27 */
95
    B_ASN1_GRAPHICSTRING,
96
    B_ASN1_ISO64STRING,
97
    B_ASN1_GENERALSTRING,
98
    /* tags 28-31 */
99
    B_ASN1_UNIVERSALSTRING,
100
    B_ASN1_UNKNOWN,
101
    B_ASN1_BMPSTRING,
102
    B_ASN1_UNKNOWN,
103
};
104
105
unsigned long ASN1_tag2bit(int tag)
106
44.0M
{
107
44.0M
    if ((tag < 0) || (tag > 30))
108
5.96k
        return 0;
109
44.0M
    return tag2bit[tag];
110
44.0M
}
111
112
/* Macro to initialize and invalidate the cache */
113
114
#define asn1_tlc_clear(c)   \
115
380M
    do {                    \
116
380M
        if ((c) != NULL)    \
117
380M
            (c)->valid = 0; \
118
380M
    } while (0)
119
/* Version to avoid compiler warning about 'c' always non-NULL */
120
#define asn1_tlc_clear_nc(c) \
121
24.6M
    do {                     \
122
24.6M
        (c)->valid = 0;      \
123
24.6M
    } 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.6M
{
137
27.6M
    int rv;
138
139
27.6M
    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.6M
    rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0,
144
27.6M
        libctx, propq);
145
27.6M
    if (rv <= 0)
146
16.7M
        ASN1_item_ex_free(pval, it);
147
27.6M
    return rv;
148
27.6M
}
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.95M
{
154
2.95M
    return asn1_item_ex_d2i_intern(pval, in, len, it, tag, aclass, opt, ctx,
155
2.95M
        NULL, NULL);
156
2.95M
}
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.6M
{
163
24.6M
    ASN1_TLC c;
164
24.6M
    ASN1_VALUE *ptmpval = NULL;
165
166
24.6M
    if (pval == NULL)
167
21.6M
        pval = &ptmpval;
168
24.6M
    asn1_tlc_clear_nc(&c);
169
24.6M
    if (asn1_item_ex_d2i_intern(pval, in, len, it, -1, 0, 0, &c, libctx,
170
24.6M
            propq)
171
24.6M
        > 0)
172
8.29M
        return *pval;
173
16.3M
    return NULL;
174
24.6M
}
175
176
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
177
    const unsigned char **in, long len,
178
    const ASN1_ITEM *it)
179
23.0M
{
180
23.0M
    return ASN1_item_d2i_ex(pval, in, len, it, NULL, NULL);
181
23.0M
}
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
408M
{
194
408M
    const ASN1_TEMPLATE *tt, *errtt = NULL;
195
408M
    const ASN1_EXTERN_FUNCS *ef;
196
408M
    const ASN1_AUX *aux;
197
408M
    ASN1_aux_cb *asn1_cb;
198
408M
    const unsigned char *p = NULL, *q;
199
408M
    unsigned char oclass;
200
408M
    char seq_eoc, seq_nolen, cst, isopt;
201
408M
    long tmplen;
202
408M
    int i;
203
408M
    int otag;
204
408M
    int ret = 0;
205
408M
    ASN1_VALUE **pchptr;
206
207
408M
    if (pval == NULL || it == NULL) {
208
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
209
0
        return 0;
210
0
    }
211
408M
    if (len <= 0) {
212
86.0k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
213
86.0k
        return 0;
214
86.0k
    }
215
408M
    aux = it->funcs;
216
408M
    if (aux && aux->asn1_cb)
217
13.6M
        asn1_cb = aux->asn1_cb;
218
395M
    else
219
395M
        asn1_cb = 0;
220
221
408M
    if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
222
16
        ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_TOO_DEEP);
223
16
        goto err;
224
16
    }
225
226
408M
    switch (it->itype) {
227
311M
    case ASN1_ITYPE_PRIMITIVE:
228
311M
        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
170M
            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
170M
            return asn1_template_ex_d2i(pval, in, len, it->templates, opt, ctx,
241
170M
                depth, libctx, propq);
242
170M
        }
243
141M
        return asn1_d2i_ex_primitive(pval, in, len, it,
244
141M
            tag, aclass, opt, ctx);
245
246
22.3M
    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.3M
        if (tag != -1) {
252
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
253
0
            goto err;
254
0
        }
255
256
22.3M
        p = *in;
257
        /* Just read in tag and class */
258
22.3M
        ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
259
22.3M
            &p, len, -1, 0, 1, ctx);
260
22.3M
        if (!ret) {
261
22.7k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
262
22.7k
            goto err;
263
22.7k
        }
264
265
        /* Must be UNIVERSAL class */
266
22.2M
        if (oclass != V_ASN1_UNIVERSAL) {
267
            /* If OPTIONAL, assume this is OK */
268
156k
            if (opt)
269
122k
                return -1;
270
156k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
271
33.9k
            goto err;
272
156k
        }
273
274
        /* Check tag matches bit map */
275
22.1M
        if (!(ASN1_tag2bit(otag) & it->utype)) {
276
            /* If OPTIONAL, assume this is OK */
277
430k
            if (opt)
278
17.1k
                return -1;
279
430k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_WRONG_TAG);
280
413k
            goto err;
281
430k
        }
282
21.7M
        return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
283
284
5.90M
    case ASN1_ITYPE_EXTERN:
285
        /* Use new style d2i */
286
5.90M
        ef = it->funcs;
287
5.90M
        if (ef->asn1_ex_d2i_ex != NULL)
288
2.94M
            return ef->asn1_ex_d2i_ex(pval, in, len, it, tag, aclass, opt, ctx,
289
2.94M
                libctx, propq);
290
2.95M
        return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
291
292
6.17M
    case ASN1_ITYPE_CHOICE:
293
        /*
294
         * It never makes sense for CHOICE types to have implicit tagging, so
295
         * if tag != -1, then this looks like an error in the template.
296
         */
297
6.17M
        if (tag != -1) {
298
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
299
0
            goto err;
300
0
        }
301
302
6.17M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
303
0
            goto auxerr;
304
6.17M
        if (*pval) {
305
            /* Free up and zero CHOICE value if initialised */
306
853k
            i = ossl_asn1_get_choice_selector(pval, it);
307
853k
            if ((i >= 0) && (i < it->tcount)) {
308
0
                tt = it->templates + i;
309
0
                pchptr = ossl_asn1_get_field_ptr(pval, tt);
310
0
                ossl_asn1_template_free(pchptr, tt);
311
0
                ossl_asn1_set_choice_selector(pval, -1, it);
312
0
            }
313
5.31M
        } else if (!ossl_asn1_item_ex_new_intern(pval, it, libctx, propq)) {
314
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
315
0
            goto err;
316
0
        }
317
        /* CHOICE type, try each possibility in turn */
318
6.17M
        p = *in;
319
23.8M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
320
23.1M
            pchptr = ossl_asn1_get_field_ptr(pval, tt);
321
            /*
322
             * We mark field as OPTIONAL so its absence can be recognised.
323
             */
324
23.1M
            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth,
325
23.1M
                libctx, propq);
326
            /* If field not present, try the next one */
327
23.1M
            if (ret == -1)
328
17.6M
                continue;
329
            /* If positive return, read OK, break loop */
330
5.46M
            if (ret > 0)
331
4.43M
                break;
332
            /*
333
             * Must be an ASN1 parsing error.
334
             * Free up any partial choice value
335
             */
336
1.03M
            ossl_asn1_template_free(pchptr, tt);
337
1.03M
            errtt = tt;
338
1.03M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
339
1.03M
            goto err;
340
5.46M
        }
341
342
        /* Did we fall off the end without reading anything? */
343
5.13M
        if (i == it->tcount) {
344
            /* If OPTIONAL, this is OK */
345
704k
            if (opt) {
346
                /* Free and zero it */
347
31.3k
                ASN1_item_ex_free(pval, it);
348
31.3k
                return -1;
349
31.3k
            }
350
704k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
351
673k
            goto err;
352
704k
        }
353
354
4.43M
        ossl_asn1_set_choice_selector(pval, i, it);
355
356
4.43M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
357
0
            goto auxerr;
358
4.43M
        *in = p;
359
4.43M
        return 1;
360
361
876k
    case ASN1_ITYPE_NDEF_SEQUENCE:
362
63.0M
    case ASN1_ITYPE_SEQUENCE:
363
63.0M
        p = *in;
364
63.0M
        tmplen = len;
365
366
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
367
63.0M
        if (tag == -1) {
368
57.7M
            tag = V_ASN1_SEQUENCE;
369
57.7M
            aclass = V_ASN1_UNIVERSAL;
370
57.7M
        }
371
        /* Get SEQUENCE length and update len, p */
372
63.0M
        ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
373
63.0M
            &p, len, tag, aclass, opt, ctx);
374
63.0M
        if (!ret) {
375
4.46M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
376
4.46M
            goto err;
377
58.6M
        } else if (ret == -1)
378
5.85M
            return -1;
379
52.7M
        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
52.7M
        else
385
52.7M
            seq_nolen = seq_eoc;
386
52.7M
        if (!cst) {
387
123k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
388
123k
            goto err;
389
123k
        }
390
391
52.6M
        if (*pval == NULL
392
39.2M
            && !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
52.6M
        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
199M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
402
146M
            if (tt->flags & ASN1_TFLG_ADB_MASK) {
403
776k
                const ASN1_TEMPLATE *seqtt;
404
776k
                ASN1_VALUE **pseqval;
405
776k
                seqtt = ossl_asn1_do_adb(*pval, tt, 0);
406
776k
                if (seqtt == NULL)
407
625k
                    continue;
408
150k
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
409
150k
                ossl_asn1_template_free(pseqval, seqtt);
410
150k
            }
411
146M
        }
412
413
        /* Get each field entry */
414
154M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
415
118M
            const ASN1_TEMPLATE *seqtt;
416
118M
            ASN1_VALUE **pseqval;
417
118M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
418
118M
            if (seqtt == NULL)
419
0
                goto err;
420
118M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
421
            /* Have we ran out of data? */
422
118M
            if (!len)
423
2.83M
                break;
424
115M
            q = p;
425
115M
            if (asn1_check_eoc(&p, len)) {
426
3.77M
                if (!seq_eoc) {
427
31.2k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
428
31.2k
                    goto err;
429
31.2k
                }
430
3.74M
                len -= p - q;
431
3.74M
                seq_eoc = 0;
432
3.74M
                break;
433
3.77M
            }
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
111M
            if (i == (it->tcount - 1))
441
37.8M
                isopt = 0;
442
73.9M
            else
443
73.9M
                isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
444
            /*
445
             * attempt to read in field, allowing each to be OPTIONAL
446
             */
447
448
111M
            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
449
111M
                depth, libctx, propq);
450
111M
            if (!ret) {
451
9.96M
                errtt = seqtt;
452
9.96M
                goto err;
453
101M
            } else if (ret == -1) {
454
                /*
455
                 * OPTIONAL component absent. Free and zero the field.
456
                 */
457
10.4M
                ossl_asn1_template_free(pseqval, seqtt);
458
10.4M
                continue;
459
10.4M
            }
460
            /* Update length */
461
91.3M
            len -= p - q;
462
91.3M
        }
463
464
        /* Check for EOC if expecting one */
465
42.6M
        if (seq_eoc && !asn1_check_eoc(&p, len)) {
466
199k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
467
199k
            goto err;
468
199k
        }
469
        /* Check all data read */
470
42.4M
        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
55.2M
        for (; i < it->tcount; tt++, i++) {
481
13.0M
            const ASN1_TEMPLATE *seqtt;
482
13.0M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
483
13.0M
            if (seqtt == NULL)
484
0
                goto err;
485
13.0M
            if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
486
12.7M
                ASN1_VALUE **pseqval;
487
12.7M
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
488
12.7M
                ossl_asn1_template_free(pseqval, seqtt);
489
12.7M
            } else {
490
288k
                errtt = seqtt;
491
288k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_FIELD_MISSING);
492
288k
                goto err;
493
288k
            }
494
13.0M
        }
495
        /* Save encoding */
496
42.1M
        if (!ossl_asn1_enc_save(pval, *in, p - *in, it))
497
0
            goto auxerr;
498
42.1M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
499
22.0k
            goto auxerr;
500
42.0M
        *in = p;
501
42.0M
        return 1;
502
503
0
    default:
504
0
        return 0;
505
408M
    }
506
22.0k
auxerr:
507
22.0k
    ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
508
17.3M
err:
509
17.3M
    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.3M
    return 0;
515
22.0k
}
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
305M
{
528
305M
    int flags, aclass;
529
305M
    int ret;
530
305M
    long len;
531
305M
    const unsigned char *p, *q;
532
305M
    char exp_eoc;
533
305M
    if (!val)
534
0
        return 0;
535
305M
    flags = tt->flags;
536
305M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
537
538
305M
    p = *in;
539
540
    /* Check if EXPLICIT tag expected */
541
305M
    if (flags & ASN1_TFLG_EXPTAG) {
542
8.11M
        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
8.11M
        ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
548
8.11M
            &p, inlen, tt->tag, aclass, opt, ctx);
549
8.11M
        q = p;
550
8.11M
        if (!ret) {
551
370k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
552
370k
            return 0;
553
7.74M
        } else if (ret == -1)
554
5.90M
            return -1;
555
1.84M
        if (!cst) {
556
6.64k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
557
6.64k
            return 0;
558
6.64k
        }
559
        /* We've found the field so it can't be OPTIONAL now */
560
1.83M
        ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth, libctx,
561
1.83M
            propq);
562
1.83M
        if (!ret) {
563
321k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
564
321k
            return 0;
565
321k
        }
566
        /* We read the field in OK so update length */
567
1.51M
        len -= p - q;
568
1.51M
        if (exp_eoc) {
569
            /* If NDEF we must have an EOC here */
570
484k
            if (!asn1_check_eoc(&p, len)) {
571
47.6k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
572
47.6k
                goto err;
573
47.6k
            }
574
1.03M
        } else {
575
            /*
576
             * Otherwise we must hit the EXPLICIT tag end or its an error
577
             */
578
1.03M
            if (len) {
579
4.61k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
580
4.61k
                goto err;
581
4.61k
            }
582
1.03M
        }
583
1.51M
    } else
584
296M
        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth,
585
296M
            libctx, propq);
586
587
1.46M
    *in = p;
588
1.46M
    return 1;
589
590
52.3k
err:
591
52.3k
    return 0;
592
305M
}
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
298M
{
600
298M
    int flags, aclass;
601
298M
    int ret;
602
298M
    ASN1_VALUE *tval;
603
298M
    const unsigned char *p, *q;
604
298M
    if (!val)
605
0
        return 0;
606
298M
    flags = tt->flags;
607
298M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
608
609
298M
    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
298M
    if (tt->flags & ASN1_TFLG_EMBED) {
616
13.5M
        tval = (ASN1_VALUE *)val;
617
13.5M
        val = &tval;
618
13.5M
    }
619
620
298M
    if (flags & ASN1_TFLG_SK_MASK) {
621
        /* SET OF, SEQUENCE OF */
622
174M
        int sktag, skaclass;
623
174M
        char sk_eoc;
624
        /* First work out expected inner tag value */
625
174M
        if (flags & ASN1_TFLG_IMPTAG) {
626
1.38M
            sktag = tt->tag;
627
1.38M
            skaclass = aclass;
628
172M
        } else {
629
172M
            skaclass = V_ASN1_UNIVERSAL;
630
172M
            if (flags & ASN1_TFLG_SET_OF)
631
165M
                sktag = V_ASN1_SET;
632
7.30M
            else
633
7.30M
                sktag = V_ASN1_SEQUENCE;
634
172M
        }
635
        /* Get the tag */
636
174M
        ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
637
174M
            &p, len, sktag, skaclass, opt, ctx);
638
174M
        if (!ret) {
639
1.34M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
640
1.34M
            return 0;
641
172M
        } else if (ret == -1)
642
821k
            return -1;
643
171M
        if (*val == NULL)
644
171M
            *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
645
713k
        else {
646
            /*
647
             * We've got a valid STACK: free up any items present
648
             */
649
713k
            STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
650
713k
            ASN1_VALUE *vtmp;
651
713k
            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
713k
        }
656
657
171M
        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
423M
        while (len > 0) {
664
260M
            ASN1_VALUE *skfield;
665
260M
            q = p;
666
            /* See if EOC found */
667
260M
            if (asn1_check_eoc(&p, len)) {
668
6.65M
                if (!sk_eoc) {
669
12.8k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
670
12.8k
                    goto err;
671
12.8k
                }
672
6.64M
                len -= p - q;
673
6.64M
                sk_eoc = 0;
674
6.64M
                break;
675
6.65M
            }
676
253M
            skfield = NULL;
677
253M
            if (asn1_item_embed_d2i(&skfield, &p, len,
678
253M
                    ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
679
253M
                    depth, libctx, propq)
680
253M
                <= 0) {
681
1.95M
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
682
                /* |skfield| may be partially allocated despite failure. */
683
1.95M
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
684
1.95M
                goto err;
685
1.95M
            }
686
251M
            len -= p - q;
687
251M
            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
251M
        }
693
169M
        if (sk_eoc) {
694
16.3k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
695
16.3k
            goto err;
696
16.3k
        }
697
169M
    } else if (flags & ASN1_TFLG_IMPTAG) {
698
        /* IMPLICIT tagging */
699
18.9M
        ret = asn1_item_embed_d2i(val, &p, len,
700
18.9M
            ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
701
18.9M
            ctx, depth, libctx, propq);
702
18.9M
        if (!ret) {
703
539k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
704
539k
            goto err;
705
18.4M
        } else if (ret == -1)
706
14.9M
            return -1;
707
105M
    } else {
708
        /* Nothing special */
709
105M
        ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
710
105M
            -1, 0, opt, ctx, depth, libctx, propq);
711
105M
        if (!ret) {
712
8.82M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
713
8.82M
            goto err;
714
96.9M
        } else if (ret == -1)
715
6.48M
            return -1;
716
105M
    }
717
718
263M
    *in = p;
719
263M
    return 1;
720
721
11.3M
err:
722
11.3M
    return 0;
723
298M
}
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
162M
{
730
162M
    int ret = 0, utype;
731
162M
    long plen;
732
162M
    char cst, inf, free_cont = 0;
733
162M
    const unsigned char *p;
734
162M
    BUF_MEM buf = { 0, NULL, 0, 0 };
735
162M
    const unsigned char *cont = NULL;
736
162M
    long len;
737
738
162M
    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
162M
    if (it->itype == ASN1_ITYPE_MSTRING) {
744
21.7M
        utype = tag;
745
21.7M
        tag = -1;
746
21.7M
    } else
747
141M
        utype = it->utype;
748
749
162M
    if (utype == V_ASN1_ANY) {
750
        /* If type is ANY need to figure out type from tag */
751
59.6M
        unsigned char oclass;
752
59.6M
        if (tag >= 0) {
753
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
754
0
            return 0;
755
0
        }
756
59.6M
        if (opt) {
757
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
758
0
            return 0;
759
0
        }
760
59.6M
        p = *in;
761
59.6M
        ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
762
59.6M
            &p, inlen, -1, 0, 0, ctx);
763
59.6M
        if (!ret) {
764
39.8k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
765
39.8k
            return 0;
766
39.8k
        }
767
59.5M
        if (oclass != V_ASN1_UNIVERSAL)
768
4.82M
            utype = V_ASN1_OTHER;
769
59.5M
    }
770
162M
    if (tag == -1) {
771
149M
        tag = utype;
772
149M
        aclass = V_ASN1_UNIVERSAL;
773
149M
    }
774
162M
    p = *in;
775
    /* Check header */
776
162M
    ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
777
162M
        &p, inlen, tag, aclass, opt, ctx);
778
162M
    if (!ret) {
779
7.02M
        ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
780
7.02M
        return 0;
781
155M
    } else if (ret == -1)
782
15.4M
        return -1;
783
140M
    ret = 0;
784
    /* SEQUENCE, SET and "OTHER" are left in encoded form */
785
140M
    if ((utype == V_ASN1_SEQUENCE)
786
136M
        || (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.3M
        if (utype == V_ASN1_OTHER) {
792
4.82M
            asn1_tlc_clear(ctx);
793
4.82M
        }
794
        /* SEQUENCE and SET must be constructed */
795
20.5M
        else if (!cst) {
796
15.3k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
797
15.3k
            return 0;
798
15.3k
        }
799
800
25.3M
        cont = *in;
801
        /* If indefinite length constructed find the real end */
802
25.3M
        if (inf) {
803
1.45M
            if (!asn1_find_end(&p, plen, inf))
804
118k
                goto err;
805
1.33M
            len = p - cont;
806
23.8M
        } else {
807
23.8M
            len = p - cont + plen;
808
23.8M
            p += plen;
809
23.8M
        }
810
115M
    } else if (cst) {
811
9.49M
        if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
812
9.48M
            || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
813
9.46M
            || 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
9.46M
        free_cont = 1;
820
        /*
821
         * Should really check the internal tags are correct but some things
822
         * may get this wrong. The relevant specs say that constructed string
823
         * types should be OCTET STRINGs internally irrespective of the type.
824
         * So instead just check for UNIVERSAL class and ignore the tag.
825
         */
826
9.46M
        if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
827
68.6k
            goto err;
828
68.6k
        }
829
9.39M
        len = buf.length;
830
        /* Append a final null to string */
831
9.39M
        if (!BUF_MEM_grow_clean(&buf, len + 1)) {
832
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
833
0
            goto err;
834
0
        }
835
9.39M
        buf.data[len] = 0;
836
9.39M
        cont = (const unsigned char *)buf.data;
837
105M
    } else {
838
105M
        cont = p;
839
105M
        len = plen;
840
105M
        p += plen;
841
105M
    }
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
140M
    if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
846
870k
        goto err;
847
848
139M
    *in = p;
849
139M
    ret = 1;
850
140M
err:
851
140M
    if (free_cont)
852
533k
        OPENSSL_free(buf.data);
853
140M
    return ret;
854
139M
}
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
126M
{
861
126M
    ASN1_VALUE **opval = NULL;
862
126M
    ASN1_STRING *stmp;
863
126M
    ASN1_TYPE *typ = NULL;
864
126M
    int ret = 0;
865
126M
    const ASN1_PRIMITIVE_FUNCS *pf;
866
126M
    ASN1_INTEGER **tint;
867
126M
    pf = it->funcs;
868
869
126M
    if (pf && pf->prim_c2i)
870
3.48M
        return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
871
    /* If ANY type clear type and set pointer to internal value */
872
122M
    if (it->utype == V_ASN1_ANY) {
873
57.6M
        if (*pval == NULL) {
874
57.5M
            typ = ASN1_TYPE_new();
875
57.5M
            if (typ == NULL)
876
0
                goto err;
877
57.5M
            *pval = (ASN1_VALUE *)typ;
878
57.5M
        } else
879
119k
            typ = (ASN1_TYPE *)*pval;
880
881
57.6M
        if (utype != typ->type)
882
57.6M
            ASN1_TYPE_set(typ, utype, NULL);
883
57.6M
        opval = pval;
884
57.6M
        pval = &typ->value.asn1_value;
885
57.6M
    }
886
122M
    switch (utype) {
887
29.2M
    case V_ASN1_OBJECT:
888
29.2M
        if (!ossl_c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
889
49.4k
            goto err;
890
29.2M
        break;
891
892
29.2M
    case V_ASN1_NULL:
893
907k
        if (len) {
894
5.03k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
895
5.03k
            goto err;
896
5.03k
        }
897
902k
        *pval = (ASN1_VALUE *)1;
898
902k
        break;
899
900
7.95M
    case V_ASN1_BOOLEAN:
901
7.95M
        if (len != 1) {
902
8.06k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
903
8.06k
            goto err;
904
7.94M
        } else {
905
7.94M
            ASN1_BOOLEAN *tbool;
906
7.94M
            tbool = (ASN1_BOOLEAN *)pval;
907
7.94M
            *tbool = *cont;
908
7.94M
        }
909
7.94M
        break;
910
911
7.94M
    case V_ASN1_BIT_STRING:
912
6.67M
        if (!ossl_c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
913
47.4k
            goto err;
914
6.63M
        break;
915
916
11.6M
    case V_ASN1_INTEGER:
917
13.0M
    case V_ASN1_ENUMERATED:
918
13.0M
        tint = (ASN1_INTEGER **)pval;
919
13.0M
        if (!ossl_c2i_ASN1_INTEGER(tint, &cont, len))
920
550k
            goto err;
921
        /* Fixup type to match the expected form */
922
12.4M
        (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
923
12.4M
        break;
924
925
20.8M
    case V_ASN1_OCTET_STRING:
926
23.3M
    case V_ASN1_NUMERICSTRING:
927
27.0M
    case V_ASN1_PRINTABLESTRING:
928
27.7M
    case V_ASN1_T61STRING:
929
27.8M
    case V_ASN1_VIDEOTEXSTRING:
930
29.6M
    case V_ASN1_IA5STRING:
931
31.8M
    case V_ASN1_UTCTIME:
932
32.0M
    case V_ASN1_GENERALIZEDTIME:
933
32.5M
    case V_ASN1_GRAPHICSTRING:
934
32.5M
    case V_ASN1_VISIBLESTRING:
935
32.6M
    case V_ASN1_GENERALSTRING:
936
33.1M
    case V_ASN1_UNIVERSALSTRING:
937
34.0M
    case V_ASN1_BMPSTRING:
938
35.9M
    case V_ASN1_UTF8STRING:
939
40.6M
    case V_ASN1_OTHER:
940
56.1M
    case V_ASN1_SET:
941
59.6M
    case V_ASN1_SEQUENCE:
942
65.0M
    default:
943
65.0M
        if (utype == V_ASN1_BMPSTRING && (len & 1)) {
944
2.43k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
945
2.43k
            goto err;
946
2.43k
        }
947
65.0M
        if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
948
2.24k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
949
2.24k
            goto err;
950
2.24k
        }
951
65.0M
        if (utype == V_ASN1_GENERALIZEDTIME && (len < 15)) {
952
7.15k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_GENERALIZEDTIME_IS_TOO_SHORT);
953
7.15k
            goto err;
954
7.15k
        }
955
65.0M
        if (utype == V_ASN1_UTCTIME && (len < 13)) {
956
2.92k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UTCTIME_IS_TOO_SHORT);
957
2.92k
            goto err;
958
2.92k
        }
959
        /* All based on ASN1_STRING and handled the same */
960
65.0M
        if (*pval == NULL) {
961
44.4M
            stmp = ASN1_STRING_type_new(utype);
962
44.4M
            if (stmp == NULL) {
963
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
964
0
                goto err;
965
0
            }
966
44.4M
            *pval = (ASN1_VALUE *)stmp;
967
44.4M
        } else {
968
20.6M
            stmp = (ASN1_STRING *)*pval;
969
20.6M
            stmp->type = utype;
970
20.6M
        }
971
        /* If we've already allocated a buffer use it */
972
65.0M
        if (*free_cont) {
973
7.65M
            ASN1_STRING_set0(stmp, (unsigned char *)cont /* UGLY CAST! */, len);
974
7.65M
            *free_cont = 0;
975
57.4M
        } else {
976
57.4M
            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
57.4M
        }
983
65.0M
        break;
984
122M
    }
985
    /* If ASN1_ANY and NULL type fix up value */
986
122M
    if (typ && (utype == V_ASN1_NULL))
987
883k
        typ->value.ptr = NULL;
988
989
122M
    ret = 1;
990
122M
err:
991
122M
    if (!ret) {
992
675k
        ASN1_TYPE_free(typ);
993
675k
        if (opval)
994
35.9k
            *opval = NULL;
995
675k
    }
996
122M
    return ret;
997
122M
}
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.45M
{
1008
1.45M
    uint32_t expected_eoc;
1009
1.45M
    long plen;
1010
1.45M
    const unsigned char *p = *in, *q;
1011
    /* If not indefinite length constructed just add length */
1012
1.45M
    if (inf == 0) {
1013
0
        *in += len;
1014
0
        return 1;
1015
0
    }
1016
1.45M
    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
416M
    while (len > 0) {
1024
416M
        if (asn1_check_eoc(&p, len)) {
1025
40.2M
            expected_eoc--;
1026
40.2M
            if (expected_eoc == 0)
1027
1.33M
                break;
1028
38.8M
            len -= 2;
1029
38.8M
            continue;
1030
40.2M
        }
1031
376M
        q = p;
1032
        /* Just read in a header: only care about the length */
1033
376M
        if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1034
376M
                -1, 0, 0, NULL)) {
1035
76.6k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1036
76.6k
            return 0;
1037
76.6k
        }
1038
376M
        if (inf) {
1039
97.8M
            if (expected_eoc == UINT32_MAX) {
1040
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1041
0
                return 0;
1042
0
            }
1043
97.8M
            expected_eoc++;
1044
278M
        } else {
1045
278M
            p += plen;
1046
278M
        }
1047
376M
        len -= p - q;
1048
376M
    }
1049
1.38M
    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.33M
    *in = p;
1054
1.33M
    return 1;
1055
1.38M
}
1056
1057
/*
1058
 * This function collects the asn1 data from a constructed string type into
1059
 * a buffer. The values of 'in' and 'len' should refer to the contents of the
1060
 * constructed type and 'inf' should be set if it is indefinite length.
1061
 */
1062
1063
#ifndef ASN1_MAX_STRING_NEST
1064
/*
1065
 * This determines how many levels of recursion are permitted in ASN1 string
1066
 * types. If it is not limited stack overflows can occur. If set to zero no
1067
 * recursion is allowed at all. Although zero should be adequate examples
1068
 * exist that require a value of 1. So 5 should be more than enough.
1069
 */
1070
2.08M
#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
11.5M
{
1076
11.5M
    const unsigned char *p, *q;
1077
11.5M
    long plen;
1078
11.5M
    char cst, ininf;
1079
11.5M
    p = *in;
1080
11.5M
    inf &= 1;
1081
    /*
1082
     * If no buffer and not indefinite length constructed just pass over the
1083
     * encoded data
1084
     */
1085
11.5M
    if (!buf && !inf) {
1086
0
        *in += len;
1087
0
        return 1;
1088
0
    }
1089
46.7M
    while (len > 0) {
1090
36.4M
        q = p;
1091
        /* Check for EOC */
1092
36.4M
        if (asn1_check_eoc(&p, len)) {
1093
            /*
1094
             * EOC is illegal outside indefinite length constructed form
1095
             */
1096
1.13M
            if (!inf) {
1097
11.0k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
1098
11.0k
                return 0;
1099
11.0k
            }
1100
1.12M
            inf = 0;
1101
1.12M
            break;
1102
1.13M
        }
1103
1104
35.3M
        if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1105
35.3M
                len, tag, aclass, 0, NULL)) {
1106
38.4k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1107
38.4k
            return 0;
1108
38.4k
        }
1109
1110
        /* If indefinite length constructed update max length */
1111
35.2M
        if (cst) {
1112
2.08M
            if (depth >= ASN1_MAX_STRING_NEST) {
1113
2.34k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_ASN1_STRING);
1114
2.34k
                return 0;
1115
2.34k
            }
1116
2.08M
            if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1117
53.7k
                return 0;
1118
33.1M
        } else if (plen && !collect_data(buf, &p, plen))
1119
0
            return 0;
1120
35.2M
        len -= p - q;
1121
35.2M
    }
1122
11.4M
    if (inf) {
1123
16.7k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1124
16.7k
        return 0;
1125
16.7k
    }
1126
11.4M
    *in = p;
1127
11.4M
    return 1;
1128
11.4M
}
1129
1130
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1131
16.8M
{
1132
16.8M
    int len;
1133
16.8M
    if (buf) {
1134
16.8M
        len = buf->length;
1135
16.8M
        if (!BUF_MEM_grow_clean(buf, len + plen)) {
1136
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
1137
0
            return 0;
1138
0
        }
1139
16.8M
        memcpy(buf->data + len, *p, plen);
1140
16.8M
    }
1141
16.8M
    *p += plen;
1142
16.8M
    return 1;
1143
16.8M
}
1144
1145
/* Check for ASN1 EOC and swallow it if found */
1146
1147
static int asn1_check_eoc(const unsigned char **in, long len)
1148
841M
{
1149
841M
    const unsigned char *p;
1150
1151
841M
    if (len < 2)
1152
207k
        return 0;
1153
841M
    p = *in;
1154
841M
    if (p[0] == '\0' && p[1] == '\0') {
1155
64.3M
        *in += 2;
1156
64.3M
        return 1;
1157
64.3M
    }
1158
777M
    return 0;
1159
841M
}
1160
1161
/*
1162
 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1163
 * length for indefinite length constructed form, we don't know the exact
1164
 * length but we can set an upper bound to the amount of data available minus
1165
 * the header length just read.
1166
 */
1167
1168
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1169
    char *inf, char *cst,
1170
    const unsigned char **in, long len,
1171
    int exptag, int expclass, char opt, ASN1_TLC *ctx)
1172
901M
{
1173
901M
    int i;
1174
901M
    int ptag, pclass;
1175
901M
    long plen;
1176
901M
    const unsigned char *p, *q;
1177
901M
    p = *in;
1178
901M
    q = p;
1179
1180
901M
    if (len <= 0) {
1181
933
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
1182
933
        goto err;
1183
933
    }
1184
901M
    if (ctx != NULL && ctx->valid) {
1185
108M
        i = ctx->ret;
1186
108M
        plen = ctx->plen;
1187
108M
        pclass = ctx->pclass;
1188
108M
        ptag = ctx->ptag;
1189
108M
        p += ctx->hdrlen;
1190
793M
    } else {
1191
793M
        i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1192
793M
        if (ctx != NULL) {
1193
381M
            ctx->ret = i;
1194
381M
            ctx->plen = plen;
1195
381M
            ctx->pclass = pclass;
1196
381M
            ctx->ptag = ptag;
1197
381M
            ctx->hdrlen = p - q;
1198
381M
            ctx->valid = 1;
1199
            /*
1200
             * If definite length, and no error, length + header can't exceed
1201
             * total amount of data available.
1202
             */
1203
381M
            if ((i & 0x81) == 0 && (plen + ctx->hdrlen) > len) {
1204
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
1205
0
                goto err;
1206
0
            }
1207
381M
        }
1208
793M
    }
1209
1210
901M
    if ((i & 0x80) != 0) {
1211
1.09M
        ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER);
1212
1.09M
        goto err;
1213
1.09M
    }
1214
900M
    if (exptag >= 0) {
1215
402M
        if (exptag != ptag || expclass != pclass) {
1216
            /*
1217
             * If type is OPTIONAL, not an error: indicate missing type.
1218
             */
1219
40.2M
            if (opt != 0)
1220
27.9M
                return -1;
1221
40.2M
            ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG);
1222
12.2M
            goto err;
1223
40.2M
        }
1224
        /*
1225
         * We have a tag and class match: assume we are going to do something
1226
         * with it
1227
         */
1228
362M
        asn1_tlc_clear(ctx);
1229
362M
    }
1230
1231
860M
    if ((i & 1) != 0)
1232
134M
        plen = len - (p - q);
1233
1234
860M
    if (inf != NULL)
1235
778M
        *inf = i & 1;
1236
1237
860M
    if (cst != NULL)
1238
230M
        *cst = i & V_ASN1_CONSTRUCTED;
1239
1240
860M
    if (olen != NULL)
1241
778M
        *olen = plen;
1242
1243
860M
    if (oclass != NULL)
1244
81.8M
        *oclass = pclass;
1245
1246
860M
    if (otag != NULL)
1247
81.8M
        *otag = ptag;
1248
1249
860M
    *in = p;
1250
860M
    return 1;
1251
1252
13.3M
err:
1253
    asn1_tlc_clear(ctx);
1254
13.3M
    return 0;
1255
900M
}