Coverage Report

Created: 2025-12-31 06:58

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
363M
#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
41.4M
{
107
41.4M
    if ((tag < 0) || (tag > 30))
108
5.32k
        return 0;
109
41.4M
    return tag2bit[tag];
110
41.4M
}
111
112
/* Macro to initialize and invalidate the cache */
113
114
#define asn1_tlc_clear(c)   \
115
337M
    do {                    \
116
337M
        if ((c) != NULL)    \
117
337M
            (c)->valid = 0; \
118
337M
    } while (0)
119
/* Version to avoid compiler warning about 'c' always non-NULL */
120
#define asn1_tlc_clear_nc(c) \
121
24.0M
    do {                     \
122
24.0M
        (c)->valid = 0;      \
123
24.0M
    } while (0)
124
125
/*
126
 * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
127
 * function. 'in' points to a buffer to read the data from, in future we
128
 * will have more advanced versions that can input data a piece at a time and
129
 * this will simply be a special case.
130
 */
131
132
static int asn1_item_ex_d2i_intern(ASN1_VALUE **pval, const unsigned char **in,
133
    long len, const ASN1_ITEM *it, int tag,
134
    int aclass, char opt, ASN1_TLC *ctx,
135
    OSSL_LIB_CTX *libctx, const char *propq)
136
26.5M
{
137
26.5M
    int rv;
138
139
26.5M
    if (pval == NULL || it == NULL) {
140
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
141
0
        return 0;
142
0
    }
143
26.5M
    rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0,
144
26.5M
        libctx, propq);
145
26.5M
    if (rv <= 0)
146
16.9M
        ASN1_item_ex_free(pval, it);
147
26.5M
    return rv;
148
26.5M
}
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.52M
{
154
2.52M
    return asn1_item_ex_d2i_intern(pval, in, len, it, tag, aclass, opt, ctx,
155
2.52M
        NULL, NULL);
156
2.52M
}
157
158
ASN1_VALUE *ASN1_item_d2i_ex(ASN1_VALUE **pval,
159
    const unsigned char **in, long len,
160
    const ASN1_ITEM *it, OSSL_LIB_CTX *libctx,
161
    const char *propq)
162
24.0M
{
163
24.0M
    ASN1_TLC c;
164
24.0M
    ASN1_VALUE *ptmpval = NULL;
165
166
24.0M
    if (pval == NULL)
167
21.4M
        pval = &ptmpval;
168
24.0M
    asn1_tlc_clear_nc(&c);
169
24.0M
    if (asn1_item_ex_d2i_intern(pval, in, len, it, -1, 0, 0, &c, libctx,
170
24.0M
            propq)
171
24.0M
        > 0)
172
7.45M
        return *pval;
173
16.5M
    return NULL;
174
24.0M
}
175
176
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
177
    const unsigned char **in, long len,
178
    const ASN1_ITEM *it)
179
22.6M
{
180
22.6M
    return ASN1_item_d2i_ex(pval, in, len, it, NULL, NULL);
181
22.6M
}
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
363M
{
194
363M
    const ASN1_TEMPLATE *tt, *errtt = NULL;
195
363M
    const ASN1_EXTERN_FUNCS *ef;
196
363M
    const ASN1_AUX *aux;
197
363M
    ASN1_aux_cb *asn1_cb;
198
363M
    const unsigned char *p = NULL, *q;
199
363M
    unsigned char oclass;
200
363M
    char seq_eoc, seq_nolen, cst, isopt;
201
363M
    long tmplen;
202
363M
    int i;
203
363M
    int otag;
204
363M
    int ret = 0;
205
363M
    ASN1_VALUE **pchptr;
206
207
363M
    if (pval == NULL || it == NULL) {
208
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
209
0
        return 0;
210
0
    }
211
363M
    if (len <= 0) {
212
66.6k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
213
66.6k
        return 0;
214
66.6k
    }
215
363M
    aux = it->funcs;
216
363M
    if (aux && aux->asn1_cb)
217
12.6M
        asn1_cb = aux->asn1_cb;
218
350M
    else
219
350M
        asn1_cb = 0;
220
221
363M
    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
363M
    switch (it->itype) {
227
271M
    case ASN1_ITYPE_PRIMITIVE:
228
271M
        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
137M
            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
137M
            return asn1_template_ex_d2i(pval, in, len, it->templates, opt, ctx,
241
137M
                depth, libctx, propq);
242
137M
        }
243
134M
        return asn1_d2i_ex_primitive(pval, in, len, it,
244
134M
            tag, aclass, opt, ctx);
245
246
21.1M
    case ASN1_ITYPE_MSTRING:
247
        /*
248
         * It never makes sense for multi-strings to have implicit tagging, so
249
         * if tag != -1, then this looks like an error in the template.
250
         */
251
21.1M
        if (tag != -1) {
252
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
253
0
            goto err;
254
0
        }
255
256
21.1M
        p = *in;
257
        /* Just read in tag and class */
258
21.1M
        ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
259
21.1M
            &p, len, -1, 0, 1, ctx);
260
21.1M
        if (!ret) {
261
25.2k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
262
25.2k
            goto err;
263
25.2k
        }
264
265
        /* Must be UNIVERSAL class */
266
21.0M
        if (oclass != V_ASN1_UNIVERSAL) {
267
            /* If OPTIONAL, assume this is OK */
268
138k
            if (opt)
269
102k
                return -1;
270
138k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
271
35.5k
            goto err;
272
138k
        }
273
274
        /* Check tag matches bit map */
275
20.9M
        if (!(ASN1_tag2bit(otag) & it->utype)) {
276
            /* If OPTIONAL, assume this is OK */
277
424k
            if (opt)
278
13.5k
                return -1;
279
424k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_WRONG_TAG);
280
410k
            goto err;
281
424k
        }
282
20.5M
        return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
283
284
4.97M
    case ASN1_ITYPE_EXTERN:
285
        /* Use new style d2i */
286
4.97M
        ef = it->funcs;
287
4.97M
        if (ef->asn1_ex_d2i_ex != NULL)
288
2.45M
            return ef->asn1_ex_d2i_ex(pval, in, len, it, tag, aclass, opt, ctx,
289
2.45M
                libctx, propq);
290
2.52M
        return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
291
292
6.35M
    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.35M
        if (tag != -1) {
298
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
299
0
            goto err;
300
0
        }
301
302
6.35M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
303
0
            goto auxerr;
304
6.35M
        if (*pval) {
305
            /* Free up and zero CHOICE value if initialised */
306
778k
            i = ossl_asn1_get_choice_selector(pval, it);
307
778k
            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.57M
        } 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.35M
        p = *in;
319
21.3M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
320
20.6M
            pchptr = ossl_asn1_get_field_ptr(pval, tt);
321
            /*
322
             * We mark field as OPTIONAL so its absence can be recognised.
323
             */
324
20.6M
            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth,
325
20.6M
                libctx, propq);
326
            /* If field not present, try the next one */
327
20.6M
            if (ret == -1)
328
15.0M
                continue;
329
            /* If positive return, read OK, break loop */
330
5.64M
            if (ret > 0)
331
4.60M
                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.64M
        }
341
342
        /* Did we fall off the end without reading anything? */
343
5.31M
        if (i == it->tcount) {
344
            /* If OPTIONAL, this is OK */
345
708k
            if (opt) {
346
                /* Free and zero it */
347
25.2k
                ASN1_item_ex_free(pval, it);
348
25.2k
                return -1;
349
25.2k
            }
350
708k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
351
683k
            goto err;
352
708k
        }
353
354
4.60M
        ossl_asn1_set_choice_selector(pval, i, it);
355
356
4.60M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
357
0
            goto auxerr;
358
4.60M
        *in = p;
359
4.60M
        return 1;
360
361
842k
    case ASN1_ITYPE_NDEF_SEQUENCE:
362
59.3M
    case ASN1_ITYPE_SEQUENCE:
363
59.3M
        p = *in;
364
59.3M
        tmplen = len;
365
366
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
367
59.3M
        if (tag == -1) {
368
54.1M
            tag = V_ASN1_SEQUENCE;
369
54.1M
            aclass = V_ASN1_UNIVERSAL;
370
54.1M
        }
371
        /* Get SEQUENCE length and update len, p */
372
59.3M
        ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
373
59.3M
            &p, len, tag, aclass, opt, ctx);
374
59.3M
        if (!ret) {
375
4.62M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
376
4.62M
            goto err;
377
54.7M
        } else if (ret == -1)
378
5.65M
            return -1;
379
49.0M
        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.0M
        else
385
49.0M
            seq_nolen = seq_eoc;
386
49.0M
        if (!cst) {
387
120k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
388
120k
            goto err;
389
120k
        }
390
391
48.9M
        if (*pval == NULL
392
37.8M
            && !ossl_asn1_item_ex_new_intern(pval, it, libctx, propq)) {
393
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
394
0
            goto err;
395
0
        }
396
397
48.9M
        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
744k
                const ASN1_TEMPLATE *seqtt;
404
744k
                ASN1_VALUE **pseqval;
405
744k
                seqtt = ossl_asn1_do_adb(*pval, tt, 0);
406
744k
                if (seqtt == NULL)
407
617k
                    continue;
408
127k
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
409
127k
                ossl_asn1_template_free(pseqval, seqtt);
410
127k
            }
411
136M
        }
412
413
        /* Get each field entry */
414
141M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
415
108M
            const ASN1_TEMPLATE *seqtt;
416
108M
            ASN1_VALUE **pseqval;
417
108M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
418
108M
            if (seqtt == NULL)
419
0
                goto err;
420
108M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
421
            /* Have we ran out of data? */
422
108M
            if (!len)
423
2.64M
                break;
424
106M
            q = p;
425
106M
            if (asn1_check_eoc(&p, len)) {
426
3.19M
                if (!seq_eoc) {
427
35.5k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
428
35.5k
                    goto err;
429
35.5k
                }
430
3.16M
                len -= p - q;
431
3.16M
                seq_eoc = 0;
432
3.16M
                break;
433
3.19M
            }
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
102M
            if (i == (it->tcount - 1))
441
34.9M
                isopt = 0;
442
67.9M
            else
443
67.9M
                isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
444
            /*
445
             * attempt to read in field, allowing each to be OPTIONAL
446
             */
447
448
102M
            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
449
102M
                depth, libctx, propq);
450
102M
            if (!ret) {
451
9.90M
                errtt = seqtt;
452
9.90M
                goto err;
453
92.9M
            } else if (ret == -1) {
454
                /*
455
                 * OPTIONAL component absent. Free and zero the field.
456
                 */
457
9.83M
                ossl_asn1_template_free(pseqval, seqtt);
458
9.83M
                continue;
459
9.83M
            }
460
            /* Update length */
461
83.1M
            len -= p - q;
462
83.1M
        }
463
464
        /* Check for EOC if expecting one */
465
39.0M
        if (seq_eoc && !asn1_check_eoc(&p, len)) {
466
181k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
467
181k
            goto err;
468
181k
        }
469
        /* Check all data read */
470
38.8M
        if (!seq_nolen && len) {
471
35.8k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
472
35.8k
            goto err;
473
35.8k
        }
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
50.8M
        for (; i < it->tcount; tt++, i++) {
481
12.3M
            const ASN1_TEMPLATE *seqtt;
482
12.3M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
483
12.3M
            if (seqtt == NULL)
484
0
                goto err;
485
12.3M
            if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
486
12.0M
                ASN1_VALUE **pseqval;
487
12.0M
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
488
12.0M
                ossl_asn1_template_free(pseqval, seqtt);
489
12.0M
            } else {
490
283k
                errtt = seqtt;
491
283k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_FIELD_MISSING);
492
283k
                goto err;
493
283k
            }
494
12.3M
        }
495
        /* Save encoding */
496
38.5M
        if (!ossl_asn1_enc_save(pval, *in, p - *in, it))
497
0
            goto auxerr;
498
38.5M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
499
20.2k
            goto auxerr;
500
38.4M
        *in = p;
501
38.4M
        return 1;
502
503
0
    default:
504
0
        return 0;
505
363M
    }
506
20.2k
auxerr:
507
20.2k
    ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
508
17.4M
err:
509
17.4M
    if (errtt)
510
11.2M
        ERR_add_error_data(4, "Field=", errtt->field_name,
511
11.2M
            ", Type=", it->sname);
512
6.17M
    else
513
6.17M
        ERR_add_error_data(2, "Type=", it->sname);
514
17.4M
    return 0;
515
20.2k
}
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
260M
{
528
260M
    int flags, aclass;
529
260M
    int ret;
530
260M
    long len;
531
260M
    const unsigned char *p, *q;
532
260M
    char exp_eoc;
533
260M
    if (!val)
534
0
        return 0;
535
260M
    flags = tt->flags;
536
260M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
537
538
260M
    p = *in;
539
540
    /* Check if EXPLICIT tag expected */
541
260M
    if (flags & ASN1_TFLG_EXPTAG) {
542
7.04M
        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.04M
        ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
548
7.04M
            &p, inlen, tt->tag, aclass, opt, ctx);
549
7.04M
        q = p;
550
7.04M
        if (!ret) {
551
381k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
552
381k
            return 0;
553
6.66M
        } else if (ret == -1)
554
4.83M
            return -1;
555
1.82M
        if (!cst) {
556
6.21k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
557
6.21k
            return 0;
558
6.21k
        }
559
        /* We've found the field so it can't be OPTIONAL now */
560
1.82M
        ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth, libctx,
561
1.82M
            propq);
562
1.82M
        if (!ret) {
563
340k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
564
340k
            return 0;
565
340k
        }
566
        /* We read the field in OK so update length */
567
1.48M
        len -= p - q;
568
1.48M
        if (exp_eoc) {
569
            /* If NDEF we must have an EOC here */
570
448k
            if (!asn1_check_eoc(&p, len)) {
571
55.2k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
572
55.2k
                goto err;
573
55.2k
            }
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.68k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
580
4.68k
                goto err;
581
4.68k
            }
582
1.03M
        }
583
1.48M
    } else
584
253M
        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth,
585
253M
            libctx, propq);
586
587
1.42M
    *in = p;
588
1.42M
    return 1;
589
590
59.9k
err:
591
59.9k
    return 0;
592
260M
}
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
255M
{
600
255M
    int flags, aclass;
601
255M
    int ret;
602
255M
    ASN1_VALUE *tval;
603
255M
    const unsigned char *p, *q;
604
255M
    if (!val)
605
0
        return 0;
606
255M
    flags = tt->flags;
607
255M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
608
609
255M
    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
255M
    if (tt->flags & ASN1_TFLG_EMBED) {
616
12.2M
        tval = (ASN1_VALUE *)val;
617
12.2M
        val = &tval;
618
12.2M
    }
619
620
255M
    if (flags & ASN1_TFLG_SK_MASK) {
621
        /* SET OF, SEQUENCE OF */
622
140M
        int sktag, skaclass;
623
140M
        char sk_eoc;
624
        /* First work out expected inner tag value */
625
140M
        if (flags & ASN1_TFLG_IMPTAG) {
626
1.48M
            sktag = tt->tag;
627
1.48M
            skaclass = aclass;
628
139M
        } else {
629
139M
            skaclass = V_ASN1_UNIVERSAL;
630
139M
            if (flags & ASN1_TFLG_SET_OF)
631
132M
                sktag = V_ASN1_SET;
632
6.78M
            else
633
6.78M
                sktag = V_ASN1_SEQUENCE;
634
139M
        }
635
        /* Get the tag */
636
140M
        ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
637
140M
            &p, len, sktag, skaclass, opt, ctx);
638
140M
        if (!ret) {
639
1.40M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
640
1.40M
            return 0;
641
139M
        } else if (ret == -1)
642
832k
            return -1;
643
138M
        if (*val == NULL)
644
137M
            *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
645
581k
        else {
646
            /*
647
             * We've got a valid STACK: free up any items present
648
             */
649
581k
            STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
650
581k
            ASN1_VALUE *vtmp;
651
581k
            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
581k
        }
656
657
138M
        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
356M
        while (len > 0) {
664
225M
            ASN1_VALUE *skfield;
665
225M
            q = p;
666
            /* See if EOC found */
667
225M
            if (asn1_check_eoc(&p, len)) {
668
5.83M
                if (!sk_eoc) {
669
11.5k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
670
11.5k
                    goto err;
671
11.5k
                }
672
5.82M
                len -= p - q;
673
5.82M
                sk_eoc = 0;
674
5.82M
                break;
675
5.83M
            }
676
219M
            skfield = NULL;
677
219M
            if (asn1_item_embed_d2i(&skfield, &p, len,
678
219M
                    ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
679
219M
                    depth, libctx, propq)
680
219M
                <= 0) {
681
2.05M
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
682
                /* |skfield| may be partially allocated despite failure. */
683
2.05M
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
684
2.05M
                goto err;
685
2.05M
            }
686
217M
            len -= p - q;
687
217M
            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
217M
        }
693
136M
        if (sk_eoc) {
694
14.3k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
695
14.3k
            goto err;
696
14.3k
        }
697
136M
    } else if (flags & ASN1_TFLG_IMPTAG) {
698
        /* IMPLICIT tagging */
699
17.1M
        ret = asn1_item_embed_d2i(val, &p, len,
700
17.1M
            ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
701
17.1M
            ctx, depth, libctx, propq);
702
17.1M
        if (!ret) {
703
548k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
704
548k
            goto err;
705
16.6M
        } else if (ret == -1)
706
12.8M
            return -1;
707
97.4M
    } else {
708
        /* Nothing special */
709
97.4M
        ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
710
97.4M
            -1, 0, opt, ctx, depth, libctx, propq);
711
97.4M
        if (!ret) {
712
8.71M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
713
8.71M
            goto err;
714
88.6M
        } else if (ret == -1)
715
6.31M
            return -1;
716
97.4M
    }
717
718
222M
    *in = p;
719
222M
    return 1;
720
721
11.3M
err:
722
11.3M
    return 0;
723
255M
}
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
154M
{
730
154M
    int ret = 0, utype;
731
154M
    long plen;
732
154M
    char cst, inf, free_cont = 0;
733
154M
    const unsigned char *p;
734
154M
    BUF_MEM buf = { 0, NULL, 0, 0 };
735
154M
    const unsigned char *cont = NULL;
736
154M
    long len;
737
738
154M
    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
154M
    if (it->itype == ASN1_ITYPE_MSTRING) {
744
20.5M
        utype = tag;
745
20.5M
        tag = -1;
746
20.5M
    } else
747
134M
        utype = it->utype;
748
749
154M
    if (utype == V_ASN1_ANY) {
750
        /* If type is ANY need to figure out type from tag */
751
58.5M
        unsigned char oclass;
752
58.5M
        if (tag >= 0) {
753
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
754
0
            return 0;
755
0
        }
756
58.5M
        if (opt) {
757
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
758
0
            return 0;
759
0
        }
760
58.5M
        p = *in;
761
58.5M
        ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
762
58.5M
            &p, inlen, -1, 0, 0, ctx);
763
58.5M
        if (!ret) {
764
45.3k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
765
45.3k
            return 0;
766
45.3k
        }
767
58.4M
        if (oclass != V_ASN1_UNIVERSAL)
768
3.13M
            utype = V_ASN1_OTHER;
769
58.4M
    }
770
154M
    if (tag == -1) {
771
142M
        tag = utype;
772
142M
        aclass = V_ASN1_UNIVERSAL;
773
142M
    }
774
154M
    p = *in;
775
    /* Check header */
776
154M
    ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
777
154M
        &p, inlen, tag, aclass, opt, ctx);
778
154M
    if (!ret) {
779
7.00M
        ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
780
7.00M
        return 0;
781
147M
    } else if (ret == -1)
782
13.3M
        return -1;
783
134M
    ret = 0;
784
    /* SEQUENCE, SET and "OTHER" are left in encoded form */
785
134M
    if ((utype == V_ASN1_SEQUENCE)
786
130M
        || (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.4M
        if (utype == V_ASN1_OTHER) {
792
3.13M
            asn1_tlc_clear(ctx);
793
3.13M
        }
794
        /* SEQUENCE and SET must be constructed */
795
22.2M
        else if (!cst) {
796
15.5k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
797
15.5k
            return 0;
798
15.5k
        }
799
800
25.4M
        cont = *in;
801
        /* If indefinite length constructed find the real end */
802
25.4M
        if (inf) {
803
1.21M
            if (!asn1_find_end(&p, plen, inf))
804
114k
                goto err;
805
1.10M
            len = p - cont;
806
24.1M
        } else {
807
24.1M
            len = p - cont + plen;
808
24.1M
            p += plen;
809
24.1M
        }
810
109M
    } else if (cst) {
811
7.10M
        if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
812
7.09M
            || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
813
7.07M
            || utype == V_ASN1_ENUMERATED) {
814
27.1k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
815
27.1k
            return 0;
816
27.1k
        }
817
818
        /* Free any returned 'buf' content */
819
7.07M
        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
7.07M
        if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
827
64.9k
            goto err;
828
64.9k
        }
829
7.00M
        len = buf.length;
830
        /* Append a final null to string */
831
7.00M
        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
7.00M
        buf.data[len] = 0;
836
7.00M
        cont = (const unsigned char *)buf.data;
837
102M
    } else {
838
102M
        cont = p;
839
102M
        len = plen;
840
102M
        p += plen;
841
102M
    }
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
134M
    if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
846
878k
        goto err;
847
848
133M
    *in = p;
849
133M
    ret = 1;
850
134M
err:
851
134M
    if (free_cont)
852
401k
        OPENSSL_free(buf.data);
853
134M
    return ret;
854
133M
}
855
856
/* Translate ASN1 content octets into a structure */
857
858
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
859
    int utype, char *free_cont, const ASN1_ITEM *it)
860
120M
{
861
120M
    ASN1_VALUE **opval = NULL;
862
120M
    ASN1_STRING *stmp;
863
120M
    ASN1_TYPE *typ = NULL;
864
120M
    int ret = 0;
865
120M
    const ASN1_PRIMITIVE_FUNCS *pf;
866
120M
    ASN1_INTEGER **tint;
867
120M
    pf = it->funcs;
868
869
120M
    if (pf && pf->prim_c2i)
870
2.95M
        return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
871
    /* If ANY type clear type and set pointer to internal value */
872
117M
    if (it->utype == V_ASN1_ANY) {
873
56.1M
        if (*pval == NULL) {
874
56.0M
            typ = ASN1_TYPE_new();
875
56.0M
            if (typ == NULL)
876
0
                goto err;
877
56.0M
            *pval = (ASN1_VALUE *)typ;
878
56.0M
        } else
879
116k
            typ = (ASN1_TYPE *)*pval;
880
881
56.1M
        if (utype != typ->type)
882
56.1M
            ASN1_TYPE_set(typ, utype, NULL);
883
56.1M
        opval = pval;
884
56.1M
        pval = &typ->value.asn1_value;
885
56.1M
    }
886
117M
    switch (utype) {
887
26.4M
    case V_ASN1_OBJECT:
888
26.4M
        if (!ossl_c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
889
50.3k
            goto err;
890
26.3M
        break;
891
892
26.3M
    case V_ASN1_NULL:
893
825k
        if (len) {
894
5.98k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
895
5.98k
            goto err;
896
5.98k
        }
897
819k
        *pval = (ASN1_VALUE *)1;
898
819k
        break;
899
900
12.1M
    case V_ASN1_BOOLEAN:
901
12.1M
        if (len != 1) {
902
7.56k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
903
7.56k
            goto err;
904
12.1M
        } else {
905
12.1M
            ASN1_BOOLEAN *tbool;
906
12.1M
            tbool = (ASN1_BOOLEAN *)pval;
907
12.1M
            *tbool = *cont;
908
12.1M
        }
909
12.1M
        break;
910
911
12.1M
    case V_ASN1_BIT_STRING:
912
6.61M
        if (!ossl_c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
913
51.3k
            goto err;
914
6.56M
        break;
915
916
8.23M
    case V_ASN1_INTEGER:
917
11.1M
    case V_ASN1_ENUMERATED:
918
11.1M
        tint = (ASN1_INTEGER **)pval;
919
11.1M
        if (!ossl_c2i_ASN1_INTEGER(tint, &cont, len))
920
547k
            goto err;
921
        /* Fixup type to match the expected form */
922
10.5M
        (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
923
10.5M
        break;
924
925
18.1M
    case V_ASN1_OCTET_STRING:
926
19.5M
    case V_ASN1_NUMERICSTRING:
927
22.6M
    case V_ASN1_PRINTABLESTRING:
928
23.0M
    case V_ASN1_T61STRING:
929
23.0M
    case V_ASN1_VIDEOTEXSTRING:
930
25.6M
    case V_ASN1_IA5STRING:
931
27.4M
    case V_ASN1_UTCTIME:
932
27.6M
    case V_ASN1_GENERALIZEDTIME:
933
27.9M
    case V_ASN1_GRAPHICSTRING:
934
28.0M
    case V_ASN1_VISIBLESTRING:
935
28.0M
    case V_ASN1_GENERALSTRING:
936
28.5M
    case V_ASN1_UNIVERSALSTRING:
937
29.1M
    case V_ASN1_BMPSTRING:
938
31.0M
    case V_ASN1_UTF8STRING:
939
34.0M
    case V_ASN1_OTHER:
940
51.5M
    case V_ASN1_SET:
941
54.8M
    case V_ASN1_SEQUENCE:
942
59.9M
    default:
943
59.9M
        if (utype == V_ASN1_BMPSTRING && (len & 1)) {
944
1.91k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
945
1.91k
            goto err;
946
1.91k
        }
947
59.9M
        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
59.9M
        if (utype == V_ASN1_GENERALIZEDTIME && (len < 15)) {
952
5.63k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_GENERALIZEDTIME_IS_TOO_SHORT);
953
5.63k
            goto err;
954
5.63k
        }
955
59.9M
        if (utype == V_ASN1_UTCTIME && (len < 13)) {
956
3.02k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UTCTIME_IS_TOO_SHORT);
957
3.02k
            goto err;
958
3.02k
        }
959
        /* All based on ASN1_STRING and handled the same */
960
59.9M
        if (*pval == NULL) {
961
41.2M
            stmp = ASN1_STRING_type_new(utype);
962
41.2M
            if (stmp == NULL) {
963
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
964
0
                goto err;
965
0
            }
966
41.2M
            *pval = (ASN1_VALUE *)stmp;
967
41.2M
        } else {
968
18.7M
            stmp = (ASN1_STRING *)*pval;
969
18.7M
            stmp->type = utype;
970
18.7M
        }
971
        /* If we've already allocated a buffer use it */
972
59.9M
        if (*free_cont) {
973
5.27M
            ASN1_STRING_set0(stmp, (unsigned char *)cont /* UGLY CAST! */, len);
974
5.27M
            *free_cont = 0;
975
54.6M
        } else {
976
54.6M
            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
54.6M
        }
983
59.9M
        break;
984
117M
    }
985
    /* If ASN1_ANY and NULL type fix up value */
986
116M
    if (typ && (utype == V_ASN1_NULL))
987
804k
        typ->value.ptr = NULL;
988
989
116M
    ret = 1;
990
117M
err:
991
117M
    if (!ret) {
992
675k
        ASN1_TYPE_free(typ);
993
675k
        if (opval)
994
37.9k
            *opval = NULL;
995
675k
    }
996
117M
    return ret;
997
116M
}
998
999
/*
1000
 * This function finds the end of an ASN1 structure when passed its maximum
1001
 * length, whether it is indefinite length and a pointer to the content. This
1002
 * is more efficient than calling asn1_collect because it does not recurse on
1003
 * each indefinite length header.
1004
 */
1005
1006
static int asn1_find_end(const unsigned char **in, long len, char inf)
1007
1.21M
{
1008
1.21M
    uint32_t expected_eoc;
1009
1.21M
    long plen;
1010
1.21M
    const unsigned char *p = *in, *q;
1011
    /* If not indefinite length constructed just add length */
1012
1.21M
    if (inf == 0) {
1013
0
        *in += len;
1014
0
        return 1;
1015
0
    }
1016
1.21M
    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
414M
    while (len > 0) {
1024
414M
        if (asn1_check_eoc(&p, len)) {
1025
40.6M
            expected_eoc--;
1026
40.6M
            if (expected_eoc == 0)
1027
1.10M
                break;
1028
39.5M
            len -= 2;
1029
39.5M
            continue;
1030
40.6M
        }
1031
373M
        q = p;
1032
        /* Just read in a header: only care about the length */
1033
373M
        if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1034
373M
                -1, 0, 0, NULL)) {
1035
74.5k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1036
74.5k
            return 0;
1037
74.5k
        }
1038
373M
        if (inf) {
1039
85.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
85.8M
            expected_eoc++;
1044
287M
        } else {
1045
287M
            p += plen;
1046
287M
        }
1047
373M
        len -= p - q;
1048
373M
    }
1049
1.14M
    if (expected_eoc) {
1050
39.5k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1051
39.5k
        return 0;
1052
39.5k
    }
1053
1.10M
    *in = p;
1054
1.10M
    return 1;
1055
1.14M
}
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.42M
#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
9.49M
{
1076
9.49M
    const unsigned char *p, *q;
1077
9.49M
    long plen;
1078
9.49M
    char cst, ininf;
1079
9.49M
    p = *in;
1080
9.49M
    inf &= 1;
1081
    /*
1082
     * If no buffer and not indefinite length constructed just pass over the
1083
     * encoded data
1084
     */
1085
9.49M
    if (!buf && !inf) {
1086
0
        *in += len;
1087
0
        return 1;
1088
0
    }
1089
49.4M
    while (len > 0) {
1090
41.3M
        q = p;
1091
        /* Check for EOC */
1092
41.3M
        if (asn1_check_eoc(&p, len)) {
1093
            /*
1094
             * EOC is illegal outside indefinite length constructed form
1095
             */
1096
1.29M
            if (!inf) {
1097
11.3k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
1098
11.3k
                return 0;
1099
11.3k
            }
1100
1.28M
            inf = 0;
1101
1.28M
            break;
1102
1.29M
        }
1103
1104
40.0M
        if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1105
40.0M
                len, tag, aclass, 0, NULL)) {
1106
36.7k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1107
36.7k
            return 0;
1108
36.7k
        }
1109
1110
        /* If indefinite length constructed update max length */
1111
39.9M
        if (cst) {
1112
2.42M
            if (depth >= ASN1_MAX_STRING_NEST) {
1113
2.47k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_ASN1_STRING);
1114
2.47k
                return 0;
1115
2.47k
            }
1116
2.42M
            if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1117
49.9k
                return 0;
1118
37.5M
        } else if (plen && !collect_data(buf, &p, plen))
1119
0
            return 0;
1120
39.9M
        len -= p - q;
1121
39.9M
    }
1122
9.39M
    if (inf) {
1123
14.4k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1124
14.4k
        return 0;
1125
14.4k
    }
1126
9.38M
    *in = p;
1127
9.38M
    return 1;
1128
9.39M
}
1129
1130
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1131
21.1M
{
1132
21.1M
    int len;
1133
21.1M
    if (buf) {
1134
21.1M
        len = buf->length;
1135
21.1M
        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
21.1M
        memcpy(buf->data + len, *p, plen);
1140
21.1M
    }
1141
21.1M
    *p += plen;
1142
21.1M
    return 1;
1143
21.1M
}
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
798M
{
1149
798M
    const unsigned char *p;
1150
1151
798M
    if (len < 2)
1152
195k
        return 0;
1153
798M
    p = *in;
1154
798M
    if (p[0] == '\0' && p[1] == '\0') {
1155
62.3M
        *in += 2;
1156
62.3M
        return 1;
1157
62.3M
    }
1158
736M
    return 0;
1159
798M
}
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
855M
{
1173
855M
    int i;
1174
855M
    int ptag, pclass;
1175
855M
    long plen;
1176
855M
    const unsigned char *p, *q;
1177
855M
    p = *in;
1178
855M
    q = p;
1179
1180
855M
    if (len <= 0) {
1181
432
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
1182
432
        goto err;
1183
432
    }
1184
855M
    if (ctx != NULL && ctx->valid) {
1185
103M
        i = ctx->ret;
1186
103M
        plen = ctx->plen;
1187
103M
        pclass = ctx->pclass;
1188
103M
        ptag = ctx->ptag;
1189
103M
        p += ctx->hdrlen;
1190
752M
    } else {
1191
752M
        i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1192
752M
        if (ctx != NULL) {
1193
338M
            ctx->ret = i;
1194
338M
            ctx->plen = plen;
1195
338M
            ctx->pclass = pclass;
1196
338M
            ctx->ptag = ptag;
1197
338M
            ctx->hdrlen = p - q;
1198
338M
            ctx->valid = 1;
1199
            /*
1200
             * If definite length, and no error, length + header can't exceed
1201
             * total amount of data available.
1202
             */
1203
338M
            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
338M
        }
1208
752M
    }
1209
1210
855M
    if ((i & 0x80) != 0) {
1211
1.25M
        ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER);
1212
1.25M
        goto err;
1213
1.25M
    }
1214
854M
    if (exptag >= 0) {
1215
357M
        if (exptag != ptag || expclass != pclass) {
1216
            /*
1217
             * If type is OPTIONAL, not an error: indicate missing type.
1218
             */
1219
37.0M
            if (opt != 0)
1220
24.6M
                return -1;
1221
37.0M
            ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG);
1222
12.3M
            goto err;
1223
37.0M
        }
1224
        /*
1225
         * We have a tag and class match: assume we are going to do something
1226
         * with it
1227
         */
1228
320M
        asn1_tlc_clear(ctx);
1229
320M
    }
1230
1231
817M
    if ((i & 1) != 0)
1232
119M
        plen = len - (p - q);
1233
1234
817M
    if (inf != NULL)
1235
737M
        *inf = i & 1;
1236
1237
817M
    if (cst != NULL)
1238
225M
        *cst = i & V_ASN1_CONSTRUCTED;
1239
1240
817M
    if (olen != NULL)
1241
737M
        *olen = plen;
1242
1243
817M
    if (oclass != NULL)
1244
79.5M
        *oclass = pclass;
1245
1246
817M
    if (otag != NULL)
1247
79.5M
        *otag = ptag;
1248
1249
817M
    *in = p;
1250
817M
    return 1;
1251
1252
13.6M
err:
1253
    asn1_tlc_clear(ctx);
1254
13.6M
    return 0;
1255
854M
}