/src/openssl31/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 | 60.4M | { |
31 | 60.4M | ASN1_VALUE *ret = NULL; |
32 | 60.4M | if (ASN1_item_ex_new(&ret, it) > 0) |
33 | 60.4M | return ret; |
34 | 0 | return NULL; |
35 | 60.4M | } |
36 | | |
37 | | ASN1_VALUE *ASN1_item_new_ex(const ASN1_ITEM *it, OSSL_LIB_CTX *libctx, |
38 | | const char *propq) |
39 | 60.6k | { |
40 | 60.6k | ASN1_VALUE *ret = NULL; |
41 | 60.6k | if (asn1_item_embed_new(&ret, it, 0, libctx, propq) > 0) |
42 | 60.6k | return ret; |
43 | 0 | return NULL; |
44 | 60.6k | } |
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 | 33.8M | { |
52 | 33.8M | return asn1_item_embed_new(pval, it, 0, libctx, propq); |
53 | 33.8M | } |
54 | | |
55 | | int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
56 | 60.4M | { |
57 | 60.4M | return asn1_item_embed_new(pval, it, 0, NULL, NULL); |
58 | 60.4M | } |
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 | 89.5M | { |
63 | 89.5M | const ASN1_TEMPLATE *tt = NULL; |
64 | 89.5M | const ASN1_EXTERN_FUNCS *ef; |
65 | 89.5M | const ASN1_AUX *aux = it->funcs; |
66 | 89.5M | ASN1_aux_cb *asn1_cb; |
67 | 89.5M | ASN1_VALUE **pseqval; |
68 | 89.5M | int i; |
69 | 89.5M | if (aux && aux->asn1_cb) |
70 | 2.70M | asn1_cb = aux->asn1_cb; |
71 | 86.8M | else |
72 | 86.8M | asn1_cb = 0; |
73 | | |
74 | 89.5M | switch (it->itype) { |
75 | | |
76 | 1.58M | case ASN1_ITYPE_EXTERN: |
77 | 1.58M | ef = it->funcs; |
78 | 1.58M | if (ef != NULL) { |
79 | 1.58M | if (ef->asn1_ex_new_ex != NULL) { |
80 | 476k | if (!ef->asn1_ex_new_ex(pval, it, libctx, propq)) |
81 | 0 | goto memerr; |
82 | 1.11M | } else if (ef->asn1_ex_new != NULL) { |
83 | 1.11M | if (!ef->asn1_ex_new(pval, it)) |
84 | 0 | goto memerr; |
85 | 1.11M | } |
86 | 1.58M | } |
87 | 1.58M | break; |
88 | | |
89 | 34.8M | case ASN1_ITYPE_PRIMITIVE: |
90 | 34.8M | if (it->templates) { |
91 | 45.3k | if (!asn1_template_new(pval, it->templates, libctx, propq)) |
92 | 0 | goto memerr; |
93 | 34.7M | } else if (!asn1_primitive_new(pval, it, embed)) |
94 | 0 | goto memerr; |
95 | 34.8M | break; |
96 | | |
97 | 34.8M | case ASN1_ITYPE_MSTRING: |
98 | 19.8M | if (!asn1_primitive_new(pval, it, embed)) |
99 | 0 | goto memerr; |
100 | 19.8M | break; |
101 | | |
102 | 19.8M | case ASN1_ITYPE_CHOICE: |
103 | 3.90M | if (asn1_cb) { |
104 | 122k | i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); |
105 | 122k | if (!i) |
106 | 0 | goto auxerr; |
107 | 122k | if (i == 2) { |
108 | 0 | return 1; |
109 | 0 | } |
110 | 122k | } |
111 | 3.90M | if (embed) { |
112 | 44.5k | memset(*pval, 0, it->size); |
113 | 3.85M | } else { |
114 | 3.85M | *pval = OPENSSL_zalloc(it->size); |
115 | 3.85M | if (*pval == NULL) |
116 | 0 | goto memerr; |
117 | 3.85M | } |
118 | 3.90M | ossl_asn1_set_choice_selector(pval, -1, it); |
119 | 3.90M | if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) |
120 | 0 | goto auxerr2; |
121 | 3.90M | break; |
122 | | |
123 | 3.90M | case ASN1_ITYPE_NDEF_SEQUENCE: |
124 | 29.3M | case ASN1_ITYPE_SEQUENCE: |
125 | 29.3M | if (asn1_cb) { |
126 | 1.83M | i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); |
127 | 1.83M | if (!i) |
128 | 0 | goto auxerr; |
129 | 1.83M | if (i == 2) { |
130 | 412k | return 1; |
131 | 412k | } |
132 | 1.83M | } |
133 | 28.9M | if (embed) { |
134 | 2.12M | memset(*pval, 0, it->size); |
135 | 26.8M | } else { |
136 | 26.8M | *pval = OPENSSL_zalloc(it->size); |
137 | 26.8M | if (*pval == NULL) |
138 | 0 | goto memerr; |
139 | 26.8M | } |
140 | | /* 0 : init. lock */ |
141 | 28.9M | 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 | 28.9M | ossl_asn1_enc_init(pval, it); |
149 | 99.0M | for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { |
150 | 70.0M | pseqval = ossl_asn1_get_field_ptr(pval, tt); |
151 | 70.0M | if (!asn1_template_new(pseqval, tt, libctx, propq)) |
152 | 0 | goto memerr2; |
153 | 70.0M | } |
154 | 28.9M | if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) |
155 | 0 | goto auxerr2; |
156 | 28.9M | break; |
157 | 89.5M | } |
158 | 89.1M | 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 | 23.4M | { |
176 | 23.4M | const ASN1_EXTERN_FUNCS *ef; |
177 | | |
178 | 23.4M | switch (it->itype) { |
179 | | |
180 | 787k | case ASN1_ITYPE_EXTERN: |
181 | 787k | ef = it->funcs; |
182 | 787k | if (ef && ef->asn1_ex_clear) |
183 | 0 | ef->asn1_ex_clear(pval, it); |
184 | 787k | else |
185 | 787k | *pval = NULL; |
186 | 787k | break; |
187 | | |
188 | 19.6M | case ASN1_ITYPE_PRIMITIVE: |
189 | 19.6M | if (it->templates) |
190 | 256k | asn1_template_clear(pval, it->templates); |
191 | 19.3M | else |
192 | 19.3M | asn1_primitive_clear(pval, it); |
193 | 19.6M | break; |
194 | | |
195 | 408k | case ASN1_ITYPE_MSTRING: |
196 | 408k | asn1_primitive_clear(pval, it); |
197 | 408k | break; |
198 | | |
199 | 913k | case ASN1_ITYPE_CHOICE: |
200 | 2.52M | case ASN1_ITYPE_SEQUENCE: |
201 | 2.56M | case ASN1_ITYPE_NDEF_SEQUENCE: |
202 | 2.56M | *pval = NULL; |
203 | 2.56M | break; |
204 | 23.4M | } |
205 | 23.4M | } |
206 | | |
207 | | static int asn1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, |
208 | | OSSL_LIB_CTX *libctx, const char *propq) |
209 | 141M | { |
210 | 141M | const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item); |
211 | 141M | int embed = tt->flags & ASN1_TFLG_EMBED; |
212 | 141M | ASN1_VALUE *tval; |
213 | 141M | int ret; |
214 | 141M | if (embed) { |
215 | 9.62M | tval = (ASN1_VALUE *)pval; |
216 | 9.62M | pval = &tval; |
217 | 9.62M | } |
218 | 141M | if (tt->flags & ASN1_TFLG_OPTIONAL) { |
219 | 28.6M | asn1_template_clear(pval, tt); |
220 | 28.6M | return 1; |
221 | 28.6M | } |
222 | | /* If ANY DEFINED BY nothing to do */ |
223 | | |
224 | 112M | if (tt->flags & ASN1_TFLG_ADB_MASK) { |
225 | 829k | *pval = NULL; |
226 | 829k | return 1; |
227 | 829k | } |
228 | | /* If SET OF or SEQUENCE OF, its a STACK */ |
229 | 111M | if (tt->flags & ASN1_TFLG_SK_MASK) { |
230 | 1.08M | STACK_OF(ASN1_VALUE) *skval; |
231 | 1.08M | skval = sk_ASN1_VALUE_new_null(); |
232 | 1.08M | if (!skval) { |
233 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
234 | 0 | ret = 0; |
235 | 0 | goto done; |
236 | 0 | } |
237 | 1.08M | *pval = (ASN1_VALUE *)skval; |
238 | 1.08M | ret = 1; |
239 | 1.08M | goto done; |
240 | 1.08M | } |
241 | | /* Otherwise pass it back to the item routine */ |
242 | 110M | ret = asn1_item_embed_new(pval, it, embed, libctx, propq); |
243 | 111M | done: |
244 | 111M | return ret; |
245 | 110M | } |
246 | | |
247 | | static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) |
248 | 28.9M | { |
249 | | /* If ADB or STACK just NULL the field */ |
250 | 28.9M | if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK)) |
251 | 5.52M | *pval = NULL; |
252 | 23.4M | else |
253 | 23.4M | asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item)); |
254 | 28.9M | } |
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 | 137M | { |
264 | 137M | ASN1_TYPE *typ; |
265 | 137M | ASN1_STRING *str; |
266 | 137M | int utype; |
267 | | |
268 | 137M | if (!it) |
269 | 0 | return 0; |
270 | | |
271 | 137M | if (it->funcs) { |
272 | 1.73M | const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; |
273 | 1.73M | if (embed) { |
274 | 658k | if (pf->prim_clear) { |
275 | 658k | pf->prim_clear(pval, it); |
276 | 658k | return 1; |
277 | 658k | } |
278 | 1.08M | } else if (pf->prim_new) { |
279 | 1.08M | return pf->prim_new(pval, it); |
280 | 1.08M | } |
281 | 1.73M | } |
282 | | |
283 | 135M | if (it->itype == ASN1_ITYPE_MSTRING) |
284 | 39.2M | utype = -1; |
285 | 96.3M | else |
286 | 96.3M | utype = it->utype; |
287 | 135M | switch (utype) { |
288 | 47.6M | case V_ASN1_OBJECT: |
289 | 47.6M | *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef); |
290 | 47.6M | 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 | 38.0M | case V_ASN1_ANY: |
301 | 38.0M | 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 | 38.0M | typ->value.ptr = NULL; |
306 | 38.0M | typ->type = -1; |
307 | 38.0M | *pval = (ASN1_VALUE *)typ; |
308 | 38.0M | break; |
309 | | |
310 | 49.7M | default: |
311 | 49.7M | if (embed) { |
312 | 4.38M | str = *(ASN1_STRING **)pval; |
313 | 4.38M | memset(str, 0, sizeof(*str)); |
314 | 4.38M | str->type = utype; |
315 | 4.38M | str->flags = ASN1_STRING_FLAG_EMBED; |
316 | 45.3M | } else { |
317 | 45.3M | str = ASN1_STRING_type_new(utype); |
318 | 45.3M | *pval = (ASN1_VALUE *)str; |
319 | 45.3M | } |
320 | 49.7M | if (it->itype == ASN1_ITYPE_MSTRING && str) |
321 | 39.2M | str->flags |= ASN1_STRING_FLAG_MSTRING; |
322 | 49.7M | break; |
323 | 135M | } |
324 | 87.8M | if (*pval) |
325 | 87.8M | return 1; |
326 | 0 | return 0; |
327 | 87.8M | } |
328 | | |
329 | | static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) |
330 | 19.7M | { |
331 | 19.7M | int utype; |
332 | 19.7M | if (it && it->funcs) { |
333 | 490k | const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; |
334 | 490k | if (pf->prim_clear) |
335 | 391k | pf->prim_clear(pval, it); |
336 | 98.9k | else |
337 | 98.9k | *pval = NULL; |
338 | 490k | return; |
339 | 490k | } |
340 | 19.3M | if (!it || (it->itype == ASN1_ITYPE_MSTRING)) |
341 | 408k | utype = -1; |
342 | 18.8M | else |
343 | 18.8M | utype = it->utype; |
344 | 19.3M | if (utype == V_ASN1_BOOLEAN) |
345 | 3.17M | *(ASN1_BOOLEAN *)pval = it->size; |
346 | 16.1M | else |
347 | 16.1M | *pval = NULL; |
348 | 19.3M | } |