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