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 "asn1_locl.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 const ASN1_AUX *aux = it->funcs;
99 ASN1_aux_cb *asn1_cb;
100 ASN1_VALUE **pseqval;
101 int i;
102 if (aux && aux->asn1_cb)
103 asn1_cb = aux->asn1_cb;
104 else
105 asn1_cb = 0;
106
107 switch (it->itype) {
108
109 case ASN1_ITYPE_EXTERN:
110 ef = it->funcs;
111 if (ef && ef->asn1_ex_new) {
112 if (!ef->asn1_ex_new(pval, it))
113 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 if (!ASN1_primitive_new(pval, it))
122 goto memerr;
123 break;
124
125 case ASN1_ITYPE_MSTRING:
126 if (!ASN1_primitive_new(pval, it))
127 goto memerr;
128 break;
129
130 case ASN1_ITYPE_CHOICE:
131 if (asn1_cb) {
132 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
133 if (!i)
134 goto auxerr;
135 if (i == 2) {
136 return 1;
137 }
138 }
139 if (!combine) {
140 *pval = OPENSSL_malloc(it->size);
141 if (!*pval)
142 goto memerr;
143 OPENSSL_memset(*pval, 0, it->size);
144 }
145 asn1_set_choice_selector(pval, -1, it);
146 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
147 goto auxerr2;
148 break;
149
150 case ASN1_ITYPE_SEQUENCE:
151 if (asn1_cb) {
152 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
153 if (!i)
154 goto auxerr;
155 if (i == 2) {
156 return 1;
157 }
158 }
159 if (!combine) {
160 *pval = OPENSSL_malloc(it->size);
161 if (!*pval)
162 goto memerr;
163 OPENSSL_memset(*pval, 0, it->size);
164 asn1_refcount_set_one(pval, it);
165 asn1_enc_init(pval, it);
166 }
167 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
168 pseqval = asn1_get_field_ptr(pval, tt);
169 if (!ASN1_template_new(pseqval, tt))
170 goto memerr2;
171 }
172 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
173 goto auxerr2;
174 break;
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 ASN1_STRING *str;
275 int utype;
276
277 if (!it)
278 return 0;
279
280 /* Historically, |it->funcs| for primitive types contained an
281 * |ASN1_PRIMITIVE_FUNCS| table of calbacks. */
282 assert(it->funcs == NULL);
283
284 if (it->itype == ASN1_ITYPE_MSTRING)
285 utype = -1;
286 else
287 utype = it->utype;
288 switch (utype) {
289 case V_ASN1_OBJECT:
290 *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
291 return 1;
292
293 case V_ASN1_BOOLEAN:
294 *(ASN1_BOOLEAN *)pval = it->size;
295 return 1;
296
297 case V_ASN1_NULL:
298 *pval = (ASN1_VALUE *)1;
299 return 1;
300
301 case V_ASN1_ANY:
302 typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
303 if (!typ)
304 return 0;
305 typ->value.ptr = NULL;
306 typ->type = -1;
307 *pval = (ASN1_VALUE *)typ;
308 break;
309
310 default:
311 str = ASN1_STRING_type_new(utype);
312 if (it->itype == ASN1_ITYPE_MSTRING && str)
313 str->flags |= ASN1_STRING_FLAG_MSTRING;
314 *pval = (ASN1_VALUE *)str;
315 break;
316 }
317 if (*pval)
318 return 1;
319 return 0;
320 }
321
asn1_primitive_clear(ASN1_VALUE ** pval,const ASN1_ITEM * it)322 static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
323 {
324 int utype;
325 /* Historically, |it->funcs| for primitive types contained an
326 * |ASN1_PRIMITIVE_FUNCS| table of calbacks. */
327 assert(it == NULL || it->funcs == NULL);
328 if (!it || (it->itype == ASN1_ITYPE_MSTRING))
329 utype = -1;
330 else
331 utype = it->utype;
332 if (utype == V_ASN1_BOOLEAN)
333 *(ASN1_BOOLEAN *)pval = it->size;
334 else
335 *pval = NULL;
336 }
337