Coverage Report

Created: 2025-08-28 07:07

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