2 * Copyright (c) 1999 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
38 #include <asn1-common.h>
42 #include "check-common.h"
44 __RCSID("$Heimdal: check-der.c 21359 2007-06-27 08:15:41Z lha $"
48 cmp_integer (void *a
, void *b
)
59 struct test_case tests
[] = {
62 {NULL
, 2, "\x00\x80"},
63 {NULL
, 2, "\x01\x00"},
65 {NULL
, 2, "\xff\x7f"},
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,
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
)
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
,
91 for (i
= 0; i
< ntests
; ++i
)
103 len
= _heim_len_int(val
);
105 buf
= emalloc(len
+ 2);
108 buf
[len
+ 1] = '\xff';
109 memset(buf
+ 1, 0, len
);
111 ret
= der_put_integer(buf
+ 1 + len
- 1, len
, &val
, &len_len
);
113 printf("integer %d encode failed %d\n", val
, ret
);
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
);
122 ret
= der_get_integer(buf
+ 1, len
, &dval
, &len_len
);
124 printf("integer %d decode failed %d\n", val
, ret
);
127 if (len
!= len_len
) {
128 printf("integer %d decoded diffrent len %lu != %lu",
129 val
, (unsigned long)len
, (unsigned long)len_len
);
133 printf("decode decoded to diffrent value %d != %d",
138 if (buf
[0] != (unsigned char)'\xff') {
139 printf("precanary dead %d\n", val
);
142 if (buf
[len
+ 1] != (unsigned char)'\xff') {
143 printf("postecanary dead %d\n", val
);
151 test_integer_more (void)
153 int i
, n1
, n2
, n3
, n4
, n5
, n6
;
156 for (i
= 0; i
< (sizeof(int) * 8); i
++) {
161 n5
= (-1) & ~(0x3f << i
);
162 n6
= (-1) & ~(0x7f << i
);
175 cmp_unsigned (void *a
, void *b
)
177 return *(unsigned int*)b
- *(unsigned int*)a
;
183 struct test_case tests
[] = {
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};
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
)
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
,
212 for (i
= 0; i
< ntests
; ++i
)
213 free (tests
[i
].name
);
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
));
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
);
241 asprintf (&tests
[0].name
, "a octet string");
242 if (tests
[0].name
== NULL
)
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
,
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 };
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
);
281 asprintf (&tests
[0].name
, "a bmp string");
282 if (tests
[0].name
== NULL
)
285 asprintf (&tests
[1].name
, "second bmp string");
286 if (tests
[1].name
== NULL
)
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
,
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 };
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
);
326 asprintf (&tests
[0].name
, "a universal string");
327 if (tests
[0].name
== NULL
)
330 asprintf (&tests
[1].name
, "second universal string");
331 if (tests
[1].name
== NULL
)
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
);
346 cmp_general_string (void *a
, void *b
)
348 char **sa
= (char **)a
;
349 char **sb
= (char **)b
;
351 return strcmp (*sa
, *sb
);
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
);
365 asprintf (&tests
[0].name
, "the string \"%s\"", s1
);
366 if (tests
[0].name
== NULL
)
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
,
380 cmp_generalized_time (void *a
, void *b
)
382 time_t *ta
= (time_t *)a
;
383 time_t *tb
= (time_t *)b
;
389 test_generalized_time (void)
391 struct test_case tests
[] = {
392 {NULL
, 15, "19700101000000Z"},
393 {NULL
, 15, "19851106210627Z"}
395 time_t values
[] = {0, 500159187};
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
)
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
,
411 cmp_generalized_time
);
412 for (i
= 0; i
< ntests
; ++i
)
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 };
431 struct test_case tests
[] = {
432 {NULL
, 2, "\x29\x01"},
434 {NULL
, 2, "\xff\x01"},
437 heim_oid values
[] = {
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
)
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
,
459 for (i
= 0; i
< ntests
; ++i
)
465 test_cmp_bit_string (void *a
, void *b
)
467 return der_heim_bit_string_cmp((heim_bit_string
*)a
, (heim_bit_string
*)b
);
471 test_bit_string (void)
473 struct test_case tests
[] = {
476 heim_bit_string values
[] = {
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
)
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
)
501 test_cmp_heim_integer (void *a
, void *b
)
503 return der_heim_integer_cmp((heim_integer
*)a
, (heim_integer
*)b
);
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"},
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 },
529 int ntests
= sizeof(tests
) / sizeof(tests
[0]);
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
)
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
);
551 /* test zero length integer (BER format) */
552 ret
= der_get_heim_integer(NULL
, 0, &i2
, &size
);
554 errx(1, "der_get_heim_integer");
556 errx(1, "der_get_heim_integer wrong length");
557 der_free_heim_integer(&i2
);
563 test_cmp_boolean (void *a
, void *b
)
565 return !!*(int *)a
!= !!*(int *)b
;
571 struct test_case tests
[] = {
576 int values
[] = { 1, 0 };
578 int ntests
= sizeof(tests
) / sizeof(tests
[0]);
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
)
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
,
595 for (i
= 0; i
< ntests
; ++i
)
596 free (tests
[i
].name
);
600 /* test zero length integer (BER format) */
601 ret
= der_get_heim_integer(NULL
, 0, &i2
, &size
);
603 errx(1, "der_get_heim_integer");
605 errx(1, "der_get_heim_integer wrong length");
606 der_free_heim_integer(&i2
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
705 check_fail_heim_integer(void)
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
);
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
);
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
);
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
);
763 check_heim_integer_same(const char *p
, const char *norm_p
, heim_integer
*i
)
769 ret
= der_print_hex_heim_integer(i
, &str
);
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
);
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
);
786 ret
= der_parse_hex_heim_integer(p
, &i2
);
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
);
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 };
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";
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",
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
);
839 r
= der_parse_hex_heim_integer("-", &f
);
841 der_free_heim_integer(&f
);
844 /* used to cause UMR */
845 r
= der_parse_hex_heim_integer("00", &f
);
847 der_free_heim_integer(&f
);
856 test_heim_oid_format_same(const char *str
, const heim_oid
*oid
)
862 ret
= der_print_heim_oid(oid
, ' ', &p
);
864 printf("fail to print oid: %s\n", str
);
867 ret
= strcmp(p
, str
);
869 printf("oid %s != formated oid %s\n", str
, p
);
874 ret
= der_parse_heim_oid(p
, " ", &o2
);
876 printf("failed to parse %s\n", p
);
881 ret
= der_heim_oid_cmp(&o2
, oid
);
887 static unsigned sha1_oid_tree
[] = { 1, 3, 14, 3, 2, 26 };
890 test_heim_oid_format(void)
892 heim_oid sha1
= { 6, sha1_oid_tree
};
895 ret
+= test_heim_oid_format_same("1 3 14 3 2 26", &sha1
);
901 check_trailing_nul(void)
906 const unsigned char *p
;
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
++) {
924 ret
= der_get_general_string(foo
[i
].p
, foo
[i
].len
, &s
, &size
);
927 errx(1, "check %d NULL didn't fail", i
);
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
);
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
);
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
);
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
);
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
);
974 /* diffrent lengths are diffrent */
977 heim_bmp_string bs1
= { 1, NULL
} , bs2
= { 0, NULL
};
979 ret
= der_heim_bmp_string_cmp(&bs1
, &bs2
);
983 /* diffrent lengths are diffrent */
986 heim_universal_string us1
= { 1, NULL
} , us2
= { 0, NULL
};
988 ret
= der_heim_universal_string_cmp(&us1
, &us2
);
994 uint32_t data
= (uint32_t)'a';
995 heim_universal_string us1
= { 1, NULL
} , us2
= { 1, NULL
};
998 ret
= der_heim_universal_string_cmp(&us1
, &us2
);
1007 corner_generalized_time(void)
1009 const char *str
= "760520140000Z";
1014 ret
= der_get_generalized_time((const unsigned char*)str
, strlen(str
),
1031 { 0, "\xff\xff\xff\xff\xff\xff\xff\xff", 8 }
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
);
1044 errx(1, "failed while shouldn't");
1047 errx(1, "passed while shouldn't");
1054 main(int argc
, char **argv
)
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 ();
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();