/src/openssl30/crypto/asn1/tasn_new.c
Line | Count | Source (jump to first uncovered line) |
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 | 1.47M | { |
31 | 1.47M | ASN1_VALUE *ret = NULL; |
32 | 1.47M | if (ASN1_item_ex_new(&ret, it) > 0) |
33 | 1.47M | return ret; |
34 | 0 | return NULL; |
35 | 1.47M | } |
36 | | |
37 | | ASN1_VALUE *ASN1_item_new_ex(const ASN1_ITEM *it, OSSL_LIB_CTX *libctx, |
38 | | const char *propq) |
39 | 0 | { |
40 | 0 | ASN1_VALUE *ret = NULL; |
41 | 0 | if (asn1_item_embed_new(&ret, it, 0, libctx, propq) > 0) |
42 | 0 | return ret; |
43 | 0 | return NULL; |
44 | 0 | } |
45 | | |
46 | | /* Allocate an ASN1 structure */ |
47 | | |
48 | | |
49 | | int ossl_asn1_item_ex_new_intern(ASN1_VALUE **pval, const ASN1_ITEM *it, |
50 | | OSSL_LIB_CTX *libctx, const char *propq) |
51 | 704k | { |
52 | 704k | return asn1_item_embed_new(pval, it, 0, libctx, propq); |
53 | 704k | } |
54 | | |
55 | | int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
56 | 1.47M | { |
57 | 1.47M | return asn1_item_embed_new(pval, it, 0, NULL, NULL); |
58 | 1.47M | } |
59 | | |
60 | | int asn1_item_embed_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int embed, |
61 | | OSSL_LIB_CTX *libctx, const char *propq) |
62 | 4.94M | { |
63 | 4.94M | const ASN1_TEMPLATE *tt = NULL; |
64 | 4.94M | const ASN1_EXTERN_FUNCS *ef; |
65 | 4.94M | const ASN1_AUX *aux = it->funcs; |
66 | 4.94M | ASN1_aux_cb *asn1_cb; |
67 | 4.94M | ASN1_VALUE **pseqval; |
68 | 4.94M | int i; |
69 | 4.94M | if (aux && aux->asn1_cb) |
70 | 178k | asn1_cb = aux->asn1_cb; |
71 | 4.77M | else |
72 | 4.77M | asn1_cb = 0; |
73 | | |
74 | 4.94M | switch (it->itype) { |
75 | | |
76 | 196k | case ASN1_ITYPE_EXTERN: |
77 | 196k | ef = it->funcs; |
78 | 196k | if (ef != NULL) { |
79 | 196k | if (ef->asn1_ex_new_ex != NULL) { |
80 | 57.9k | if (!ef->asn1_ex_new_ex(pval, it, libctx, propq)) |
81 | 0 | goto memerr; |
82 | 139k | } else if (ef->asn1_ex_new != NULL) { |
83 | 139k | if (!ef->asn1_ex_new(pval, it)) |
84 | 0 | goto memerr; |
85 | 139k | } |
86 | 196k | } |
87 | 196k | break; |
88 | | |
89 | 2.30M | case ASN1_ITYPE_PRIMITIVE: |
90 | 2.30M | if (it->templates) { |
91 | 0 | if (!asn1_template_new(pval, it->templates, libctx, propq)) |
92 | 0 | goto memerr; |
93 | 2.30M | } else if (!asn1_primitive_new(pval, it, embed)) |
94 | 0 | goto memerr; |
95 | 2.30M | break; |
96 | | |
97 | 2.30M | case ASN1_ITYPE_MSTRING: |
98 | 864k | if (!asn1_primitive_new(pval, it, embed)) |
99 | 0 | goto memerr; |
100 | 864k | break; |
101 | | |
102 | 864k | case ASN1_ITYPE_CHOICE: |
103 | 171k | if (asn1_cb) { |
104 | 14.7k | i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); |
105 | 14.7k | if (!i) |
106 | 0 | goto auxerr; |
107 | 14.7k | if (i == 2) { |
108 | 0 | return 1; |
109 | 0 | } |
110 | 14.7k | } |
111 | 171k | if (embed) { |
112 | 0 | memset(*pval, 0, it->size); |
113 | 171k | } else { |
114 | 171k | *pval = OPENSSL_zalloc(it->size); |
115 | 171k | if (*pval == NULL) |
116 | 0 | goto memerr; |
117 | 171k | } |
118 | 171k | ossl_asn1_set_choice_selector(pval, -1, it); |
119 | 171k | if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) |
120 | 0 | goto auxerr2; |
121 | 171k | break; |
122 | | |
123 | 171k | case ASN1_ITYPE_NDEF_SEQUENCE: |
124 | 1.40M | case ASN1_ITYPE_SEQUENCE: |
125 | 1.40M | if (asn1_cb) { |
126 | 122k | i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); |
127 | 122k | if (!i) |
128 | 0 | goto auxerr; |
129 | 122k | if (i == 2) { |
130 | 8.26k | return 1; |
131 | 8.26k | } |
132 | 122k | } |
133 | 1.40M | if (embed) { |
134 | 295k | memset(*pval, 0, it->size); |
135 | 1.10M | } else { |
136 | 1.10M | *pval = OPENSSL_zalloc(it->size); |
137 | 1.10M | if (*pval == NULL) |
138 | 0 | goto memerr; |
139 | 1.10M | } |
140 | | /* 0 : init. lock */ |
141 | 1.40M | if (ossl_asn1_do_lock(pval, 0, it) < 0) { |
142 | 0 | if (!embed) { |
143 | 0 | OPENSSL_free(*pval); |
144 | 0 | *pval = NULL; |
145 | 0 | } |
146 | 0 | goto memerr; |
147 | 0 | } |
148 | 1.40M | ossl_asn1_enc_init(pval, it); |
149 | 5.05M | for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { |
150 | 3.64M | pseqval = ossl_asn1_get_field_ptr(pval, tt); |
151 | 3.64M | if (!asn1_template_new(pseqval, tt, libctx, propq)) |
152 | 0 | goto memerr2; |
153 | 3.64M | } |
154 | 1.40M | if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) |
155 | 0 | goto auxerr2; |
156 | 1.40M | break; |
157 | 4.94M | } |
158 | 4.94M | return 1; |
159 | | |
160 | 0 | memerr2: |
161 | 0 | ossl_asn1_item_embed_free(pval, it, embed); |
162 | 0 | memerr: |
163 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
164 | 0 | return 0; |
165 | | |
166 | 0 | auxerr2: |
167 | 0 | ossl_asn1_item_embed_free(pval, it, embed); |
168 | 0 | auxerr: |
169 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR); |
170 | 0 | return 0; |
171 | |
|
172 | 0 | } |
173 | | |
174 | | static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) |
175 | 712k | { |
176 | 712k | const ASN1_EXTERN_FUNCS *ef; |
177 | | |
178 | 712k | switch (it->itype) { |
179 | | |
180 | 0 | case ASN1_ITYPE_EXTERN: |
181 | 0 | ef = it->funcs; |
182 | 0 | if (ef && ef->asn1_ex_clear) |
183 | 0 | ef->asn1_ex_clear(pval, it); |
184 | 0 | else |
185 | 0 | *pval = NULL; |
186 | 0 | break; |
187 | | |
188 | 662k | case ASN1_ITYPE_PRIMITIVE: |
189 | 662k | if (it->templates) |
190 | 0 | asn1_template_clear(pval, it->templates); |
191 | 662k | else |
192 | 662k | asn1_primitive_clear(pval, it); |
193 | 662k | break; |
194 | | |
195 | 22.3k | case ASN1_ITYPE_MSTRING: |
196 | 22.3k | asn1_primitive_clear(pval, it); |
197 | 22.3k | break; |
198 | | |
199 | 10.7k | case ASN1_ITYPE_CHOICE: |
200 | 27.7k | case ASN1_ITYPE_SEQUENCE: |
201 | 27.7k | case ASN1_ITYPE_NDEF_SEQUENCE: |
202 | 27.7k | *pval = NULL; |
203 | 27.7k | break; |
204 | 712k | } |
205 | 712k | } |
206 | | |
207 | | static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, |
208 | | OSSL_LIB_CTX *libctx, const char *propq) |
209 | 3.64M | { |
210 | 3.64M | const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item); |
211 | 3.64M | int embed = tt->flags & ASN1_TFLG_EMBED; |
212 | 3.64M | ASN1_VALUE *tval; |
213 | 3.64M | int ret; |
214 | 3.64M | if (embed) { |
215 | 485k | tval = (ASN1_VALUE *)pval; |
216 | 485k | pval = &tval; |
217 | 485k | } |
218 | 3.64M | if (tt->flags & ASN1_TFLG_OPTIONAL) { |
219 | 848k | asn1_template_clear(pval, tt); |
220 | 848k | return 1; |
221 | 848k | } |
222 | | /* If ANY DEFINED BY nothing to do */ |
223 | | |
224 | 2.80M | if (tt->flags & ASN1_TFLG_ADB_MASK) { |
225 | 16.1k | *pval = NULL; |
226 | 16.1k | return 1; |
227 | 16.1k | } |
228 | | /* If SET OF or SEQUENCE OF, its a STACK */ |
229 | 2.78M | if (tt->flags & ASN1_TFLG_SK_MASK) { |
230 | 11.4k | STACK_OF(ASN1_VALUE) *skval; |
231 | 11.4k | skval = sk_ASN1_VALUE_new_null(); |
232 | 11.4k | if (!skval) { |
233 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
234 | 0 | ret = 0; |
235 | 0 | goto done; |
236 | 0 | } |
237 | 11.4k | *pval = (ASN1_VALUE *)skval; |
238 | 11.4k | ret = 1; |
239 | 11.4k | goto done; |
240 | 11.4k | } |
241 | | /* Otherwise pass it back to the item routine */ |
242 | 2.77M | ret = asn1_item_embed_new(pval, it, embed, libctx, propq); |
243 | 2.78M | done: |
244 | 2.78M | return ret; |
245 | 2.77M | } |
246 | | |
247 | | static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) |
248 | 848k | { |
249 | | /* If ADB or STACK just NULL the field */ |
250 | 848k | if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK)) |
251 | 136k | *pval = NULL; |
252 | 712k | else |
253 | 712k | asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item)); |
254 | 848k | } |
255 | | |
256 | | /* |
257 | | * NB: could probably combine most of the real XXX_new() behaviour and junk |
258 | | * all the old functions. |
259 | | */ |
260 | | |
261 | | static int asn1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it, |
262 | | int embed) |
263 | 3.17M | { |
264 | 3.17M | ASN1_TYPE *typ; |
265 | 3.17M | ASN1_STRING *str; |
266 | 3.17M | int utype; |
267 | | |
268 | 3.17M | if (!it) |
269 | 0 | return 0; |
270 | | |
271 | 3.17M | if (it->funcs) { |
272 | 41.1k | const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; |
273 | 41.1k | if (embed) { |
274 | 22.1k | if (pf->prim_clear) { |
275 | 22.1k | pf->prim_clear(pval, it); |
276 | 22.1k | return 1; |
277 | 22.1k | } |
278 | 22.1k | } else if (pf->prim_new) { |
279 | 19.0k | return pf->prim_new(pval, it); |
280 | 19.0k | } |
281 | 41.1k | } |
282 | | |
283 | 3.13M | if (it->itype == ASN1_ITYPE_MSTRING) |
284 | 864k | utype = -1; |
285 | 2.26M | else |
286 | 2.26M | utype = it->utype; |
287 | 3.13M | switch (utype) { |
288 | 1.11M | case V_ASN1_OBJECT: |
289 | 1.11M | *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef); |
290 | 1.11M | return 1; |
291 | | |
292 | 0 | case V_ASN1_BOOLEAN: |
293 | 0 | *(ASN1_BOOLEAN *)pval = it->size; |
294 | 0 | return 1; |
295 | | |
296 | 0 | case V_ASN1_NULL: |
297 | 0 | *pval = (ASN1_VALUE *)1; |
298 | 0 | return 1; |
299 | | |
300 | 937k | case V_ASN1_ANY: |
301 | 937k | if ((typ = OPENSSL_malloc(sizeof(*typ))) == NULL) { |
302 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
303 | 0 | return 0; |
304 | 0 | } |
305 | 937k | typ->value.ptr = NULL; |
306 | 937k | typ->type = -1; |
307 | 937k | *pval = (ASN1_VALUE *)typ; |
308 | 937k | break; |
309 | | |
310 | 1.07M | default: |
311 | 1.07M | if (embed) { |
312 | 168k | str = *(ASN1_STRING **)pval; |
313 | 168k | memset(str, 0, sizeof(*str)); |
314 | 168k | str->type = utype; |
315 | 168k | str->flags = ASN1_STRING_FLAG_EMBED; |
316 | 904k | } else { |
317 | 904k | str = ASN1_STRING_type_new(utype); |
318 | 904k | *pval = (ASN1_VALUE *)str; |
319 | 904k | } |
320 | 1.07M | if (it->itype == ASN1_ITYPE_MSTRING && str) |
321 | 864k | str->flags |= ASN1_STRING_FLAG_MSTRING; |
322 | 1.07M | break; |
323 | 3.13M | } |
324 | 2.01M | if (*pval) |
325 | 2.01M | return 1; |
326 | 0 | return 0; |
327 | 2.01M | } |
328 | | |
329 | | static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) |
330 | 684k | { |
331 | 684k | int utype; |
332 | 684k | if (it && it->funcs) { |
333 | 6.33k | const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; |
334 | 6.33k | if (pf->prim_clear) |
335 | 0 | pf->prim_clear(pval, it); |
336 | 6.33k | else |
337 | 6.33k | *pval = NULL; |
338 | 6.33k | return; |
339 | 6.33k | } |
340 | 678k | if (!it || (it->itype == ASN1_ITYPE_MSTRING)) |
341 | 22.3k | utype = -1; |
342 | 656k | else |
343 | 656k | utype = it->utype; |
344 | 678k | if (utype == V_ASN1_BOOLEAN) |
345 | 69.3k | *(ASN1_BOOLEAN *)pval = it->size; |
346 | 609k | else |
347 | 609k | *pval = NULL; |
348 | 678k | } |