No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / asn1 / check-der.c
blob4515f5eb6a29a8800a5b4cf6935cca98584d626d
1 /*
2 * Copyright (c) 1999 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "der_locl.h"
35 #include <err.h>
36 #include <roken.h>
38 #include <asn1-common.h>
39 #include <asn1_err.h>
40 #include <der.h>
42 #include "check-common.h"
44 __RCSID("$Heimdal: check-der.c 21359 2007-06-27 08:15:41Z lha $"
45 "$NetBSD$");
47 static int
48 cmp_integer (void *a, void *b)
50 int *ia = (int *)a;
51 int *ib = (int *)b;
53 return *ib - *ia;
56 static int
57 test_integer (void)
59 struct test_case tests[] = {
60 {NULL, 1, "\x00"},
61 {NULL, 1, "\x7f"},
62 {NULL, 2, "\x00\x80"},
63 {NULL, 2, "\x01\x00"},
64 {NULL, 1, "\x80"},
65 {NULL, 2, "\xff\x7f"},
66 {NULL, 1, "\xff"},
67 {NULL, 2, "\xff\x01"},
68 {NULL, 2, "\x00\xff"},
69 {NULL, 4, "\x7f\xff\xff\xff"}
72 int values[] = {0, 127, 128, 256, -128, -129, -1, -255, 255,
73 0x7fffffff};
74 int i, ret;
75 int ntests = sizeof(tests) / sizeof(*tests);
77 for (i = 0; i < ntests; ++i) {
78 tests[i].val = &values[i];
79 asprintf (&tests[i].name, "integer %d", values[i]);
80 if (tests[i].name == NULL)
81 errx(1, "malloc");
84 ret = generic_test (tests, ntests, sizeof(int),
85 (generic_encode)der_put_integer,
86 (generic_length) der_length_integer,
87 (generic_decode)der_get_integer,
88 (generic_free)NULL,
89 cmp_integer);
91 for (i = 0; i < ntests; ++i)
92 free (tests[i].name);
93 return ret;
96 static int
97 test_one_int(int val)
99 int ret, dval;
100 unsigned char *buf;
101 size_t len_len, len;
103 len = _heim_len_int(val);
105 buf = emalloc(len + 2);
107 buf[0] = '\xff';
108 buf[len + 1] = '\xff';
109 memset(buf + 1, 0, len);
111 ret = der_put_integer(buf + 1 + len - 1, len, &val, &len_len);
112 if (ret) {
113 printf("integer %d encode failed %d\n", val, ret);
114 return 1;
116 if (len != len_len) {
117 printf("integer %d encode fail with %d len %lu, result len %lu\n",
118 val, ret, (unsigned long)len, (unsigned long)len_len);
119 return 1;
122 ret = der_get_integer(buf + 1, len, &dval, &len_len);
123 if (ret) {
124 printf("integer %d decode failed %d\n", val, ret);
125 return 1;
127 if (len != len_len) {
128 printf("integer %d decoded diffrent len %lu != %lu",
129 val, (unsigned long)len, (unsigned long)len_len);
130 return 1;
132 if (val != dval) {
133 printf("decode decoded to diffrent value %d != %d",
134 val, dval);
135 return 1;
138 if (buf[0] != (unsigned char)'\xff') {
139 printf("precanary dead %d\n", val);
140 return 1;
142 if (buf[len + 1] != (unsigned char)'\xff') {
143 printf("postecanary dead %d\n", val);
144 return 1;
146 free(buf);
147 return 0;
150 static int
151 test_integer_more (void)
153 int i, n1, n2, n3, n4, n5, n6;
155 n2 = 0;
156 for (i = 0; i < (sizeof(int) * 8); i++) {
157 n1 = 0x01 << i;
158 n2 = n2 | n1;
159 n3 = ~n1;
160 n4 = ~n2;
161 n5 = (-1) & ~(0x3f << i);
162 n6 = (-1) & ~(0x7f << i);
164 test_one_int(n1);
165 test_one_int(n2);
166 test_one_int(n3);
167 test_one_int(n4);
168 test_one_int(n5);
169 test_one_int(n6);
171 return 0;
174 static int
175 cmp_unsigned (void *a, void *b)
177 return *(unsigned int*)b - *(unsigned int*)a;
180 static int
181 test_unsigned (void)
183 struct test_case tests[] = {
184 {NULL, 1, "\x00"},
185 {NULL, 1, "\x7f"},
186 {NULL, 2, "\x00\x80"},
187 {NULL, 2, "\x01\x00"},
188 {NULL, 2, "\x02\x00"},
189 {NULL, 3, "\x00\x80\x00"},
190 {NULL, 5, "\x00\x80\x00\x00\x00"},
191 {NULL, 4, "\x7f\xff\xff\xff"}
194 unsigned int values[] = {0, 127, 128, 256, 512, 32768,
195 0x80000000, 0x7fffffff};
196 int i, ret;
197 int ntests = sizeof(tests) / sizeof(*tests);
199 for (i = 0; i < ntests; ++i) {
200 tests[i].val = &values[i];
201 asprintf (&tests[i].name, "unsigned %u", values[i]);
202 if (tests[i].name == NULL)
203 errx(1, "malloc");
206 ret = generic_test (tests, ntests, sizeof(int),
207 (generic_encode)der_put_unsigned,
208 (generic_length)der_length_unsigned,
209 (generic_decode)der_get_unsigned,
210 (generic_free)NULL,
211 cmp_unsigned);
212 for (i = 0; i < ntests; ++i)
213 free (tests[i].name);
214 return ret;
217 static int
218 cmp_octet_string (void *a, void *b)
220 heim_octet_string *oa = (heim_octet_string *)a;
221 heim_octet_string *ob = (heim_octet_string *)b;
223 if (oa->length != ob->length)
224 return ob->length - oa->length;
226 return (memcmp (oa->data, ob->data, oa->length));
229 static int
230 test_octet_string (void)
232 heim_octet_string s1 = {8, "\x01\x23\x45\x67\x89\xab\xcd\xef"};
234 struct test_case tests[] = {
235 {NULL, 8, "\x01\x23\x45\x67\x89\xab\xcd\xef"}
237 int ntests = sizeof(tests) / sizeof(*tests);
238 int ret;
240 tests[0].val = &s1;
241 asprintf (&tests[0].name, "a octet string");
242 if (tests[0].name == NULL)
243 errx(1, "malloc");
245 ret = generic_test (tests, ntests, sizeof(heim_octet_string),
246 (generic_encode)der_put_octet_string,
247 (generic_length)der_length_octet_string,
248 (generic_decode)der_get_octet_string,
249 (generic_free)der_free_octet_string,
250 cmp_octet_string);
251 free(tests[0].name);
252 return ret;
255 static int
256 cmp_bmp_string (void *a, void *b)
258 heim_bmp_string *oa = (heim_bmp_string *)a;
259 heim_bmp_string *ob = (heim_bmp_string *)b;
261 return der_heim_bmp_string_cmp(oa, ob);
264 static uint16_t bmp_d1[] = { 32 };
265 static uint16_t bmp_d2[] = { 32, 32 };
267 static int
268 test_bmp_string (void)
270 heim_bmp_string s1 = { 1, bmp_d1 };
271 heim_bmp_string s2 = { 2, bmp_d2 };
273 struct test_case tests[] = {
274 {NULL, 2, "\x00\x20"},
275 {NULL, 4, "\x00\x20\x00\x20"}
277 int ntests = sizeof(tests) / sizeof(*tests);
278 int ret;
280 tests[0].val = &s1;
281 asprintf (&tests[0].name, "a bmp string");
282 if (tests[0].name == NULL)
283 errx(1, "malloc");
284 tests[1].val = &s2;
285 asprintf (&tests[1].name, "second bmp string");
286 if (tests[1].name == NULL)
287 errx(1, "malloc");
289 ret = generic_test (tests, ntests, sizeof(heim_bmp_string),
290 (generic_encode)der_put_bmp_string,
291 (generic_length)der_length_bmp_string,
292 (generic_decode)der_get_bmp_string,
293 (generic_free)der_free_bmp_string,
294 cmp_bmp_string);
295 free(tests[0].name);
296 free(tests[1].name);
297 return ret;
300 static int
301 cmp_universal_string (void *a, void *b)
303 heim_universal_string *oa = (heim_universal_string *)a;
304 heim_universal_string *ob = (heim_universal_string *)b;
306 return der_heim_universal_string_cmp(oa, ob);
309 static uint32_t universal_d1[] = { 32 };
310 static uint32_t universal_d2[] = { 32, 32 };
312 static int
313 test_universal_string (void)
315 heim_universal_string s1 = { 1, universal_d1 };
316 heim_universal_string s2 = { 2, universal_d2 };
318 struct test_case tests[] = {
319 {NULL, 4, "\x00\x00\x00\x20"},
320 {NULL, 8, "\x00\x00\x00\x20\x00\x00\x00\x20"}
322 int ntests = sizeof(tests) / sizeof(*tests);
323 int ret;
325 tests[0].val = &s1;
326 asprintf (&tests[0].name, "a universal string");
327 if (tests[0].name == NULL)
328 errx(1, "malloc");
329 tests[1].val = &s2;
330 asprintf (&tests[1].name, "second universal string");
331 if (tests[1].name == NULL)
332 errx(1, "malloc");
334 ret = generic_test (tests, ntests, sizeof(heim_universal_string),
335 (generic_encode)der_put_universal_string,
336 (generic_length)der_length_universal_string,
337 (generic_decode)der_get_universal_string,
338 (generic_free)der_free_universal_string,
339 cmp_universal_string);
340 free(tests[0].name);
341 free(tests[1].name);
342 return ret;
345 static int
346 cmp_general_string (void *a, void *b)
348 char **sa = (char **)a;
349 char **sb = (char **)b;
351 return strcmp (*sa, *sb);
354 static int
355 test_general_string (void)
357 char *s1 = "Test User 1";
359 struct test_case tests[] = {
360 {NULL, 11, "\x54\x65\x73\x74\x20\x55\x73\x65\x72\x20\x31"}
362 int ret, ntests = sizeof(tests) / sizeof(*tests);
364 tests[0].val = &s1;
365 asprintf (&tests[0].name, "the string \"%s\"", s1);
366 if (tests[0].name == NULL)
367 errx(1, "malloc");
369 ret = generic_test (tests, ntests, sizeof(unsigned char *),
370 (generic_encode)der_put_general_string,
371 (generic_length)der_length_general_string,
372 (generic_decode)der_get_general_string,
373 (generic_free)der_free_general_string,
374 cmp_general_string);
375 free(tests[0].name);
376 return ret;
379 static int
380 cmp_generalized_time (void *a, void *b)
382 time_t *ta = (time_t *)a;
383 time_t *tb = (time_t *)b;
385 return *tb - *ta;
388 static int
389 test_generalized_time (void)
391 struct test_case tests[] = {
392 {NULL, 15, "19700101000000Z"},
393 {NULL, 15, "19851106210627Z"}
395 time_t values[] = {0, 500159187};
396 int i, ret;
397 int ntests = sizeof(tests) / sizeof(*tests);
399 for (i = 0; i < ntests; ++i) {
400 tests[i].val = &values[i];
401 asprintf (&tests[i].name, "time %d", (int)values[i]);
402 if (tests[i].name == NULL)
403 errx(1, "malloc");
406 ret = generic_test (tests, ntests, sizeof(time_t),
407 (generic_encode)der_put_generalized_time,
408 (generic_length)der_length_generalized_time,
409 (generic_decode)der_get_generalized_time,
410 (generic_free)NULL,
411 cmp_generalized_time);
412 for (i = 0; i < ntests; ++i)
413 free(tests[i].name);
414 return ret;
417 static int
418 test_cmp_oid (void *a, void *b)
420 return der_heim_oid_cmp((heim_oid *)a, (heim_oid *)b);
423 static unsigned oid_comp1[] = { 1, 1, 1 };
424 static unsigned oid_comp2[] = { 1, 1 };
425 static unsigned oid_comp3[] = { 6, 15, 1 };
426 static unsigned oid_comp4[] = { 6, 15 };
428 static int
429 test_oid (void)
431 struct test_case tests[] = {
432 {NULL, 2, "\x29\x01"},
433 {NULL, 1, "\x29"},
434 {NULL, 2, "\xff\x01"},
435 {NULL, 1, "\xff"}
437 heim_oid values[] = {
438 { 3, oid_comp1 },
439 { 2, oid_comp2 },
440 { 3, oid_comp3 },
441 { 2, oid_comp4 }
443 int i, ret;
444 int ntests = sizeof(tests) / sizeof(*tests);
446 for (i = 0; i < ntests; ++i) {
447 tests[i].val = &values[i];
448 asprintf (&tests[i].name, "oid %d", i);
449 if (tests[i].name == NULL)
450 errx(1, "malloc");
453 ret = generic_test (tests, ntests, sizeof(heim_oid),
454 (generic_encode)der_put_oid,
455 (generic_length)der_length_oid,
456 (generic_decode)der_get_oid,
457 (generic_free)der_free_oid,
458 test_cmp_oid);
459 for (i = 0; i < ntests; ++i)
460 free(tests[i].name);
461 return ret;
464 static int
465 test_cmp_bit_string (void *a, void *b)
467 return der_heim_bit_string_cmp((heim_bit_string *)a, (heim_bit_string *)b);
470 static int
471 test_bit_string (void)
473 struct test_case tests[] = {
474 {NULL, 1, "\x00"}
476 heim_bit_string values[] = {
477 { 0, "" }
479 int i, ret;
480 int ntests = sizeof(tests) / sizeof(*tests);
482 for (i = 0; i < ntests; ++i) {
483 tests[i].val = &values[i];
484 asprintf (&tests[i].name, "bit_string %d", i);
485 if (tests[i].name == NULL)
486 errx(1, "malloc");
489 ret = generic_test (tests, ntests, sizeof(heim_bit_string),
490 (generic_encode)der_put_bit_string,
491 (generic_length)der_length_bit_string,
492 (generic_decode)der_get_bit_string,
493 (generic_free)der_free_bit_string,
494 test_cmp_bit_string);
495 for (i = 0; i < ntests; ++i)
496 free(tests[i].name);
497 return ret;
500 static int
501 test_cmp_heim_integer (void *a, void *b)
503 return der_heim_integer_cmp((heim_integer *)a, (heim_integer *)b);
506 static int
507 test_heim_integer (void)
509 struct test_case tests[] = {
510 {NULL, 2, "\xfe\x01"},
511 {NULL, 2, "\xef\x01"},
512 {NULL, 3, "\xff\x00\xff"},
513 {NULL, 3, "\xff\x01\x00"},
514 {NULL, 1, "\x00"},
515 {NULL, 1, "\x01"},
516 {NULL, 2, "\x00\x80"}
519 heim_integer values[] = {
520 { 2, "\x01\xff", 1 },
521 { 2, "\x10\xff", 1 },
522 { 2, "\xff\x01", 1 },
523 { 2, "\xff\x00", 1 },
524 { 0, "", 0 },
525 { 1, "\x01", 0 },
526 { 1, "\x80", 0 }
528 int i, ret;
529 int ntests = sizeof(tests) / sizeof(tests[0]);
530 size_t size;
531 heim_integer i2;
533 for (i = 0; i < ntests; ++i) {
534 tests[i].val = &values[i];
535 asprintf (&tests[i].name, "heim_integer %d", i);
536 if (tests[i].name == NULL)
537 errx(1, "malloc");
540 ret = generic_test (tests, ntests, sizeof(heim_integer),
541 (generic_encode)der_put_heim_integer,
542 (generic_length)der_length_heim_integer,
543 (generic_decode)der_get_heim_integer,
544 (generic_free)der_free_heim_integer,
545 test_cmp_heim_integer);
546 for (i = 0; i < ntests; ++i)
547 free (tests[i].name);
548 if (ret)
549 return ret;
551 /* test zero length integer (BER format) */
552 ret = der_get_heim_integer(NULL, 0, &i2, &size);
553 if (ret)
554 errx(1, "der_get_heim_integer");
555 if (i2.length != 0)
556 errx(1, "der_get_heim_integer wrong length");
557 der_free_heim_integer(&i2);
559 return 0;
562 static int
563 test_cmp_boolean (void *a, void *b)
565 return !!*(int *)a != !!*(int *)b;
568 static int
569 test_boolean (void)
571 struct test_case tests[] = {
572 {NULL, 1, "\xff"},
573 {NULL, 1, "\x00"}
576 int values[] = { 1, 0 };
577 int i, ret;
578 int ntests = sizeof(tests) / sizeof(tests[0]);
579 size_t size;
580 heim_integer i2;
582 for (i = 0; i < ntests; ++i) {
583 tests[i].val = &values[i];
584 asprintf (&tests[i].name, "heim_boolean %d", i);
585 if (tests[i].name == NULL)
586 errx(1, "malloc");
589 ret = generic_test (tests, ntests, sizeof(int),
590 (generic_encode)der_put_boolean,
591 (generic_length)der_length_boolean,
592 (generic_decode)der_get_boolean,
593 (generic_free)NULL,
594 test_cmp_boolean);
595 for (i = 0; i < ntests; ++i)
596 free (tests[i].name);
597 if (ret)
598 return ret;
600 /* test zero length integer (BER format) */
601 ret = der_get_heim_integer(NULL, 0, &i2, &size);
602 if (ret)
603 errx(1, "der_get_heim_integer");
604 if (i2.length != 0)
605 errx(1, "der_get_heim_integer wrong length");
606 der_free_heim_integer(&i2);
608 return 0;
611 static int
612 check_fail_unsigned(void)
614 struct test_case tests[] = {
615 {NULL, sizeof(unsigned) + 1,
616 "\x01\x01\x01\x01\x01\x01\x01\x01\x01", "data overrun" }
618 int ntests = sizeof(tests) / sizeof(*tests);
620 return generic_decode_fail(tests, ntests, sizeof(unsigned),
621 (generic_decode)der_get_unsigned);
624 static int
625 check_fail_integer(void)
627 struct test_case tests[] = {
628 {NULL, sizeof(int) + 1,
629 "\x01\x01\x01\x01\x01\x01\x01\x01\x01", "data overrun" }
631 int ntests = sizeof(tests) / sizeof(*tests);
633 return generic_decode_fail(tests, ntests, sizeof(int),
634 (generic_decode)der_get_integer);
637 static int
638 check_fail_length(void)
640 struct test_case tests[] = {
641 {NULL, 0, "", "empty input data"},
642 {NULL, 1, "\x82", "internal length overrun" }
644 int ntests = sizeof(tests) / sizeof(*tests);
646 return generic_decode_fail(tests, ntests, sizeof(size_t),
647 (generic_decode)der_get_length);
650 static int
651 check_fail_boolean(void)
653 struct test_case tests[] = {
654 {NULL, 0, "", "empty input data"}
656 int ntests = sizeof(tests) / sizeof(*tests);
658 return generic_decode_fail(tests, ntests, sizeof(int),
659 (generic_decode)der_get_boolean);
662 static int
663 check_fail_general_string(void)
665 struct test_case tests[] = {
666 { NULL, 3, "A\x00i", "NUL char in string"}
668 int ntests = sizeof(tests) / sizeof(*tests);
670 return generic_decode_fail(tests, ntests, sizeof(heim_general_string),
671 (generic_decode)der_get_general_string);
674 static int
675 check_fail_bmp_string(void)
677 struct test_case tests[] = {
678 {NULL, 1, "\x00", "odd (1) length bmpstring"},
679 {NULL, 3, "\x00\x00\x00", "odd (3) length bmpstring"}
681 int ntests = sizeof(tests) / sizeof(*tests);
683 return generic_decode_fail(tests, ntests, sizeof(heim_bmp_string),
684 (generic_decode)der_get_bmp_string);
687 static int
688 check_fail_universal_string(void)
690 struct test_case tests[] = {
691 {NULL, 1, "\x00", "x & 3 == 1 universal string"},
692 {NULL, 2, "\x00\x00", "x & 3 == 2 universal string"},
693 {NULL, 3, "\x00\x00\x00", "x & 3 == 3 universal string"},
694 {NULL, 5, "\x00\x00\x00\x00\x00", "x & 3 == 1 universal string"},
695 {NULL, 6, "\x00\x00\x00\x00\x00\x00", "x & 3 == 2 universal string"},
696 {NULL, 7, "\x00\x00\x00\x00\x00\x00\x00", "x & 3 == 3 universal string"}
698 int ntests = sizeof(tests) / sizeof(*tests);
700 return generic_decode_fail(tests, ntests, sizeof(heim_universal_string),
701 (generic_decode)der_get_universal_string);
704 static int
705 check_fail_heim_integer(void)
707 #if 0
708 struct test_case tests[] = {
710 int ntests = sizeof(tests) / sizeof(*tests);
712 return generic_decode_fail(tests, ntests, sizeof(heim_integer),
713 (generic_decode)der_get_heim_integer);
714 #else
715 return 0;
716 #endif
719 static int
720 check_fail_generalized_time(void)
722 struct test_case tests[] = {
723 {NULL, 1, "\x00", "no time"}
725 int ntests = sizeof(tests) / sizeof(*tests);
727 return generic_decode_fail(tests, ntests, sizeof(time_t),
728 (generic_decode)der_get_generalized_time);
731 static int
732 check_fail_oid(void)
734 struct test_case tests[] = {
735 {NULL, 0, "", "empty input data"},
736 {NULL, 2, "\x00\x80", "last byte continuation" },
737 {NULL, 11, "\x00\x81\x80\x80\x80\x80\x80\x80\x80\x80\x00",
738 "oid element overflow" }
740 int ntests = sizeof(tests) / sizeof(*tests);
742 return generic_decode_fail(tests, ntests, sizeof(heim_oid),
743 (generic_decode)der_get_oid);
746 static int
747 check_fail_bitstring(void)
749 struct test_case tests[] = {
750 {NULL, 0, "", "empty input data"},
751 {NULL, 1, "\x08", "larger then 8 bits trailer"},
752 {NULL, 1, "\x01", "to few bytes for bits"},
753 {NULL, -2, "\x00", "length overrun"},
754 {NULL, -1, "", "length to short"}
756 int ntests = sizeof(tests) / sizeof(*tests);
758 return generic_decode_fail(tests, ntests, sizeof(heim_bit_string),
759 (generic_decode)der_get_bit_string);
762 static int
763 check_heim_integer_same(const char *p, const char *norm_p, heim_integer *i)
765 heim_integer i2;
766 char *str;
767 int ret;
769 ret = der_print_hex_heim_integer(i, &str);
770 if (ret)
771 errx(1, "der_print_hex_heim_integer: %d", ret);
773 if (strcmp(str, norm_p) != 0)
774 errx(1, "der_print_hex_heim_integer: %s != %s", str, p);
776 ret = der_parse_hex_heim_integer(str, &i2);
777 if (ret)
778 errx(1, "der_parse_hex_heim_integer: %d", ret);
780 if (der_heim_integer_cmp(i, &i2) != 0)
781 errx(1, "der_heim_integer_cmp: p %s", p);
783 der_free_heim_integer(&i2);
784 free(str);
786 ret = der_parse_hex_heim_integer(p, &i2);
787 if (ret)
788 errx(1, "der_parse_hex_heim_integer: %d", ret);
790 if (der_heim_integer_cmp(i, &i2) != 0)
791 errx(1, "der_heim_integer_cmp: norm");
793 der_free_heim_integer(&i2);
795 return 0;
798 static int
799 test_heim_int_format(void)
801 heim_integer i = { 1, "\x10", 0 };
802 heim_integer i2 = { 1, "\x10", 1 };
803 heim_integer i3 = { 1, "\01", 0 };
804 char *p =
805 "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1"
806 "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD"
807 "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245"
808 "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED"
809 "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE65381"
810 "FFFFFFFF" "FFFFFFFF";
811 heim_integer bni = {
812 128,
813 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC9\x0F\xDA\xA2"
814 "\x21\x68\xC2\x34\xC4\xC6\x62\x8B\x80\xDC\x1C\xD1"
815 "\x29\x02\x4E\x08\x8A\x67\xCC\x74\x02\x0B\xBE\xA6"
816 "\x3B\x13\x9B\x22\x51\x4A\x08\x79\x8E\x34\x04\xDD"
817 "\xEF\x95\x19\xB3\xCD\x3A\x43\x1B\x30\x2B\x0A\x6D"
818 "\xF2\x5F\x14\x37\x4F\xE1\x35\x6D\x6D\x51\xC2\x45"
819 "\xE4\x85\xB5\x76\x62\x5E\x7E\xC6\xF4\x4C\x42\xE9"
820 "\xA6\x37\xED\x6B\x0B\xFF\x5C\xB6\xF4\x06\xB7\xED"
821 "\xEE\x38\x6B\xFB\x5A\x89\x9F\xA5\xAE\x9F\x24\x11"
822 "\x7C\x4B\x1F\xE6\x49\x28\x66\x51\xEC\xE6\x53\x81"
823 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
826 heim_integer f;
827 int ret = 0;
829 ret += check_heim_integer_same(p, p, &bni);
830 ret += check_heim_integer_same("10", "10", &i);
831 ret += check_heim_integer_same("00000010", "10", &i);
832 ret += check_heim_integer_same("-10", "-10", &i2);
833 ret += check_heim_integer_same("-00000010", "-10", &i2);
834 ret += check_heim_integer_same("01", "01", &i3);
835 ret += check_heim_integer_same("1", "01", &i3);
838 int r;
839 r = der_parse_hex_heim_integer("-", &f);
840 if (r == 0) {
841 der_free_heim_integer(&f);
842 ret++;
844 /* used to cause UMR */
845 r = der_parse_hex_heim_integer("00", &f);
846 if (r == 0)
847 der_free_heim_integer(&f);
848 else
849 ret++;
852 return ret;
855 static int
856 test_heim_oid_format_same(const char *str, const heim_oid *oid)
858 int ret;
859 char *p;
860 heim_oid o2;
862 ret = der_print_heim_oid(oid, ' ', &p);
863 if (ret) {
864 printf("fail to print oid: %s\n", str);
865 return 1;
867 ret = strcmp(p, str);
868 if (ret) {
869 printf("oid %s != formated oid %s\n", str, p);
870 free(p);
871 return ret;
874 ret = der_parse_heim_oid(p, " ", &o2);
875 if (ret) {
876 printf("failed to parse %s\n", p);
877 free(p);
878 return ret;
880 free(p);
881 ret = der_heim_oid_cmp(&o2, oid);
882 der_free_oid(&o2);
884 return ret;
887 static unsigned sha1_oid_tree[] = { 1, 3, 14, 3, 2, 26 };
889 static int
890 test_heim_oid_format(void)
892 heim_oid sha1 = { 6, sha1_oid_tree };
893 int ret = 0;
895 ret += test_heim_oid_format_same("1 3 14 3 2 26", &sha1);
897 return ret;
900 static int
901 check_trailing_nul(void)
903 int i, ret;
904 struct {
905 int fail;
906 const unsigned char *p;
907 size_t len;
908 const char *s;
909 size_t size;
910 } foo[] = {
911 { 1, (const unsigned char *)"foo\x00o", 5, NULL, 0 },
912 { 1, (const unsigned char *)"\x00o", 2, NULL, 0 },
913 { 0, (const unsigned char *)"\x00\x00\x00\x00\x00", 5, "", 5 },
914 { 0, (const unsigned char *)"\x00", 1, "", 1 },
915 { 0, (const unsigned char *)"", 0, "", 0 },
916 { 0, (const unsigned char *)"foo\x00\x00", 5, "foo", 5 },
917 { 0, (const unsigned char *)"foo\0", 4, "foo", 4 },
918 { 0, (const unsigned char *)"foo", 3, "foo", 3 }
921 for (i = 0; i < sizeof(foo)/sizeof(foo[0]); i++) {
922 char *s;
923 size_t size;
924 ret = der_get_general_string(foo[i].p, foo[i].len, &s, &size);
925 if (foo[i].fail) {
926 if (ret == 0)
927 errx(1, "check %d NULL didn't fail", i);
928 continue;
930 if (ret)
931 errx(1, "NULL check %d der_get_general_string failed", i);
932 if (foo[i].size != size)
933 errx(1, "NUL check i = %d size failed", i);
934 if (strcmp(foo[i].s, s) != 0)
935 errx(1, "NUL check i = %d content failed", i);
936 free(s);
938 return 0;
941 static int
942 test_misc_cmp(void)
944 int ret;
946 /* diffrent lengths are diffrent */
948 const heim_octet_string os1 = { 1, "a" } , os2 = { 0, NULL };
949 ret = der_heim_octet_string_cmp(&os1, &os2);
950 if (ret == 0)
951 return 1;
953 /* diffrent data are diffrent */
955 const heim_octet_string os1 = { 1, "a" } , os2 = { 1, "b" };
956 ret = der_heim_octet_string_cmp(&os1, &os2);
957 if (ret == 0)
958 return 1;
960 /* diffrent lengths are diffrent */
962 const heim_bit_string bs1 = { 8, "a" } , bs2 = { 7, "a" };
963 ret = der_heim_bit_string_cmp(&bs1, &bs2);
964 if (ret == 0)
965 return 1;
967 /* diffrent data are diffrent */
969 const heim_bit_string bs1 = { 7, "\x0f" } , bs2 = { 7, "\x02" };
970 ret = der_heim_bit_string_cmp(&bs1, &bs2);
971 if (ret == 0)
972 return 1;
974 /* diffrent lengths are diffrent */
976 uint16_t data = 1;
977 heim_bmp_string bs1 = { 1, NULL } , bs2 = { 0, NULL };
978 bs1.data = &data;
979 ret = der_heim_bmp_string_cmp(&bs1, &bs2);
980 if (ret == 0)
981 return 1;
983 /* diffrent lengths are diffrent */
985 uint32_t data;
986 heim_universal_string us1 = { 1, NULL } , us2 = { 0, NULL };
987 us1.data = &data;
988 ret = der_heim_universal_string_cmp(&us1, &us2);
989 if (ret == 0)
990 return 1;
992 /* same */
994 uint32_t data = (uint32_t)'a';
995 heim_universal_string us1 = { 1, NULL } , us2 = { 1, NULL };
996 us1.data = &data;
997 us2.data = &data;
998 ret = der_heim_universal_string_cmp(&us1, &us2);
999 if (ret != 0)
1000 return 1;
1003 return 0;
1006 static int
1007 corner_generalized_time(void)
1009 const char *str = "760520140000Z";
1010 size_t size;
1011 time_t t;
1012 int ret;
1014 ret = der_get_generalized_time((const unsigned char*)str, strlen(str),
1015 &t, &size);
1016 if (ret)
1017 return 1;
1018 return 0;
1021 static int
1022 corner_tag(void)
1024 struct {
1025 int ok;
1026 const char *ptr;
1027 size_t len;
1028 } tests[] = {
1029 { 1, "\x00", 1 },
1030 { 0, "\xff", 1 },
1031 { 0, "\xff\xff\xff\xff\xff\xff\xff\xff", 8 }
1033 int i, ret;
1034 Der_class cl;
1035 Der_type ty;
1036 unsigned int tag;
1037 size_t size;
1039 for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
1040 ret = der_get_tag((const unsigned char*)tests[i].ptr,
1041 tests[i].len, &cl, &ty, &tag, &size);
1042 if (ret) {
1043 if (tests[i].ok)
1044 errx(1, "failed while shouldn't");
1045 } else {
1046 if (!tests[i].ok)
1047 errx(1, "passed while shouldn't");
1050 return 0;
1054 main(int argc, char **argv)
1056 int ret = 0;
1058 ret += test_integer ();
1059 ret += test_integer_more();
1060 ret += test_unsigned ();
1061 ret += test_octet_string ();
1062 ret += test_bmp_string ();
1063 ret += test_universal_string ();
1064 ret += test_general_string ();
1065 ret += test_generalized_time ();
1066 ret += test_oid ();
1067 ret += test_bit_string();
1068 ret += test_heim_integer();
1069 ret += test_boolean();
1071 ret += check_fail_unsigned();
1072 ret += check_fail_integer();
1073 ret += check_fail_length();
1074 ret += check_fail_boolean();
1075 ret += check_fail_general_string();
1076 ret += check_fail_bmp_string();
1077 ret += check_fail_universal_string();
1078 ret += check_fail_heim_integer();
1079 ret += check_fail_generalized_time();
1080 ret += check_fail_oid();
1081 ret += check_fail_bitstring();
1082 ret += test_heim_int_format();
1083 ret += test_heim_oid_format();
1084 ret += check_trailing_nul();
1085 ret += test_misc_cmp();
1086 ret += corner_generalized_time();
1087 ret += corner_tag();
1089 return ret;