Coverage Report

Created: 2023-06-08 06:41

/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
3.14M
{
29
3.14M
    ASN1_VALUE *ret = NULL;
30
3.14M
    if (ASN1_item_ex_new(&ret, it) > 0)
31
3.14M
        return ret;
32
0
    return NULL;
33
3.14M
}
34
35
/* Allocate an ASN1 structure */
36
37
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
38
4.86M
{
39
4.86M
    return asn1_item_embed_new(pval, it, 0);
40
4.86M
}
41
42
int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
43
13.0M
{
44
13.0M
    const ASN1_TEMPLATE *tt = NULL;
45
13.0M
    const ASN1_EXTERN_FUNCS *ef;
46
13.0M
    const ASN1_AUX *aux = it->funcs;
47
13.0M
    ASN1_aux_cb *asn1_cb;
48
13.0M
    ASN1_VALUE **pseqval;
49
13.0M
    int i;
50
13.0M
    if (aux && aux->asn1_cb)
51
186k
        asn1_cb = aux->asn1_cb;
52
12.8M
    else
53
12.8M
        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
13.0M
    switch (it->itype) {
60
61
108k
    case ASN1_ITYPE_EXTERN:
62
108k
        ef = it->funcs;
63
108k
        if (ef && ef->asn1_ex_new) {
64
108k
            if (!ef->asn1_ex_new(pval, it))
65
0
                goto memerr;
66
108k
        }
67
108k
        break;
68
69
5.05M
    case ASN1_ITYPE_PRIMITIVE:
70
5.05M
        if (it->templates) {
71
0
            if (!asn1_template_new(pval, it->templates))
72
0
                goto memerr;
73
5.05M
        } else if (!asn1_primitive_new(pval, it, embed))
74
0
            goto memerr;
75
5.05M
        break;
76
77
5.05M
    case ASN1_ITYPE_MSTRING:
78
3.70M
        if (!asn1_primitive_new(pval, it, embed))
79
0
            goto memerr;
80
3.70M
        break;
81
82
3.70M
    case ASN1_ITYPE_CHOICE:
83
112k
        if (asn1_cb) {
84
20.2k
            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
85
20.2k
            if (!i)
86
0
                goto auxerr;
87
20.2k
            if (i == 2) {
88
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
89
                OPENSSL_mem_debug_pop();
90
#endif
91
0
                return 1;
92
0
            }
93
20.2k
        }
94
112k
        if (embed) {
95
0
            memset(*pval, 0, it->size);
96
112k
        } else {
97
112k
            *pval = OPENSSL_zalloc(it->size);
98
112k
            if (*pval == NULL)
99
0
                goto memerr;
100
112k
        }
101
112k
        asn1_set_choice_selector(pval, -1, it);
102
112k
        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
103
0
            goto auxerr2;
104
112k
        break;
105
106
112k
    case ASN1_ITYPE_NDEF_SEQUENCE:
107
4.09M
    case ASN1_ITYPE_SEQUENCE:
108
4.09M
        if (asn1_cb) {
109
149k
            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
110
149k
            if (!i)
111
0
                goto auxerr;
112
149k
            if (i == 2) {
113
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
114
                OPENSSL_mem_debug_pop();
115
#endif
116
3.08k
                return 1;
117
3.08k
            }
118
149k
        }
119
4.09M
        if (embed) {
120
237k
            memset(*pval, 0, it->size);
121
3.85M
        } else {
122
3.85M
            *pval = OPENSSL_zalloc(it->size);
123
3.85M
            if (*pval == NULL)
124
0
                goto memerr;
125
3.85M
        }
126
        /* 0 : init. lock */
127
4.09M
        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
4.09M
        asn1_enc_init(pval, it);
135
12.9M
        for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
136
8.86M
            pseqval = asn1_get_field_ptr(pval, tt);
137
8.86M
            if (!asn1_template_new(pseqval, tt))
138
0
                goto memerr2;
139
8.86M
        }
140
4.09M
        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
141
0
            goto auxerr2;
142
4.09M
        break;
143
13.0M
    }
144
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
145
    OPENSSL_mem_debug_pop();
146
#endif
147
13.0M
    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
514k
{
171
514k
    const ASN1_EXTERN_FUNCS *ef;
172
173
514k
    switch (it->itype) {
174
175
0
    case ASN1_ITYPE_EXTERN:
176
0
        ef = it->funcs;
177
0
        if (ef && ef->asn1_ex_clear)
178
0
            ef->asn1_ex_clear(pval, it);
179
0
        else
180
0
            *pval = NULL;
181
0
        break;
182
183
464k
    case ASN1_ITYPE_PRIMITIVE:
184
464k
        if (it->templates)
185
0
            asn1_template_clear(pval, it->templates);
186
464k
        else
187
464k
            asn1_primitive_clear(pval, it);
188
464k
        break;
189
190
28.6k
    case ASN1_ITYPE_MSTRING:
191
28.6k
        asn1_primitive_clear(pval, it);
192
28.6k
        break;
193
194
14.6k
    case ASN1_ITYPE_CHOICE:
195
21.6k
    case ASN1_ITYPE_SEQUENCE:
196
21.6k
    case ASN1_ITYPE_NDEF_SEQUENCE:
197
21.6k
        *pval = NULL;
198
21.6k
        break;
199
514k
    }
200
514k
}
201
202
static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
203
8.86M
{
204
8.86M
    const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
205
8.86M
    int embed = tt->flags & ASN1_TFLG_EMBED;
206
8.86M
    ASN1_VALUE *tval;
207
8.86M
    int ret;
208
8.86M
    if (embed) {
209
397k
        tval = (ASN1_VALUE *)pval;
210
397k
        pval = &tval;
211
397k
    }
212
8.86M
    if (tt->flags & ASN1_TFLG_OPTIONAL) {
213
637k
        asn1_template_clear(pval, tt);
214
637k
        return 1;
215
637k
    }
216
    /* If ANY DEFINED BY nothing to do */
217
218
8.22M
    if (tt->flags & ASN1_TFLG_ADB_MASK) {
219
7.99k
        *pval = NULL;
220
7.99k
        return 1;
221
7.99k
    }
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
8.21M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
228
14.3k
        STACK_OF(ASN1_VALUE) *skval;
229
14.3k
        skval = sk_ASN1_VALUE_new_null();
230
14.3k
        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
14.3k
        *pval = (ASN1_VALUE *)skval;
236
14.3k
        ret = 1;
237
14.3k
        goto done;
238
14.3k
    }
239
    /* Otherwise pass it back to the item routine */
240
8.20M
    ret = asn1_item_embed_new(pval, it, embed);
241
8.21M
 done:
242
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
243
    OPENSSL_mem_debug_pop();
244
#endif
245
8.21M
    return ret;
246
8.20M
}
247
248
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
249
637k
{
250
    /* If ADB or STACK just NULL the field */
251
637k
    if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
252
123k
        *pval = NULL;
253
514k
    else
254
514k
        asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
255
637k
}
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
8.75M
{
265
8.75M
    ASN1_TYPE *typ;
266
8.75M
    ASN1_STRING *str;
267
8.75M
    int utype;
268
269
8.75M
    if (!it)
270
0
        return 0;
271
272
8.75M
    if (it->funcs) {
273
16.6k
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
274
16.6k
        if (embed) {
275
16.0k
            if (pf->prim_clear) {
276
16.0k
                pf->prim_clear(pval, it);
277
16.0k
                return 1;
278
16.0k
            }
279
16.0k
        } else if (pf->prim_new) {
280
570
            return pf->prim_new(pval, it);
281
570
        }
282
16.6k
    }
283
284
8.73M
    if (it->itype == ASN1_ITYPE_MSTRING)
285
3.70M
        utype = -1;
286
5.03M
    else
287
5.03M
        utype = it->utype;
288
8.73M
    switch (utype) {
289
3.83M
    case V_ASN1_OBJECT:
290
3.83M
        *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
291
3.83M
        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
1.00M
    case V_ASN1_ANY:
302
1.00M
        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
1.00M
        typ->value.ptr = NULL;
307
1.00M
        typ->type = -1;
308
1.00M
        *pval = (ASN1_VALUE *)typ;
309
1.00M
        break;
310
311
3.89M
    default:
312
3.89M
        if (embed) {
313
144k
            str = *(ASN1_STRING **)pval;
314
144k
            memset(str, 0, sizeof(*str));
315
144k
            str->type = utype;
316
144k
            str->flags = ASN1_STRING_FLAG_EMBED;
317
3.75M
        } else {
318
3.75M
            str = ASN1_STRING_type_new(utype);
319
3.75M
            *pval = (ASN1_VALUE *)str;
320
3.75M
        }
321
3.89M
        if (it->itype == ASN1_ITYPE_MSTRING && str)
322
3.70M
            str->flags |= ASN1_STRING_FLAG_MSTRING;
323
3.89M
        break;
324
8.73M
    }
325
4.90M
    if (*pval)
326
4.90M
        return 1;
327
0
    return 0;
328
4.90M
}
329
330
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
331
493k
{
332
493k
    int utype;
333
493k
    if (it && it->funcs) {
334
190
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
335
190
        if (pf->prim_clear)
336
0
            pf->prim_clear(pval, it);
337
190
        else
338
190
            *pval = NULL;
339
190
        return;
340
190
    }
341
492k
    if (!it || (it->itype == ASN1_ITYPE_MSTRING))
342
28.6k
        utype = -1;
343
464k
    else
344
464k
        utype = it->utype;
345
492k
    if (utype == V_ASN1_BOOLEAN)
346
92.6k
        *(ASN1_BOOLEAN *)pval = it->size;
347
400k
    else
348
400k
        *pval = NULL;
349
492k
}