Coverage Report

Created: 2024-07-24 06:31

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