1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <openssl/asn1.h>
58 
59 #include <string.h>
60 
61 #include <openssl/asn1t.h>
62 #include <openssl/err.h>
63 #include <openssl/mem.h>
64 #include <openssl/obj.h>
65 
66 #include "internal.h"
67 #include "../internal.h"
68 
69 
70 static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
71                                     int combine);
72 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
73 static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
74 static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
75 static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
76 static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
77 
ASN1_item_new(const ASN1_ITEM * it)78 ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
79 {
80     ASN1_VALUE *ret = NULL;
81     if (ASN1_item_ex_new(&ret, it) > 0)
82         return ret;
83     return NULL;
84 }
85 
86 /* Allocate an ASN1 structure */
87 
ASN1_item_ex_new(ASN1_VALUE ** pval,const ASN1_ITEM * it)88 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
89 {
90     return asn1_item_ex_combine_new(pval, it, 0);
91 }
92 
asn1_item_ex_combine_new(ASN1_VALUE ** pval,const ASN1_ITEM * it,int combine)93 static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
94                                     int combine)
95 {
96     const ASN1_TEMPLATE *tt = NULL;
97     const ASN1_EXTERN_FUNCS *ef;
98     ASN1_VALUE **pseqval;
99     int i;
100 
101     switch (it->itype) {
102 
103     case ASN1_ITYPE_EXTERN:
104         ef = it->funcs;
105         if (ef && ef->asn1_ex_new) {
106             if (!ef->asn1_ex_new(pval, it))
107                 goto memerr;
108         }
109         break;
110 
111     case ASN1_ITYPE_PRIMITIVE:
112         if (it->templates) {
113             if (!ASN1_template_new(pval, it->templates))
114                 goto memerr;
115         } else if (!ASN1_primitive_new(pval, it))
116             goto memerr;
117         break;
118 
119     case ASN1_ITYPE_MSTRING:
120         if (!ASN1_primitive_new(pval, it))
121             goto memerr;
122         break;
123 
124     case ASN1_ITYPE_CHOICE: {
125         const ASN1_AUX *aux = it->funcs;
126         ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
127         if (asn1_cb) {
128             i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
129             if (!i)
130                 goto auxerr;
131             if (i == 2) {
132                 return 1;
133             }
134         }
135         if (!combine) {
136             *pval = OPENSSL_malloc(it->size);
137             if (!*pval)
138                 goto memerr;
139             OPENSSL_memset(*pval, 0, it->size);
140         }
141         asn1_set_choice_selector(pval, -1, it);
142         if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
143             goto auxerr2;
144         break;
145     }
146 
147     case ASN1_ITYPE_SEQUENCE: {
148         const ASN1_AUX *aux = it->funcs;
149         ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
150         if (asn1_cb) {
151             i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
152             if (!i)
153                 goto auxerr;
154             if (i == 2) {
155                 return 1;
156             }
157         }
158         if (!combine) {
159             *pval = OPENSSL_malloc(it->size);
160             if (!*pval)
161                 goto memerr;
162             OPENSSL_memset(*pval, 0, it->size);
163             asn1_refcount_set_one(pval, it);
164             asn1_enc_init(pval, it);
165         }
166         for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
167             pseqval = asn1_get_field_ptr(pval, tt);
168             if (!ASN1_template_new(pseqval, tt))
169                 goto memerr2;
170         }
171         if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
172             goto auxerr2;
173         break;
174     }
175     }
176     return 1;
177 
178  memerr2:
179     asn1_item_combine_free(pval, it, combine);
180  memerr:
181     OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
182     return 0;
183 
184  auxerr2:
185     asn1_item_combine_free(pval, it, combine);
186  auxerr:
187     OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR);
188     return 0;
189 
190 }
191 
asn1_item_clear(ASN1_VALUE ** pval,const ASN1_ITEM * it)192 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
193 {
194     const ASN1_EXTERN_FUNCS *ef;
195 
196     switch (it->itype) {
197 
198     case ASN1_ITYPE_EXTERN:
199         ef = it->funcs;
200         if (ef && ef->asn1_ex_clear)
201             ef->asn1_ex_clear(pval, it);
202         else
203             *pval = NULL;
204         break;
205 
206     case ASN1_ITYPE_PRIMITIVE:
207         if (it->templates)
208             asn1_template_clear(pval, it->templates);
209         else
210             asn1_primitive_clear(pval, it);
211         break;
212 
213     case ASN1_ITYPE_MSTRING:
214         asn1_primitive_clear(pval, it);
215         break;
216 
217     case ASN1_ITYPE_CHOICE:
218     case ASN1_ITYPE_SEQUENCE:
219         *pval = NULL;
220         break;
221     }
222 }
223 
ASN1_template_new(ASN1_VALUE ** pval,const ASN1_TEMPLATE * tt)224 static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
225 {
226     const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
227     int ret;
228     if (tt->flags & ASN1_TFLG_OPTIONAL) {
229         asn1_template_clear(pval, tt);
230         return 1;
231     }
232     /* If ANY DEFINED BY nothing to do */
233 
234     if (tt->flags & ASN1_TFLG_ADB_MASK) {
235         *pval = NULL;
236         return 1;
237     }
238     /* If SET OF or SEQUENCE OF, its a STACK */
239     if (tt->flags & ASN1_TFLG_SK_MASK) {
240         STACK_OF(ASN1_VALUE) *skval;
241         skval = sk_ASN1_VALUE_new_null();
242         if (!skval) {
243             OPENSSL_PUT_ERROR(ASN1, ERR_R_MALLOC_FAILURE);
244             ret = 0;
245             goto done;
246         }
247         *pval = (ASN1_VALUE *)skval;
248         ret = 1;
249         goto done;
250     }
251     /* Otherwise pass it back to the item routine */
252     ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
253  done:
254     return ret;
255 }
256 
asn1_template_clear(ASN1_VALUE ** pval,const ASN1_TEMPLATE * tt)257 static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
258 {
259     /* If ADB or STACK just NULL the field */
260     if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
261         *pval = NULL;
262     else
263         asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
264 }
265 
266 /*
267  * NB: could probably combine most of the real XXX_new() behaviour and junk
268  * all the old functions.
269  */
270 
ASN1_primitive_new(ASN1_VALUE ** pval,const ASN1_ITEM * it)271 static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
272 {
273     ASN1_TYPE *typ;
274     int utype;
275 
276     if (!it)
277         return 0;
278 
279     /* Historically, |it->funcs| for primitive types contained an
280      * |ASN1_PRIMITIVE_FUNCS| table of calbacks. */
281     assert(it->funcs == NULL);
282 
283     if (it->itype == ASN1_ITYPE_MSTRING)
284         utype = -1;
285     else
286         utype = it->utype;
287     switch (utype) {
288     case V_ASN1_OBJECT:
289         *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
290         return 1;
291 
292     case V_ASN1_BOOLEAN:
293         *(ASN1_BOOLEAN *)pval = it->size;
294         return 1;
295 
296     case V_ASN1_NULL:
297         *pval = (ASN1_VALUE *)1;
298         return 1;
299 
300     case V_ASN1_ANY:
301         typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
302         if (!typ)
303             return 0;
304         typ->value.ptr = NULL;
305         typ->type = -1;
306         *pval = (ASN1_VALUE *)typ;
307         break;
308 
309     default:
310         *pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype);
311         break;
312     }
313     if (*pval)
314         return 1;
315     return 0;
316 }
317 
asn1_primitive_clear(ASN1_VALUE ** pval,const ASN1_ITEM * it)318 static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
319 {
320     int utype;
321     /* Historically, |it->funcs| for primitive types contained an
322      * |ASN1_PRIMITIVE_FUNCS| table of calbacks. */
323     assert(it == NULL || it->funcs == NULL);
324     if (!it || (it->itype == ASN1_ITYPE_MSTRING))
325         utype = -1;
326     else
327         utype = it->utype;
328     if (utype == V_ASN1_BOOLEAN)
329         *(ASN1_BOOLEAN *)pval = it->size;
330     else
331         *pval = NULL;
332 }
333