Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/asn1/tasn_new.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/asn1.h>
5
6 #include <string.h>
7
8 #include <openssl/asn1t.h>
9 #include <openssl/err.h>
10 #include <openssl/mem.h>
11 #include <openssl/obj.h>
12
13 #include "../internal.h"
14 #include "internal.h"
15
16
17 static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
18 int combine);
19 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
20 static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
21 static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
22 static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
23 static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
24
25 ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it) {
26 ASN1_VALUE *ret = NULL;
27 if (ASN1_item_ex_new(&ret, it) > 0) {
28 return ret;
29 }
30 return NULL;
31 }
32
33 // Allocate an ASN1 structure
34
35 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) {
36 return asn1_item_ex_combine_new(pval, it, 0);
37 }
38
39 static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
40 int combine) {
41 const ASN1_TEMPLATE *tt = NULL;
42 const ASN1_EXTERN_FUNCS *ef;
43 ASN1_VALUE **pseqval;
44 int i;
45
46 switch (it->itype) {
47 case ASN1_ITYPE_EXTERN:
48 ef = it->funcs;
49 if (ef && ef->asn1_ex_new) {
50 if (!ef->asn1_ex_new(pval, it)) {
51 goto memerr;
52 }
53 }
54 break;
55
56 case ASN1_ITYPE_PRIMITIVE:
57 if (it->templates) {
58 if (!ASN1_template_new(pval, it->templates)) {
59 goto memerr;
60 }
61 } else if (!ASN1_primitive_new(pval, it)) {
62 goto memerr;
63 }
64 break;
65
66 case ASN1_ITYPE_MSTRING:
67 if (!ASN1_primitive_new(pval, it)) {
68 goto memerr;
69 }
70 break;
71
72 case ASN1_ITYPE_CHOICE: {
73 const ASN1_AUX *aux = it->funcs;
74 ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
75 if (asn1_cb) {
76 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
77 if (!i) {
78 goto auxerr;
79 }
80 if (i == 2) {
81 return 1;
82 }
83 }
84 if (!combine) {
85 *pval = OPENSSL_zalloc(it->size);
86 if (!*pval) {
87 goto memerr;
88 }
89 }
90 asn1_set_choice_selector(pval, -1, it);
91 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) {
92 goto auxerr2;
93 }
94 break;
95 }
96
97 case ASN1_ITYPE_SEQUENCE: {
98 const ASN1_AUX *aux = it->funcs;
99 ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
100 if (asn1_cb) {
101 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
102 if (!i) {
103 goto auxerr;
104 }
105 if (i == 2) {
106 return 1;
107 }
108 }
109 if (!combine) {
110 *pval = OPENSSL_zalloc(it->size);
111 if (!*pval) {
112 goto memerr;
113 }
114 asn1_refcount_set_one(pval, it);
115 asn1_enc_init(pval, it);
116 }
117 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
118 pseqval = asn1_get_field_ptr(pval, tt);
119 if (!ASN1_template_new(pseqval, tt)) {
120 goto memerr2;
121 }
122 }
123 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) {
124 goto auxerr2;
125 }
126 break;
127 }
128 }
129 return 1;
130
131 memerr2:
132 asn1_item_combine_free(pval, it, combine);
133 memerr:
134 return 0;
135
136 auxerr2:
137 asn1_item_combine_free(pval, it, combine);
138 auxerr:
139 OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR);
140 return 0;
141 }
142
143 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) {
144 switch (it->itype) {
145 case ASN1_ITYPE_EXTERN:
146 *pval = NULL;
147 break;
148
149 case ASN1_ITYPE_PRIMITIVE:
150 if (it->templates) {
151 asn1_template_clear(pval, it->templates);
152 } else {
153 asn1_primitive_clear(pval, it);
154 }
155 break;
156
157 case ASN1_ITYPE_MSTRING:
158 asn1_primitive_clear(pval, it);
159 break;
160
161 case ASN1_ITYPE_CHOICE:
162 case ASN1_ITYPE_SEQUENCE:
163 *pval = NULL;
164 break;
165 }
166 }
167
168 static int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
169 const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
170 int ret;
171 if (tt->flags & ASN1_TFLG_OPTIONAL) {
172 asn1_template_clear(pval, tt);
173 return 1;
174 }
175 // If ANY DEFINED BY nothing to do
176
177 if (tt->flags & ASN1_TFLG_ADB_MASK) {
178 *pval = NULL;
179 return 1;
180 }
181 // If SET OF or SEQUENCE OF, its a STACK
182 if (tt->flags & ASN1_TFLG_SK_MASK) {
183 STACK_OF(ASN1_VALUE) *skval;
184 skval = sk_ASN1_VALUE_new_null();
185 if (!skval) {
186 ret = 0;
187 goto done;
188 }
189 *pval = (ASN1_VALUE *)skval;
190 ret = 1;
191 goto done;
192 }
193 // Otherwise pass it back to the item routine
194 ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
195 done:
196 return ret;
197 }
198
199 static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) {
200 // If ADB or STACK just NULL the field
201 if (tt->flags & (ASN1_TFLG_ADB_MASK | ASN1_TFLG_SK_MASK)) {
202 *pval = NULL;
203 } else {
204 asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
205 }
206 }
207
208 // NB: could probably combine most of the real XXX_new() behaviour and junk
209 // all the old functions.
210
211 static int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it) {
212 if (!it) {
213 return 0;
214 }
215
216 // Historically, |it->funcs| for primitive types contained an
217 // |ASN1_PRIMITIVE_FUNCS| table of calbacks.
218 assert(it->funcs == NULL);
219
220 int utype;
221 if (it->itype == ASN1_ITYPE_MSTRING) {
222 utype = -1;
223 } else {
224 utype = it->utype;
225 }
226 switch (utype) {
227 case V_ASN1_OBJECT:
228 *pval = (ASN1_VALUE *)OBJ_get_undef();
229 return 1;
230
231 case V_ASN1_BOOLEAN:
232 *(ASN1_BOOLEAN *)pval = (ASN1_BOOLEAN)it->size;
233 return 1;
234
235 case V_ASN1_NULL:
236 *pval = (ASN1_VALUE *)1;
237 return 1;
238
239 case V_ASN1_ANY: {
240 ASN1_TYPE *typ = OPENSSL_zalloc(sizeof(ASN1_TYPE));
241 if (!typ) {
242 return 0;
243 }
244 typ->type = -1;
245 *pval = (ASN1_VALUE *)typ;
246 break;
247 }
248
249 default:
250 *pval = (ASN1_VALUE *)ASN1_STRING_type_new(utype);
251 break;
252 }
253 if (*pval) {
254 return 1;
255 }
256 return 0;
257 }
258
259 static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) {
260 int utype;
261 // Historically, |it->funcs| for primitive types contained an
262 // |ASN1_PRIMITIVE_FUNCS| table of calbacks.
263 assert(it == NULL || it->funcs == NULL);
264 if (!it || (it->itype == ASN1_ITYPE_MSTRING)) {
265 utype = -1;
266 } else {
267 utype = it->utype;
268 }
269 if (utype == V_ASN1_BOOLEAN) {
270 *(ASN1_BOOLEAN *)pval = (ASN1_BOOLEAN)it->size;
271 } else {
272 *pval = NULL;
273 }
274 }