Coverage Report

Created: 2025-06-13 06:58

/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
265M
#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
38.9M
{
89
38.9M
    if ((tag < 0) || (tag > 30))
90
3.50k
        return 0;
91
38.9M
    return tag2bit[tag];
92
38.9M
}
93
94
/* Macro to initialize and invalidate the cache */
95
96
241M
#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
16.1M
#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
19.8M
{
112
19.8M
    int rv;
113
114
19.8M
    if (pval == NULL || it == NULL) {
115
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
116
0
        return 0;
117
0
    }
118
19.8M
    rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0,
119
19.8M
                             libctx, propq);
120
19.8M
    if (rv <= 0)
121
11.7M
        ASN1_item_ex_free(pval, it);
122
19.8M
    return rv;
123
19.8M
}
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.72M
{
129
3.72M
    return asn1_item_ex_d2i_intern(pval, in, len, it, tag, aclass, opt, ctx,
130
3.72M
                                   NULL, NULL);
131
3.72M
}
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
16.1M
{
138
16.1M
    ASN1_TLC c;
139
16.1M
    ASN1_VALUE *ptmpval = NULL;
140
141
16.1M
    if (pval == NULL)
142
14.3M
        pval = &ptmpval;
143
16.1M
    asn1_tlc_clear_nc(&c);
144
16.1M
    if (asn1_item_ex_d2i_intern(pval, in, len, it, -1, 0, 0, &c, libctx,
145
16.1M
                                propq) > 0)
146
4.85M
        return *pval;
147
11.3M
    return NULL;
148
16.1M
}
149
150
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
151
                          const unsigned char **in, long len,
152
                          const ASN1_ITEM *it)
153
15.1M
{
154
15.1M
    return ASN1_item_d2i_ex(pval, in, len, it, NULL, NULL);
155
15.1M
}
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
265M
{
168
265M
    const ASN1_TEMPLATE *tt, *errtt = NULL;
169
265M
    const ASN1_EXTERN_FUNCS *ef;
170
265M
    const ASN1_AUX *aux;
171
265M
    ASN1_aux_cb *asn1_cb;
172
265M
    const unsigned char *p = NULL, *q;
173
265M
    unsigned char oclass;
174
265M
    char seq_eoc, seq_nolen, cst, isopt;
175
265M
    long tmplen;
176
265M
    int i;
177
265M
    int otag;
178
265M
    int ret = 0;
179
265M
    ASN1_VALUE **pchptr;
180
181
265M
    if (pval == NULL || it == NULL) {
182
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
183
0
        return 0;
184
0
    }
185
265M
    if (len <= 0) {
186
57.6k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
187
57.6k
        return 0;
188
57.6k
    }
189
265M
    aux = it->funcs;
190
265M
    if (aux && aux->asn1_cb)
191
11.0M
        asn1_cb = aux->asn1_cb;
192
254M
    else
193
254M
        asn1_cb = 0;
194
195
265M
    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
265M
    switch (it->itype) {
201
192M
    case ASN1_ITYPE_PRIMITIVE:
202
192M
        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
96.0M
            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
96.0M
            return asn1_template_ex_d2i(pval, in, len, it->templates, opt, ctx,
215
96.0M
                                        depth, libctx, propq);
216
96.0M
        }
217
95.9M
        return asn1_d2i_ex_primitive(pval, in, len, it,
218
95.9M
                                     tag, aclass, opt, ctx);
219
220
18.8M
    case ASN1_ITYPE_MSTRING:
221
        /*
222
         * It never makes sense for multi-strings to have implicit tagging, so
223
         * if tag != -1, then this looks like an error in the template.
224
         */
225
18.8M
        if (tag != -1) {
226
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
227
0
            goto err;
228
0
        }
229
230
18.8M
        p = *in;
231
        /* Just read in tag and class */
232
18.8M
        ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
233
18.8M
                              &p, len, -1, 0, 1, ctx);
234
18.8M
        if (!ret) {
235
11.7k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
236
11.7k
            goto err;
237
11.7k
        }
238
239
        /* Must be UNIVERSAL class */
240
18.8M
        if (oclass != V_ASN1_UNIVERSAL) {
241
            /* If OPTIONAL, assume this is OK */
242
142k
            if (opt)
243
113k
                return -1;
244
142k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
245
29.3k
            goto err;
246
142k
        }
247
248
        /* Check tag matches bit map */
249
18.6M
        if (!(ASN1_tag2bit(otag) & it->utype)) {
250
            /* If OPTIONAL, assume this is OK */
251
310k
            if (opt)
252
13.9k
                return -1;
253
310k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_WRONG_TAG);
254
296k
            goto err;
255
310k
        }
256
18.3M
        return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
257
258
3.72M
    case ASN1_ITYPE_EXTERN:
259
        /* Use new style d2i */
260
3.72M
        ef = it->funcs;
261
3.72M
        if (ef->asn1_ex_d2i_ex != NULL)
262
1.81M
            return ef->asn1_ex_d2i_ex(pval, in, len, it, tag, aclass, opt, ctx,
263
1.81M
                                      libctx, propq);
264
1.90M
        return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
265
266
5.52M
    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.52M
        if (tag != -1) {
272
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
273
0
            goto err;
274
0
        }
275
276
5.52M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
277
0
            goto auxerr;
278
5.52M
        if (*pval) {
279
            /* Free up and zero CHOICE value if initialised */
280
589k
            i = ossl_asn1_get_choice_selector(pval, it);
281
589k
            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.93M
        } 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.52M
        p = *in;
293
20.6M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
294
20.1M
            pchptr = ossl_asn1_get_field_ptr(pval, tt);
295
            /*
296
             * We mark field as OPTIONAL so its absence can be recognised.
297
             */
298
20.1M
            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth,
299
20.1M
                                       libctx, propq);
300
            /* If field not present, try the next one */
301
20.1M
            if (ret == -1)
302
15.1M
                continue;
303
            /* If positive return, read OK, break loop */
304
5.01M
            if (ret > 0)
305
4.29M
                break;
306
            /*
307
             * Must be an ASN1 parsing error.
308
             * Free up any partial choice value
309
             */
310
715k
            ossl_asn1_template_free(pchptr, tt);
311
715k
            errtt = tt;
312
715k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
313
715k
            goto err;
314
5.01M
        }
315
316
        /* Did we fall off the end without reading anything? */
317
4.80M
        if (i == it->tcount) {
318
            /* If OPTIONAL, this is OK */
319
507k
            if (opt) {
320
                /* Free and zero it */
321
25.7k
                ASN1_item_ex_free(pval, it);
322
25.7k
                return -1;
323
25.7k
            }
324
507k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
325
481k
            goto err;
326
507k
        }
327
328
4.29M
        ossl_asn1_set_choice_selector(pval, i, it);
329
330
4.29M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
331
0
            goto auxerr;
332
4.29M
        *in = p;
333
4.29M
        return 1;
334
335
636k
    case ASN1_ITYPE_NDEF_SEQUENCE:
336
45.1M
    case ASN1_ITYPE_SEQUENCE:
337
45.1M
        p = *in;
338
45.1M
        tmplen = len;
339
340
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
341
45.1M
        if (tag == -1) {
342
42.7M
            tag = V_ASN1_SEQUENCE;
343
42.7M
            aclass = V_ASN1_UNIVERSAL;
344
42.7M
        }
345
        /* Get SEQUENCE length and update len, p */
346
45.1M
        ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
347
45.1M
                              &p, len, tag, aclass, opt, ctx);
348
45.1M
        if (!ret) {
349
2.93M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
350
2.93M
            goto err;
351
42.2M
        } else if (ret == -1)
352
4.87M
            return -1;
353
37.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
37.3M
        else
359
37.3M
            seq_nolen = seq_eoc;
360
37.3M
        if (!cst) {
361
105k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
362
105k
            goto err;
363
105k
        }
364
365
37.2M
        if (*pval == NULL
366
37.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
37.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
137M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
376
99.9M
            if (tt->flags & ASN1_TFLG_ADB_MASK) {
377
520k
                const ASN1_TEMPLATE *seqtt;
378
520k
                ASN1_VALUE **pseqval;
379
520k
                seqtt = ossl_asn1_do_adb(*pval, tt, 0);
380
520k
                if (seqtt == NULL)
381
421k
                    continue;
382
99.5k
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
383
99.5k
                ossl_asn1_template_free(pseqval, seqtt);
384
99.5k
            }
385
99.9M
        }
386
387
        /* Get each field entry */
388
108M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
389
81.9M
            const ASN1_TEMPLATE *seqtt;
390
81.9M
            ASN1_VALUE **pseqval;
391
81.9M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
392
81.9M
            if (seqtt == NULL)
393
0
                goto err;
394
81.9M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
395
            /* Have we ran out of data? */
396
81.9M
            if (!len)
397
1.59M
                break;
398
80.3M
            q = p;
399
80.3M
            if (asn1_check_eoc(&p, len)) {
400
2.31M
                if (!seq_eoc) {
401
14.7k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
402
14.7k
                    goto err;
403
14.7k
                }
404
2.30M
                len -= p - q;
405
2.30M
                seq_eoc = 0;
406
2.30M
                break;
407
2.31M
            }
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
78.0M
            if (i == (it->tcount - 1))
415
27.7M
                isopt = 0;
416
50.2M
            else
417
50.2M
                isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
418
            /*
419
             * attempt to read in field, allowing each to be OPTIONAL
420
             */
421
422
78.0M
            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
423
78.0M
                                       depth, libctx, propq);
424
78.0M
            if (!ret) {
425
6.71M
                errtt = seqtt;
426
6.71M
                goto err;
427
71.3M
            } else if (ret == -1) {
428
                /*
429
                 * OPTIONAL component absent. Free and zero the field.
430
                 */
431
6.26M
                ossl_asn1_template_free(pseqval, seqtt);
432
6.26M
                continue;
433
6.26M
            }
434
            /* Update length */
435
65.0M
            len -= p - q;
436
65.0M
        }
437
438
        /* Check for EOC if expecting one */
439
30.5M
        if (seq_eoc && !asn1_check_eoc(&p, len)) {
440
135k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
441
135k
            goto err;
442
135k
        }
443
        /* Check all data read */
444
30.3M
        if (!seq_nolen && len) {
445
21.3k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
446
21.3k
            goto err;
447
21.3k
        }
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
37.4M
        for (; i < it->tcount; tt++, i++) {
455
7.24M
            const ASN1_TEMPLATE *seqtt;
456
7.24M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
457
7.24M
            if (seqtt == NULL)
458
0
                goto err;
459
7.24M
            if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
460
7.06M
                ASN1_VALUE **pseqval;
461
7.06M
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
462
7.06M
                ossl_asn1_template_free(pseqval, seqtt);
463
7.06M
            } else {
464
180k
                errtt = seqtt;
465
180k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_FIELD_MISSING);
466
180k
                goto err;
467
180k
            }
468
7.24M
        }
469
        /* Save encoding */
470
30.1M
        if (!ossl_asn1_enc_save(pval, *in, p - *in, it))
471
0
            goto auxerr;
472
30.1M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
473
9.75k
            goto auxerr;
474
30.1M
        *in = p;
475
30.1M
        return 1;
476
477
0
    default:
478
0
        return 0;
479
265M
    }
480
9.75k
 auxerr:
481
9.75k
    ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
482
11.6M
 err:
483
11.6M
    if (errtt)
484
7.60M
        ERR_add_error_data(4, "Field=", errtt->field_name,
485
7.60M
                           ", Type=", it->sname);
486
4.03M
    else
487
4.03M
        ERR_add_error_data(2, "Type=", it->sname);
488
11.6M
    return 0;
489
9.75k
}
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
194M
{
502
194M
    int flags, aclass;
503
194M
    int ret;
504
194M
    long len;
505
194M
    const unsigned char *p, *q;
506
194M
    char exp_eoc;
507
194M
    if (!val)
508
0
        return 0;
509
194M
    flags = tt->flags;
510
194M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
511
512
194M
    p = *in;
513
514
    /* Check if EXPLICIT tag expected */
515
194M
    if (flags & ASN1_TFLG_EXPTAG) {
516
5.14M
        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
5.14M
        ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
522
5.14M
                              &p, inlen, tt->tag, aclass, opt, ctx);
523
5.14M
        q = p;
524
5.14M
        if (!ret) {
525
250k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
526
250k
            return 0;
527
4.89M
        } else if (ret == -1)
528
3.77M
            return -1;
529
1.11M
        if (!cst) {
530
5.87k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
531
5.87k
            return 0;
532
5.87k
        }
533
        /* We've found the field so it can't be OPTIONAL now */
534
1.11M
        ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth, libctx,
535
1.11M
                                      propq);
536
1.11M
        if (!ret) {
537
218k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
538
218k
            return 0;
539
218k
        }
540
        /* We read the field in OK so update length */
541
893k
        len -= p - q;
542
893k
        if (exp_eoc) {
543
            /* If NDEF we must have an EOC here */
544
330k
            if (!asn1_check_eoc(&p, len)) {
545
19.1k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
546
19.1k
                goto err;
547
19.1k
            }
548
562k
        } else {
549
            /*
550
             * Otherwise we must hit the EXPLICIT tag end or its an error
551
             */
552
562k
            if (len) {
553
3.21k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
554
3.21k
                goto err;
555
3.21k
            }
556
562k
        }
557
893k
    } else
558
189M
        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth,
559
189M
                                       libctx, propq);
560
561
870k
    *in = p;
562
870k
    return 1;
563
564
22.4k
 err:
565
22.4k
    return 0;
566
194M
}
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
190M
{
574
190M
    int flags, aclass;
575
190M
    int ret;
576
190M
    ASN1_VALUE *tval;
577
190M
    const unsigned char *p, *q;
578
190M
    if (!val)
579
0
        return 0;
580
190M
    flags = tt->flags;
581
190M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
582
583
190M
    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
190M
    if (tt->flags & ASN1_TFLG_EMBED) {
590
8.13M
        tval = (ASN1_VALUE *)val;
591
8.13M
        val = &tval;
592
8.13M
    }
593
594
190M
    if (flags & ASN1_TFLG_SK_MASK) {
595
        /* SET OF, SEQUENCE OF */
596
98.2M
        int sktag, skaclass;
597
98.2M
        char sk_eoc;
598
        /* First work out expected inner tag value */
599
98.2M
        if (flags & ASN1_TFLG_IMPTAG) {
600
845k
            sktag = tt->tag;
601
845k
            skaclass = aclass;
602
97.4M
        } else {
603
97.4M
            skaclass = V_ASN1_UNIVERSAL;
604
97.4M
            if (flags & ASN1_TFLG_SET_OF)
605
93.0M
                sktag = V_ASN1_SET;
606
4.41M
            else
607
4.41M
                sktag = V_ASN1_SEQUENCE;
608
97.4M
        }
609
        /* Get the tag */
610
98.2M
        ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
611
98.2M
                              &p, len, sktag, skaclass, opt, ctx);
612
98.2M
        if (!ret) {
613
876k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
614
876k
            return 0;
615
97.3M
        } else if (ret == -1)
616
535k
            return -1;
617
96.8M
        if (*val == NULL)
618
96.5M
            *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
619
309k
        else {
620
            /*
621
             * We've got a valid STACK: free up any items present
622
             */
623
309k
            STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
624
309k
            ASN1_VALUE *vtmp;
625
309k
            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
309k
        }
630
631
96.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
249M
        while (len > 0) {
638
157M
            ASN1_VALUE *skfield;
639
157M
            q = p;
640
            /* See if EOC found */
641
157M
            if (asn1_check_eoc(&p, len)) {
642
4.14M
                if (!sk_eoc) {
643
6.34k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
644
6.34k
                    goto err;
645
6.34k
                }
646
4.14M
                len -= p - q;
647
4.14M
                sk_eoc = 0;
648
4.14M
                break;
649
4.14M
            }
650
153M
            skfield = NULL;
651
153M
            if (asn1_item_embed_d2i(&skfield, &p, len,
652
153M
                                     ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
653
153M
                                     depth, libctx, propq) <= 0) {
654
1.12M
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
655
                /* |skfield| may be partially allocated despite failure. */
656
1.12M
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
657
1.12M
                goto err;
658
1.12M
            }
659
152M
            len -= p - q;
660
152M
            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
152M
        }
666
95.7M
        if (sk_eoc) {
667
12.4k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
668
12.4k
            goto err;
669
12.4k
        }
670
95.7M
    } 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
385k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
677
385k
            goto err;
678
14.9M
        } else if (ret == -1)
679
11.3M
            return -1;
680
76.6M
    } else {
681
        /* Nothing special */
682
76.6M
        ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
683
76.6M
                                  -1, 0, opt, ctx, depth, libctx, propq);
684
76.6M
        if (!ret) {
685
6.01M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
686
6.01M
            goto err;
687
70.6M
        } else if (ret == -1)
688
5.80M
            return -1;
689
76.6M
    }
690
691
164M
    *in = p;
692
164M
    return 1;
693
694
7.54M
 err:
695
7.54M
    return 0;
696
190M
}
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
114M
{
703
114M
    int ret = 0, utype;
704
114M
    long plen;
705
114M
    char cst, inf, free_cont = 0;
706
114M
    const unsigned char *p;
707
114M
    BUF_MEM buf = { 0, NULL, 0, 0 };
708
114M
    const unsigned char *cont = NULL;
709
114M
    long len;
710
711
114M
    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
114M
    if (it->itype == ASN1_ITYPE_MSTRING) {
717
18.3M
        utype = tag;
718
18.3M
        tag = -1;
719
18.3M
    } else
720
95.9M
        utype = it->utype;
721
722
114M
    if (utype == V_ASN1_ANY) {
723
        /* If type is ANY need to figure out type from tag */
724
38.1M
        unsigned char oclass;
725
38.1M
        if (tag >= 0) {
726
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
727
0
            return 0;
728
0
        }
729
38.1M
        if (opt) {
730
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
731
0
            return 0;
732
0
        }
733
38.1M
        p = *in;
734
38.1M
        ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
735
38.1M
                              &p, inlen, -1, 0, 0, ctx);
736
38.1M
        if (!ret) {
737
16.5k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
738
16.5k
            return 0;
739
16.5k
        }
740
38.0M
        if (oclass != V_ASN1_UNIVERSAL)
741
374k
            utype = V_ASN1_OTHER;
742
38.0M
    }
743
114M
    if (tag == -1) {
744
101M
        tag = utype;
745
101M
        aclass = V_ASN1_UNIVERSAL;
746
101M
    }
747
114M
    p = *in;
748
    /* Check header */
749
114M
    ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
750
114M
                          &p, inlen, tag, aclass, opt, ctx);
751
114M
    if (!ret) {
752
5.08M
        ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
753
5.08M
        return 0;
754
109M
    } else if (ret == -1)
755
12.1M
        return -1;
756
97.1M
    ret = 0;
757
    /* SEQUENCE, SET and "OTHER" are left in encoded form */
758
97.1M
    if ((utype == V_ASN1_SEQUENCE)
759
97.1M
        || (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
19.9M
        if (utype == V_ASN1_OTHER) {
765
374k
            asn1_tlc_clear(ctx);
766
374k
        }
767
        /* SEQUENCE and SET must be constructed */
768
19.6M
        else if (!cst) {
769
6.83k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
770
6.83k
            return 0;
771
6.83k
        }
772
773
19.9M
        cont = *in;
774
        /* If indefinite length constructed find the real end */
775
19.9M
        if (inf) {
776
796k
            if (!asn1_find_end(&p, plen, inf))
777
123k
                goto err;
778
673k
            len = p - cont;
779
19.1M
        } else {
780
19.1M
            len = p - cont + plen;
781
19.1M
            p += plen;
782
19.1M
        }
783
77.1M
    } else if (cst) {
784
6.69M
        if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
785
6.69M
            || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
786
6.69M
            || utype == V_ASN1_ENUMERATED) {
787
20.6k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
788
20.6k
            return 0;
789
20.6k
        }
790
791
        /* Free any returned 'buf' content */
792
6.66M
        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.66M
        if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
800
32.0k
            goto err;
801
32.0k
        }
802
6.63M
        len = buf.length;
803
        /* Append a final null to string */
804
6.63M
        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.63M
        buf.data[len] = 0;
809
6.63M
        cont = (const unsigned char *)buf.data;
810
70.4M
    } else {
811
70.4M
        cont = p;
812
70.4M
        len = plen;
813
70.4M
        p += plen;
814
70.4M
    }
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
96.9M
    if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
819
549k
        goto err;
820
821
96.4M
    *in = p;
822
96.4M
    ret = 1;
823
97.1M
 err:
824
97.1M
    if (free_cont)
825
139k
        OPENSSL_free(buf.data);
826
97.1M
    return ret;
827
96.4M
}
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
69.3M
{
834
69.3M
    ASN1_VALUE **opval = NULL;
835
69.3M
    ASN1_STRING *stmp;
836
69.3M
    ASN1_TYPE *typ = NULL;
837
69.3M
    int ret = 0;
838
69.3M
    const ASN1_PRIMITIVE_FUNCS *pf;
839
69.3M
    ASN1_INTEGER **tint;
840
69.3M
    pf = it->funcs;
841
842
69.3M
    if (pf && pf->prim_c2i)
843
1.83M
        return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
844
    /* If ANY type clear type and set pointer to internal value */
845
67.4M
    if (it->utype == V_ASN1_ANY) {
846
26.1M
        if (*pval == NULL) {
847
26.0M
            typ = ASN1_TYPE_new();
848
26.0M
            if (typ == NULL)
849
0
                goto err;
850
26.0M
            *pval = (ASN1_VALUE *)typ;
851
26.0M
        } else
852
79.3k
            typ = (ASN1_TYPE *)*pval;
853
854
26.1M
        if (utype != typ->type)
855
26.1M
            ASN1_TYPE_set(typ, utype, NULL);
856
26.1M
        opval = pval;
857
26.1M
        pval = &typ->value.asn1_value;
858
26.1M
    }
859
67.4M
    switch (utype) {
860
18.2M
    case V_ASN1_OBJECT:
861
18.2M
        if (!ossl_c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
862
21.8k
            goto err;
863
18.2M
        break;
864
865
18.2M
    case V_ASN1_NULL:
866
278k
        if (len) {
867
3.07k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
868
3.07k
            goto err;
869
3.07k
        }
870
275k
        *pval = (ASN1_VALUE *)1;
871
275k
        break;
872
873
12.6M
    case V_ASN1_BOOLEAN:
874
12.6M
        if (len != 1) {
875
4.18k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
876
4.18k
            goto err;
877
12.6M
        } else {
878
12.6M
            ASN1_BOOLEAN *tbool;
879
12.6M
            tbool = (ASN1_BOOLEAN *)pval;
880
12.6M
            *tbool = *cont;
881
12.6M
        }
882
12.6M
        break;
883
884
12.6M
    case V_ASN1_BIT_STRING:
885
3.80M
        if (!ossl_c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
886
15.4k
            goto err;
887
3.79M
        break;
888
889
3.79M
    case V_ASN1_INTEGER:
890
3.49M
    case V_ASN1_ENUMERATED:
891
3.49M
        tint = (ASN1_INTEGER **)pval;
892
3.49M
        if (!ossl_c2i_ASN1_INTEGER(tint, &cont, len))
893
265k
            goto err;
894
        /* Fixup type to match the expected form */
895
3.23M
        (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
896
3.23M
        break;
897
898
2.22M
    case V_ASN1_OCTET_STRING:
899
2.74M
    case V_ASN1_NUMERICSTRING:
900
6.12M
    case V_ASN1_PRINTABLESTRING:
901
6.52M
    case V_ASN1_T61STRING:
902
6.53M
    case V_ASN1_VIDEOTEXSTRING:
903
8.39M
    case V_ASN1_IA5STRING:
904
9.23M
    case V_ASN1_UTCTIME:
905
9.74M
    case V_ASN1_GENERALIZEDTIME:
906
9.87M
    case V_ASN1_GRAPHICSTRING:
907
9.89M
    case V_ASN1_VISIBLESTRING:
908
9.89M
    case V_ASN1_GENERALSTRING:
909
10.4M
    case V_ASN1_UNIVERSALSTRING:
910
11.2M
    case V_ASN1_BMPSTRING:
911
11.7M
    case V_ASN1_UTF8STRING:
912
11.9M
    case V_ASN1_OTHER:
913
22.0M
    case V_ASN1_SET:
914
25.9M
    case V_ASN1_SEQUENCE:
915
29.0M
    default:
916
29.0M
        if (utype == V_ASN1_BMPSTRING && (len & 1)) {
917
912
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
918
912
            goto err;
919
912
        }
920
29.0M
        if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
921
1.80k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
922
1.80k
            goto err;
923
1.80k
        }
924
        /* All based on ASN1_STRING and handled the same */
925
29.0M
        if (*pval == NULL) {
926
15.3M
            stmp = ASN1_STRING_type_new(utype);
927
15.3M
            if (stmp == NULL) {
928
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
929
0
                goto err;
930
0
            }
931
15.3M
            *pval = (ASN1_VALUE *)stmp;
932
15.3M
        } else {
933
13.6M
            stmp = (ASN1_STRING *)*pval;
934
13.6M
            stmp->type = utype;
935
13.6M
        }
936
        /* If we've already allocated a buffer use it */
937
29.0M
        if (*free_cont) {
938
5.49M
            OPENSSL_free(stmp->data);
939
5.49M
            stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
940
5.49M
            stmp->length = len;
941
5.49M
            *free_cont = 0;
942
23.5M
        } else {
943
23.5M
            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
23.5M
        }
950
29.0M
        break;
951
67.4M
    }
952
    /* If ASN1_ANY and NULL type fix up value */
953
67.1M
    if (typ && (utype == V_ASN1_NULL))
954
266k
        typ->value.ptr = NULL;
955
956
67.1M
    ret = 1;
957
67.4M
 err:
958
67.4M
    if (!ret) {
959
312k
        ASN1_TYPE_free(typ);
960
312k
        if (opval)
961
15.1k
            *opval = NULL;
962
312k
    }
963
67.4M
    return ret;
964
67.1M
}
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
796k
{
975
796k
    uint32_t expected_eoc;
976
796k
    long plen;
977
796k
    const unsigned char *p = *in, *q;
978
    /* If not indefinite length constructed just add length */
979
796k
    if (inf == 0) {
980
0
        *in += len;
981
0
        return 1;
982
0
    }
983
796k
    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
323M
    while (len > 0) {
991
323M
        if (asn1_check_eoc(&p, len)) {
992
34.9M
            expected_eoc--;
993
34.9M
            if (expected_eoc == 0)
994
673k
                break;
995
34.3M
            len -= 2;
996
34.3M
            continue;
997
34.9M
        }
998
288M
        q = p;
999
        /* Just read in a header: only care about the length */
1000
288M
        if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1001
288M
                             -1, 0, 0, NULL)) {
1002
76.1k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1003
76.1k
            return 0;
1004
76.1k
        }
1005
288M
        if (inf) {
1006
72.4M
            if (expected_eoc == UINT32_MAX) {
1007
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1008
0
                return 0;
1009
0
            }
1010
72.4M
            expected_eoc++;
1011
216M
        } else {
1012
216M
            p += plen;
1013
216M
        }
1014
288M
        len -= p - q;
1015
288M
    }
1016
720k
    if (expected_eoc) {
1017
46.8k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1018
46.8k
        return 0;
1019
46.8k
    }
1020
673k
    *in = p;
1021
673k
    return 1;
1022
720k
}
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
755k
# 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.42M
{
1043
7.42M
    const unsigned char *p, *q;
1044
7.42M
    long plen;
1045
7.42M
    char cst, ininf;
1046
7.42M
    p = *in;
1047
7.42M
    inf &= 1;
1048
    /*
1049
     * If no buffer and not indefinite length constructed just pass over the
1050
     * encoded data
1051
     */
1052
7.42M
    if (!buf && !inf) {
1053
0
        *in += len;
1054
0
        return 1;
1055
0
    }
1056
20.7M
    while (len > 0) {
1057
13.6M
        q = p;
1058
        /* Check for EOC */
1059
13.6M
        if (asn1_check_eoc(&p, len)) {
1060
            /*
1061
             * EOC is illegal outside indefinite length constructed form
1062
             */
1063
248k
            if (!inf) {
1064
4.36k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
1065
4.36k
                return 0;
1066
4.36k
            }
1067
244k
            inf = 0;
1068
244k
            break;
1069
248k
        }
1070
1071
13.4M
        if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1072
13.4M
                             len, tag, aclass, 0, NULL)) {
1073
18.0k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1074
18.0k
            return 0;
1075
18.0k
        }
1076
1077
        /* If indefinite length constructed update max length */
1078
13.4M
        if (cst) {
1079
755k
            if (depth >= ASN1_MAX_STRING_NEST) {
1080
2.78k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_ASN1_STRING);
1081
2.78k
                return 0;
1082
2.78k
            }
1083
753k
            if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1084
27.9k
                return 0;
1085
12.6M
        } else if (plen && !collect_data(buf, &p, plen))
1086
0
            return 0;
1087
13.3M
        len -= p - q;
1088
13.3M
    }
1089
7.36M
    if (inf) {
1090
6.81k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1091
6.81k
        return 0;
1092
6.81k
    }
1093
7.36M
    *in = p;
1094
7.36M
    return 1;
1095
7.36M
}
1096
1097
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1098
11.4M
{
1099
11.4M
    int len;
1100
11.4M
    if (buf) {
1101
11.4M
        len = buf->length;
1102
11.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
11.4M
        memcpy(buf->data + len, *p, plen);
1107
11.4M
    }
1108
11.4M
    *p += plen;
1109
11.4M
    return 1;
1110
11.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
581M
{
1116
581M
    const unsigned char *p;
1117
1118
581M
    if (len < 2)
1119
158k
        return 0;
1120
581M
    p = *in;
1121
581M
    if (p[0] == '\0' && p[1] == '\0') {
1122
47.8M
        *in += 2;
1123
47.8M
        return 1;
1124
47.8M
    }
1125
533M
    return 0;
1126
581M
}
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
622M
{
1140
622M
    int i;
1141
622M
    int ptag, pclass;
1142
622M
    long plen;
1143
622M
    const unsigned char *p, *q;
1144
622M
    p = *in;
1145
622M
    q = p;
1146
1147
622M
    if (len <= 0) {
1148
156
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
1149
156
        goto err;
1150
156
    }
1151
622M
    if (ctx != NULL && ctx->valid) {
1152
77.4M
        i = ctx->ret;
1153
77.4M
        plen = ctx->plen;
1154
77.4M
        pclass = ctx->pclass;
1155
77.4M
        ptag = ctx->ptag;
1156
77.4M
        p += ctx->hdrlen;
1157
544M
    } else {
1158
544M
        i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1159
544M
        if (ctx != NULL) {
1160
242M
            ctx->ret = i;
1161
242M
            ctx->plen = plen;
1162
242M
            ctx->pclass = pclass;
1163
242M
            ctx->ptag = ptag;
1164
242M
            ctx->hdrlen = p - q;
1165
242M
            ctx->valid = 1;
1166
            /*
1167
             * If definite length, and no error, length + header can't exceed
1168
             * total amount of data available.
1169
             */
1170
242M
            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
242M
        }
1175
544M
    }
1176
1177
622M
    if ((i & 0x80) != 0) {
1178
657k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER);
1179
657k
        goto err;
1180
657k
    }
1181
621M
    if (exptag >= 0) {
1182
262M
        if (exptag != ptag || expclass != pclass) {
1183
            /*
1184
             * If type is OPTIONAL, not an error: indicate missing type.
1185
             */
1186
29.9M
            if (opt != 0)
1187
21.2M
                return -1;
1188
29.9M
            ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG);
1189
8.61M
            goto err;
1190
29.9M
        }
1191
        /*
1192
         * We have a tag and class match: assume we are going to do something
1193
         * with it
1194
         */
1195
232M
        asn1_tlc_clear(ctx);
1196
232M
    }
1197
1198
591M
    if ((i & 1) != 0)
1199
92.8M
        plen = len - (p - q);
1200
1201
591M
    if (inf != NULL)
1202
534M
        *inf = i & 1;
1203
1204
591M
    if (cst != NULL)
1205
149M
        *cst = i & V_ASN1_CONSTRUCTED;
1206
1207
591M
    if (olen != NULL)
1208
534M
        *olen = plen;
1209
1210
591M
    if (oclass != NULL)
1211
56.9M
        *oclass = pclass;
1212
1213
591M
    if (otag != NULL)
1214
56.9M
        *otag = ptag;
1215
1216
591M
    *in = p;
1217
591M
    return 1;
1218
1219
9.27M
 err:
1220
9.27M
    asn1_tlc_clear(ctx);
1221
9.27M
    return 0;
1222
621M
}