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