7608b43b7f0d986b04786881482d9ae0b8dfbe28
[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 auxerr2;
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         /* 0 : init. lock */
127         if (asn1_do_lock(pval, 0, it) < 0)
128             goto memerr2;
129         asn1_enc_init(pval, it);
130         for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
131             pseqval = asn1_get_field_ptr(pval, tt);
132             if (!asn1_template_new(pseqval, tt))
133                 goto memerr2;
134         }
135         if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
136             goto auxerr2;
137         break;
138     }
139 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
140     OPENSSL_mem_debug_pop();
141 #endif
142     return 1;
143
144  memerr2:
145     if (!embed)
146         ASN1_item_ex_free(pval, it);
147  memerr:
148     ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ERR_R_MALLOC_FAILURE);
149 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
150     OPENSSL_mem_debug_pop();
151 #endif
152     return 0;
153
154  auxerr2:
155     if (!embed)
156         ASN1_item_ex_free(pval, it);
157  auxerr:
158     ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ASN1_R_AUX_ERROR);
159 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
160     OPENSSL_mem_debug_pop();
161 #endif
162     return 0;
163
164 }
165
166 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
167 {
168     const ASN1_EXTERN_FUNCS *ef;
169
170     switch (it->itype) {
171
172     case ASN1_ITYPE_EXTERN:
173         ef = it->funcs;
174         if (ef && ef->asn1_ex_clear)
175             ef->asn1_ex_clear(pval, it);
176         else
177             *pval = NULL;
178         break;
179
180     case ASN1_ITYPE_PRIMITIVE:
181         if (it->templates)
182             asn1_template_clear(pval, it->templates);
183         else
184             asn1_primitive_clear(pval, it);
185         break;
186
187     case ASN1_ITYPE_MSTRING:
188         asn1_primitive_clear(pval, it);
189         break;
190
191     case ASN1_ITYPE_CHOICE:
192     case ASN1_ITYPE_SEQUENCE:
193     case ASN1_ITYPE_NDEF_SEQUENCE:
194         *pval = NULL;
195         break;
196     }
197 }
198
199 static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
200 {
201     const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
202     int embed = tt->flags & ASN1_TFLG_EMBED;
203     ASN1_VALUE *tval;
204     int ret;
205     if (embed) {
206         tval = (ASN1_VALUE *)pval;
207         pval = &tval;
208     }
209     if (tt->flags & ASN1_TFLG_OPTIONAL) {
210         asn1_template_clear(pval, tt);
211         return 1;
212     }
213     /* If ANY DEFINED BY nothing to do */
214
215     if (tt->flags & ASN1_TFLG_ADB_MASK) {
216         *pval = NULL;
217         return 1;
218     }
219 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
220     OPENSSL_mem_debug_push(tt->field_name
221             ? tt->field_name : "asn1_template_new");
222 #endif
223     /* If SET OF or SEQUENCE OF, its a STACK */
224     if (tt->flags & ASN1_TFLG_SK_MASK) {
225         STACK_OF(ASN1_VALUE) *skval;
226         skval = sk_ASN1_VALUE_new_null();
227         if (!skval) {
228             ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
229             ret = 0;
230             goto done;
231         }
232         *pval = (ASN1_VALUE *)skval;
233         ret = 1;
234         goto done;
235     }
236     /* Otherwise pass it back to the item routine */
237     ret = asn1_item_embed_new(pval, it, embed);
238  done:
239 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
240     OPENSSL_mem_debug_pop();
241 #endif
242     return ret;
243 }
244
245 static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
246 {
247     /* If ADB or STACK just NULL the field */
248     if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
249         *pval = NULL;
250     else
251         asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
252 }
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 {
262     ASN1_TYPE *typ;
263     ASN1_STRING *str;
264     int utype;
265
266     if (!it)
267         return 0;
268
269     if (it->funcs) {
270         const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
271         if (embed) {
272             if (pf->prim_clear) {
273                 pf->prim_clear(pval, it);
274                 return 1;
275             }
276         } else if (pf->prim_new) {
277             return pf->prim_new(pval, it);
278         }
279     }
280
281     if (it->itype == ASN1_ITYPE_MSTRING)
282         utype = -1;
283     else
284         utype = it->utype;
285     switch (utype) {
286     case V_ASN1_OBJECT:
287         *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
288         return 1;
289
290     case V_ASN1_BOOLEAN:
291         *(ASN1_BOOLEAN *)pval = it->size;
292         return 1;
293
294     case V_ASN1_NULL:
295         *pval = (ASN1_VALUE *)1;
296         return 1;
297
298     case V_ASN1_ANY:
299         typ = OPENSSL_malloc(sizeof(*typ));
300         if (typ == NULL)
301             return 0;
302         typ->value.ptr = NULL;
303         typ->type = -1;
304         *pval = (ASN1_VALUE *)typ;
305         break;
306
307     default:
308         if (embed) {
309             str = *(ASN1_STRING **)pval;
310             memset(str, 0, sizeof(*str));
311             str->type = utype;
312             str->flags = ASN1_STRING_FLAG_EMBED;
313         } else {
314             str = ASN1_STRING_type_new(utype);
315             *pval = (ASN1_VALUE *)str;
316         }
317         if (it->itype == ASN1_ITYPE_MSTRING && str)
318             str->flags |= ASN1_STRING_FLAG_MSTRING;
319         break;
320     }
321     if (*pval)
322         return 1;
323     return 0;
324 }
325
326 static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
327 {
328     int utype;
329     if (it && it->funcs) {
330         const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
331         if (pf->prim_clear)
332             pf->prim_clear(pval, it);
333         else
334             *pval = NULL;
335         return;
336     }
337     if (!it || (it->itype == ASN1_ITYPE_MSTRING))
338         utype = -1;
339     else
340         utype = it->utype;
341     if (utype == V_ASN1_BOOLEAN)
342         *(ASN1_BOOLEAN *)pval = it->size;
343     else
344         *pval = NULL;
345 }