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