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