/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 | 455k | { |
31 | 455k | ASN1_VALUE *ret = NULL; |
32 | 455k | if (ASN1_item_ex_new(&ret, it) > 0) |
33 | 455k | return ret; |
34 | 0 | return NULL; |
35 | 455k | } |
36 | | |
37 | | ASN1_VALUE *ASN1_item_new_ex(const ASN1_ITEM *it, OSSL_LIB_CTX *libctx, |
38 | | const char *propq) |
39 | 5.62k | { |
40 | 5.62k | ASN1_VALUE *ret = NULL; |
41 | 5.62k | if (asn1_item_embed_new(&ret, it, 0, libctx, propq) > 0) |
42 | 5.62k | return ret; |
43 | 0 | return NULL; |
44 | 5.62k | } |
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 | 293k | { |
52 | 293k | return asn1_item_embed_new(pval, it, 0, libctx, propq); |
53 | 293k | } |
54 | | |
55 | | int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
56 | 455k | { |
57 | 455k | return asn1_item_embed_new(pval, it, 0, NULL, NULL); |
58 | 455k | } |
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 | 1.71M | { |
63 | 1.71M | const ASN1_TEMPLATE *tt = NULL; |
64 | 1.71M | const ASN1_EXTERN_FUNCS *ef; |
65 | 1.71M | const ASN1_AUX *aux = it->funcs; |
66 | 1.71M | ASN1_aux_cb *asn1_cb; |
67 | 1.71M | ASN1_VALUE **pseqval; |
68 | 1.71M | int i; |
69 | 1.71M | if (aux && aux->asn1_cb) |
70 | 62.0k | asn1_cb = aux->asn1_cb; |
71 | 1.65M | else |
72 | 1.65M | asn1_cb = 0; |
73 | | |
74 | 1.71M | switch (it->itype) { |
75 | | |
76 | 38.3k | case ASN1_ITYPE_EXTERN: |
77 | 38.3k | ef = it->funcs; |
78 | 38.3k | if (ef != NULL) { |
79 | 38.3k | if (ef->asn1_ex_new_ex != NULL) { |
80 | 8.41k | if (!ef->asn1_ex_new_ex(pval, it, libctx, propq)) |
81 | 0 | goto memerr; |
82 | 29.8k | } else if (ef->asn1_ex_new != NULL) { |
83 | 29.8k | if (!ef->asn1_ex_new(pval, it)) |
84 | 0 | goto memerr; |
85 | 29.8k | } |
86 | 38.3k | } |
87 | 38.3k | break; |
88 | | |
89 | 703k | case ASN1_ITYPE_PRIMITIVE: |
90 | 703k | if (it->templates) { |
91 | 18.1k | if (!asn1_template_new(pval, it->templates, libctx, propq)) |
92 | 0 | goto memerr; |
93 | 685k | } else if (!asn1_primitive_new(pval, it, embed)) |
94 | 0 | goto memerr; |
95 | 703k | break; |
96 | | |
97 | 703k | case ASN1_ITYPE_MSTRING: |
98 | 167k | if (!asn1_primitive_new(pval, it, embed)) |
99 | 0 | goto memerr; |
100 | 167k | break; |
101 | | |
102 | 167k | case ASN1_ITYPE_CHOICE: |
103 | 125k | if (asn1_cb) { |
104 | 94 | i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); |
105 | 94 | if (!i) |
106 | 0 | goto auxerr; |
107 | 94 | if (i == 2) { |
108 | 0 | return 1; |
109 | 0 | } |
110 | 94 | } |
111 | 125k | if (embed) { |
112 | 0 | memset(*pval, 0, it->size); |
113 | 125k | } else { |
114 | 125k | *pval = OPENSSL_zalloc(it->size); |
115 | 125k | if (*pval == NULL) |
116 | 0 | goto memerr; |
117 | 125k | } |
118 | 125k | ossl_asn1_set_choice_selector(pval, -1, it); |
119 | 125k | if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) |
120 | 0 | goto auxerr2; |
121 | 125k | break; |
122 | | |
123 | 125k | case ASN1_ITYPE_NDEF_SEQUENCE: |
124 | 681k | case ASN1_ITYPE_SEQUENCE: |
125 | 681k | if (asn1_cb) { |
126 | 58.1k | i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); |
127 | 58.1k | if (!i) |
128 | 0 | goto auxerr; |
129 | 58.1k | if (i == 2) { |
130 | 7.72k | return 1; |
131 | 7.72k | } |
132 | 58.1k | } |
133 | 674k | if (embed) { |
134 | 32.1k | memset(*pval, 0, it->size); |
135 | 641k | } else { |
136 | 641k | *pval = OPENSSL_zalloc(it->size); |
137 | 641k | if (*pval == NULL) |
138 | 0 | goto memerr; |
139 | 641k | } |
140 | | /* 0 : init. lock */ |
141 | 674k | 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 | 674k | ossl_asn1_enc_init(pval, it); |
149 | 3.18M | for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { |
150 | 2.51M | pseqval = ossl_asn1_get_field_ptr(pval, tt); |
151 | 2.51M | if (!asn1_template_new(pseqval, tt, libctx, propq)) |
152 | 0 | goto memerr2; |
153 | 2.51M | } |
154 | 674k | if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) |
155 | 0 | goto auxerr2; |
156 | 674k | break; |
157 | 1.71M | } |
158 | 1.70M | 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 | 1.34M | { |
176 | 1.34M | const ASN1_EXTERN_FUNCS *ef; |
177 | | |
178 | 1.34M | switch (it->itype) { |
179 | | |
180 | 220k | case ASN1_ITYPE_EXTERN: |
181 | 220k | ef = it->funcs; |
182 | 220k | if (ef && ef->asn1_ex_clear) |
183 | 0 | ef->asn1_ex_clear(pval, it); |
184 | 220k | else |
185 | 220k | *pval = NULL; |
186 | 220k | break; |
187 | | |
188 | 922k | case ASN1_ITYPE_PRIMITIVE: |
189 | 922k | if (it->templates) |
190 | 72.5k | asn1_template_clear(pval, it->templates); |
191 | 850k | else |
192 | 850k | asn1_primitive_clear(pval, it); |
193 | 922k | break; |
194 | | |
195 | 718 | case ASN1_ITYPE_MSTRING: |
196 | 718 | asn1_primitive_clear(pval, it); |
197 | 718 | break; |
198 | | |
199 | 1.19k | case ASN1_ITYPE_CHOICE: |
200 | 196k | case ASN1_ITYPE_SEQUENCE: |
201 | 196k | case ASN1_ITYPE_NDEF_SEQUENCE: |
202 | 196k | *pval = NULL; |
203 | 196k | break; |
204 | 1.34M | } |
205 | 1.34M | } |
206 | | |
207 | | static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, |
208 | | OSSL_LIB_CTX *libctx, const char *propq) |
209 | 2.53M | { |
210 | 2.53M | const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item); |
211 | 2.53M | int embed = tt->flags & ASN1_TFLG_EMBED; |
212 | 2.53M | ASN1_VALUE *tval; |
213 | 2.53M | int ret; |
214 | 2.53M | if (embed) { |
215 | 60.3k | tval = (ASN1_VALUE *)pval; |
216 | 60.3k | pval = &tval; |
217 | 60.3k | } |
218 | 2.53M | if (tt->flags & ASN1_TFLG_OPTIONAL) { |
219 | 1.56M | asn1_template_clear(pval, tt); |
220 | 1.56M | return 1; |
221 | 1.56M | } |
222 | | /* If ANY DEFINED BY nothing to do */ |
223 | | |
224 | 967k | if (tt->flags & ASN1_TFLG_ADB_MASK) { |
225 | 4.09k | *pval = NULL; |
226 | 4.09k | return 1; |
227 | 4.09k | } |
228 | | /* If SET OF or SEQUENCE OF, its a STACK */ |
229 | 963k | if (tt->flags & ASN1_TFLG_SK_MASK) { |
230 | 2.06k | STACK_OF(ASN1_VALUE) *skval; |
231 | 2.06k | skval = sk_ASN1_VALUE_new_null(); |
232 | 2.06k | if (!skval) { |
233 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
234 | 0 | ret = 0; |
235 | 0 | goto done; |
236 | 0 | } |
237 | 2.06k | *pval = (ASN1_VALUE *)skval; |
238 | 2.06k | ret = 1; |
239 | 2.06k | goto done; |
240 | 2.06k | } |
241 | | /* Otherwise pass it back to the item routine */ |
242 | 961k | ret = asn1_item_embed_new(pval, it, embed, libctx, propq); |
243 | 963k | done: |
244 | 963k | return ret; |
245 | 961k | } |
246 | | |
247 | | static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) |
248 | 1.63M | { |
249 | | /* If ADB or STACK just NULL the field */ |
250 | 1.63M | if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK)) |
251 | 294k | *pval = NULL; |
252 | 1.34M | else |
253 | 1.34M | asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item)); |
254 | 1.63M | } |
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 | 852k | { |
264 | 852k | ASN1_TYPE *typ; |
265 | 852k | ASN1_STRING *str; |
266 | 852k | int utype; |
267 | | |
268 | 852k | if (!it) |
269 | 0 | return 0; |
270 | | |
271 | 852k | if (it->funcs) { |
272 | 3.78k | const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; |
273 | 3.78k | if (embed) { |
274 | 1.42k | if (pf->prim_clear) { |
275 | 1.42k | pf->prim_clear(pval, it); |
276 | 1.42k | return 1; |
277 | 1.42k | } |
278 | 2.36k | } else if (pf->prim_new) { |
279 | 2.36k | return pf->prim_new(pval, it); |
280 | 2.36k | } |
281 | 3.78k | } |
282 | | |
283 | 849k | if (it->itype == ASN1_ITYPE_MSTRING) |
284 | 167k | utype = -1; |
285 | 681k | else |
286 | 681k | utype = it->utype; |
287 | 849k | switch (utype) { |
288 | 389k | case V_ASN1_OBJECT: |
289 | 389k | *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef); |
290 | 389k | 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 | 199k | case V_ASN1_ANY: |
301 | 199k | 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 | 199k | typ->value.ptr = NULL; |
306 | 199k | typ->type = -1; |
307 | 199k | *pval = (ASN1_VALUE *)typ; |
308 | 199k | break; |
309 | | |
310 | 259k | default: |
311 | 259k | if (embed) { |
312 | 26.7k | str = *(ASN1_STRING **)pval; |
313 | 26.7k | memset(str, 0, sizeof(*str)); |
314 | 26.7k | str->type = utype; |
315 | 26.7k | str->flags = ASN1_STRING_FLAG_EMBED; |
316 | 233k | } else { |
317 | 233k | str = ASN1_STRING_type_new(utype); |
318 | 233k | *pval = (ASN1_VALUE *)str; |
319 | 233k | } |
320 | 259k | if (it->itype == ASN1_ITYPE_MSTRING && str) |
321 | 167k | str->flags |= ASN1_STRING_FLAG_MSTRING; |
322 | 259k | break; |
323 | 849k | } |
324 | 459k | if (*pval) |
325 | 459k | return 1; |
326 | 0 | return 0; |
327 | 459k | } |
328 | | |
329 | | static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) |
330 | 850k | { |
331 | 850k | int utype; |
332 | 850k | if (it && it->funcs) { |
333 | 789 | const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; |
334 | 789 | if (pf->prim_clear) |
335 | 0 | pf->prim_clear(pval, it); |
336 | 789 | else |
337 | 789 | *pval = NULL; |
338 | 789 | return; |
339 | 789 | } |
340 | 850k | if (!it || (it->itype == ASN1_ITYPE_MSTRING)) |
341 | 718 | utype = -1; |
342 | 849k | else |
343 | 849k | utype = it->utype; |
344 | 850k | if (utype == V_ASN1_BOOLEAN) |
345 | 11.8k | *(ASN1_BOOLEAN *)pval = it->size; |
346 | 838k | else |
347 | 838k | *pval = NULL; |
348 | 850k | } |