/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 | 2.60M | { |
31 | 2.60M | ASN1_VALUE *ret = NULL; |
32 | 2.60M | if (ASN1_item_ex_new(&ret, it) > 0) |
33 | 2.60M | return ret; |
34 | 0 | return NULL; |
35 | 2.60M | } |
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 | 2.62M | { |
52 | 2.62M | return asn1_item_embed_new(pval, it, 0, libctx, propq); |
53 | 2.62M | } |
54 | | |
55 | | int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
56 | 2.60M | { |
57 | 2.60M | return asn1_item_embed_new(pval, it, 0, NULL, NULL); |
58 | 2.60M | } |
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 | 13.6M | { |
63 | 13.6M | const ASN1_TEMPLATE *tt = NULL; |
64 | 13.6M | const ASN1_EXTERN_FUNCS *ef; |
65 | 13.6M | const ASN1_AUX *aux = it->funcs; |
66 | 13.6M | ASN1_aux_cb *asn1_cb; |
67 | 13.6M | ASN1_VALUE **pseqval; |
68 | 13.6M | int i; |
69 | 13.6M | if (aux && aux->asn1_cb) |
70 | 885k | asn1_cb = aux->asn1_cb; |
71 | 12.7M | else |
72 | 12.7M | asn1_cb = 0; |
73 | | |
74 | 13.6M | switch (it->itype) { |
75 | | |
76 | 370k | case ASN1_ITYPE_EXTERN: |
77 | 370k | ef = it->funcs; |
78 | 370k | if (ef != NULL) { |
79 | 370k | if (ef->asn1_ex_new_ex != NULL) { |
80 | 117k | if (!ef->asn1_ex_new_ex(pval, it, libctx, propq)) |
81 | 0 | goto memerr; |
82 | 253k | } else if (ef->asn1_ex_new != NULL) { |
83 | 253k | if (!ef->asn1_ex_new(pval, it)) |
84 | 0 | goto memerr; |
85 | 253k | } |
86 | 370k | } |
87 | 370k | break; |
88 | | |
89 | 6.24M | case ASN1_ITYPE_PRIMITIVE: |
90 | 6.24M | if (it->templates) { |
91 | 0 | if (!asn1_template_new(pval, it->templates, libctx, propq)) |
92 | 0 | goto memerr; |
93 | 6.24M | } else if (!asn1_primitive_new(pval, it, embed)) |
94 | 0 | goto memerr; |
95 | 6.24M | break; |
96 | | |
97 | 6.24M | case ASN1_ITYPE_MSTRING: |
98 | 2.00M | if (!asn1_primitive_new(pval, it, embed)) |
99 | 0 | goto memerr; |
100 | 2.00M | break; |
101 | | |
102 | 2.00M | case ASN1_ITYPE_CHOICE: |
103 | 341k | if (asn1_cb) { |
104 | 31.3k | i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); |
105 | 31.3k | if (!i) |
106 | 0 | goto auxerr; |
107 | 31.3k | if (i == 2) { |
108 | 0 | return 1; |
109 | 0 | } |
110 | 31.3k | } |
111 | 341k | if (embed) { |
112 | 19.5k | memset(*pval, 0, it->size); |
113 | 322k | } else { |
114 | 322k | *pval = OPENSSL_zalloc(it->size); |
115 | 322k | if (*pval == NULL) |
116 | 0 | goto memerr; |
117 | 322k | } |
118 | 341k | ossl_asn1_set_choice_selector(pval, -1, it); |
119 | 341k | if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) |
120 | 0 | goto auxerr2; |
121 | 341k | break; |
122 | | |
123 | 341k | case ASN1_ITYPE_NDEF_SEQUENCE: |
124 | 4.67M | case ASN1_ITYPE_SEQUENCE: |
125 | 4.67M | if (asn1_cb) { |
126 | 638k | i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); |
127 | 638k | if (!i) |
128 | 0 | goto auxerr; |
129 | 638k | if (i == 2) { |
130 | 162k | return 1; |
131 | 162k | } |
132 | 638k | } |
133 | 4.50M | if (embed) { |
134 | 476k | memset(*pval, 0, it->size); |
135 | 4.03M | } else { |
136 | 4.03M | *pval = OPENSSL_zalloc(it->size); |
137 | 4.03M | if (*pval == NULL) |
138 | 0 | goto memerr; |
139 | 4.03M | } |
140 | | /* 0 : init. lock */ |
141 | 4.50M | 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 | 4.50M | ossl_asn1_enc_init(pval, it); |
149 | 16.4M | for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { |
150 | 11.9M | pseqval = ossl_asn1_get_field_ptr(pval, tt); |
151 | 11.9M | if (!asn1_template_new(pseqval, tt, libctx, propq)) |
152 | 0 | goto memerr2; |
153 | 11.9M | } |
154 | 4.50M | if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) |
155 | 0 | goto auxerr2; |
156 | 4.50M | break; |
157 | 13.6M | } |
158 | 13.4M | 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 | 2.51M | { |
176 | 2.51M | const ASN1_EXTERN_FUNCS *ef; |
177 | | |
178 | 2.51M | 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 | 2.14M | case ASN1_ITYPE_PRIMITIVE: |
189 | 2.14M | if (it->templates) |
190 | 0 | asn1_template_clear(pval, it->templates); |
191 | 2.14M | else |
192 | 2.14M | asn1_primitive_clear(pval, it); |
193 | 2.14M | break; |
194 | | |
195 | 55.1k | case ASN1_ITYPE_MSTRING: |
196 | 55.1k | asn1_primitive_clear(pval, it); |
197 | 55.1k | break; |
198 | | |
199 | 132k | case ASN1_ITYPE_CHOICE: |
200 | 304k | case ASN1_ITYPE_SEQUENCE: |
201 | 314k | case ASN1_ITYPE_NDEF_SEQUENCE: |
202 | 314k | *pval = NULL; |
203 | 314k | break; |
204 | 2.51M | } |
205 | 2.51M | } |
206 | | |
207 | | static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, |
208 | | OSSL_LIB_CTX *libctx, const char *propq) |
209 | 11.9M | { |
210 | 11.9M | const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item); |
211 | 11.9M | int embed = tt->flags & ASN1_TFLG_EMBED; |
212 | 11.9M | ASN1_VALUE *tval; |
213 | 11.9M | int ret; |
214 | 11.9M | if (embed) { |
215 | 1.04M | tval = (ASN1_VALUE *)pval; |
216 | 1.04M | pval = &tval; |
217 | 1.04M | } |
218 | 11.9M | if (tt->flags & ASN1_TFLG_OPTIONAL) { |
219 | 3.23M | asn1_template_clear(pval, tt); |
220 | 3.23M | return 1; |
221 | 3.23M | } |
222 | | /* If ANY DEFINED BY nothing to do */ |
223 | | |
224 | 8.71M | if (tt->flags & ASN1_TFLG_ADB_MASK) { |
225 | 134k | *pval = NULL; |
226 | 134k | return 1; |
227 | 134k | } |
228 | | /* If SET OF or SEQUENCE OF, its a STACK */ |
229 | 8.58M | if (tt->flags & ASN1_TFLG_SK_MASK) { |
230 | 174k | STACK_OF(ASN1_VALUE) *skval; |
231 | 174k | skval = sk_ASN1_VALUE_new_null(); |
232 | 174k | if (!skval) { |
233 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
234 | 0 | ret = 0; |
235 | 0 | goto done; |
236 | 0 | } |
237 | 174k | *pval = (ASN1_VALUE *)skval; |
238 | 174k | ret = 1; |
239 | 174k | goto done; |
240 | 174k | } |
241 | | /* Otherwise pass it back to the item routine */ |
242 | 8.40M | ret = asn1_item_embed_new(pval, it, embed, libctx, propq); |
243 | 8.58M | done: |
244 | 8.58M | return ret; |
245 | 8.40M | } |
246 | | |
247 | | static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) |
248 | 3.23M | { |
249 | | /* If ADB or STACK just NULL the field */ |
250 | 3.23M | if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK)) |
251 | 719k | *pval = NULL; |
252 | 2.51M | else |
253 | 2.51M | asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item)); |
254 | 3.23M | } |
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 | 8.25M | { |
264 | 8.25M | ASN1_TYPE *typ; |
265 | 8.25M | ASN1_STRING *str; |
266 | 8.25M | int utype; |
267 | | |
268 | 8.25M | if (!it) |
269 | 0 | return 0; |
270 | | |
271 | 8.25M | if (it->funcs) { |
272 | 215k | const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; |
273 | 215k | if (embed) { |
274 | 116k | if (pf->prim_clear) { |
275 | 116k | pf->prim_clear(pval, it); |
276 | 116k | return 1; |
277 | 116k | } |
278 | 116k | } else if (pf->prim_new) { |
279 | 99.4k | return pf->prim_new(pval, it); |
280 | 99.4k | } |
281 | 215k | } |
282 | | |
283 | 8.03M | if (it->itype == ASN1_ITYPE_MSTRING) |
284 | 2.00M | utype = -1; |
285 | 6.02M | else |
286 | 6.02M | utype = it->utype; |
287 | 8.03M | switch (utype) { |
288 | 3.02M | case V_ASN1_OBJECT: |
289 | 3.02M | *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef); |
290 | 3.02M | 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 | 1.40M | case V_ASN1_ANY: |
301 | 1.40M | 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 | 1.40M | typ->value.ptr = NULL; |
306 | 1.40M | typ->type = -1; |
307 | 1.40M | *pval = (ASN1_VALUE *)typ; |
308 | 1.40M | break; |
309 | | |
310 | 3.60M | default: |
311 | 3.60M | if (embed) { |
312 | 342k | str = *(ASN1_STRING **)pval; |
313 | 342k | memset(str, 0, sizeof(*str)); |
314 | 342k | str->type = utype; |
315 | 342k | str->flags = ASN1_STRING_FLAG_EMBED; |
316 | 3.26M | } else { |
317 | 3.26M | str = ASN1_STRING_type_new(utype); |
318 | 3.26M | *pval = (ASN1_VALUE *)str; |
319 | 3.26M | } |
320 | 3.60M | if (it->itype == ASN1_ITYPE_MSTRING && str) |
321 | 2.00M | str->flags |= ASN1_STRING_FLAG_MSTRING; |
322 | 3.60M | break; |
323 | 8.03M | } |
324 | 5.00M | if (*pval) |
325 | 5.00M | return 1; |
326 | 0 | return 0; |
327 | 5.00M | } |
328 | | |
329 | | static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) |
330 | 2.19M | { |
331 | 2.19M | int utype; |
332 | 2.19M | if (it && it->funcs) { |
333 | 102k | const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; |
334 | 102k | if (pf->prim_clear) |
335 | 87.9k | pf->prim_clear(pval, it); |
336 | 14.7k | else |
337 | 14.7k | *pval = NULL; |
338 | 102k | return; |
339 | 102k | } |
340 | 2.09M | if (!it || (it->itype == ASN1_ITYPE_MSTRING)) |
341 | 55.1k | utype = -1; |
342 | 2.04M | else |
343 | 2.04M | utype = it->utype; |
344 | 2.09M | if (utype == V_ASN1_BOOLEAN) |
345 | 190k | *(ASN1_BOOLEAN *)pval = it->size; |
346 | 1.90M | else |
347 | 1.90M | *pval = NULL; |
348 | 2.09M | } |