Debian Codegraph @

lib/asn1/der_get.c

raw
1 /*
2 * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include "der_locl.h"
35
36 /*
37 * All decoding functions take a pointer `p' to first position in
38 * which to read, from the left, `len' which means the maximum number
39 * of characters we are able to read, `ret' were the value will be
40 * returned and `size' where the number of used bytes is stored.
41 * Either 0 or an error code is returned.
42 */
43
44 int
45 der_get_unsigned (const unsigned char *p, size_t len,
46 unsigned *ret, size_t *size)
47 {
48 unsigned val = 0;
49 size_t oldlen = len;
50
51 if (len == sizeof(val) + 1 && p[0] == 0)
52 ;
53 else if (len > sizeof(val))
54 return ASN1_OVERRUN;
55
56 while (len--)
57 val = val * 256 + *p++;
58 *ret = val;
59 if(size) *size = oldlen;
60 return 0;
61 }
62
63 int
64 der_get_unsigned64 (const unsigned char *p, size_t len,
65 uint64_t *ret, size_t *size)
66 {
67 uint64_t val = 0;
68 size_t oldlen = len;
69
70 if (len == sizeof(val) + 1 && p[0] == 0)
71 ;
72 else if (len > sizeof(val))
73 return ASN1_OVERRUN;
74
75 while (len--)
76 val = val * 256 + *p++;
77 *ret = val;
78 if(size) *size = oldlen;
79 return 0;
80 }
81
82 int
83 der_get_integer (const unsigned char *p, size_t len,
84 int *ret, size_t *size)
85 {
86 int val = 0;
87 size_t oldlen = len;
88
89 if (len == sizeof(val) + 1 && (p[0] == 0 || p[0] == 0xff))
90 ;
91 else if (len > sizeof(val))
92 return ASN1_OVERRUN;
93
94 /* We assume we're on a twos-complement platform */
95 if (len > 0) {
96 val = (signed char)*p++;
97 while (--len)
98 val = val * 256 + *p++;
99 }
100 *ret = val;
101 if(size) *size = oldlen;
102 return 0;
103 }
104
105 int
106 der_get_integer64 (const unsigned char *p, size_t len,
107 int64_t *ret, size_t *size)
108 {
109 int64_t val = 0;
110 size_t oldlen = len;
111
112 if (len > sizeof(val))
113 return ASN1_OVERRUN;
114
115 /* We assume we're on a twos-complement platform */
116 if (len > 0) {
117 val = (signed char)*p++;
118 while (--len)
119 val = val * 256 + *p++;
120 }
121 *ret = val;
122 if(size) *size = oldlen;
123 return 0;
124 }
125
126
127 int
128 der_get_length (const unsigned char *p, size_t len,
129 size_t *val, size_t *size)
130 {
131 size_t v;
132
133 if (len <= 0)
134 return ASN1_OVERRUN;
135 --len;
136 v = *p++;
137 if (v < 128) {
138 *val = v;
139 if(size) *size = 1;
140 } else {
141 int e;
142 size_t l;
143 unsigned tmp;
144
145 if(v == 0x80){
146 *val = ASN1_INDEFINITE;
147 if(size) *size = 1;
148 return 0;
149 }
150 v &= 0x7F;
151 if (len < v)
152 return ASN1_OVERRUN;
153 e = der_get_unsigned (p, v, &tmp, &l);
154 if(e) return e;
155 *val = tmp;
156 if(size) *size = l + 1;
157 }
158 return 0;
159 }
160
161 int
162 der_get_boolean(const unsigned char *p, size_t len, int *data, size_t *size)
163 {
164 if(len < 1)
165 return ASN1_OVERRUN;
166 if(*p != 0)
167 *data = 1;
168 else
169 *data = 0;
170 *size = 1;
171 return 0;
172 }
173
174 int
175 der_get_general_string (const unsigned char *p, size_t len,
176 heim_general_string *str, size_t *size)
177 {
178 const unsigned char *p1;
179 char *s;
180
181 p1 = memchr(p, 0, len);
182 if (p1 != NULL) {
183 /*
184 * Allow trailing NULs. We allow this since MIT Kerberos sends
185 * an strings in the NEED_PREAUTH case that includes a
186 * trailing NUL.
187 */
188 while ((size_t)(p1 - p) < len && *p1 == '\0')
189 p1++;
190 if ((size_t)(p1 - p) != len) {
191 *str = NULL;
192 return ASN1_BAD_CHARACTER;
193 }
194 }
195 if (len == SIZE_MAX) {
196 *str = NULL;
197 return ASN1_BAD_LENGTH;
198 }
199
200 *str = s = malloc (len + 1);
201 if (s == NULL)
202 return ENOMEM;
203 memcpy (s, p, len);
204 s[len] = '\0';
205
206 if(size) *size = len;
207 return 0;
208 }
209
210 int
211 der_get_utf8string (const unsigned char *p, size_t len,
212 heim_utf8_string *str, size_t *size)
213 {
214 return der_get_general_string(p, len, str, size);
215 }
216
217 #define gen_data_zero(_data) \
218 do { (_data)->length = 0; (_data)->data = NULL; } while(0)
219
220 int
221 der_get_printable_string(const unsigned char *p, size_t len,
222 heim_printable_string *str, size_t *size)
223 {
224 if (len == SIZE_MAX) {
225 gen_data_zero(str);
226 return ASN1_BAD_LENGTH;
227 }
228 str->length = len;
229 str->data = malloc(len + 1);
230 if (str->data == NULL) {
231 gen_data_zero(str);
232 return ENOMEM;
233 }
234 memcpy(str->data, p, len);
235 ((char *)str->data)[len] = '\0';
236 if(size) *size = len;
237 return 0;
238 }
239
240 int
241 der_get_ia5_string(const unsigned char *p, size_t len,
242 heim_ia5_string *str, size_t *size)
243 {
244 return der_get_printable_string(p, len, str, size);
245 }
246
247 int
248 der_get_bmp_string (const unsigned char *p, size_t len,
249 heim_bmp_string *data, size_t *size)
250 {
251 size_t i;
252
253 if (len & 1) {
254 gen_data_zero(data);
255 return ASN1_BAD_FORMAT;
256 }
257 data->length = len / 2;
258 if (data->length > UINT_MAX/sizeof(data->data[0])) {
259 gen_data_zero(data);
260 return ERANGE;
261 }
262 data->data = malloc(data->length * sizeof(data->data[0]));
263 if (data->data == NULL && data->length != 0) {
264 gen_data_zero(data);
265 return ENOMEM;
266 }
267
268 for (i = 0; i < data->length; i++) {
269 data->data[i] = (p[0] << 8) | p[1];
270 p += 2;
271 /* check for NUL in the middle of the string */
272 if (data->data[i] == 0 && i != (data->length - 1)) {
273 free(data->data);
274 gen_data_zero(data);
275 return ASN1_BAD_CHARACTER;
276 }
277 }
278 if (size) *size = len;
279
280 return 0;
281 }
282
283 int
284 der_get_universal_string (const unsigned char *p, size_t len,
285 heim_universal_string *data, size_t *size)
286 {
287 size_t i;
288
289 if (len & 3) {
290 gen_data_zero(data);
291 return ASN1_BAD_FORMAT;
292 }
293 data->length = len / 4;
294 if (data->length > UINT_MAX/sizeof(data->data[0])) {
295 gen_data_zero(data);
296 return ERANGE;
297 }
298 data->data = malloc(data->length * sizeof(data->data[0]));
299 if (data->data == NULL && data->length != 0) {
300 gen_data_zero(data);
301 return ENOMEM;
302 }
303
304 for (i = 0; i < data->length; i++) {
305 data->data[i] = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
306 p += 4;
307 /* check for NUL in the middle of the string */
308 if (data->data[i] == 0 && i != (data->length - 1)) {
309 free(data->data);
310 gen_data_zero(data);
311 return ASN1_BAD_CHARACTER;
312 }
313 }
314 if (size) *size = len;
315 return 0;
316 }
317
318 int
319 der_get_visible_string (const unsigned char *p, size_t len,
320 heim_visible_string *str, size_t *size)
321 {
322 return der_get_general_string(p, len, str, size);
323 }
324
325 int
326 der_get_octet_string (const unsigned char *p, size_t len,
327 heim_octet_string *data, size_t *size)
328 {
329 data->length = len;
330 data->data = malloc(len);
331 if (data->data == NULL && data->length != 0)
332 return ENOMEM;
333 memcpy (data->data, p, len);
334 if(size) *size = len;
335 return 0;
336 }
337
338 int
339 der_get_octet_string_ber (const unsigned char *p, size_t len,
340 heim_octet_string *data, size_t *size)
341 {
342 int e;
343 Der_type type;
344 Der_class cls;
345 unsigned int tag, depth = 0;
346 size_t l, datalen, oldlen = len;
347
348 data->length = 0;
349 data->data = NULL;
350
351 while (len) {
352 e = der_get_tag (p, len, &cls, &type, &tag, &l);
353 if (e) goto out;
354 if (cls != ASN1_C_UNIV) {
355 e = ASN1_BAD_ID;
356 goto out;
357 }
358 if (type == PRIM && tag == UT_EndOfContent) {
359 if (depth == 0)
360 break;
361 depth--;
362 }
363 if (tag != UT_OctetString) {
364 e = ASN1_BAD_ID;
365 goto out;
366 }
367
368 p += l;
369 len -= l;
370 e = der_get_length (p, len, &datalen, &l);
371 if (e) goto out;
372 p += l;
373 len -= l;
374
375 if (datalen > len)
376 return ASN1_OVERRUN;
377
378 if (type == PRIM) {
379 void *ptr;
380
381 ptr = realloc(data->data, data->length + datalen);
382 if (ptr == NULL) {
383 e = ENOMEM;
384 goto out;
385 }
386 data->data = ptr;
387 memcpy(((unsigned char *)data->data) + data->length, p, datalen);
388 data->length += datalen;
389 } else
390 depth++;
391
392 p += datalen;
393 len -= datalen;
394 }
395 if (depth != 0)
396 return ASN1_INDEF_OVERRUN;
397 if(size) *size = oldlen - len;
398 return 0;
399 out:
400 free(data->data);
401 data->data = NULL;
402 data->length = 0;
403 return e;
404 }
405
406
407 int
408 der_get_heim_integer (const unsigned char *p, size_t len,
409 heim_integer *data, size_t *size)
410 {
411 data->length = 0;
412 data->negative = 0;
413 data->data = NULL;
414
415 if (len == 0) {
416 if (size)
417 *size = 0;
418 return 0;
419 }
420 if (p[0] & 0x80) {
421 unsigned char *q;
422 int carry = 1;
423
424
425 /*
426 * A negative number. It's going to be a twos complement byte array.
427 * We're going to leave the positive value in `data->data', but set the
428 * `data->negative' flag. That means we need to negate the
429 * twos-complement integer received.
430 */
431 data->negative = 1;
432 data->length = len;
433
434 if (p[0] == 0xff) {
435 if (data->length == 1) {
436 /* One byte of all ones == -1 */
437 q = data->data = malloc(1);
438 *q = 1;
439 data->length = 1;
440 if (size)
441 *size = 1;
442 return 0;
443 }
444
445 p++;
446 data->length--;
447
448 /*
449 * We could check if the next byte's high bit is set, which would
450 * be an error ("illegal padding" in OpenSSL). However, this would
451 * mean failing to accept certificates made by certain CAs that
452 * would read 8 bytes of RNG into a buffer, slap on length 8, then
453 * slap on the tag [UNIVERSAL INTEGER], and make that the
454 * serialNumber field's encoding, which then fails to parse in
455 * around 1 in 256 certificates.
456 *
457 * So let's not.
458 *
459 * if (p[0] & 0x80)
460 * return ASN1_PARSE_ERROR; // or a new error code
461 */
462 }
463 data->data = malloc(data->length);
464 if (data->data == NULL) {
465 data->length = 0;
466 if (size)
467 *size = 0;
468 return ENOMEM;
469 }
470
471 /*
472 * Note that if `data->length' were zero, this would be UB because we
473 * underflow if data->length is zero even though we wouldn't actually
474 * dereference the byte before data->data. Thus we check above for
475 * that.
476 */
477 q = &((unsigned char*)data->data)[data->length - 1];
478 p += data->length - 1;
479 while (q >= (unsigned char*)data->data) {
480 /* *p XOR 0xff -> ~*p; we're dealing with twos complement */
481 *q = *p ^ 0xff;
482 if (carry)
483 carry = !++*q;
484 p--;
485 q--;
486 }
487 } else {
488 data->negative = 0;
489 data->length = len;
490
491 if (p[0] == 0) {
492 p++;
493 data->length--;
494 }
495 data->data = malloc(data->length);
496 if (data->data == NULL && data->length != 0) {
497 data->length = 0;
498 if (size)
499 *size = 0;
500 return ENOMEM;
501 }
502 memcpy(data->data, p, data->length);
503 }
504 if (size)
505 *size = len;
506 return 0;
507 }
508
509 static int
510 generalizedtime2time (const char *s, time_t *t)
511 {
512 struct tm tm;
513
514 memset(&tm, 0, sizeof(tm));
515 if (sscanf (s, "%04d%02d%02d%02d%02d%02dZ",
516 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
517 &tm.tm_min, &tm.tm_sec) != 6) {
518 if (sscanf (s, "%02d%02d%02d%02d%02d%02dZ",
519 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
520 &tm.tm_min, &tm.tm_sec) != 6)
521 return ASN1_BAD_TIMEFORMAT;
522 if (tm.tm_year < 50)
523 tm.tm_year += 2000;
524 else
525 tm.tm_year += 1900;
526 }
527 tm.tm_year -= 1900;
528 tm.tm_mon -= 1;
529 *t = _der_timegm (&tm);
530 return 0;
531 }
532
533 static int
534 der_get_time (const unsigned char *p, size_t len,
535 time_t *data, size_t *size)
536 {
537 char *times;
538 int e;
539
540 if (len == SIZE_MAX || len == 0)
541 return ASN1_BAD_LENGTH;
542
543 times = malloc(len + 1);
544 if (times == NULL)
545 return ENOMEM;
546 memcpy(times, p, len);
547 times[len] = '\0';
548 e = generalizedtime2time(times, data);
549 free (times);
550 if(size) *size = len;
551 return e;
552 }
553
554 int
555 der_get_generalized_time (const unsigned char *p, size_t len,
556 time_t *data, size_t *size)
557 {
558 return der_get_time(p, len, data, size);
559 }
560
561 int
562 der_get_utctime (const unsigned char *p, size_t len,
563 time_t *data, size_t *size)
564 {
565 return der_get_time(p, len, data, size);
566 }
567
568 int
569 der_get_oid (const unsigned char *p, size_t len,
570 heim_oid *data, size_t *size)
571 {
572 size_t n;
573 size_t oldlen = len;
574
575 if (len < 1)
576 return ASN1_OVERRUN;
577
578 if (len == SIZE_MAX)
579 return ASN1_BAD_LENGTH;
580
581 if (len + 1 > UINT_MAX/sizeof(data->components[0]))
582 return ERANGE;
583
584 data->components = malloc((len + 1) * sizeof(data->components[0]));
585 if (data->components == NULL)
586 return ENOMEM;
587 data->components[0] = (*p) / 40;
588 data->components[1] = (*p) % 40;
589 --len;
590 ++p;
591 for (n = 2; len > 0; ++n) {
592 unsigned u = 0, u1;
593
594 do {
595 --len;
596 u1 = u * 128 + (*p++ % 128);
597 /* check that we don't overflow the element */
598 if (u1 < u) {
599 der_free_oid(data);
600 return ASN1_OVERRUN;
601 }
602 u = u1;
603 } while (len > 0 && p[-1] & 0x80);
604 data->components[n] = u;
605 }
606 if (n > 2 && p[-1] & 0x80) {
607 der_free_oid (data);
608 return ASN1_OVERRUN;
609 }
610 data->length = n;
611 if (size)
612 *size = oldlen;
613 return 0;
614 }
615
616 int
617 der_get_tag (const unsigned char *p, size_t len,
618 Der_class *cls, Der_type *type,
619 unsigned int *tag, size_t *size)
620 {
621 size_t ret = 0;
622 if (len < 1)
623 return ASN1_OVERRUN;
624 *cls = (Der_class)(((*p) >> 6) & 0x03);
625 *type = (Der_type)(((*p) >> 5) & 0x01);
626 *tag = (*p) & 0x1f;
627 p++; len--; ret++;
628 if(*tag == 0x1f) {
629 unsigned int continuation;
630 unsigned int tag1;
631 *tag = 0;
632 do {
633 if(len < 1)
634 return ASN1_OVERRUN;
635 continuation = *p & 128;
636 tag1 = *tag * 128 + (*p % 128);
637 /* check that we don't overflow the tag */
638 if (tag1 < *tag)
639 return ASN1_OVERFLOW;
640 *tag = tag1;
641 p++; len--; ret++;
642 } while(continuation);
643 }
644 if(size) *size = ret;
645 return 0;
646 }
647
648 int
649 der_match_tag (const unsigned char *p, size_t len,
650 Der_class cls, Der_type type,
651 unsigned int tag, size_t *size)
652 {
653 Der_type thistype;
654 int e;
655
656 e = der_match_tag2(p, len, cls, &thistype, tag, size);
657 if (e) return e;
658 if (thistype != type) return ASN1_BAD_ID;
659 return 0;
660 }
661
662 int
663 der_match_tag2 (const unsigned char *p, size_t len,
664 Der_class cls, Der_type *type,
665 unsigned int tag, size_t *size)
666 {
667 size_t l;
668 Der_class thisclass;
669 unsigned int thistag;
670 int e;
671
672 e = der_get_tag (p, len, &thisclass, type, &thistag, &l);
673 if (e) return e;
674 if (cls != thisclass)
675 return ASN1_BAD_ID;
676 if(tag > thistag)
677 return ASN1_MISPLACED_FIELD;
678 if(tag < thistag)
679 return ASN1_MISSING_FIELD;
680 if(size) *size = l;
681 return 0;
682 }
683
684 int
685 der_match_tag_and_length (const unsigned char *p, size_t len,
686 Der_class cls, Der_type *type, unsigned int tag,
687 size_t *length_ret, size_t *size)
688 {
689 size_t l, ret = 0;
690 int e;
691
692 e = der_match_tag2 (p, len, cls, type, tag, &l);
693 if (e) return e;
694 p += l;
695 len -= l;
696 ret += l;
697 e = der_get_length (p, len, length_ret, &l);
698 if (e) return e;
699 if(size) *size = ret + l;
700 return 0;
701 }
702
703
704
705 /*
706 * Old versions of DCE was based on a very early beta of the MIT code,
707 * which used MAVROS for ASN.1 encoding. MAVROS had the interesting
708 * feature that it encoded data in the forward direction, which has
709 * it's problems, since you have no idea how long the data will be
710 * until after you're done. MAVROS solved this by reserving one byte
711 * for length, and later, if the actual length was longer, it reverted
712 * to indefinite, BER style, lengths. The version of MAVROS used by
713 * the DCE people could apparently generate correct X.509 DER encodings, and
714 * did this by making space for the length after encoding, but
715 * unfortunately this feature wasn't used with Kerberos.
716 */
717
718 int
719 _heim_fix_dce(size_t reallen, size_t *len)
720 {
721 if(reallen == ASN1_INDEFINITE)
722 return 1;
723 if(*len < reallen)
724 return -1;
725 *len = reallen;
726 return 0;
727 }
728
729 int
730 der_get_bit_string (const unsigned char *p, size_t len,
731 heim_bit_string *data, size_t *size)
732 {
733 if (len < 1)
734 return ASN1_OVERRUN;
735 if (p[0] > 7)
736 return ASN1_BAD_FORMAT;
737 if (len - 1 == 0 && p[0] != 0)
738 return ASN1_BAD_FORMAT;
739 /* check if any of the three upper bits are set
740 * any of them will cause a interger overrun */
741 if ((len - 1) >> (sizeof(len) * 8 - 3))
742 return ASN1_OVERRUN;
743 /*
744 * If there is data to copy, do that now.
745 */
746 if (len - 1 > 0) {
747 data->length = (len - 1) * 8;
748 data->data = malloc(len - 1);
749 if (data->data == NULL)
750 return ENOMEM;
751 memcpy (data->data, p + 1, len - 1);
752 data->length -= p[0];
753 } else {
754 data->data = NULL;
755 data->length = 0;
756 }
757 if(size) *size = len;
758 return 0;
759 }