Coverage Report

Created: 2024-07-27 06:38

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