Coverage Report

Created: 2025-08-11 07:04

/src/openssl33/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
66.6M
{
31
66.6M
    ASN1_VALUE *ret = NULL;
32
66.6M
    if (ASN1_item_ex_new(&ret, it) > 0)
33
66.6M
        return ret;
34
0
    return NULL;
35
66.6M
}
36
37
ASN1_VALUE *ASN1_item_new_ex(const ASN1_ITEM *it, OSSL_LIB_CTX *libctx,
38
                             const char *propq)
39
89.4k
{
40
89.4k
    ASN1_VALUE *ret = NULL;
41
89.4k
    if (asn1_item_embed_new(&ret, it, 0, libctx, propq) > 0)
42
89.4k
        return ret;
43
0
    return NULL;
44
89.4k
}
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
35.3M
{
52
35.3M
    return asn1_item_embed_new(pval, it, 0, libctx, propq);
53
35.3M
}
54
55
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
56
66.6M
{
57
66.6M
    return asn1_item_embed_new(pval, it, 0, NULL, NULL);
58
66.6M
}
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
175M
{
63
175M
    const ASN1_TEMPLATE *tt = NULL;
64
175M
    const ASN1_EXTERN_FUNCS *ef;
65
175M
    const ASN1_AUX *aux = it->funcs;
66
175M
    ASN1_aux_cb *asn1_cb;
67
175M
    ASN1_VALUE **pseqval;
68
175M
    int i;
69
175M
    if (aux && aux->asn1_cb)
70
5.65M
        asn1_cb = aux->asn1_cb;
71
169M
    else
72
169M
        asn1_cb = 0;
73
74
175M
    switch (it->itype) {
75
76
2.46M
    case ASN1_ITYPE_EXTERN:
77
2.46M
        ef = it->funcs;
78
2.46M
        if (ef != NULL) {
79
2.46M
            if (ef->asn1_ex_new_ex != NULL) {
80
738k
                if (!ef->asn1_ex_new_ex(pval, it, libctx, propq))
81
0
                    goto asn1err;
82
1.72M
            } else if (ef->asn1_ex_new != NULL) {
83
1.72M
                if (!ef->asn1_ex_new(pval, it))
84
0
                    goto asn1err;
85
1.72M
            }
86
2.46M
        }
87
2.46M
        break;
88
89
94.5M
    case ASN1_ITYPE_PRIMITIVE:
90
94.5M
        if (it->templates) {
91
96.6k
            if (!asn1_template_new(pval, it->templates, libctx, propq))
92
0
                goto asn1err;
93
94.4M
        } else if (!asn1_primitive_new(pval, it, embed))
94
0
            goto asn1err;
95
94.5M
        break;
96
97
94.5M
    case ASN1_ITYPE_MSTRING:
98
26.7M
        if (!asn1_primitive_new(pval, it, embed))
99
0
            goto asn1err;
100
26.7M
        break;
101
102
26.7M
    case ASN1_ITYPE_CHOICE:
103
4.87M
        if (asn1_cb) {
104
331k
            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
105
331k
            if (!i)
106
0
                goto auxerr;
107
331k
            if (i == 2) {
108
0
                return 1;
109
0
            }
110
331k
        }
111
4.87M
        if (embed) {
112
99.5k
            memset(*pval, 0, it->size);
113
4.77M
        } else {
114
4.77M
            *pval = OPENSSL_zalloc(it->size);
115
4.77M
            if (*pval == NULL)
116
0
                return 0;
117
4.77M
        }
118
4.87M
        ossl_asn1_set_choice_selector(pval, -1, it);
119
4.87M
        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
120
0
            goto auxerr2;
121
4.87M
        break;
122
123
4.87M
    case ASN1_ITYPE_NDEF_SEQUENCE:
124
46.9M
    case ASN1_ITYPE_SEQUENCE:
125
46.9M
        if (asn1_cb) {
126
3.59M
            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
127
3.59M
            if (!i)
128
0
                goto auxerr;
129
3.59M
            if (i == 2) {
130
821k
                return 1;
131
821k
            }
132
3.59M
        }
133
46.1M
        if (embed) {
134
3.21M
            memset(*pval, 0, it->size);
135
42.9M
        } else {
136
42.9M
            *pval = OPENSSL_zalloc(it->size);
137
42.9M
            if (*pval == NULL)
138
0
                return 0;
139
42.9M
        }
140
        /* 0 : init. lock */
141
46.1M
        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
46.1M
        ossl_asn1_enc_init(pval, it);
149
164M
        for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
150
118M
            pseqval = ossl_asn1_get_field_ptr(pval, tt);
151
118M
            if (!asn1_template_new(pseqval, tt, libctx, propq))
152
0
                goto asn1err2;
153
118M
        }
154
46.1M
        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
155
0
            goto auxerr2;
156
46.1M
        break;
157
175M
    }
158
174M
    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
30.4M
{
176
30.4M
    const ASN1_EXTERN_FUNCS *ef;
177
178
30.4M
    switch (it->itype) {
179
180
1.26M
    case ASN1_ITYPE_EXTERN:
181
1.26M
        ef = it->funcs;
182
1.26M
        if (ef && ef->asn1_ex_clear)
183
0
            ef->asn1_ex_clear(pval, it);
184
1.26M
        else
185
1.26M
            *pval = NULL;
186
1.26M
        break;
187
188
24.9M
    case ASN1_ITYPE_PRIMITIVE:
189
24.9M
        if (it->templates)
190
414k
            asn1_template_clear(pval, it->templates);
191
24.5M
        else
192
24.5M
            asn1_primitive_clear(pval, it);
193
24.9M
        break;
194
195
431k
    case ASN1_ITYPE_MSTRING:
196
431k
        asn1_primitive_clear(pval, it);
197
431k
        break;
198
199
1.58M
    case ASN1_ITYPE_CHOICE:
200
3.73M
    case ASN1_ITYPE_SEQUENCE:
201
3.78M
    case ASN1_ITYPE_NDEF_SEQUENCE:
202
3.78M
        *pval = NULL;
203
3.78M
        break;
204
30.4M
    }
205
30.4M
}
206
207
static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
208
                             OSSL_LIB_CTX *libctx, const char *propq)
209
146M
{
210
146M
    const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
211
146M
    int embed = tt->flags & ASN1_TFLG_EMBED;
212
146M
    ASN1_VALUE *tval;
213
146M
    int ret;
214
146M
    if (embed) {
215
11.1M
        tval = (ASN1_VALUE *)pval;
216
11.1M
        pval = &tval;
217
11.1M
    }
218
146M
    if (tt->flags & ASN1_TFLG_OPTIONAL) {
219
37.3M
        asn1_template_clear(pval, tt);
220
37.3M
        return 1;
221
37.3M
    }
222
    /* If ANY DEFINED BY nothing to do */
223
224
109M
    if (tt->flags & ASN1_TFLG_ADB_MASK) {
225
955k
        *pval = NULL;
226
955k
        return 1;
227
955k
    }
228
    /* If SET OF or SEQUENCE OF, its a STACK */
229
108M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
230
1.32M
        STACK_OF(ASN1_VALUE) *skval;
231
1.32M
        skval = sk_ASN1_VALUE_new_null();
232
1.32M
        if (!skval) {
233
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
234
0
            ret = 0;
235
0
            goto done;
236
0
        }
237
1.32M
        *pval = (ASN1_VALUE *)skval;
238
1.32M
        ret = 1;
239
1.32M
        goto done;
240
1.32M
    }
241
    /* Otherwise pass it back to the item routine */
242
106M
    ret = asn1_item_embed_new(pval, it, embed, libctx, propq);
243
108M
 done:
244
108M
    return ret;
245
106M
}
246
247
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
248
37.7M
{
249
    /* If ADB or STACK just NULL the field */
250
37.7M
    if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
251
7.30M
        *pval = NULL;
252
30.4M
    else
253
30.4M
        asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
254
37.7M
}
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
141M
{
264
141M
    ASN1_TYPE *typ;
265
141M
    ASN1_STRING *str;
266
141M
    int utype;
267
268
141M
    if (!it)
269
0
        return 0;
270
271
141M
    if (it->funcs) {
272
2.03M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
273
2.03M
        if (embed) {
274
854k
            if (pf->prim_clear) {
275
854k
                pf->prim_clear(pval, it);
276
854k
                return 1;
277
854k
            }
278
1.18M
        } else if (pf->prim_new) {
279
1.18M
            return pf->prim_new(pval, it);
280
1.18M
        }
281
2.03M
    }
282
283
139M
    if (it->itype == ASN1_ITYPE_MSTRING)
284
33.7M
        utype = -1;
285
105M
    else
286
105M
        utype = it->utype;
287
139M
    switch (utype) {
288
44.5M
    case V_ASN1_OBJECT:
289
44.5M
        *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
290
44.5M
        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
47.5M
    case V_ASN1_ANY:
301
47.5M
        if ((typ = OPENSSL_malloc(sizeof(*typ))) == NULL)
302
0
            return 0;
303
47.5M
        typ->value.ptr = NULL;
304
47.5M
        typ->type = -1;
305
47.5M
        *pval = (ASN1_VALUE *)typ;
306
47.5M
        break;
307
308
47.2M
    default:
309
47.2M
        if (embed) {
310
5.54M
            str = *(ASN1_STRING **)pval;
311
5.54M
            memset(str, 0, sizeof(*str));
312
5.54M
            str->type = utype;
313
5.54M
            str->flags = ASN1_STRING_FLAG_EMBED;
314
41.7M
        } else {
315
41.7M
            str = ASN1_STRING_type_new(utype);
316
41.7M
            *pval = (ASN1_VALUE *)str;
317
41.7M
        }
318
47.2M
        if (it->itype == ASN1_ITYPE_MSTRING && str)
319
33.7M
            str->flags |= ASN1_STRING_FLAG_MSTRING;
320
47.2M
        break;
321
139M
    }
322
94.8M
    if (*pval)
323
94.8M
        return 1;
324
0
    return 0;
325
94.8M
}
326
327
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
328
25.0M
{
329
25.0M
    int utype;
330
25.0M
    if (it && it->funcs) {
331
602k
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
332
602k
        if (pf->prim_clear)
333
482k
            pf->prim_clear(pval, it);
334
120k
        else
335
120k
            *pval = NULL;
336
602k
        return;
337
602k
    }
338
24.3M
    if (!it || (it->itype == ASN1_ITYPE_MSTRING))
339
431k
        utype = -1;
340
23.9M
    else
341
23.9M
        utype = it->utype;
342
24.3M
    if (utype == V_ASN1_BOOLEAN)
343
4.88M
        *(ASN1_BOOLEAN *)pval = it->size;
344
19.5M
    else
345
19.5M
        *pval = NULL;
346
24.3M
}