/src/boringssl/crypto/asn1/tasn_dec.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <openssl/asn1.h> |
16 | | #include <openssl/asn1t.h> |
17 | | #include <openssl/bytestring.h> |
18 | | #include <openssl/err.h> |
19 | | #include <openssl/mem.h> |
20 | | #include <openssl/pool.h> |
21 | | |
22 | | #include <assert.h> |
23 | | #include <limits.h> |
24 | | #include <string.h> |
25 | | |
26 | | #include "../bytestring/internal.h" |
27 | | #include "../internal.h" |
28 | | #include "internal.h" |
29 | | |
30 | | // Constructed types with a recursive definition (such as can be found in PKCS7) |
31 | | // could eventually exceed the stack given malicious input with excessive |
32 | | // recursion. Therefore we limit the stack depth. This is the maximum number of |
33 | | // recursive invocations of asn1_item_embed_d2i(). |
34 | 13.7M | #define ASN1_MAX_CONSTRUCTED_NEST 30 |
35 | | |
36 | | static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, |
37 | | char *cst, const unsigned char **in, long len, |
38 | | int exptag, int expclass, char opt); |
39 | | |
40 | | static int asn1_template_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, |
41 | | long len, const ASN1_TEMPLATE *tt, char opt, |
42 | | CRYPTO_BUFFER *buf, int depth); |
43 | | static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in, |
44 | | long len, const ASN1_TEMPLATE *tt, char opt, |
45 | | CRYPTO_BUFFER *buf, int depth); |
46 | | static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, long len, |
47 | | int utype, const ASN1_ITEM *it); |
48 | | static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in, |
49 | | long len, const ASN1_ITEM *it, int tag, |
50 | | int aclass, char opt); |
51 | | static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, |
52 | | long len, const ASN1_ITEM *it, int tag, int aclass, |
53 | | char opt, CRYPTO_BUFFER *buf, int depth); |
54 | | |
55 | 1.78M | unsigned long ASN1_tag2bit(int tag) { |
56 | 1.78M | switch (tag) { |
57 | 24.0k | case V_ASN1_BIT_STRING: |
58 | 24.0k | return B_ASN1_BIT_STRING; |
59 | 1.37k | case V_ASN1_OCTET_STRING: |
60 | 1.37k | return B_ASN1_OCTET_STRING; |
61 | 412k | case V_ASN1_UTF8STRING: |
62 | 412k | return B_ASN1_UTF8STRING; |
63 | 47.4k | case V_ASN1_SEQUENCE: |
64 | 47.4k | return B_ASN1_SEQUENCE; |
65 | 1.90k | case V_ASN1_NUMERICSTRING: |
66 | 1.90k | return B_ASN1_NUMERICSTRING; |
67 | 377k | case V_ASN1_PRINTABLESTRING: |
68 | 377k | return B_ASN1_PRINTABLESTRING; |
69 | 18.3k | case V_ASN1_T61STRING: |
70 | 18.3k | return B_ASN1_T61STRING; |
71 | 864 | case V_ASN1_VIDEOTEXSTRING: |
72 | 864 | return B_ASN1_VIDEOTEXSTRING; |
73 | 33.3k | case V_ASN1_IA5STRING: |
74 | 33.3k | return B_ASN1_IA5STRING; |
75 | 429k | case V_ASN1_UTCTIME: |
76 | 429k | return B_ASN1_UTCTIME; |
77 | 2.45k | case V_ASN1_GENERALIZEDTIME: |
78 | 2.45k | return B_ASN1_GENERALIZEDTIME; |
79 | 965 | case V_ASN1_GRAPHICSTRING: |
80 | 965 | return B_ASN1_GRAPHICSTRING; |
81 | 1.24k | case V_ASN1_ISO64STRING: |
82 | 1.24k | return B_ASN1_ISO64STRING; |
83 | 1.49k | case V_ASN1_GENERALSTRING: |
84 | 1.49k | return B_ASN1_GENERALSTRING; |
85 | 251k | case V_ASN1_UNIVERSALSTRING: |
86 | 251k | return B_ASN1_UNIVERSALSTRING; |
87 | 65.9k | case V_ASN1_BMPSTRING: |
88 | 65.9k | return B_ASN1_BMPSTRING; |
89 | 112k | default: |
90 | 112k | return 0; |
91 | 1.78M | } |
92 | 1.78M | } |
93 | | |
94 | 1.65M | static int is_supported_universal_type(int tag, int aclass) { |
95 | 1.65M | if (aclass != V_ASN1_UNIVERSAL) { |
96 | 45.7k | return 0; |
97 | 45.7k | } |
98 | 1.60M | return tag == V_ASN1_OBJECT || tag == V_ASN1_NULL || tag == V_ASN1_BOOLEAN || |
99 | 1.60M | tag == V_ASN1_BIT_STRING || tag == V_ASN1_INTEGER || |
100 | 1.60M | tag == V_ASN1_ENUMERATED || tag == V_ASN1_OCTET_STRING || |
101 | 1.60M | tag == V_ASN1_NUMERICSTRING || tag == V_ASN1_PRINTABLESTRING || |
102 | 1.60M | tag == V_ASN1_T61STRING || tag == V_ASN1_VIDEOTEXSTRING || |
103 | 1.60M | tag == V_ASN1_IA5STRING || tag == V_ASN1_UTCTIME || |
104 | 1.60M | tag == V_ASN1_GENERALIZEDTIME || tag == V_ASN1_GRAPHICSTRING || |
105 | 1.60M | tag == V_ASN1_VISIBLESTRING || tag == V_ASN1_GENERALSTRING || |
106 | 1.60M | tag == V_ASN1_UNIVERSALSTRING || tag == V_ASN1_BMPSTRING || |
107 | 1.60M | tag == V_ASN1_UTF8STRING || tag == V_ASN1_SET || |
108 | 1.60M | tag == V_ASN1_SEQUENCE; |
109 | 1.65M | } |
110 | | |
111 | | // Macro to initialize and invalidate the cache |
112 | | |
113 | | // Decode an ASN1 item, this currently behaves just like a standard 'd2i' |
114 | | // function. 'in' points to a buffer to read the data from, in future we |
115 | | // will have more advanced versions that can input data a piece at a time and |
116 | | // this will simply be a special case. |
117 | | |
118 | | ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, |
119 | 338k | const ASN1_ITEM *it) { |
120 | 338k | ASN1_VALUE *ret = NULL; |
121 | 338k | if (asn1_item_ex_d2i(&ret, in, len, it, /*tag=*/-1, /*aclass=*/0, /*opt=*/0, |
122 | 338k | /*buf=*/NULL, /*depth=*/0) <= 0) { |
123 | | // Clean up, in case the caller left a partial object. |
124 | | // |
125 | | // TODO(davidben): I don't think it can leave one, but the codepaths below |
126 | | // are a bit inconsistent. Revisit this when rewriting this function. |
127 | 19.5k | ASN1_item_ex_free(&ret, it); |
128 | 19.5k | } |
129 | | |
130 | | // If the caller supplied an output pointer, free the old one and replace it |
131 | | // with |ret|. This differs from OpenSSL slightly in that we don't support |
132 | | // object reuse. We run this on both success and failure. On failure, even |
133 | | // with object reuse, OpenSSL destroys the previous object. |
134 | 338k | if (pval != NULL) { |
135 | 0 | ASN1_item_ex_free(pval, it); |
136 | 0 | *pval = ret; |
137 | 0 | } |
138 | 338k | return ret; |
139 | 338k | } |
140 | | |
141 | | // Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and |
142 | | // tag mismatch return -1 to handle OPTIONAL |
143 | | // |
144 | | // TODO(davidben): Historically, all functions in this file had to account for |
145 | | // |*pval| containing an arbitrary existing value. This is no longer the case |
146 | | // because |ASN1_item_d2i| now always starts from NULL. As part of rewriting |
147 | | // this function, take the simplified assumptions into account. Though we must |
148 | | // still account for the internal calls to |ASN1_item_ex_new|. |
149 | | |
150 | | static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, |
151 | | long len, const ASN1_ITEM *it, int tag, int aclass, |
152 | 13.7M | char opt, CRYPTO_BUFFER *buf, int depth) { |
153 | 13.7M | const ASN1_TEMPLATE *tt, *errtt = NULL; |
154 | 13.7M | const unsigned char *p = NULL, *q; |
155 | 13.7M | unsigned char oclass; |
156 | 13.7M | char cst, isopt; |
157 | 13.7M | int i; |
158 | 13.7M | int otag; |
159 | 13.7M | int ret = 0; |
160 | 13.7M | ASN1_VALUE **pchptr; |
161 | 13.7M | if (!pval) { |
162 | 0 | return 0; |
163 | 0 | } |
164 | | |
165 | 13.7M | if (buf != NULL) { |
166 | 6.09M | assert(CRYPTO_BUFFER_data(buf) <= *in && |
167 | 6.09M | *in + len <= CRYPTO_BUFFER_data(buf) + CRYPTO_BUFFER_len(buf)); |
168 | 6.09M | } |
169 | | |
170 | | // Bound |len| to comfortably fit in an int. Lengths in this module often |
171 | | // switch between int and long without overflow checks. |
172 | 13.7M | if (len > INT_MAX / 2) { |
173 | 0 | len = INT_MAX / 2; |
174 | 0 | } |
175 | | |
176 | 13.7M | if (++depth > ASN1_MAX_CONSTRUCTED_NEST) { |
177 | 0 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_TOO_DEEP); |
178 | 0 | goto err; |
179 | 0 | } |
180 | | |
181 | 13.7M | switch (it->itype) { |
182 | 9.02M | case ASN1_ITYPE_PRIMITIVE: |
183 | 9.02M | if (it->templates) { |
184 | | // tagging or OPTIONAL is currently illegal on an item template |
185 | | // because the flags can't get passed down. In practice this |
186 | | // isn't a problem: we include the relevant flags from the item |
187 | | // template in the template itself. |
188 | 1.53M | if ((tag != -1) || opt) { |
189 | 0 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE); |
190 | 0 | goto err; |
191 | 0 | } |
192 | 1.53M | return asn1_template_ex_d2i(pval, in, len, it->templates, opt, buf, |
193 | 1.53M | depth); |
194 | 1.53M | } |
195 | 7.48M | return asn1_d2i_ex_primitive(pval, in, len, it, tag, aclass, opt); |
196 | 0 | break; |
197 | | |
198 | 430k | case ASN1_ITYPE_MSTRING: |
199 | | // It never makes sense for multi-strings to have implicit tagging, so |
200 | | // if tag != -1, then this looks like an error in the template. |
201 | 430k | if (tag != -1) { |
202 | 0 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); |
203 | 0 | goto err; |
204 | 0 | } |
205 | | |
206 | 430k | p = *in; |
207 | | // Just read in tag and class |
208 | 430k | ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, &p, len, -1, 0, 1); |
209 | 430k | if (!ret) { |
210 | 65 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
211 | 65 | goto err; |
212 | 65 | } |
213 | | |
214 | | // Must be UNIVERSAL class |
215 | 430k | if (oclass != V_ASN1_UNIVERSAL) { |
216 | | // If OPTIONAL, assume this is OK |
217 | 35 | if (opt) { |
218 | 0 | return -1; |
219 | 0 | } |
220 | 35 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL); |
221 | 35 | goto err; |
222 | 35 | } |
223 | | // Check tag matches bit map |
224 | 430k | if (!(ASN1_tag2bit(otag) & it->utype)) { |
225 | | // If OPTIONAL, assume this is OK |
226 | 90 | if (opt) { |
227 | 0 | return -1; |
228 | 0 | } |
229 | 90 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_MSTRING_WRONG_TAG); |
230 | 90 | goto err; |
231 | 90 | } |
232 | 430k | return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0); |
233 | | |
234 | 438k | case ASN1_ITYPE_EXTERN: { |
235 | | // We don't support implicit tagging with external types. |
236 | 438k | if (tag != -1) { |
237 | 0 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); |
238 | 0 | goto err; |
239 | 0 | } |
240 | 438k | const ASN1_EXTERN_FUNCS *ef = |
241 | 438k | reinterpret_cast<const ASN1_EXTERN_FUNCS *>(it->funcs); |
242 | 438k | return ef->asn1_ex_d2i(pval, in, len, it, opt, NULL); |
243 | 438k | } |
244 | | |
245 | 294k | case ASN1_ITYPE_CHOICE: { |
246 | | // It never makes sense for CHOICE types to have implicit tagging, so if |
247 | | // tag != -1, then this looks like an error in the template. |
248 | 294k | if (tag != -1) { |
249 | 0 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); |
250 | 0 | goto err; |
251 | 0 | } |
252 | | |
253 | 294k | const ASN1_AUX *aux = reinterpret_cast<const ASN1_AUX *>(it->funcs); |
254 | 294k | ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL; |
255 | 294k | if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) { |
256 | 0 | goto auxerr; |
257 | 0 | } |
258 | | |
259 | 294k | if (*pval) { |
260 | | // Free up and zero CHOICE value if initialised |
261 | 1.02k | i = asn1_get_choice_selector(pval, it); |
262 | 1.02k | if ((i >= 0) && (i < it->tcount)) { |
263 | 0 | tt = it->templates + i; |
264 | 0 | pchptr = asn1_get_field_ptr(pval, tt); |
265 | 0 | ASN1_template_free(pchptr, tt); |
266 | 0 | asn1_set_choice_selector(pval, -1, it); |
267 | 0 | } |
268 | 293k | } else if (!ASN1_item_ex_new(pval, it)) { |
269 | 0 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
270 | 0 | goto err; |
271 | 0 | } |
272 | | // CHOICE type, try each possibility in turn |
273 | 294k | p = *in; |
274 | 1.84M | for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { |
275 | 1.84M | pchptr = asn1_get_field_ptr(pval, tt); |
276 | | // We mark field as OPTIONAL so its absence can be recognised. |
277 | 1.84M | ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, buf, depth); |
278 | | // If field not present, try the next one |
279 | 1.84M | if (ret == -1) { |
280 | 1.54M | continue; |
281 | 1.54M | } |
282 | | // If positive return, read OK, break loop |
283 | 294k | if (ret > 0) { |
284 | 292k | break; |
285 | 292k | } |
286 | | // Otherwise must be an ASN1 parsing error |
287 | 1.79k | errtt = tt; |
288 | 1.79k | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
289 | 1.79k | goto err; |
290 | 294k | } |
291 | | |
292 | | // Did we fall off the end without reading anything? |
293 | 292k | if (i == it->tcount) { |
294 | | // If OPTIONAL, this is OK |
295 | 306 | if (opt) { |
296 | | // Free and zero it |
297 | 0 | ASN1_item_ex_free(pval, it); |
298 | 0 | return -1; |
299 | 0 | } |
300 | 306 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE); |
301 | 306 | goto err; |
302 | 306 | } |
303 | | |
304 | 292k | asn1_set_choice_selector(pval, i, it); |
305 | 292k | if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) { |
306 | 0 | goto auxerr; |
307 | 0 | } |
308 | 292k | *in = p; |
309 | 292k | return 1; |
310 | 292k | } |
311 | | |
312 | 3.59M | case ASN1_ITYPE_SEQUENCE: { |
313 | 3.59M | p = *in; |
314 | | |
315 | | // If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL |
316 | 3.59M | if (tag == -1) { |
317 | 3.06M | tag = V_ASN1_SEQUENCE; |
318 | 3.06M | aclass = V_ASN1_UNIVERSAL; |
319 | 3.06M | } |
320 | | // Get SEQUENCE length and update len, p |
321 | 3.59M | ret = asn1_check_tlen(&len, NULL, NULL, &cst, &p, len, tag, aclass, opt); |
322 | 3.59M | if (!ret) { |
323 | 5.44k | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
324 | 5.44k | goto err; |
325 | 3.58M | } else if (ret == -1) { |
326 | 525k | return -1; |
327 | 525k | } |
328 | 3.06M | if (!cst) { |
329 | 120 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED); |
330 | 120 | goto err; |
331 | 120 | } |
332 | | |
333 | 3.06M | if (!*pval && !ASN1_item_ex_new(pval, it)) { |
334 | 0 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
335 | 0 | goto err; |
336 | 0 | } |
337 | | |
338 | 3.06M | const ASN1_AUX *aux = reinterpret_cast<const ASN1_AUX *>(it->funcs); |
339 | 3.06M | ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL; |
340 | 3.06M | if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) { |
341 | 0 | goto auxerr; |
342 | 0 | } |
343 | | |
344 | | // Free up and zero any ADB found |
345 | 11.6M | for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { |
346 | 8.56M | if (tt->flags & ASN1_TFLG_ADB_MASK) { |
347 | 1.62k | const ASN1_TEMPLATE *seqtt; |
348 | 1.62k | ASN1_VALUE **pseqval; |
349 | 1.62k | seqtt = asn1_do_adb(pval, tt, 0); |
350 | 1.62k | if (seqtt == NULL) { |
351 | 1.62k | continue; |
352 | 1.62k | } |
353 | 0 | pseqval = asn1_get_field_ptr(pval, seqtt); |
354 | 0 | ASN1_template_free(pseqval, seqtt); |
355 | 0 | } |
356 | 8.56M | } |
357 | | |
358 | | // Get each field entry |
359 | 11.3M | for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { |
360 | 8.47M | const ASN1_TEMPLATE *seqtt; |
361 | 8.47M | ASN1_VALUE **pseqval; |
362 | 8.47M | seqtt = asn1_do_adb(pval, tt, 1); |
363 | 8.47M | if (seqtt == NULL) { |
364 | 0 | goto err; |
365 | 0 | } |
366 | 8.47M | pseqval = asn1_get_field_ptr(pval, seqtt); |
367 | | // Have we ran out of data? |
368 | 8.47M | if (!len) { |
369 | 192k | break; |
370 | 192k | } |
371 | 8.28M | q = p; |
372 | | // This determines the OPTIONAL flag value. The field cannot be |
373 | | // omitted if it is the last of a SEQUENCE and there is still |
374 | | // data to be read. This isn't strictly necessary but it |
375 | | // increases efficiency in some cases. |
376 | 8.28M | if (i == (it->tcount - 1)) { |
377 | 2.85M | isopt = 0; |
378 | 5.42M | } else { |
379 | 5.42M | isopt = (seqtt->flags & ASN1_TFLG_OPTIONAL) != 0; |
380 | 5.42M | } |
381 | | // attempt to read in field, allowing each to be OPTIONAL |
382 | | |
383 | 8.28M | ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, buf, depth); |
384 | 8.28M | if (!ret) { |
385 | 18.4k | errtt = seqtt; |
386 | 18.4k | goto err; |
387 | 8.26M | } else if (ret == -1) { |
388 | | // OPTIONAL component absent. Free and zero the field. |
389 | 907k | ASN1_template_free(pseqval, seqtt); |
390 | 907k | continue; |
391 | 907k | } |
392 | | // Update length |
393 | 7.36M | len -= p - q; |
394 | 7.36M | } |
395 | | |
396 | | // Check all data read |
397 | 3.04M | if (len) { |
398 | 635 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH); |
399 | 635 | goto err; |
400 | 635 | } |
401 | | |
402 | | // If we get here we've got no more data in the SEQUENCE, however we |
403 | | // may not have read all fields so check all remaining are OPTIONAL |
404 | | // and clear any that are. |
405 | 3.25M | for (; i < it->tcount; tt++, i++) { |
406 | 212k | const ASN1_TEMPLATE *seqtt; |
407 | 212k | seqtt = asn1_do_adb(pval, tt, 1); |
408 | 212k | if (seqtt == NULL) { |
409 | 0 | goto err; |
410 | 0 | } |
411 | 212k | if (seqtt->flags & ASN1_TFLG_OPTIONAL) { |
412 | 209k | ASN1_VALUE **pseqval; |
413 | 209k | pseqval = asn1_get_field_ptr(pval, seqtt); |
414 | 209k | ASN1_template_free(pseqval, seqtt); |
415 | 209k | } else { |
416 | 3.64k | errtt = seqtt; |
417 | 3.64k | OPENSSL_PUT_ERROR(ASN1, ASN1_R_FIELD_MISSING); |
418 | 3.64k | goto err; |
419 | 3.64k | } |
420 | 212k | } |
421 | | // Save encoding |
422 | 3.04M | if (!asn1_enc_save(pval, *in, p - *in, it, buf)) { |
423 | 0 | goto auxerr; |
424 | 0 | } |
425 | 3.04M | if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) { |
426 | 0 | goto auxerr; |
427 | 0 | } |
428 | 3.04M | *in = p; |
429 | 3.04M | return 1; |
430 | 3.04M | } |
431 | | |
432 | 0 | default: |
433 | 0 | return 0; |
434 | 13.7M | } |
435 | 0 | auxerr: |
436 | 0 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR); |
437 | 30.6k | err: |
438 | 30.6k | ASN1_item_ex_free(pval, it); |
439 | 30.6k | if (errtt) { |
440 | 23.9k | ERR_add_error_data(4, "Field=", errtt->field_name, ", Type=", it->sname); |
441 | 23.9k | } else { |
442 | 6.69k | ERR_add_error_data(2, "Type=", it->sname); |
443 | 6.69k | } |
444 | 30.6k | return 0; |
445 | 0 | } |
446 | | |
447 | | int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, |
448 | | const ASN1_ITEM *it, int tag, int aclass, char opt, |
449 | 661k | CRYPTO_BUFFER *buf) { |
450 | 661k | return asn1_item_ex_d2i(pval, in, len, it, tag, aclass, opt, buf, |
451 | 661k | /*depth=*/0); |
452 | 661k | } |
453 | | |
454 | | // Templates are handled with two separate functions. One handles any |
455 | | // EXPLICIT tag and the other handles the rest. |
456 | | |
457 | | static int asn1_template_ex_d2i(ASN1_VALUE **val, const unsigned char **in, |
458 | | long inlen, const ASN1_TEMPLATE *tt, char opt, |
459 | 11.6M | CRYPTO_BUFFER *buf, int depth) { |
460 | 11.6M | int aclass; |
461 | 11.6M | int ret; |
462 | 11.6M | long len; |
463 | 11.6M | const unsigned char *p, *q; |
464 | 11.6M | if (!val) { |
465 | 0 | return 0; |
466 | 0 | } |
467 | 11.6M | uint32_t flags = tt->flags; |
468 | 11.6M | aclass = flags & ASN1_TFLG_TAG_CLASS; |
469 | | |
470 | 11.6M | p = *in; |
471 | | |
472 | | // Check if EXPLICIT tag expected |
473 | 11.6M | if (flags & ASN1_TFLG_EXPTAG) { |
474 | 679k | char cst; |
475 | | // Need to work out amount of data available to the inner content and |
476 | | // where it starts: so read in EXPLICIT header to get the info. |
477 | 679k | ret = asn1_check_tlen(&len, NULL, NULL, &cst, &p, inlen, tt->tag, aclass, |
478 | 679k | opt); |
479 | 679k | q = p; |
480 | 679k | if (!ret) { |
481 | 1.00k | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
482 | 1.00k | return 0; |
483 | 678k | } else if (ret == -1) { |
484 | 250k | return -1; |
485 | 250k | } |
486 | 428k | if (!cst) { |
487 | 108 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED); |
488 | 108 | return 0; |
489 | 108 | } |
490 | | // We've found the field so it can't be OPTIONAL now |
491 | 428k | ret = asn1_template_noexp_d2i(val, &p, len, tt, /*opt=*/0, buf, depth); |
492 | 428k | if (!ret) { |
493 | 1.86k | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
494 | 1.86k | return 0; |
495 | 1.86k | } |
496 | | // We read the field in OK so update length |
497 | 426k | len -= p - q; |
498 | | // Check for trailing data. |
499 | 426k | if (len) { |
500 | 180 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH); |
501 | 180 | goto err; |
502 | 180 | } |
503 | 10.9M | } else { |
504 | 10.9M | return asn1_template_noexp_d2i(val, in, inlen, tt, opt, buf, depth); |
505 | 10.9M | } |
506 | | |
507 | 426k | *in = p; |
508 | 426k | return 1; |
509 | | |
510 | 180 | err: |
511 | 180 | ASN1_template_free(val, tt); |
512 | 180 | return 0; |
513 | 11.6M | } |
514 | | |
515 | | static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in, |
516 | | long len, const ASN1_TEMPLATE *tt, char opt, |
517 | 11.4M | CRYPTO_BUFFER *buf, int depth) { |
518 | 11.4M | int aclass; |
519 | 11.4M | int ret; |
520 | 11.4M | const unsigned char *p; |
521 | 11.4M | if (!val) { |
522 | 0 | return 0; |
523 | 0 | } |
524 | 11.4M | uint32_t flags = tt->flags; |
525 | 11.4M | aclass = flags & ASN1_TFLG_TAG_CLASS; |
526 | | |
527 | 11.4M | p = *in; |
528 | | |
529 | 11.4M | if (flags & ASN1_TFLG_SK_MASK) { |
530 | | // SET OF, SEQUENCE OF |
531 | 1.74M | int sktag, skaclass; |
532 | | // First work out expected inner tag value |
533 | 1.74M | if (flags & ASN1_TFLG_IMPTAG) { |
534 | 3.37k | sktag = tt->tag; |
535 | 3.37k | skaclass = aclass; |
536 | 1.74M | } else { |
537 | 1.74M | skaclass = V_ASN1_UNIVERSAL; |
538 | 1.74M | if (flags & ASN1_TFLG_SET_OF) { |
539 | 1.08M | sktag = V_ASN1_SET; |
540 | 1.08M | } else { |
541 | 652k | sktag = V_ASN1_SEQUENCE; |
542 | 652k | } |
543 | 1.74M | } |
544 | | // Get the tag |
545 | 1.74M | ret = |
546 | 1.74M | asn1_check_tlen(&len, NULL, NULL, NULL, &p, len, sktag, skaclass, opt); |
547 | 1.74M | if (!ret) { |
548 | 4.49k | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
549 | 4.49k | return 0; |
550 | 1.73M | } else if (ret == -1) { |
551 | 738 | return -1; |
552 | 738 | } |
553 | 1.73M | if (!*val) { |
554 | 1.73M | *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null(); |
555 | 1.73M | } else { |
556 | | // We've got a valid STACK: free up any items present |
557 | 12 | STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val; |
558 | 12 | ASN1_VALUE *vtmp; |
559 | 12 | while (sk_ASN1_VALUE_num(sktmp) > 0) { |
560 | 0 | vtmp = sk_ASN1_VALUE_pop(sktmp); |
561 | 0 | ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item)); |
562 | 0 | } |
563 | 12 | } |
564 | | |
565 | 1.73M | if (!*val) { |
566 | 0 | goto err; |
567 | 0 | } |
568 | | |
569 | | // Read as many items as we can |
570 | 4.84M | while (len > 0) { |
571 | 3.11M | ASN1_VALUE *skfield; |
572 | 3.11M | const unsigned char *q = p; |
573 | 3.11M | skfield = NULL; |
574 | 3.11M | if (!asn1_item_ex_d2i(&skfield, &p, len, ASN1_ITEM_ptr(tt->item), |
575 | 3.11M | /*tag=*/-1, /*aclass=*/0, /*opt=*/0, buf, depth)) { |
576 | 7.69k | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
577 | 7.69k | goto err; |
578 | 7.69k | } |
579 | 3.10M | len -= p - q; |
580 | 3.10M | if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) { |
581 | 0 | ASN1_item_ex_free(&skfield, ASN1_ITEM_ptr(tt->item)); |
582 | 0 | goto err; |
583 | 0 | } |
584 | 3.10M | } |
585 | 9.66M | } else if (flags & ASN1_TFLG_IMPTAG) { |
586 | | // IMPLICIT tagging |
587 | 2.01M | ret = asn1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), tt->tag, |
588 | 2.01M | aclass, opt, buf, depth); |
589 | 2.01M | if (!ret) { |
590 | 2.41k | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
591 | 2.41k | goto err; |
592 | 2.01M | } else if (ret == -1) { |
593 | 1.71M | return -1; |
594 | 1.71M | } |
595 | 7.65M | } else { |
596 | | // Nothing special |
597 | 7.65M | ret = asn1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), /*tag=*/-1, |
598 | 7.65M | /*aclass=*/0, opt, buf, depth); |
599 | 7.65M | if (!ret) { |
600 | 14.3k | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
601 | 14.3k | goto err; |
602 | 7.64M | } else if (ret == -1) { |
603 | 487k | return -1; |
604 | 487k | } |
605 | 7.65M | } |
606 | | |
607 | 9.18M | *in = p; |
608 | 9.18M | return 1; |
609 | | |
610 | 24.4k | err: |
611 | 24.4k | ASN1_template_free(val, tt); |
612 | 24.4k | return 0; |
613 | 11.4M | } |
614 | | |
615 | | static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in, |
616 | | long inlen, const ASN1_ITEM *it, int tag, |
617 | 7.91M | int aclass, char opt) { |
618 | 7.91M | int ret = 0, utype; |
619 | 7.91M | long plen; |
620 | 7.91M | char cst; |
621 | 7.91M | const unsigned char *p; |
622 | 7.91M | const unsigned char *cont = NULL; |
623 | 7.91M | long len; |
624 | 7.91M | if (!pval) { |
625 | 0 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_NULL); |
626 | 0 | return 0; // Should never happen |
627 | 0 | } |
628 | | |
629 | 7.91M | assert(it->itype == ASN1_ITYPE_PRIMITIVE || it->itype == ASN1_ITYPE_MSTRING); |
630 | 7.91M | if (it->itype == ASN1_ITYPE_MSTRING) { |
631 | 430k | utype = tag; |
632 | 430k | tag = -1; |
633 | 7.48M | } else { |
634 | 7.48M | utype = it->utype; |
635 | 7.48M | } |
636 | | |
637 | 7.91M | if (utype == V_ASN1_ANY || utype == V_ASN1_ANY_AS_STRING) { |
638 | | // If type is ANY need to figure out type from tag |
639 | 1.65M | unsigned char oclass; |
640 | 1.65M | if (tag >= 0) { |
641 | 0 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_TAGGED_ANY); |
642 | 0 | return 0; |
643 | 0 | } |
644 | 1.65M | if (opt) { |
645 | 0 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY); |
646 | 0 | return 0; |
647 | 0 | } |
648 | 1.65M | const int is_string = utype == V_ASN1_ANY_AS_STRING; |
649 | 1.65M | p = *in; |
650 | 1.65M | ret = asn1_check_tlen(&plen, &utype, &oclass, &cst, &p, inlen, -1, 0, 0); |
651 | 1.65M | if (!ret) { |
652 | 279 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
653 | 279 | return 0; |
654 | 279 | } |
655 | 1.65M | if (!is_supported_universal_type(utype, oclass)) { |
656 | 59.2k | utype = V_ASN1_OTHER; |
657 | 59.2k | } |
658 | | // These three types are not represented as |ASN1_STRING|, so they must be |
659 | | // parsed separately and then treated as an opaque |V_ASN1_OTHER|. |
660 | 1.65M | if (is_string && (utype == V_ASN1_OBJECT || utype == V_ASN1_NULL || |
661 | 1.10M | utype == V_ASN1_BOOLEAN)) { |
662 | 2.54k | if (cst) { |
663 | 39 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_PRIMITIVE); |
664 | 39 | return 0; |
665 | 39 | } |
666 | 2.50k | CBS cbs; |
667 | 2.50k | CBS_init(&cbs, p, plen); |
668 | 2.50k | if (utype == V_ASN1_OBJECT && !CBS_is_valid_asn1_oid(&cbs)) { |
669 | 28 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING); |
670 | 28 | return 0; |
671 | 28 | } |
672 | 2.47k | if (utype == V_ASN1_NULL && CBS_len(&cbs) != 0) { |
673 | 58 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NULL_IS_WRONG_LENGTH); |
674 | 58 | return 0; |
675 | 58 | } |
676 | 2.41k | if (utype == V_ASN1_BOOLEAN) { |
677 | 545 | if (CBS_len(&cbs) != 1) { |
678 | 55 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH); |
679 | 55 | return 0; |
680 | 55 | } |
681 | 490 | uint8_t v = CBS_data(&cbs)[0]; |
682 | 490 | if (v != 0 && v != 0xff) { |
683 | 55 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); |
684 | 55 | return 0; |
685 | 55 | } |
686 | 490 | } |
687 | 2.30k | utype = V_ASN1_OTHER; |
688 | 2.30k | } |
689 | 1.65M | } |
690 | 7.91M | if (tag == -1) { |
691 | 6.43M | tag = utype; |
692 | 6.43M | aclass = V_ASN1_UNIVERSAL; |
693 | 6.43M | } |
694 | 7.91M | p = *in; |
695 | | // Check header |
696 | 7.91M | ret = asn1_check_tlen(&plen, NULL, NULL, &cst, &p, inlen, tag, aclass, opt); |
697 | 7.91M | if (!ret) { |
698 | 7.07k | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR); |
699 | 7.07k | return 0; |
700 | 7.90M | } else if (ret == -1) { |
701 | 1.67M | return -1; |
702 | 1.67M | } |
703 | 6.23M | ret = 0; |
704 | | // SEQUENCE, SET and "OTHER" are left in encoded form |
705 | 6.23M | if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) || |
706 | 6.23M | (utype == V_ASN1_OTHER)) { |
707 | | // SEQUENCE and SET must be constructed |
708 | 195k | if (utype != V_ASN1_OTHER && !cst) { |
709 | 99 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED); |
710 | 99 | return 0; |
711 | 99 | } |
712 | | |
713 | 195k | cont = *in; |
714 | 195k | len = p - cont + plen; |
715 | 195k | p += plen; |
716 | 6.03M | } else if (cst) { |
717 | | // This parser historically supported BER constructed strings. We no |
718 | | // longer do and will gradually tighten this parser into a DER |
719 | | // parser. BER types should use |CBS_asn1_ber_to_der|. |
720 | 305 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_PRIMITIVE); |
721 | 305 | return 0; |
722 | 6.03M | } else { |
723 | 6.03M | cont = p; |
724 | 6.03M | len = plen; |
725 | 6.03M | p += plen; |
726 | 6.03M | } |
727 | | |
728 | | // We now have content length and type: translate into a structure |
729 | 6.23M | if (!asn1_ex_c2i(pval, cont, len, utype, it)) { |
730 | 7.67k | goto err; |
731 | 7.67k | } |
732 | | |
733 | 6.22M | *in = p; |
734 | 6.22M | ret = 1; |
735 | 6.23M | err: |
736 | 6.23M | return ret; |
737 | 6.22M | } |
738 | | |
739 | | // Translate ASN1 content octets into a structure |
740 | | |
741 | | static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, long len, |
742 | 6.23M | int utype, const ASN1_ITEM *it) { |
743 | 6.23M | ASN1_VALUE **opval = NULL; |
744 | 6.23M | ASN1_STRING *stmp; |
745 | 6.23M | ASN1_TYPE *typ = NULL; |
746 | 6.23M | int ret = 0; |
747 | 6.23M | ASN1_INTEGER **tint; |
748 | | |
749 | | // Historically, |it->funcs| for primitive types contained an |
750 | | // |ASN1_PRIMITIVE_FUNCS| table of callbacks. |
751 | 6.23M | assert(it->funcs == NULL); |
752 | | |
753 | | // If ANY type clear type and set pointer to internal value |
754 | 6.23M | if (it->utype == V_ASN1_ANY) { |
755 | 548k | if (!*pval) { |
756 | 541k | typ = ASN1_TYPE_new(); |
757 | 541k | if (typ == NULL) { |
758 | 0 | goto err; |
759 | 0 | } |
760 | 541k | *pval = (ASN1_VALUE *)typ; |
761 | 541k | } else { |
762 | 7.16k | typ = (ASN1_TYPE *)*pval; |
763 | 7.16k | } |
764 | | |
765 | 548k | if (utype != typ->type) { |
766 | 548k | ASN1_TYPE_set(typ, utype, NULL); |
767 | 548k | } |
768 | 548k | opval = pval; |
769 | 548k | pval = &typ->value.asn1_value; |
770 | 548k | } |
771 | | |
772 | | // If implementing a type that is not represented in |ASN1_STRING|, the |
773 | | // |V_ASN1_ANY_AS_STRING| logic must be modified to redirect it to |
774 | | // |V_ASN1_OTHER|. |
775 | 6.23M | switch (utype) { |
776 | 2.46M | case V_ASN1_OBJECT: |
777 | 2.46M | if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len)) { |
778 | 380 | goto err; |
779 | 380 | } |
780 | 2.45M | break; |
781 | | |
782 | 2.45M | case V_ASN1_NULL: |
783 | 327k | if (len) { |
784 | 87 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NULL_IS_WRONG_LENGTH); |
785 | 87 | goto err; |
786 | 87 | } |
787 | 327k | *pval = (ASN1_VALUE *)1; |
788 | 327k | break; |
789 | | |
790 | 157k | case V_ASN1_BOOLEAN: |
791 | 157k | if (len != 1) { |
792 | 141 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH); |
793 | 141 | goto err; |
794 | 157k | } else { |
795 | 157k | ASN1_BOOLEAN *tbool; |
796 | 157k | tbool = (ASN1_BOOLEAN *)pval; |
797 | 157k | *tbool = *cont; |
798 | 157k | } |
799 | 157k | break; |
800 | | |
801 | 257k | case V_ASN1_BIT_STRING: |
802 | 257k | if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len)) { |
803 | 1.83k | goto err; |
804 | 1.83k | } |
805 | 256k | break; |
806 | | |
807 | 456k | case V_ASN1_INTEGER: |
808 | 555k | case V_ASN1_ENUMERATED: |
809 | 555k | tint = (ASN1_INTEGER **)pval; |
810 | 555k | if (!c2i_ASN1_INTEGER(tint, &cont, len)) { |
811 | 327 | goto err; |
812 | 327 | } |
813 | | // Fixup type to match the expected form |
814 | 555k | (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG); |
815 | 555k | break; |
816 | | |
817 | 656k | case V_ASN1_OCTET_STRING: |
818 | 659k | case V_ASN1_NUMERICSTRING: |
819 | 1.03M | case V_ASN1_PRINTABLESTRING: |
820 | 1.05M | case V_ASN1_T61STRING: |
821 | 1.05M | case V_ASN1_VIDEOTEXSTRING: |
822 | 1.34M | case V_ASN1_IA5STRING: |
823 | 1.77M | case V_ASN1_UTCTIME: |
824 | 1.77M | case V_ASN1_GENERALIZEDTIME: |
825 | 1.78M | case V_ASN1_GRAPHICSTRING: |
826 | 1.78M | case V_ASN1_VISIBLESTRING: |
827 | 1.78M | case V_ASN1_GENERALSTRING: |
828 | 1.79M | case V_ASN1_UNIVERSALSTRING: |
829 | 1.85M | case V_ASN1_BMPSTRING: |
830 | 2.27M | case V_ASN1_UTF8STRING: |
831 | 2.33M | case V_ASN1_OTHER: |
832 | 2.40M | case V_ASN1_SET: |
833 | 2.47M | case V_ASN1_SEQUENCE: { |
834 | 2.47M | CBS cbs; |
835 | 2.47M | CBS_init(&cbs, cont, (size_t)len); |
836 | 2.47M | if (utype == V_ASN1_BMPSTRING) { |
837 | 9.81M | while (CBS_len(&cbs) != 0) { |
838 | 9.75M | uint32_t c; |
839 | 9.75M | if (!CBS_get_ucs2_be(&cbs, &c)) { |
840 | 348 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BMPSTRING); |
841 | 348 | goto err; |
842 | 348 | } |
843 | 9.75M | } |
844 | 61.5k | } |
845 | 2.47M | if (utype == V_ASN1_UNIVERSALSTRING) { |
846 | 54.1k | while (CBS_len(&cbs) != 0) { |
847 | 43.3k | uint32_t c; |
848 | 43.3k | if (!CBS_get_utf32_be(&cbs, &c)) { |
849 | 638 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_UNIVERSALSTRING); |
850 | 638 | goto err; |
851 | 638 | } |
852 | 43.3k | } |
853 | 11.4k | } |
854 | 2.47M | if (utype == V_ASN1_UTF8STRING) { |
855 | 7.19M | while (CBS_len(&cbs) != 0) { |
856 | 6.78M | uint32_t c; |
857 | 6.78M | if (!CBS_get_utf8(&cbs, &c)) { |
858 | 680 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_UTF8STRING); |
859 | 680 | goto err; |
860 | 680 | } |
861 | 6.78M | } |
862 | 417k | } |
863 | 2.47M | if (utype == V_ASN1_UTCTIME) { |
864 | 431k | if (!CBS_parse_utc_time(&cbs, NULL, /*allow_timezone_offset=*/1)) { |
865 | 1.48k | OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_TIME_FORMAT); |
866 | 1.48k | goto err; |
867 | 1.48k | } |
868 | 431k | } |
869 | 2.46M | if (utype == V_ASN1_GENERALIZEDTIME) { |
870 | 4.37k | if (!CBS_parse_generalized_time(&cbs, NULL, |
871 | 4.37k | /*allow_timezone_offset=*/0)) { |
872 | 1.75k | OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_TIME_FORMAT); |
873 | 1.75k | goto err; |
874 | 1.75k | } |
875 | 4.37k | } |
876 | | // TODO(https://crbug.com/boringssl/427): Check other string types. |
877 | | |
878 | | // All based on ASN1_STRING and handled the same |
879 | 2.46M | if (!*pval) { |
880 | 408k | stmp = ASN1_STRING_type_new(utype); |
881 | 408k | if (!stmp) { |
882 | 0 | goto err; |
883 | 0 | } |
884 | 408k | *pval = (ASN1_VALUE *)stmp; |
885 | 2.05M | } else { |
886 | 2.05M | stmp = (ASN1_STRING *)*pval; |
887 | 2.05M | stmp->type = utype; |
888 | 2.05M | } |
889 | 2.46M | if (!ASN1_STRING_set(stmp, cont, len)) { |
890 | 0 | ASN1_STRING_free(stmp); |
891 | 0 | *pval = NULL; |
892 | 0 | goto err; |
893 | 0 | } |
894 | 2.46M | break; |
895 | 2.46M | } |
896 | | |
897 | 2.46M | default: |
898 | 0 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); |
899 | 0 | goto err; |
900 | 6.23M | } |
901 | | // If ASN1_ANY and NULL type fix up value |
902 | 6.22M | if (typ && (utype == V_ASN1_NULL)) { |
903 | 327k | typ->value.ptr = NULL; |
904 | 327k | } |
905 | | |
906 | 6.22M | ret = 1; |
907 | 6.23M | err: |
908 | 6.23M | if (!ret) { |
909 | 7.67k | ASN1_TYPE_free(typ); |
910 | 7.67k | if (opval) { |
911 | 2.46k | *opval = NULL; |
912 | 2.46k | } |
913 | 7.67k | } |
914 | 6.23M | return ret; |
915 | 6.22M | } |
916 | | |
917 | | // Check an ASN1 tag and length: a bit like ASN1_get_object but it |
918 | | // checks the expected tag. |
919 | | |
920 | | static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, |
921 | | char *cst, const unsigned char **in, long len, |
922 | 16.0M | int exptag, int expclass, char opt) { |
923 | 16.0M | int i; |
924 | 16.0M | int ptag, pclass; |
925 | 16.0M | long plen; |
926 | 16.0M | const unsigned char *p; |
927 | 16.0M | p = *in; |
928 | | |
929 | 16.0M | i = ASN1_get_object(&p, &plen, &ptag, &pclass, len); |
930 | 16.0M | if (i & 0x80) { |
931 | 10.7k | OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_OBJECT_HEADER); |
932 | 10.7k | return 0; |
933 | 10.7k | } |
934 | 16.0M | if (exptag >= 0) { |
935 | 13.8M | if ((exptag != ptag) || (expclass != pclass)) { |
936 | | // If type is OPTIONAL, not an error: indicate missing type. |
937 | 2.46M | if (opt) { |
938 | 2.45M | return -1; |
939 | 2.45M | } |
940 | 7.62k | OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TAG); |
941 | 7.62k | return 0; |
942 | 2.46M | } |
943 | 13.8M | } |
944 | | |
945 | 13.5M | if (cst) { |
946 | 11.3M | *cst = i & V_ASN1_CONSTRUCTED; |
947 | 11.3M | } |
948 | | |
949 | 13.5M | if (olen) { |
950 | 13.1M | *olen = plen; |
951 | 13.1M | } |
952 | | |
953 | 13.5M | if (oclass) { |
954 | 2.08M | *oclass = pclass; |
955 | 2.08M | } |
956 | | |
957 | 13.5M | if (otag) { |
958 | 2.08M | *otag = ptag; |
959 | 2.08M | } |
960 | | |
961 | 13.5M | *in = p; |
962 | 13.5M | return 1; |
963 | 16.0M | } |