Coverage Report

Created: 2025-10-28 06:56

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