]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/tasn_new.c
Combined patch against master branch for the following issues:
[thirdparty/openssl.git] / crypto / asn1 / tasn_new.c
CommitLineData
0f113f3e 1/*
2039c421 2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
9d6b1ce6 3 *
2039c421
RS
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
9d6b1ce6
DSH
8 */
9
9d6b1ce6
DSH
10#include <stddef.h>
11#include <openssl/asn1.h>
12#include <openssl/objects.h>
13#include <openssl/err.h>
14#include <openssl/asn1t.h>
448361a8 15#include <string.h>
c1ee50aa 16#include "asn1_locl.h"
9d6b1ce6 17
de17bd5d
DSH
18static int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
19 int embed);
47c9a1b5
DSH
20static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
21 int embed);
9d6b1ce6 22static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
c315a547 23static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
9d6b1ce6 24static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
b3e72fc3 25static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
9d6b1ce6
DSH
26
27ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
0f113f3e
MC
28{
29 ASN1_VALUE *ret = NULL;
30 if (ASN1_item_ex_new(&ret, it) > 0)
31 return ret;
32 return NULL;
33}
9d6b1ce6
DSH
34
35/* Allocate an ASN1 structure */
36
37int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
de17bd5d
DSH
38{
39 return asn1_item_embed_new(pval, it, 0);
40}
41
42int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
0f113f3e
MC
43{
44 const ASN1_TEMPLATE *tt = NULL;
0f113f3e
MC
45 const ASN1_EXTERN_FUNCS *ef;
46 const ASN1_AUX *aux = it->funcs;
47 ASN1_aux_cb *asn1_cb;
48 ASN1_VALUE **pseqval;
49 int i;
50 if (aux && aux->asn1_cb)
51 asn1_cb = aux->asn1_cb;
52 else
53 asn1_cb = 0;
54
c2e27310 55#ifndef OPENSSL_NO_CRYPTO_MDEBUG
4fae386c 56 OPENSSL_mem_debug_push(it->sname ? it->sname : "asn1_item_embed_new");
a43cf9fa
DSH
57#endif
58
0f113f3e
MC
59 switch (it->itype) {
60
61 case ASN1_ITYPE_EXTERN:
62 ef = it->funcs;
63 if (ef && ef->asn1_ex_new) {
64 if (!ef->asn1_ex_new(pval, it))
65 goto memerr;
66 }
67 break;
68
0f113f3e
MC
69 case ASN1_ITYPE_PRIMITIVE:
70 if (it->templates) {
c315a547 71 if (!asn1_template_new(pval, it->templates))
0f113f3e 72 goto memerr;
47c9a1b5 73 } else if (!asn1_primitive_new(pval, it, embed))
0f113f3e
MC
74 goto memerr;
75 break;
76
77 case ASN1_ITYPE_MSTRING:
47c9a1b5 78 if (!asn1_primitive_new(pval, it, embed))
0f113f3e
MC
79 goto memerr;
80 break;
81
82 case ASN1_ITYPE_CHOICE:
83 if (asn1_cb) {
84 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
85 if (!i)
86 goto auxerr;
87 if (i == 2) {
c2e27310 88#ifndef OPENSSL_NO_CRYPTO_MDEBUG
4fae386c 89 OPENSSL_mem_debug_pop();
722ca278 90#endif
0f113f3e
MC
91 return 1;
92 }
93 }
44c734e9
DSH
94 if (embed) {
95 memset(*pval, 0, it->size);
96 } else {
97 *pval = OPENSSL_zalloc(it->size);
90945fa3 98 if (*pval == NULL)
44c734e9
DSH
99 goto memerr;
100 }
0f113f3e
MC
101 asn1_set_choice_selector(pval, -1, it);
102 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
83b4049a 103 goto auxerr2;
0f113f3e
MC
104 break;
105
106 case ASN1_ITYPE_NDEF_SEQUENCE:
107 case ASN1_ITYPE_SEQUENCE:
108 if (asn1_cb) {
109 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
110 if (!i)
111 goto auxerr;
112 if (i == 2) {
c2e27310 113#ifndef OPENSSL_NO_CRYPTO_MDEBUG
4fae386c 114 OPENSSL_mem_debug_pop();
a6b7ffdd 115#endif
0f113f3e
MC
116 return 1;
117 }
118 }
de17bd5d
DSH
119 if (embed) {
120 memset(*pval, 0, it->size);
121 } else {
122 *pval = OPENSSL_zalloc(it->size);
90945fa3 123 if (*pval == NULL)
de17bd5d
DSH
124 goto memerr;
125 }
687b4868
F
126 /* 0 : init. lock */
127 if (asn1_do_lock(pval, 0, it) < 0)
83b4049a 128 goto memerr2;
ee9d7637 129 asn1_enc_init(pval, it);
0f113f3e
MC
130 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
131 pseqval = asn1_get_field_ptr(pval, tt);
c315a547 132 if (!asn1_template_new(pseqval, tt))
83b4049a 133 goto memerr2;
0f113f3e
MC
134 }
135 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
83b4049a 136 goto auxerr2;
0f113f3e
MC
137 break;
138 }
c2e27310 139#ifndef OPENSSL_NO_CRYPTO_MDEBUG
4fae386c 140 OPENSSL_mem_debug_pop();
a43cf9fa 141#endif
0f113f3e 142 return 1;
9d6b1ce6 143
83b4049a
BE
144 memerr2:
145 ASN1_item_ex_free(pval, it);
0f113f3e 146 memerr:
de17bd5d 147 ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ERR_R_MALLOC_FAILURE);
c2e27310 148#ifndef OPENSSL_NO_CRYPTO_MDEBUG
4fae386c 149 OPENSSL_mem_debug_pop();
a43cf9fa 150#endif
0f113f3e 151 return 0;
9d6b1ce6 152
83b4049a
BE
153 auxerr2:
154 ASN1_item_ex_free(pval, it);
0f113f3e 155 auxerr:
de17bd5d 156 ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ASN1_R_AUX_ERROR);
c2e27310 157#ifndef OPENSSL_NO_CRYPTO_MDEBUG
4fae386c 158 OPENSSL_mem_debug_pop();
a43cf9fa 159#endif
0f113f3e 160 return 0;
9d6b1ce6 161
0f113f3e 162}
9d6b1ce6
DSH
163
164static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
0f113f3e
MC
165{
166 const ASN1_EXTERN_FUNCS *ef;
167
168 switch (it->itype) {
169
170 case ASN1_ITYPE_EXTERN:
171 ef = it->funcs;
172 if (ef && ef->asn1_ex_clear)
173 ef->asn1_ex_clear(pval, it);
174 else
175 *pval = NULL;
176 break;
177
178 case ASN1_ITYPE_PRIMITIVE:
179 if (it->templates)
180 asn1_template_clear(pval, it->templates);
181 else
182 asn1_primitive_clear(pval, it);
183 break;
184
185 case ASN1_ITYPE_MSTRING:
186 asn1_primitive_clear(pval, it);
187 break;
188
0f113f3e
MC
189 case ASN1_ITYPE_CHOICE:
190 case ASN1_ITYPE_SEQUENCE:
191 case ASN1_ITYPE_NDEF_SEQUENCE:
192 *pval = NULL;
193 break;
194 }
195}
9d6b1ce6 196
c315a547 197static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
0f113f3e
MC
198{
199 const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
de17bd5d
DSH
200 int embed = tt->flags & ASN1_TFLG_EMBED;
201 ASN1_VALUE *tval;
0f113f3e 202 int ret;
de17bd5d
DSH
203 if (embed) {
204 tval = (ASN1_VALUE *)pval;
205 pval = &tval;
206 }
0f113f3e
MC
207 if (tt->flags & ASN1_TFLG_OPTIONAL) {
208 asn1_template_clear(pval, tt);
209 return 1;
210 }
211 /* If ANY DEFINED BY nothing to do */
212
213 if (tt->flags & ASN1_TFLG_ADB_MASK) {
214 *pval = NULL;
215 return 1;
216 }
c2e27310 217#ifndef OPENSSL_NO_CRYPTO_MDEBUG
4fae386c
RS
218 OPENSSL_mem_debug_push(tt->field_name
219 ? tt->field_name : "asn1_template_new");
a43cf9fa 220#endif
0f113f3e
MC
221 /* If SET OF or SEQUENCE OF, its a STACK */
222 if (tt->flags & ASN1_TFLG_SK_MASK) {
223 STACK_OF(ASN1_VALUE) *skval;
224 skval = sk_ASN1_VALUE_new_null();
225 if (!skval) {
226 ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
227 ret = 0;
228 goto done;
229 }
230 *pval = (ASN1_VALUE *)skval;
231 ret = 1;
232 goto done;
233 }
234 /* Otherwise pass it back to the item routine */
de17bd5d 235 ret = asn1_item_embed_new(pval, it, embed);
0f113f3e 236 done:
c2e27310 237#ifndef OPENSSL_NO_CRYPTO_MDEBUG
4fae386c 238 OPENSSL_mem_debug_pop();
a43cf9fa 239#endif
0f113f3e
MC
240 return ret;
241}
9d6b1ce6 242
866eedb9 243static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
0f113f3e
MC
244{
245 /* If ADB or STACK just NULL the field */
246 if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK))
247 *pval = NULL;
248 else
249 asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
250}
251
252/*
253 * NB: could probably combine most of the real XXX_new() behaviour and junk
f3f52d7f 254 * all the old functions.
9d6b1ce6
DSH
255 */
256
47c9a1b5
DSH
257static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
258 int embed)
0f113f3e
MC
259{
260 ASN1_TYPE *typ;
261 ASN1_STRING *str;
262 int utype;
263
9e488fd6
MC
264 if (!it)
265 return 0;
266
267 if (it->funcs) {
0f113f3e
MC
268 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
269 if (pf->prim_new)
270 return pf->prim_new(pval, it);
271 }
272
9e488fd6 273 if (it->itype == ASN1_ITYPE_MSTRING)
0f113f3e
MC
274 utype = -1;
275 else
276 utype = it->utype;
277 switch (utype) {
278 case V_ASN1_OBJECT:
279 *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
280 return 1;
281
282 case V_ASN1_BOOLEAN:
283 *(ASN1_BOOLEAN *)pval = it->size;
284 return 1;
285
286 case V_ASN1_NULL:
287 *pval = (ASN1_VALUE *)1;
288 return 1;
289
290 case V_ASN1_ANY:
b4faea50 291 typ = OPENSSL_malloc(sizeof(*typ));
90945fa3 292 if (typ == NULL)
0f113f3e
MC
293 return 0;
294 typ->value.ptr = NULL;
295 typ->type = -1;
296 *pval = (ASN1_VALUE *)typ;
297 break;
298
299 default:
47c9a1b5
DSH
300 if (embed) {
301 str = *(ASN1_STRING **)pval;
302 memset(str, 0, sizeof(*str));
7f3e6f8c 303 str->type = utype;
47c9a1b5
DSH
304 str->flags = ASN1_STRING_FLAG_EMBED;
305 } else {
306 str = ASN1_STRING_type_new(utype);
307 *pval = (ASN1_VALUE *)str;
308 }
0f113f3e
MC
309 if (it->itype == ASN1_ITYPE_MSTRING && str)
310 str->flags |= ASN1_STRING_FLAG_MSTRING;
0f113f3e
MC
311 break;
312 }
313 if (*pval)
314 return 1;
315 return 0;
316}
9d6b1ce6 317
b3e72fc3 318static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
0f113f3e
MC
319{
320 int utype;
321 if (it && it->funcs) {
322 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
323 if (pf->prim_clear)
324 pf->prim_clear(pval, it);
325 else
326 *pval = NULL;
327 return;
328 }
329 if (!it || (it->itype == ASN1_ITYPE_MSTRING))
330 utype = -1;
331 else
332 utype = it->utype;
333 if (utype == V_ASN1_BOOLEAN)
334 *(ASN1_BOOLEAN *)pval = it->size;
335 else
336 *pval = NULL;
337}