Coverage Report

Created: 2023-09-25 06:45

/src/openssl30/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
95.3M
#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
18.1M
{
89
18.1M
    if ((tag < 0) || (tag > 30))
90
165k
        return 0;
91
18.0M
    return tag2bit[tag];
92
18.1M
}
93
94
/* Macro to initialize and invalidate the cache */
95
96
88.8M
#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
7.60M
#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
9.31M
{
112
9.31M
    int rv;
113
114
9.31M
    if (pval == NULL || it == NULL) {
115
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
116
0
        return 0;
117
0
    }
118
9.31M
    rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0,
119
9.31M
                             libctx, propq);
120
9.31M
    if (rv <= 0)
121
5.33M
        ASN1_item_ex_free(pval, it);
122
9.31M
    return rv;
123
9.31M
}
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
1.71M
{
129
1.71M
    return asn1_item_ex_d2i_intern(pval, in, len, it, tag, aclass, opt, ctx,
130
1.71M
                                   NULL, NULL);
131
1.71M
}
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
7.60M
{
138
7.60M
    ASN1_TLC c;
139
7.60M
    ASN1_VALUE *ptmpval = NULL;
140
141
7.60M
    if (pval == NULL)
142
6.72M
        pval = &ptmpval;
143
7.60M
    asn1_tlc_clear_nc(&c);
144
7.60M
    if (asn1_item_ex_d2i_intern(pval, in, len, it, -1, 0, 0, &c, libctx,
145
7.60M
                                propq) > 0)
146
2.48M
        return *pval;
147
5.11M
    return NULL;
148
7.60M
}
149
150
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
151
                          const unsigned char **in, long len,
152
                          const ASN1_ITEM *it)
153
7.10M
{
154
7.10M
    return ASN1_item_d2i_ex(pval, in, len, it, NULL, NULL);
155
7.10M
}
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
95.3M
{
168
95.3M
    const ASN1_TEMPLATE *tt, *errtt = NULL;
169
95.3M
    const ASN1_EXTERN_FUNCS *ef;
170
95.3M
    const ASN1_AUX *aux;
171
95.3M
    ASN1_aux_cb *asn1_cb;
172
95.3M
    const unsigned char *p = NULL, *q;
173
95.3M
    unsigned char oclass;
174
95.3M
    char seq_eoc, seq_nolen, cst, isopt;
175
95.3M
    long tmplen;
176
95.3M
    int i;
177
95.3M
    int otag;
178
95.3M
    int ret = 0;
179
95.3M
    ASN1_VALUE **pchptr;
180
181
95.3M
    if (pval == NULL || it == NULL) {
182
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
183
0
        return 0;
184
0
    }
185
95.3M
    if (len <= 0) {
186
29.4k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
187
29.4k
        return 0;
188
29.4k
    }
189
95.3M
    aux = it->funcs;
190
95.3M
    if (aux && aux->asn1_cb)
191
3.94M
        asn1_cb = aux->asn1_cb;
192
91.4M
    else
193
91.4M
        asn1_cb = 0;
194
195
95.3M
    if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
196
5
        ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_TOO_DEEP);
197
5
        goto err;
198
5
    }
199
200
95.3M
    switch (it->itype) {
201
71.2M
    case ASN1_ITYPE_PRIMITIVE:
202
71.2M
        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
44.5M
            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
44.5M
            return asn1_template_ex_d2i(pval, in, len, it->templates, opt, ctx,
215
44.5M
                                        depth, libctx, propq);
216
44.5M
        }
217
26.7M
        return asn1_d2i_ex_primitive(pval, in, len, it,
218
26.7M
                                     tag, aclass, opt, ctx);
219
220
5.06M
    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
5.06M
        if (tag != -1) {
226
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
227
0
            goto err;
228
0
        }
229
230
5.06M
        p = *in;
231
        /* Just read in tag and class */
232
5.06M
        ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
233
5.06M
                              &p, len, -1, 0, 1, ctx);
234
5.06M
        if (!ret) {
235
6.61k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
236
6.61k
            goto err;
237
6.61k
        }
238
239
        /* Must be UNIVERSAL class */
240
5.05M
        if (oclass != V_ASN1_UNIVERSAL) {
241
            /* If OPTIONAL, assume this is OK */
242
70.2k
            if (opt)
243
56.8k
                return -1;
244
70.2k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
245
13.3k
            goto err;
246
70.2k
        }
247
248
        /* Check tag matches bit map */
249
4.98M
        if (!(ASN1_tag2bit(otag) & it->utype)) {
250
            /* If OPTIONAL, assume this is OK */
251
140k
            if (opt)
252
7.44k
                return -1;
253
140k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_WRONG_TAG);
254
132k
            goto err;
255
140k
        }
256
4.84M
        return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
257
258
1.71M
    case ASN1_ITYPE_EXTERN:
259
        /* Use new style d2i */
260
1.71M
        ef = it->funcs;
261
1.71M
        if (ef->asn1_ex_d2i_ex != NULL)
262
863k
            return ef->asn1_ex_d2i_ex(pval, in, len, it, tag, aclass, opt, ctx,
263
863k
                                      libctx, propq);
264
851k
        return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
265
266
1.43M
    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
1.43M
        if (tag != -1) {
272
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
273
0
            goto err;
274
0
        }
275
276
1.43M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
277
0
            goto auxerr;
278
1.43M
        if (*pval) {
279
            /* Free up and zero CHOICE value if initialised */
280
261k
            i = ossl_asn1_get_choice_selector(pval, it);
281
261k
            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
1.17M
        } 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
1.43M
        p = *in;
293
5.16M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
294
4.93M
            pchptr = ossl_asn1_get_field_ptr(pval, tt);
295
            /*
296
             * We mark field as OPTIONAL so its absence can be recognised.
297
             */
298
4.93M
            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth,
299
4.93M
                                       libctx, propq);
300
            /* If field not present, try the next one */
301
4.93M
            if (ret == -1)
302
3.73M
                continue;
303
            /* If positive return, read OK, break loop */
304
1.20M
            if (ret > 0)
305
868k
                break;
306
            /*
307
             * Must be an ASN1 parsing error.
308
             * Free up any partial choice value
309
             */
310
337k
            ossl_asn1_template_free(pchptr, tt);
311
337k
            errtt = tt;
312
337k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
313
337k
            goto err;
314
1.20M
        }
315
316
        /* Did we fall off the end without reading anything? */
317
1.10M
        if (i == it->tcount) {
318
            /* If OPTIONAL, this is OK */
319
232k
            if (opt) {
320
                /* Free and zero it */
321
14.6k
                ASN1_item_ex_free(pval, it);
322
14.6k
                return -1;
323
14.6k
            }
324
232k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
325
217k
            goto err;
326
232k
        }
327
328
868k
        ossl_asn1_set_choice_selector(pval, i, it);
329
330
868k
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
331
0
            goto auxerr;
332
868k
        *in = p;
333
868k
        return 1;
334
335
284k
    case ASN1_ITYPE_NDEF_SEQUENCE:
336
15.8M
    case ASN1_ITYPE_SEQUENCE:
337
15.8M
        p = *in;
338
15.8M
        tmplen = len;
339
340
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
341
15.8M
        if (tag == -1) {
342
14.7M
            tag = V_ASN1_SEQUENCE;
343
14.7M
            aclass = V_ASN1_UNIVERSAL;
344
14.7M
        }
345
        /* Get SEQUENCE length and update len, p */
346
15.8M
        ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
347
15.8M
                              &p, len, tag, aclass, opt, ctx);
348
15.8M
        if (!ret) {
349
1.34M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
350
1.34M
            goto err;
351
14.5M
        } else if (ret == -1)
352
1.19M
            return -1;
353
13.3M
        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
13.3M
        else
359
13.3M
            seq_nolen = seq_eoc;
360
13.3M
        if (!cst) {
361
47.6k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
362
47.6k
            goto err;
363
47.6k
        }
364
365
13.2M
        if (*pval == NULL
366
13.2M
                && !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
13.2M
        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
51.2M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
376
37.9M
            if (tt->flags & ASN1_TFLG_ADB_MASK) {
377
237k
                const ASN1_TEMPLATE *seqtt;
378
237k
                ASN1_VALUE **pseqval;
379
237k
                seqtt = ossl_asn1_do_adb(*pval, tt, 0);
380
237k
                if (seqtt == NULL)
381
191k
                    continue;
382
46.0k
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
383
46.0k
                ossl_asn1_template_free(pseqval, seqtt);
384
46.0k
            }
385
37.9M
        }
386
387
        /* Get each field entry */
388
38.1M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
389
29.7M
            const ASN1_TEMPLATE *seqtt;
390
29.7M
            ASN1_VALUE **pseqval;
391
29.7M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
392
29.7M
            if (seqtt == NULL)
393
0
                goto err;
394
29.7M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
395
            /* Have we ran out of data? */
396
29.7M
            if (!len)
397
663k
                break;
398
29.0M
            q = p;
399
29.0M
            if (asn1_check_eoc(&p, len)) {
400
1.14M
                if (!seq_eoc) {
401
8.20k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
402
8.20k
                    goto err;
403
8.20k
                }
404
1.14M
                len -= p - q;
405
1.14M
                seq_eoc = 0;
406
1.14M
                break;
407
1.14M
            }
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
27.8M
            if (i == (it->tcount - 1))
415
8.95M
                isopt = 0;
416
18.9M
            else
417
18.9M
                isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
418
            /*
419
             * attempt to read in field, allowing each to be OPTIONAL
420
             */
421
422
27.8M
            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
423
27.8M
                                       depth, libctx, propq);
424
27.8M
            if (!ret) {
425
3.04M
                errtt = seqtt;
426
3.04M
                goto err;
427
24.8M
            } else if (ret == -1) {
428
                /*
429
                 * OPTIONAL component absent. Free and zero the field.
430
                 */
431
2.85M
                ossl_asn1_template_free(pseqval, seqtt);
432
2.85M
                continue;
433
2.85M
            }
434
            /* Update length */
435
22.0M
            len -= p - q;
436
22.0M
        }
437
438
        /* Check for EOC if expecting one */
439
10.2M
        if (seq_eoc && !asn1_check_eoc(&p, len)) {
440
73.6k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
441
73.6k
            goto err;
442
73.6k
        }
443
        /* Check all data read */
444
10.1M
        if (!seq_nolen && len) {
445
7.45k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
446
7.45k
            goto err;
447
7.45k
        }
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
13.5M
        for (; i < it->tcount; tt++, i++) {
455
3.43M
            const ASN1_TEMPLATE *seqtt;
456
3.43M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
457
3.43M
            if (seqtt == NULL)
458
0
                goto err;
459
3.43M
            if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
460
3.36M
                ASN1_VALUE **pseqval;
461
3.36M
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
462
3.36M
                ossl_asn1_template_free(pseqval, seqtt);
463
3.36M
            } else {
464
78.1k
                errtt = seqtt;
465
78.1k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_FIELD_MISSING);
466
78.1k
                goto err;
467
78.1k
            }
468
3.43M
        }
469
        /* Save encoding */
470
10.0M
        if (!ossl_asn1_enc_save(pval, *in, p - *in, it))
471
0
            goto auxerr;
472
10.0M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
473
172
            goto auxerr;
474
10.0M
        *in = p;
475
10.0M
        return 1;
476
477
0
    default:
478
0
        return 0;
479
95.3M
    }
480
172
 auxerr:
481
172
    ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
482
5.31M
 err:
483
5.31M
    if (errtt)
484
3.45M
        ERR_add_error_data(4, "Field=", errtt->field_name,
485
3.45M
                           ", Type=", it->sname);
486
1.85M
    else
487
1.85M
        ERR_add_error_data(2, "Type=", it->sname);
488
5.31M
    return 0;
489
172
}
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
109M
{
502
109M
    int flags, aclass;
503
109M
    int ret;
504
109M
    long len;
505
109M
    const unsigned char *p, *q;
506
109M
    char exp_eoc;
507
109M
    if (!val)
508
0
        return 0;
509
109M
    flags = tt->flags;
510
109M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
511
512
109M
    p = *in;
513
514
    /* Check if EXPLICIT tag expected */
515
109M
    if (flags & ASN1_TFLG_EXPTAG) {
516
3.07M
        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
3.07M
        ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
522
3.07M
                              &p, inlen, tt->tag, aclass, opt, ctx);
523
3.07M
        q = p;
524
3.07M
        if (!ret) {
525
170k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
526
170k
            return 0;
527
2.90M
        } else if (ret == -1)
528
2.19M
            return -1;
529
703k
        if (!cst) {
530
3.89k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
531
3.89k
            return 0;
532
3.89k
        }
533
        /* We've found the field so it can't be OPTIONAL now */
534
699k
        ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth, libctx,
535
699k
                                      propq);
536
699k
        if (!ret) {
537
183k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
538
183k
            return 0;
539
183k
        }
540
        /* We read the field in OK so update length */
541
516k
        len -= p - q;
542
516k
        if (exp_eoc) {
543
            /* If NDEF we must have an EOC here */
544
256k
            if (!asn1_check_eoc(&p, len)) {
545
22.6k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
546
22.6k
                goto err;
547
22.6k
            }
548
259k
        } else {
549
            /*
550
             * Otherwise we must hit the EXPLICIT tag end or its an error
551
             */
552
259k
            if (len) {
553
2.73k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
554
2.73k
                goto err;
555
2.73k
            }
556
259k
        }
557
516k
    } else
558
106M
        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth,
559
106M
                                       libctx, propq);
560
561
490k
    *in = p;
562
490k
    return 1;
563
564
25.3k
 err:
565
25.3k
    return 0;
566
109M
}
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
75.4M
{
574
75.4M
    int flags, aclass;
575
75.4M
    int ret;
576
75.4M
    ASN1_VALUE *tval;
577
75.4M
    const unsigned char *p, *q;
578
75.4M
    if (!val)
579
0
        return 0;
580
75.4M
    flags = tt->flags;
581
75.4M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
582
583
75.4M
    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
75.4M
    if (tt->flags & ASN1_TFLG_EMBED) {
590
3.47M
        tval = (ASN1_VALUE *)val;
591
3.47M
        val = &tval;
592
3.47M
    }
593
594
75.4M
    if (flags & ASN1_TFLG_SK_MASK) {
595
        /* SET OF, SEQUENCE OF */
596
45.4M
        int sktag, skaclass;
597
45.4M
        char sk_eoc;
598
        /* First work out expected inner tag value */
599
45.4M
        if (flags & ASN1_TFLG_IMPTAG) {
600
404k
            sktag = tt->tag;
601
404k
            skaclass = aclass;
602
45.0M
        } else {
603
45.0M
            skaclass = V_ASN1_UNIVERSAL;
604
45.0M
            if (flags & ASN1_TFLG_SET_OF)
605
43.1M
                sktag = V_ASN1_SET;
606
1.85M
            else
607
1.85M
                sktag = V_ASN1_SEQUENCE;
608
45.0M
        }
609
        /* Get the tag */
610
45.4M
        ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
611
45.4M
                              &p, len, sktag, skaclass, opt, ctx);
612
45.4M
        if (!ret) {
613
387k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
614
387k
            return 0;
615
45.0M
        } else if (ret == -1)
616
238k
            return -1;
617
44.8M
        if (*val == NULL)
618
44.7M
            *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
619
93.1k
        else {
620
            /*
621
             * We've got a valid STACK: free up any items present
622
             */
623
93.1k
            STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
624
93.1k
            ASN1_VALUE *vtmp;
625
93.1k
            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
93.1k
        }
630
631
44.8M
        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
100M
        while (len > 0) {
638
56.7M
            ASN1_VALUE *skfield;
639
56.7M
            q = p;
640
            /* See if EOC found */
641
56.7M
            if (asn1_check_eoc(&p, len)) {
642
690k
                if (!sk_eoc) {
643
2.87k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
644
2.87k
                    goto err;
645
2.87k
                }
646
687k
                len -= p - q;
647
687k
                sk_eoc = 0;
648
687k
                break;
649
690k
            }
650
56.0M
            skfield = NULL;
651
56.0M
            if (asn1_item_embed_d2i(&skfield, &p, len,
652
56.0M
                                     ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
653
56.0M
                                     depth, libctx, propq) <= 0) {
654
505k
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
655
                /* |skfield| may be partially allocated despite failure. */
656
505k
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
657
505k
                goto err;
658
505k
            }
659
55.5M
            len -= p - q;
660
55.5M
            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
55.5M
        }
666
44.3M
        if (sk_eoc) {
667
5.49k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
668
5.49k
            goto err;
669
5.49k
        }
670
44.3M
    } else if (flags & ASN1_TFLG_IMPTAG) {
671
        /* IMPLICIT tagging */
672
3.75M
        ret = asn1_item_embed_d2i(val, &p, len,
673
3.75M
                                  ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
674
3.75M
                                  ctx, depth, libctx, propq);
675
3.75M
        if (!ret) {
676
165k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
677
165k
            goto err;
678
3.58M
        } else if (ret == -1)
679
2.98M
            return -1;
680
26.2M
    } else {
681
        /* Nothing special */
682
26.2M
        ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
683
26.2M
                                  -1, 0, opt, ctx, depth, libctx, propq);
684
26.2M
        if (!ret) {
685
2.74M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
686
2.74M
            goto err;
687
23.5M
        } else if (ret == -1)
688
1.54M
            return -1;
689
26.2M
    }
690
691
66.8M
    *in = p;
692
66.8M
    return 1;
693
694
3.42M
 err:
695
3.42M
    return 0;
696
75.4M
}
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
31.5M
{
703
31.5M
    int ret = 0, utype;
704
31.5M
    long plen;
705
31.5M
    char cst, inf, free_cont = 0;
706
31.5M
    const unsigned char *p;
707
31.5M
    BUF_MEM buf = { 0, NULL, 0, 0 };
708
31.5M
    const unsigned char *cont = NULL;
709
31.5M
    long len;
710
711
31.5M
    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
31.5M
    if (it->itype == ASN1_ITYPE_MSTRING) {
717
4.84M
        utype = tag;
718
4.84M
        tag = -1;
719
4.84M
    } else
720
26.7M
        utype = it->utype;
721
722
31.5M
    if (utype == V_ASN1_ANY) {
723
        /* If type is ANY need to figure out type from tag */
724
5.89M
        unsigned char oclass;
725
5.89M
        if (tag >= 0) {
726
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
727
0
            return 0;
728
0
        }
729
5.89M
        if (opt) {
730
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
731
0
            return 0;
732
0
        }
733
5.89M
        p = *in;
734
5.89M
        ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
735
5.89M
                              &p, inlen, -1, 0, 0, ctx);
736
5.89M
        if (!ret) {
737
5.50k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
738
5.50k
            return 0;
739
5.50k
        }
740
5.89M
        if (oclass != V_ASN1_UNIVERSAL)
741
158k
            utype = V_ASN1_OTHER;
742
5.89M
    }
743
31.5M
    if (tag == -1) {
744
28.9M
        tag = utype;
745
28.9M
        aclass = V_ASN1_UNIVERSAL;
746
28.9M
    }
747
31.5M
    p = *in;
748
    /* Check header */
749
31.5M
    ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
750
31.5M
                          &p, inlen, tag, aclass, opt, ctx);
751
31.5M
    if (!ret) {
752
2.35M
        ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
753
2.35M
        return 0;
754
29.1M
    } else if (ret == -1)
755
3.25M
        return -1;
756
25.9M
    ret = 0;
757
    /* SEQUENCE, SET and "OTHER" are left in encoded form */
758
25.9M
    if ((utype == V_ASN1_SEQUENCE)
759
25.9M
        || (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
4.08M
        if (utype == V_ASN1_OTHER) {
765
158k
            asn1_tlc_clear(ctx);
766
158k
        }
767
        /* SEQUENCE and SET must be constructed */
768
3.92M
        else if (!cst) {
769
2.97k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
770
2.97k
            return 0;
771
2.97k
        }
772
773
4.08M
        cont = *in;
774
        /* If indefinite length constructed find the real end */
775
4.08M
        if (inf) {
776
743k
            if (!asn1_find_end(&p, plen, inf))
777
56.9k
                goto err;
778
686k
            len = p - cont;
779
3.34M
        } else {
780
3.34M
            len = p - cont + plen;
781
3.34M
            p += plen;
782
3.34M
        }
783
21.8M
    } else if (cst) {
784
2.56M
        if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
785
2.56M
            || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
786
2.56M
            || utype == V_ASN1_ENUMERATED) {
787
8.74k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
788
8.74k
            return 0;
789
8.74k
        }
790
791
        /* Free any returned 'buf' content */
792
2.55M
        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
2.55M
        if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
800
13.3k
            goto err;
801
13.3k
        }
802
2.54M
        len = buf.length;
803
        /* Append a final null to string */
804
2.54M
        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
2.54M
        buf.data[len] = 0;
809
2.54M
        cont = (const unsigned char *)buf.data;
810
19.2M
    } else {
811
19.2M
        cont = p;
812
19.2M
        len = plen;
813
19.2M
        p += plen;
814
19.2M
    }
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
25.8M
    if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
819
175k
        goto err;
820
821
25.6M
    *in = p;
822
25.6M
    ret = 1;
823
25.9M
 err:
824
25.9M
    if (free_cont)
825
92.2k
        OPENSSL_free(buf.data);
826
25.9M
    return ret;
827
25.6M
}
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
25.8M
{
834
25.8M
    ASN1_VALUE **opval = NULL;
835
25.8M
    ASN1_STRING *stmp;
836
25.8M
    ASN1_TYPE *typ = NULL;
837
25.8M
    int ret = 0;
838
25.8M
    const ASN1_PRIMITIVE_FUNCS *pf;
839
25.8M
    ASN1_INTEGER **tint;
840
25.8M
    pf = it->funcs;
841
842
25.8M
    if (pf && pf->prim_c2i)
843
1.08M
        return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
844
    /* If ANY type clear type and set pointer to internal value */
845
24.7M
    if (it->utype == V_ASN1_ANY) {
846
5.84M
        if (*pval == NULL) {
847
5.81M
            typ = ASN1_TYPE_new();
848
5.81M
            if (typ == NULL)
849
0
                goto err;
850
5.81M
            *pval = (ASN1_VALUE *)typ;
851
5.81M
        } else
852
33.2k
            typ = (ASN1_TYPE *)*pval;
853
854
5.84M
        if (utype != typ->type)
855
5.84M
            ASN1_TYPE_set(typ, utype, NULL);
856
5.84M
        opval = pval;
857
5.84M
        pval = &typ->value.asn1_value;
858
5.84M
    }
859
24.7M
    switch (utype) {
860
7.86M
    case V_ASN1_OBJECT:
861
7.86M
        if (!ossl_c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
862
15.0k
            goto err;
863
7.84M
        break;
864
865
7.84M
    case V_ASN1_NULL:
866
541k
        if (len) {
867
1.90k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
868
1.90k
            goto err;
869
1.90k
        }
870
539k
        *pval = (ASN1_VALUE *)1;
871
539k
        break;
872
873
114k
    case V_ASN1_BOOLEAN:
874
114k
        if (len != 1) {
875
2.51k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
876
2.51k
            goto err;
877
111k
        } else {
878
111k
            ASN1_BOOLEAN *tbool;
879
111k
            tbool = (ASN1_BOOLEAN *)pval;
880
111k
            *tbool = *cont;
881
111k
        }
882
111k
        break;
883
884
2.46M
    case V_ASN1_BIT_STRING:
885
2.46M
        if (!ossl_c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
886
5.95k
            goto err;
887
2.45M
        break;
888
889
3.47M
    case V_ASN1_INTEGER:
890
3.52M
    case V_ASN1_ENUMERATED:
891
3.52M
        tint = (ASN1_INTEGER **)pval;
892
3.52M
        if (!ossl_c2i_ASN1_INTEGER(tint, &cont, len))
893
118k
            goto err;
894
        /* Fixup type to match the expected form */
895
3.40M
        (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
896
3.40M
        break;
897
898
1.31M
    case V_ASN1_OCTET_STRING:
899
2.39M
    case V_ASN1_NUMERICSTRING:
900
2.59M
    case V_ASN1_PRINTABLESTRING:
901
2.66M
    case V_ASN1_T61STRING:
902
2.67M
    case V_ASN1_VIDEOTEXSTRING:
903
3.20M
    case V_ASN1_IA5STRING:
904
3.63M
    case V_ASN1_UTCTIME:
905
3.92M
    case V_ASN1_GENERALIZEDTIME:
906
4.23M
    case V_ASN1_GRAPHICSTRING:
907
4.23M
    case V_ASN1_VISIBLESTRING:
908
4.23M
    case V_ASN1_GENERALSTRING:
909
4.45M
    case V_ASN1_UNIVERSALSTRING:
910
5.04M
    case V_ASN1_BMPSTRING:
911
5.41M
    case V_ASN1_UTF8STRING:
912
5.56M
    case V_ASN1_OTHER:
913
8.09M
    case V_ASN1_SET:
914
9.44M
    case V_ASN1_SEQUENCE:
915
10.2M
    default:
916
10.2M
        if (utype == V_ASN1_BMPSTRING && (len & 1)) {
917
312
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
918
312
            goto err;
919
312
        }
920
10.2M
        if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
921
357
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
922
357
            goto err;
923
357
        }
924
        /* All based on ASN1_STRING and handled the same */
925
10.2M
        if (*pval == NULL) {
926
5.41M
            stmp = ASN1_STRING_type_new(utype);
927
5.41M
            if (stmp == NULL) {
928
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
929
0
                goto err;
930
0
            }
931
5.41M
            *pval = (ASN1_VALUE *)stmp;
932
5.41M
        } else {
933
4.85M
            stmp = (ASN1_STRING *)*pval;
934
4.85M
            stmp->type = utype;
935
4.85M
        }
936
        /* If we've already allocated a buffer use it */
937
10.2M
        if (*free_cont) {
938
2.46M
            OPENSSL_free(stmp->data);
939
2.46M
            stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
940
2.46M
            stmp->length = len;
941
2.46M
            *free_cont = 0;
942
7.80M
        } else {
943
7.80M
            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
7.80M
        }
950
10.2M
        break;
951
24.7M
    }
952
    /* If ASN1_ANY and NULL type fix up value */
953
24.6M
    if (typ && (utype == V_ASN1_NULL))
954
535k
        typ->value.ptr = NULL;
955
956
24.6M
    ret = 1;
957
24.7M
 err:
958
24.7M
    if (!ret) {
959
144k
        ASN1_TYPE_free(typ);
960
144k
        if (opval)
961
7.96k
            *opval = NULL;
962
144k
    }
963
24.7M
    return ret;
964
24.6M
}
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
869k
{
975
869k
    uint32_t expected_eoc;
976
869k
    long plen;
977
869k
    const unsigned char *p = *in, *q;
978
    /* If not indefinite length constructed just add length */
979
869k
    if (inf == 0) {
980
0
        *in += len;
981
0
        return 1;
982
0
    }
983
869k
    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
229M
    while (len > 0) {
991
229M
        if (asn1_check_eoc(&p, len)) {
992
17.0M
            expected_eoc--;
993
17.0M
            if (expected_eoc == 0)
994
784k
                break;
995
16.3M
            len -= 2;
996
16.3M
            continue;
997
17.0M
        }
998
212M
        q = p;
999
        /* Just read in a header: only care about the length */
1000
212M
        if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1001
212M
                             -1, 0, 0, NULL)) {
1002
54.0k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1003
54.0k
            return 0;
1004
54.0k
        }
1005
212M
        if (inf) {
1006
41.6M
            if (expected_eoc == UINT32_MAX) {
1007
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1008
0
                return 0;
1009
0
            }
1010
41.6M
            expected_eoc++;
1011
170M
        } else {
1012
170M
            p += plen;
1013
170M
        }
1014
212M
        len -= p - q;
1015
212M
    }
1016
815k
    if (expected_eoc) {
1017
30.3k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1018
30.3k
        return 0;
1019
30.3k
    }
1020
784k
    *in = p;
1021
784k
    return 1;
1022
815k
}
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
796k
# 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
5.88M
{
1043
5.88M
    const unsigned char *p, *q;
1044
5.88M
    long plen;
1045
5.88M
    char cst, ininf;
1046
5.88M
    p = *in;
1047
5.88M
    inf &= 1;
1048
    /*
1049
     * If no buffer and not indefinite length constructed just pass over the
1050
     * encoded data
1051
     */
1052
5.88M
    if (!buf && !inf) {
1053
0
        *in += len;
1054
0
        return 1;
1055
0
    }
1056
14.7M
    while (len > 0) {
1057
9.23M
        q = p;
1058
        /* Check for EOC */
1059
9.23M
        if (asn1_check_eoc(&p, len)) {
1060
            /*
1061
             * EOC is illegal outside indefinite length constructed form
1062
             */
1063
306k
            if (!inf) {
1064
2.79k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
1065
2.79k
                return 0;
1066
2.79k
            }
1067
303k
            inf = 0;
1068
303k
            break;
1069
306k
        }
1070
1071
8.92M
        if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1072
8.92M
                             len, tag, aclass, 0, NULL)) {
1073
9.65k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1074
9.65k
            return 0;
1075
9.65k
        }
1076
1077
        /* If indefinite length constructed update max length */
1078
8.91M
        if (cst) {
1079
796k
            if (depth >= ASN1_MAX_STRING_NEST) {
1080
1.95k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_ASN1_STRING);
1081
1.95k
                return 0;
1082
1.95k
            }
1083
794k
            if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1084
17.9k
                return 0;
1085
8.12M
        } else if (plen && !collect_data(buf, &p, plen))
1086
0
            return 0;
1087
8.89M
        len -= p - q;
1088
8.89M
    }
1089
5.85M
    if (inf) {
1090
4.77k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1091
4.77k
        return 0;
1092
4.77k
    }
1093
5.84M
    *in = p;
1094
5.84M
    return 1;
1095
5.85M
}
1096
1097
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1098
7.24M
{
1099
7.24M
    int len;
1100
7.24M
    if (buf) {
1101
7.24M
        len = buf->length;
1102
7.24M
        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
7.24M
        memcpy(buf->data + len, *p, plen);
1107
7.24M
    }
1108
7.24M
    *p += plen;
1109
7.24M
    return 1;
1110
7.24M
}
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
249M
{
1116
249M
    const unsigned char *p;
1117
1118
249M
    if (len < 2)
1119
65.8k
        return 0;
1120
249M
    p = *in;
1121
249M
    if (p[0] == '\0' && p[1] == '\0') {
1122
18.7M
        *in += 2;
1123
18.7M
        return 1;
1124
18.7M
    }
1125
230M
    return 0;
1126
249M
}
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
253M
{
1140
253M
    int i;
1141
253M
    int ptag, pclass;
1142
253M
    long plen;
1143
253M
    const unsigned char *p, *q;
1144
253M
    p = *in;
1145
253M
    q = p;
1146
1147
253M
    if (len <= 0) {
1148
63
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
1149
63
        goto err;
1150
63
    }
1151
253M
    if (ctx != NULL && ctx->valid) {
1152
17.0M
        i = ctx->ret;
1153
17.0M
        plen = ctx->plen;
1154
17.0M
        pclass = ctx->pclass;
1155
17.0M
        ptag = ctx->ptag;
1156
17.0M
        p += ctx->hdrlen;
1157
235M
    } else {
1158
235M
        i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1159
235M
        if (ctx != NULL) {
1160
89.1M
            ctx->ret = i;
1161
89.1M
            ctx->plen = plen;
1162
89.1M
            ctx->pclass = pclass;
1163
89.1M
            ctx->ptag = ptag;
1164
89.1M
            ctx->hdrlen = p - q;
1165
89.1M
            ctx->valid = 1;
1166
            /*
1167
             * If definite length, and no error, length + header can't exceed
1168
             * total amount of data available.
1169
             */
1170
89.1M
            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
89.1M
        }
1175
235M
    }
1176
1177
253M
    if ((i & 0x80) != 0) {
1178
320k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER);
1179
320k
        goto err;
1180
320k
    }
1181
252M
    if (exptag >= 0) {
1182
94.8M
        if (exptag != ptag || expclass != pclass) {
1183
            /*
1184
             * If type is OPTIONAL, not an error: indicate missing type.
1185
             */
1186
10.4M
            if (opt != 0)
1187
6.50M
                return -1;
1188
10.4M
            ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG);
1189
3.93M
            goto err;
1190
10.4M
        }
1191
        /*
1192
         * We have a tag and class match: assume we are going to do something
1193
         * with it
1194
         */
1195
84.4M
        asn1_tlc_clear(ctx);
1196
84.4M
    }
1197
1198
242M
    if ((i & 1) != 0)
1199
44.0M
        plen = len - (p - q);
1200
1201
242M
    if (inf != NULL)
1202
231M
        *inf = i & 1;
1203
1204
242M
    if (cst != NULL)
1205
45.4M
        *cst = i & V_ASN1_CONSTRUCTED;
1206
1207
242M
    if (olen != NULL)
1208
231M
        *olen = plen;
1209
1210
242M
    if (oclass != NULL)
1211
10.9M
        *oclass = pclass;
1212
1213
242M
    if (otag != NULL)
1214
10.9M
        *otag = ptag;
1215
1216
242M
    *in = p;
1217
242M
    return 1;
1218
1219
4.25M
 err:
1220
4.25M
    asn1_tlc_clear(ctx);
1221
4.25M
    return 0;
1222
252M
}