Coverage Report

Created: 2026-06-18 06:34

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