summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/tasn_new.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/asn1/tasn_new.c')
-rw-r--r--src/lib/libcrypto/asn1/tasn_new.c234
1 files changed, 95 insertions, 139 deletions
diff --git a/src/lib/libcrypto/asn1/tasn_new.c b/src/lib/libcrypto/asn1/tasn_new.c
index 531dad365c..a0e3db574f 100644
--- a/src/lib/libcrypto/asn1/tasn_new.c
+++ b/src/lib/libcrypto/asn1/tasn_new.c
@@ -3,7 +3,7 @@
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
6 * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved. 6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
@@ -64,30 +64,27 @@
64#include <openssl/asn1t.h> 64#include <openssl/asn1t.h>
65#include <string.h> 65#include <string.h>
66 66
67static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, 67static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine);
68 int combine);
69static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it); 68static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
70static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt); 69static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
71void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it); 70void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
72 71
73ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it) 72ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
74 { 73{
75 ASN1_VALUE *ret = NULL; 74 ASN1_VALUE *ret = NULL;
76 if (ASN1_item_ex_new(&ret, it) > 0) 75 if(ASN1_item_ex_new(&ret, it) > 0) return ret;
77 return ret;
78 return NULL; 76 return NULL;
79 } 77}
80 78
81/* Allocate an ASN1 structure */ 79/* Allocate an ASN1 structure */
82 80
83int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) 81int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
84 { 82{
85 return asn1_item_ex_combine_new(pval, it, 0); 83 return asn1_item_ex_combine_new(pval, it, 0);
86 } 84}
87 85
88static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, 86static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
89 int combine) 87{
90 {
91 const ASN1_TEMPLATE *tt = NULL; 88 const ASN1_TEMPLATE *tt = NULL;
92 const ASN1_COMPAT_FUNCS *cf; 89 const ASN1_COMPAT_FUNCS *cf;
93 const ASN1_EXTERN_FUNCS *ef; 90 const ASN1_EXTERN_FUNCS *ef;
@@ -95,155 +92,133 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
95 ASN1_aux_cb *asn1_cb; 92 ASN1_aux_cb *asn1_cb;
96 ASN1_VALUE **pseqval; 93 ASN1_VALUE **pseqval;
97 int i; 94 int i;
98 if (aux && aux->asn1_cb) 95 if(aux && aux->asn1_cb) asn1_cb = aux->asn1_cb;
99 asn1_cb = aux->asn1_cb; 96 else asn1_cb = 0;
100 else
101 asn1_cb = 0;
102 97
103 if (!combine) *pval = NULL; 98 if(!combine) *pval = NULL;
104 99
105#ifdef CRYPTO_MDEBUG 100#ifdef CRYPTO_MDEBUG
106 if (it->sname) 101 if(it->sname) CRYPTO_push_info(it->sname);
107 CRYPTO_push_info(it->sname);
108#endif 102#endif
109 103
110 switch(it->itype) 104 switch(it->itype) {
111 {
112 105
113 case ASN1_ITYPE_EXTERN: 106 case ASN1_ITYPE_EXTERN:
114 ef = it->funcs; 107 ef = it->funcs;
115 if (ef && ef->asn1_ex_new) 108 if(ef && ef->asn1_ex_new) {
116 { 109 if(!ef->asn1_ex_new(pval, it))
117 if (!ef->asn1_ex_new(pval, it))
118 goto memerr; 110 goto memerr;
119 } 111 }
120 break; 112 break;
121 113
122 case ASN1_ITYPE_COMPAT: 114 case ASN1_ITYPE_COMPAT:
123 cf = it->funcs; 115 cf = it->funcs;
124 if (cf && cf->asn1_new) { 116 if(cf && cf->asn1_new) {
125 *pval = cf->asn1_new(); 117 *pval = cf->asn1_new();
126 if (!*pval) 118 if(!*pval) goto memerr;
127 goto memerr;
128 } 119 }
129 break; 120 break;
130 121
131 case ASN1_ITYPE_PRIMITIVE: 122 case ASN1_ITYPE_PRIMITIVE:
132 if (it->templates) 123 if(it->templates) {
133 { 124 if(!ASN1_template_new(pval, it->templates))
134 if (!ASN1_template_new(pval, it->templates))
135 goto memerr; 125 goto memerr;
136 } 126 } else {
137 else if (!ASN1_primitive_new(pval, it)) 127 if(!ASN1_primitive_new(pval, it))
138 goto memerr; 128 goto memerr;
129 }
139 break; 130 break;
140 131
141 case ASN1_ITYPE_MSTRING: 132 case ASN1_ITYPE_MSTRING:
142 if (!ASN1_primitive_new(pval, it)) 133 if(!ASN1_primitive_new(pval, it))
143 goto memerr; 134 goto memerr;
144 break; 135 break;
145 136
146 case ASN1_ITYPE_CHOICE: 137 case ASN1_ITYPE_CHOICE:
147 if (asn1_cb) 138 if(asn1_cb) {
148 {
149 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it); 139 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it);
150 if (!i) 140 if(!i) goto auxerr;
151 goto auxerr; 141 if(i==2) {
152 if (i==2)
153 {
154#ifdef CRYPTO_MDEBUG 142#ifdef CRYPTO_MDEBUG
155 if (it->sname) 143 if(it->sname) CRYPTO_pop_info();
156 CRYPTO_pop_info();
157#endif 144#endif
158 return 1; 145 return 1;
159 }
160 } 146 }
161 if (!combine) 147 }
162 { 148 if(!combine) {
163 *pval = OPENSSL_malloc(it->size); 149 *pval = OPENSSL_malloc(it->size);
164 if (!*pval) 150 if(!*pval) goto memerr;
165 goto memerr;
166 memset(*pval, 0, it->size); 151 memset(*pval, 0, it->size);
167 } 152 }
168 asn1_set_choice_selector(pval, -1, it); 153 asn1_set_choice_selector(pval, -1, it);
169 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it)) 154 if(asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it))
170 goto auxerr; 155 goto auxerr;
171 break; 156 break;
172 157
173 case ASN1_ITYPE_NDEF_SEQUENCE:
174 case ASN1_ITYPE_SEQUENCE: 158 case ASN1_ITYPE_SEQUENCE:
175 if (asn1_cb) 159 if(asn1_cb) {
176 {
177 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it); 160 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it);
178 if (!i) 161 if(!i) goto auxerr;
179 goto auxerr; 162 if(i==2) {
180 if (i==2)
181 {
182#ifdef CRYPTO_MDEBUG 163#ifdef CRYPTO_MDEBUG
183 if (it->sname) 164 if(it->sname) CRYPTO_pop_info();
184 CRYPTO_pop_info();
185#endif 165#endif
186 return 1; 166 return 1;
187 }
188 } 167 }
189 if (!combine) 168 }
190 { 169 if(!combine) {
191 *pval = OPENSSL_malloc(it->size); 170 *pval = OPENSSL_malloc(it->size);
192 if (!*pval) 171 if(!*pval) goto memerr;
193 goto memerr;
194 memset(*pval, 0, it->size); 172 memset(*pval, 0, it->size);
195 asn1_do_lock(pval, 0, it); 173 asn1_do_lock(pval, 0, it);
196 asn1_enc_init(pval, it); 174 asn1_enc_init(pval, it);
197 } 175 }
198 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) 176 for(i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
199 {
200 pseqval = asn1_get_field_ptr(pval, tt); 177 pseqval = asn1_get_field_ptr(pval, tt);
201 if (!ASN1_template_new(pseqval, tt)) 178 if(!ASN1_template_new(pseqval, tt)) goto memerr;
202 goto memerr; 179 }
203 } 180 if(asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it))
204 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it))
205 goto auxerr; 181 goto auxerr;
206 break; 182 break;
207 } 183 }
208#ifdef CRYPTO_MDEBUG 184#ifdef CRYPTO_MDEBUG
209 if (it->sname) CRYPTO_pop_info(); 185 if(it->sname) CRYPTO_pop_info();
210#endif 186#endif
211 return 1; 187 return 1;
212 188
213 memerr: 189 memerr:
214 ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ERR_R_MALLOC_FAILURE); 190 ASN1err(ASN1_F_ASN1_ITEM_NEW, ERR_R_MALLOC_FAILURE);
215#ifdef CRYPTO_MDEBUG 191#ifdef CRYPTO_MDEBUG
216 if (it->sname) CRYPTO_pop_info(); 192 if(it->sname) CRYPTO_pop_info();
217#endif 193#endif
218 return 0; 194 return 0;
219 195
220 auxerr: 196 auxerr:
221 ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ASN1_R_AUX_ERROR); 197 ASN1err(ASN1_F_ASN1_ITEM_NEW, ASN1_R_AUX_ERROR);
222 ASN1_item_ex_free(pval, it); 198 ASN1_item_ex_free(pval, it);
223#ifdef CRYPTO_MDEBUG 199#ifdef CRYPTO_MDEBUG
224 if (it->sname) CRYPTO_pop_info(); 200 if(it->sname) CRYPTO_pop_info();
225#endif 201#endif
226 return 0; 202 return 0;
227 203
228 } 204}
229 205
230static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) 206static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
231 { 207{
232 const ASN1_EXTERN_FUNCS *ef; 208 const ASN1_EXTERN_FUNCS *ef;
233 209
234 switch(it->itype) 210 switch(it->itype) {
235 {
236 211
237 case ASN1_ITYPE_EXTERN: 212 case ASN1_ITYPE_EXTERN:
238 ef = it->funcs; 213 ef = it->funcs;
239 if (ef && ef->asn1_ex_clear) 214 if(ef && ef->asn1_ex_clear)
240 ef->asn1_ex_clear(pval, it); 215 ef->asn1_ex_clear(pval, it);
241 else *pval = NULL; 216 else *pval = NULL;
242 break; 217 break;
243 218
244 219
245 case ASN1_ITYPE_PRIMITIVE: 220 case ASN1_ITYPE_PRIMITIVE:
246 if (it->templates) 221 if(it->templates)
247 asn1_template_clear(pval, it->templates); 222 asn1_template_clear(pval, it->templates);
248 else 223 else
249 asn1_primitive_clear(pval, it); 224 asn1_primitive_clear(pval, it);
@@ -256,90 +231,75 @@ static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
256 case ASN1_ITYPE_COMPAT: 231 case ASN1_ITYPE_COMPAT:
257 case ASN1_ITYPE_CHOICE: 232 case ASN1_ITYPE_CHOICE:
258 case ASN1_ITYPE_SEQUENCE: 233 case ASN1_ITYPE_SEQUENCE:
259 case ASN1_ITYPE_NDEF_SEQUENCE:
260 *pval = NULL; 234 *pval = NULL;
261 break; 235 break;
262 }
263 } 236 }
237}
264 238
265 239
266int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) 240int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
267 { 241{
268 const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item); 242 const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
269 int ret; 243 int ret;
270 if (tt->flags & ASN1_TFLG_OPTIONAL) 244 if(tt->flags & ASN1_TFLG_OPTIONAL) {
271 {
272 asn1_template_clear(pval, tt); 245 asn1_template_clear(pval, tt);
273 return 1; 246 return 1;
274 } 247 }
275 /* If ANY DEFINED BY nothing to do */ 248 /* If ANY DEFINED BY nothing to do */
276 249
277 if (tt->flags & ASN1_TFLG_ADB_MASK) 250 if(tt->flags & ASN1_TFLG_ADB_MASK) {
278 {
279 *pval = NULL; 251 *pval = NULL;
280 return 1; 252 return 1;
281 } 253 }
282#ifdef CRYPTO_MDEBUG 254#ifdef CRYPTO_MDEBUG
283 if (tt->field_name) 255 if(tt->field_name) CRYPTO_push_info(tt->field_name);
284 CRYPTO_push_info(tt->field_name);
285#endif 256#endif
286 /* If SET OF or SEQUENCE OF, its a STACK */ 257 /* If SET OF or SEQUENCE OF, its a STACK */
287 if (tt->flags & ASN1_TFLG_SK_MASK) 258 if(tt->flags & ASN1_TFLG_SK_MASK) {
288 {
289 STACK_OF(ASN1_VALUE) *skval; 259 STACK_OF(ASN1_VALUE) *skval;
290 skval = sk_ASN1_VALUE_new_null(); 260 skval = sk_ASN1_VALUE_new_null();
291 if (!skval) 261 if(!skval) {
292 {
293 ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE); 262 ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
294 ret = 0; 263 ret = 0;
295 goto done; 264 goto done;
296 } 265 }
297 *pval = (ASN1_VALUE *)skval; 266 *pval = (ASN1_VALUE *)skval;
298 ret = 1; 267 ret = 1;
299 goto done; 268 goto done;
300 } 269 }
301 /* Otherwise pass it back to the item routine */ 270 /* Otherwise pass it back to the item routine */
302 ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE); 271 ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
303 done: 272 done:
304#ifdef CRYPTO_MDEBUG 273#ifdef CRYPTO_MDEBUG
305 if (it->sname) 274 if(it->sname) CRYPTO_pop_info();
306 CRYPTO_pop_info();
307#endif 275#endif
308 return ret; 276 return ret;
309 } 277}
310 278
311static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) 279static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
312 { 280{
313 /* If ADB or STACK just NULL the field */ 281 /* If ADB or STACK just NULL the field */
314 if (tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK)) 282 if(tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK))
315 *pval = NULL; 283 *pval = NULL;
316 else 284 else
317 asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item)); 285 asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
318 } 286}
319 287
320 288
321/* NB: could probably combine most of the real XXX_new() behaviour and junk 289/* NB: could probably combine most of the real XXX_new() behaviour and junk all the old
322 * all the old functions. 290 * functions.
323 */ 291 */
324 292
325int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it) 293int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
326 { 294{
327 ASN1_TYPE *typ; 295 ASN1_TYPE *typ;
328 int utype; 296 int utype;
329 297 const ASN1_PRIMITIVE_FUNCS *pf;
330 if (it && it->funcs) 298 pf = it->funcs;
331 { 299 if(pf && pf->prim_new) return pf->prim_new(pval, it);
332 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; 300 if(!it || (it->itype == ASN1_ITYPE_MSTRING)) utype = -1;
333 if (pf->prim_new) 301 else utype = it->utype;
334 return pf->prim_new(pval, it); 302 switch(utype) {
335 }
336
337 if (!it || (it->itype == ASN1_ITYPE_MSTRING))
338 utype = -1;
339 else
340 utype = it->utype;
341 switch(utype)
342 {
343 case V_ASN1_OBJECT: 303 case V_ASN1_OBJECT:
344 *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef); 304 *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
345 return 1; 305 return 1;
@@ -357,8 +317,7 @@ int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
357 317
358 case V_ASN1_ANY: 318 case V_ASN1_ANY:
359 typ = OPENSSL_malloc(sizeof(ASN1_TYPE)); 319 typ = OPENSSL_malloc(sizeof(ASN1_TYPE));
360 if (!typ) 320 if(!typ) return 0;
361 return 0;
362 typ->value.ptr = NULL; 321 typ->value.ptr = NULL;
363 typ->type = -1; 322 typ->type = -1;
364 *pval = (ASN1_VALUE *)typ; 323 *pval = (ASN1_VALUE *)typ;
@@ -367,29 +326,26 @@ int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
367 default: 326 default:
368 *pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype); 327 *pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype);
369 break; 328 break;
370 }
371 if (*pval)
372 return 1;
373 return 0;
374 } 329 }
330 if(*pval) return 1;
331 return 0;
332}
375 333
376void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) 334void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
377 { 335{
378 int utype; 336 int utype;
379 if (it && it->funcs) 337 const ASN1_PRIMITIVE_FUNCS *pf;
380 { 338 pf = it->funcs;
381 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; 339 if(pf) {
382 if (pf->prim_clear) 340 if(pf->prim_clear)
383 pf->prim_clear(pval, it); 341 pf->prim_clear(pval, it);
384 else 342 else
385 *pval = NULL; 343 *pval = NULL;
386 return; 344 return;
387 } 345 }
388 if (!it || (it->itype == ASN1_ITYPE_MSTRING)) 346 if(!it || (it->itype == ASN1_ITYPE_MSTRING)) utype = -1;
389 utype = -1; 347 else utype = it->utype;
390 else 348 if(utype == V_ASN1_BOOLEAN)
391 utype = it->utype;
392 if (utype == V_ASN1_BOOLEAN)
393 *(ASN1_BOOLEAN *)pval = it->size; 349 *(ASN1_BOOLEAN *)pval = it->size;
394 else *pval = NULL; 350 else *pval = NULL;
395 } 351}