Coverage Report

Created: 2023-09-25 06:45

/src/openssl111/crypto/asn1/tasn_new.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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 <openssl/asn1.h>
12
#include <openssl/objects.h>
13
#include <openssl/err.h>
14
#include <openssl/asn1t.h>
15
#include <string.h>
16
#include "asn1_local.h"
17
18
static int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
19
                               int embed);
20
static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
21
                              int embed);
22
static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
23
static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
24
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
25
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
26
27
ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
28
21.0M
{
29
21.0M
    ASN1_VALUE *ret = NULL;
30
21.0M
    if (ASN1_item_ex_new(&ret, it) > 0)
31
21.0M
        return ret;
32
0
    return NULL;
33
21.0M
}
34
35
/* Allocate an ASN1 structure */
36
37
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
38
27.6M
{
39
27.6M
    return asn1_item_embed_new(pval, it, 0);
40
27.6M
}
41
42
int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
43
36.6M
{
44
36.6M
    const ASN1_TEMPLATE *tt = NULL;
45
36.6M
    const ASN1_EXTERN_FUNCS *ef;
46
36.6M
    const ASN1_AUX *aux = it->funcs;
47
36.6M
    ASN1_aux_cb *asn1_cb;
48
36.6M
    ASN1_VALUE **pseqval;
49
36.6M
    int i;
50
36.6M
    if (aux && aux->asn1_cb)
51
1.06M
        asn1_cb = aux->asn1_cb;
52
35.5M
    else
53
35.5M
        asn1_cb = 0;
54
55
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
56
    OPENSSL_mem_debug_push(it->sname ? it->sname : "asn1_item_embed_new");
57
#endif
58
59
36.6M
    switch (it->itype) {
60
61
387k
    case ASN1_ITYPE_EXTERN:
62
387k
        ef = it->funcs;
63
387k
        if (ef && ef->asn1_ex_new) {
64
387k
            if (!ef->asn1_ex_new(pval, it))
65
0
                goto memerr;
66
387k
        }
67
387k
        break;
68
69
15.7M
    case ASN1_ITYPE_PRIMITIVE:
70
15.7M
        if (it->templates) {
71
0
            if (!asn1_template_new(pval, it->templates))
72
0
                goto memerr;
73
15.7M
        } else if (!asn1_primitive_new(pval, it, embed))
74
0
            goto memerr;
75
15.7M
        break;
76
77
15.7M
    case ASN1_ITYPE_MSTRING:
78
7.91M
        if (!asn1_primitive_new(pval, it, embed))
79
0
            goto memerr;
80
7.91M
        break;
81
82
7.91M
    case ASN1_ITYPE_CHOICE:
83
1.54M
        if (asn1_cb) {
84
84.0k
            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
85
84.0k
            if (!i)
86
0
                goto auxerr;
87
84.0k
            if (i == 2) {
88
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
89
                OPENSSL_mem_debug_pop();
90
#endif
91
0
                return 1;
92
0
            }
93
84.0k
        }
94
1.54M
        if (embed) {
95
19.5k
            memset(*pval, 0, it->size);
96
1.52M
        } else {
97
1.52M
            *pval = OPENSSL_zalloc(it->size);
98
1.52M
            if (*pval == NULL)
99
0
                goto memerr;
100
1.52M
        }
101
1.54M
        asn1_set_choice_selector(pval, -1, it);
102
1.54M
        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
103
0
            goto auxerr2;
104
1.54M
        break;
105
106
1.54M
    case ASN1_ITYPE_NDEF_SEQUENCE:
107
11.0M
    case ASN1_ITYPE_SEQUENCE:
108
11.0M
        if (asn1_cb) {
109
732k
            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
110
732k
            if (!i)
111
0
                goto auxerr;
112
732k
            if (i == 2) {
113
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
114
                OPENSSL_mem_debug_pop();
115
#endif
116
127k
                return 1;
117
127k
            }
118
732k
        }
119
10.9M
        if (embed) {
120
770k
            memset(*pval, 0, it->size);
121
10.1M
        } else {
122
10.1M
            *pval = OPENSSL_zalloc(it->size);
123
10.1M
            if (*pval == NULL)
124
0
                goto memerr;
125
10.1M
        }
126
        /* 0 : init. lock */
127
10.9M
        if (asn1_do_lock(pval, 0, it) < 0) {
128
0
            if (!embed) {
129
0
                OPENSSL_free(*pval);
130
0
                *pval = NULL;
131
0
            }
132
0
            goto memerr;
133
0
        }
134
10.9M
        asn1_enc_init(pval, it);
135
36.7M
        for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
136
25.7M
            pseqval = asn1_get_field_ptr(pval, tt);
137
25.7M
            if (!asn1_template_new(pseqval, tt))
138
0
                goto memerr2;
139
25.7M
        }
140
10.9M
        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
141
0
            goto auxerr2;
142
10.9M
        break;
143
36.6M
    }
144
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
145
    OPENSSL_mem_debug_pop();
146
#endif
147
36.5M
    return 1;
148
149
0
 memerr2:
150
0
    asn1_item_embed_free(pval, it, embed);
151
0
 memerr:
152
0
    ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ERR_R_MALLOC_FAILURE);
153
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
154
    OPENSSL_mem_debug_pop();
155
#endif
156
0
    return 0;
157
158
0
 auxerr2:
159
0
    asn1_item_embed_free(pval, it, embed);
160
0
 auxerr:
161
0
    ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ASN1_R_AUX_ERROR);
162
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
163
    OPENSSL_mem_debug_pop();
164
#endif
165
0
    return 0;
166
167
0
}
168
169
static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
170
14.0M
{
171
14.0M
    const ASN1_EXTERN_FUNCS *ef;
172
173
14.0M
    switch (it->itype) {
174
175
434k
    case ASN1_ITYPE_EXTERN:
176
434k
        ef = it->funcs;
177
434k
        if (ef && ef->asn1_ex_clear)
178
0
            ef->asn1_ex_clear(pval, it);
179
434k
        else
180
434k
            *pval = NULL;
181
434k
        break;
182
183
11.7M
    case ASN1_ITYPE_PRIMITIVE:
184
11.7M
        if (it->templates)
185
142k
            asn1_template_clear(pval, it->templates);
186
11.5M
        else
187
11.5M
            asn1_primitive_clear(pval, it);
188
11.7M
        break;
189
190
283k
    case ASN1_ITYPE_MSTRING:
191
283k
        asn1_primitive_clear(pval, it);
192
283k
        break;
193
194
622k
    case ASN1_ITYPE_CHOICE:
195
1.56M
    case ASN1_ITYPE_SEQUENCE:
196
1.59M
    case ASN1_ITYPE_NDEF_SEQUENCE:
197
1.59M
        *pval = NULL;
198
1.59M
        break;
199
14.0M
    }
200
14.0M
}
201
202
static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
203
74.3M
{
204
74.3M
    const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
205
74.3M
    int embed = tt->flags & ASN1_TFLG_EMBED;
206
74.3M
    ASN1_VALUE *tval;
207
74.3M
    int ret;
208
74.3M
    if (embed) {
209
5.89M
        tval = (ASN1_VALUE *)pval;
210
5.89M
        pval = &tval;
211
5.89M
    }
212
74.3M
    if (tt->flags & ASN1_TFLG_OPTIONAL) {
213
17.0M
        asn1_template_clear(pval, tt);
214
17.0M
        return 1;
215
17.0M
    }
216
    /* If ANY DEFINED BY nothing to do */
217
218
57.3M
    if (tt->flags & ASN1_TFLG_ADB_MASK) {
219
537k
        *pval = NULL;
220
537k
        return 1;
221
537k
    }
222
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
223
    OPENSSL_mem_debug_push(tt->field_name
224
            ? tt->field_name : "asn1_template_new");
225
#endif
226
    /* If SET OF or SEQUENCE OF, its a STACK */
227
56.7M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
228
635k
        STACK_OF(ASN1_VALUE) *skval;
229
635k
        skval = sk_ASN1_VALUE_new_null();
230
635k
        if (!skval) {
231
0
            ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
232
0
            ret = 0;
233
0
            goto done;
234
0
        }
235
635k
        *pval = (ASN1_VALUE *)skval;
236
635k
        ret = 1;
237
635k
        goto done;
238
635k
    }
239
    /* Otherwise pass it back to the item routine */
240
56.1M
    ret = asn1_item_embed_new(pval, it, embed);
241
56.7M
 done:
242
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
243
    OPENSSL_mem_debug_pop();
244
#endif
245
56.7M
    return ret;
246
56.1M
}
247
248
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
249
17.2M
{
250
    /* If ADB or STACK just NULL the field */
251
17.2M
    if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
252
3.17M
        *pval = NULL;
253
14.0M
    else
254
14.0M
        asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
255
17.2M
}
256
257
/*
258
 * NB: could probably combine most of the real XXX_new() behaviour and junk
259
 * all the old functions.
260
 */
261
262
static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
263
                              int embed)
264
59.2M
{
265
59.2M
    ASN1_TYPE *typ;
266
59.2M
    ASN1_STRING *str;
267
59.2M
    int utype;
268
269
59.2M
    if (!it)
270
0
        return 0;
271
272
59.2M
    if (it->funcs) {
273
943k
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
274
943k
        if (embed) {
275
405k
            if (pf->prim_clear) {
276
405k
                pf->prim_clear(pval, it);
277
405k
                return 1;
278
405k
            }
279
537k
        } else if (pf->prim_new) {
280
537k
            return pf->prim_new(pval, it);
281
537k
        }
282
943k
    }
283
284
58.3M
    if (it->itype == ASN1_ITYPE_MSTRING)
285
18.3M
        utype = -1;
286
39.9M
    else
287
39.9M
        utype = it->utype;
288
58.3M
    switch (utype) {
289
23.2M
    case V_ASN1_OBJECT:
290
23.2M
        *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
291
23.2M
        return 1;
292
293
0
    case V_ASN1_BOOLEAN:
294
0
        *(ASN1_BOOLEAN *)pval = it->size;
295
0
        return 1;
296
297
0
    case V_ASN1_NULL:
298
0
        *pval = (ASN1_VALUE *)1;
299
0
        return 1;
300
301
10.3M
    case V_ASN1_ANY:
302
10.3M
        if ((typ = OPENSSL_malloc(sizeof(*typ))) == NULL) {
303
0
            ASN1err(ASN1_F_ASN1_PRIMITIVE_NEW, ERR_R_MALLOC_FAILURE);
304
0
            return 0;
305
0
        }
306
10.3M
        typ->value.ptr = NULL;
307
10.3M
        typ->type = -1;
308
10.3M
        *pval = (ASN1_VALUE *)typ;
309
10.3M
        break;
310
311
24.7M
    default:
312
24.7M
        if (embed) {
313
2.60M
            str = *(ASN1_STRING **)pval;
314
2.60M
            memset(str, 0, sizeof(*str));
315
2.60M
            str->type = utype;
316
2.60M
            str->flags = ASN1_STRING_FLAG_EMBED;
317
22.1M
        } else {
318
22.1M
            str = ASN1_STRING_type_new(utype);
319
22.1M
            *pval = (ASN1_VALUE *)str;
320
22.1M
        }
321
24.7M
        if (it->itype == ASN1_ITYPE_MSTRING && str)
322
18.3M
            str->flags |= ASN1_STRING_FLAG_MSTRING;
323
24.7M
        break;
324
58.3M
    }
325
35.0M
    if (*pval)
326
35.0M
        return 1;
327
0
    return 0;
328
35.0M
}
329
330
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
331
11.8M
{
332
11.8M
    int utype;
333
11.8M
    if (it && it->funcs) {
334
314k
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
335
314k
        if (pf->prim_clear)
336
254k
            pf->prim_clear(pval, it);
337
59.7k
        else
338
59.7k
            *pval = NULL;
339
314k
        return;
340
314k
    }
341
11.5M
    if (!it || (it->itype == ASN1_ITYPE_MSTRING))
342
283k
        utype = -1;
343
11.2M
    else
344
11.2M
        utype = it->utype;
345
11.5M
    if (utype == V_ASN1_BOOLEAN)
346
2.16M
        *(ASN1_BOOLEAN *)pval = it->size;
347
9.39M
    else
348
9.39M
        *pval = NULL;
349
11.5M
}