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