Coverage Report

Created: 2026-07-12 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/crypto/asn1/tasn_new.c
Line
Count
Source
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
75.9M
{
31
75.9M
    ASN1_VALUE *ret = NULL;
32
75.9M
    if (ASN1_item_ex_new(&ret, it) > 0)
33
75.9M
        return ret;
34
0
    return NULL;
35
75.9M
}
36
37
ASN1_VALUE *ASN1_item_new_ex(const ASN1_ITEM *it, OSSL_LIB_CTX *libctx,
38
    const char *propq)
39
111k
{
40
111k
    ASN1_VALUE *ret = NULL;
41
111k
    if (asn1_item_embed_new(&ret, it, 0, libctx, propq) > 0)
42
111k
        return ret;
43
0
    return NULL;
44
111k
}
45
46
/* Allocate an ASN1 structure */
47
48
int ossl_asn1_item_ex_new_intern(ASN1_VALUE **pval, const ASN1_ITEM *it,
49
    OSSL_LIB_CTX *libctx, const char *propq)
50
41.3M
{
51
41.3M
    return asn1_item_embed_new(pval, it, 0, libctx, propq);
52
41.3M
}
53
54
int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
55
75.9M
{
56
75.9M
    return asn1_item_embed_new(pval, it, 0, NULL, NULL);
57
75.9M
}
58
59
int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed,
60
    OSSL_LIB_CTX *libctx, const char *propq)
61
216M
{
62
216M
    const ASN1_TEMPLATE *tt = NULL;
63
216M
    const ASN1_EXTERN_FUNCS *ef;
64
216M
    const ASN1_AUX *aux = it->funcs;
65
216M
    ASN1_aux_cb *asn1_cb;
66
216M
    ASN1_VALUE **pseqval;
67
216M
    int i;
68
216M
    if (aux && aux->asn1_cb)
69
6.33M
        asn1_cb = aux->asn1_cb;
70
210M
    else
71
210M
        asn1_cb = 0;
72
73
216M
    switch (it->itype) {
74
75
3.40M
    case ASN1_ITYPE_EXTERN:
76
3.40M
        ef = it->funcs;
77
3.40M
        if (ef != NULL) {
78
3.40M
            if (ef->asn1_ex_new_ex != NULL) {
79
1.03M
                if (!ef->asn1_ex_new_ex(pval, it, libctx, propq))
80
0
                    goto asn1err;
81
2.37M
            } else if (ef->asn1_ex_new != NULL) {
82
2.37M
                if (!ef->asn1_ex_new(pval, it))
83
0
                    goto asn1err;
84
2.37M
            }
85
3.40M
        }
86
3.40M
        break;
87
88
111M
    case ASN1_ITYPE_PRIMITIVE:
89
111M
        if (it->templates) {
90
110k
            if (!asn1_template_new(pval, it->templates, libctx, propq))
91
0
                goto asn1err;
92
111M
        } else if (!asn1_primitive_new(pval, it, embed))
93
0
            goto asn1err;
94
111M
        break;
95
96
111M
    case ASN1_ITYPE_MSTRING:
97
36.6M
        if (!asn1_primitive_new(pval, it, embed))
98
0
            goto asn1err;
99
36.6M
        break;
100
101
36.6M
    case ASN1_ITYPE_CHOICE:
102
4.45M
        if (asn1_cb) {
103
316k
            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
104
316k
            if (!i)
105
0
                goto auxerr;
106
316k
            if (i == 2) {
107
0
                return 1;
108
0
            }
109
316k
        }
110
4.45M
        if (embed) {
111
103k
            memset(*pval, 0, it->size);
112
4.35M
        } else {
113
4.35M
            *pval = OPENSSL_zalloc(it->size);
114
4.35M
            if (*pval == NULL)
115
0
                return 0;
116
4.35M
        }
117
4.45M
        ossl_asn1_set_choice_selector(pval, -1, it);
118
4.45M
        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
119
0
            goto auxerr2;
120
4.45M
        break;
121
122
4.45M
    case ASN1_ITYPE_NDEF_SEQUENCE:
123
60.6M
    case ASN1_ITYPE_SEQUENCE:
124
60.6M
        if (asn1_cb) {
125
4.11M
            i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
126
4.11M
            if (!i)
127
0
                goto auxerr;
128
4.11M
            if (i == 2) {
129
852k
                return 1;
130
852k
            }
131
4.11M
        }
132
59.8M
        if (embed) {
133
4.58M
            memset(*pval, 0, it->size);
134
55.2M
        } else {
135
55.2M
            *pval = OPENSSL_zalloc(it->size);
136
55.2M
            if (*pval == NULL)
137
0
                return 0;
138
55.2M
        }
139
        /* 0 : init. lock */
140
59.8M
        if (ossl_asn1_do_lock(pval, 0, it) < 0) {
141
0
            if (!embed) {
142
0
                OPENSSL_free(*pval);
143
0
                *pval = NULL;
144
0
            }
145
0
            goto asn1err;
146
0
        }
147
59.8M
        ossl_asn1_enc_init(pval, it);
148
210M
        for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
149
151M
            pseqval = ossl_asn1_get_field_ptr(pval, tt);
150
151M
            if (!asn1_template_new(pseqval, tt, libctx, propq))
151
0
                goto asn1err2;
152
151M
        }
153
59.8M
        if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
154
0
            goto auxerr2;
155
59.8M
        break;
156
216M
    }
157
215M
    return 1;
158
159
0
asn1err2:
160
0
    ossl_asn1_item_embed_free(pval, it, embed);
161
0
asn1err:
162
0
    ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
163
0
    return 0;
164
165
0
auxerr2:
166
0
    ossl_asn1_item_embed_free(pval, it, embed);
167
0
auxerr:
168
0
    ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR);
169
0
    return 0;
170
0
}
171
172
static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
173
35.9M
{
174
35.9M
    const ASN1_EXTERN_FUNCS *ef;
175
176
35.9M
    switch (it->itype) {
177
178
1.78M
    case ASN1_ITYPE_EXTERN:
179
1.78M
        ef = it->funcs;
180
1.78M
        if (ef && ef->asn1_ex_clear)
181
0
            ef->asn1_ex_clear(pval, it);
182
1.78M
        else
183
1.78M
            *pval = NULL;
184
1.78M
        break;
185
186
29.4M
    case ASN1_ITYPE_PRIMITIVE:
187
29.4M
        if (it->templates)
188
584k
            asn1_template_clear(pval, it->templates);
189
28.8M
        else
190
28.8M
            asn1_primitive_clear(pval, it);
191
29.4M
        break;
192
193
488k
    case ASN1_ITYPE_MSTRING:
194
488k
        asn1_primitive_clear(pval, it);
195
488k
        break;
196
197
1.54M
    case ASN1_ITYPE_CHOICE:
198
4.10M
    case ASN1_ITYPE_SEQUENCE:
199
4.15M
    case ASN1_ITYPE_NDEF_SEQUENCE:
200
4.15M
        *pval = NULL;
201
4.15M
        break;
202
35.9M
    }
203
35.9M
}
204
205
static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
206
    OSSL_LIB_CTX *libctx, const char *propq)
207
180M
{
208
180M
    const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
209
180M
    int embed = tt->flags & ASN1_TFLG_EMBED;
210
180M
    ASN1_VALUE *tval;
211
180M
    int ret;
212
180M
    if (embed) {
213
14.1M
        tval = (ASN1_VALUE *)pval;
214
14.1M
        pval = &tval;
215
14.1M
    }
216
180M
    if (tt->flags & ASN1_TFLG_OPTIONAL) {
217
43.6M
        asn1_template_clear(pval, tt);
218
43.6M
        return 1;
219
43.6M
    }
220
    /* If ANY DEFINED BY nothing to do */
221
222
136M
    if (tt->flags & ASN1_TFLG_ADB_MASK) {
223
1.03M
        *pval = NULL;
224
1.03M
        return 1;
225
1.03M
    }
226
    /* If SET OF or SEQUENCE OF, its a STACK */
227
135M
    if (tt->flags & ASN1_TFLG_SK_MASK) {
228
1.60M
        STACK_OF(ASN1_VALUE) *skval;
229
1.60M
        skval = sk_ASN1_VALUE_new_null();
230
1.60M
        if (!skval) {
231
0
            ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB);
232
0
            ret = 0;
233
0
            goto done;
234
0
        }
235
1.60M
        *pval = (ASN1_VALUE *)skval;
236
1.60M
        ret = 1;
237
1.60M
        goto done;
238
1.60M
    }
239
    /* Otherwise pass it back to the item routine */
240
134M
    ret = asn1_item_embed_new(pval, it, embed, libctx, propq);
241
135M
done:
242
135M
    return ret;
243
134M
}
244
245
static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
246
44.2M
{
247
    /* If ADB or STACK just NULL the field */
248
44.2M
    if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
249
8.34M
        *pval = NULL;
250
35.9M
    else
251
35.9M
        asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
252
44.2M
}
253
254
/*
255
 * NB: could probably combine most of the real XXX_new() behaviour and junk
256
 * all the old functions.
257
 */
258
259
static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
260
    int embed)
261
169M
{
262
169M
    ASN1_TYPE *typ;
263
169M
    ASN1_STRING *str;
264
169M
    int utype;
265
266
169M
    if (!it)
267
0
        return 0;
268
269
169M
    if (it->funcs) {
270
2.27M
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
271
2.27M
        if (embed) {
272
849k
            if (pf->prim_clear) {
273
849k
                pf->prim_clear(pval, it);
274
849k
                return 1;
275
849k
            }
276
1.42M
        } else if (pf->prim_new) {
277
1.42M
            return pf->prim_new(pval, it);
278
1.42M
        }
279
2.27M
    }
280
281
166M
    if (it->itype == ASN1_ITYPE_MSTRING)
282
44.0M
        utype = -1;
283
122M
    else
284
122M
        utype = it->utype;
285
166M
    switch (utype) {
286
56.9M
    case V_ASN1_OBJECT:
287
56.9M
        *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
288
56.9M
        return 1;
289
290
0
    case V_ASN1_BOOLEAN:
291
0
        *(ASN1_BOOLEAN *)pval = it->size;
292
0
        return 1;
293
294
0
    case V_ASN1_NULL:
295
0
        *pval = (ASN1_VALUE *)1;
296
0
        return 1;
297
298
50.7M
    case V_ASN1_ANY:
299
50.7M
        if ((typ = OPENSSL_malloc(sizeof(*typ))) == NULL)
300
0
            return 0;
301
50.7M
        typ->value.ptr = NULL;
302
50.7M
        typ->type = -1;
303
50.7M
        *pval = (ASN1_VALUE *)typ;
304
50.7M
        break;
305
306
59.2M
    default:
307
59.2M
        if (embed) {
308
7.15M
            str = *(ASN1_STRING **)pval;
309
7.15M
            memset(str, 0, sizeof(*str));
310
7.15M
            str->type = utype;
311
7.15M
            str->flags = ASN1_STRING_FLAG_EMBED;
312
52.0M
        } else {
313
52.0M
            str = ASN1_STRING_type_new(utype);
314
52.0M
            *pval = (ASN1_VALUE *)str;
315
52.0M
        }
316
59.2M
        if (it->itype == ASN1_ITYPE_MSTRING && str)
317
44.0M
            str->flags |= ASN1_STRING_FLAG_MSTRING;
318
59.2M
        break;
319
166M
    }
320
109M
    if (*pval)
321
109M
        return 1;
322
0
    return 0;
323
109M
}
324
325
static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
326
29.3M
{
327
29.3M
    int utype;
328
29.3M
    if (it && it->funcs) {
329
596k
        const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
330
596k
        if (pf->prim_clear)
331
441k
            pf->prim_clear(pval, it);
332
155k
        else
333
155k
            *pval = NULL;
334
596k
        return;
335
596k
    }
336
28.7M
    if (!it || (it->itype == ASN1_ITYPE_MSTRING))
337
488k
        utype = -1;
338
28.2M
    else
339
28.2M
        utype = it->utype;
340
28.7M
    if (utype == V_ASN1_BOOLEAN)
341
5.63M
        *(ASN1_BOOLEAN *)pval = it->size;
342
23.1M
    else
343
23.1M
        *pval = NULL;
344
28.7M
}