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