Coverage Report

Created: 2025-11-16 06:40

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