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