/src/openssl30/crypto/asn1/tasn_new.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (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_local.h" |
17 | | |
18 | | static int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, |
19 | | int embed, OSSL_LIB_CTX *libctx, |
20 | | const char *propq); |
21 | | static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it, |
22 | | int embed); |
23 | | static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it); |
24 | | static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, |
25 | | OSSL_LIB_CTX *libctx, const char *propq); |
26 | | static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt); |
27 | | static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it); |
28 | | |
29 | | ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it) |
30 | 67.4M | { |
31 | 67.4M | ASN1_VALUE *ret = NULL; |
32 | 67.4M | if (ASN1_item_ex_new(&ret, it) > 0) |
33 | 67.4M | return ret; |
34 | 0 | return NULL; |
35 | 67.4M | } |
36 | | |
37 | | ASN1_VALUE *ASN1_item_new_ex(const ASN1_ITEM *it, OSSL_LIB_CTX *libctx, |
38 | | const char *propq) |
39 | 105k | { |
40 | 105k | ASN1_VALUE *ret = NULL; |
41 | 105k | if (asn1_item_embed_new(&ret, it, 0, libctx, propq) > 0) |
42 | 105k | return ret; |
43 | 0 | return NULL; |
44 | 105k | } |
45 | | |
46 | | /* Allocate an ASN1 structure */ |
47 | | |
48 | | int ossl_asn1_item_ex_new_intern(ASN1_VALUE **pval, const ASN1_ITEM *it, |
49 | | OSSL_LIB_CTX *libctx, const char *propq) |
50 | 42.6M | { |
51 | 42.6M | return asn1_item_embed_new(pval, it, 0, libctx, propq); |
52 | 42.6M | } |
53 | | |
54 | | int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
55 | 67.4M | { |
56 | 67.4M | return asn1_item_embed_new(pval, it, 0, NULL, NULL); |
57 | 67.4M | } |
58 | | |
59 | | int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed, |
60 | | OSSL_LIB_CTX *libctx, const char *propq) |
61 | 31.2M | { |
62 | 31.2M | const ASN1_TEMPLATE *tt = NULL; |
63 | 31.2M | const ASN1_EXTERN_FUNCS *ef; |
64 | 31.2M | const ASN1_AUX *aux = it->funcs; |
65 | 31.2M | ASN1_aux_cb *asn1_cb; |
66 | 31.2M | ASN1_VALUE **pseqval; |
67 | 31.2M | int i; |
68 | 31.2M | if (aux && aux->asn1_cb) |
69 | 1.42M | asn1_cb = aux->asn1_cb; |
70 | 29.8M | else |
71 | 29.8M | asn1_cb = 0; |
72 | | |
73 | 31.2M | switch (it->itype) { |
74 | | |
75 | 744k | case ASN1_ITYPE_EXTERN: |
76 | 744k | ef = it->funcs; |
77 | 744k | if (ef != NULL) { |
78 | 744k | if (ef->asn1_ex_new_ex != NULL) { |
79 | 223k | if (!ef->asn1_ex_new_ex(pval, it, libctx, propq)) |
80 | 0 | goto memerr; |
81 | 521k | } else if (ef->asn1_ex_new != NULL) { |
82 | 521k | if (!ef->asn1_ex_new(pval, it)) |
83 | 0 | goto memerr; |
84 | 521k | } |
85 | 744k | } |
86 | 744k | break; |
87 | | |
88 | 12.6M | case ASN1_ITYPE_PRIMITIVE: |
89 | 12.6M | if (it->templates) { |
90 | 23.6k | if (!asn1_template_new(pval, it->templates, libctx, propq)) |
91 | 0 | goto memerr; |
92 | 12.5M | } else if (!asn1_primitive_new(pval, it, embed)) |
93 | 0 | goto memerr; |
94 | 12.6M | break; |
95 | | |
96 | 12.6M | case ASN1_ITYPE_MSTRING: |
97 | 6.02M | if (!asn1_primitive_new(pval, it, embed)) |
98 | 0 | goto memerr; |
99 | 6.02M | break; |
100 | | |
101 | 6.02M | case ASN1_ITYPE_CHOICE: |
102 | 1.06M | if (asn1_cb) { |
103 | 71.7k | i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); |
104 | 71.7k | if (!i) |
105 | 0 | goto auxerr; |
106 | 71.7k | if (i == 2) { |
107 | 0 | return 1; |
108 | 0 | } |
109 | 71.7k | } |
110 | 1.06M | if (embed) { |
111 | 21.7k | memset(*pval, 0, it->size); |
112 | 1.04M | } else { |
113 | 1.04M | *pval = OPENSSL_zalloc(it->size); |
114 | 1.04M | if (*pval == NULL) |
115 | 0 | goto memerr; |
116 | 1.04M | } |
117 | 1.06M | ossl_asn1_set_choice_selector(pval, -1, it); |
118 | 1.06M | if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) |
119 | 0 | goto auxerr2; |
120 | 1.06M | break; |
121 | | |
122 | 1.06M | case ASN1_ITYPE_NDEF_SEQUENCE: |
123 | 10.8M | case ASN1_ITYPE_SEQUENCE: |
124 | 10.8M | if (asn1_cb) { |
125 | 972k | i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); |
126 | 972k | if (!i) |
127 | 0 | goto auxerr; |
128 | 972k | if (i == 2) { |
129 | 225k | return 1; |
130 | 225k | } |
131 | 972k | } |
132 | 10.6M | if (embed) { |
133 | 993k | memset(*pval, 0, it->size); |
134 | 9.61M | } else { |
135 | 9.61M | *pval = OPENSSL_zalloc(it->size); |
136 | 9.61M | if (*pval == NULL) |
137 | 0 | goto memerr; |
138 | 9.61M | } |
139 | | /* 0 : init. lock */ |
140 | 10.6M | if (ossl_asn1_do_lock(pval, 0, it) < 0) { |
141 | 0 | if (!embed) { |
142 | 0 | OPENSSL_free(*pval); |
143 | 0 | *pval = NULL; |
144 | 0 | } |
145 | 0 | goto memerr; |
146 | 0 | } |
147 | 10.6M | ossl_asn1_enc_init(pval, it); |
148 | 38.0M | for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { |
149 | 27.4M | pseqval = ossl_asn1_get_field_ptr(pval, tt); |
150 | 27.4M | if (!asn1_template_new(pseqval, tt, libctx, propq)) |
151 | 0 | goto memerr2; |
152 | 27.4M | } |
153 | 10.6M | if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) |
154 | 0 | goto auxerr2; |
155 | 10.6M | break; |
156 | 31.2M | } |
157 | 31.0M | return 1; |
158 | | |
159 | 0 | memerr2: |
160 | 0 | ossl_asn1_item_embed_free(pval, it, embed); |
161 | 0 | memerr: |
162 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
163 | 0 | return 0; |
164 | | |
165 | 0 | auxerr2: |
166 | 0 | ossl_asn1_item_embed_free(pval, it, embed); |
167 | 0 | auxerr: |
168 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR); |
169 | 0 | return 0; |
170 | 0 | } |
171 | | |
172 | | static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) |
173 | 38.9M | { |
174 | 38.9M | const ASN1_EXTERN_FUNCS *ef; |
175 | | |
176 | 38.9M | switch (it->itype) { |
177 | | |
178 | 1.92M | case ASN1_ITYPE_EXTERN: |
179 | 1.92M | ef = it->funcs; |
180 | 1.92M | if (ef && ef->asn1_ex_clear) |
181 | 0 | ef->asn1_ex_clear(pval, it); |
182 | 1.92M | else |
183 | 1.92M | *pval = NULL; |
184 | 1.92M | break; |
185 | | |
186 | 31.8M | case ASN1_ITYPE_PRIMITIVE: |
187 | 31.8M | if (it->templates) |
188 | 633k | asn1_template_clear(pval, it->templates); |
189 | 31.2M | else |
190 | 31.2M | asn1_primitive_clear(pval, it); |
191 | 31.8M | break; |
192 | | |
193 | 538k | case ASN1_ITYPE_MSTRING: |
194 | 538k | asn1_primitive_clear(pval, it); |
195 | 538k | break; |
196 | | |
197 | 1.74M | case ASN1_ITYPE_CHOICE: |
198 | 4.52M | case ASN1_ITYPE_SEQUENCE: |
199 | 4.58M | case ASN1_ITYPE_NDEF_SEQUENCE: |
200 | 4.58M | *pval = NULL; |
201 | 4.58M | break; |
202 | 38.9M | } |
203 | 38.9M | } |
204 | | |
205 | | static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, |
206 | | OSSL_LIB_CTX *libctx, const char *propq) |
207 | 178M | { |
208 | 178M | const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item); |
209 | 178M | int embed = tt->flags & ASN1_TFLG_EMBED; |
210 | 178M | ASN1_VALUE *tval; |
211 | 178M | int ret; |
212 | 178M | if (embed) { |
213 | 14.7M | tval = (ASN1_VALUE *)pval; |
214 | 14.7M | pval = &tval; |
215 | 14.7M | } |
216 | 178M | if (tt->flags & ASN1_TFLG_OPTIONAL) { |
217 | 47.4M | asn1_template_clear(pval, tt); |
218 | 47.4M | return 1; |
219 | 47.4M | } |
220 | | /* If ANY DEFINED BY nothing to do */ |
221 | | |
222 | 131M | if (tt->flags & ASN1_TFLG_ADB_MASK) { |
223 | 1.21M | *pval = NULL; |
224 | 1.21M | return 1; |
225 | 1.21M | } |
226 | | /* If SET OF or SEQUENCE OF, its a STACK */ |
227 | 129M | if (tt->flags & ASN1_TFLG_SK_MASK) { |
228 | 1.71M | STACK_OF(ASN1_VALUE) *skval; |
229 | 1.71M | skval = sk_ASN1_VALUE_new_null(); |
230 | 1.71M | if (!skval) { |
231 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
232 | 0 | ret = 0; |
233 | 0 | goto done; |
234 | 0 | } |
235 | 1.71M | *pval = (ASN1_VALUE *)skval; |
236 | 1.71M | ret = 1; |
237 | 1.71M | goto done; |
238 | 1.71M | } |
239 | | /* Otherwise pass it back to the item routine */ |
240 | 128M | ret = asn1_item_embed_new(pval, it, embed, libctx, propq); |
241 | 129M | done: |
242 | 129M | return ret; |
243 | 128M | } |
244 | | |
245 | | static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) |
246 | 48.1M | { |
247 | | /* If ADB or STACK just NULL the field */ |
248 | 48.1M | if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK)) |
249 | 9.18M | *pval = NULL; |
250 | 38.9M | else |
251 | 38.9M | asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item)); |
252 | 48.1M | } |
253 | | |
254 | | /* |
255 | | * NB: could probably combine most of the real XXX_new() behaviour and junk |
256 | | * all the old functions. |
257 | | */ |
258 | | |
259 | | static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it, |
260 | | int embed) |
261 | 156M | { |
262 | 156M | ASN1_TYPE *typ; |
263 | 156M | ASN1_STRING *str; |
264 | 156M | int utype; |
265 | | |
266 | 156M | if (!it) |
267 | 0 | return 0; |
268 | | |
269 | 156M | if (it->funcs) { |
270 | 2.23M | const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; |
271 | 2.23M | if (embed) { |
272 | 952k | if (pf->prim_clear) { |
273 | 952k | pf->prim_clear(pval, it); |
274 | 952k | return 1; |
275 | 952k | } |
276 | 1.28M | } else if (pf->prim_new) { |
277 | 1.28M | return pf->prim_new(pval, it); |
278 | 1.28M | } |
279 | 2.23M | } |
280 | | |
281 | 154M | if (it->itype == ASN1_ITYPE_MSTRING) |
282 | 39.2M | utype = -1; |
283 | 115M | else |
284 | 115M | utype = it->utype; |
285 | 154M | switch (utype) { |
286 | 53.0M | case V_ASN1_OBJECT: |
287 | 53.0M | *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef); |
288 | 53.0M | return 1; |
289 | | |
290 | 0 | case V_ASN1_BOOLEAN: |
291 | 0 | *(ASN1_BOOLEAN *)pval = it->size; |
292 | 0 | return 1; |
293 | | |
294 | 0 | case V_ASN1_NULL: |
295 | 0 | *pval = (ASN1_VALUE *)1; |
296 | 0 | return 1; |
297 | | |
298 | 45.2M | case V_ASN1_ANY: |
299 | 45.2M | if ((typ = OPENSSL_malloc(sizeof(*typ))) == NULL) { |
300 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
301 | 0 | return 0; |
302 | 0 | } |
303 | 45.2M | typ->value.ptr = NULL; |
304 | 45.2M | typ->type = -1; |
305 | 45.2M | *pval = (ASN1_VALUE *)typ; |
306 | 45.2M | break; |
307 | | |
308 | 56.2M | default: |
309 | 56.2M | if (embed) { |
310 | 7.60M | str = *(ASN1_STRING **)pval; |
311 | 7.60M | memset(str, 0, sizeof(*str)); |
312 | 7.60M | str->type = utype; |
313 | 7.60M | str->flags = ASN1_STRING_FLAG_EMBED; |
314 | 48.6M | } else { |
315 | 48.6M | str = ASN1_STRING_type_new(utype); |
316 | 48.6M | *pval = (ASN1_VALUE *)str; |
317 | 48.6M | } |
318 | 56.2M | if (it->itype == ASN1_ITYPE_MSTRING && str) |
319 | 39.2M | str->flags |= ASN1_STRING_FLAG_MSTRING; |
320 | 56.2M | break; |
321 | 154M | } |
322 | 101M | if (*pval) |
323 | 101M | return 1; |
324 | 0 | return 0; |
325 | 101M | } |
326 | | |
327 | | static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) |
328 | 31.7M | { |
329 | 31.7M | int utype; |
330 | 31.7M | if (it && it->funcs) { |
331 | 691k | const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; |
332 | 691k | if (pf->prim_clear) |
333 | 540k | pf->prim_clear(pval, it); |
334 | 151k | else |
335 | 151k | *pval = NULL; |
336 | 691k | return; |
337 | 691k | } |
338 | 31.0M | if (!it || (it->itype == ASN1_ITYPE_MSTRING)) |
339 | 538k | utype = -1; |
340 | 30.5M | else |
341 | 30.5M | utype = it->utype; |
342 | 31.0M | if (utype == V_ASN1_BOOLEAN) |
343 | 6.43M | *(ASN1_BOOLEAN *)pval = it->size; |
344 | 24.6M | else |
345 | 24.6M | *pval = NULL; |
346 | 31.0M | } |