Allow a zero length extension block
[openssl.git] / crypto / asn1 / tasn_new.c
1 /* tasn_new.c */
2 /*
3  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4  * 2000.
5  */
6 /* ====================================================================
7  * Copyright (c) 2000-2004 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    licensing@OpenSSL.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This product includes cryptographic software written by Eric Young
55  * (eay@cryptsoft.com).  This product includes software written by Tim
56  * Hudson (tjh@cryptsoft.com).
57  *
58  */
59
60 #include <stddef.h>
61 #include <openssl/asn1.h>
62 #include <openssl/objects.h>
63 #include <openssl/err.h>
64 #include <openssl/asn1t.h>
65 #include <string.h>
66 #include "asn1_locl.h"
67
68 static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
69 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
70 static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
71 static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
72 static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
73
74 ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
75 {
76     ASN1_VALUE *ret = NULL;
77     if (ASN1_item_ex_new(&ret, it) > 0)
78         return ret;
79     return NULL;
80 }
81
82 /* Allocate an ASN1 structure */
83
84 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
85 {
86     const ASN1_TEMPLATE *tt = NULL;
87     const ASN1_EXTERN_FUNCS *ef;
88     const ASN1_AUX *aux = it->funcs;
89     ASN1_aux_cb *asn1_cb;
90     ASN1_VALUE **pseqval;
91     int i;
92     if (aux && aux->asn1_cb)
93         asn1_cb = aux->asn1_cb;
94     else
95         asn1_cb = 0;
96
97 #ifdef CRYPTO_MDEBUG
98     if (it->sname)
99         CRYPTO_push_info(it->sname);
100 #endif
101
102     switch (it->itype) {
103
104     case ASN1_ITYPE_EXTERN:
105         ef = it->funcs;
106         if (ef && ef->asn1_ex_new) {
107             if (!ef->asn1_ex_new(pval, it))
108                 goto memerr;
109         }
110         break;
111
112     case ASN1_ITYPE_PRIMITIVE:
113         if (it->templates) {
114             if (!asn1_template_new(pval, it->templates))
115                 goto memerr;
116         } else if (!asn1_primitive_new(pval, it))
117             goto memerr;
118         break;
119
120     case ASN1_ITYPE_MSTRING:
121         if (!asn1_primitive_new(pval, it))
122             goto memerr;
123         break;
124
125     case ASN1_ITYPE_CHOICE:
126         if (asn1_cb) {
127             i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
128             if (!i)
129                 goto auxerr;
130             if (i == 2) {
131 #ifdef CRYPTO_MDEBUG
132                 if (it->sname)
133                     CRYPTO_pop_info();
134 #endif
135                 return 1;
136             }
137         }
138         *pval = OPENSSL_malloc(it->size);
139         if (!*pval)
140             goto memerr;
141         memset(*pval, 0, it->size);
142         asn1_set_choice_selector(pval, -1, it);
143         if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
144             goto auxerr;
145         break;
146
147     case ASN1_ITYPE_NDEF_SEQUENCE:
148     case ASN1_ITYPE_SEQUENCE:
149         if (asn1_cb) {
150             i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
151             if (!i)
152                 goto auxerr;
153             if (i == 2) {
154 #ifdef CRYPTO_MDEBUG
155                 if (it->sname)
156                     CRYPTO_pop_info();
157 #endif
158                 return 1;
159             }
160         }
161         *pval = OPENSSL_malloc(it->size);
162         if (!*pval)
163             goto memerr;
164         memset(*pval, 0, it->size);
165         asn1_do_lock(pval, 0, it);
166         asn1_enc_init(pval, it);
167         for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
168             pseqval = asn1_get_field_ptr(pval, tt);
169             if (!asn1_template_new(pseqval, tt))
170                 goto memerr;
171         }
172         if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
173             goto auxerr;
174         break;
175     }
176 #ifdef CRYPTO_MDEBUG
177     if (it->sname)
178         CRYPTO_pop_info();
179 #endif
180     return 1;
181
182  memerr:
183     ASN1err(ASN1_F_ASN1_ITEM_EX_NEW, ERR_R_MALLOC_FAILURE);
184 #ifdef CRYPTO_MDEBUG
185     if (it->sname)
186         CRYPTO_pop_info();
187 #endif
188     return 0;
189
190  auxerr:
191     ASN1err(ASN1_F_ASN1_ITEM_EX_NEW, ASN1_R_AUX_ERROR);
192     ASN1_item_ex_free(pval, it);
193 #ifdef CRYPTO_MDEBUG
194     if (it->sname)
195         CRYPTO_pop_info();
196 #endif
197     return 0;
198
199 }
200
201 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
202 {
203     const ASN1_EXTERN_FUNCS *ef;
204
205     switch (it->itype) {
206
207     case ASN1_ITYPE_EXTERN:
208         ef = it->funcs;
209         if (ef && ef->asn1_ex_clear)
210             ef->asn1_ex_clear(pval, it);
211         else
212             *pval = NULL;
213         break;
214
215     case ASN1_ITYPE_PRIMITIVE:
216         if (it->templates)
217             asn1_template_clear(pval, it->templates);
218         else
219             asn1_primitive_clear(pval, it);
220         break;
221
222     case ASN1_ITYPE_MSTRING:
223         asn1_primitive_clear(pval, it);
224         break;
225
226     case ASN1_ITYPE_CHOICE:
227     case ASN1_ITYPE_SEQUENCE:
228     case ASN1_ITYPE_NDEF_SEQUENCE:
229         *pval = NULL;
230         break;
231     }
232 }
233
234 static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
235 {
236     const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
237     int ret;
238     if (tt->flags & ASN1_TFLG_OPTIONAL) {
239         asn1_template_clear(pval, tt);
240         return 1;
241     }
242     /* If ANY DEFINED BY nothing to do */
243
244     if (tt->flags & ASN1_TFLG_ADB_MASK) {
245         *pval = NULL;
246         return 1;
247     }
248 #ifdef CRYPTO_MDEBUG
249     if (tt->field_name)
250         CRYPTO_push_info(tt->field_name);
251 #endif
252     /* If SET OF or SEQUENCE OF, its a STACK */
253     if (tt->flags & ASN1_TFLG_SK_MASK) {
254         STACK_OF(ASN1_VALUE) *skval;
255         skval = sk_ASN1_VALUE_new_null();
256         if (!skval) {
257             ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
258             ret = 0;
259             goto done;
260         }
261         *pval = (ASN1_VALUE *)skval;
262         ret = 1;
263         goto done;
264     }
265     /* Otherwise pass it back to the item routine */
266     ret = ASN1_item_ex_new(pval, it);
267  done:
268 #ifdef CRYPTO_MDEBUG
269     if (it->sname)
270         CRYPTO_pop_info();
271 #endif
272     return ret;
273 }
274
275 static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
276 {
277     /* If ADB or STACK just NULL the field */
278     if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
279         *pval = NULL;
280     else
281         asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
282 }
283
284 /*
285  * NB: could probably combine most of the real XXX_new() behaviour and junk
286  * all the old functions.
287  */
288
289 static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
290 {
291     ASN1_TYPE *typ;
292     ASN1_STRING *str;
293     int utype;
294
295     if (!it)
296         return 0;
297
298     if (it->funcs) {
299         const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
300         if (pf->prim_new)
301             return pf->prim_new(pval, it);
302     }
303
304     if (it->itype == ASN1_ITYPE_MSTRING)
305         utype = -1;
306     else
307         utype = it->utype;
308     switch (utype) {
309     case V_ASN1_OBJECT:
310         *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
311         return 1;
312
313     case V_ASN1_BOOLEAN:
314         *(ASN1_BOOLEAN *)pval = it->size;
315         return 1;
316
317     case V_ASN1_NULL:
318         *pval = (ASN1_VALUE *)1;
319         return 1;
320
321     case V_ASN1_ANY:
322         typ = OPENSSL_malloc(sizeof(*typ));
323         if (!typ)
324             return 0;
325         typ->value.ptr = NULL;
326         typ->type = -1;
327         *pval = (ASN1_VALUE *)typ;
328         break;
329
330     default:
331         str = ASN1_STRING_type_new(utype);
332         if (it->itype == ASN1_ITYPE_MSTRING && str)
333             str->flags |= ASN1_STRING_FLAG_MSTRING;
334         *pval = (ASN1_VALUE *)str;
335         break;
336     }
337     if (*pval)
338         return 1;
339     return 0;
340 }
341
342 static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
343 {
344     int utype;
345     if (it && it->funcs) {
346         const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
347         if (pf->prim_clear)
348             pf->prim_clear(pval, it);
349         else
350             *pval = NULL;
351         return;
352     }
353     if (!it || (it->itype == ASN1_ITYPE_MSTRING))
354         utype = -1;
355     else
356         utype = it->utype;
357     if (utype == V_ASN1_BOOLEAN)
358         *(ASN1_BOOLEAN *)pval = it->size;
359     else
360         *pval = NULL;
361 }