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