]> git.proxmox.com Git - efi-boot-shim.git/blame - Cryptlib/OpenSSL/crypto/asn1/tasn_new.c
New upstream version 15+1533136590.3beb971
[efi-boot-shim.git] / Cryptlib / OpenSSL / crypto / asn1 / tasn_new.c
CommitLineData
d3819813 1/*
7bf7a6d0 2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
3e575651 3 *
7bf7a6d0
MTL
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
3e575651
SL
8 */
9
3e575651
SL
10#include <stddef.h>
11#include <openssl/asn1.h>
12#include <openssl/objects.h>
13#include <openssl/err.h>
14#include <openssl/asn1t.h>
15#include <string.h>
7bf7a6d0 16#include "asn1_locl.h"
3e575651 17
7bf7a6d0
MTL
18static int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
19 int embed);
20static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
21 int embed);
3e575651 22static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
7bf7a6d0 23static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
3e575651 24static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
d3819813 25static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
3e575651
SL
26
27ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
d3819813
MTL
28{
29 ASN1_VALUE *ret = NULL;
30 if (ASN1_item_ex_new(&ret, it) > 0)
31 return ret;
32 return NULL;
33}
3e575651
SL
34
35/* Allocate an ASN1 structure */
36
37int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
d3819813 38{
7bf7a6d0 39 return asn1_item_embed_new(pval, it, 0);
d3819813 40}
3e575651 41
7bf7a6d0 42int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed)
d3819813
MTL
43{
44 const ASN1_TEMPLATE *tt = NULL;
d3819813
MTL
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;
3e575651 54
7bf7a6d0
MTL
55#ifndef OPENSSL_NO_CRYPTO_MDEBUG
56 OPENSSL_mem_debug_push(it->sname ? it->sname : "asn1_item_embed_new");
3e575651
SL
57#endif
58
d3819813
MTL
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
d3819813
MTL
69 case ASN1_ITYPE_PRIMITIVE:
70 if (it->templates) {
7bf7a6d0 71 if (!asn1_template_new(pval, it->templates))
d3819813 72 goto memerr;
7bf7a6d0 73 } else if (!asn1_primitive_new(pval, it, embed))
d3819813
MTL
74 goto memerr;
75 break;
76
77 case ASN1_ITYPE_MSTRING:
7bf7a6d0 78 if (!asn1_primitive_new(pval, it, embed))
d3819813
MTL
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) {
7bf7a6d0
MTL
88#ifndef OPENSSL_NO_CRYPTO_MDEBUG
89 OPENSSL_mem_debug_pop();
3e575651 90#endif
d3819813
MTL
91 return 1;
92 }
93 }
7bf7a6d0 94 if (embed) {
b6f94dbe 95 memset(*pval, 0, it->size);
7bf7a6d0
MTL
96 } else {
97 *pval = OPENSSL_zalloc(it->size);
98 if (*pval == NULL)
99 goto memerr;
d3819813
MTL
100 }
101 asn1_set_choice_selector(pval, -1, it);
102 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
7bf7a6d0 103 goto auxerr2;
d3819813
MTL
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) {
7bf7a6d0
MTL
113#ifndef OPENSSL_NO_CRYPTO_MDEBUG
114 OPENSSL_mem_debug_pop();
3e575651 115#endif
d3819813
MTL
116 return 1;
117 }
118 }
7bf7a6d0 119 if (embed) {
b6f94dbe 120 memset(*pval, 0, it->size);
7bf7a6d0
MTL
121 } else {
122 *pval = OPENSSL_zalloc(it->size);
123 if (*pval == NULL)
124 goto memerr;
d3819813 125 }
7bf7a6d0
MTL
126 /* 0 : init. lock */
127 if (asn1_do_lock(pval, 0, it) < 0)
128 goto memerr2;
129 asn1_enc_init(pval, it);
d3819813
MTL
130 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
131 pseqval = asn1_get_field_ptr(pval, tt);
7bf7a6d0
MTL
132 if (!asn1_template_new(pseqval, tt))
133 goto memerr2;
d3819813
MTL
134 }
135 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
7bf7a6d0 136 goto auxerr2;
d3819813
MTL
137 break;
138 }
7bf7a6d0
MTL
139#ifndef OPENSSL_NO_CRYPTO_MDEBUG
140 OPENSSL_mem_debug_pop();
3e575651 141#endif
d3819813 142 return 1;
3e575651 143
7bf7a6d0
MTL
144 memerr2:
145 ASN1_item_ex_free(pval, it);
d3819813 146 memerr:
7bf7a6d0
MTL
147 ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ERR_R_MALLOC_FAILURE);
148#ifndef OPENSSL_NO_CRYPTO_MDEBUG
149 OPENSSL_mem_debug_pop();
3e575651 150#endif
d3819813 151 return 0;
3e575651 152
7bf7a6d0 153 auxerr2:
b6f94dbe 154 ASN1_item_ex_free(pval, it);
7bf7a6d0
MTL
155 auxerr:
156 ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ASN1_R_AUX_ERROR);
157#ifndef OPENSSL_NO_CRYPTO_MDEBUG
158 OPENSSL_mem_debug_pop();
3e575651 159#endif
d3819813 160 return 0;
3e575651 161
d3819813 162}
3e575651
SL
163
164static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
d3819813
MTL
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
d3819813
MTL
189 case ASN1_ITYPE_CHOICE:
190 case ASN1_ITYPE_SEQUENCE:
191 case ASN1_ITYPE_NDEF_SEQUENCE:
192 *pval = NULL;
193 break;
194 }
195}
3e575651 196
7bf7a6d0 197static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
d3819813
MTL
198{
199 const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
7bf7a6d0
MTL
200 int embed = tt->flags & ASN1_TFLG_EMBED;
201 ASN1_VALUE *tval;
d3819813 202 int ret;
7bf7a6d0
MTL
203 if (embed) {
204 tval = (ASN1_VALUE *)pval;
205 pval = &tval;
206 }
d3819813
MTL
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 }
7bf7a6d0
MTL
217#ifndef OPENSSL_NO_CRYPTO_MDEBUG
218 OPENSSL_mem_debug_push(tt->field_name
219 ? tt->field_name : "asn1_template_new");
3e575651 220#endif
d3819813
MTL
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 */
7bf7a6d0 235 ret = asn1_item_embed_new(pval, it, embed);
d3819813 236 done:
7bf7a6d0
MTL
237#ifndef OPENSSL_NO_CRYPTO_MDEBUG
238 OPENSSL_mem_debug_pop();
3e575651 239#endif
d3819813
MTL
240 return ret;
241}
3e575651
SL
242
243static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
d3819813
MTL
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
3e575651
SL
254 * all the old functions.
255 */
256
7bf7a6d0
MTL
257static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
258 int embed)
d3819813
MTL
259{
260 ASN1_TYPE *typ;
261 ASN1_STRING *str;
262 int utype;
263
264 if (!it)
265 return 0;
266
267 if (it->funcs) {
268 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
269 if (pf->prim_new)
270 return pf->prim_new(pval, it);
271 }
272
273 if (it->itype == ASN1_ITYPE_MSTRING)
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:
7bf7a6d0
MTL
291 typ = OPENSSL_malloc(sizeof(*typ));
292 if (typ == NULL)
d3819813
MTL
293 return 0;
294 typ->value.ptr = NULL;
295 typ->type = -1;
296 *pval = (ASN1_VALUE *)typ;
297 break;
298
299 default:
7bf7a6d0
MTL
300 if (embed) {
301 str = *(ASN1_STRING **)pval;
302 memset(str, 0, sizeof(*str));
303 str->type = utype;
304 str->flags = ASN1_STRING_FLAG_EMBED;
305 } else {
306 str = ASN1_STRING_type_new(utype);
307 *pval = (ASN1_VALUE *)str;
308 }
d3819813
MTL
309 if (it->itype == ASN1_ITYPE_MSTRING && str)
310 str->flags |= ASN1_STRING_FLAG_MSTRING;
d3819813
MTL
311 break;
312 }
313 if (*pval)
314 return 1;
315 return 0;
316}
317
318static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
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}