Coverage Report

Created: 2024-07-27 06:39

/src/openssl32/crypto/asn1/tasn_new.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 <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, OSSL_LIB_CTX *libctx,
20
                               const char *propq);
21
static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
22
                              int embed);
23
static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
24
static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
25
                             OSSL_LIB_CTX *libctx, const char *propq);
26
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
27
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
28
29
ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
30
43.5M
{
31
43.5M
    ASN1_VALUE *ret = NULL;
32
43.5M
    if (ASN1_item_ex_new(&ret, it) > 0)
33
43.5M
        return ret;
34
0
    return NULL;
35
43.5M
}
36
37
ASN1_VALUE *ASN1_item_new_ex(const ASN1_ITEM *it, OSSL_LIB_CTX *libctx,
38
                             const char *propq)
39
56.8k
{
40
56.8k
    ASN1_VALUE *ret = NULL;
41
56.8k
    if (asn1_item_embed_new(&ret, it, 0, libctx, propq) > 0)
42
56.8k
        return ret;
43
0
    return NULL;
44
56.8k
}
45
46
/* Allocate an ASN1 structure */
47
48
49
int ossl_asn1_item_ex_new_intern(ASN1_VALUE **pval, const ASN1_ITEM *it,
50
                                 OSSL_LIB_CTX *libctx, const char *propq)
51
29.4M
{
52
29.4M
    return asn1_item_embed_new(pval, it, 0, libctx, propq);
53
29.4M
}
54
55
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
56
43.5M
{
57
43.5M
    return asn1_item_embed_new(pval, it, 0, NULL, NULL);
58
43.5M
}
59
60
int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed,
61
                        OSSL_LIB_CTX *libctx, const char *propq)
62
93.1M
{
63
93.1M
    const ASN1_TEMPLATE *tt = NULL;
64
93.1M
    const ASN1_EXTERN_FUNCS *ef;
65
93.1M
    const ASN1_AUX *aux = it->funcs;
66
93.1M
    ASN1_aux_cb *asn1_cb;
67
93.1M
    ASN1_VALUE **pseqval;
68
93.1M
    int i;
69
93.1M
    if (aux && aux->asn1_cb)
70
2.87M
        asn1_cb = aux->asn1_cb;
71
90.2M
    else
72
90.2M
        asn1_cb = 0;
73
74
93.1M
    switch (it->itype) {
75
76
1.28M
    case ASN1_ITYPE_EXTERN:
77
1.28M
        ef = it->funcs;
78
1.28M
        if (ef != NULL) {
79
1.28M
            if (ef->asn1_ex_new_ex != NULL) {
80
383k
                if (!ef->asn1_ex_new_ex(pval, it, libctx, propq))
81
0
                    goto asn1err;
82
901k
            } else if (ef->asn1_ex_new != NULL) {
83
901k
                if (!ef->asn1_ex_new(pval, it))
84
0
                    goto asn1err;
85
901k
            }
86
1.28M
        }
87
1.28M
        break;
88
89
47.4M
    case ASN1_ITYPE_PRIMITIVE:
90
47.4M
        if (it->templates) {
91
46.1k
            if (!asn1_template_new(pval, it->templates, libctx, propq))
92
0
                goto asn1err;
93
47.3M
        } else if (!asn1_primitive_new(pval, it, embed))
94
0
            goto asn1err;
95
47.4M
        break;
96
97
47.4M
    case ASN1_ITYPE_MSTRING:
98
16.2M
        if (!asn1_primitive_new(pval, it, embed))
99
0
            goto asn1err;
100
16.2M
        break;
101
102
16.2M
    case ASN1_ITYPE_CHOICE:
103
2.54M
        if (asn1_cb) {
104
125k
            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
105
125k
            if (!i)
106
0
                goto auxerr;
107
125k
            if (i == 2) {
108
0
                return 1;
109
0
            }
110
125k
        }
111
2.54M
        if (embed) {
112
44.2k
            memset(*pval, 0, it->size);
113
2.49M
        } else {
114
2.49M
            *pval = OPENSSL_zalloc(it->size);
115
2.49M
            if (*pval == NULL)
116
0
                return 0;
117
2.49M
        }
118
2.54M
        ossl_asn1_set_choice_selector(pval, -1, it);
119
2.54M
        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
120
0
            goto auxerr2;
121
2.54M
        break;
122
123
2.54M
    case ASN1_ITYPE_NDEF_SEQUENCE:
124
25.6M
    case ASN1_ITYPE_SEQUENCE:
125
25.6M
        if (asn1_cb) {
126
1.61M
            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
127
1.61M
            if (!i)
128
0
                goto auxerr;
129
1.61M
            if (i == 2) {
130
383k
                return 1;
131
383k
            }
132
1.61M
        }
133
25.2M
        if (embed) {
134
1.71M
            memset(*pval, 0, it->size);
135
23.5M
        } else {
136
23.5M
            *pval = OPENSSL_zalloc(it->size);
137
23.5M
            if (*pval == NULL)
138
0
                return 0;
139
23.5M
        }
140
        /* 0 : init. lock */
141
25.2M
        if (ossl_asn1_do_lock(pval, 0, it) < 0) {
142
0
            if (!embed) {
143
0
                OPENSSL_free(*pval);
144
0
                *pval = NULL;
145
0
            }
146
0
            goto asn1err;
147
0
        }
148
25.2M
        ossl_asn1_enc_init(pval, it);
149
87.2M
        for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
150
62.0M
            pseqval = ossl_asn1_get_field_ptr(pval, tt);
151
62.0M
            if (!asn1_template_new(pseqval, tt, libctx, propq))
152
0
                goto asn1err2;
153
62.0M
        }
154
25.2M
        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
155
0
            goto auxerr2;
156
25.2M
        break;
157
93.1M
    }
158
92.7M
    return 1;
159
160
0
 asn1err2:
161
0
    ossl_asn1_item_embed_free(pval, it, embed);
162
0
 asn1err:
163
0
    ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
164
0
    return 0;
165
166
0
 auxerr2:
167
0
    ossl_asn1_item_embed_free(pval, it, embed);
168
0
 auxerr:
169
0
    ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
170
0
    return 0;
171
172
0
}
173
174
static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
175
21.1M
{
176
21.1M
    const ASN1_EXTERN_FUNCS *ef;
177
178
21.1M
    switch (it->itype) {
179
180
748k
    case ASN1_ITYPE_EXTERN:
181
748k
        ef = it->funcs;
182
748k
        if (ef && ef->asn1_ex_clear)
183
0
            ef->asn1_ex_clear(pval, it);
184
748k
        else
185
748k
            *pval = NULL;
186
748k
        break;
187
188
17.6M
    case ASN1_ITYPE_PRIMITIVE:
189
17.6M
        if (it->templates)
190
243k
            asn1_template_clear(pval, it->templates);
191
17.4M
        else
192
17.4M
            asn1_primitive_clear(pval, it);
193
17.6M
        break;
194
195
379k
    case ASN1_ITYPE_MSTRING:
196
379k
        asn1_primitive_clear(pval, it);
197
379k
        break;
198
199
836k
    case ASN1_ITYPE_CHOICE:
200
2.32M
    case ASN1_ITYPE_SEQUENCE:
201
2.36M
    case ASN1_ITYPE_NDEF_SEQUENCE:
202
2.36M
        *pval = NULL;
203
2.36M
        break;
204
21.1M
    }
205
21.1M
}
206
207
static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
208
                             OSSL_LIB_CTX *libctx, const char *propq)
209
118M
{
210
118M
    const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
211
118M
    int embed = tt->flags & ASN1_TFLG_EMBED;
212
118M
    ASN1_VALUE *tval;
213
118M
    int ret;
214
118M
    if (embed) {
215
8.75M
        tval = (ASN1_VALUE *)pval;
216
8.75M
        pval = &tval;
217
8.75M
    }
218
118M
    if (tt->flags & ASN1_TFLG_OPTIONAL) {
219
25.8M
        asn1_template_clear(pval, tt);
220
25.8M
        return 1;
221
25.8M
    }
222
    /* If ANY DEFINED BY nothing to do */
223
224
92.9M
    if (tt->flags & ASN1_TFLG_ADB_MASK) {
225
774k
        *pval = NULL;
226
774k
        return 1;
227
774k
    }
228
    /* If SET OF or SEQUENCE OF, its a STACK */
229
92.1M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
230
956k
        STACK_OF(ASN1_VALUE) *skval;
231
956k
        skval = sk_ASN1_VALUE_new_null();
232
956k
        if (!skval) {
233
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
234
0
            ret = 0;
235
0
            goto done;
236
0
        }
237
956k
        *pval = (ASN1_VALUE *)skval;
238
956k
        ret = 1;
239
956k
        goto done;
240
956k
    }
241
    /* Otherwise pass it back to the item routine */
242
91.2M
    ret = asn1_item_embed_new(pval, it, embed, libctx, propq);
243
92.1M
 done:
244
92.1M
    return ret;
245
91.2M
}
246
247
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
248
26.0M
{
249
    /* If ADB or STACK just NULL the field */
250
26.0M
    if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
251
4.89M
        *pval = NULL;
252
21.1M
    else
253
21.1M
        asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
254
26.0M
}
255
256
/*
257
 * NB: could probably combine most of the real XXX_new() behaviour and junk
258
 * all the old functions.
259
 */
260
261
static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
262
                              int embed)
263
106M
{
264
106M
    ASN1_TYPE *typ;
265
106M
    ASN1_STRING *str;
266
106M
    int utype;
267
268
106M
    if (!it)
269
0
        return 0;
270
271
106M
    if (it->funcs) {
272
1.72M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
273
1.72M
        if (embed) {
274
598k
            if (pf->prim_clear) {
275
598k
                pf->prim_clear(pval, it);
276
598k
                return 1;
277
598k
            }
278
1.12M
        } else if (pf->prim_new) {
279
1.12M
            return pf->prim_new(pval, it);
280
1.12M
        }
281
1.72M
    }
282
283
105M
    if (it->itype == ASN1_ITYPE_MSTRING)
284
31.0M
        utype = -1;
285
74.1M
    else
286
74.1M
        utype = it->utype;
287
105M
    switch (utype) {
288
38.8M
    case V_ASN1_OBJECT:
289
38.8M
        *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
290
38.8M
        return 1;
291
292
0
    case V_ASN1_BOOLEAN:
293
0
        *(ASN1_BOOLEAN *)pval = it->size;
294
0
        return 1;
295
296
0
    case V_ASN1_NULL:
297
0
        *pval = (ASN1_VALUE *)1;
298
0
        return 1;
299
300
25.7M
    case V_ASN1_ANY:
301
25.7M
        if ((typ = OPENSSL_malloc(sizeof(*typ))) == NULL)
302
0
            return 0;
303
25.7M
        typ->value.ptr = NULL;
304
25.7M
        typ->type = -1;
305
25.7M
        *pval = (ASN1_VALUE *)typ;
306
25.7M
        break;
307
308
40.5M
    default:
309
40.5M
        if (embed) {
310
4.14M
            str = *(ASN1_STRING **)pval;
311
4.14M
            memset(str, 0, sizeof(*str));
312
4.14M
            str->type = utype;
313
4.14M
            str->flags = ASN1_STRING_FLAG_EMBED;
314
36.4M
        } else {
315
36.4M
            str = ASN1_STRING_type_new(utype);
316
36.4M
            *pval = (ASN1_VALUE *)str;
317
36.4M
        }
318
40.5M
        if (it->itype == ASN1_ITYPE_MSTRING && str)
319
31.0M
            str->flags |= ASN1_STRING_FLAG_MSTRING;
320
40.5M
        break;
321
105M
    }
322
66.3M
    if (*pval)
323
66.3M
        return 1;
324
0
    return 0;
325
66.3M
}
326
327
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
328
17.8M
{
329
17.8M
    int utype;
330
17.8M
    if (it && it->funcs) {
331
445k
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
332
445k
        if (pf->prim_clear)
333
360k
            pf->prim_clear(pval, it);
334
85.9k
        else
335
85.9k
            *pval = NULL;
336
445k
        return;
337
445k
    }
338
17.3M
    if (!it || (it->itype == ASN1_ITYPE_MSTRING))
339
379k
        utype = -1;
340
17.0M
    else
341
17.0M
        utype = it->utype;
342
17.3M
    if (utype == V_ASN1_BOOLEAN)
343
3.20M
        *(ASN1_BOOLEAN *)pval = it->size;
344
14.1M
    else
345
14.1M
        *pval = NULL;
346
17.3M
}