Coverage Report

Created: 2025-08-11 07:04

/src/openssl30/crypto/asn1/tasn_dec.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2000-2021 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 "internal/numbers.h"
18
#include "asn1_local.h"
19
20
/*
21
 * Constructed types with a recursive definition (such as can be found in PKCS7)
22
 * could eventually exceed the stack given malicious input with excessive
23
 * recursion. Therefore we limit the stack depth. This is the maximum number of
24
 * recursive invocations of asn1_item_embed_d2i().
25
 */
26
319M
#define ASN1_MAX_CONSTRUCTED_NEST 30
27
28
static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
29
                               long len, const ASN1_ITEM *it,
30
                               int tag, int aclass, char opt, ASN1_TLC *ctx,
31
                               int depth, OSSL_LIB_CTX *libctx,
32
                               const char *propq);
33
34
static int asn1_check_eoc(const unsigned char **in, long len);
35
static int asn1_find_end(const unsigned char **in, long len, char inf);
36
37
static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
38
                        char inf, int tag, int aclass, int depth);
39
40
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
41
42
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
43
                           char *inf, char *cst,
44
                           const unsigned char **in, long len,
45
                           int exptag, int expclass, char opt, ASN1_TLC *ctx);
46
47
static int asn1_template_ex_d2i(ASN1_VALUE **pval,
48
                                const unsigned char **in, long len,
49
                                const ASN1_TEMPLATE *tt, char opt,
50
                                ASN1_TLC *ctx, int depth, OSSL_LIB_CTX *libctx,
51
                                const char *propq);
52
static int asn1_template_noexp_d2i(ASN1_VALUE **val,
53
                                   const unsigned char **in, long len,
54
                                   const ASN1_TEMPLATE *tt, char opt,
55
                                   ASN1_TLC *ctx, int depth,
56
                                   OSSL_LIB_CTX *libctx, const char *propq);
57
static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
58
                                 const unsigned char **in, long len,
59
                                 const ASN1_ITEM *it,
60
                                 int tag, int aclass, char opt,
61
                                 ASN1_TLC *ctx);
62
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
63
                       int utype, char *free_cont, const ASN1_ITEM *it);
64
65
/* Table to convert tags to bit values, used for MSTRING type */
66
static const unsigned long tag2bit[32] = {
67
    /* tags  0 -  3 */
68
    0, 0, 0, B_ASN1_BIT_STRING,
69
    /* tags  4- 7 */
70
    B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,
71
    /* tags  8-11 */
72
    B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, 0, B_ASN1_UNKNOWN,
73
    /* tags 12-15 */
74
    B_ASN1_UTF8STRING, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,
75
    /* tags 16-19 */
76
    B_ASN1_SEQUENCE, 0, B_ASN1_NUMERICSTRING, B_ASN1_PRINTABLESTRING,
77
    /* tags 20-22 */
78
    B_ASN1_T61STRING, B_ASN1_VIDEOTEXSTRING, B_ASN1_IA5STRING,
79
    /* tags 23-24 */
80
    B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME,
81
    /* tags 25-27 */
82
    B_ASN1_GRAPHICSTRING, B_ASN1_ISO64STRING, B_ASN1_GENERALSTRING,
83
    /* tags 28-31 */
84
    B_ASN1_UNIVERSALSTRING, B_ASN1_UNKNOWN, B_ASN1_BMPSTRING, B_ASN1_UNKNOWN,
85
};
86
87
unsigned long ASN1_tag2bit(int tag)
88
33.3M
{
89
33.3M
    if ((tag < 0) || (tag > 30))
90
5.29k
        return 0;
91
33.3M
    return tag2bit[tag];
92
33.3M
}
93
94
/* Macro to initialize and invalidate the cache */
95
96
297M
#define asn1_tlc_clear(c)       do { if ((c) != NULL) (c)->valid = 0; } while (0)
97
/* Version to avoid compiler warning about 'c' always non-NULL */
98
20.6M
#define asn1_tlc_clear_nc(c)    do {(c)->valid = 0; } while (0)
99
100
/*
101
 * Decode an ASN1 item, this currently behaves just like a standard 'd2i'
102
 * function. 'in' points to a buffer to read the data from, in future we
103
 * will have more advanced versions that can input data a piece at a time and
104
 * this will simply be a special case.
105
 */
106
107
static int asn1_item_ex_d2i_intern(ASN1_VALUE **pval, const unsigned char **in,
108
                                   long len, const ASN1_ITEM *it, int tag,
109
                                   int aclass, char opt, ASN1_TLC *ctx,
110
                                   OSSL_LIB_CTX *libctx, const char *propq)
111
24.5M
{
112
24.5M
    int rv;
113
114
24.5M
    if (pval == NULL || it == NULL) {
115
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
116
0
        return 0;
117
0
    }
118
24.5M
    rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0,
119
24.5M
                             libctx, propq);
120
24.5M
    if (rv <= 0)
121
15.3M
        ASN1_item_ex_free(pval, it);
122
24.5M
    return rv;
123
24.5M
}
124
125
int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
126
                     const ASN1_ITEM *it,
127
                     int tag, int aclass, char opt, ASN1_TLC *ctx)
128
3.90M
{
129
3.90M
    return asn1_item_ex_d2i_intern(pval, in, len, it, tag, aclass, opt, ctx,
130
3.90M
                                   NULL, NULL);
131
3.90M
}
132
133
ASN1_VALUE *ASN1_item_d2i_ex(ASN1_VALUE **pval,
134
                             const unsigned char **in, long len,
135
                             const ASN1_ITEM *it, OSSL_LIB_CTX *libctx,
136
                             const char *propq)
137
20.6M
{
138
20.6M
    ASN1_TLC c;
139
20.6M
    ASN1_VALUE *ptmpval = NULL;
140
141
20.6M
    if (pval == NULL)
142
18.6M
        pval = &ptmpval;
143
20.6M
    asn1_tlc_clear_nc(&c);
144
20.6M
    if (asn1_item_ex_d2i_intern(pval, in, len, it, -1, 0, 0, &c, libctx,
145
20.6M
                                propq) > 0)
146
5.94M
        return *pval;
147
14.7M
    return NULL;
148
20.6M
}
149
150
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
151
                          const unsigned char **in, long len,
152
                          const ASN1_ITEM *it)
153
19.5M
{
154
19.5M
    return ASN1_item_d2i_ex(pval, in, len, it, NULL, NULL);
155
19.5M
}
156
157
/*
158
 * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
159
 * tag mismatch return -1 to handle OPTIONAL
160
 */
161
162
static int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in,
163
                               long len, const ASN1_ITEM *it,
164
                               int tag, int aclass, char opt, ASN1_TLC *ctx,
165
                               int depth, OSSL_LIB_CTX *libctx,
166
                               const char *propq)
167
320M
{
168
320M
    const ASN1_TEMPLATE *tt, *errtt = NULL;
169
320M
    const ASN1_EXTERN_FUNCS *ef;
170
320M
    const ASN1_AUX *aux;
171
320M
    ASN1_aux_cb *asn1_cb;
172
320M
    const unsigned char *p = NULL, *q;
173
320M
    unsigned char oclass;
174
320M
    char seq_eoc, seq_nolen, cst, isopt;
175
320M
    long tmplen;
176
320M
    int i;
177
320M
    int otag;
178
320M
    int ret = 0;
179
320M
    ASN1_VALUE **pchptr;
180
181
320M
    if (pval == NULL || it == NULL) {
182
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
183
0
        return 0;
184
0
    }
185
320M
    if (len <= 0) {
186
62.0k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
187
62.0k
        return 0;
188
62.0k
    }
189
319M
    aux = it->funcs;
190
319M
    if (aux && aux->asn1_cb)
191
11.4M
        asn1_cb = aux->asn1_cb;
192
308M
    else
193
308M
        asn1_cb = 0;
194
195
319M
    if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
196
14
        ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_TOO_DEEP);
197
14
        goto err;
198
14
    }
199
200
319M
    switch (it->itype) {
201
245M
    case ASN1_ITYPE_PRIMITIVE:
202
245M
        if (it->templates) {
203
            /*
204
             * tagging or OPTIONAL is currently illegal on an item template
205
             * because the flags can't get passed down. In practice this
206
             * isn't a problem: we include the relevant flags from the item
207
             * template in the template itself.
208
             */
209
135M
            if ((tag != -1) || opt) {
210
0
                ERR_raise(ERR_LIB_ASN1,
211
0
                          ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
212
0
                goto err;
213
0
            }
214
135M
            return asn1_template_ex_d2i(pval, in, len, it->templates, opt, ctx,
215
135M
                                        depth, libctx, propq);
216
135M
        }
217
110M
        return asn1_d2i_ex_primitive(pval, in, len, it,
218
110M
                                     tag, aclass, opt, ctx);
219
220
16.7M
    case ASN1_ITYPE_MSTRING:
221
        /*
222
         * It never makes sense for multi-strings to have implicit tagging, so
223
         * if tag != -1, then this looks like an error in the template.
224
         */
225
16.7M
        if (tag != -1) {
226
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
227
0
            goto err;
228
0
        }
229
230
16.7M
        p = *in;
231
        /* Just read in tag and class */
232
16.7M
        ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
233
16.7M
                              &p, len, -1, 0, 1, ctx);
234
16.7M
        if (!ret) {
235
19.5k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
236
19.5k
            goto err;
237
19.5k
        }
238
239
        /* Must be UNIVERSAL class */
240
16.7M
        if (oclass != V_ASN1_UNIVERSAL) {
241
            /* If OPTIONAL, assume this is OK */
242
117k
            if (opt)
243
86.1k
                return -1;
244
117k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
245
31.5k
            goto err;
246
117k
        }
247
248
        /* Check tag matches bit map */
249
16.6M
        if (!(ASN1_tag2bit(otag) & it->utype)) {
250
            /* If OPTIONAL, assume this is OK */
251
382k
            if (opt)
252
11.9k
                return -1;
253
382k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_WRONG_TAG);
254
370k
            goto err;
255
382k
        }
256
16.2M
        return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
257
258
3.90M
    case ASN1_ITYPE_EXTERN:
259
        /* Use new style d2i */
260
3.90M
        ef = it->funcs;
261
3.90M
        if (ef->asn1_ex_d2i_ex != NULL)
262
1.94M
            return ef->asn1_ex_d2i_ex(pval, in, len, it, tag, aclass, opt, ctx,
263
1.94M
                                      libctx, propq);
264
1.95M
        return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
265
266
5.34M
    case ASN1_ITYPE_CHOICE:
267
        /*
268
         * It never makes sense for CHOICE types to have implicit tagging, so
269
         * if tag != -1, then this looks like an error in the template.
270
         */
271
5.34M
        if (tag != -1) {
272
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
273
0
            goto err;
274
0
        }
275
276
5.34M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
277
0
            goto auxerr;
278
5.34M
        if (*pval) {
279
            /* Free up and zero CHOICE value if initialised */
280
719k
            i = ossl_asn1_get_choice_selector(pval, it);
281
719k
            if ((i >= 0) && (i < it->tcount)) {
282
0
                tt = it->templates + i;
283
0
                pchptr = ossl_asn1_get_field_ptr(pval, tt);
284
0
                ossl_asn1_template_free(pchptr, tt);
285
0
                ossl_asn1_set_choice_selector(pval, -1, it);
286
0
            }
287
4.62M
        } else if (!ossl_asn1_item_ex_new_intern(pval, it, libctx, propq)) {
288
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
289
0
            goto err;
290
0
        }
291
        /* CHOICE type, try each possibility in turn */
292
5.34M
        p = *in;
293
19.5M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
294
18.8M
            pchptr = ossl_asn1_get_field_ptr(pval, tt);
295
            /*
296
             * We mark field as OPTIONAL so its absence can be recognised.
297
             */
298
18.8M
            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth,
299
18.8M
                                       libctx, propq);
300
            /* If field not present, try the next one */
301
18.8M
            if (ret == -1)
302
14.1M
                continue;
303
            /* If positive return, read OK, break loop */
304
4.70M
            if (ret > 0)
305
3.82M
                break;
306
            /*
307
             * Must be an ASN1 parsing error.
308
             * Free up any partial choice value
309
             */
310
878k
            ossl_asn1_template_free(pchptr, tt);
311
878k
            errtt = tt;
312
878k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
313
878k
            goto err;
314
4.70M
        }
315
316
        /* Did we fall off the end without reading anything? */
317
4.46M
        if (i == it->tcount) {
318
            /* If OPTIONAL, this is OK */
319
640k
            if (opt) {
320
                /* Free and zero it */
321
22.2k
                ASN1_item_ex_free(pval, it);
322
22.2k
                return -1;
323
22.2k
            }
324
640k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
325
618k
            goto err;
326
640k
        }
327
328
3.82M
        ossl_asn1_set_choice_selector(pval, i, it);
329
330
3.82M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
331
0
            goto auxerr;
332
3.82M
        *in = p;
333
3.82M
        return 1;
334
335
763k
    case ASN1_ITYPE_NDEF_SEQUENCE:
336
48.4M
    case ASN1_ITYPE_SEQUENCE:
337
48.4M
        p = *in;
338
48.4M
        tmplen = len;
339
340
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
341
48.4M
        if (tag == -1) {
342
44.0M
            tag = V_ASN1_SEQUENCE;
343
44.0M
            aclass = V_ASN1_UNIVERSAL;
344
44.0M
        }
345
        /* Get SEQUENCE length and update len, p */
346
48.4M
        ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
347
48.4M
                              &p, len, tag, aclass, opt, ctx);
348
48.4M
        if (!ret) {
349
4.08M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
350
4.08M
            goto err;
351
44.4M
        } else if (ret == -1)
352
4.95M
            return -1;
353
39.4M
        if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
354
0
            len = tmplen - (p - *in);
355
0
            seq_nolen = 1;
356
0
        }
357
        /* If indefinite we don't do a length check */
358
39.4M
        else
359
39.4M
            seq_nolen = seq_eoc;
360
39.4M
        if (!cst) {
361
111k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
362
111k
            goto err;
363
111k
        }
364
365
39.3M
        if (*pval == NULL
366
39.3M
                && !ossl_asn1_item_ex_new_intern(pval, it, libctx, propq)) {
367
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
368
0
            goto err;
369
0
        }
370
371
39.3M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
372
0
            goto auxerr;
373
374
        /* Free up and zero any ADB found */
375
150M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
376
110M
            if (tt->flags & ASN1_TFLG_ADB_MASK) {
377
581k
                const ASN1_TEMPLATE *seqtt;
378
581k
                ASN1_VALUE **pseqval;
379
581k
                seqtt = ossl_asn1_do_adb(*pval, tt, 0);
380
581k
                if (seqtt == NULL)
381
467k
                    continue;
382
113k
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
383
113k
                ossl_asn1_template_free(pseqval, seqtt);
384
113k
            }
385
110M
        }
386
387
        /* Get each field entry */
388
112M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
389
86.6M
            const ASN1_TEMPLATE *seqtt;
390
86.6M
            ASN1_VALUE **pseqval;
391
86.6M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
392
86.6M
            if (seqtt == NULL)
393
0
                goto err;
394
86.6M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
395
            /* Have we ran out of data? */
396
86.6M
            if (!len)
397
2.11M
                break;
398
84.5M
            q = p;
399
84.5M
            if (asn1_check_eoc(&p, len)) {
400
2.48M
                if (!seq_eoc) {
401
31.5k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
402
31.5k
                    goto err;
403
31.5k
                }
404
2.45M
                len -= p - q;
405
2.45M
                seq_eoc = 0;
406
2.45M
                break;
407
2.48M
            }
408
            /*
409
             * This determines the OPTIONAL flag value. The field cannot be
410
             * omitted if it is the last of a SEQUENCE and there is still
411
             * data to be read. This isn't strictly necessary but it
412
             * increases efficiency in some cases.
413
             */
414
82.0M
            if (i == (it->tcount - 1))
415
27.5M
                isopt = 0;
416
54.5M
            else
417
54.5M
                isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
418
            /*
419
             * attempt to read in field, allowing each to be OPTIONAL
420
             */
421
422
82.0M
            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
423
82.0M
                                       depth, libctx, propq);
424
82.0M
            if (!ret) {
425
8.67M
                errtt = seqtt;
426
8.67M
                goto err;
427
73.3M
            } else if (ret == -1) {
428
                /*
429
                 * OPTIONAL component absent. Free and zero the field.
430
                 */
431
8.02M
                ossl_asn1_template_free(pseqval, seqtt);
432
8.02M
                continue;
433
8.02M
            }
434
            /* Update length */
435
65.3M
            len -= p - q;
436
65.3M
        }
437
438
        /* Check for EOC if expecting one */
439
30.6M
        if (seq_eoc && !asn1_check_eoc(&p, len)) {
440
171k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
441
171k
            goto err;
442
171k
        }
443
        /* Check all data read */
444
30.4M
        if (!seq_nolen && len) {
445
33.1k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
446
33.1k
            goto err;
447
33.1k
        }
448
449
        /*
450
         * If we get here we've got no more data in the SEQUENCE, however we
451
         * may not have read all fields so check all remaining are OPTIONAL
452
         * and clear any that are.
453
         */
454
39.9M
        for (; i < it->tcount; tt++, i++) {
455
9.75M
            const ASN1_TEMPLATE *seqtt;
456
9.75M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
457
9.75M
            if (seqtt == NULL)
458
0
                goto err;
459
9.75M
            if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
460
9.50M
                ASN1_VALUE **pseqval;
461
9.50M
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
462
9.50M
                ossl_asn1_template_free(pseqval, seqtt);
463
9.50M
            } else {
464
246k
                errtt = seqtt;
465
246k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_FIELD_MISSING);
466
246k
                goto err;
467
246k
            }
468
9.75M
        }
469
        /* Save encoding */
470
30.1M
        if (!ossl_asn1_enc_save(pval, *in, p - *in, it))
471
0
            goto auxerr;
472
30.1M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
473
16.6k
            goto auxerr;
474
30.1M
        *in = p;
475
30.1M
        return 1;
476
477
0
    default:
478
0
        return 0;
479
319M
    }
480
16.6k
 auxerr:
481
16.6k
    ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
482
15.2M
 err:
483
15.2M
    if (errtt)
484
9.79M
        ERR_add_error_data(4, "Field=", errtt->field_name,
485
9.79M
                           ", Type=", it->sname);
486
5.48M
    else
487
5.48M
        ERR_add_error_data(2, "Type=", it->sname);
488
15.2M
    return 0;
489
16.6k
}
490
491
/*
492
 * Templates are handled with two separate functions. One handles any
493
 * EXPLICIT tag and the other handles the rest.
494
 */
495
496
static int asn1_template_ex_d2i(ASN1_VALUE **val,
497
                                const unsigned char **in, long inlen,
498
                                const ASN1_TEMPLATE *tt, char opt,
499
                                ASN1_TLC *ctx, int depth,
500
                                OSSL_LIB_CTX *libctx, const char *propq)
501
236M
{
502
236M
    int flags, aclass;
503
236M
    int ret;
504
236M
    long len;
505
236M
    const unsigned char *p, *q;
506
236M
    char exp_eoc;
507
236M
    if (!val)
508
0
        return 0;
509
236M
    flags = tt->flags;
510
236M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
511
512
236M
    p = *in;
513
514
    /* Check if EXPLICIT tag expected */
515
236M
    if (flags & ASN1_TFLG_EXPTAG) {
516
6.49M
        char cst;
517
        /*
518
         * Need to work out amount of data available to the inner content and
519
         * where it starts: so read in EXPLICIT header to get the info.
520
         */
521
6.49M
        ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
522
6.49M
                              &p, inlen, tt->tag, aclass, opt, ctx);
523
6.49M
        q = p;
524
6.49M
        if (!ret) {
525
338k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
526
338k
            return 0;
527
6.15M
        } else if (ret == -1)
528
4.65M
            return -1;
529
1.49M
        if (!cst) {
530
6.03k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
531
6.03k
            return 0;
532
6.03k
        }
533
        /* We've found the field so it can't be OPTIONAL now */
534
1.49M
        ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth, libctx,
535
1.49M
                                      propq);
536
1.49M
        if (!ret) {
537
268k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
538
268k
            return 0;
539
268k
        }
540
        /* We read the field in OK so update length */
541
1.22M
        len -= p - q;
542
1.22M
        if (exp_eoc) {
543
            /* If NDEF we must have an EOC here */
544
393k
            if (!asn1_check_eoc(&p, len)) {
545
35.6k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
546
35.6k
                goto err;
547
35.6k
            }
548
831k
        } else {
549
            /*
550
             * Otherwise we must hit the EXPLICIT tag end or its an error
551
             */
552
831k
            if (len) {
553
3.55k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
554
3.55k
                goto err;
555
3.55k
            }
556
831k
        }
557
1.22M
    } else
558
229M
        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth,
559
229M
                                       libctx, propq);
560
561
1.18M
    *in = p;
562
1.18M
    return 1;
563
564
39.2k
 err:
565
39.2k
    return 0;
566
236M
}
567
568
static int asn1_template_noexp_d2i(ASN1_VALUE **val,
569
                                   const unsigned char **in, long len,
570
                                   const ASN1_TEMPLATE *tt, char opt,
571
                                   ASN1_TLC *ctx, int depth,
572
                                   OSSL_LIB_CTX *libctx, const char *propq)
573
231M
{
574
231M
    int flags, aclass;
575
231M
    int ret;
576
231M
    ASN1_VALUE *tval;
577
231M
    const unsigned char *p, *q;
578
231M
    if (!val)
579
0
        return 0;
580
231M
    flags = tt->flags;
581
231M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
582
583
231M
    p = *in;
584
585
    /*
586
     * If field is embedded then val needs fixing so it is a pointer to
587
     * a pointer to a field.
588
     */
589
231M
    if (tt->flags & ASN1_TFLG_EMBED) {
590
9.34M
        tval = (ASN1_VALUE *)val;
591
9.34M
        val = &tval;
592
9.34M
    }
593
594
231M
    if (flags & ASN1_TFLG_SK_MASK) {
595
        /* SET OF, SEQUENCE OF */
596
138M
        int sktag, skaclass;
597
138M
        char sk_eoc;
598
        /* First work out expected inner tag value */
599
138M
        if (flags & ASN1_TFLG_IMPTAG) {
600
1.25M
            sktag = tt->tag;
601
1.25M
            skaclass = aclass;
602
136M
        } else {
603
136M
            skaclass = V_ASN1_UNIVERSAL;
604
136M
            if (flags & ASN1_TFLG_SET_OF)
605
131M
                sktag = V_ASN1_SET;
606
5.31M
            else
607
5.31M
                sktag = V_ASN1_SEQUENCE;
608
136M
        }
609
        /* Get the tag */
610
138M
        ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
611
138M
                              &p, len, sktag, skaclass, opt, ctx);
612
138M
        if (!ret) {
613
1.24M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
614
1.24M
            return 0;
615
136M
        } else if (ret == -1)
616
699k
            return -1;
617
136M
        if (*val == NULL)
618
135M
            *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
619
402k
        else {
620
            /*
621
             * We've got a valid STACK: free up any items present
622
             */
623
402k
            STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
624
402k
            ASN1_VALUE *vtmp;
625
402k
            while (sk_ASN1_VALUE_num(sktmp) > 0) {
626
0
                vtmp = sk_ASN1_VALUE_pop(sktmp);
627
0
                ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
628
0
            }
629
402k
        }
630
631
136M
        if (*val == NULL) {
632
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
633
0
            goto err;
634
0
        }
635
636
        /* Read as many items as we can */
637
336M
        while (len > 0) {
638
206M
            ASN1_VALUE *skfield;
639
206M
            q = p;
640
            /* See if EOC found */
641
206M
            if (asn1_check_eoc(&p, len)) {
642
4.44M
                if (!sk_eoc) {
643
12.6k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
644
12.6k
                    goto err;
645
12.6k
                }
646
4.42M
                len -= p - q;
647
4.42M
                sk_eoc = 0;
648
4.42M
                break;
649
4.44M
            }
650
202M
            skfield = NULL;
651
202M
            if (asn1_item_embed_d2i(&skfield, &p, len,
652
202M
                                     ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
653
202M
                                     depth, libctx, propq) <= 0) {
654
1.61M
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
655
                /* |skfield| may be partially allocated despite failure. */
656
1.61M
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
657
1.61M
                goto err;
658
1.61M
            }
659
200M
            len -= p - q;
660
200M
            if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
661
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
662
0
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
663
0
                goto err;
664
0
            }
665
200M
        }
666
134M
        if (sk_eoc) {
667
12.6k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
668
12.6k
            goto err;
669
12.6k
        }
670
134M
    } else if (flags & ASN1_TFLG_IMPTAG) {
671
        /* IMPLICIT tagging */
672
15.3M
        ret = asn1_item_embed_d2i(val, &p, len,
673
15.3M
                                  ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
674
15.3M
                                  ctx, depth, libctx, propq);
675
15.3M
        if (!ret) {
676
452k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
677
452k
            goto err;
678
14.8M
        } else if (ret == -1)
679
11.6M
            return -1;
680
77.7M
    } else {
681
        /* Nothing special */
682
77.7M
        ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
683
77.7M
                                  -1, 0, opt, ctx, depth, libctx, propq);
684
77.7M
        if (!ret) {
685
7.70M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
686
7.70M
            goto err;
687
70.0M
        } else if (ret == -1)
688
5.14M
            return -1;
689
77.7M
    }
690
691
202M
    *in = p;
692
202M
    return 1;
693
694
9.79M
 err:
695
9.79M
    return 0;
696
231M
}
697
698
static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
699
                                 const unsigned char **in, long inlen,
700
                                 const ASN1_ITEM *it,
701
                                 int tag, int aclass, char opt, ASN1_TLC *ctx)
702
126M
{
703
126M
    int ret = 0, utype;
704
126M
    long plen;
705
126M
    char cst, inf, free_cont = 0;
706
126M
    const unsigned char *p;
707
126M
    BUF_MEM buf = { 0, NULL, 0, 0 };
708
126M
    const unsigned char *cont = NULL;
709
126M
    long len;
710
711
126M
    if (pval == NULL) {
712
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NULL);
713
0
        return 0;               /* Should never happen */
714
0
    }
715
716
126M
    if (it->itype == ASN1_ITYPE_MSTRING) {
717
16.2M
        utype = tag;
718
16.2M
        tag = -1;
719
16.2M
    } else
720
110M
        utype = it->utype;
721
722
126M
    if (utype == V_ASN1_ANY) {
723
        /* If type is ANY need to figure out type from tag */
724
47.6M
        unsigned char oclass;
725
47.6M
        if (tag >= 0) {
726
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
727
0
            return 0;
728
0
        }
729
47.6M
        if (opt) {
730
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
731
0
            return 0;
732
0
        }
733
47.6M
        p = *in;
734
47.6M
        ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
735
47.6M
                              &p, inlen, -1, 0, 0, ctx);
736
47.6M
        if (!ret) {
737
29.1k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
738
29.1k
            return 0;
739
29.1k
        }
740
47.5M
        if (oclass != V_ASN1_UNIVERSAL)
741
613k
            utype = V_ASN1_OTHER;
742
47.5M
    }
743
126M
    if (tag == -1) {
744
115M
        tag = utype;
745
115M
        aclass = V_ASN1_UNIVERSAL;
746
115M
    }
747
126M
    p = *in;
748
    /* Check header */
749
126M
    ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
750
126M
                          &p, inlen, tag, aclass, opt, ctx);
751
126M
    if (!ret) {
752
6.29M
        ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
753
6.29M
        return 0;
754
120M
    } else if (ret == -1)
755
11.7M
        return -1;
756
108M
    ret = 0;
757
    /* SEQUENCE, SET and "OTHER" are left in encoded form */
758
108M
    if ((utype == V_ASN1_SEQUENCE)
759
108M
        || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) {
760
        /*
761
         * Clear context cache for type OTHER because the auto clear when we
762
         * have a exact match won't work
763
         */
764
18.1M
        if (utype == V_ASN1_OTHER) {
765
613k
            asn1_tlc_clear(ctx);
766
613k
        }
767
        /* SEQUENCE and SET must be constructed */
768
17.5M
        else if (!cst) {
769
10.5k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
770
10.5k
            return 0;
771
10.5k
        }
772
773
18.1M
        cont = *in;
774
        /* If indefinite length constructed find the real end */
775
18.1M
        if (inf) {
776
818k
            if (!asn1_find_end(&p, plen, inf))
777
98.8k
                goto err;
778
719k
            len = p - cont;
779
17.3M
        } else {
780
17.3M
            len = p - cont + plen;
781
17.3M
            p += plen;
782
17.3M
        }
783
90.2M
    } else if (cst) {
784
5.92M
        if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
785
5.92M
            || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
786
5.92M
            || utype == V_ASN1_ENUMERATED) {
787
21.2k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
788
21.2k
            return 0;
789
21.2k
        }
790
791
        /* Free any returned 'buf' content */
792
5.90M
        free_cont = 1;
793
        /*
794
         * Should really check the internal tags are correct but some things
795
         * may get this wrong. The relevant specs say that constructed string
796
         * types should be OCTET STRINGs internally irrespective of the type.
797
         * So instead just check for UNIVERSAL class and ignore the tag.
798
         */
799
5.90M
        if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
800
40.7k
            goto err;
801
40.7k
        }
802
5.86M
        len = buf.length;
803
        /* Append a final null to string */
804
5.86M
        if (!BUF_MEM_grow_clean(&buf, len + 1)) {
805
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
806
0
            goto err;
807
0
        }
808
5.86M
        buf.data[len] = 0;
809
5.86M
        cont = (const unsigned char *)buf.data;
810
84.3M
    } else {
811
84.3M
        cont = p;
812
84.3M
        len = plen;
813
84.3M
        p += plen;
814
84.3M
    }
815
816
    /* We now have content length and type: translate into a structure */
817
    /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */
818
108M
    if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
819
750k
        goto err;
820
821
107M
    *in = p;
822
107M
    ret = 1;
823
108M
 err:
824
108M
    if (free_cont)
825
329k
        OPENSSL_free(buf.data);
826
108M
    return ret;
827
107M
}
828
829
/* Translate ASN1 content octets into a structure */
830
831
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
832
                       int utype, char *free_cont, const ASN1_ITEM *it)
833
42.9M
{
834
42.9M
    ASN1_VALUE **opval = NULL;
835
42.9M
    ASN1_STRING *stmp;
836
42.9M
    ASN1_TYPE *typ = NULL;
837
42.9M
    int ret = 0;
838
42.9M
    const ASN1_PRIMITIVE_FUNCS *pf;
839
42.9M
    ASN1_INTEGER **tint;
840
42.9M
    pf = it->funcs;
841
842
42.9M
    if (pf && pf->prim_c2i)
843
1.22M
        return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
844
    /* If ANY type clear type and set pointer to internal value */
845
41.7M
    if (it->utype == V_ASN1_ANY) {
846
18.6M
        if (*pval == NULL) {
847
18.5M
            typ = ASN1_TYPE_new();
848
18.5M
            if (typ == NULL)
849
0
                goto err;
850
18.5M
            *pval = (ASN1_VALUE *)typ;
851
18.5M
        } else
852
49.9k
            typ = (ASN1_TYPE *)*pval;
853
854
18.6M
        if (utype != typ->type)
855
18.6M
            ASN1_TYPE_set(typ, utype, NULL);
856
18.6M
        opval = pval;
857
18.6M
        pval = &typ->value.asn1_value;
858
18.6M
    }
859
41.7M
    switch (utype) {
860
10.6M
    case V_ASN1_OBJECT:
861
10.6M
        if (!ossl_c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
862
13.7k
            goto err;
863
10.6M
        break;
864
865
10.6M
    case V_ASN1_NULL:
866
188k
        if (len) {
867
1.93k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
868
1.93k
            goto err;
869
1.93k
        }
870
186k
        *pval = (ASN1_VALUE *)1;
871
186k
        break;
872
873
9.57M
    case V_ASN1_BOOLEAN:
874
9.57M
        if (len != 1) {
875
2.96k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
876
2.96k
            goto err;
877
9.57M
        } else {
878
9.57M
            ASN1_BOOLEAN *tbool;
879
9.57M
            tbool = (ASN1_BOOLEAN *)pval;
880
9.57M
            *tbool = *cont;
881
9.57M
        }
882
9.57M
        break;
883
884
9.57M
    case V_ASN1_BIT_STRING:
885
2.02M
        if (!ossl_c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
886
9.54k
            goto err;
887
2.01M
        break;
888
889
2.14M
    case V_ASN1_INTEGER:
890
2.19M
    case V_ASN1_ENUMERATED:
891
2.19M
        tint = (ASN1_INTEGER **)pval;
892
2.19M
        if (!ossl_c2i_ASN1_INTEGER(tint, &cont, len))
893
179k
            goto err;
894
        /* Fixup type to match the expected form */
895
2.01M
        (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
896
2.01M
        break;
897
898
1.35M
    case V_ASN1_OCTET_STRING:
899
1.65M
    case V_ASN1_NUMERICSTRING:
900
3.42M
    case V_ASN1_PRINTABLESTRING:
901
3.65M
    case V_ASN1_T61STRING:
902
3.66M
    case V_ASN1_VIDEOTEXSTRING:
903
4.63M
    case V_ASN1_IA5STRING:
904
5.13M
    case V_ASN1_UTCTIME:
905
5.43M
    case V_ASN1_GENERALIZEDTIME:
906
5.50M
    case V_ASN1_GRAPHICSTRING:
907
5.51M
    case V_ASN1_VISIBLESTRING:
908
5.51M
    case V_ASN1_GENERALSTRING:
909
5.73M
    case V_ASN1_UNIVERSALSTRING:
910
6.04M
    case V_ASN1_BMPSTRING:
911
6.35M
    case V_ASN1_UTF8STRING:
912
6.53M
    case V_ASN1_OTHER:
913
13.4M
    case V_ASN1_SET:
914
14.8M
    case V_ASN1_SEQUENCE:
915
17.1M
    default:
916
17.1M
        if (utype == V_ASN1_BMPSTRING && (len & 1)) {
917
748
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
918
748
            goto err;
919
748
        }
920
17.1M
        if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
921
1.13k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
922
1.13k
            goto err;
923
1.13k
        }
924
        /* All based on ASN1_STRING and handled the same */
925
17.1M
        if (*pval == NULL) {
926
9.01M
            stmp = ASN1_STRING_type_new(utype);
927
9.01M
            if (stmp == NULL) {
928
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
929
0
                goto err;
930
0
            }
931
9.01M
            *pval = (ASN1_VALUE *)stmp;
932
9.01M
        } else {
933
8.10M
            stmp = (ASN1_STRING *)*pval;
934
8.10M
            stmp->type = utype;
935
8.10M
        }
936
        /* If we've already allocated a buffer use it */
937
17.1M
        if (*free_cont) {
938
2.89M
            OPENSSL_free(stmp->data);
939
2.89M
            stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
940
2.89M
            stmp->length = len;
941
2.89M
            *free_cont = 0;
942
14.2M
        } else {
943
14.2M
            if (!ASN1_STRING_set(stmp, cont, len)) {
944
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
945
0
                ASN1_STRING_free(stmp);
946
0
                *pval = NULL;
947
0
                goto err;
948
0
            }
949
14.2M
        }
950
17.1M
        break;
951
41.7M
    }
952
    /* If ASN1_ANY and NULL type fix up value */
953
41.5M
    if (typ && (utype == V_ASN1_NULL))
954
180k
        typ->value.ptr = NULL;
955
956
41.5M
    ret = 1;
957
41.7M
 err:
958
41.7M
    if (!ret) {
959
209k
        ASN1_TYPE_free(typ);
960
209k
        if (opval)
961
10.0k
            *opval = NULL;
962
209k
    }
963
41.7M
    return ret;
964
41.5M
}
965
966
/*
967
 * This function finds the end of an ASN1 structure when passed its maximum
968
 * length, whether it is indefinite length and a pointer to the content. This
969
 * is more efficient than calling asn1_collect because it does not recurse on
970
 * each indefinite length header.
971
 */
972
973
static int asn1_find_end(const unsigned char **in, long len, char inf)
974
818k
{
975
818k
    uint32_t expected_eoc;
976
818k
    long plen;
977
818k
    const unsigned char *p = *in, *q;
978
    /* If not indefinite length constructed just add length */
979
818k
    if (inf == 0) {
980
0
        *in += len;
981
0
        return 1;
982
0
    }
983
818k
    expected_eoc = 1;
984
    /*
985
     * Indefinite length constructed form. Find the end when enough EOCs are
986
     * found. If more indefinite length constructed headers are encountered
987
     * increment the expected eoc count otherwise just skip to the end of the
988
     * data.
989
     */
990
360M
    while (len > 0) {
991
360M
        if (asn1_check_eoc(&p, len)) {
992
36.9M
            expected_eoc--;
993
36.9M
            if (expected_eoc == 0)
994
719k
                break;
995
36.2M
            len -= 2;
996
36.2M
            continue;
997
36.9M
        }
998
323M
        q = p;
999
        /* Just read in a header: only care about the length */
1000
323M
        if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1001
323M
                             -1, 0, 0, NULL)) {
1002
59.8k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1003
59.8k
            return 0;
1004
59.8k
        }
1005
323M
        if (inf) {
1006
87.4M
            if (expected_eoc == UINT32_MAX) {
1007
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1008
0
                return 0;
1009
0
            }
1010
87.4M
            expected_eoc++;
1011
235M
        } else {
1012
235M
            p += plen;
1013
235M
        }
1014
323M
        len -= p - q;
1015
323M
    }
1016
758k
    if (expected_eoc) {
1017
39.0k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1018
39.0k
        return 0;
1019
39.0k
    }
1020
719k
    *in = p;
1021
719k
    return 1;
1022
758k
}
1023
1024
/*
1025
 * This function collects the asn1 data from a constructed string type into
1026
 * a buffer. The values of 'in' and 'len' should refer to the contents of the
1027
 * constructed type and 'inf' should be set if it is indefinite length.
1028
 */
1029
1030
#ifndef ASN1_MAX_STRING_NEST
1031
/*
1032
 * This determines how many levels of recursion are permitted in ASN1 string
1033
 * types. If it is not limited stack overflows can occur. If set to zero no
1034
 * recursion is allowed at all. Although zero should be adequate examples
1035
 * exist that require a value of 1. So 5 should be more than enough.
1036
 */
1037
3.54M
# define ASN1_MAX_STRING_NEST 5
1038
#endif
1039
1040
static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1041
                        char inf, int tag, int aclass, int depth)
1042
9.44M
{
1043
9.44M
    const unsigned char *p, *q;
1044
9.44M
    long plen;
1045
9.44M
    char cst, ininf;
1046
9.44M
    p = *in;
1047
9.44M
    inf &= 1;
1048
    /*
1049
     * If no buffer and not indefinite length constructed just pass over the
1050
     * encoded data
1051
     */
1052
9.44M
    if (!buf && !inf) {
1053
0
        *in += len;
1054
0
        return 1;
1055
0
    }
1056
55.6M
    while (len > 0) {
1057
47.4M
        q = p;
1058
        /* Check for EOC */
1059
47.4M
        if (asn1_check_eoc(&p, len)) {
1060
            /*
1061
             * EOC is illegal outside indefinite length constructed form
1062
             */
1063
1.15M
            if (!inf) {
1064
4.57k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
1065
4.57k
                return 0;
1066
4.57k
            }
1067
1.15M
            inf = 0;
1068
1.15M
            break;
1069
1.15M
        }
1070
1071
46.2M
        if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1072
46.2M
                             len, tag, aclass, 0, NULL)) {
1073
24.8k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1074
24.8k
            return 0;
1075
24.8k
        }
1076
1077
        /* If indefinite length constructed update max length */
1078
46.2M
        if (cst) {
1079
3.54M
            if (depth >= ASN1_MAX_STRING_NEST) {
1080
3.01k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_ASN1_STRING);
1081
3.01k
                return 0;
1082
3.01k
            }
1083
3.53M
            if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1084
35.4k
                return 0;
1085
42.7M
        } else if (plen && !collect_data(buf, &p, plen))
1086
0
            return 0;
1087
46.2M
        len -= p - q;
1088
46.2M
    }
1089
9.37M
    if (inf) {
1090
8.34k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1091
8.34k
        return 0;
1092
8.34k
    }
1093
9.36M
    *in = p;
1094
9.36M
    return 1;
1095
9.37M
}
1096
1097
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1098
36.0M
{
1099
36.0M
    int len;
1100
36.0M
    if (buf) {
1101
36.0M
        len = buf->length;
1102
36.0M
        if (!BUF_MEM_grow_clean(buf, len + plen)) {
1103
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
1104
0
            return 0;
1105
0
        }
1106
36.0M
        memcpy(buf->data + len, *p, plen);
1107
36.0M
    }
1108
36.0M
    *p += plen;
1109
36.0M
    return 1;
1110
36.0M
}
1111
1112
/* Check for ASN1 EOC and swallow it if found */
1113
1114
static int asn1_check_eoc(const unsigned char **in, long len)
1115
706M
{
1116
706M
    const unsigned char *p;
1117
1118
706M
    if (len < 2)
1119
161k
        return 0;
1120
706M
    p = *in;
1121
706M
    if (p[0] == '\0' && p[1] == '\0') {
1122
52.2M
        *in += 2;
1123
52.2M
        return 1;
1124
52.2M
    }
1125
654M
    return 0;
1126
706M
}
1127
1128
/*
1129
 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1130
 * length for indefinite length constructed form, we don't know the exact
1131
 * length but we can set an upper bound to the amount of data available minus
1132
 * the header length just read.
1133
 */
1134
1135
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1136
                           char *inf, char *cst,
1137
                           const unsigned char **in, long len,
1138
                           int exptag, int expclass, char opt, ASN1_TLC *ctx)
1139
753M
{
1140
753M
    int i;
1141
753M
    int ptag, pclass;
1142
753M
    long plen;
1143
753M
    const unsigned char *p, *q;
1144
753M
    p = *in;
1145
753M
    q = p;
1146
1147
753M
    if (len <= 0) {
1148
184
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
1149
184
        goto err;
1150
184
    }
1151
753M
    if (ctx != NULL && ctx->valid) {
1152
85.3M
        i = ctx->ret;
1153
85.3M
        plen = ctx->plen;
1154
85.3M
        pclass = ctx->pclass;
1155
85.3M
        ptag = ctx->ptag;
1156
85.3M
        p += ctx->hdrlen;
1157
668M
    } else {
1158
668M
        i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1159
668M
        if (ctx != NULL) {
1160
298M
            ctx->ret = i;
1161
298M
            ctx->plen = plen;
1162
298M
            ctx->pclass = pclass;
1163
298M
            ctx->ptag = ptag;
1164
298M
            ctx->hdrlen = p - q;
1165
298M
            ctx->valid = 1;
1166
            /*
1167
             * If definite length, and no error, length + header can't exceed
1168
             * total amount of data available.
1169
             */
1170
298M
            if ((i & 0x81) == 0 && (plen + ctx->hdrlen) > len) {
1171
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
1172
0
                goto err;
1173
0
            }
1174
298M
        }
1175
668M
    }
1176
1177
753M
    if ((i & 0x80) != 0) {
1178
1.00M
        ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER);
1179
1.00M
        goto err;
1180
1.00M
    }
1181
752M
    if (exptag >= 0) {
1182
318M
        if (exptag != ptag || expclass != pclass) {
1183
            /*
1184
             * If type is OPTIONAL, not an error: indicate missing type.
1185
             */
1186
33.1M
            if (opt != 0)
1187
22.0M
                return -1;
1188
33.1M
            ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG);
1189
11.0M
            goto err;
1190
33.1M
        }
1191
        /*
1192
         * We have a tag and class match: assume we are going to do something
1193
         * with it
1194
         */
1195
284M
        asn1_tlc_clear(ctx);
1196
284M
    }
1197
1198
719M
    if ((i & 1) != 0)
1199
112M
        plen = len - (p - q);
1200
1201
719M
    if (inf != NULL)
1202
654M
        *inf = i & 1;
1203
1204
719M
    if (cst != NULL)
1205
195M
        *cst = i & V_ASN1_CONSTRUCTED;
1206
1207
719M
    if (olen != NULL)
1208
654M
        *olen = plen;
1209
1210
719M
    if (oclass != NULL)
1211
64.3M
        *oclass = pclass;
1212
1213
719M
    if (otag != NULL)
1214
64.3M
        *otag = ptag;
1215
1216
719M
    *in = p;
1217
719M
    return 1;
1218
1219
12.0M
 err:
1220
12.0M
    asn1_tlc_clear(ctx);
1221
12.0M
    return 0;
1222
752M
}