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