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