Coverage Report

Created: 2025-06-13 06:56

/src/openssl/crypto/asn1/tasn_dec.c
Line
Count
Source (jump to first uncovered line)
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 "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
1.29M
#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
98.1k
{
89
98.1k
    if ((tag < 0) || (tag > 30))
90
23
        return 0;
91
98.1k
    return tag2bit[tag];
92
98.1k
}
93
94
/* Macro to initialize and invalidate the cache */
95
96
1.14M
#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
147k
#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
212k
{
112
212k
    int rv;
113
114
212k
    if (pval == NULL || it == NULL) {
115
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
116
0
        return 0;
117
0
    }
118
212k
    rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0,
119
212k
                             libctx, propq);
120
212k
    if (rv <= 0)
121
732
        ASN1_item_ex_free(pval, it);
122
212k
    return rv;
123
212k
}
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
65.5k
{
129
65.5k
    return asn1_item_ex_d2i_intern(pval, in, len, it, tag, aclass, opt, ctx,
130
65.5k
                                   NULL, NULL);
131
65.5k
}
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
147k
{
138
147k
    ASN1_TLC c;
139
147k
    ASN1_VALUE *ptmpval = NULL;
140
141
147k
    if (pval == NULL)
142
114k
        pval = &ptmpval;
143
147k
    asn1_tlc_clear_nc(&c);
144
147k
    if (asn1_item_ex_d2i_intern(pval, in, len, it, -1, 0, 0, &c, libctx,
145
147k
                                propq) > 0)
146
146k
        return *pval;
147
534
    return NULL;
148
147k
}
149
150
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
151
                          const unsigned char **in, long len,
152
                          const ASN1_ITEM *it)
153
130k
{
154
130k
    return ASN1_item_d2i_ex(pval, in, len, it, NULL, NULL);
155
130k
}
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
1.29M
{
168
1.29M
    const ASN1_TEMPLATE *tt, *errtt = NULL;
169
1.29M
    const ASN1_EXTERN_FUNCS *ef;
170
1.29M
    const ASN1_AUX *aux;
171
1.29M
    ASN1_aux_cb *asn1_cb;
172
1.29M
    const unsigned char *p = NULL, *q;
173
1.29M
    unsigned char oclass;
174
1.29M
    char seq_eoc, seq_nolen, cst, isopt;
175
1.29M
    long tmplen;
176
1.29M
    int i;
177
1.29M
    int otag;
178
1.29M
    int ret = 0;
179
1.29M
    ASN1_VALUE **pchptr;
180
181
1.29M
    if (pval == NULL || it == NULL) {
182
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
183
0
        return 0;
184
0
    }
185
1.29M
    if (len <= 0) {
186
6
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
187
6
        return 0;
188
6
    }
189
1.29M
    aux = it->funcs;
190
1.29M
    if (aux && aux->asn1_cb)
191
152k
        asn1_cb = aux->asn1_cb;
192
1.13M
    else
193
1.13M
        asn1_cb = 0;
194
195
1.29M
    if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
196
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_TOO_DEEP);
197
0
        goto err;
198
0
    }
199
200
1.29M
    switch (it->itype) {
201
800k
    case ASN1_ITYPE_PRIMITIVE:
202
800k
        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
98.5k
            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
98.5k
            return asn1_template_ex_d2i(pval, in, len, it->templates, opt, ctx,
215
98.5k
                                        depth, libctx, propq);
216
98.5k
        }
217
702k
        return asn1_d2i_ex_primitive(pval, in, len, it,
218
702k
                                     tag, aclass, opt, ctx);
219
220
65.4k
    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
65.4k
        if (tag != -1) {
226
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
227
0
            goto err;
228
0
        }
229
230
65.4k
        p = *in;
231
        /* Just read in tag and class */
232
65.4k
        ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
233
65.4k
                              &p, len, -1, 0, 1, ctx);
234
65.4k
        if (!ret) {
235
25
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
236
25
            goto err;
237
25
        }
238
239
        /* Must be UNIVERSAL class */
240
65.4k
        if (oclass != V_ASN1_UNIVERSAL) {
241
            /* If OPTIONAL, assume this is OK */
242
3
            if (opt)
243
0
                return -1;
244
3
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
245
3
            goto err;
246
3
        }
247
248
        /* Check tag matches bit map */
249
65.4k
        if (!(ASN1_tag2bit(otag) & it->utype)) {
250
            /* If OPTIONAL, assume this is OK */
251
28
            if (opt)
252
0
                return -1;
253
28
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_WRONG_TAG);
254
28
            goto err;
255
28
        }
256
65.3k
        return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
257
258
65.5k
    case ASN1_ITYPE_EXTERN:
259
        /* Use new style d2i */
260
65.5k
        ef = it->funcs;
261
65.5k
        if (ef->asn1_ex_d2i_ex != NULL)
262
32.5k
            return ef->asn1_ex_d2i_ex(pval, in, len, it, tag, aclass, opt, ctx,
263
32.5k
                                      libctx, propq);
264
32.9k
        return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
265
266
22.2k
    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
22.2k
        if (tag != -1) {
272
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
273
0
            goto err;
274
0
        }
275
276
22.2k
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
277
0
            goto auxerr;
278
22.2k
        if (*pval) {
279
            /* Free up and zero CHOICE value if initialised */
280
0
            i = ossl_asn1_get_choice_selector(pval, it);
281
0
            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
22.2k
        } 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
22.2k
        p = *in;
293
54.8k
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
294
54.8k
            pchptr = ossl_asn1_get_field_ptr(pval, tt);
295
            /*
296
             * We mark field as OPTIONAL so its absence can be recognised.
297
             */
298
54.8k
            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth,
299
54.8k
                                       libctx, propq);
300
            /* If field not present, try the next one */
301
54.8k
            if (ret == -1)
302
32.6k
                continue;
303
            /* If positive return, read OK, break loop */
304
22.1k
            if (ret > 0)
305
21.7k
                break;
306
            /*
307
             * Must be an ASN1 parsing error.
308
             * Free up any partial choice value
309
             */
310
449
            ossl_asn1_template_free(pchptr, tt);
311
449
            errtt = tt;
312
449
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
313
449
            goto err;
314
22.1k
        }
315
316
        /* Did we fall off the end without reading anything? */
317
21.7k
        if (i == it->tcount) {
318
            /* If OPTIONAL, this is OK */
319
25
            if (opt) {
320
                /* Free and zero it */
321
0
                ASN1_item_ex_free(pval, it);
322
0
                return -1;
323
0
            }
324
25
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
325
25
            goto err;
326
25
        }
327
328
21.7k
        ossl_asn1_set_choice_selector(pval, i, it);
329
330
21.7k
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
331
0
            goto auxerr;
332
21.7k
        *in = p;
333
21.7k
        return 1;
334
335
0
    case ASN1_ITYPE_NDEF_SEQUENCE:
336
337k
    case ASN1_ITYPE_SEQUENCE:
337
337k
        p = *in;
338
337k
        tmplen = len;
339
340
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
341
337k
        if (tag == -1) {
342
320k
            tag = V_ASN1_SEQUENCE;
343
320k
            aclass = V_ASN1_UNIVERSAL;
344
320k
        }
345
        /* Get SEQUENCE length and update len, p */
346
337k
        ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
347
337k
                              &p, len, tag, aclass, opt, ctx);
348
337k
        if (!ret) {
349
22
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
350
22
            goto err;
351
336k
        } else if (ret == -1)
352
16.2k
            return -1;
353
320k
        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
320k
        else
359
320k
            seq_nolen = seq_eoc;
360
320k
        if (!cst) {
361
1
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
362
1
            goto err;
363
1
        }
364
365
320k
        if (*pval == NULL
366
320k
                && !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
320k
        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
1.25M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
376
938k
            if (tt->flags & ASN1_TFLG_ADB_MASK) {
377
0
                const ASN1_TEMPLATE *seqtt;
378
0
                ASN1_VALUE **pseqval;
379
0
                seqtt = ossl_asn1_do_adb(*pval, tt, 0);
380
0
                if (seqtt == NULL)
381
0
                    continue;
382
0
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
383
0
                ossl_asn1_template_free(pseqval, seqtt);
384
0
            }
385
938k
        }
386
387
        /* Get each field entry */
388
1.19M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
389
921k
            const ASN1_TEMPLATE *seqtt;
390
921k
            ASN1_VALUE **pseqval;
391
921k
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
392
921k
            if (seqtt == NULL)
393
0
                goto err;
394
921k
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
395
            /* Have we ran out of data? */
396
921k
            if (!len)
397
43.5k
                break;
398
877k
            q = p;
399
877k
            if (asn1_check_eoc(&p, len)) {
400
5
                if (!seq_eoc) {
401
1
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
402
1
                    goto err;
403
1
                }
404
4
                len -= p - q;
405
4
                seq_eoc = 0;
406
4
                break;
407
5
            }
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
877k
            if (i == (it->tcount - 1))
415
277k
                isopt = 0;
416
600k
            else
417
600k
                isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
418
            /*
419
             * attempt to read in field, allowing each to be OPTIONAL
420
             */
421
422
877k
            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
423
877k
                                       depth, libctx, propq);
424
877k
            if (!ret) {
425
132
                errtt = seqtt;
426
132
                goto err;
427
877k
            } else if (ret == -1) {
428
                /*
429
                 * OPTIONAL component absent. Free and zero the field.
430
                 */
431
88.6k
                ossl_asn1_template_free(pseqval, seqtt);
432
88.6k
                continue;
433
88.6k
            }
434
            /* Update length */
435
788k
            len -= p - q;
436
788k
        }
437
438
        /* Check for EOC if expecting one */
439
320k
        if (seq_eoc && !asn1_check_eoc(&p, len)) {
440
12
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
441
12
            goto err;
442
12
        }
443
        /* Check all data read */
444
320k
        if (!seq_nolen && len) {
445
1
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
446
1
            goto err;
447
1
        }
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
381k
        for (; i < it->tcount; tt++, i++) {
455
60.7k
            const ASN1_TEMPLATE *seqtt;
456
60.7k
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
457
60.7k
            if (seqtt == NULL)
458
0
                goto err;
459
60.7k
            if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
460
60.7k
                ASN1_VALUE **pseqval;
461
60.7k
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
462
60.7k
                ossl_asn1_template_free(pseqval, seqtt);
463
60.7k
            } else {
464
6
                errtt = seqtt;
465
6
                ERR_raise(ERR_LIB_ASN1, ASN1_R_FIELD_MISSING);
466
6
                goto err;
467
6
            }
468
60.7k
        }
469
        /* Save encoding */
470
320k
        if (!ossl_asn1_enc_save(pval, *in, p - *in, it))
471
0
            goto auxerr;
472
320k
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
473
0
            goto auxerr;
474
320k
        *in = p;
475
320k
        return 1;
476
477
0
    default:
478
0
        return 0;
479
1.29M
    }
480
0
 auxerr:
481
0
    ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
482
705
 err:
483
705
    if (errtt)
484
587
        ERR_add_error_data(4, "Field=", errtt->field_name,
485
587
                           ", Type=", it->sname);
486
118
    else
487
118
        ERR_add_error_data(2, "Type=", it->sname);
488
705
    return 0;
489
0
}
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
1.03M
{
502
1.03M
    int flags, aclass;
503
1.03M
    int ret;
504
1.03M
    long len;
505
1.03M
    const unsigned char *p, *q;
506
1.03M
    char exp_eoc;
507
1.03M
    if (!val)
508
0
        return 0;
509
1.03M
    flags = tt->flags;
510
1.03M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
511
512
1.03M
    p = *in;
513
514
    /* Check if EXPLICIT tag expected */
515
1.03M
    if (flags & ASN1_TFLG_EXPTAG) {
516
46.1k
        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
46.1k
        ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
522
46.1k
                              &p, inlen, tt->tag, aclass, opt, ctx);
523
46.1k
        q = p;
524
46.1k
        if (!ret) {
525
72
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
526
72
            return 0;
527
46.1k
        } else if (ret == -1)
528
1.72k
            return -1;
529
44.4k
        if (!cst) {
530
1
            ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
531
1
            return 0;
532
1
        }
533
        /* We've found the field so it can't be OPTIONAL now */
534
44.4k
        ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth, libctx,
535
44.4k
                                      propq);
536
44.4k
        if (!ret) {
537
293
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
538
293
            return 0;
539
293
        }
540
        /* We read the field in OK so update length */
541
44.1k
        len -= p - q;
542
44.1k
        if (exp_eoc) {
543
            /* If NDEF we must have an EOC here */
544
113
            if (!asn1_check_eoc(&p, len)) {
545
88
                ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
546
88
                goto err;
547
88
            }
548
43.9k
        } else {
549
            /*
550
             * Otherwise we must hit the EXPLICIT tag end or its an error
551
             */
552
43.9k
            if (len) {
553
3
                ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
554
3
                goto err;
555
3
            }
556
43.9k
        }
557
44.1k
    } else
558
984k
        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth,
559
984k
                                       libctx, propq);
560
561
44.0k
    *in = p;
562
44.0k
    return 1;
563
564
91
 err:
565
91
    return 0;
566
1.03M
}
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
1.02M
{
574
1.02M
    int flags, aclass;
575
1.02M
    int ret;
576
1.02M
    ASN1_VALUE *tval;
577
1.02M
    const unsigned char *p, *q;
578
1.02M
    if (!val)
579
0
        return 0;
580
1.02M
    flags = tt->flags;
581
1.02M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
582
583
1.02M
    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
1.02M
    if (tt->flags & ASN1_TFLG_EMBED) {
590
179k
        tval = (ASN1_VALUE *)val;
591
179k
        val = &tval;
592
179k
    }
593
594
1.02M
    if (flags & ASN1_TFLG_SK_MASK) {
595
        /* SET OF, SEQUENCE OF */
596
114k
        int sktag, skaclass;
597
114k
        char sk_eoc;
598
        /* First work out expected inner tag value */
599
114k
        if (flags & ASN1_TFLG_IMPTAG) {
600
0
            sktag = tt->tag;
601
0
            skaclass = aclass;
602
114k
        } else {
603
114k
            skaclass = V_ASN1_UNIVERSAL;
604
114k
            if (flags & ASN1_TFLG_SET_OF)
605
32.9k
                sktag = V_ASN1_SET;
606
81.8k
            else
607
81.8k
                sktag = V_ASN1_SEQUENCE;
608
114k
        }
609
        /* Get the tag */
610
114k
        ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
611
114k
                              &p, len, sktag, skaclass, opt, ctx);
612
114k
        if (!ret) {
613
73
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
614
73
            return 0;
615
114k
        } else if (ret == -1)
616
0
            return -1;
617
114k
        if (*val == NULL)
618
114k
            *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
619
0
        else {
620
            /*
621
             * We've got a valid STACK: free up any items present
622
             */
623
0
            STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
624
0
            ASN1_VALUE *vtmp;
625
0
            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
0
        }
630
631
114k
        if (*val == NULL) {
632
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
633
0
            goto err;
634
0
        }
635
636
        /* Read as many items as we can */
637
277k
        while (len > 0) {
638
163k
            ASN1_VALUE *skfield;
639
163k
            q = p;
640
            /* See if EOC found */
641
163k
            if (asn1_check_eoc(&p, len)) {
642
327
                if (!sk_eoc) {
643
1
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
644
1
                    goto err;
645
1
                }
646
326
                len -= p - q;
647
326
                sk_eoc = 0;
648
326
                break;
649
327
            }
650
163k
            skfield = NULL;
651
163k
            if (asn1_item_embed_d2i(&skfield, &p, len,
652
163k
                                     ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
653
163k
                                     depth, libctx, propq) <= 0) {
654
310
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
655
                /* |skfield| may be partially allocated despite failure. */
656
310
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
657
310
                goto err;
658
310
            }
659
163k
            len -= p - q;
660
163k
            if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
661
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
662
0
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
663
0
                goto err;
664
0
            }
665
163k
        }
666
114k
        if (sk_eoc) {
667
1
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
668
1
            goto err;
669
1
        }
670
914k
    } else if (flags & ASN1_TFLG_IMPTAG) {
671
        /* IMPLICIT tagging */
672
81.5k
        ret = asn1_item_embed_d2i(val, &p, len,
673
81.5k
                                  ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
674
81.5k
                                  ctx, depth, libctx, propq);
675
81.5k
        if (!ret) {
676
3
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
677
3
            goto err;
678
81.5k
        } else if (ret == -1)
679
65.2k
            return -1;
680
832k
    } else {
681
        /* Nothing special */
682
832k
        ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
683
832k
                                  -1, 0, opt, ctx, depth, libctx, propq);
684
832k
        if (!ret) {
685
414
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
686
414
            goto err;
687
832k
        } else if (ret == -1)
688
54.2k
            return -1;
689
832k
    }
690
691
908k
    *in = p;
692
908k
    return 1;
693
694
729
 err:
695
729
    return 0;
696
1.02M
}
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
767k
{
703
767k
    int ret = 0, utype;
704
767k
    long plen;
705
767k
    char cst, inf, free_cont = 0;
706
767k
    const unsigned char *p;
707
767k
    BUF_MEM buf = { 0, NULL, 0, 0 };
708
767k
    const unsigned char *cont = NULL;
709
767k
    long len;
710
711
767k
    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
767k
    if (it->itype == ASN1_ITYPE_MSTRING) {
717
65.3k
        utype = tag;
718
65.3k
        tag = -1;
719
65.3k
    } else
720
702k
        utype = it->utype;
721
722
767k
    if (utype == V_ASN1_ANY) {
723
        /* If type is ANY need to figure out type from tag */
724
59.6k
        unsigned char oclass;
725
59.6k
        if (tag >= 0) {
726
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
727
0
            return 0;
728
0
        }
729
59.6k
        if (opt) {
730
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
731
0
            return 0;
732
0
        }
733
59.6k
        p = *in;
734
59.6k
        ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
735
59.6k
                              &p, inlen, -1, 0, 0, ctx);
736
59.6k
        if (!ret) {
737
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
738
0
            return 0;
739
0
        }
740
59.6k
        if (oclass != V_ASN1_UNIVERSAL)
741
0
            utype = V_ASN1_OTHER;
742
59.6k
    }
743
767k
    if (tag == -1) {
744
702k
        tag = utype;
745
702k
        aclass = V_ASN1_UNIVERSAL;
746
702k
    }
747
767k
    p = *in;
748
    /* Check header */
749
767k
    ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
750
767k
                          &p, inlen, tag, aclass, opt, ctx);
751
767k
    if (!ret) {
752
14
        ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
753
14
        return 0;
754
767k
    } else if (ret == -1)
755
103k
        return -1;
756
664k
    ret = 0;
757
    /* SEQUENCE, SET and "OTHER" are left in encoded form */
758
664k
    if ((utype == V_ASN1_SEQUENCE)
759
664k
        || (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
16.3k
        if (utype == V_ASN1_OTHER) {
765
0
            asn1_tlc_clear(ctx);
766
0
        }
767
        /* SEQUENCE and SET must be constructed */
768
16.3k
        else if (!cst) {
769
2
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
770
2
            return 0;
771
2
        }
772
773
16.3k
        cont = *in;
774
        /* If indefinite length constructed find the real end */
775
16.3k
        if (inf) {
776
24
            if (!asn1_find_end(&p, plen, inf))
777
20
                goto err;
778
4
            len = p - cont;
779
16.2k
        } else {
780
16.2k
            len = p - cont + plen;
781
16.2k
            p += plen;
782
16.2k
        }
783
647k
    } else if (cst) {
784
90
        if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
785
90
            || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
786
90
            || utype == V_ASN1_ENUMERATED) {
787
2
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
788
2
            return 0;
789
2
        }
790
791
        /* Free any returned 'buf' content */
792
88
        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
88
        if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
800
31
            goto err;
801
31
        }
802
57
        len = buf.length;
803
        /* Append a final null to string */
804
57
        if (!BUF_MEM_grow_clean(&buf, len + 1)) {
805
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
806
0
            goto err;
807
0
        }
808
57
        buf.data[len] = 0;
809
57
        cont = (const unsigned char *)buf.data;
810
647k
    } else {
811
647k
        cont = p;
812
647k
        len = plen;
813
647k
        p += plen;
814
647k
    }
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
664k
    if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
819
20
        goto err;
820
821
664k
    *in = p;
822
664k
    ret = 1;
823
664k
 err:
824
664k
    if (free_cont)
825
34
        OPENSSL_free(buf.data);
826
664k
    return ret;
827
664k
}
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
664k
{
834
664k
    ASN1_VALUE **opval = NULL;
835
664k
    ASN1_STRING *stmp;
836
664k
    ASN1_TYPE *typ = NULL;
837
664k
    int ret = 0;
838
664k
    const ASN1_PRIMITIVE_FUNCS *pf;
839
664k
    ASN1_INTEGER **tint;
840
664k
    pf = it->funcs;
841
842
664k
    if (pf && pf->prim_c2i)
843
114k
        return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
844
    /* If ANY type clear type and set pointer to internal value */
845
549k
    if (it->utype == V_ASN1_ANY) {
846
59.6k
        if (*pval == NULL) {
847
59.6k
            typ = ASN1_TYPE_new();
848
59.6k
            if (typ == NULL)
849
0
                goto err;
850
59.6k
            *pval = (ASN1_VALUE *)typ;
851
59.6k
        } else
852
0
            typ = (ASN1_TYPE *)*pval;
853
854
59.6k
        if (utype != typ->type)
855
59.6k
            ASN1_TYPE_set(typ, utype, NULL);
856
59.6k
        opval = pval;
857
59.6k
        pval = &typ->value.asn1_value;
858
59.6k
    }
859
549k
    switch (utype) {
860
217k
    case V_ASN1_OBJECT:
861
217k
        if (!ossl_c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
862
4
            goto err;
863
217k
        break;
864
865
217k
    case V_ASN1_NULL:
866
27.1k
        if (len) {
867
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
868
0
            goto err;
869
0
        }
870
27.1k
        *pval = (ASN1_VALUE *)1;
871
27.1k
        break;
872
873
10.8k
    case V_ASN1_BOOLEAN:
874
10.8k
        if (len != 1) {
875
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
876
0
            goto err;
877
10.8k
        } else {
878
10.8k
            ASN1_BOOLEAN *tbool;
879
10.8k
            tbool = (ASN1_BOOLEAN *)pval;
880
10.8k
            *tbool = *cont;
881
10.8k
        }
882
10.8k
        break;
883
884
86.8k
    case V_ASN1_BIT_STRING:
885
86.8k
        if (!ossl_c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
886
5
            goto err;
887
86.8k
        break;
888
889
86.8k
    case V_ASN1_INTEGER:
890
37.9k
    case V_ASN1_ENUMERATED:
891
37.9k
        tint = (ASN1_INTEGER **)pval;
892
37.9k
        if (!ossl_c2i_ASN1_INTEGER(tint, &cont, len))
893
0
            goto err;
894
        /* Fixup type to match the expected form */
895
37.9k
        (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
896
37.9k
        break;
897
898
71.2k
    case V_ASN1_OCTET_STRING:
899
71.2k
    case V_ASN1_NUMERICSTRING:
900
82.0k
    case V_ASN1_PRINTABLESTRING:
901
82.0k
    case V_ASN1_T61STRING:
902
82.0k
    case V_ASN1_VIDEOTEXSTRING:
903
98.3k
    case V_ASN1_IA5STRING:
904
130k
    case V_ASN1_UTCTIME:
905
130k
    case V_ASN1_GENERALIZEDTIME:
906
130k
    case V_ASN1_GRAPHICSTRING:
907
130k
    case V_ASN1_VISIBLESTRING:
908
130k
    case V_ASN1_GENERALSTRING:
909
131k
    case V_ASN1_UNIVERSALSTRING:
910
131k
    case V_ASN1_BMPSTRING:
911
152k
    case V_ASN1_UTF8STRING:
912
152k
    case V_ASN1_OTHER:
913
152k
    case V_ASN1_SET:
914
169k
    case V_ASN1_SEQUENCE:
915
169k
    default:
916
169k
        if (utype == V_ASN1_BMPSTRING && (len & 1)) {
917
1
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
918
1
            goto err;
919
1
        }
920
169k
        if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
921
2
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
922
2
            goto err;
923
2
        }
924
169k
        if (utype == V_ASN1_GENERALIZEDTIME && (len < 15)) {
925
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_GENERALIZEDTIME_IS_TOO_SHORT);
926
0
            goto err;
927
0
        }
928
169k
        if (utype == V_ASN1_UTCTIME && (len < 13)) {
929
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UTCTIME_IS_TOO_SHORT);
930
0
            goto err;
931
0
        }
932
        /* All based on ASN1_STRING and handled the same */
933
169k
        if (*pval == NULL) {
934
32.7k
            stmp = ASN1_STRING_type_new(utype);
935
32.7k
            if (stmp == NULL) {
936
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
937
0
                goto err;
938
0
            }
939
32.7k
            *pval = (ASN1_VALUE *)stmp;
940
136k
        } else {
941
136k
            stmp = (ASN1_STRING *)*pval;
942
136k
            stmp->type = utype;
943
136k
        }
944
        /* If we've already allocated a buffer use it */
945
169k
        if (*free_cont) {
946
54
            ASN1_STRING_set0(stmp, (unsigned char *)cont /* UGLY CAST! */, len);
947
54
            *free_cont = 0;
948
169k
        } else {
949
169k
            if (!ASN1_STRING_set(stmp, cont, len)) {
950
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
951
0
                ASN1_STRING_free(stmp);
952
0
                *pval = NULL;
953
0
                goto err;
954
0
            }
955
169k
        }
956
169k
        break;
957
549k
    }
958
    /* If ASN1_ANY and NULL type fix up value */
959
549k
    if (typ && (utype == V_ASN1_NULL))
960
27.1k
        typ->value.ptr = NULL;
961
962
549k
    ret = 1;
963
549k
 err:
964
549k
    if (!ret) {
965
12
        ASN1_TYPE_free(typ);
966
12
        if (opval)
967
0
            *opval = NULL;
968
12
    }
969
549k
    return ret;
970
549k
}
971
972
/*
973
 * This function finds the end of an ASN1 structure when passed its maximum
974
 * length, whether it is indefinite length and a pointer to the content. This
975
 * is more efficient than calling asn1_collect because it does not recurse on
976
 * each indefinite length header.
977
 */
978
979
static int asn1_find_end(const unsigned char **in, long len, char inf)
980
24
{
981
24
    uint32_t expected_eoc;
982
24
    long plen;
983
24
    const unsigned char *p = *in, *q;
984
    /* If not indefinite length constructed just add length */
985
24
    if (inf == 0) {
986
0
        *in += len;
987
0
        return 1;
988
0
    }
989
24
    expected_eoc = 1;
990
    /*
991
     * Indefinite length constructed form. Find the end when enough EOCs are
992
     * found. If more indefinite length constructed headers are encountered
993
     * increment the expected eoc count otherwise just skip to the end of the
994
     * data.
995
     */
996
134
    while (len > 0) {
997
129
        if (asn1_check_eoc(&p, len)) {
998
20
            expected_eoc--;
999
20
            if (expected_eoc == 0)
1000
4
                break;
1001
16
            len -= 2;
1002
16
            continue;
1003
20
        }
1004
109
        q = p;
1005
        /* Just read in a header: only care about the length */
1006
109
        if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1007
109
                             -1, 0, 0, NULL)) {
1008
15
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1009
15
            return 0;
1010
15
        }
1011
94
        if (inf) {
1012
51
            if (expected_eoc == UINT32_MAX) {
1013
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1014
0
                return 0;
1015
0
            }
1016
51
            expected_eoc++;
1017
51
        } else {
1018
43
            p += plen;
1019
43
        }
1020
94
        len -= p - q;
1021
94
    }
1022
9
    if (expected_eoc) {
1023
5
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1024
5
        return 0;
1025
5
    }
1026
4
    *in = p;
1027
4
    return 1;
1028
9
}
1029
1030
/*
1031
 * This function collects the asn1 data from a constructed string type into
1032
 * a buffer. The values of 'in' and 'len' should refer to the contents of the
1033
 * constructed type and 'inf' should be set if it is indefinite length.
1034
 */
1035
1036
#ifndef ASN1_MAX_STRING_NEST
1037
/*
1038
 * This determines how many levels of recursion are permitted in ASN1 string
1039
 * types. If it is not limited stack overflows can occur. If set to zero no
1040
 * recursion is allowed at all. Although zero should be adequate examples
1041
 * exist that require a value of 1. So 5 should be more than enough.
1042
 */
1043
125
# define ASN1_MAX_STRING_NEST 5
1044
#endif
1045
1046
static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1047
                        char inf, int tag, int aclass, int depth)
1048
212
{
1049
212
    const unsigned char *p, *q;
1050
212
    long plen;
1051
212
    char cst, ininf;
1052
212
    p = *in;
1053
212
    inf &= 1;
1054
    /*
1055
     * If no buffer and not indefinite length constructed just pass over the
1056
     * encoded data
1057
     */
1058
212
    if (!buf && !inf) {
1059
0
        *in += len;
1060
0
        return 1;
1061
0
    }
1062
412
    while (len > 0) {
1063
268
        q = p;
1064
        /* Check for EOC */
1065
268
        if (asn1_check_eoc(&p, len)) {
1066
            /*
1067
             * EOC is illegal outside indefinite length constructed form
1068
             */
1069
22
            if (!inf) {
1070
1
                ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
1071
1
                return 0;
1072
1
            }
1073
21
            inf = 0;
1074
21
            break;
1075
22
        }
1076
1077
246
        if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1078
246
                             len, tag, aclass, 0, NULL)) {
1079
22
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1080
22
            return 0;
1081
22
        }
1082
1083
        /* If indefinite length constructed update max length */
1084
224
        if (cst) {
1085
125
            if (depth >= ASN1_MAX_STRING_NEST) {
1086
1
                ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_ASN1_STRING);
1087
1
                return 0;
1088
1
            }
1089
124
            if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1090
23
                return 0;
1091
124
        } else if (plen && !collect_data(buf, &p, plen))
1092
0
            return 0;
1093
200
        len -= p - q;
1094
200
    }
1095
165
    if (inf) {
1096
7
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1097
7
        return 0;
1098
7
    }
1099
158
    *in = p;
1100
158
    return 1;
1101
165
}
1102
1103
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1104
70
{
1105
70
    int len;
1106
70
    if (buf) {
1107
70
        len = buf->length;
1108
70
        if (!BUF_MEM_grow_clean(buf, len + plen)) {
1109
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
1110
0
            return 0;
1111
0
        }
1112
70
        memcpy(buf->data + len, *p, plen);
1113
70
    }
1114
70
    *p += plen;
1115
70
    return 1;
1116
70
}
1117
1118
/* Check for ASN1 EOC and swallow it if found */
1119
1120
static int asn1_check_eoc(const unsigned char **in, long len)
1121
1.04M
{
1122
1.04M
    const unsigned char *p;
1123
1124
1.04M
    if (len < 2)
1125
85
        return 0;
1126
1.04M
    p = *in;
1127
1.04M
    if (p[0] == '\0' && p[1] == '\0') {
1128
609
        *in += 2;
1129
609
        return 1;
1130
609
    }
1131
1.04M
    return 0;
1132
1.04M
}
1133
1134
/*
1135
 * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the
1136
 * length for indefinite length constructed form, we don't know the exact
1137
 * length but we can set an upper bound to the amount of data available minus
1138
 * the header length just read.
1139
 */
1140
1141
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1142
                           char *inf, char *cst,
1143
                           const unsigned char **in, long len,
1144
                           int exptag, int expclass, char opt, ASN1_TLC *ctx)
1145
1.39M
{
1146
1.39M
    int i;
1147
1.39M
    int ptag, pclass;
1148
1.39M
    long plen;
1149
1.39M
    const unsigned char *p, *q;
1150
1.39M
    p = *in;
1151
1.39M
    q = p;
1152
1153
1.39M
    if (len <= 0) {
1154
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
1155
0
        goto err;
1156
0
    }
1157
1.39M
    if (ctx != NULL && ctx->valid) {
1158
246k
        i = ctx->ret;
1159
246k
        plen = ctx->plen;
1160
246k
        pclass = ctx->pclass;
1161
246k
        ptag = ctx->ptag;
1162
246k
        p += ctx->hdrlen;
1163
1.14M
    } else {
1164
1.14M
        i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1165
1.14M
        if (ctx != NULL) {
1166
1.14M
            ctx->ret = i;
1167
1.14M
            ctx->plen = plen;
1168
1.14M
            ctx->pclass = pclass;
1169
1.14M
            ctx->ptag = ptag;
1170
1.14M
            ctx->hdrlen = p - q;
1171
1.14M
            ctx->valid = 1;
1172
            /*
1173
             * If definite length, and no error, length + header can't exceed
1174
             * total amount of data available.
1175
             */
1176
1.14M
            if ((i & 0x81) == 0 && (plen + ctx->hdrlen) > len) {
1177
0
                ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG);
1178
0
                goto err;
1179
0
            }
1180
1.14M
        }
1181
1.14M
    }
1182
1183
1.39M
    if ((i & 0x80) != 0) {
1184
213
        ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER);
1185
213
        goto err;
1186
213
    }
1187
1.39M
    if (exptag >= 0) {
1188
1.26M
        if (exptag != ptag || expclass != pclass) {
1189
            /*
1190
             * If type is OPTIONAL, not an error: indicate missing type.
1191
             */
1192
121k
            if (opt != 0)
1193
121k
                return -1;
1194
121k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG);
1195
30
            goto err;
1196
121k
        }
1197
        /*
1198
         * We have a tag and class match: assume we are going to do something
1199
         * with it
1200
         */
1201
1.14M
        asn1_tlc_clear(ctx);
1202
1.14M
    }
1203
1204
1.26M
    if ((i & 1) != 0)
1205
1.52k
        plen = len - (p - q);
1206
1207
1.26M
    if (inf != NULL)
1208
1.14M
        *inf = i & 1;
1209
1210
1.26M
    if (cst != NULL)
1211
1.02M
        *cst = i & V_ASN1_CONSTRUCTED;
1212
1213
1.26M
    if (olen != NULL)
1214
1.14M
        *olen = plen;
1215
1216
1.26M
    if (oclass != NULL)
1217
125k
        *oclass = pclass;
1218
1219
1.26M
    if (otag != NULL)
1220
125k
        *otag = ptag;
1221
1222
1.26M
    *in = p;
1223
1.26M
    return 1;
1224
1225
243
 err:
1226
243
    asn1_tlc_clear(ctx);
1227
243
    return 0;
1228
1.39M
}