798f9bfd5e38ff3e8b467ea9b9cc750d434ab6af
[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 #include <string.h>
66
67 static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine);
68 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
69 static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
70 void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
71
72 ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
73 {
74         ASN1_VALUE *ret = NULL;
75         if(ASN1_item_ex_new(&ret, it) > 0) return ret;
76         return NULL;
77 }
78
79 /* Allocate an ASN1 structure */
80
81 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
82 {
83         return asn1_item_ex_combine_new(pval, it, 0);
84 }
85
86 static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
87 {
88         const ASN1_TEMPLATE *tt = NULL;
89         const ASN1_COMPAT_FUNCS *cf;
90         const ASN1_EXTERN_FUNCS *ef;
91         const ASN1_AUX *aux = it->funcs;
92         ASN1_aux_cb *asn1_cb;
93         ASN1_VALUE **pseqval;
94         int i;
95         if(aux && aux->asn1_cb) asn1_cb = aux->asn1_cb;
96         else asn1_cb = 0;
97
98         if(!combine) *pval = NULL;
99
100 #ifdef CRYPTO_MDEBUG
101         if(it->sname) CRYPTO_push_info(it->sname);
102 #endif
103
104         switch(it->itype) {
105
106                 case ASN1_ITYPE_EXTERN:
107                 ef = it->funcs;
108                 if(ef && ef->asn1_ex_new) {
109                         if(!ef->asn1_ex_new(pval, it))
110                                 goto memerr;
111                 }
112                 break;
113
114                 case ASN1_ITYPE_COMPAT:
115                 cf = it->funcs;
116                 if(cf && cf->asn1_new) {
117                         *pval = cf->asn1_new();
118                         if(!*pval) goto memerr;
119                 }
120                 break;
121
122                 case ASN1_ITYPE_PRIMITIVE:
123                 if(it->templates) {
124                         if(!ASN1_template_new(pval, it->templates))
125                                 goto memerr;
126                 } else {
127                         if(!ASN1_primitive_new(pval, it))
128                                 goto memerr;
129                 }
130                 break;
131
132                 case ASN1_ITYPE_MSTRING:
133                 if(!ASN1_primitive_new(pval, it))
134                                 goto memerr;
135                 break;
136
137                 case ASN1_ITYPE_CHOICE:
138                 if(asn1_cb) {
139                         i = asn1_cb(ASN1_OP_NEW_PRE, pval, it);
140                         if(!i) goto auxerr;
141                         if(i==2) return 1;
142                 }
143                 if(!combine) {
144                         *pval = OPENSSL_malloc(it->size);
145                         if(!*pval) goto memerr;
146                         memset(*pval, 0, it->size);
147                 }
148                 asn1_set_choice_selector(pval, -1, it);
149                 if(asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it))
150                                 goto auxerr;
151                 break;
152
153                 case ASN1_ITYPE_SEQUENCE:
154                 if(asn1_cb) {
155                         i = asn1_cb(ASN1_OP_NEW_PRE, pval, it);
156                         if(!i) goto auxerr;
157                         if(i==2) {
158 #ifdef CRYPTO_MDEBUG
159                                 if(it->sname) CRYPTO_pop_info();
160 #endif
161                                 return 1;
162                         }
163                 }
164                 if(!combine) {
165                         *pval = OPENSSL_malloc(it->size);
166                         if(!*pval) goto memerr;
167                         memset(*pval, 0, it->size);
168                         asn1_do_lock(pval, 0, it);
169                         asn1_enc_init(pval, it);
170                 }
171                 for(i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
172                         pseqval = asn1_get_field_ptr(pval, tt);
173                         if(!ASN1_template_new(pseqval, tt)) goto memerr;
174                 }
175                 if(asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it))
176                                 goto auxerr;
177                 break;
178         }
179 #ifdef CRYPTO_MDEBUG
180         if(it->sname) CRYPTO_pop_info();
181 #endif
182         return 1;
183
184         memerr:
185         ASN1err(ASN1_F_ASN1_ITEM_NEW, ERR_R_MALLOC_FAILURE);
186 #ifdef CRYPTO_MDEBUG
187         if(it->sname) CRYPTO_pop_info();
188 #endif
189         return 0;
190
191         auxerr:
192         ASN1err(ASN1_F_ASN1_ITEM_NEW, ASN1_R_AUX_ERROR);
193         ASN1_item_ex_free(pval, it);
194 #ifdef CRYPTO_MDEBUG
195         if(it->sname) 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 *pval = NULL;
212                 break;
213
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_COMPAT:
227                 case ASN1_ITYPE_CHOICE:
228                 case ASN1_ITYPE_SEQUENCE:
229                 *pval = NULL;
230                 break;
231         }
232 }
233
234
235 int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
236 {
237         const ASN1_ITEM *it = tt->item;
238         int ret;
239         if(tt->flags & ASN1_TFLG_OPTIONAL) {
240                 asn1_template_clear(pval, tt);
241                 return 1;
242         }
243         /* If ANY DEFINED BY nothing to do */
244
245         if(tt->flags & ASN1_TFLG_ADB_MASK) {
246                 *pval = NULL;
247                 return 1;
248         }
249 #ifdef CRYPTO_MDEBUG
250         if(tt->field_name) 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_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
267         done:
268 #ifdef CRYPTO_MDEBUG
269         if(it->sname) CRYPTO_pop_info();
270 #endif
271         return ret;
272 }
273
274 void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
275 {
276         /* If ADB or STACK just NULL the field */
277         if(tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK)) 
278                 *pval = NULL;
279         else
280                 asn1_item_clear(pval, tt->item);
281 }
282
283
284 /* NB: could probably combine most of the real XXX_new() behaviour and junk all the old
285  * functions.
286  */
287
288 int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
289 {
290         ASN1_TYPE *typ;
291         int utype;
292         const ASN1_PRIMITIVE_FUNCS *pf;
293         pf = it->funcs;
294         if(pf && pf->prim_new) return pf->prim_new(pval, it);
295         if(!it || (it->itype == ASN1_ITYPE_MSTRING)) utype = -1;
296         else utype = it->utype;
297         switch(utype) {
298                 case V_ASN1_OBJECT:
299                 *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
300                 return 1;
301
302                 case V_ASN1_BOOLEAN:
303                 *(ASN1_BOOLEAN *)pval = it->size;
304                 return 1;
305
306                 case V_ASN1_NULL:
307                 *pval = (ASN1_VALUE *)1;
308                 return 1;
309
310                 case V_ASN1_ANY:
311                 typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
312                 if(!typ) return 0;
313                 typ->value.ptr = NULL;
314                 typ->type = -1;
315                 *pval = (ASN1_VALUE *)typ;
316                 break;
317
318                 default:
319                 *pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype);
320                 break;
321         }
322         if(*pval) return 1;
323         return 0;
324 }
325
326 void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
327 {
328         int utype;
329         const ASN1_PRIMITIVE_FUNCS *pf;
330         pf = it->funcs;
331         if(pf) {
332                 if(pf->prim_clear)
333                         pf->prim_clear(pval, it);
334                 else 
335                         *pval = NULL;
336                 return;
337         }
338         if(!it || (it->itype == ASN1_ITYPE_MSTRING)) utype = -1;
339         else utype = it->utype;
340         if(utype == V_ASN1_BOOLEAN)
341                 *(ASN1_BOOLEAN *)pval = it->size;
342         else *pval = NULL;
343 }