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