Remove old style ASN.1 support.
[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
67 static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
68                                     int combine);
69 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
70 static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
71 static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
72
73 ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
74 {
75     ASN1_VALUE *ret = NULL;
76     if (ASN1_item_ex_new(&ret, it) > 0)
77         return ret;
78     return NULL;
79 }
80
81 /* Allocate an ASN1 structure */
82
83 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
84 {
85     return asn1_item_ex_combine_new(pval, it, 0);
86 }
87
88 static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
89                                     int combine)
90 {
91     const ASN1_TEMPLATE *tt = NULL;
92     const ASN1_EXTERN_FUNCS *ef;
93     const ASN1_AUX *aux = it->funcs;
94     ASN1_aux_cb *asn1_cb;
95     ASN1_VALUE **pseqval;
96     int i;
97     if (aux && aux->asn1_cb)
98         asn1_cb = aux->asn1_cb;
99     else
100         asn1_cb = 0;
101
102     if (!combine)
103         *pval = NULL;
104
105 #ifdef CRYPTO_MDEBUG
106     if (it->sname)
107         CRYPTO_push_info(it->sname);
108 #endif
109
110     switch (it->itype) {
111
112     case ASN1_ITYPE_EXTERN:
113         ef = it->funcs;
114         if (ef && ef->asn1_ex_new) {
115             if (!ef->asn1_ex_new(pval, it))
116                 goto memerr;
117         }
118         break;
119
120     case ASN1_ITYPE_PRIMITIVE:
121         if (it->templates) {
122             if (!ASN1_template_new(pval, it->templates))
123                 goto memerr;
124         } else if (!ASN1_primitive_new(pval, it))
125             goto memerr;
126         break;
127
128     case ASN1_ITYPE_MSTRING:
129         if (!ASN1_primitive_new(pval, it))
130             goto memerr;
131         break;
132
133     case ASN1_ITYPE_CHOICE:
134         if (asn1_cb) {
135             i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
136             if (!i)
137                 goto auxerr;
138             if (i == 2) {
139 #ifdef CRYPTO_MDEBUG
140                 if (it->sname)
141                     CRYPTO_pop_info();
142 #endif
143                 return 1;
144             }
145         }
146         if (!combine) {
147             *pval = OPENSSL_malloc(it->size);
148             if (!*pval)
149                 goto memerr;
150             memset(*pval, 0, it->size);
151         }
152         asn1_set_choice_selector(pval, -1, it);
153         if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
154             goto auxerr;
155         break;
156
157     case ASN1_ITYPE_NDEF_SEQUENCE:
158     case ASN1_ITYPE_SEQUENCE:
159         if (asn1_cb) {
160             i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
161             if (!i)
162                 goto auxerr;
163             if (i == 2) {
164 #ifdef CRYPTO_MDEBUG
165                 if (it->sname)
166                     CRYPTO_pop_info();
167 #endif
168                 return 1;
169             }
170         }
171         if (!combine) {
172             *pval = OPENSSL_malloc(it->size);
173             if (!*pval)
174                 goto memerr;
175             memset(*pval, 0, it->size);
176             asn1_do_lock(pval, 0, it);
177             asn1_enc_init(pval, it);
178         }
179         for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
180             pseqval = asn1_get_field_ptr(pval, tt);
181             if (!ASN1_template_new(pseqval, tt))
182                 goto memerr;
183         }
184         if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
185             goto auxerr;
186         break;
187     }
188 #ifdef CRYPTO_MDEBUG
189     if (it->sname)
190         CRYPTO_pop_info();
191 #endif
192     return 1;
193
194  memerr:
195     ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ERR_R_MALLOC_FAILURE);
196 #ifdef CRYPTO_MDEBUG
197     if (it->sname)
198         CRYPTO_pop_info();
199 #endif
200     return 0;
201
202  auxerr:
203     ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ASN1_R_AUX_ERROR);
204     ASN1_item_ex_free(pval, it);
205 #ifdef CRYPTO_MDEBUG
206     if (it->sname)
207         CRYPTO_pop_info();
208 #endif
209     return 0;
210
211 }
212
213 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
214 {
215     const ASN1_EXTERN_FUNCS *ef;
216
217     switch (it->itype) {
218
219     case ASN1_ITYPE_EXTERN:
220         ef = it->funcs;
221         if (ef && ef->asn1_ex_clear)
222             ef->asn1_ex_clear(pval, it);
223         else
224             *pval = NULL;
225         break;
226
227     case ASN1_ITYPE_PRIMITIVE:
228         if (it->templates)
229             asn1_template_clear(pval, it->templates);
230         else
231             asn1_primitive_clear(pval, it);
232         break;
233
234     case ASN1_ITYPE_MSTRING:
235         asn1_primitive_clear(pval, it);
236         break;
237
238     case ASN1_ITYPE_CHOICE:
239     case ASN1_ITYPE_SEQUENCE:
240     case ASN1_ITYPE_NDEF_SEQUENCE:
241         *pval = NULL;
242         break;
243     }
244 }
245
246 int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
247 {
248     const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
249     int ret;
250     if (tt->flags & ASN1_TFLG_OPTIONAL) {
251         asn1_template_clear(pval, tt);
252         return 1;
253     }
254     /* If ANY DEFINED BY nothing to do */
255
256     if (tt->flags & ASN1_TFLG_ADB_MASK) {
257         *pval = NULL;
258         return 1;
259     }
260 #ifdef CRYPTO_MDEBUG
261     if (tt->field_name)
262         CRYPTO_push_info(tt->field_name);
263 #endif
264     /* If SET OF or SEQUENCE OF, its a STACK */
265     if (tt->flags & ASN1_TFLG_SK_MASK) {
266         STACK_OF(ASN1_VALUE) *skval;
267         skval = sk_ASN1_VALUE_new_null();
268         if (!skval) {
269             ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
270             ret = 0;
271             goto done;
272         }
273         *pval = (ASN1_VALUE *)skval;
274         ret = 1;
275         goto done;
276     }
277     /* Otherwise pass it back to the item routine */
278     ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
279  done:
280 #ifdef CRYPTO_MDEBUG
281     if (it->sname)
282         CRYPTO_pop_info();
283 #endif
284     return ret;
285 }
286
287 static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
288 {
289     /* If ADB or STACK just NULL the field */
290     if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
291         *pval = NULL;
292     else
293         asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
294 }
295
296 /*
297  * NB: could probably combine most of the real XXX_new() behaviour and junk
298  * all the old functions.
299  */
300
301 int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
302 {
303     ASN1_TYPE *typ;
304     ASN1_STRING *str;
305     int utype;
306
307     if (!it)
308         return 0;
309
310     if (it->funcs) {
311         const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
312         if (pf->prim_new)
313             return pf->prim_new(pval, it);
314     }
315
316     if (it->itype == ASN1_ITYPE_MSTRING)
317         utype = -1;
318     else
319         utype = it->utype;
320     switch (utype) {
321     case V_ASN1_OBJECT:
322         *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
323         return 1;
324
325     case V_ASN1_BOOLEAN:
326         *(ASN1_BOOLEAN *)pval = it->size;
327         return 1;
328
329     case V_ASN1_NULL:
330         *pval = (ASN1_VALUE *)1;
331         return 1;
332
333     case V_ASN1_ANY:
334         typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
335         if (!typ)
336             return 0;
337         typ->value.ptr = NULL;
338         typ->type = -1;
339         *pval = (ASN1_VALUE *)typ;
340         break;
341
342     default:
343         str = ASN1_STRING_type_new(utype);
344         if (it->itype == ASN1_ITYPE_MSTRING && str)
345             str->flags |= ASN1_STRING_FLAG_MSTRING;
346         *pval = (ASN1_VALUE *)str;
347         break;
348     }
349     if (*pval)
350         return 1;
351     return 0;
352 }
353
354 static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
355 {
356     int utype;
357     if (it && it->funcs) {
358         const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
359         if (pf->prim_clear)
360             pf->prim_clear(pval, it);
361         else
362             *pval = NULL;
363         return;
364     }
365     if (!it || (it->itype == ASN1_ITYPE_MSTRING))
366         utype = -1;
367     else
368         utype = it->utype;
369     if (utype == V_ASN1_BOOLEAN)
370         *(ASN1_BOOLEAN *)pval = it->size;
371     else
372         *pval = NULL;
373 }