Coverage Report

Created: 2025-04-22 06:18

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