Support both BE and LE MIT master key file formats
[heimdal.git] / lib / asn1 / check-der.c
blob1bdec5d956f600e0b4c814b6efd8d7eb4043a786
1 /*
2 * Copyright (c) 1999 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #include "der_locl.h"
37 #include <err.h>
38 #include <roken.h>
40 #include <asn1-common.h>
41 #include <asn1_err.h>
42 #include <der.h>
44 #include "check-common.h"
46 RCSID("$Id$");
48 static int
49 cmp_integer (void *a, void *b)
51 int *ia = (int *)a;
52 int *ib = (int *)b;
54 return *ib - *ia;
57 static int
58 test_integer (void)
60 struct test_case tests[] = {
61 {NULL, 1, "\x00"},
62 {NULL, 1, "\x7f"},
63 {NULL, 2, "\x00\x80"},
64 {NULL, 2, "\x01\x00"},
65 {NULL, 1, "\x80"},
66 {NULL, 2, "\xff\x7f"},
67 {NULL, 1, "\xff"},
68 {NULL, 2, "\xff\x01"},
69 {NULL, 2, "\x00\xff"},
70 {NULL, 4, "\x7f\xff\xff\xff"}
73 int values[] = {0, 127, 128, 256, -128, -129, -1, -255, 255,
74 0x7fffffff};
75 int i, ret;
76 int ntests = sizeof(tests) / sizeof(*tests);
78 for (i = 0; i < ntests; ++i) {
79 tests[i].val = &values[i];
80 asprintf (&tests[i].name, "integer %d", values[i]);
81 if (tests[i].name == NULL)
82 errx(1, "malloc");
85 ret = generic_test (tests, ntests, sizeof(int),
86 (generic_encode)der_put_integer,
87 (generic_length) der_length_integer,
88 (generic_decode)der_get_integer,
89 (generic_free)NULL,
90 cmp_integer,
91 NULL);
93 for (i = 0; i < ntests; ++i)
94 free (tests[i].name);
95 return ret;
98 static int
99 test_one_int(int val)
101 int ret, dval;
102 unsigned char *buf;
103 size_t len_len, len;
105 len = _heim_len_int(val);
107 buf = emalloc(len + 2);
109 buf[0] = '\xff';
110 buf[len + 1] = '\xff';
111 memset(buf + 1, 0, len);
113 ret = der_put_integer(buf + 1 + len - 1, len, &val, &len_len);
114 if (ret) {
115 printf("integer %d encode failed %d\n", val, ret);
116 return 1;
118 if (len != len_len) {
119 printf("integer %d encode fail with %d len %lu, result len %lu\n",
120 val, ret, (unsigned long)len, (unsigned long)len_len);
121 return 1;
124 ret = der_get_integer(buf + 1, len, &dval, &len_len);
125 if (ret) {
126 printf("integer %d decode failed %d\n", val, ret);
127 return 1;
129 if (len != len_len) {
130 printf("integer %d decoded diffrent len %lu != %lu",
131 val, (unsigned long)len, (unsigned long)len_len);
132 return 1;
134 if (val != dval) {
135 printf("decode decoded to diffrent value %d != %d",
136 val, dval);
137 return 1;
140 if (buf[0] != (unsigned char)'\xff') {
141 printf("precanary dead %d\n", val);
142 return 1;
144 if (buf[len + 1] != (unsigned char)'\xff') {
145 printf("postecanary dead %d\n", val);
146 return 1;
148 free(buf);
149 return 0;
152 static int
153 test_integer_more (void)
155 int i, n1, n2, n3, n4, n5, n6;
157 n2 = 0;
158 for (i = 0; i < (sizeof(int) * 8); i++) {
159 n1 = 0x01 << i;
160 n2 = n2 | n1;
161 n3 = ~n1;
162 n4 = ~n2;
163 n5 = (-1) & ~(0x3f << i);
164 n6 = (-1) & ~(0x7f << i);
166 test_one_int(n1);
167 test_one_int(n2);
168 test_one_int(n3);
169 test_one_int(n4);
170 test_one_int(n5);
171 test_one_int(n6);
173 return 0;
176 static int
177 cmp_unsigned (void *a, void *b)
179 return *(unsigned int*)b - *(unsigned int*)a;
182 static int
183 test_unsigned (void)
185 struct test_case tests[] = {
186 {NULL, 1, "\x00"},
187 {NULL, 1, "\x7f"},
188 {NULL, 2, "\x00\x80"},
189 {NULL, 2, "\x01\x00"},
190 {NULL, 2, "\x02\x00"},
191 {NULL, 3, "\x00\x80\x00"},
192 {NULL, 5, "\x00\x80\x00\x00\x00"},
193 {NULL, 4, "\x7f\xff\xff\xff"}
196 unsigned int values[] = {0, 127, 128, 256, 512, 32768,
197 0x80000000, 0x7fffffff};
198 int i, ret;
199 int ntests = sizeof(tests) / sizeof(*tests);
201 for (i = 0; i < ntests; ++i) {
202 tests[i].val = &values[i];
203 asprintf (&tests[i].name, "unsigned %u", values[i]);
204 if (tests[i].name == NULL)
205 errx(1, "malloc");
208 ret = generic_test (tests, ntests, sizeof(int),
209 (generic_encode)der_put_unsigned,
210 (generic_length)der_length_unsigned,
211 (generic_decode)der_get_unsigned,
212 (generic_free)NULL,
213 cmp_unsigned,
214 NULL);
215 for (i = 0; i < ntests; ++i)
216 free (tests[i].name);
217 return ret;
220 static int
221 cmp_octet_string (void *a, void *b)
223 heim_octet_string *oa = (heim_octet_string *)a;
224 heim_octet_string *ob = (heim_octet_string *)b;
226 if (oa->length != ob->length)
227 return ob->length - oa->length;
229 return (memcmp (oa->data, ob->data, oa->length));
232 static int
233 test_octet_string (void)
235 heim_octet_string s1 = {8, "\x01\x23\x45\x67\x89\xab\xcd\xef"};
237 struct test_case tests[] = {
238 {NULL, 8, "\x01\x23\x45\x67\x89\xab\xcd\xef"}
240 int ntests = sizeof(tests) / sizeof(*tests);
241 int ret;
243 tests[0].val = &s1;
244 asprintf (&tests[0].name, "a octet string");
245 if (tests[0].name == NULL)
246 errx(1, "malloc");
248 ret = generic_test (tests, ntests, sizeof(heim_octet_string),
249 (generic_encode)der_put_octet_string,
250 (generic_length)der_length_octet_string,
251 (generic_decode)der_get_octet_string,
252 (generic_free)der_free_octet_string,
253 cmp_octet_string,
254 NULL);
255 free(tests[0].name);
256 return ret;
259 static int
260 cmp_bmp_string (void *a, void *b)
262 heim_bmp_string *oa = (heim_bmp_string *)a;
263 heim_bmp_string *ob = (heim_bmp_string *)b;
265 return der_heim_bmp_string_cmp(oa, ob);
268 static uint16_t bmp_d1[] = { 32 };
269 static uint16_t bmp_d2[] = { 32, 32 };
271 static int
272 test_bmp_string (void)
274 heim_bmp_string s1 = { 1, bmp_d1 };
275 heim_bmp_string s2 = { 2, bmp_d2 };
277 struct test_case tests[] = {
278 {NULL, 2, "\x00\x20"},
279 {NULL, 4, "\x00\x20\x00\x20"}
281 int ntests = sizeof(tests) / sizeof(*tests);
282 int ret;
284 tests[0].val = &s1;
285 asprintf (&tests[0].name, "a bmp string");
286 if (tests[0].name == NULL)
287 errx(1, "malloc");
288 tests[1].val = &s2;
289 asprintf (&tests[1].name, "second bmp string");
290 if (tests[1].name == NULL)
291 errx(1, "malloc");
293 ret = generic_test (tests, ntests, sizeof(heim_bmp_string),
294 (generic_encode)der_put_bmp_string,
295 (generic_length)der_length_bmp_string,
296 (generic_decode)der_get_bmp_string,
297 (generic_free)der_free_bmp_string,
298 cmp_bmp_string,
299 NULL);
300 free(tests[0].name);
301 free(tests[1].name);
302 return ret;
305 static int
306 cmp_universal_string (void *a, void *b)
308 heim_universal_string *oa = (heim_universal_string *)a;
309 heim_universal_string *ob = (heim_universal_string *)b;
311 return der_heim_universal_string_cmp(oa, ob);
314 static uint32_t universal_d1[] = { 32 };
315 static uint32_t universal_d2[] = { 32, 32 };
317 static int
318 test_universal_string (void)
320 heim_universal_string s1 = { 1, universal_d1 };
321 heim_universal_string s2 = { 2, universal_d2 };
323 struct test_case tests[] = {
324 {NULL, 4, "\x00\x00\x00\x20"},
325 {NULL, 8, "\x00\x00\x00\x20\x00\x00\x00\x20"}
327 int ntests = sizeof(tests) / sizeof(*tests);
328 int ret;
330 tests[0].val = &s1;
331 asprintf (&tests[0].name, "a universal string");
332 if (tests[0].name == NULL)
333 errx(1, "malloc");
334 tests[1].val = &s2;
335 asprintf (&tests[1].name, "second universal string");
336 if (tests[1].name == NULL)
337 errx(1, "malloc");
339 ret = generic_test (tests, ntests, sizeof(heim_universal_string),
340 (generic_encode)der_put_universal_string,
341 (generic_length)der_length_universal_string,
342 (generic_decode)der_get_universal_string,
343 (generic_free)der_free_universal_string,
344 cmp_universal_string,
345 NULL);
346 free(tests[0].name);
347 free(tests[1].name);
348 return ret;
351 static int
352 cmp_general_string (void *a, void *b)
354 char **sa = (char **)a;
355 char **sb = (char **)b;
357 return strcmp (*sa, *sb);
360 static int
361 test_general_string (void)
363 char *s1 = "Test User 1";
365 struct test_case tests[] = {
366 {NULL, 11, "\x54\x65\x73\x74\x20\x55\x73\x65\x72\x20\x31"}
368 int ret, ntests = sizeof(tests) / sizeof(*tests);
370 tests[0].val = &s1;
371 asprintf (&tests[0].name, "the string \"%s\"", s1);
372 if (tests[0].name == NULL)
373 errx(1, "malloc");
375 ret = generic_test (tests, ntests, sizeof(unsigned char *),
376 (generic_encode)der_put_general_string,
377 (generic_length)der_length_general_string,
378 (generic_decode)der_get_general_string,
379 (generic_free)der_free_general_string,
380 cmp_general_string,
381 NULL);
382 free(tests[0].name);
383 return ret;
386 static int
387 cmp_generalized_time (void *a, void *b)
389 time_t *ta = (time_t *)a;
390 time_t *tb = (time_t *)b;
392 return *tb - *ta;
395 static int
396 test_generalized_time (void)
398 struct test_case tests[] = {
399 {NULL, 15, "19700101000000Z"},
400 {NULL, 15, "19851106210627Z"}
402 time_t values[] = {0, 500159187};
403 int i, ret;
404 int ntests = sizeof(tests) / sizeof(*tests);
406 for (i = 0; i < ntests; ++i) {
407 tests[i].val = &values[i];
408 asprintf (&tests[i].name, "time %d", (int)values[i]);
409 if (tests[i].name == NULL)
410 errx(1, "malloc");
413 ret = generic_test (tests, ntests, sizeof(time_t),
414 (generic_encode)der_put_generalized_time,
415 (generic_length)der_length_generalized_time,
416 (generic_decode)der_get_generalized_time,
417 (generic_free)NULL,
418 cmp_generalized_time,
419 NULL);
420 for (i = 0; i < ntests; ++i)
421 free(tests[i].name);
422 return ret;
425 static int
426 test_cmp_oid (void *a, void *b)
428 return der_heim_oid_cmp((heim_oid *)a, (heim_oid *)b);
431 static unsigned oid_comp1[] = { 1, 1, 1 };
432 static unsigned oid_comp2[] = { 1, 1 };
433 static unsigned oid_comp3[] = { 6, 15, 1 };
434 static unsigned oid_comp4[] = { 6, 15 };
436 static int
437 test_oid (void)
439 struct test_case tests[] = {
440 {NULL, 2, "\x29\x01"},
441 {NULL, 1, "\x29"},
442 {NULL, 2, "\xff\x01"},
443 {NULL, 1, "\xff"}
445 heim_oid values[] = {
446 { 3, oid_comp1 },
447 { 2, oid_comp2 },
448 { 3, oid_comp3 },
449 { 2, oid_comp4 }
451 int i, ret;
452 int ntests = sizeof(tests) / sizeof(*tests);
454 for (i = 0; i < ntests; ++i) {
455 tests[i].val = &values[i];
456 asprintf (&tests[i].name, "oid %d", i);
457 if (tests[i].name == NULL)
458 errx(1, "malloc");
461 ret = generic_test (tests, ntests, sizeof(heim_oid),
462 (generic_encode)der_put_oid,
463 (generic_length)der_length_oid,
464 (generic_decode)der_get_oid,
465 (generic_free)der_free_oid,
466 test_cmp_oid,
467 NULL);
468 for (i = 0; i < ntests; ++i)
469 free(tests[i].name);
470 return ret;
473 static int
474 test_cmp_bit_string (void *a, void *b)
476 return der_heim_bit_string_cmp((heim_bit_string *)a, (heim_bit_string *)b);
479 static int
480 test_bit_string (void)
482 struct test_case tests[] = {
483 {NULL, 1, "\x00"}
485 heim_bit_string values[] = {
486 { 0, "" }
488 int i, ret;
489 int ntests = sizeof(tests) / sizeof(*tests);
491 for (i = 0; i < ntests; ++i) {
492 tests[i].val = &values[i];
493 asprintf (&tests[i].name, "bit_string %d", i);
494 if (tests[i].name == NULL)
495 errx(1, "malloc");
498 ret = generic_test (tests, ntests, sizeof(heim_bit_string),
499 (generic_encode)der_put_bit_string,
500 (generic_length)der_length_bit_string,
501 (generic_decode)der_get_bit_string,
502 (generic_free)der_free_bit_string,
503 test_cmp_bit_string,
504 NULL);
505 for (i = 0; i < ntests; ++i)
506 free(tests[i].name);
507 return ret;
510 static int
511 test_cmp_heim_integer (void *a, void *b)
513 return der_heim_integer_cmp((heim_integer *)a, (heim_integer *)b);
516 static int
517 test_heim_integer (void)
519 struct test_case tests[] = {
520 {NULL, 2, "\xfe\x01"},
521 {NULL, 2, "\xef\x01"},
522 {NULL, 3, "\xff\x00\xff"},
523 {NULL, 3, "\xff\x01\x00"},
524 {NULL, 1, "\x00"},
525 {NULL, 1, "\x01"},
526 {NULL, 2, "\x00\x80"}
529 heim_integer values[] = {
530 { 2, "\x01\xff", 1 },
531 { 2, "\x10\xff", 1 },
532 { 2, "\xff\x01", 1 },
533 { 2, "\xff\x00", 1 },
534 { 0, "", 0 },
535 { 1, "\x01", 0 },
536 { 1, "\x80", 0 }
538 int i, ret;
539 int ntests = sizeof(tests) / sizeof(tests[0]);
540 size_t size;
541 heim_integer i2;
543 for (i = 0; i < ntests; ++i) {
544 tests[i].val = &values[i];
545 asprintf (&tests[i].name, "heim_integer %d", i);
546 if (tests[i].name == NULL)
547 errx(1, "malloc");
550 ret = generic_test (tests, ntests, sizeof(heim_integer),
551 (generic_encode)der_put_heim_integer,
552 (generic_length)der_length_heim_integer,
553 (generic_decode)der_get_heim_integer,
554 (generic_free)der_free_heim_integer,
555 test_cmp_heim_integer,
556 NULL);
557 for (i = 0; i < ntests; ++i)
558 free (tests[i].name);
559 if (ret)
560 return ret;
562 /* test zero length integer (BER format) */
563 ret = der_get_heim_integer(NULL, 0, &i2, &size);
564 if (ret)
565 errx(1, "der_get_heim_integer");
566 if (i2.length != 0)
567 errx(1, "der_get_heim_integer wrong length");
568 der_free_heim_integer(&i2);
570 return 0;
573 static int
574 test_cmp_boolean (void *a, void *b)
576 return !!*(int *)a != !!*(int *)b;
579 static int
580 test_boolean (void)
582 struct test_case tests[] = {
583 {NULL, 1, "\xff"},
584 {NULL, 1, "\x00"}
587 int values[] = { 1, 0 };
588 int i, ret;
589 int ntests = sizeof(tests) / sizeof(tests[0]);
590 size_t size;
591 heim_integer i2;
593 for (i = 0; i < ntests; ++i) {
594 tests[i].val = &values[i];
595 asprintf (&tests[i].name, "heim_boolean %d", i);
596 if (tests[i].name == NULL)
597 errx(1, "malloc");
600 ret = generic_test (tests, ntests, sizeof(int),
601 (generic_encode)der_put_boolean,
602 (generic_length)der_length_boolean,
603 (generic_decode)der_get_boolean,
604 (generic_free)NULL,
605 test_cmp_boolean,
606 NULL);
607 for (i = 0; i < ntests; ++i)
608 free (tests[i].name);
609 if (ret)
610 return ret;
612 /* test zero length integer (BER format) */
613 ret = der_get_heim_integer(NULL, 0, &i2, &size);
614 if (ret)
615 errx(1, "der_get_heim_integer");
616 if (i2.length != 0)
617 errx(1, "der_get_heim_integer wrong length");
618 der_free_heim_integer(&i2);
620 return 0;
623 static int
624 check_fail_unsigned(void)
626 struct test_case tests[] = {
627 {NULL, sizeof(unsigned) + 1,
628 "\x01\x01\x01\x01\x01\x01\x01\x01\x01", "data overrun" }
630 int ntests = sizeof(tests) / sizeof(*tests);
632 return generic_decode_fail(tests, ntests, sizeof(unsigned),
633 (generic_decode)der_get_unsigned);
636 static int
637 check_fail_integer(void)
639 struct test_case tests[] = {
640 {NULL, sizeof(int) + 1,
641 "\x01\x01\x01\x01\x01\x01\x01\x01\x01", "data overrun" }
643 int ntests = sizeof(tests) / sizeof(*tests);
645 return generic_decode_fail(tests, ntests, sizeof(int),
646 (generic_decode)der_get_integer);
649 static int
650 check_fail_length(void)
652 struct test_case tests[] = {
653 {NULL, 0, "", "empty input data"},
654 {NULL, 1, "\x82", "internal length overrun" }
656 int ntests = sizeof(tests) / sizeof(*tests);
658 return generic_decode_fail(tests, ntests, sizeof(size_t),
659 (generic_decode)der_get_length);
662 static int
663 check_fail_boolean(void)
665 struct test_case tests[] = {
666 {NULL, 0, "", "empty input data"}
668 int ntests = sizeof(tests) / sizeof(*tests);
670 return generic_decode_fail(tests, ntests, sizeof(int),
671 (generic_decode)der_get_boolean);
674 static int
675 check_fail_general_string(void)
677 struct test_case tests[] = {
678 { NULL, 3, "A\x00i", "NUL char in string"}
680 int ntests = sizeof(tests) / sizeof(*tests);
682 return generic_decode_fail(tests, ntests, sizeof(heim_general_string),
683 (generic_decode)der_get_general_string);
686 static int
687 check_fail_bmp_string(void)
689 struct test_case tests[] = {
690 {NULL, 1, "\x00", "odd (1) length bmpstring"},
691 {NULL, 3, "\x00\x00\x00", "odd (3) length bmpstring"}
693 int ntests = sizeof(tests) / sizeof(*tests);
695 return generic_decode_fail(tests, ntests, sizeof(heim_bmp_string),
696 (generic_decode)der_get_bmp_string);
699 static int
700 check_fail_universal_string(void)
702 struct test_case tests[] = {
703 {NULL, 1, "\x00", "x & 3 == 1 universal string"},
704 {NULL, 2, "\x00\x00", "x & 3 == 2 universal string"},
705 {NULL, 3, "\x00\x00\x00", "x & 3 == 3 universal string"},
706 {NULL, 5, "\x00\x00\x00\x00\x00", "x & 3 == 1 universal string"},
707 {NULL, 6, "\x00\x00\x00\x00\x00\x00", "x & 3 == 2 universal string"},
708 {NULL, 7, "\x00\x00\x00\x00\x00\x00\x00", "x & 3 == 3 universal string"}
710 int ntests = sizeof(tests) / sizeof(*tests);
712 return generic_decode_fail(tests, ntests, sizeof(heim_universal_string),
713 (generic_decode)der_get_universal_string);
716 static int
717 check_fail_heim_integer(void)
719 #if 0
720 struct test_case tests[] = {
722 int ntests = sizeof(tests) / sizeof(*tests);
724 return generic_decode_fail(tests, ntests, sizeof(heim_integer),
725 (generic_decode)der_get_heim_integer);
726 #else
727 return 0;
728 #endif
731 static int
732 check_fail_generalized_time(void)
734 struct test_case tests[] = {
735 {NULL, 1, "\x00", "no time"}
737 int ntests = sizeof(tests) / sizeof(*tests);
739 return generic_decode_fail(tests, ntests, sizeof(time_t),
740 (generic_decode)der_get_generalized_time);
743 static int
744 check_fail_oid(void)
746 struct test_case tests[] = {
747 {NULL, 0, "", "empty input data"},
748 {NULL, 2, "\x00\x80", "last byte continuation" },
749 {NULL, 11, "\x00\x81\x80\x80\x80\x80\x80\x80\x80\x80\x00",
750 "oid element overflow" }
752 int ntests = sizeof(tests) / sizeof(*tests);
754 return generic_decode_fail(tests, ntests, sizeof(heim_oid),
755 (generic_decode)der_get_oid);
758 static int
759 check_fail_bitstring(void)
761 struct test_case tests[] = {
762 {NULL, 0, "", "empty input data"},
763 {NULL, 1, "\x08", "larger then 8 bits trailer"},
764 {NULL, 1, "\x01", "to few bytes for bits"},
765 {NULL, -2, "\x00", "length overrun"},
766 {NULL, -1, "", "length to short"}
768 int ntests = sizeof(tests) / sizeof(*tests);
770 return generic_decode_fail(tests, ntests, sizeof(heim_bit_string),
771 (generic_decode)der_get_bit_string);
774 static int
775 check_heim_integer_same(const char *p, const char *norm_p, heim_integer *i)
777 heim_integer i2;
778 char *str;
779 int ret;
781 ret = der_print_hex_heim_integer(i, &str);
782 if (ret)
783 errx(1, "der_print_hex_heim_integer: %d", ret);
785 if (strcmp(str, norm_p) != 0)
786 errx(1, "der_print_hex_heim_integer: %s != %s", str, p);
788 ret = der_parse_hex_heim_integer(str, &i2);
789 if (ret)
790 errx(1, "der_parse_hex_heim_integer: %d", ret);
792 if (der_heim_integer_cmp(i, &i2) != 0)
793 errx(1, "der_heim_integer_cmp: p %s", p);
795 der_free_heim_integer(&i2);
796 free(str);
798 ret = der_parse_hex_heim_integer(p, &i2);
799 if (ret)
800 errx(1, "der_parse_hex_heim_integer: %d", ret);
802 if (der_heim_integer_cmp(i, &i2) != 0)
803 errx(1, "der_heim_integer_cmp: norm");
805 der_free_heim_integer(&i2);
807 return 0;
810 static int
811 test_heim_int_format(void)
813 heim_integer i = { 1, "\x10", 0 };
814 heim_integer i2 = { 1, "\x10", 1 };
815 heim_integer i3 = { 1, "\01", 0 };
816 char *p =
817 "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1"
818 "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD"
819 "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245"
820 "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED"
821 "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE65381"
822 "FFFFFFFF" "FFFFFFFF";
823 heim_integer bni = {
824 128,
825 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC9\x0F\xDA\xA2"
826 "\x21\x68\xC2\x34\xC4\xC6\x62\x8B\x80\xDC\x1C\xD1"
827 "\x29\x02\x4E\x08\x8A\x67\xCC\x74\x02\x0B\xBE\xA6"
828 "\x3B\x13\x9B\x22\x51\x4A\x08\x79\x8E\x34\x04\xDD"
829 "\xEF\x95\x19\xB3\xCD\x3A\x43\x1B\x30\x2B\x0A\x6D"
830 "\xF2\x5F\x14\x37\x4F\xE1\x35\x6D\x6D\x51\xC2\x45"
831 "\xE4\x85\xB5\x76\x62\x5E\x7E\xC6\xF4\x4C\x42\xE9"
832 "\xA6\x37\xED\x6B\x0B\xFF\x5C\xB6\xF4\x06\xB7\xED"
833 "\xEE\x38\x6B\xFB\x5A\x89\x9F\xA5\xAE\x9F\x24\x11"
834 "\x7C\x4B\x1F\xE6\x49\x28\x66\x51\xEC\xE6\x53\x81"
835 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
838 heim_integer f;
839 int ret = 0;
841 ret += check_heim_integer_same(p, p, &bni);
842 ret += check_heim_integer_same("10", "10", &i);
843 ret += check_heim_integer_same("00000010", "10", &i);
844 ret += check_heim_integer_same("-10", "-10", &i2);
845 ret += check_heim_integer_same("-00000010", "-10", &i2);
846 ret += check_heim_integer_same("01", "01", &i3);
847 ret += check_heim_integer_same("1", "01", &i3);
850 int r;
851 r = der_parse_hex_heim_integer("-", &f);
852 if (r == 0) {
853 der_free_heim_integer(&f);
854 ret++;
856 /* used to cause UMR */
857 r = der_parse_hex_heim_integer("00", &f);
858 if (r == 0)
859 der_free_heim_integer(&f);
860 else
861 ret++;
864 return ret;
867 static int
868 test_heim_oid_format_same(const char *str, const heim_oid *oid)
870 int ret;
871 char *p;
872 heim_oid o2;
874 ret = der_print_heim_oid(oid, ' ', &p);
875 if (ret) {
876 printf("fail to print oid: %s\n", str);
877 return 1;
879 ret = strcmp(p, str);
880 if (ret) {
881 printf("oid %s != formated oid %s\n", str, p);
882 free(p);
883 return ret;
886 ret = der_parse_heim_oid(p, " ", &o2);
887 if (ret) {
888 printf("failed to parse %s\n", p);
889 free(p);
890 return ret;
892 free(p);
893 ret = der_heim_oid_cmp(&o2, oid);
894 der_free_oid(&o2);
896 return ret;
899 static unsigned sha1_oid_tree[] = { 1, 3, 14, 3, 2, 26 };
901 static int
902 test_heim_oid_format(void)
904 heim_oid sha1 = { 6, sha1_oid_tree };
905 int ret = 0;
907 ret += test_heim_oid_format_same("1 3 14 3 2 26", &sha1);
909 return ret;
912 static int
913 check_trailing_nul(void)
915 int i, ret;
916 struct {
917 int fail;
918 const unsigned char *p;
919 size_t len;
920 const char *s;
921 size_t size;
922 } foo[] = {
923 { 1, (const unsigned char *)"foo\x00o", 5, NULL, 0 },
924 { 1, (const unsigned char *)"\x00o", 2, NULL, 0 },
925 { 0, (const unsigned char *)"\x00\x00\x00\x00\x00", 5, "", 5 },
926 { 0, (const unsigned char *)"\x00", 1, "", 1 },
927 { 0, (const unsigned char *)"", 0, "", 0 },
928 { 0, (const unsigned char *)"foo\x00\x00", 5, "foo", 5 },
929 { 0, (const unsigned char *)"foo\0", 4, "foo", 4 },
930 { 0, (const unsigned char *)"foo", 3, "foo", 3 }
933 for (i = 0; i < sizeof(foo)/sizeof(foo[0]); i++) {
934 char *s;
935 size_t size;
936 ret = der_get_general_string(foo[i].p, foo[i].len, &s, &size);
937 if (foo[i].fail) {
938 if (ret == 0)
939 errx(1, "check %d NULL didn't fail", i);
940 continue;
942 if (ret)
943 errx(1, "NULL check %d der_get_general_string failed", i);
944 if (foo[i].size != size)
945 errx(1, "NUL check i = %d size failed", i);
946 if (strcmp(foo[i].s, s) != 0)
947 errx(1, "NUL check i = %d content failed", i);
948 free(s);
950 return 0;
953 static int
954 test_misc_cmp(void)
956 int ret;
958 /* diffrent lengths are diffrent */
960 const heim_octet_string os1 = { 1, "a" } , os2 = { 0, NULL };
961 ret = der_heim_octet_string_cmp(&os1, &os2);
962 if (ret == 0)
963 return 1;
965 /* diffrent data are diffrent */
967 const heim_octet_string os1 = { 1, "a" } , os2 = { 1, "b" };
968 ret = der_heim_octet_string_cmp(&os1, &os2);
969 if (ret == 0)
970 return 1;
972 /* diffrent lengths are diffrent */
974 const heim_bit_string bs1 = { 8, "a" } , bs2 = { 7, "a" };
975 ret = der_heim_bit_string_cmp(&bs1, &bs2);
976 if (ret == 0)
977 return 1;
979 /* diffrent data are diffrent */
981 const heim_bit_string bs1 = { 7, "\x0f" } , bs2 = { 7, "\x02" };
982 ret = der_heim_bit_string_cmp(&bs1, &bs2);
983 if (ret == 0)
984 return 1;
986 /* diffrent lengths are diffrent */
988 uint16_t data = 1;
989 heim_bmp_string bs1 = { 1, NULL } , bs2 = { 0, NULL };
990 bs1.data = &data;
991 ret = der_heim_bmp_string_cmp(&bs1, &bs2);
992 if (ret == 0)
993 return 1;
995 /* diffrent lengths are diffrent */
997 uint32_t data;
998 heim_universal_string us1 = { 1, NULL } , us2 = { 0, NULL };
999 us1.data = &data;
1000 ret = der_heim_universal_string_cmp(&us1, &us2);
1001 if (ret == 0)
1002 return 1;
1004 /* same */
1006 uint32_t data = (uint32_t)'a';
1007 heim_universal_string us1 = { 1, NULL } , us2 = { 1, NULL };
1008 us1.data = &data;
1009 us2.data = &data;
1010 ret = der_heim_universal_string_cmp(&us1, &us2);
1011 if (ret != 0)
1012 return 1;
1015 return 0;
1018 static int
1019 corner_generalized_time(void)
1021 const char *str = "760520140000Z";
1022 size_t size;
1023 time_t t;
1024 int ret;
1026 ret = der_get_generalized_time((const unsigned char*)str, strlen(str),
1027 &t, &size);
1028 if (ret)
1029 return 1;
1030 return 0;
1033 static int
1034 corner_tag(void)
1036 struct {
1037 int ok;
1038 const char *ptr;
1039 size_t len;
1040 } tests[] = {
1041 { 1, "\x00", 1 },
1042 { 0, "\xff", 1 },
1043 { 0, "\xff\xff\xff\xff\xff\xff\xff\xff", 8 }
1045 int i, ret;
1046 Der_class cl;
1047 Der_type ty;
1048 unsigned int tag;
1049 size_t size;
1051 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
1052 ret = der_get_tag((const unsigned char*)tests[i].ptr,
1053 tests[i].len, &cl, &ty, &tag, &size);
1054 if (ret) {
1055 if (tests[i].ok)
1056 errx(1, "failed while shouldn't");
1057 } else {
1058 if (!tests[i].ok)
1059 errx(1, "passed while shouldn't");
1062 return 0;
1066 main(int argc, char **argv)
1068 int ret = 0;
1070 ret += test_integer ();
1071 ret += test_integer_more();
1072 ret += test_unsigned ();
1073 ret += test_octet_string ();
1074 ret += test_bmp_string ();
1075 ret += test_universal_string ();
1076 ret += test_general_string ();
1077 ret += test_generalized_time ();
1078 ret += test_oid ();
1079 ret += test_bit_string();
1080 ret += test_heim_integer();
1081 ret += test_boolean();
1083 ret += check_fail_unsigned();
1084 ret += check_fail_integer();
1085 ret += check_fail_length();
1086 ret += check_fail_boolean();
1087 ret += check_fail_general_string();
1088 ret += check_fail_bmp_string();
1089 ret += check_fail_universal_string();
1090 ret += check_fail_heim_integer();
1091 ret += check_fail_generalized_time();
1092 ret += check_fail_oid();
1093 ret += check_fail_bitstring();
1094 ret += test_heim_int_format();
1095 ret += test_heim_oid_format();
1096 ret += check_trailing_nul();
1097 ret += test_misc_cmp();
1098 ret += corner_generalized_time();
1099 ret += corner_tag();
1101 return ret;