Fix typo in OCSP nonce extension.
[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         switch(it->itype) {
100
101                 case ASN1_ITYPE_EXTERN:
102                 ef = it->funcs;
103                 if(ef && ef->asn1_ex_new) {
104                         if(!ef->asn1_ex_new(pval, it))
105                                 goto memerr;
106                 }
107                 break;
108
109                 case ASN1_ITYPE_COMPAT:
110                 cf = it->funcs;
111                 if(cf && cf->asn1_new) {
112                         *pval = cf->asn1_new();
113                         if(!*pval) goto memerr;
114                 }
115                 break;
116
117                 case ASN1_ITYPE_PRIMITIVE:
118                 if(it->templates) {
119                         if(!ASN1_template_new(pval, it->templates))
120                                 goto memerr;
121                 } else {
122                         if(!ASN1_primitive_new(pval, it))
123                                 goto memerr;
124                 }
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);
135                         if(!i) goto auxerr;
136                         if(i==2) return 1;
137                 }
138                 if(!combine) {
139                         *pval = OPENSSL_malloc(it->size);
140                         if(!*pval) goto memerr;
141                         memset(*pval, 0, it->size);
142                 }
143                 asn1_set_choice_selector(pval, -1, it);
144                 if(asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it))
145                                 goto auxerr;
146                 break;
147
148                 case ASN1_ITYPE_SEQUENCE:
149                 if(asn1_cb) {
150                         i = asn1_cb(ASN1_OP_NEW_PRE, pval, it);
151                         if(!i) goto auxerr;
152                         if(i==2) return 1;
153                 }
154                 if(!combine) {
155                         *pval = OPENSSL_malloc(it->size);
156                         if(!*pval) goto memerr;
157                         memset(*pval, 0, it->size);
158                         asn1_do_lock(pval, 0, it);
159                         asn1_enc_init(pval, it);
160                 }
161                 for(i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
162                         pseqval = asn1_get_field_ptr(pval, tt);
163                         if(!ASN1_template_new(pseqval, tt)) goto memerr;
164                 }
165                 if(asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it))
166                                 goto auxerr;
167                 break;
168         }
169         return 1;
170
171         memerr:
172         ASN1err(ASN1_F_ASN1_ITEM_NEW, ERR_R_MALLOC_FAILURE);
173         return 0;
174
175         auxerr:
176         ASN1err(ASN1_F_ASN1_ITEM_NEW, ASN1_R_AUX_ERROR);
177         ASN1_item_ex_free(pval, it);
178         return 0;
179
180 }
181
182 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
183 {
184         const ASN1_EXTERN_FUNCS *ef;
185
186         switch(it->itype) {
187
188                 case ASN1_ITYPE_EXTERN:
189                 ef = it->funcs;
190                 if(ef && ef->asn1_ex_clear) 
191                         ef->asn1_ex_clear(pval, it);
192                 else *pval = NULL;
193                 break;
194
195
196                 case ASN1_ITYPE_PRIMITIVE:
197                 if(it->templates) 
198                         asn1_template_clear(pval, it->templates);
199                 else
200                         asn1_primitive_clear(pval, it);
201                 break;
202
203                 case ASN1_ITYPE_MSTRING:
204                 asn1_primitive_clear(pval, it);
205                 break;
206
207                 case ASN1_ITYPE_COMPAT:
208                 case ASN1_ITYPE_CHOICE:
209                 case ASN1_ITYPE_SEQUENCE:
210                 *pval = NULL;
211                 break;
212         }
213 }
214
215
216 int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
217 {
218         const ASN1_ITEM *it = tt->item;
219         if(tt->flags & ASN1_TFLG_OPTIONAL) {
220                 asn1_template_clear(pval, tt);
221                 return 1;
222         }
223         /* If ANY DEFINED BY nothing to do */
224
225         if(tt->flags & ASN1_TFLG_ADB_MASK) {
226                 *pval = NULL;
227                 return 1;
228         }
229         /* If SET OF or SEQUENCE OF, its a STACK */
230         if(tt->flags & ASN1_TFLG_SK_MASK) {
231                 STACK_OF(ASN1_VALUE) *skval;
232                 skval = sk_ASN1_VALUE_new_null();
233                 if(!skval) {
234                         ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
235                         return 0;
236                 }
237                 *pval = (ASN1_VALUE *)skval;
238                 return 1;
239         }
240         /* Otherwise pass it back to the item routine */
241         return asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
242 }
243
244 void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
245 {
246         /* If ADB or STACK just NULL the field */
247         if(tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK)) 
248                 *pval = NULL;
249         else
250                 asn1_item_clear(pval, tt->item);
251 }
252
253
254 /* NB: could probably combine most of the real XXX_new() behaviour and junk all the old
255  * functions.
256  */
257
258 int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
259 {
260         ASN1_TYPE *typ;
261         int utype;
262         const ASN1_PRIMITIVE_FUNCS *pf;
263         pf = it->funcs;
264         if(pf && pf->prim_new) return pf->prim_new(pval, it);
265         if(!it || (it->itype == ASN1_ITYPE_MSTRING)) utype = -1;
266         else utype = it->utype;
267         switch(utype) {
268                 case V_ASN1_OBJECT:
269                 *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
270                 return 1;
271
272                 case V_ASN1_BOOLEAN:
273                 *(ASN1_BOOLEAN *)pval = it->size;
274                 return 1;
275
276                 case V_ASN1_NULL:
277                 *pval = (ASN1_VALUE *)1;
278                 return 1;
279
280                 case V_ASN1_ANY:
281                 typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
282                 if(!typ) return 0;
283                 typ->value.ptr = NULL;
284                 typ->type = -1;
285                 *pval = (ASN1_VALUE *)typ;
286                 break;
287
288                 default:
289                 *pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype);
290                 break;
291         }
292         if(*pval) return 1;
293         return 0;
294 }
295
296 void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
297 {
298         int utype;
299         const ASN1_PRIMITIVE_FUNCS *pf;
300         pf = it->funcs;
301         if(pf) {
302                 if(pf->prim_clear)
303                         pf->prim_clear(pval, it);
304                 else 
305                         *pval = NULL;
306                 return;
307         }
308         if(!it || (it->itype == ASN1_ITYPE_MSTRING)) utype = -1;
309         else utype = it->utype;
310         if(utype == V_ASN1_BOOLEAN)
311                 *(ASN1_BOOLEAN *)pval = it->size;
312         else *pval = NULL;
313 }