source: EcnlProtoTool/trunk/openssl-1.1.0e/crypto/asn1/tasn_new.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 8.6 KB
Line 
1/*
2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
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>
16#include "asn1_locl.h"
17
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);
22static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
23static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
24static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
25static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
26
27ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
28{
29 ASN1_VALUE *ret = NULL;
30 if (ASN1_item_ex_new(&ret, it) > 0)
31 return ret;
32 return NULL;
33}
34
35/* Allocate an ASN1 structure */
36
37int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
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)
43{
44 const ASN1_TEMPLATE *tt = NULL;
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
55#ifndef OPENSSL_NO_CRYPTO_MDEBUG
56 OPENSSL_mem_debug_push(it->sname ? it->sname : "asn1_item_embed_new");
57#endif
58
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
69 case ASN1_ITYPE_PRIMITIVE:
70 if (it->templates) {
71 if (!asn1_template_new(pval, it->templates))
72 goto memerr;
73 } else if (!asn1_primitive_new(pval, it, embed))
74 goto memerr;
75 break;
76
77 case ASN1_ITYPE_MSTRING:
78 if (!asn1_primitive_new(pval, it, embed))
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) {
88#ifndef OPENSSL_NO_CRYPTO_MDEBUG
89 OPENSSL_mem_debug_pop();
90#endif
91 return 1;
92 }
93 }
94 if (embed) {
95 memset(*pval, 0, it->size);
96 } else {
97 *pval = OPENSSL_zalloc(it->size);
98 if (*pval == NULL)
99 goto memerr;
100 }
101 asn1_set_choice_selector(pval, -1, it);
102 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
103 goto auxerr2;
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) {
113#ifndef OPENSSL_NO_CRYPTO_MDEBUG
114 OPENSSL_mem_debug_pop();
115#endif
116 return 1;
117 }
118 }
119 if (embed) {
120 memset(*pval, 0, it->size);
121 } else {
122 *pval = OPENSSL_zalloc(it->size);
123 if (*pval == NULL)
124 goto memerr;
125 }
126 /* 0 : init. lock */
127 if (asn1_do_lock(pval, 0, it) < 0)
128 goto memerr2;
129 asn1_enc_init(pval, it);
130 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
131 pseqval = asn1_get_field_ptr(pval, tt);
132 if (!asn1_template_new(pseqval, tt))
133 goto memerr2;
134 }
135 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
136 goto auxerr2;
137 break;
138 }
139#ifndef OPENSSL_NO_CRYPTO_MDEBUG
140 OPENSSL_mem_debug_pop();
141#endif
142 return 1;
143
144 memerr2:
145 ASN1_item_ex_free(pval, it);
146 memerr:
147 ASN1err(ASN1_F_ASN1_ITEM_EMBED_NEW, ERR_R_MALLOC_FAILURE);
148#ifndef OPENSSL_NO_CRYPTO_MDEBUG
149 OPENSSL_mem_debug_pop();
150#endif
151 return 0;
152
153 auxerr2:
154 ASN1_item_ex_free(pval, it);
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();
159#endif
160 return 0;
161
162}
163
164static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
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
189 case ASN1_ITYPE_CHOICE:
190 case ASN1_ITYPE_SEQUENCE:
191 case ASN1_ITYPE_NDEF_SEQUENCE:
192 *pval = NULL;
193 break;
194 }
195}
196
197static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
198{
199 const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
200 int embed = tt->flags & ASN1_TFLG_EMBED;
201 ASN1_VALUE *tval;
202 int ret;
203 if (embed) {
204 tval = (ASN1_VALUE *)pval;
205 pval = &tval;
206 }
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 }
217#ifndef OPENSSL_NO_CRYPTO_MDEBUG
218 OPENSSL_mem_debug_push(tt->field_name
219 ? tt->field_name : "asn1_template_new");
220#endif
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 */
235 ret = asn1_item_embed_new(pval, it, embed);
236 done:
237#ifndef OPENSSL_NO_CRYPTO_MDEBUG
238 OPENSSL_mem_debug_pop();
239#endif
240 return ret;
241}
242
243static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
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
254 * all the old functions.
255 */
256
257static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
258 int embed)
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:
291 typ = OPENSSL_malloc(sizeof(*typ));
292 if (typ == NULL)
293 return 0;
294 typ->value.ptr = NULL;
295 typ->type = -1;
296 *pval = (ASN1_VALUE *)typ;
297 break;
298
299 default:
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 }
309 if (it->itype == ASN1_ITYPE_MSTRING && str)
310 str->flags |= ASN1_STRING_FLAG_MSTRING;
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}
Note: See TracBrowser for help on using the repository browser.