Update copyright year
[oweals/openssl.git] / crypto / asn1 / tasn_new.c
1 /*
2  * Copyright 2000-2020 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 DEFINE_STACK_OF(ASN1_VALUE)
19
20 static int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
21                                int embed);
22 static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
23                               int embed);
24 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
25 static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
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 {
31     ASN1_VALUE *ret = NULL;
32     if (ASN1_item_ex_new(&ret, it) > 0)
33         return ret;
34     return NULL;
35 }
36
37 /* Allocate an ASN1 structure */
38
39 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
40 {
41     return asn1_item_embed_new(pval, it, 0);
42 }
43
44 int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
45 {
46     const ASN1_TEMPLATE *tt = NULL;
47     const ASN1_EXTERN_FUNCS *ef;
48     const ASN1_AUX *aux = it->funcs;
49     ASN1_aux_cb *asn1_cb;
50     ASN1_VALUE **pseqval;
51     int i;
52     if (aux && aux->asn1_cb)
53         asn1_cb = aux->asn1_cb;
54     else
55         asn1_cb = 0;
56
57     switch (it->itype) {
58
59     case ASN1_ITYPE_EXTERN:
60         ef = it->funcs;
61         if (ef && ef->asn1_ex_new) {
62             if (!ef->asn1_ex_new(pval, it))
63                 goto memerr;
64         }
65         break;
66
67     case ASN1_ITYPE_PRIMITIVE:
68         if (it->templates) {
69             if (!asn1_template_new(pval, it->templates))
70                 goto memerr;
71         } else if (!asn1_primitive_new(pval, it, embed))
72             goto memerr;
73         break;
74
75     case ASN1_ITYPE_MSTRING:
76         if (!asn1_primitive_new(pval, it, embed))
77             goto memerr;
78         break;
79
80     case ASN1_ITYPE_CHOICE:
81         if (asn1_cb) {
82             i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
83             if (!i)
84                 goto auxerr;
85             if (i == 2) {
86                 return 1;
87             }
88         }
89         if (embed) {
90             memset(*pval, 0, it->size);
91         } else {
92             *pval = OPENSSL_zalloc(it->size);
93             if (*pval == NULL)
94                 goto memerr;
95         }
96         asn1_set_choice_selector(pval, -1, it);
97         if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
98             goto auxerr2;
99         break;
100
101     case ASN1_ITYPE_NDEF_SEQUENCE:
102     case ASN1_ITYPE_SEQUENCE:
103         if (asn1_cb) {
104             i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
105             if (!i)
106                 goto auxerr;
107             if (i == 2) {
108                 return 1;
109             }
110         }
111         if (embed) {
112             memset(*pval, 0, it->size);
113         } else {
114             *pval = OPENSSL_zalloc(it->size);
115             if (*pval == NULL)
116                 goto memerr;
117         }
118         /* 0 : init. lock */
119         if (asn1_do_lock(pval, 0, it) < 0) {
120             if (!embed) {
121                 OPENSSL_free(*pval);
122                 *pval = NULL;
123             }
124             goto memerr;
125         }
126         asn1_enc_init(pval, it);
127         for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
128             pseqval = asn1_get_field_ptr(pval, tt);
129             if (!asn1_template_new(pseqval, tt))
130                 goto memerr2;
131         }
132         if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
133             goto auxerr2;
134         break;
135     }
136     return 1;
137
138  memerr2:
139     asn1_item_embed_free(pval, it, embed);
140  memerr:
141     ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ERR_R_MALLOC_FAILURE);
142     return 0;
143
144  auxerr2:
145     asn1_item_embed_free(pval, it, embed);
146  auxerr:
147     ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ASN1_R_AUX_ERROR);
148     return 0;
149
150 }
151
152 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
153 {
154     const ASN1_EXTERN_FUNCS *ef;
155
156     switch (it->itype) {
157
158     case ASN1_ITYPE_EXTERN:
159         ef = it->funcs;
160         if (ef && ef->asn1_ex_clear)
161             ef->asn1_ex_clear(pval, it);
162         else
163             *pval = NULL;
164         break;
165
166     case ASN1_ITYPE_PRIMITIVE:
167         if (it->templates)
168             asn1_template_clear(pval, it->templates);
169         else
170             asn1_primitive_clear(pval, it);
171         break;
172
173     case ASN1_ITYPE_MSTRING:
174         asn1_primitive_clear(pval, it);
175         break;
176
177     case ASN1_ITYPE_CHOICE:
178     case ASN1_ITYPE_SEQUENCE:
179     case ASN1_ITYPE_NDEF_SEQUENCE:
180         *pval = NULL;
181         break;
182     }
183 }
184
185 static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
186 {
187     const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
188     int embed = tt->flags & ASN1_TFLG_EMBED;
189     ASN1_VALUE *tval;
190     int ret;
191     if (embed) {
192         tval = (ASN1_VALUE *)pval;
193         pval = &tval;
194     }
195     if (tt->flags & ASN1_TFLG_OPTIONAL) {
196         asn1_template_clear(pval, tt);
197         return 1;
198     }
199     /* If ANY DEFINED BY nothing to do */
200
201     if (tt->flags & ASN1_TFLG_ADB_MASK) {
202         *pval = NULL;
203         return 1;
204     }
205     /* If SET OF or SEQUENCE OF, its a STACK */
206     if (tt->flags & ASN1_TFLG_SK_MASK) {
207         STACK_OF(ASN1_VALUE) *skval;
208         skval = sk_ASN1_VALUE_new_null();
209         if (!skval) {
210             ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
211             ret = 0;
212             goto done;
213         }
214         *pval = (ASN1_VALUE *)skval;
215         ret = 1;
216         goto done;
217     }
218     /* Otherwise pass it back to the item routine */
219     ret = asn1_item_embed_new(pval, it, embed);
220  done:
221     return ret;
222 }
223
224 static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
225 {
226     /* If ADB or STACK just NULL the field */
227     if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
228         *pval = NULL;
229     else
230         asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
231 }
232
233 /*
234  * NB: could probably combine most of the real XXX_new() behaviour and junk
235  * all the old functions.
236  */
237
238 static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
239                               int embed)
240 {
241     ASN1_TYPE *typ;
242     ASN1_STRING *str;
243     int utype;
244
245     if (!it)
246         return 0;
247
248     if (it->funcs) {
249         const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
250         if (embed) {
251             if (pf->prim_clear) {
252                 pf->prim_clear(pval, it);
253                 return 1;
254             }
255         } else if (pf->prim_new) {
256             return pf->prim_new(pval, it);
257         }
258     }
259
260     if (it->itype == ASN1_ITYPE_MSTRING)
261         utype = -1;
262     else
263         utype = it->utype;
264     switch (utype) {
265     case V_ASN1_OBJECT:
266         *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
267         return 1;
268
269     case V_ASN1_BOOLEAN:
270         *(ASN1_BOOLEAN *)pval = it->size;
271         return 1;
272
273     case V_ASN1_NULL:
274         *pval = (ASN1_VALUE *)1;
275         return 1;
276
277     case V_ASN1_ANY:
278         if ((typ = OPENSSL_malloc(sizeof(*typ))) == NULL) {
279             ASN1err(ASN1_F_ASN1_PRIMITIVE_NEW, ERR_R_MALLOC_FAILURE);
280             return 0;
281         }
282         typ->value.ptr = NULL;
283         typ->type = -1;
284         *pval = (ASN1_VALUE *)typ;
285         break;
286
287     default:
288         if (embed) {
289             str = *(ASN1_STRING **)pval;
290             memset(str, 0, sizeof(*str));
291             str->type = utype;
292             str->flags = ASN1_STRING_FLAG_EMBED;
293         } else {
294             str = ASN1_STRING_type_new(utype);
295             *pval = (ASN1_VALUE *)str;
296         }
297         if (it->itype == ASN1_ITYPE_MSTRING && str)
298             str->flags |= ASN1_STRING_FLAG_MSTRING;
299         break;
300     }
301     if (*pval)
302         return 1;
303     return 0;
304 }
305
306 static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
307 {
308     int utype;
309     if (it && it->funcs) {
310         const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
311         if (pf->prim_clear)
312             pf->prim_clear(pval, it);
313         else
314             *pval = NULL;
315         return;
316     }
317     if (!it || (it->itype == ASN1_ITYPE_MSTRING))
318         utype = -1;
319     else
320         utype = it->utype;
321     if (utype == V_ASN1_BOOLEAN)
322         *(ASN1_BOOLEAN *)pval = it->size;
323     else
324         *pval = NULL;
325 }