/src/openssl30/crypto/asn1/tasn_dec.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 <string.h> |
12 | | #include <openssl/asn1.h> |
13 | | #include <openssl/asn1t.h> |
14 | | #include <openssl/objects.h> |
15 | | #include <openssl/buffer.h> |
16 | | #include <openssl/err.h> |
17 | | #include "crypto/asn1.h" |
18 | | #include "internal/numbers.h" |
19 | | #include "asn1_local.h" |
20 | | |
21 | | /* |
22 | | * Constructed types with a recursive definition (such as can be found in PKCS7) |
23 | | * could eventually exceed the stack given malicious input with excessive |
24 | | * recursion. Therefore we limit the stack depth. This is the maximum number of |
25 | | * recursive invocations of asn1_item_embed_d2i(). |
26 | | */ |
27 | 363M | #define ASN1_MAX_CONSTRUCTED_NEST 30 |
28 | | |
29 | | static int asn1_check_eoc(const unsigned char **in, long len); |
30 | | static int asn1_find_end(const unsigned char **in, long len, char inf); |
31 | | |
32 | | static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len, |
33 | | char inf, int tag, int aclass, int depth); |
34 | | |
35 | | static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen); |
36 | | |
37 | | static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, |
38 | | char *inf, char *cst, |
39 | | const unsigned char **in, long len, |
40 | | int exptag, int expclass, char opt, ASN1_TLC *ctx); |
41 | | |
42 | | static int asn1_template_ex_d2i(ASN1_VALUE **pval, |
43 | | const unsigned char **in, long len, |
44 | | const ASN1_TEMPLATE *tt, char opt, |
45 | | ASN1_TLC *ctx, int depth, OSSL_LIB_CTX *libctx, |
46 | | const char *propq); |
47 | | static int asn1_template_noexp_d2i(ASN1_VALUE **val, |
48 | | const unsigned char **in, long len, |
49 | | const ASN1_TEMPLATE *tt, char opt, |
50 | | ASN1_TLC *ctx, int depth, |
51 | | OSSL_LIB_CTX *libctx, const char *propq); |
52 | | static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, |
53 | | const unsigned char **in, long len, |
54 | | const ASN1_ITEM *it, |
55 | | int tag, int aclass, char opt, |
56 | | ASN1_TLC *ctx); |
57 | | static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, |
58 | | int utype, char *free_cont, const ASN1_ITEM *it); |
59 | | |
60 | | /* Table to convert tags to bit values, used for MSTRING type */ |
61 | | static const unsigned long tag2bit[32] = { |
62 | | /* tags 0 - 3 */ |
63 | | 0, |
64 | | 0, |
65 | | 0, |
66 | | B_ASN1_BIT_STRING, |
67 | | /* tags 4- 7 */ |
68 | | B_ASN1_OCTET_STRING, |
69 | | 0, |
70 | | 0, |
71 | | B_ASN1_UNKNOWN, |
72 | | /* tags 8-11 */ |
73 | | B_ASN1_UNKNOWN, |
74 | | B_ASN1_UNKNOWN, |
75 | | 0, |
76 | | B_ASN1_UNKNOWN, |
77 | | /* tags 12-15 */ |
78 | | B_ASN1_UTF8STRING, |
79 | | B_ASN1_UNKNOWN, |
80 | | B_ASN1_UNKNOWN, |
81 | | B_ASN1_UNKNOWN, |
82 | | /* tags 16-19 */ |
83 | | B_ASN1_SEQUENCE, |
84 | | 0, |
85 | | B_ASN1_NUMERICSTRING, |
86 | | B_ASN1_PRINTABLESTRING, |
87 | | /* tags 20-22 */ |
88 | | B_ASN1_T61STRING, |
89 | | B_ASN1_VIDEOTEXSTRING, |
90 | | B_ASN1_IA5STRING, |
91 | | /* tags 23-24 */ |
92 | | B_ASN1_UTCTIME, |
93 | | B_ASN1_GENERALIZEDTIME, |
94 | | /* tags 25-27 */ |
95 | | B_ASN1_GRAPHICSTRING, |
96 | | B_ASN1_ISO64STRING, |
97 | | B_ASN1_GENERALSTRING, |
98 | | /* tags 28-31 */ |
99 | | B_ASN1_UNIVERSALSTRING, |
100 | | B_ASN1_UNKNOWN, |
101 | | B_ASN1_BMPSTRING, |
102 | | B_ASN1_UNKNOWN, |
103 | | }; |
104 | | |
105 | | unsigned long ASN1_tag2bit(int tag) |
106 | 41.4M | { |
107 | 41.4M | if ((tag < 0) || (tag > 30)) |
108 | 5.32k | return 0; |
109 | 41.4M | return tag2bit[tag]; |
110 | 41.4M | } |
111 | | |
112 | | /* Macro to initialize and invalidate the cache */ |
113 | | |
114 | | #define asn1_tlc_clear(c) \ |
115 | 337M | do { \ |
116 | 337M | if ((c) != NULL) \ |
117 | 337M | (c)->valid = 0; \ |
118 | 337M | } while (0) |
119 | | /* Version to avoid compiler warning about 'c' always non-NULL */ |
120 | | #define asn1_tlc_clear_nc(c) \ |
121 | 24.0M | do { \ |
122 | 24.0M | (c)->valid = 0; \ |
123 | 24.0M | } while (0) |
124 | | |
125 | | /* |
126 | | * Decode an ASN1 item, this currently behaves just like a standard 'd2i' |
127 | | * function. 'in' points to a buffer to read the data from, in future we |
128 | | * will have more advanced versions that can input data a piece at a time and |
129 | | * this will simply be a special case. |
130 | | */ |
131 | | |
132 | | static int asn1_item_ex_d2i_intern(ASN1_VALUE **pval, const unsigned char **in, |
133 | | long len, const ASN1_ITEM *it, int tag, |
134 | | int aclass, char opt, ASN1_TLC *ctx, |
135 | | OSSL_LIB_CTX *libctx, const char *propq) |
136 | 26.5M | { |
137 | 26.5M | int rv; |
138 | | |
139 | 26.5M | if (pval == NULL || it == NULL) { |
140 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER); |
141 | 0 | return 0; |
142 | 0 | } |
143 | 26.5M | rv = asn1_item_embed_d2i(pval, in, len, it, tag, aclass, opt, ctx, 0, |
144 | 26.5M | libctx, propq); |
145 | 26.5M | if (rv <= 0) |
146 | 16.9M | ASN1_item_ex_free(pval, it); |
147 | 26.5M | return rv; |
148 | 26.5M | } |
149 | | |
150 | | int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, |
151 | | const ASN1_ITEM *it, |
152 | | int tag, int aclass, char opt, ASN1_TLC *ctx) |
153 | 2.52M | { |
154 | 2.52M | return asn1_item_ex_d2i_intern(pval, in, len, it, tag, aclass, opt, ctx, |
155 | 2.52M | NULL, NULL); |
156 | 2.52M | } |
157 | | |
158 | | ASN1_VALUE *ASN1_item_d2i_ex(ASN1_VALUE **pval, |
159 | | const unsigned char **in, long len, |
160 | | const ASN1_ITEM *it, OSSL_LIB_CTX *libctx, |
161 | | const char *propq) |
162 | 24.0M | { |
163 | 24.0M | ASN1_TLC c; |
164 | 24.0M | ASN1_VALUE *ptmpval = NULL; |
165 | | |
166 | 24.0M | if (pval == NULL) |
167 | 21.4M | pval = &ptmpval; |
168 | 24.0M | asn1_tlc_clear_nc(&c); |
169 | 24.0M | if (asn1_item_ex_d2i_intern(pval, in, len, it, -1, 0, 0, &c, libctx, |
170 | 24.0M | propq) |
171 | 24.0M | > 0) |
172 | 7.45M | return *pval; |
173 | 16.5M | return NULL; |
174 | 24.0M | } |
175 | | |
176 | | ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, |
177 | | const unsigned char **in, long len, |
178 | | const ASN1_ITEM *it) |
179 | 22.6M | { |
180 | 22.6M | return ASN1_item_d2i_ex(pval, in, len, it, NULL, NULL); |
181 | 22.6M | } |
182 | | |
183 | | /* |
184 | | * Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and |
185 | | * tag mismatch return -1 to handle OPTIONAL |
186 | | */ |
187 | | |
188 | | int asn1_item_embed_d2i(ASN1_VALUE **pval, const unsigned char **in, |
189 | | long len, const ASN1_ITEM *it, |
190 | | int tag, int aclass, char opt, ASN1_TLC *ctx, |
191 | | int depth, OSSL_LIB_CTX *libctx, |
192 | | const char *propq) |
193 | 363M | { |
194 | 363M | const ASN1_TEMPLATE *tt, *errtt = NULL; |
195 | 363M | const ASN1_EXTERN_FUNCS *ef; |
196 | 363M | const ASN1_AUX *aux; |
197 | 363M | ASN1_aux_cb *asn1_cb; |
198 | 363M | const unsigned char *p = NULL, *q; |
199 | 363M | unsigned char oclass; |
200 | 363M | char seq_eoc, seq_nolen, cst, isopt; |
201 | 363M | long tmplen; |
202 | 363M | int i; |
203 | 363M | int otag; |
204 | 363M | int ret = 0; |
205 | 363M | ASN1_VALUE **pchptr; |
206 | | |
207 | 363M | if (pval == NULL || it == NULL) { |
208 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER); |
209 | 0 | return 0; |
210 | 0 | } |
211 | 363M | if (len <= 0) { |
212 | 66.6k | ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL); |
213 | 66.6k | return 0; |
214 | 66.6k | } |
215 | 363M | aux = it->funcs; |
216 | 363M | if (aux && aux->asn1_cb) |
217 | 12.6M | asn1_cb = aux->asn1_cb; |
218 | 350M | else |
219 | 350M | asn1_cb = 0; |
220 | | |
221 | 363M | if (++depth > ASN1_MAX_CONSTRUCTED_NEST) { |
222 | 15 | ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_TOO_DEEP); |
223 | 15 | goto err; |
224 | 15 | } |
225 | | |
226 | 363M | switch (it->itype) { |
227 | 271M | case ASN1_ITYPE_PRIMITIVE: |
228 | 271M | if (it->templates) { |
229 | | /* |
230 | | * tagging or OPTIONAL is currently illegal on an item template |
231 | | * because the flags can't get passed down. In practice this |
232 | | * isn't a problem: we include the relevant flags from the item |
233 | | * template in the template itself. |
234 | | */ |
235 | 137M | if ((tag != -1) || opt) { |
236 | 0 | ERR_raise(ERR_LIB_ASN1, |
237 | 0 | ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE); |
238 | 0 | goto err; |
239 | 0 | } |
240 | 137M | return asn1_template_ex_d2i(pval, in, len, it->templates, opt, ctx, |
241 | 137M | depth, libctx, propq); |
242 | 137M | } |
243 | 134M | return asn1_d2i_ex_primitive(pval, in, len, it, |
244 | 134M | tag, aclass, opt, ctx); |
245 | | |
246 | 21.1M | case ASN1_ITYPE_MSTRING: |
247 | | /* |
248 | | * It never makes sense for multi-strings to have implicit tagging, so |
249 | | * if tag != -1, then this looks like an error in the template. |
250 | | */ |
251 | 21.1M | if (tag != -1) { |
252 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE); |
253 | 0 | goto err; |
254 | 0 | } |
255 | | |
256 | 21.1M | p = *in; |
257 | | /* Just read in tag and class */ |
258 | 21.1M | ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL, |
259 | 21.1M | &p, len, -1, 0, 1, ctx); |
260 | 21.1M | if (!ret) { |
261 | 25.2k | ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR); |
262 | 25.2k | goto err; |
263 | 25.2k | } |
264 | | |
265 | | /* Must be UNIVERSAL class */ |
266 | 21.0M | if (oclass != V_ASN1_UNIVERSAL) { |
267 | | /* If OPTIONAL, assume this is OK */ |
268 | 138k | if (opt) |
269 | 102k | return -1; |
270 | 138k | ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL); |
271 | 35.5k | goto err; |
272 | 138k | } |
273 | | |
274 | | /* Check tag matches bit map */ |
275 | 20.9M | if (!(ASN1_tag2bit(otag) & it->utype)) { |
276 | | /* If OPTIONAL, assume this is OK */ |
277 | 424k | if (opt) |
278 | 13.5k | return -1; |
279 | 424k | ERR_raise(ERR_LIB_ASN1, ASN1_R_MSTRING_WRONG_TAG); |
280 | 410k | goto err; |
281 | 424k | } |
282 | 20.5M | return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0, ctx); |
283 | | |
284 | 4.97M | case ASN1_ITYPE_EXTERN: |
285 | | /* Use new style d2i */ |
286 | 4.97M | ef = it->funcs; |
287 | 4.97M | if (ef->asn1_ex_d2i_ex != NULL) |
288 | 2.45M | return ef->asn1_ex_d2i_ex(pval, in, len, it, tag, aclass, opt, ctx, |
289 | 2.45M | libctx, propq); |
290 | 2.52M | return ef->asn1_ex_d2i(pval, in, len, it, tag, aclass, opt, ctx); |
291 | | |
292 | 6.35M | case ASN1_ITYPE_CHOICE: |
293 | | /* |
294 | | * It never makes sense for CHOICE types to have implicit tagging, so |
295 | | * if tag != -1, then this looks like an error in the template. |
296 | | */ |
297 | 6.35M | if (tag != -1) { |
298 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE); |
299 | 0 | goto err; |
300 | 0 | } |
301 | | |
302 | 6.35M | if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) |
303 | 0 | goto auxerr; |
304 | 6.35M | if (*pval) { |
305 | | /* Free up and zero CHOICE value if initialised */ |
306 | 778k | i = ossl_asn1_get_choice_selector(pval, it); |
307 | 778k | if ((i >= 0) && (i < it->tcount)) { |
308 | 0 | tt = it->templates + i; |
309 | 0 | pchptr = ossl_asn1_get_field_ptr(pval, tt); |
310 | 0 | ossl_asn1_template_free(pchptr, tt); |
311 | 0 | ossl_asn1_set_choice_selector(pval, -1, it); |
312 | 0 | } |
313 | 5.57M | } else if (!ossl_asn1_item_ex_new_intern(pval, it, libctx, propq)) { |
314 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR); |
315 | 0 | goto err; |
316 | 0 | } |
317 | | /* CHOICE type, try each possibility in turn */ |
318 | 6.35M | p = *in; |
319 | 21.3M | for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { |
320 | 20.6M | pchptr = ossl_asn1_get_field_ptr(pval, tt); |
321 | | /* |
322 | | * We mark field as OPTIONAL so its absence can be recognised. |
323 | | */ |
324 | 20.6M | ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, ctx, depth, |
325 | 20.6M | libctx, propq); |
326 | | /* If field not present, try the next one */ |
327 | 20.6M | if (ret == -1) |
328 | 15.0M | continue; |
329 | | /* If positive return, read OK, break loop */ |
330 | 5.64M | if (ret > 0) |
331 | 4.60M | break; |
332 | | /* |
333 | | * Must be an ASN1 parsing error. |
334 | | * Free up any partial choice value |
335 | | */ |
336 | 1.03M | ossl_asn1_template_free(pchptr, tt); |
337 | 1.03M | errtt = tt; |
338 | 1.03M | ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR); |
339 | 1.03M | goto err; |
340 | 5.64M | } |
341 | | |
342 | | /* Did we fall off the end without reading anything? */ |
343 | 5.31M | if (i == it->tcount) { |
344 | | /* If OPTIONAL, this is OK */ |
345 | 708k | if (opt) { |
346 | | /* Free and zero it */ |
347 | 25.2k | ASN1_item_ex_free(pval, it); |
348 | 25.2k | return -1; |
349 | 25.2k | } |
350 | 708k | ERR_raise(ERR_LIB_ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE); |
351 | 683k | goto err; |
352 | 708k | } |
353 | | |
354 | 4.60M | ossl_asn1_set_choice_selector(pval, i, it); |
355 | | |
356 | 4.60M | if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) |
357 | 0 | goto auxerr; |
358 | 4.60M | *in = p; |
359 | 4.60M | return 1; |
360 | | |
361 | 842k | case ASN1_ITYPE_NDEF_SEQUENCE: |
362 | 59.3M | case ASN1_ITYPE_SEQUENCE: |
363 | 59.3M | p = *in; |
364 | 59.3M | tmplen = len; |
365 | | |
366 | | /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */ |
367 | 59.3M | if (tag == -1) { |
368 | 54.1M | tag = V_ASN1_SEQUENCE; |
369 | 54.1M | aclass = V_ASN1_UNIVERSAL; |
370 | 54.1M | } |
371 | | /* Get SEQUENCE length and update len, p */ |
372 | 59.3M | ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst, |
373 | 59.3M | &p, len, tag, aclass, opt, ctx); |
374 | 59.3M | if (!ret) { |
375 | 4.62M | ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR); |
376 | 4.62M | goto err; |
377 | 54.7M | } else if (ret == -1) |
378 | 5.65M | return -1; |
379 | 49.0M | if (aux && (aux->flags & ASN1_AFLG_BROKEN)) { |
380 | 0 | len = tmplen - (p - *in); |
381 | 0 | seq_nolen = 1; |
382 | 0 | } |
383 | | /* If indefinite we don't do a length check */ |
384 | 49.0M | else |
385 | 49.0M | seq_nolen = seq_eoc; |
386 | 49.0M | if (!cst) { |
387 | 120k | ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED); |
388 | 120k | goto err; |
389 | 120k | } |
390 | | |
391 | 48.9M | if (*pval == NULL |
392 | 37.8M | && !ossl_asn1_item_ex_new_intern(pval, it, libctx, propq)) { |
393 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR); |
394 | 0 | goto err; |
395 | 0 | } |
396 | | |
397 | 48.9M | if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) |
398 | 0 | goto auxerr; |
399 | | |
400 | | /* Free up and zero any ADB found */ |
401 | 185M | for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { |
402 | 136M | if (tt->flags & ASN1_TFLG_ADB_MASK) { |
403 | 744k | const ASN1_TEMPLATE *seqtt; |
404 | 744k | ASN1_VALUE **pseqval; |
405 | 744k | seqtt = ossl_asn1_do_adb(*pval, tt, 0); |
406 | 744k | if (seqtt == NULL) |
407 | 617k | continue; |
408 | 127k | pseqval = ossl_asn1_get_field_ptr(pval, seqtt); |
409 | 127k | ossl_asn1_template_free(pseqval, seqtt); |
410 | 127k | } |
411 | 136M | } |
412 | | |
413 | | /* Get each field entry */ |
414 | 141M | for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { |
415 | 108M | const ASN1_TEMPLATE *seqtt; |
416 | 108M | ASN1_VALUE **pseqval; |
417 | 108M | seqtt = ossl_asn1_do_adb(*pval, tt, 1); |
418 | 108M | if (seqtt == NULL) |
419 | 0 | goto err; |
420 | 108M | pseqval = ossl_asn1_get_field_ptr(pval, seqtt); |
421 | | /* Have we ran out of data? */ |
422 | 108M | if (!len) |
423 | 2.64M | break; |
424 | 106M | q = p; |
425 | 106M | if (asn1_check_eoc(&p, len)) { |
426 | 3.19M | if (!seq_eoc) { |
427 | 35.5k | ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC); |
428 | 35.5k | goto err; |
429 | 35.5k | } |
430 | 3.16M | len -= p - q; |
431 | 3.16M | seq_eoc = 0; |
432 | 3.16M | break; |
433 | 3.19M | } |
434 | | /* |
435 | | * This determines the OPTIONAL flag value. The field cannot be |
436 | | * omitted if it is the last of a SEQUENCE and there is still |
437 | | * data to be read. This isn't strictly necessary but it |
438 | | * increases efficiency in some cases. |
439 | | */ |
440 | 102M | if (i == (it->tcount - 1)) |
441 | 34.9M | isopt = 0; |
442 | 67.9M | else |
443 | 67.9M | isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL); |
444 | | /* |
445 | | * attempt to read in field, allowing each to be OPTIONAL |
446 | | */ |
447 | | |
448 | 102M | ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, ctx, |
449 | 102M | depth, libctx, propq); |
450 | 102M | if (!ret) { |
451 | 9.90M | errtt = seqtt; |
452 | 9.90M | goto err; |
453 | 92.9M | } else if (ret == -1) { |
454 | | /* |
455 | | * OPTIONAL component absent. Free and zero the field. |
456 | | */ |
457 | 9.83M | ossl_asn1_template_free(pseqval, seqtt); |
458 | 9.83M | continue; |
459 | 9.83M | } |
460 | | /* Update length */ |
461 | 83.1M | len -= p - q; |
462 | 83.1M | } |
463 | | |
464 | | /* Check for EOC if expecting one */ |
465 | 39.0M | if (seq_eoc && !asn1_check_eoc(&p, len)) { |
466 | 181k | ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC); |
467 | 181k | goto err; |
468 | 181k | } |
469 | | /* Check all data read */ |
470 | 38.8M | if (!seq_nolen && len) { |
471 | 35.8k | ERR_raise(ERR_LIB_ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH); |
472 | 35.8k | goto err; |
473 | 35.8k | } |
474 | | |
475 | | /* |
476 | | * If we get here we've got no more data in the SEQUENCE, however we |
477 | | * may not have read all fields so check all remaining are OPTIONAL |
478 | | * and clear any that are. |
479 | | */ |
480 | 50.8M | for (; i < it->tcount; tt++, i++) { |
481 | 12.3M | const ASN1_TEMPLATE *seqtt; |
482 | 12.3M | seqtt = ossl_asn1_do_adb(*pval, tt, 1); |
483 | 12.3M | if (seqtt == NULL) |
484 | 0 | goto err; |
485 | 12.3M | if (seqtt->flags & ASN1_TFLG_OPTIONAL) { |
486 | 12.0M | ASN1_VALUE **pseqval; |
487 | 12.0M | pseqval = ossl_asn1_get_field_ptr(pval, seqtt); |
488 | 12.0M | ossl_asn1_template_free(pseqval, seqtt); |
489 | 12.0M | } else { |
490 | 283k | errtt = seqtt; |
491 | 283k | ERR_raise(ERR_LIB_ASN1, ASN1_R_FIELD_MISSING); |
492 | 283k | goto err; |
493 | 283k | } |
494 | 12.3M | } |
495 | | /* Save encoding */ |
496 | 38.5M | if (!ossl_asn1_enc_save(pval, *in, p - *in, it)) |
497 | 0 | goto auxerr; |
498 | 38.5M | if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) |
499 | 20.2k | goto auxerr; |
500 | 38.4M | *in = p; |
501 | 38.4M | return 1; |
502 | | |
503 | 0 | default: |
504 | 0 | return 0; |
505 | 363M | } |
506 | 20.2k | auxerr: |
507 | 20.2k | ERR_raise(ERR_LIB_ASN1, ASN1_R_AUX_ERROR); |
508 | 17.4M | err: |
509 | 17.4M | if (errtt) |
510 | 11.2M | ERR_add_error_data(4, "Field=", errtt->field_name, |
511 | 11.2M | ", Type=", it->sname); |
512 | 6.17M | else |
513 | 6.17M | ERR_add_error_data(2, "Type=", it->sname); |
514 | 17.4M | return 0; |
515 | 20.2k | } |
516 | | |
517 | | /* |
518 | | * Templates are handled with two separate functions. One handles any |
519 | | * EXPLICIT tag and the other handles the rest. |
520 | | */ |
521 | | |
522 | | static int asn1_template_ex_d2i(ASN1_VALUE **val, |
523 | | const unsigned char **in, long inlen, |
524 | | const ASN1_TEMPLATE *tt, char opt, |
525 | | ASN1_TLC *ctx, int depth, |
526 | | OSSL_LIB_CTX *libctx, const char *propq) |
527 | 260M | { |
528 | 260M | int flags, aclass; |
529 | 260M | int ret; |
530 | 260M | long len; |
531 | 260M | const unsigned char *p, *q; |
532 | 260M | char exp_eoc; |
533 | 260M | if (!val) |
534 | 0 | return 0; |
535 | 260M | flags = tt->flags; |
536 | 260M | aclass = flags & ASN1_TFLG_TAG_CLASS; |
537 | | |
538 | 260M | p = *in; |
539 | | |
540 | | /* Check if EXPLICIT tag expected */ |
541 | 260M | if (flags & ASN1_TFLG_EXPTAG) { |
542 | 7.04M | char cst; |
543 | | /* |
544 | | * Need to work out amount of data available to the inner content and |
545 | | * where it starts: so read in EXPLICIT header to get the info. |
546 | | */ |
547 | 7.04M | ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst, |
548 | 7.04M | &p, inlen, tt->tag, aclass, opt, ctx); |
549 | 7.04M | q = p; |
550 | 7.04M | if (!ret) { |
551 | 381k | ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR); |
552 | 381k | return 0; |
553 | 6.66M | } else if (ret == -1) |
554 | 4.83M | return -1; |
555 | 1.82M | if (!cst) { |
556 | 6.21k | ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED); |
557 | 6.21k | return 0; |
558 | 6.21k | } |
559 | | /* We've found the field so it can't be OPTIONAL now */ |
560 | 1.82M | ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx, depth, libctx, |
561 | 1.82M | propq); |
562 | 1.82M | if (!ret) { |
563 | 340k | ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR); |
564 | 340k | return 0; |
565 | 340k | } |
566 | | /* We read the field in OK so update length */ |
567 | 1.48M | len -= p - q; |
568 | 1.48M | if (exp_eoc) { |
569 | | /* If NDEF we must have an EOC here */ |
570 | 448k | if (!asn1_check_eoc(&p, len)) { |
571 | 55.2k | ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC); |
572 | 55.2k | goto err; |
573 | 55.2k | } |
574 | 1.03M | } else { |
575 | | /* |
576 | | * Otherwise we must hit the EXPLICIT tag end or its an error |
577 | | */ |
578 | 1.03M | if (len) { |
579 | 4.68k | ERR_raise(ERR_LIB_ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH); |
580 | 4.68k | goto err; |
581 | 4.68k | } |
582 | 1.03M | } |
583 | 1.48M | } else |
584 | 253M | return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx, depth, |
585 | 253M | libctx, propq); |
586 | | |
587 | 1.42M | *in = p; |
588 | 1.42M | return 1; |
589 | | |
590 | 59.9k | err: |
591 | 59.9k | return 0; |
592 | 260M | } |
593 | | |
594 | | static int asn1_template_noexp_d2i(ASN1_VALUE **val, |
595 | | const unsigned char **in, long len, |
596 | | const ASN1_TEMPLATE *tt, char opt, |
597 | | ASN1_TLC *ctx, int depth, |
598 | | OSSL_LIB_CTX *libctx, const char *propq) |
599 | 255M | { |
600 | 255M | int flags, aclass; |
601 | 255M | int ret; |
602 | 255M | ASN1_VALUE *tval; |
603 | 255M | const unsigned char *p, *q; |
604 | 255M | if (!val) |
605 | 0 | return 0; |
606 | 255M | flags = tt->flags; |
607 | 255M | aclass = flags & ASN1_TFLG_TAG_CLASS; |
608 | | |
609 | 255M | p = *in; |
610 | | |
611 | | /* |
612 | | * If field is embedded then val needs fixing so it is a pointer to |
613 | | * a pointer to a field. |
614 | | */ |
615 | 255M | if (tt->flags & ASN1_TFLG_EMBED) { |
616 | 12.2M | tval = (ASN1_VALUE *)val; |
617 | 12.2M | val = &tval; |
618 | 12.2M | } |
619 | | |
620 | 255M | if (flags & ASN1_TFLG_SK_MASK) { |
621 | | /* SET OF, SEQUENCE OF */ |
622 | 140M | int sktag, skaclass; |
623 | 140M | char sk_eoc; |
624 | | /* First work out expected inner tag value */ |
625 | 140M | if (flags & ASN1_TFLG_IMPTAG) { |
626 | 1.48M | sktag = tt->tag; |
627 | 1.48M | skaclass = aclass; |
628 | 139M | } else { |
629 | 139M | skaclass = V_ASN1_UNIVERSAL; |
630 | 139M | if (flags & ASN1_TFLG_SET_OF) |
631 | 132M | sktag = V_ASN1_SET; |
632 | 6.78M | else |
633 | 6.78M | sktag = V_ASN1_SEQUENCE; |
634 | 139M | } |
635 | | /* Get the tag */ |
636 | 140M | ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL, |
637 | 140M | &p, len, sktag, skaclass, opt, ctx); |
638 | 140M | if (!ret) { |
639 | 1.40M | ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR); |
640 | 1.40M | return 0; |
641 | 139M | } else if (ret == -1) |
642 | 832k | return -1; |
643 | 138M | if (*val == NULL) |
644 | 137M | *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null(); |
645 | 581k | else { |
646 | | /* |
647 | | * We've got a valid STACK: free up any items present |
648 | | */ |
649 | 581k | STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val; |
650 | 581k | ASN1_VALUE *vtmp; |
651 | 581k | while (sk_ASN1_VALUE_num(sktmp) > 0) { |
652 | 0 | vtmp = sk_ASN1_VALUE_pop(sktmp); |
653 | 0 | ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item)); |
654 | 0 | } |
655 | 581k | } |
656 | | |
657 | 138M | if (*val == NULL) { |
658 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
659 | 0 | goto err; |
660 | 0 | } |
661 | | |
662 | | /* Read as many items as we can */ |
663 | 356M | while (len > 0) { |
664 | 225M | ASN1_VALUE *skfield; |
665 | 225M | q = p; |
666 | | /* See if EOC found */ |
667 | 225M | if (asn1_check_eoc(&p, len)) { |
668 | 5.83M | if (!sk_eoc) { |
669 | 11.5k | ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC); |
670 | 11.5k | goto err; |
671 | 11.5k | } |
672 | 5.82M | len -= p - q; |
673 | 5.82M | sk_eoc = 0; |
674 | 5.82M | break; |
675 | 5.83M | } |
676 | 219M | skfield = NULL; |
677 | 219M | if (asn1_item_embed_d2i(&skfield, &p, len, |
678 | 219M | ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx, |
679 | 219M | depth, libctx, propq) |
680 | 219M | <= 0) { |
681 | 2.05M | ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR); |
682 | | /* |skfield| may be partially allocated despite failure. */ |
683 | 2.05M | ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item)); |
684 | 2.05M | goto err; |
685 | 2.05M | } |
686 | 217M | len -= p - q; |
687 | 217M | if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) { |
688 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
689 | 0 | ASN1_item_free(skfield, ASN1_ITEM_ptr(tt->item)); |
690 | 0 | goto err; |
691 | 0 | } |
692 | 217M | } |
693 | 136M | if (sk_eoc) { |
694 | 14.3k | ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC); |
695 | 14.3k | goto err; |
696 | 14.3k | } |
697 | 136M | } else if (flags & ASN1_TFLG_IMPTAG) { |
698 | | /* IMPLICIT tagging */ |
699 | 17.1M | ret = asn1_item_embed_d2i(val, &p, len, |
700 | 17.1M | ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, |
701 | 17.1M | ctx, depth, libctx, propq); |
702 | 17.1M | if (!ret) { |
703 | 548k | ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR); |
704 | 548k | goto err; |
705 | 16.6M | } else if (ret == -1) |
706 | 12.8M | return -1; |
707 | 97.4M | } else { |
708 | | /* Nothing special */ |
709 | 97.4M | ret = asn1_item_embed_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), |
710 | 97.4M | -1, 0, opt, ctx, depth, libctx, propq); |
711 | 97.4M | if (!ret) { |
712 | 8.71M | ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR); |
713 | 8.71M | goto err; |
714 | 88.6M | } else if (ret == -1) |
715 | 6.31M | return -1; |
716 | 97.4M | } |
717 | | |
718 | 222M | *in = p; |
719 | 222M | return 1; |
720 | | |
721 | 11.3M | err: |
722 | 11.3M | return 0; |
723 | 255M | } |
724 | | |
725 | | static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, |
726 | | const unsigned char **in, long inlen, |
727 | | const ASN1_ITEM *it, |
728 | | int tag, int aclass, char opt, ASN1_TLC *ctx) |
729 | 154M | { |
730 | 154M | int ret = 0, utype; |
731 | 154M | long plen; |
732 | 154M | char cst, inf, free_cont = 0; |
733 | 154M | const unsigned char *p; |
734 | 154M | BUF_MEM buf = { 0, NULL, 0, 0 }; |
735 | 154M | const unsigned char *cont = NULL; |
736 | 154M | long len; |
737 | | |
738 | 154M | if (pval == NULL) { |
739 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_NULL); |
740 | 0 | return 0; /* Should never happen */ |
741 | 0 | } |
742 | | |
743 | 154M | if (it->itype == ASN1_ITYPE_MSTRING) { |
744 | 20.5M | utype = tag; |
745 | 20.5M | tag = -1; |
746 | 20.5M | } else |
747 | 134M | utype = it->utype; |
748 | | |
749 | 154M | if (utype == V_ASN1_ANY) { |
750 | | /* If type is ANY need to figure out type from tag */ |
751 | 58.5M | unsigned char oclass; |
752 | 58.5M | if (tag >= 0) { |
753 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_TAGGED_ANY); |
754 | 0 | return 0; |
755 | 0 | } |
756 | 58.5M | if (opt) { |
757 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY); |
758 | 0 | return 0; |
759 | 0 | } |
760 | 58.5M | p = *in; |
761 | 58.5M | ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL, |
762 | 58.5M | &p, inlen, -1, 0, 0, ctx); |
763 | 58.5M | if (!ret) { |
764 | 45.3k | ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR); |
765 | 45.3k | return 0; |
766 | 45.3k | } |
767 | 58.4M | if (oclass != V_ASN1_UNIVERSAL) |
768 | 3.13M | utype = V_ASN1_OTHER; |
769 | 58.4M | } |
770 | 154M | if (tag == -1) { |
771 | 142M | tag = utype; |
772 | 142M | aclass = V_ASN1_UNIVERSAL; |
773 | 142M | } |
774 | 154M | p = *in; |
775 | | /* Check header */ |
776 | 154M | ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst, |
777 | 154M | &p, inlen, tag, aclass, opt, ctx); |
778 | 154M | if (!ret) { |
779 | 7.00M | ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR); |
780 | 7.00M | return 0; |
781 | 147M | } else if (ret == -1) |
782 | 13.3M | return -1; |
783 | 134M | ret = 0; |
784 | | /* SEQUENCE, SET and "OTHER" are left in encoded form */ |
785 | 134M | if ((utype == V_ASN1_SEQUENCE) |
786 | 130M | || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) { |
787 | | /* |
788 | | * Clear context cache for type OTHER because the auto clear when we |
789 | | * have a exact match won't work |
790 | | */ |
791 | 25.4M | if (utype == V_ASN1_OTHER) { |
792 | 3.13M | asn1_tlc_clear(ctx); |
793 | 3.13M | } |
794 | | /* SEQUENCE and SET must be constructed */ |
795 | 22.2M | else if (!cst) { |
796 | 15.5k | ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED); |
797 | 15.5k | return 0; |
798 | 15.5k | } |
799 | | |
800 | 25.4M | cont = *in; |
801 | | /* If indefinite length constructed find the real end */ |
802 | 25.4M | if (inf) { |
803 | 1.21M | if (!asn1_find_end(&p, plen, inf)) |
804 | 114k | goto err; |
805 | 1.10M | len = p - cont; |
806 | 24.1M | } else { |
807 | 24.1M | len = p - cont + plen; |
808 | 24.1M | p += plen; |
809 | 24.1M | } |
810 | 109M | } else if (cst) { |
811 | 7.10M | if (utype == V_ASN1_NULL || utype == V_ASN1_BOOLEAN |
812 | 7.09M | || utype == V_ASN1_OBJECT || utype == V_ASN1_INTEGER |
813 | 7.07M | || utype == V_ASN1_ENUMERATED) { |
814 | 27.1k | ERR_raise(ERR_LIB_ASN1, ASN1_R_TYPE_NOT_PRIMITIVE); |
815 | 27.1k | return 0; |
816 | 27.1k | } |
817 | | |
818 | | /* Free any returned 'buf' content */ |
819 | 7.07M | free_cont = 1; |
820 | | /* |
821 | | * Should really check the internal tags are correct but some things |
822 | | * may get this wrong. The relevant specs say that constructed string |
823 | | * types should be OCTET STRINGs internally irrespective of the type. |
824 | | * So instead just check for UNIVERSAL class and ignore the tag. |
825 | | */ |
826 | 7.07M | if (!asn1_collect(&buf, &p, plen, inf, -1, V_ASN1_UNIVERSAL, 0)) { |
827 | 64.9k | goto err; |
828 | 64.9k | } |
829 | 7.00M | len = buf.length; |
830 | | /* Append a final null to string */ |
831 | 7.00M | if (!BUF_MEM_grow_clean(&buf, len + 1)) { |
832 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
833 | 0 | goto err; |
834 | 0 | } |
835 | 7.00M | buf.data[len] = 0; |
836 | 7.00M | cont = (const unsigned char *)buf.data; |
837 | 102M | } else { |
838 | 102M | cont = p; |
839 | 102M | len = plen; |
840 | 102M | p += plen; |
841 | 102M | } |
842 | | |
843 | | /* We now have content length and type: translate into a structure */ |
844 | | /* asn1_ex_c2i may reuse allocated buffer, and so sets free_cont to 0 */ |
845 | 134M | if (!asn1_ex_c2i(pval, cont, len, utype, &free_cont, it)) |
846 | 878k | goto err; |
847 | | |
848 | 133M | *in = p; |
849 | 133M | ret = 1; |
850 | 134M | err: |
851 | 134M | if (free_cont) |
852 | 401k | OPENSSL_free(buf.data); |
853 | 134M | return ret; |
854 | 133M | } |
855 | | |
856 | | /* Translate ASN1 content octets into a structure */ |
857 | | |
858 | | static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, |
859 | | int utype, char *free_cont, const ASN1_ITEM *it) |
860 | 14.2M | { |
861 | 14.2M | ASN1_VALUE **opval = NULL; |
862 | 14.2M | ASN1_STRING *stmp; |
863 | 14.2M | ASN1_TYPE *typ = NULL; |
864 | 14.2M | int ret = 0; |
865 | 14.2M | const ASN1_PRIMITIVE_FUNCS *pf; |
866 | 14.2M | ASN1_INTEGER **tint; |
867 | 14.2M | pf = it->funcs; |
868 | | |
869 | 14.2M | if (pf && pf->prim_c2i) |
870 | 489k | return pf->prim_c2i(pval, cont, len, utype, free_cont, it); |
871 | | /* If ANY type clear type and set pointer to internal value */ |
872 | 13.7M | if (it->utype == V_ASN1_ANY) { |
873 | 2.20M | if (*pval == NULL) { |
874 | 2.17M | typ = ASN1_TYPE_new(); |
875 | 2.17M | if (typ == NULL) |
876 | 0 | goto err; |
877 | 2.17M | *pval = (ASN1_VALUE *)typ; |
878 | 2.17M | } else |
879 | 27.3k | typ = (ASN1_TYPE *)*pval; |
880 | | |
881 | 2.20M | if (utype != typ->type) |
882 | 2.20M | ASN1_TYPE_set(typ, utype, NULL); |
883 | 2.20M | opval = pval; |
884 | 2.20M | pval = &typ->value.asn1_value; |
885 | 2.20M | } |
886 | 13.7M | switch (utype) { |
887 | 5.17M | case V_ASN1_OBJECT: |
888 | 5.17M | if (!ossl_c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len)) |
889 | 6.21k | goto err; |
890 | 5.16M | break; |
891 | | |
892 | 5.16M | case V_ASN1_NULL: |
893 | 84.8k | if (len) { |
894 | 660 | ERR_raise(ERR_LIB_ASN1, ASN1_R_NULL_IS_WRONG_LENGTH); |
895 | 660 | goto err; |
896 | 660 | } |
897 | 84.1k | *pval = (ASN1_VALUE *)1; |
898 | 84.1k | break; |
899 | | |
900 | 56.9k | case V_ASN1_BOOLEAN: |
901 | 56.9k | if (len != 1) { |
902 | 1.30k | ERR_raise(ERR_LIB_ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH); |
903 | 1.30k | goto err; |
904 | 55.6k | } else { |
905 | 55.6k | ASN1_BOOLEAN *tbool; |
906 | 55.6k | tbool = (ASN1_BOOLEAN *)pval; |
907 | 55.6k | *tbool = *cont; |
908 | 55.6k | } |
909 | 55.6k | break; |
910 | | |
911 | 1.41M | case V_ASN1_BIT_STRING: |
912 | 1.41M | if (!ossl_c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len)) |
913 | 6.79k | goto err; |
914 | 1.41M | break; |
915 | | |
916 | 1.41M | case V_ASN1_INTEGER: |
917 | 1.22M | case V_ASN1_ENUMERATED: |
918 | 1.22M | tint = (ASN1_INTEGER **)pval; |
919 | 1.22M | if (!ossl_c2i_ASN1_INTEGER(tint, &cont, len)) |
920 | 63.2k | goto err; |
921 | | /* Fixup type to match the expected form */ |
922 | 1.15M | (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG); |
923 | 1.15M | break; |
924 | | |
925 | 729k | case V_ASN1_OCTET_STRING: |
926 | 1.09M | case V_ASN1_NUMERICSTRING: |
927 | 2.03M | case V_ASN1_PRINTABLESTRING: |
928 | 2.14M | case V_ASN1_T61STRING: |
929 | 2.15M | case V_ASN1_VIDEOTEXSTRING: |
930 | 2.47M | case V_ASN1_IA5STRING: |
931 | 2.74M | case V_ASN1_UTCTIME: |
932 | 2.90M | case V_ASN1_GENERALIZEDTIME: |
933 | 3.00M | case V_ASN1_GRAPHICSTRING: |
934 | 3.00M | case V_ASN1_VISIBLESTRING: |
935 | 3.00M | case V_ASN1_GENERALSTRING: |
936 | 3.16M | case V_ASN1_UNIVERSALSTRING: |
937 | 3.26M | case V_ASN1_BMPSTRING: |
938 | 3.39M | case V_ASN1_UTF8STRING: |
939 | 3.47M | case V_ASN1_OTHER: |
940 | 4.38M | case V_ASN1_SET: |
941 | 4.90M | case V_ASN1_SEQUENCE: |
942 | 5.78M | default: |
943 | 5.78M | if (utype == V_ASN1_BMPSTRING && (len & 1)) { |
944 | 489 | ERR_raise(ERR_LIB_ASN1, ASN1_R_BMPSTRING_IS_WRONG_LENGTH); |
945 | 489 | goto err; |
946 | 489 | } |
947 | 5.78M | if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) { |
948 | 608 | ERR_raise(ERR_LIB_ASN1, ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH); |
949 | 608 | goto err; |
950 | 608 | } |
951 | | /* All based on ASN1_STRING and handled the same */ |
952 | 5.78M | if (*pval == NULL) { |
953 | 2.26M | stmp = ASN1_STRING_type_new(utype); |
954 | 2.26M | if (stmp == NULL) { |
955 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
956 | 0 | goto err; |
957 | 0 | } |
958 | 2.26M | *pval = (ASN1_VALUE *)stmp; |
959 | 3.52M | } else { |
960 | 3.52M | stmp = (ASN1_STRING *)*pval; |
961 | 3.52M | stmp->type = utype; |
962 | 3.52M | } |
963 | | /* If we've already allocated a buffer use it */ |
964 | 5.78M | if (*free_cont) { |
965 | 1.40M | OPENSSL_free(stmp->data); |
966 | 1.40M | stmp->data = (unsigned char *)cont; /* UGLY CAST! RL */ |
967 | 1.40M | stmp->length = len; |
968 | 1.40M | *free_cont = 0; |
969 | 4.38M | } else { |
970 | 4.38M | if (!ASN1_STRING_set(stmp, cont, len)) { |
971 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
972 | 0 | ASN1_STRING_free(stmp); |
973 | 0 | *pval = NULL; |
974 | 0 | goto err; |
975 | 0 | } |
976 | 4.38M | } |
977 | 5.78M | break; |
978 | 13.7M | } |
979 | | /* If ASN1_ANY and NULL type fix up value */ |
980 | 13.6M | if (typ && (utype == V_ASN1_NULL)) |
981 | 81.5k | typ->value.ptr = NULL; |
982 | | |
983 | 13.6M | ret = 1; |
984 | 13.7M | err: |
985 | 13.7M | if (!ret) { |
986 | 79.3k | ASN1_TYPE_free(typ); |
987 | 79.3k | if (opval) |
988 | 4.17k | *opval = NULL; |
989 | 79.3k | } |
990 | 13.7M | return ret; |
991 | 13.6M | } |
992 | | |
993 | | /* |
994 | | * This function finds the end of an ASN1 structure when passed its maximum |
995 | | * length, whether it is indefinite length and a pointer to the content. This |
996 | | * is more efficient than calling asn1_collect because it does not recurse on |
997 | | * each indefinite length header. |
998 | | */ |
999 | | |
1000 | | static int asn1_find_end(const unsigned char **in, long len, char inf) |
1001 | 1.21M | { |
1002 | 1.21M | uint32_t expected_eoc; |
1003 | 1.21M | long plen; |
1004 | 1.21M | const unsigned char *p = *in, *q; |
1005 | | /* If not indefinite length constructed just add length */ |
1006 | 1.21M | if (inf == 0) { |
1007 | 0 | *in += len; |
1008 | 0 | return 1; |
1009 | 0 | } |
1010 | 1.21M | expected_eoc = 1; |
1011 | | /* |
1012 | | * Indefinite length constructed form. Find the end when enough EOCs are |
1013 | | * found. If more indefinite length constructed headers are encountered |
1014 | | * increment the expected eoc count otherwise just skip to the end of the |
1015 | | * data. |
1016 | | */ |
1017 | 414M | while (len > 0) { |
1018 | 414M | if (asn1_check_eoc(&p, len)) { |
1019 | 40.6M | expected_eoc--; |
1020 | 40.6M | if (expected_eoc == 0) |
1021 | 1.10M | break; |
1022 | 39.5M | len -= 2; |
1023 | 39.5M | continue; |
1024 | 40.6M | } |
1025 | 373M | q = p; |
1026 | | /* Just read in a header: only care about the length */ |
1027 | 373M | if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len, |
1028 | 373M | -1, 0, 0, NULL)) { |
1029 | 74.5k | ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR); |
1030 | 74.5k | return 0; |
1031 | 74.5k | } |
1032 | 373M | if (inf) { |
1033 | 85.8M | if (expected_eoc == UINT32_MAX) { |
1034 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR); |
1035 | 0 | return 0; |
1036 | 0 | } |
1037 | 85.8M | expected_eoc++; |
1038 | 287M | } else { |
1039 | 287M | p += plen; |
1040 | 287M | } |
1041 | 373M | len -= p - q; |
1042 | 373M | } |
1043 | 1.14M | if (expected_eoc) { |
1044 | 39.5k | ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC); |
1045 | 39.5k | return 0; |
1046 | 39.5k | } |
1047 | 1.10M | *in = p; |
1048 | 1.10M | return 1; |
1049 | 1.14M | } |
1050 | | |
1051 | | /* |
1052 | | * This function collects the asn1 data from a constructed string type into |
1053 | | * a buffer. The values of 'in' and 'len' should refer to the contents of the |
1054 | | * constructed type and 'inf' should be set if it is indefinite length. |
1055 | | */ |
1056 | | |
1057 | | #ifndef ASN1_MAX_STRING_NEST |
1058 | | /* |
1059 | | * This determines how many levels of recursion are permitted in ASN1 string |
1060 | | * types. If it is not limited stack overflows can occur. If set to zero no |
1061 | | * recursion is allowed at all. Although zero should be adequate examples |
1062 | | * exist that require a value of 1. So 5 should be more than enough. |
1063 | | */ |
1064 | 2.42M | #define ASN1_MAX_STRING_NEST 5 |
1065 | | #endif |
1066 | | |
1067 | | static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len, |
1068 | | char inf, int tag, int aclass, int depth) |
1069 | 9.49M | { |
1070 | 9.49M | const unsigned char *p, *q; |
1071 | 9.49M | long plen; |
1072 | 9.49M | char cst, ininf; |
1073 | 9.49M | p = *in; |
1074 | 9.49M | inf &= 1; |
1075 | | /* |
1076 | | * If no buffer and not indefinite length constructed just pass over the |
1077 | | * encoded data |
1078 | | */ |
1079 | 9.49M | if (!buf && !inf) { |
1080 | 0 | *in += len; |
1081 | 0 | return 1; |
1082 | 0 | } |
1083 | 49.4M | while (len > 0) { |
1084 | 41.3M | q = p; |
1085 | | /* Check for EOC */ |
1086 | 41.3M | if (asn1_check_eoc(&p, len)) { |
1087 | | /* |
1088 | | * EOC is illegal outside indefinite length constructed form |
1089 | | */ |
1090 | 1.29M | if (!inf) { |
1091 | 11.3k | ERR_raise(ERR_LIB_ASN1, ASN1_R_UNEXPECTED_EOC); |
1092 | 11.3k | return 0; |
1093 | 11.3k | } |
1094 | 1.28M | inf = 0; |
1095 | 1.28M | break; |
1096 | 1.29M | } |
1097 | | |
1098 | 40.0M | if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p, |
1099 | 40.0M | len, tag, aclass, 0, NULL)) { |
1100 | 36.7k | ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR); |
1101 | 36.7k | return 0; |
1102 | 36.7k | } |
1103 | | |
1104 | | /* If indefinite length constructed update max length */ |
1105 | 39.9M | if (cst) { |
1106 | 2.42M | if (depth >= ASN1_MAX_STRING_NEST) { |
1107 | 2.47k | ERR_raise(ERR_LIB_ASN1, ASN1_R_NESTED_ASN1_STRING); |
1108 | 2.47k | return 0; |
1109 | 2.47k | } |
1110 | 2.42M | if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, depth + 1)) |
1111 | 49.9k | return 0; |
1112 | 37.5M | } else if (plen && !collect_data(buf, &p, plen)) |
1113 | 0 | return 0; |
1114 | 39.9M | len -= p - q; |
1115 | 39.9M | } |
1116 | 9.39M | if (inf) { |
1117 | 14.4k | ERR_raise(ERR_LIB_ASN1, ASN1_R_MISSING_EOC); |
1118 | 14.4k | return 0; |
1119 | 14.4k | } |
1120 | 9.38M | *in = p; |
1121 | 9.38M | return 1; |
1122 | 9.39M | } |
1123 | | |
1124 | | static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen) |
1125 | 21.1M | { |
1126 | 21.1M | int len; |
1127 | 21.1M | if (buf) { |
1128 | 21.1M | len = buf->length; |
1129 | 21.1M | if (!BUF_MEM_grow_clean(buf, len + plen)) { |
1130 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
1131 | 0 | return 0; |
1132 | 0 | } |
1133 | 21.1M | memcpy(buf->data + len, *p, plen); |
1134 | 21.1M | } |
1135 | 21.1M | *p += plen; |
1136 | 21.1M | return 1; |
1137 | 21.1M | } |
1138 | | |
1139 | | /* Check for ASN1 EOC and swallow it if found */ |
1140 | | |
1141 | | static int asn1_check_eoc(const unsigned char **in, long len) |
1142 | 798M | { |
1143 | 798M | const unsigned char *p; |
1144 | | |
1145 | 798M | if (len < 2) |
1146 | 195k | return 0; |
1147 | 798M | p = *in; |
1148 | 798M | if (p[0] == '\0' && p[1] == '\0') { |
1149 | 62.3M | *in += 2; |
1150 | 62.3M | return 1; |
1151 | 62.3M | } |
1152 | 736M | return 0; |
1153 | 798M | } |
1154 | | |
1155 | | /* |
1156 | | * Check an ASN1 tag and length: a bit like ASN1_get_object but it sets the |
1157 | | * length for indefinite length constructed form, we don't know the exact |
1158 | | * length but we can set an upper bound to the amount of data available minus |
1159 | | * the header length just read. |
1160 | | */ |
1161 | | |
1162 | | static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, |
1163 | | char *inf, char *cst, |
1164 | | const unsigned char **in, long len, |
1165 | | int exptag, int expclass, char opt, ASN1_TLC *ctx) |
1166 | 855M | { |
1167 | 855M | int i; |
1168 | 855M | int ptag, pclass; |
1169 | 855M | long plen; |
1170 | 855M | const unsigned char *p, *q; |
1171 | 855M | p = *in; |
1172 | 855M | q = p; |
1173 | | |
1174 | 855M | if (len <= 0) { |
1175 | 432 | ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_SMALL); |
1176 | 432 | goto err; |
1177 | 432 | } |
1178 | 855M | if (ctx != NULL && ctx->valid) { |
1179 | 103M | i = ctx->ret; |
1180 | 103M | plen = ctx->plen; |
1181 | 103M | pclass = ctx->pclass; |
1182 | 103M | ptag = ctx->ptag; |
1183 | 103M | p += ctx->hdrlen; |
1184 | 752M | } else { |
1185 | 752M | i = ASN1_get_object(&p, &plen, &ptag, &pclass, len); |
1186 | 752M | if (ctx != NULL) { |
1187 | 338M | ctx->ret = i; |
1188 | 338M | ctx->plen = plen; |
1189 | 338M | ctx->pclass = pclass; |
1190 | 338M | ctx->ptag = ptag; |
1191 | 338M | ctx->hdrlen = p - q; |
1192 | 338M | ctx->valid = 1; |
1193 | | /* |
1194 | | * If definite length, and no error, length + header can't exceed |
1195 | | * total amount of data available. |
1196 | | */ |
1197 | 338M | if ((i & 0x81) == 0 && (plen + ctx->hdrlen) > len) { |
1198 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LONG); |
1199 | 0 | goto err; |
1200 | 0 | } |
1201 | 338M | } |
1202 | 752M | } |
1203 | | |
1204 | 855M | if ((i & 0x80) != 0) { |
1205 | 1.25M | ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_OBJECT_HEADER); |
1206 | 1.25M | goto err; |
1207 | 1.25M | } |
1208 | 854M | if (exptag >= 0) { |
1209 | 357M | if (exptag != ptag || expclass != pclass) { |
1210 | | /* |
1211 | | * If type is OPTIONAL, not an error: indicate missing type. |
1212 | | */ |
1213 | 37.0M | if (opt != 0) |
1214 | 24.6M | return -1; |
1215 | 37.0M | ERR_raise(ERR_LIB_ASN1, ASN1_R_WRONG_TAG); |
1216 | 12.3M | goto err; |
1217 | 37.0M | } |
1218 | | /* |
1219 | | * We have a tag and class match: assume we are going to do something |
1220 | | * with it |
1221 | | */ |
1222 | 320M | asn1_tlc_clear(ctx); |
1223 | 320M | } |
1224 | | |
1225 | 817M | if ((i & 1) != 0) |
1226 | 119M | plen = len - (p - q); |
1227 | | |
1228 | 817M | if (inf != NULL) |
1229 | 737M | *inf = i & 1; |
1230 | | |
1231 | 817M | if (cst != NULL) |
1232 | 225M | *cst = i & V_ASN1_CONSTRUCTED; |
1233 | | |
1234 | 817M | if (olen != NULL) |
1235 | 737M | *olen = plen; |
1236 | | |
1237 | 817M | if (oclass != NULL) |
1238 | 79.5M | *oclass = pclass; |
1239 | | |
1240 | 817M | if (otag != NULL) |
1241 | 79.5M | *otag = ptag; |
1242 | | |
1243 | 817M | *in = p; |
1244 | 817M | return 1; |
1245 | | |
1246 | 13.6M | err: |
1247 | | asn1_tlc_clear(ctx); |
1248 | 13.6M | return 0; |
1249 | 854M | } |