Coverage Report

Created: 2023-06-08 06:43

/src/openssl30/crypto/asn1/tasn_dec.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <stddef.h>
11
#include <string.h>
12
#include <openssl/asn1.h>
13
#include <openssl/asn1t.h>
14
#include <openssl/objects.h>
15
#include <openssl/buffer.h>
16
#include <openssl/err.h>
17
#include "internal/numbers.h"
18
#include "asn1_local.h"
19
20
/*
21
 * Constructed types with a recursive definition (such as can be found in PKCS7)
22
 * could eventually exceed the stack given malicious input with excessive
23
 * recursion. Therefore we limit the stack depth. This is the maximum number of
24
 * recursive invocations of asn1_item_embed_d2i().
25
 */
26
14.4M
#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
1.96M
{
89
1.96M
    if ((tag < 0) || (tag > 30))
90
455
        return 0;
91
1.96M
    return tag2bit[tag];
92
1.96M
}
93
94
/* Macro to initialize and invalidate the cache */
95
96
13.2M
#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
3.10M
#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
3.42M
{
112
3.42M
    int rv;
113
114
3.42M
    if (pval == NULL || it == NULL) {
115
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
116
0
        return 0;
117
0
    }
118
3.42M
    rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0,
119
3.42M
                             libctx, propq);
120
3.42M
    if (rv <= 0)
121
2.48M
        ASN1_item_ex_free(pval, it);
122
3.42M
    return rv;
123
3.42M
}
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
324k
{
129
324k
    return asn1_item_ex_d2i_intern(pval, in, len, it, tag, aclass, opt, ctx,
130
324k
                                   NULL, NULL);
131
324k
}
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
3.10M
{
138
3.10M
    ASN1_TLC c;
139
3.10M
    ASN1_VALUE *ptmpval = NULL;
140
141
3.10M
    if (pval == NULL)
142
2.96M
        pval = &ptmpval;
143
3.10M
    asn1_tlc_clear_nc(&c);
144
3.10M
    if (asn1_item_ex_d2i_intern(pval, in, len, it, -1, 0, 0, &c, libctx,
145
3.10M
                                propq) > 0)
146
684k
        return *pval;
147
2.41M
    return NULL;
148
3.10M
}
149
150
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval,
151
                          const unsigned char **in, long len,
152
                          const ASN1_ITEM *it)
153
3.01M
{
154
3.01M
    return ASN1_item_d2i_ex(pval, in, len, it, NULL, NULL);
155
3.01M
}
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
14.4M
{
168
14.4M
    const ASN1_TEMPLATE *tt, *errtt = NULL;
169
14.4M
    const ASN1_EXTERN_FUNCS *ef;
170
14.4M
    const ASN1_AUX *aux;
171
14.4M
    ASN1_aux_cb *asn1_cb;
172
14.4M
    const unsigned char *p = NULL, *q;
173
14.4M
    unsigned char oclass;
174
14.4M
    char seq_eoc, seq_nolen, cst, isopt;
175
14.4M
    long tmplen;
176
14.4M
    int i;
177
14.4M
    int otag;
178
14.4M
    int ret = 0;
179
14.4M
    ASN1_VALUE **pchptr;
180
181
14.4M
    if (pval == NULL || it == NULL) {
182
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
183
0
        return 0;
184
0
    }
185
14.4M
    if (len <= 0) {
186
1.53k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
187
1.53k
        return 0;
188
1.53k
    }
189
14.4M
    aux = it->funcs;
190
14.4M
    if (aux && aux->asn1_cb)
191
1.40M
        asn1_cb = aux->asn1_cb;
192
13.0M
    else
193
13.0M
        asn1_cb = 0;
194
195
14.4M
    if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
196
1
        ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_TOO_DEEP);
197
1
        goto err;
198
1
    }
199
200
14.4M
    switch (it->itype) {
201
8.97M
    case ASN1_ITYPE_PRIMITIVE:
202
8.97M
        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
3.10M
            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
3.10M
            return asn1_template_ex_d2i(pval, in, len, it->templates, opt, ctx,
215
3.10M
                                        depth, libctx, propq);
216
3.10M
        }
217
5.87M
        return asn1_d2i_ex_primitive(pval, in, len, it,
218
5.87M
                                     tag, aclass, opt, ctx);
219
220
947k
    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
947k
        if (tag != -1) {
226
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
227
0
            goto err;
228
0
        }
229
230
947k
        p = *in;
231
        /* Just read in tag and class */
232
947k
        ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
233
947k
                              &p, len, -1, 0, 1, ctx);
234
947k
        if (!ret) {
235
1.54k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
236
1.54k
            goto err;
237
1.54k
        }
238
239
        /* Must be UNIVERSAL class */
240
945k
        if (oclass != V_ASN1_UNIVERSAL) {
241
            /* If OPTIONAL, assume this is OK */
242
16.4k
            if (opt)
243
10.5k
                return -1;
244
16.4k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
245
5.91k
            goto err;
246
16.4k
        }
247
248
        /* Check tag matches bit map */
249
929k
        if (!(ASN1_tag2bit(otag) & it->utype)) {
250
            /* If OPTIONAL, assume this is OK */
251
67.7k
            if (opt)
252
1.46k
                return -1;
253
67.7k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_WRONG_TAG);
254
66.3k
            goto err;
255
67.7k
        }
256
861k
        return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx);
257
258
324k
    case ASN1_ITYPE_EXTERN:
259
        /* Use new style d2i */
260
324k
        ef = it->funcs;
261
324k
        if (ef->asn1_ex_d2i_ex != NULL)
262
140k
            return ef->asn1_ex_d2i_ex(pval, in, len, it, tag, aclass, opt, ctx,
263
140k
                                      libctx, propq);
264
184k
        return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx);
265
266
295k
    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
295k
        if (tag != -1) {
272
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE);
273
0
            goto err;
274
0
        }
275
276
295k
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
277
0
            goto auxerr;
278
295k
        if (*pval) {
279
            /* Free up and zero CHOICE value if initialised */
280
29.1k
            i = ossl_asn1_get_choice_selector(pval, it);
281
29.1k
            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
265k
        } 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
295k
        p = *in;
293
996k
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
294
892k
            pchptr = ossl_asn1_get_field_ptr(pval, tt);
295
            /*
296
             * We mark field as OPTIONAL so its absence can be recognised.
297
             */
298
892k
            ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth,
299
892k
                                       libctx, propq);
300
            /* If field not present, try the next one */
301
892k
            if (ret == -1)
302
701k
                continue;
303
            /* If positive return, read OK, break loop */
304
190k
            if (ret > 0)
305
99.1k
                break;
306
            /*
307
             * Must be an ASN1 parsing error.
308
             * Free up any partial choice value
309
             */
310
91.0k
            ossl_asn1_template_free(pchptr, tt);
311
91.0k
            errtt = tt;
312
91.0k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
313
91.0k
            goto err;
314
190k
        }
315
316
        /* Did we fall off the end without reading anything? */
317
204k
        if (i == it->tcount) {
318
            /* If OPTIONAL, this is OK */
319
104k
            if (opt) {
320
                /* Free and zero it */
321
1
                ASN1_item_ex_free(pval, it);
322
1
                return -1;
323
1
            }
324
104k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
325
104k
            goto err;
326
104k
        }
327
328
99.1k
        ossl_asn1_set_choice_selector(pval, i, it);
329
330
99.1k
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
331
0
            goto auxerr;
332
99.1k
        *in = p;
333
99.1k
        return 1;
334
335
122k
    case ASN1_ITYPE_NDEF_SEQUENCE:
336
3.93M
    case ASN1_ITYPE_SEQUENCE:
337
3.93M
        p = *in;
338
3.93M
        tmplen = len;
339
340
        /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
341
3.93M
        if (tag == -1) {
342
3.77M
            tag = V_ASN1_SEQUENCE;
343
3.77M
            aclass = V_ASN1_UNIVERSAL;
344
3.77M
        }
345
        /* Get SEQUENCE length and update len, p */
346
3.93M
        ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
347
3.93M
                              &p, len, tag, aclass, opt, ctx);
348
3.93M
        if (!ret) {
349
624k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
350
624k
            goto err;
351
3.30M
        } else if (ret == -1)
352
185k
            return -1;
353
3.12M
        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
3.12M
        else
359
3.12M
            seq_nolen = seq_eoc;
360
3.12M
        if (!cst) {
361
29.3k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
362
29.3k
            goto err;
363
29.3k
        }
364
365
3.09M
        if (*pval == NULL
366
3.09M
                && !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
3.09M
        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
12.5M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
376
9.45M
            if (tt->flags & ASN1_TFLG_ADB_MASK) {
377
71.4k
                const ASN1_TEMPLATE *seqtt;
378
71.4k
                ASN1_VALUE **pseqval;
379
71.4k
                seqtt = ossl_asn1_do_adb(*pval, tt, 0);
380
71.4k
                if (seqtt == NULL)
381
51.1k
                    continue;
382
20.2k
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
383
20.2k
                ossl_asn1_template_free(pseqval, seqtt);
384
20.2k
            }
385
9.45M
        }
386
387
        /* Get each field entry */
388
7.44M
        for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
389
6.10M
            const ASN1_TEMPLATE *seqtt;
390
6.10M
            ASN1_VALUE **pseqval;
391
6.10M
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
392
6.10M
            if (seqtt == NULL)
393
0
                goto err;
394
6.10M
            pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
395
            /* Have we ran out of data? */
396
6.10M
            if (!len)
397
108k
                break;
398
5.99M
            q = p;
399
5.99M
            if (asn1_check_eoc(&p, len)) {
400
228k
                if (!seq_eoc) {
401
1.32k
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
402
1.32k
                    goto err;
403
1.32k
                }
404
227k
                len -= p - q;
405
227k
                seq_eoc = 0;
406
227k
                break;
407
228k
            }
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
5.76M
            if (i == (it->tcount - 1))
415
1.55M
                isopt = 0;
416
4.20M
            else
417
4.20M
                isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
418
            /*
419
             * attempt to read in field, allowing each to be OPTIONAL
420
             */
421
422
5.76M
            ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx,
423
5.76M
                                       depth, libctx, propq);
424
5.76M
            if (!ret) {
425
1.42M
                errtt = seqtt;
426
1.42M
                goto err;
427
4.34M
            } else if (ret == -1) {
428
                /*
429
                 * OPTIONAL component absent. Free and zero the field.
430
                 */
431
514k
                ossl_asn1_template_free(pseqval, seqtt);
432
514k
                continue;
433
514k
            }
434
            /* Update length */
435
3.83M
            len -= p - q;
436
3.83M
        }
437
438
        /* Check for EOC if expecting one */
439
1.67M
        if (seq_eoc && !asn1_check_eoc(&p, len)) {
440
16.8k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
441
16.8k
            goto err;
442
16.8k
        }
443
        /* Check all data read */
444
1.65M
        if (!seq_nolen && len) {
445
633
            ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
446
633
            goto err;
447
633
        }
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
2.13M
        for (; i < it->tcount; tt++, i++) {
455
509k
            const ASN1_TEMPLATE *seqtt;
456
509k
            seqtt = ossl_asn1_do_adb(*pval, tt, 1);
457
509k
            if (seqtt == NULL)
458
0
                goto err;
459
509k
            if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
460
476k
                ASN1_VALUE **pseqval;
461
476k
                pseqval = ossl_asn1_get_field_ptr(pval, seqtt);
462
476k
                ossl_asn1_template_free(pseqval, seqtt);
463
476k
            } else {
464
33.8k
                errtt = seqtt;
465
33.8k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_FIELD_MISSING);
466
33.8k
                goto err;
467
33.8k
            }
468
509k
        }
469
        /* Save encoding */
470
1.62M
        if (!ossl_asn1_enc_save(pval, *in, p - *in, it))
471
0
            goto auxerr;
472
1.62M
        if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
473
61
            goto auxerr;
474
1.62M
        *in = p;
475
1.62M
        return 1;
476
477
0
    default:
478
0
        return 0;
479
14.4M
    }
480
61
 auxerr:
481
61
    ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
482
2.39M
 err:
483
2.39M
    if (errtt)
484
1.54M
        ERR_add_error_data(4, "Field=", errtt->field_name,
485
1.54M
                           ", Type=", it->sname);
486
851k
    else
487
851k
        ERR_add_error_data(2, "Type=", it->sname);
488
2.39M
    return 0;
489
61
}
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
9.75M
{
502
9.75M
    int flags, aclass;
503
9.75M
    int ret;
504
9.75M
    long len;
505
9.75M
    const unsigned char *p, *q;
506
9.75M
    char exp_eoc;
507
9.75M
    if (!val)
508
0
        return 0;
509
9.75M
    flags = tt->flags;
510
9.75M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
511
512
9.75M
    p = *in;
513
514
    /* Check if EXPLICIT tag expected */
515
9.75M
    if (flags & ASN1_TFLG_EXPTAG) {
516
414k
        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
414k
        ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
522
414k
                              &p, inlen, tt->tag, aclass, opt, ctx);
523
414k
        q = p;
524
414k
        if (!ret) {
525
51.8k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
526
51.8k
            return 0;
527
362k
        } else if (ret == -1)
528
313k
            return -1;
529
49.4k
        if (!cst) {
530
589
            ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
531
589
            return 0;
532
589
        }
533
        /* We've found the field so it can't be OPTIONAL now */
534
48.8k
        ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth, libctx,
535
48.8k
                                      propq);
536
48.8k
        if (!ret) {
537
16.4k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
538
16.4k
            return 0;
539
16.4k
        }
540
        /* We read the field in OK so update length */
541
32.3k
        len -= p - q;
542
32.3k
        if (exp_eoc) {
543
            /* If NDEF we must have an EOC here */
544
23.2k
            if (!asn1_check_eoc(&p, len)) {
545
1.05k
                ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
546
1.05k
                goto err;
547
1.05k
            }
548
23.2k
        } else {
549
            /*
550
             * Otherwise we must hit the EXPLICIT tag end or its an error
551
             */
552
9.09k
            if (len) {
553
201
                ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
554
201
                goto err;
555
201
            }
556
9.09k
        }
557
32.3k
    } else
558
9.34M
        return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth,
559
9.34M
                                       libctx, propq);
560
561
31.0k
    *in = p;
562
31.0k
    return 1;
563
564
1.25k
 err:
565
1.25k
    return 0;
566
9.75M
}
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
9.39M
{
574
9.39M
    int flags, aclass;
575
9.39M
    int ret;
576
9.39M
    ASN1_VALUE *tval;
577
9.39M
    const unsigned char *p, *q;
578
9.39M
    if (!val)
579
0
        return 0;
580
9.39M
    flags = tt->flags;
581
9.39M
    aclass = flags & ASN1_TFLG_TAG_CLASS;
582
583
9.39M
    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
9.39M
    if (tt->flags & ASN1_TFLG_EMBED) {
590
794k
        tval = (ASN1_VALUE *)val;
591
794k
        val = &tval;
592
794k
    }
593
594
9.39M
    if (flags & ASN1_TFLG_SK_MASK) {
595
        /* SET OF, SEQUENCE OF */
596
3.34M
        int sktag, skaclass;
597
3.34M
        char sk_eoc;
598
        /* First work out expected inner tag value */
599
3.34M
        if (flags & ASN1_TFLG_IMPTAG) {
600
119k
            sktag = tt->tag;
601
119k
            skaclass = aclass;
602
3.22M
        } else {
603
3.22M
            skaclass = V_ASN1_UNIVERSAL;
604
3.22M
            if (flags & ASN1_TFLG_SET_OF)
605
2.77M
                sktag = V_ASN1_SET;
606
453k
            else
607
453k
                sktag = V_ASN1_SEQUENCE;
608
3.22M
        }
609
        /* Get the tag */
610
3.34M
        ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
611
3.34M
                              &p, len, sktag, skaclass, opt, ctx);
612
3.34M
        if (!ret) {
613
179k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
614
179k
            return 0;
615
3.16M
        } else if (ret == -1)
616
84.2k
            return -1;
617
3.07M
        if (*val == NULL)
618
3.06M
            *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
619
15.6k
        else {
620
            /*
621
             * We've got a valid STACK: free up any items present
622
             */
623
15.6k
            STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
624
15.6k
            ASN1_VALUE *vtmp;
625
15.6k
            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
15.6k
        }
630
631
3.07M
        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
7.91M
        while (len > 0) {
638
5.04M
            ASN1_VALUE *skfield;
639
5.04M
            q = p;
640
            /* See if EOC found */
641
5.04M
            if (asn1_check_eoc(&p, len)) {
642
39.8k
                if (!sk_eoc) {
643
495
                    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
644
495
                    goto err;
645
495
                }
646
39.4k
                len -= p - q;
647
39.4k
                sk_eoc = 0;
648
39.4k
                break;
649
39.8k
            }
650
5.00M
            skfield = NULL;
651
5.00M
            if (asn1_item_embed_d2i(&skfield, &p, len,
652
5.00M
                                     ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx,
653
5.00M
                                     depth, libctx, propq) <= 0) {
654
166k
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
655
                /* |skfield| may be partially allocated despite failure. */
656
166k
                ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item));
657
166k
                goto err;
658
166k
            }
659
4.83M
            len -= p - q;
660
4.83M
            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
4.83M
        }
666
2.91M
        if (sk_eoc) {
667
1.33k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
668
1.33k
            goto err;
669
1.33k
        }
670
6.05M
    } else if (flags & ASN1_TFLG_IMPTAG) {
671
        /* IMPLICIT tagging */
672
631k
        ret = asn1_item_embed_d2i(val, &p, len,
673
631k
                                  ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt,
674
631k
                                  ctx, depth, libctx, propq);
675
631k
        if (!ret) {
676
57.4k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
677
57.4k
            goto err;
678
573k
        } else if (ret == -1)
679
539k
            return -1;
680
5.41M
    } else {
681
        /* Nothing special */
682
5.41M
        ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
683
5.41M
                                  -1, 0, opt, ctx, depth, libctx, propq);
684
5.41M
        if (!ret) {
685
1.27M
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
686
1.27M
            goto err;
687
4.14M
        } else if (ret == -1)
688
279k
            return -1;
689
5.41M
    }
690
691
6.80M
    *in = p;
692
6.80M
    return 1;
693
694
1.50M
 err:
695
1.50M
    return 0;
696
9.39M
}
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
6.73M
{
703
6.73M
    int ret = 0, utype;
704
6.73M
    long plen;
705
6.73M
    char cst, inf, free_cont = 0;
706
6.73M
    const unsigned char *p;
707
6.73M
    BUF_MEM buf = { 0, NULL, 0, 0 };
708
6.73M
    const unsigned char *cont = NULL;
709
6.73M
    long len;
710
711
6.73M
    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
6.73M
    if (it->itype == ASN1_ITYPE_MSTRING) {
717
861k
        utype = tag;
718
861k
        tag = -1;
719
861k
    } else
720
5.87M
        utype = it->utype;
721
722
6.73M
    if (utype == V_ASN1_ANY) {
723
        /* If type is ANY need to figure out type from tag */
724
1.41M
        unsigned char oclass;
725
1.41M
        if (tag >= 0) {
726
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
727
0
            return 0;
728
0
        }
729
1.41M
        if (opt) {
730
0
            ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
731
0
            return 0;
732
0
        }
733
1.41M
        p = *in;
734
1.41M
        ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
735
1.41M
                              &p, inlen, -1, 0, 0, ctx);
736
1.41M
        if (!ret) {
737
1.20k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
738
1.20k
            return 0;
739
1.20k
        }
740
1.41M
        if (oclass != V_ASN1_UNIVERSAL)
741
106k
            utype = V_ASN1_OTHER;
742
1.41M
    }
743
6.73M
    if (tag == -1) {
744
6.25M
        tag = utype;
745
6.25M
        aclass = V_ASN1_UNIVERSAL;
746
6.25M
    }
747
6.73M
    p = *in;
748
    /* Check header */
749
6.73M
    ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
750
6.73M
                          &p, inlen, tag, aclass, opt, ctx);
751
6.73M
    if (!ret) {
752
1.22M
        ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
753
1.22M
        return 0;
754
5.51M
    } else if (ret == -1)
755
621k
        return -1;
756
4.89M
    ret = 0;
757
    /* SEQUENCE, SET and "OTHER" are left in encoded form */
758
4.89M
    if ((utype == V_ASN1_SEQUENCE)
759
4.89M
        || (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
345k
        if (utype == V_ASN1_OTHER) {
765
106k
            asn1_tlc_clear(ctx);
766
106k
        }
767
        /* SEQUENCE and SET must be constructed */
768
239k
        else if (!cst) {
769
1.14k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
770
1.14k
            return 0;
771
1.14k
        }
772
773
344k
        cont = *in;
774
        /* If indefinite length constructed find the real end */
775
344k
        if (inf) {
776
120k
            if (!asn1_find_end(&p, plen, inf))
777
26.9k
                goto err;
778
93.9k
            len = p - cont;
779
223k
        } else {
780
223k
            len = p - cont + plen;
781
223k
            p += plen;
782
223k
        }
783
4.54M
    } else if (cst) {
784
288k
        if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN
785
288k
            || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER
786
288k
            || utype == V_ASN1_ENUMERATED) {
787
1.82k
            ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
788
1.82k
            return 0;
789
1.82k
        }
790
791
        /* Free any returned 'buf' content */
792
286k
        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
286k
        if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) {
800
1.75k
            goto err;
801
1.75k
        }
802
284k
        len = buf.length;
803
        /* Append a final null to string */
804
284k
        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
284k
        buf.data[len] = 0;
809
284k
        cont = (const unsigned char *)buf.data;
810
4.25M
    } else {
811
4.25M
        cont = p;
812
4.25M
        len = plen;
813
4.25M
        p += plen;
814
4.25M
    }
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
4.86M
    if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it))
819
40.4k
        goto err;
820
821
4.82M
    *in = p;
822
4.82M
    ret = 1;
823
4.88M
 err:
824
4.88M
    if (free_cont)
825
2.95k
        OPENSSL_free(buf.data);
826
4.88M
    return ret;
827
4.82M
}
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
4.86M
{
834
4.86M
    ASN1_VALUE **opval = NULL;
835
4.86M
    ASN1_STRING *stmp;
836
4.86M
    ASN1_TYPE *typ = NULL;
837
4.86M
    int ret = 0;
838
4.86M
    const ASN1_PRIMITIVE_FUNCS *pf;
839
4.86M
    ASN1_INTEGER **tint;
840
4.86M
    pf = it->funcs;
841
842
4.86M
    if (pf && pf->prim_c2i)
843
256k
        return pf->prim_c2i(pval, cont, len, utype, free_cont, it);
844
    /* If ANY type clear type and set pointer to internal value */
845
4.60M
    if (it->utype == V_ASN1_ANY) {
846
1.38M
        if (*pval == NULL) {
847
1.37M
            typ = ASN1_TYPE_new();
848
1.37M
            if (typ == NULL)
849
0
                goto err;
850
1.37M
            *pval = (ASN1_VALUE *)typ;
851
1.37M
        } else
852
9.99k
            typ = (ASN1_TYPE *)*pval;
853
854
1.38M
        if (utype != typ->type)
855
1.38M
            ASN1_TYPE_set(typ, utype, NULL);
856
1.38M
        opval = pval;
857
1.38M
        pval = &typ->value.asn1_value;
858
1.38M
    }
859
4.60M
    switch (utype) {
860
1.29M
    case V_ASN1_OBJECT:
861
1.29M
        if (!ossl_c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
862
983
            goto err;
863
1.29M
        break;
864
865
1.29M
    case V_ASN1_NULL:
866
892k
        if (len) {
867
79
            ERR_raise(ERR_LIB_ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
868
79
            goto err;
869
79
        }
870
892k
        *pval = (ASN1_VALUE *)1;
871
892k
        break;
872
873
11.2k
    case V_ASN1_BOOLEAN:
874
11.2k
        if (len != 1) {
875
175
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
876
175
            goto err;
877
11.0k
        } else {
878
11.0k
            ASN1_BOOLEAN *tbool;
879
11.0k
            tbool = (ASN1_BOOLEAN *)pval;
880
11.0k
            *tbool = *cont;
881
11.0k
        }
882
11.0k
        break;
883
884
398k
    case V_ASN1_BIT_STRING:
885
398k
        if (!ossl_c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len))
886
526
            goto err;
887
398k
        break;
888
889
629k
    case V_ASN1_INTEGER:
890
648k
    case V_ASN1_ENUMERATED:
891
648k
        tint = (ASN1_INTEGER **)pval;
892
648k
        if (!ossl_c2i_ASN1_INTEGER(tint, &cont, len))
893
27.4k
            goto err;
894
        /* Fixup type to match the expected form */
895
621k
        (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
896
621k
        break;
897
898
250k
    case V_ASN1_OCTET_STRING:
899
251k
    case V_ASN1_NUMERICSTRING:
900
263k
    case V_ASN1_PRINTABLESTRING:
901
272k
    case V_ASN1_T61STRING:
902
273k
    case V_ASN1_VIDEOTEXSTRING:
903
438k
    case V_ASN1_IA5STRING:
904
506k
    case V_ASN1_UTCTIME:
905
586k
    case V_ASN1_GENERALIZEDTIME:
906
587k
    case V_ASN1_GRAPHICSTRING:
907
588k
    case V_ASN1_VISIBLESTRING:
908
588k
    case V_ASN1_GENERALSTRING:
909
596k
    case V_ASN1_UNIVERSALSTRING:
910
859k
    case V_ASN1_BMPSTRING:
911
870k
    case V_ASN1_UTF8STRING:
912
968k
    case V_ASN1_OTHER:
913
1.03M
    case V_ASN1_SET:
914
1.18M
    case V_ASN1_SEQUENCE:
915
1.35M
    default:
916
1.35M
        if (utype == V_ASN1_BMPSTRING && (len & 1)) {
917
68
            ERR_raise(ERR_LIB_ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
918
68
            goto err;
919
68
        }
920
1.35M
        if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
921
67
            ERR_raise(ERR_LIB_ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
922
67
            goto err;
923
67
        }
924
        /* All based on ASN1_STRING and handled the same */
925
1.35M
        if (*pval == NULL) {
926
566k
            stmp = ASN1_STRING_type_new(utype);
927
566k
            if (stmp == NULL) {
928
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
929
0
                goto err;
930
0
            }
931
566k
            *pval = (ASN1_VALUE *)stmp;
932
791k
        } else {
933
791k
            stmp = (ASN1_STRING *)*pval;
934
791k
            stmp->type = utype;
935
791k
        }
936
        /* If we've already allocated a buffer use it */
937
1.35M
        if (*free_cont) {
938
283k
            OPENSSL_free(stmp->data);
939
283k
            stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */
940
283k
            stmp->length = len;
941
283k
            *free_cont = 0;
942
1.07M
        } else {
943
1.07M
            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
1.07M
        }
950
1.35M
        break;
951
4.60M
    }
952
    /* If ASN1_ANY and NULL type fix up value */
953
4.57M
    if (typ && (utype == V_ASN1_NULL))
954
892k
        typ->value.ptr = NULL;
955
956
4.57M
    ret = 1;
957
4.60M
 err:
958
4.60M
    if (!ret) {
959
29.3k
        ASN1_TYPE_free(typ);
960
29.3k
        if (opval)
961
1.50k
            *opval = NULL;
962
29.3k
    }
963
4.60M
    return ret;
964
4.57M
}
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
120k
{
975
120k
    uint32_t expected_eoc;
976
120k
    long plen;
977
120k
    const unsigned char *p = *in, *q;
978
    /* If not indefinite length constructed just add length */
979
120k
    if (inf == 0) {
980
0
        *in += len;
981
0
        return 1;
982
0
    }
983
120k
    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
76.7M
    while (len > 0) {
991
76.7M
        if (asn1_check_eoc(&p, len)) {
992
5.80M
            expected_eoc--;
993
5.80M
            if (expected_eoc == 0)
994
93.9k
                break;
995
5.70M
            len -= 2;
996
5.70M
            continue;
997
5.80M
        }
998
70.9M
        q = p;
999
        /* Just read in a header: only care about the length */
1000
70.9M
        if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1001
70.9M
                             -1, 0, 0, NULL)) {
1002
17.5k
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1003
17.5k
            return 0;
1004
17.5k
        }
1005
70.9M
        if (inf) {
1006
11.2M
            if (expected_eoc == UINT32_MAX) {
1007
0
                ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1008
0
                return 0;
1009
0
            }
1010
11.2M
            expected_eoc++;
1011
59.6M
        } else {
1012
59.6M
            p += plen;
1013
59.6M
        }
1014
70.9M
        len -= p - q;
1015
70.9M
    }
1016
103k
    if (expected_eoc) {
1017
9.43k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1018
9.43k
        return 0;
1019
9.43k
    }
1020
93.9k
    *in = p;
1021
93.9k
    return 1;
1022
103k
}
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
35.9k
# 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
322k
{
1043
322k
    const unsigned char *p, *q;
1044
322k
    long plen;
1045
322k
    char cst, ininf;
1046
322k
    p = *in;
1047
322k
    inf &= 1;
1048
    /*
1049
     * If no buffer and not indefinite length constructed just pass over the
1050
     * encoded data
1051
     */
1052
322k
    if (!buf && !inf) {
1053
0
        *in += len;
1054
0
        return 1;
1055
0
    }
1056
906k
    while (len > 0) {
1057
606k
        q = p;
1058
        /* Check for EOC */
1059
606k
        if (asn1_check_eoc(&p, len)) {
1060
            /*
1061
             * EOC is illegal outside indefinite length constructed form
1062
             */
1063
20.4k
            if (!inf) {
1064
244
                ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC);
1065
244
                return 0;
1066
244
            }
1067
20.1k
            inf = 0;
1068
20.1k
            break;
1069
20.4k
        }
1070
1071
586k
        if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1072
586k
                             len, tag, aclass, 0, NULL)) {
1073
923
            ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR);
1074
923
            return 0;
1075
923
        }
1076
1077
        /* If indefinite length constructed update max length */
1078
585k
        if (cst) {
1079
35.9k
            if (depth >= ASN1_MAX_STRING_NEST) {
1080
125
                ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_ASN1_STRING);
1081
125
                return 0;
1082
125
            }
1083
35.8k
            if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1))
1084
1.39k
                return 0;
1085
549k
        } else if (plen && !collect_data(buf, &p, plen))
1086
0
            return 0;
1087
583k
        len -= p - q;
1088
583k
    }
1089
319k
    if (inf) {
1090
462
        ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC);
1091
462
        return 0;
1092
462
    }
1093
319k
    *in = p;
1094
319k
    return 1;
1095
319k
}
1096
1097
static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1098
520k
{
1099
520k
    int len;
1100
520k
    if (buf) {
1101
520k
        len = buf->length;
1102
520k
        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
520k
        memcpy(buf->data + len, *p, plen);
1107
520k
    }
1108
520k
    *p += plen;
1109
520k
    return 1;
1110
520k
}
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
89.0M
{
1116
89.0M
    const unsigned char *p;
1117
1118
89.0M
    if (len < 2)
1119
11.6k
        return 0;
1120
89.0M
    p = *in;
1121
89.0M
    if (p[0] == '\0' && p[1] == '\0') {
1122
6.68M
        *in += 2;
1123
6.68M
        return 1;
1124
6.68M
    }
1125
82.3M
    return 0;
1126
89.0M
}
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
88.3M
{
1140
88.3M
    int i;
1141
88.3M
    int ptag, pclass;
1142
88.3M
    long plen;
1143
88.3M
    const unsigned char *p, *q;
1144
88.3M
    p = *in;
1145
88.3M
    q = p;
1146
1147
88.3M
    if (len <= 0) {
1148
16
        ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL);
1149
16
        goto err;
1150
16
    }
1151
88.3M
    if (ctx != NULL && ctx->valid) {
1152
3.38M
        i = ctx->ret;
1153
3.38M
        plen = ctx->plen;
1154
3.38M
        pclass = ctx->pclass;
1155
3.38M
        ptag = ctx->ptag;
1156
3.38M
        p += ctx->hdrlen;
1157
84.9M
    } else {
1158
84.9M
        i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
1159
84.9M
        if (ctx != NULL) {
1160
13.4M
            ctx->ret = i;
1161
13.4M
            ctx->plen = plen;
1162
13.4M
            ctx->pclass = pclass;
1163
13.4M
            ctx->ptag = ptag;
1164
13.4M
            ctx->hdrlen = p - q;
1165
13.4M
            ctx->valid = 1;
1166
            /*
1167
             * If definite length, and no error, length + header can't exceed
1168
             * total amount of data available.
1169
             */
1170
13.4M
            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
13.4M
        }
1175
84.9M
    }
1176
1177
88.3M
    if ((i & 0x80) != 0) {
1178
106k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER);
1179
106k
        goto err;
1180
106k
    }
1181
88.2M
    if (exptag >= 0) {
1182
14.2M
        if (exptag != ptag || expclass != pclass) {
1183
            /*
1184
             * If type is OPTIONAL, not an error: indicate missing type.
1185
             */
1186
3.19M
            if (opt != 0)
1187
1.20M
                return -1;
1188
3.19M
            ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG);
1189
1.99M
            goto err;
1190
3.19M
        }
1191
        /*
1192
         * We have a tag and class match: assume we are going to do something
1193
         * with it
1194
         */
1195
11.0M
        asn1_tlc_clear(ctx);
1196
11.0M
    }
1197
1198
85.0M
    if ((i & 1) != 0)
1199
13.8M
        plen = len - (p - q);
1200
1201
85.0M
    if (inf != NULL)
1202
82.6M
        *inf = i & 1;
1203
1204
85.0M
    if (cst != NULL)
1205
8.65M
        *cst = i & V_ASN1_CONSTRUCTED;
1206
1207
85.0M
    if (olen != NULL)
1208
82.6M
        *olen = plen;
1209
1210
85.0M
    if (oclass != NULL)
1211
2.35M
        *oclass = pclass;
1212
1213
85.0M
    if (otag != NULL)
1214
2.35M
        *otag = ptag;
1215
1216
85.0M
    *in = p;
1217
85.0M
    return 1;
1218
1219
2.09M
 err:
1220
2.09M
    asn1_tlc_clear(ctx);
1221
2.09M
    return 0;
1222
88.2M
}