Coverage Report

Created: 2024-07-27 06:39

/src/openssl31/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
243M
#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
30.8M
{
89
30.8M
    if ((tag < 0) || (tag > 30))
90
3.28k
        return 0;
91
30.7M
    return tag2bit[tag];
92
30.8M
}
93
94
/* Macro to initialize and invalidate the cache */
95
96
220M
#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
14.8M
#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
18.0M
{
112
18.0M
    int rv;
113
114
18.0M
    if (pval == NULL || it == NULL) {
115
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
116
0
        return 0;
117
0
    }
118
18.0M
    rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0,
119
18.0M
                             libctx, propq);
120
18.0M
    if (rv <= 0)
121
10.6M
        ASN1_item_ex_free(pval, it);
122
18.0M
    return rv;
123
18.0M
}
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
3.21M
{
129
3.21M
    return asn1_item_ex_d2i_intern(pval, in, len, it, tag, aclass, opt, ctx,
130
3.21M
                                   NULL, NULL);
131
3.21M
}
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
14.8M
{
138
14.8M
    ASN1_TLC c;
139
14.8M
    ASN1_VALUE *ptmpval = NULL;
140
141
14.8M
    if (pval == NULL)
142
13.2M
        pval = &ptmpval;
143
14.8M
    asn1_tlc_clear_nc(&c);
144
14.8M
    if (asn1_item_ex_d2i_intern(pval, in, len, it, -1, 0, 0, &c, libctx,
145
14.8M
                                propq) > 0)
146
4.59M
        return *pval;
147
10.2M
    return NULL;
148
14.8M
}
149
150
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
151
                          const unsigned char **in, long len,
152
                          const ASN1_ITEM *it)
153
13.9M
{
154
13.9M
    return ASN1_item_d2i_ex(pval, in, len, it, NULL, NULL);
155
13.9M
}
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
243M
{
168
243M
    const ASN1_TEMPLATE *tt, *errtt = NULL;
169
243M
    const ASN1_EXTERN_FUNCS *ef;
170
243M
    const ASN1_AUX *aux;
171
243M
    ASN1_aux_cb *asn1_cb;
172
243M
    const unsigned char *p = NULL, *q;
173
243M
    unsigned char oclass;
174
243M
    char seq_eoc, seq_nolen, cst, isopt;
175
243M
    long tmplen;
176
243M
    int i;
177
243M
    int otag;
178
243M
    int ret = 0;
179
243M
    ASN1_VALUE **pchptr;
180
181
243M
    if (pval == NULL || it == NULL) {
182
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
183
0
        return 0;
184
0
    }
185
243M
    if (len <= 0) {
186
54.3k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
187
54.3k
        return 0;
188
54.3k
    }
189
243M
    aux = it->funcs;
190
243M
    if (aux && aux->asn1_cb)
191
10.5M
        asn1_cb = aux->asn1_cb;
192
232M
    else
193
232M
        asn1_cb = 0;
194
195
243M
    if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
196
8
        ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_TOO_DEEP);
197
8
        goto err;
198
8
    }
199
200
243M
    switch (it->itype) {
201
180M
    case ASN1_ITYPE_PRIMITIVE:
202
180M
        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
99.1M
            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
99.1M
            return asn1_template_ex_d2i(pval, in, len, it->templates, opt, ctx,
215
99.1M
                                        depth, libctx, propq);
216
99.1M
        }
217
81.2M
        return asn1_d2i_ex_primitive(pval, in, len, it,
218
81.2M
                                     tag, aclass, opt, ctx);
219
220
15.0M
    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
15.0M
        if (tag != -1) {
226
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
227
0
            goto err;
228
0
        }
229
230
15.0M
        p = *in;
231
        /* Just read in tag and class */
232
15.0M
        ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
233
15.0M
                              &p, len, -1, 0, 1, ctx);
234
15.0M
        if (!ret) {
235
10.6k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
236
10.6k
            goto err;
237
10.6k
        }
238
239
        /* Must be UNIVERSAL class */
240
15.0M
        if (oclass != V_ASN1_UNIVERSAL) {
241
            /* If OPTIONAL, assume this is OK */
242
137k
            if (opt)
243
110k
                return -1;
244
137k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
245
27.3k
            goto err;
246
137k
        }
247
248
        /* Check tag matches bit map */
249
14.9M
        if (!(ASN1_tag2bit(otag) & it->utype)) {
250
            /* If OPTIONAL, assume this is OK */
251
284k
            if (opt)
252
10.8k
                return -1;
253
284k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_WRONG_TAG);
254
273k
            goto err;
255
284k
        }
256
14.6M
        return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
257
258
3.21M
    case ASN1_ITYPE_EXTERN:
259
        /* Use new style d2i */
260
3.21M
        ef = it->funcs;
261
3.21M
        if (ef->asn1_ex_d2i_ex != NULL)
262
1.58M
            return ef->asn1_ex_d2i_ex(pval, in, len, it, tag, aclass, opt, ctx,
263
1.58M
                                      libctx, propq);
264
1.62M
        return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
265
266
5.35M
    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
5.35M
        if (tag != -1) {
272
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
273
0
            goto err;
274
0
        }
275
276
5.35M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
277
0
            goto auxerr;
278
5.35M
        if (*pval) {
279
            /* Free up and zero CHOICE value if initialised */
280
494k
            i = ossl_asn1_get_choice_selector(pval, it);
281
494k
            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
4.85M
        } 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
5.35M
        p = *in;
293
20.3M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
294
19.9M
            pchptr = ossl_asn1_get_field_ptr(pval, tt);
295
            /*
296
             * We mark field as OPTIONAL so its absence can be recognised.
297
             */
298
19.9M
            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth,
299
19.9M
                                       libctx, propq);
300
            /* If field not present, try the next one */
301
19.9M
            if (ret == -1)
302
15.0M
                continue;
303
            /* If positive return, read OK, break loop */
304
4.88M
            if (ret > 0)
305
4.25M
                break;
306
            /*
307
             * Must be an ASN1 parsing error.
308
             * Free up any partial choice value
309
             */
310
626k
            ossl_asn1_template_free(pchptr, tt);
311
626k
            errtt = tt;
312
626k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
313
626k
            goto err;
314
4.88M
        }
315
316
        /* Did we fall off the end without reading anything? */
317
4.72M
        if (i == it->tcount) {
318
            /* If OPTIONAL, this is OK */
319
470k
            if (opt) {
320
                /* Free and zero it */
321
26.9k
                ASN1_item_ex_free(pval, it);
322
26.9k
                return -1;
323
26.9k
            }
324
470k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
325
443k
            goto err;
326
470k
        }
327
328
4.25M
        ossl_asn1_set_choice_selector(pval, i, it);
329
330
4.25M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
331
0
            goto auxerr;
332
4.25M
        *in = p;
333
4.25M
        return 1;
334
335
588k
    case ASN1_ITYPE_NDEF_SEQUENCE:
336
39.3M
    case ASN1_ITYPE_SEQUENCE:
337
39.3M
        p = *in;
338
39.3M
        tmplen = len;
339
340
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
341
39.3M
        if (tag == -1) {
342
37.0M
            tag = V_ASN1_SEQUENCE;
343
37.0M
            aclass = V_ASN1_UNIVERSAL;
344
37.0M
        }
345
        /* Get SEQUENCE length and update len, p */
346
39.3M
        ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
347
39.3M
                              &p, len, tag, aclass, opt, ctx);
348
39.3M
        if (!ret) {
349
2.65M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
350
2.65M
            goto err;
351
36.7M
        } else if (ret == -1)
352
4.89M
            return -1;
353
31.8M
        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
31.8M
        else
359
31.8M
            seq_nolen = seq_eoc;
360
31.8M
        if (!cst) {
361
94.3k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
362
94.3k
            goto err;
363
94.3k
        }
364
365
31.7M
        if (*pval == NULL
366
31.7M
                && !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
31.7M
        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
117M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
376
86.2M
            if (tt->flags & ASN1_TFLG_ADB_MASK) {
377
494k
                const ASN1_TEMPLATE *seqtt;
378
494k
                ASN1_VALUE **pseqval;
379
494k
                seqtt = ossl_asn1_do_adb(*pval, tt, 0);
380
494k
                if (seqtt == NULL)
381
401k
                    continue;
382
93.1k
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
383
93.1k
                ossl_asn1_template_free(pseqval, seqtt);
384
93.1k
            }
385
86.2M
        }
386
387
        /* Get each field entry */
388
92.7M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
389
70.4M
            const ASN1_TEMPLATE *seqtt;
390
70.4M
            ASN1_VALUE **pseqval;
391
70.4M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
392
70.4M
            if (seqtt == NULL)
393
0
                goto err;
394
70.4M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
395
            /* Have we ran out of data? */
396
70.4M
            if (!len)
397
1.52M
                break;
398
68.9M
            q = p;
399
68.9M
            if (asn1_check_eoc(&p, len)) {
400
1.98M
                if (!seq_eoc) {
401
14.0k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
402
14.0k
                    goto err;
403
14.0k
                }
404
1.97M
                len -= p - q;
405
1.97M
                seq_eoc = 0;
406
1.97M
                break;
407
1.98M
            }
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
66.9M
            if (i == (it->tcount - 1))
415
23.3M
                isopt = 0;
416
43.5M
            else
417
43.5M
                isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
418
            /*
419
             * attempt to read in field, allowing each to be OPTIONAL
420
             */
421
422
66.9M
            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
423
66.9M
                                       depth, libctx, propq);
424
66.9M
            if (!ret) {
425
5.96M
                errtt = seqtt;
426
5.96M
                goto err;
427
60.9M
            } else if (ret == -1) {
428
                /*
429
                 * OPTIONAL component absent. Free and zero the field.
430
                 */
431
5.99M
                ossl_asn1_template_free(pseqval, seqtt);
432
5.99M
                continue;
433
5.99M
            }
434
            /* Update length */
435
54.9M
            len -= p - q;
436
54.9M
        }
437
438
        /* Check for EOC if expecting one */
439
25.7M
        if (seq_eoc && !asn1_check_eoc(&p, len)) {
440
127k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
441
127k
            goto err;
442
127k
        }
443
        /* Check all data read */
444
25.6M
        if (!seq_nolen && len) {
445
22.8k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
446
22.8k
            goto err;
447
22.8k
        }
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
32.0M
        for (; i < it->tcount; tt++, i++) {
455
6.56M
            const ASN1_TEMPLATE *seqtt;
456
6.56M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
457
6.56M
            if (seqtt == NULL)
458
0
                goto err;
459
6.56M
            if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
460
6.40M
                ASN1_VALUE **pseqval;
461
6.40M
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
462
6.40M
                ossl_asn1_template_free(pseqval, seqtt);
463
6.40M
            } else {
464
162k
                errtt = seqtt;
465
162k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_FIELD_MISSING);
466
162k
                goto err;
467
162k
            }
468
6.56M
        }
469
        /* Save encoding */
470
25.4M
        if (!ossl_asn1_enc_save(pval, *in, p - *in, it))
471
0
            goto auxerr;
472
25.4M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
473
396
            goto auxerr;
474
25.4M
        *in = p;
475
25.4M
        return 1;
476
477
0
    default:
478
0
        return 0;
479
243M
    }
480
396
 auxerr:
481
396
    ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
482
10.4M
 err:
483
10.4M
    if (errtt)
484
6.74M
        ERR_add_error_data(4, "Field=", errtt->field_name,
485
6.74M
                           ", Type=", it->sname);
486
3.67M
    else
487
3.67M
        ERR_add_error_data(2, "Type=", it->sname);
488
10.4M
    return 0;
489
396
}
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
186M
{
502
186M
    int flags, aclass;
503
186M
    int ret;
504
186M
    long len;
505
186M
    const unsigned char *p, *q;
506
186M
    char exp_eoc;
507
186M
    if (!val)
508
0
        return 0;
509
186M
    flags = tt->flags;
510
186M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
511
512
186M
    p = *in;
513
514
    /* Check if EXPLICIT tag expected */
515
186M
    if (flags & ASN1_TFLG_EXPTAG) {
516
4.71M
        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
4.71M
        ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
522
4.71M
                              &p, inlen, tt->tag, aclass, opt, ctx);
523
4.71M
        q = p;
524
4.71M
        if (!ret) {
525
231k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
526
231k
            return 0;
527
4.48M
        } else if (ret == -1)
528
3.46M
            return -1;
529
1.02M
        if (!cst) {
530
5.13k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
531
5.13k
            return 0;
532
5.13k
        }
533
        /* We've found the field so it can't be OPTIONAL now */
534
1.01M
        ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth, libctx,
535
1.01M
                                      propq);
536
1.01M
        if (!ret) {
537
179k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
538
179k
            return 0;
539
179k
        }
540
        /* We read the field in OK so update length */
541
837k
        len -= p - q;
542
837k
        if (exp_eoc) {
543
            /* If NDEF we must have an EOC here */
544
317k
            if (!asn1_check_eoc(&p, len)) {
545
20.2k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
546
20.2k
                goto err;
547
20.2k
            }
548
520k
        } else {
549
            /*
550
             * Otherwise we must hit the EXPLICIT tag end or its an error
551
             */
552
520k
            if (len) {
553
3.50k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
554
3.50k
                goto err;
555
3.50k
            }
556
520k
        }
557
837k
    } else
558
181M
        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth,
559
181M
                                       libctx, propq);
560
561
813k
    *in = p;
562
813k
    return 1;
563
564
23.8k
 err:
565
23.8k
    return 0;
566
186M
}
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
182M
{
574
182M
    int flags, aclass;
575
182M
    int ret;
576
182M
    ASN1_VALUE *tval;
577
182M
    const unsigned char *p, *q;
578
182M
    if (!val)
579
0
        return 0;
580
182M
    flags = tt->flags;
581
182M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
582
583
182M
    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
182M
    if (tt->flags & ASN1_TFLG_EMBED) {
590
7.38M
        tval = (ASN1_VALUE *)val;
591
7.38M
        val = &tval;
592
7.38M
    }
593
594
182M
    if (flags & ASN1_TFLG_SK_MASK) {
595
        /* SET OF, SEQUENCE OF */
596
101M
        int sktag, skaclass;
597
101M
        char sk_eoc;
598
        /* First work out expected inner tag value */
599
101M
        if (flags & ASN1_TFLG_IMPTAG) {
600
778k
            sktag = tt->tag;
601
778k
            skaclass = aclass;
602
100M
        } else {
603
100M
            skaclass = V_ASN1_UNIVERSAL;
604
100M
            if (flags & ASN1_TFLG_SET_OF)
605
96.4M
                sktag = V_ASN1_SET;
606
4.00M
            else
607
4.00M
                sktag = V_ASN1_SEQUENCE;
608
100M
        }
609
        /* Get the tag */
610
101M
        ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
611
101M
                              &p, len, sktag, skaclass, opt, ctx);
612
101M
        if (!ret) {
613
812k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
614
812k
            return 0;
615
100M
        } else if (ret == -1)
616
499k
            return -1;
617
99.8M
        if (*val == NULL)
618
99.6M
            *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
619
244k
        else {
620
            /*
621
             * We've got a valid STACK: free up any items present
622
             */
623
244k
            STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
624
244k
            ASN1_VALUE *vtmp;
625
244k
            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
244k
        }
630
631
99.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
243M
        while (len > 0) {
638
148M
            ASN1_VALUE *skfield;
639
148M
            q = p;
640
            /* See if EOC found */
641
148M
            if (asn1_check_eoc(&p, len)) {
642
3.94M
                if (!sk_eoc) {
643
5.36k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
644
5.36k
                    goto err;
645
5.36k
                }
646
3.93M
                len -= p - q;
647
3.93M
                sk_eoc = 0;
648
3.93M
                break;
649
3.94M
            }
650
144M
            skfield = NULL;
651
144M
            if (asn1_item_embed_d2i(&skfield, &p, len,
652
144M
                                     ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
653
144M
                                     depth, libctx, propq) <= 0) {
654
1.03M
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
655
                /* |skfield| may be partially allocated despite failure. */
656
1.03M
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
657
1.03M
                goto err;
658
1.03M
            }
659
143M
            len -= p - q;
660
143M
            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
143M
        }
666
98.8M
        if (sk_eoc) {
667
10.9k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
668
10.9k
            goto err;
669
10.9k
        }
670
98.8M
    } else if (flags & ASN1_TFLG_IMPTAG) {
671
        /* IMPLICIT tagging */
672
15.2M
        ret = asn1_item_embed_d2i(val, &p, len,
673
15.2M
                                  ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
674
15.2M
                                  ctx, depth, libctx, propq);
675
15.2M
        if (!ret) {
676
354k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
677
354k
            goto err;
678
14.8M
        } else if (ret == -1)
679
11.1M
            return -1;
680
65.9M
    } else {
681
        /* Nothing special */
682
65.9M
        ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
683
65.9M
                                  -1, 0, opt, ctx, depth, libctx, propq);
684
65.9M
        if (!ret) {
685
5.28M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
686
5.28M
            goto err;
687
60.6M
        } else if (ret == -1)
688
5.89M
            return -1;
689
65.9M
    }
690
691
157M
    *in = p;
692
157M
    return 1;
693
694
6.68M
 err:
695
6.68M
    return 0;
696
182M
}
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
95.9M
{
703
95.9M
    int ret = 0, utype;
704
95.9M
    long plen;
705
95.9M
    char cst, inf, free_cont = 0;
706
95.9M
    const unsigned char *p;
707
95.9M
    BUF_MEM buf = { 0, NULL, 0, 0 };
708
95.9M
    const unsigned char *cont = NULL;
709
95.9M
    long len;
710
711
95.9M
    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
95.9M
    if (it->itype == ASN1_ITYPE_MSTRING) {
717
14.6M
        utype = tag;
718
14.6M
        tag = -1;
719
14.6M
    } else
720
81.2M
        utype = it->utype;
721
722
95.9M
    if (utype == V_ASN1_ANY) {
723
        /* If type is ANY need to figure out type from tag */
724
25.8M
        unsigned char oclass;
725
25.8M
        if (tag >= 0) {
726
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
727
0
            return 0;
728
0
        }
729
25.8M
        if (opt) {
730
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
731
0
            return 0;
732
0
        }
733
25.8M
        p = *in;
734
25.8M
        ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
735
25.8M
                              &p, inlen, -1, 0, 0, ctx);
736
25.8M
        if (!ret) {
737
15.5k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
738
15.5k
            return 0;
739
15.5k
        }
740
25.8M
        if (oclass != V_ASN1_UNIVERSAL)
741
411k
            utype = V_ASN1_OTHER;
742
25.8M
    }
743
95.9M
    if (tag == -1) {
744
82.9M
        tag = utype;
745
82.9M
        aclass = V_ASN1_UNIVERSAL;
746
82.9M
    }
747
95.9M
    p = *in;
748
    /* Check header */
749
95.9M
    ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
750
95.9M
                          &p, inlen, tag, aclass, opt, ctx);
751
95.9M
    if (!ret) {
752
4.63M
        ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
753
4.63M
        return 0;
754
91.2M
    } else if (ret == -1)
755
12.0M
        return -1;
756
79.2M
    ret = 0;
757
    /* SEQUENCE, SET and "OTHER" are left in encoded form */
758
79.2M
    if ((utype == V_ASN1_SEQUENCE)
759
79.2M
        || (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
13.0M
        if (utype == V_ASN1_OTHER) {
765
411k
            asn1_tlc_clear(ctx);
766
411k
        }
767
        /* SEQUENCE and SET must be constructed */
768
12.6M
        else if (!cst) {
769
5.75k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
770
5.75k
            return 0;
771
5.75k
        }
772
773
13.0M
        cont = *in;
774
        /* If indefinite length constructed find the real end */
775
13.0M
        if (inf) {
776
788k
            if (!asn1_find_end(&p, plen, inf))
777
103k
                goto err;
778
684k
            len = p - cont;
779
12.2M
        } else {
780
12.2M
            len = p - cont + plen;
781
12.2M
            p += plen;
782
12.2M
        }
783
66.1M
    } else if (cst) {
784
6.61M
        if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
785
6.61M
            || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
786
6.61M
            || utype == V_ASN1_ENUMERATED) {
787
18.6k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
788
18.6k
            return 0;
789
18.6k
        }
790
791
        /* Free any returned 'buf' content */
792
6.59M
        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
6.59M
        if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
800
30.0k
            goto err;
801
30.0k
        }
802
6.56M
        len = buf.length;
803
        /* Append a final null to string */
804
6.56M
        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
6.56M
        buf.data[len] = 0;
809
6.56M
        cont = (const unsigned char *)buf.data;
810
59.5M
    } else {
811
59.5M
        cont = p;
812
59.5M
        len = plen;
813
59.5M
        p += plen;
814
59.5M
    }
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
79.0M
    if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
819
431k
        goto err;
820
821
78.6M
    *in = p;
822
78.6M
    ret = 1;
823
79.2M
 err:
824
79.2M
    if (free_cont)
825
100k
        OPENSSL_free(buf.data);
826
79.2M
    return ret;
827
78.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
58.0M
{
834
58.0M
    ASN1_VALUE **opval = NULL;
835
58.0M
    ASN1_STRING *stmp;
836
58.0M
    ASN1_TYPE *typ = NULL;
837
58.0M
    int ret = 0;
838
58.0M
    const ASN1_PRIMITIVE_FUNCS *pf;
839
58.0M
    ASN1_INTEGER **tint;
840
58.0M
    pf = it->funcs;
841
842
58.0M
    if (pf && pf->prim_c2i)
843
1.77M
        return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
844
    /* If ANY type clear type and set pointer to internal value */
845
56.3M
    if (it->utype == V_ASN1_ANY) {
846
17.7M
        if (*pval == NULL) {
847
17.6M
            typ = ASN1_TYPE_new();
848
17.6M
            if (typ == NULL)
849
0
                goto err;
850
17.6M
            *pval = (ASN1_VALUE *)typ;
851
17.6M
        } else
852
61.6k
            typ = (ASN1_TYPE *)*pval;
853
854
17.7M
        if (utype != typ->type)
855
17.7M
            ASN1_TYPE_set(typ, utype, NULL);
856
17.7M
        opval = pval;
857
17.7M
        pval = &typ->value.asn1_value;
858
17.7M
    }
859
56.3M
    switch (utype) {
860
15.7M
    case V_ASN1_OBJECT:
861
15.7M
        if (!ossl_c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
862
21.2k
            goto err;
863
15.6M
        break;
864
865
15.6M
    case V_ASN1_NULL:
866
255k
        if (len) {
867
2.55k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
868
2.55k
            goto err;
869
2.55k
        }
870
253k
        *pval = (ASN1_VALUE *)1;
871
253k
        break;
872
873
7.91M
    case V_ASN1_BOOLEAN:
874
7.91M
        if (len != 1) {
875
4.21k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
876
4.21k
            goto err;
877
7.90M
        } else {
878
7.90M
            ASN1_BOOLEAN *tbool;
879
7.90M
            tbool = (ASN1_BOOLEAN *)pval;
880
7.90M
            *tbool = *cont;
881
7.90M
        }
882
7.90M
        break;
883
884
7.90M
    case V_ASN1_BIT_STRING:
885
2.87M
        if (!ossl_c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
886
12.5k
            goto err;
887
2.86M
        break;
888
889
6.58M
    case V_ASN1_INTEGER:
890
6.66M
    case V_ASN1_ENUMERATED:
891
6.66M
        tint = (ASN1_INTEGER **)pval;
892
6.66M
        if (!ossl_c2i_ASN1_INTEGER(tint, &cont, len))
893
198k
            goto err;
894
        /* Fixup type to match the expected form */
895
6.47M
        (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
896
6.47M
        break;
897
898
2.24M
    case V_ASN1_OCTET_STRING:
899
3.99M
    case V_ASN1_NUMERICSTRING:
900
6.63M
    case V_ASN1_PRINTABLESTRING:
901
6.96M
    case V_ASN1_T61STRING:
902
6.96M
    case V_ASN1_VIDEOTEXSTRING:
903
8.25M
    case V_ASN1_IA5STRING:
904
8.94M
    case V_ASN1_UTCTIME:
905
9.40M
    case V_ASN1_GENERALIZEDTIME:
906
9.88M
    case V_ASN1_GRAPHICSTRING:
907
9.92M
    case V_ASN1_VISIBLESTRING:
908
9.92M
    case V_ASN1_GENERALSTRING:
909
10.6M
    case V_ASN1_UNIVERSALSTRING:
910
11.2M
    case V_ASN1_BMPSTRING:
911
11.7M
    case V_ASN1_UTF8STRING:
912
12.0M
    case V_ASN1_OTHER:
913
16.6M
    case V_ASN1_SET:
914
19.7M
    case V_ASN1_SEQUENCE:
915
22.8M
    default:
916
22.8M
        if (utype == V_ASN1_BMPSTRING && (len & 1)) {
917
757
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
918
757
            goto err;
919
757
        }
920
22.8M
        if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
921
1.48k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
922
1.48k
            goto err;
923
1.48k
        }
924
        /* All based on ASN1_STRING and handled the same */
925
22.8M
        if (*pval == NULL) {
926
10.8M
            stmp = ASN1_STRING_type_new(utype);
927
10.8M
            if (stmp == NULL) {
928
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
929
0
                goto err;
930
0
            }
931
10.8M
            *pval = (ASN1_VALUE *)stmp;
932
12.0M
        } else {
933
12.0M
            stmp = (ASN1_STRING *)*pval;
934
12.0M
            stmp->type = utype;
935
12.0M
        }
936
        /* If we've already allocated a buffer use it */
937
22.8M
        if (*free_cont) {
938
5.85M
            OPENSSL_free(stmp->data);
939
5.85M
            stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
940
5.85M
            stmp->length = len;
941
5.85M
            *free_cont = 0;
942
17.0M
        } else {
943
17.0M
            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
17.0M
        }
950
22.8M
        break;
951
56.3M
    }
952
    /* If ASN1_ANY and NULL type fix up value */
953
56.0M
    if (typ && (utype == V_ASN1_NULL))
954
245k
        typ->value.ptr = NULL;
955
956
56.0M
    ret = 1;
957
56.3M
 err:
958
56.3M
    if (!ret) {
959
240k
        ASN1_TYPE_free(typ);
960
240k
        if (opval)
961
13.3k
            *opval = NULL;
962
240k
    }
963
56.3M
    return ret;
964
56.0M
}
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
788k
{
975
788k
    uint32_t expected_eoc;
976
788k
    long plen;
977
788k
    const unsigned char *p = *in, *q;
978
    /* If not indefinite length constructed just add length */
979
788k
    if (inf == 0) {
980
0
        *in += len;
981
0
        return 1;
982
0
    }
983
788k
    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
324M
    while (len > 0) {
991
324M
        if (asn1_check_eoc(&p, len)) {
992
30.1M
            expected_eoc--;
993
30.1M
            if (expected_eoc == 0)
994
684k
                break;
995
29.4M
            len -= 2;
996
29.4M
            continue;
997
30.1M
        }
998
294M
        q = p;
999
        /* Just read in a header: only care about the length */
1000
294M
        if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1001
294M
                             -1, 0, 0, NULL)) {
1002
65.5k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1003
65.5k
            return 0;
1004
65.5k
        }
1005
294M
        if (inf) {
1006
63.0M
            if (expected_eoc == UINT32_MAX) {
1007
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1008
0
                return 0;
1009
0
            }
1010
63.0M
            expected_eoc++;
1011
231M
        } else {
1012
231M
            p += plen;
1013
231M
        }
1014
294M
        len -= p - q;
1015
294M
    }
1016
722k
    if (expected_eoc) {
1017
37.9k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1018
37.9k
        return 0;
1019
37.9k
    }
1020
684k
    *in = p;
1021
684k
    return 1;
1022
722k
}
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
873k
# 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
7.47M
{
1043
7.47M
    const unsigned char *p, *q;
1044
7.47M
    long plen;
1045
7.47M
    char cst, ininf;
1046
7.47M
    p = *in;
1047
7.47M
    inf &= 1;
1048
    /*
1049
     * If no buffer and not indefinite length constructed just pass over the
1050
     * encoded data
1051
     */
1052
7.47M
    if (!buf && !inf) {
1053
0
        *in += len;
1054
0
        return 1;
1055
0
    }
1056
23.3M
    while (len > 0) {
1057
16.1M
        q = p;
1058
        /* Check for EOC */
1059
16.1M
        if (asn1_check_eoc(&p, len)) {
1060
            /*
1061
             * EOC is illegal outside indefinite length constructed form
1062
             */
1063
286k
            if (!inf) {
1064
4.09k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
1065
4.09k
                return 0;
1066
4.09k
            }
1067
282k
            inf = 0;
1068
282k
            break;
1069
286k
        }
1070
1071
15.8M
        if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1072
15.8M
                             len, tag, aclass, 0, NULL)) {
1073
17.2k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1074
17.2k
            return 0;
1075
17.2k
        }
1076
1077
        /* If indefinite length constructed update max length */
1078
15.8M
        if (cst) {
1079
873k
            if (depth >= ASN1_MAX_STRING_NEST) {
1080
2.29k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_ASN1_STRING);
1081
2.29k
                return 0;
1082
2.29k
            }
1083
871k
            if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1084
25.5k
                return 0;
1085
14.9M
        } else if (plen && !collect_data(buf, &p, plen))
1086
0
            return 0;
1087
15.8M
        len -= p - q;
1088
15.8M
    }
1089
7.42M
    if (inf) {
1090
6.47k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1091
6.47k
        return 0;
1092
6.47k
    }
1093
7.41M
    *in = p;
1094
7.41M
    return 1;
1095
7.42M
}
1096
1097
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1098
13.4M
{
1099
13.4M
    int len;
1100
13.4M
    if (buf) {
1101
13.4M
        len = buf->length;
1102
13.4M
        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
13.4M
        memcpy(buf->data + len, *p, plen);
1107
13.4M
    }
1108
13.4M
    *p += plen;
1109
13.4M
    return 1;
1110
13.4M
}
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
563M
{
1116
563M
    const unsigned char *p;
1117
1118
563M
    if (len < 2)
1119
135k
        return 0;
1120
563M
    p = *in;
1121
563M
    if (p[0] == '\0' && p[1] == '\0') {
1122
42.1M
        *in += 2;
1123
42.1M
        return 1;
1124
42.1M
    }
1125
521M
    return 0;
1126
563M
}
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
592M
{
1140
592M
    int i;
1141
592M
    int ptag, pclass;
1142
592M
    long plen;
1143
592M
    const unsigned char *p, *q;
1144
592M
    p = *in;
1145
592M
    q = p;
1146
1147
592M
    if (len <= 0) {
1148
134
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
1149
134
        goto err;
1150
134
    }
1151
592M
    if (ctx != NULL && ctx->valid) {
1152
61.0M
        i = ctx->ret;
1153
61.0M
        plen = ctx->plen;
1154
61.0M
        pclass = ctx->pclass;
1155
61.0M
        ptag = ctx->ptag;
1156
61.0M
        p += ctx->hdrlen;
1157
531M
    } else {
1158
531M
        i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1159
531M
        if (ctx != NULL) {
1160
221M
            ctx->ret = i;
1161
221M
            ctx->plen = plen;
1162
221M
            ctx->pclass = pclass;
1163
221M
            ctx->ptag = ptag;
1164
221M
            ctx->hdrlen = p - q;
1165
221M
            ctx->valid = 1;
1166
            /*
1167
             * If definite length, and no error, length + header can't exceed
1168
             * total amount of data available.
1169
             */
1170
221M
            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
221M
        }
1175
531M
    }
1176
1177
592M
    if ((i & 0x80) != 0) {
1178
621k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER);
1179
621k
        goto err;
1180
621k
    }
1181
591M
    if (exptag >= 0) {
1182
240M
        if (exptag != ptag || expclass != pclass) {
1183
            /*
1184
             * If type is OPTIONAL, not an error: indicate missing type.
1185
             */
1186
28.7M
            if (opt != 0)
1187
20.8M
                return -1;
1188
28.7M
            ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG);
1189
7.82M
            goto err;
1190
28.7M
        }
1191
        /*
1192
         * We have a tag and class match: assume we are going to do something
1193
         * with it
1194
         */
1195
211M
        asn1_tlc_clear(ctx);
1196
211M
    }
1197
1198
563M
    if ((i & 1) != 0)
1199
82.0M
        plen = len - (p - q);
1200
1201
563M
    if (inf != NULL)
1202
522M
        *inf = i & 1;
1203
1204
563M
    if (cst != NULL)
1205
127M
        *cst = i & V_ASN1_CONSTRUCTED;
1206
1207
563M
    if (olen != NULL)
1208
522M
        *olen = plen;
1209
1210
563M
    if (oclass != NULL)
1211
40.8M
        *oclass = pclass;
1212
1213
563M
    if (otag != NULL)
1214
40.8M
        *otag = ptag;
1215
1216
563M
    *in = p;
1217
563M
    return 1;
1218
1219
8.45M
 err:
1220
8.45M
    asn1_tlc_clear(ctx);
1221
8.45M
    return 0;
1222
591M
}