1 /* $NetBSD: check-der.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
4 * Copyright (c) 1999 - 2007 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the Institute nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <krb5/roken.h>
42 #include <krb5/asn1-common.h>
43 #include <krb5/asn1_err.h>
46 #include "check-common.h"
51 cmp_integer (void *a
, void *b
)
62 struct test_case tests
[] = {
65 {NULL
, 2, "\x00\x80"},
66 {NULL
, 2, "\x01\x00"},
68 {NULL
, 2, "\xff\x7f"},
70 {NULL
, 2, "\xff\x01"},
71 {NULL
, 2, "\x00\xff"},
72 {NULL
, 4, "\x7f\xff\xff\xff"}
75 int values
[] = {0, 127, 128, 256, -128, -129, -1, -255, 255,
78 int ntests
= sizeof(tests
) / sizeof(*tests
);
80 for (i
= 0; i
< ntests
; ++i
) {
81 tests
[i
].val
= &values
[i
];
82 if (asprintf (&tests
[i
].name
, "integer %d", values
[i
]) < 0)
84 if (tests
[i
].name
== NULL
)
88 ret
= generic_test (tests
, ntests
, sizeof(int),
89 (generic_encode
)der_put_integer
,
90 (generic_length
) der_length_integer
,
91 (generic_decode
)der_get_integer
,
96 for (i
= 0; i
< ntests
; ++i
)
102 test_one_int(int val
)
108 len
= _heim_len_int(val
);
110 buf
= emalloc(len
+ 2);
113 buf
[len
+ 1] = '\xff';
114 memset(buf
+ 1, 0, len
);
116 ret
= der_put_integer(buf
+ 1 + len
- 1, len
, &val
, &len_len
);
118 printf("integer %d encode failed %d\n", val
, ret
);
121 if (len
!= len_len
) {
122 printf("integer %d encode fail with %d len %lu, result len %lu\n",
123 val
, ret
, (unsigned long)len
, (unsigned long)len_len
);
127 ret
= der_get_integer(buf
+ 1, len
, &dval
, &len_len
);
129 printf("integer %d decode failed %d\n", val
, ret
);
132 if (len
!= len_len
) {
133 printf("integer %d decoded diffrent len %lu != %lu",
134 val
, (unsigned long)len
, (unsigned long)len_len
);
138 printf("decode decoded to diffrent value %d != %d",
143 if (buf
[0] != (unsigned char)'\xff') {
144 printf("precanary dead %d\n", val
);
147 if (buf
[len
+ 1] != (unsigned char)'\xff') {
148 printf("postecanary dead %d\n", val
);
156 test_integer_more (void)
158 int i
, n1
, n2
, n3
, n4
, n5
, n6
;
161 for (i
= 0; i
< (sizeof(int) * 8); i
++) {
166 n5
= (-1) & ~(0x3f << i
);
167 n6
= (-1) & ~(0x7f << i
);
180 cmp_unsigned (void *a
, void *b
)
182 return *(unsigned int*)b
- *(unsigned int*)a
;
188 struct test_case tests
[] = {
191 {NULL
, 2, "\x00\x80"},
192 {NULL
, 2, "\x01\x00"},
193 {NULL
, 2, "\x02\x00"},
194 {NULL
, 3, "\x00\x80\x00"},
195 {NULL
, 5, "\x00\x80\x00\x00\x00"},
196 {NULL
, 4, "\x7f\xff\xff\xff"}
199 unsigned int values
[] = {0, 127, 128, 256, 512, 32768,
200 0x80000000, 0x7fffffff};
202 int ntests
= sizeof(tests
) / sizeof(*tests
);
204 for (i
= 0; i
< ntests
; ++i
) {
205 tests
[i
].val
= &values
[i
];
206 if (asprintf (&tests
[i
].name
, "unsigned %u", values
[i
]) < 0)
208 if (tests
[i
].name
== NULL
)
212 ret
= generic_test (tests
, ntests
, sizeof(int),
213 (generic_encode
)der_put_unsigned
,
214 (generic_length
)der_length_unsigned
,
215 (generic_decode
)der_get_unsigned
,
219 for (i
= 0; i
< ntests
; ++i
)
220 free (tests
[i
].name
);
225 cmp_octet_string (void *a
, void *b
)
227 heim_octet_string
*oa
= (heim_octet_string
*)a
;
228 heim_octet_string
*ob
= (heim_octet_string
*)b
;
230 if (oa
->length
!= ob
->length
)
231 return ob
->length
- oa
->length
;
233 return (memcmp (oa
->data
, ob
->data
, oa
->length
));
237 test_octet_string (void)
239 heim_octet_string s1
= {8, "\x01\x23\x45\x67\x89\xab\xcd\xef"};
241 struct test_case tests
[] = {
242 {NULL
, 8, "\x01\x23\x45\x67\x89\xab\xcd\xef"}
244 int ntests
= sizeof(tests
) / sizeof(*tests
);
248 if (asprintf (&tests
[0].name
, "a octet string") < 0)
250 if (tests
[0].name
== NULL
)
253 ret
= generic_test (tests
, ntests
, sizeof(heim_octet_string
),
254 (generic_encode
)der_put_octet_string
,
255 (generic_length
)der_length_octet_string
,
256 (generic_decode
)der_get_octet_string
,
257 (generic_free
)der_free_octet_string
,
265 cmp_bmp_string (void *a
, void *b
)
267 heim_bmp_string
*oa
= (heim_bmp_string
*)a
;
268 heim_bmp_string
*ob
= (heim_bmp_string
*)b
;
270 return der_heim_bmp_string_cmp(oa
, ob
);
273 static uint16_t bmp_d1
[] = { 32 };
274 static uint16_t bmp_d2
[] = { 32, 32 };
277 test_bmp_string (void)
279 heim_bmp_string s1
= { 1, bmp_d1
};
280 heim_bmp_string s2
= { 2, bmp_d2
};
282 struct test_case tests
[] = {
283 {NULL
, 2, "\x00\x20"},
284 {NULL
, 4, "\x00\x20\x00\x20"}
286 int ntests
= sizeof(tests
) / sizeof(*tests
);
290 if (asprintf (&tests
[0].name
, "a bmp string") < 0)
292 if (tests
[0].name
== NULL
)
295 if (asprintf (&tests
[1].name
, "second bmp string") < 0)
297 if (tests
[1].name
== NULL
)
300 ret
= generic_test (tests
, ntests
, sizeof(heim_bmp_string
),
301 (generic_encode
)der_put_bmp_string
,
302 (generic_length
)der_length_bmp_string
,
303 (generic_decode
)der_get_bmp_string
,
304 (generic_free
)der_free_bmp_string
,
313 cmp_universal_string (void *a
, void *b
)
315 heim_universal_string
*oa
= (heim_universal_string
*)a
;
316 heim_universal_string
*ob
= (heim_universal_string
*)b
;
318 return der_heim_universal_string_cmp(oa
, ob
);
321 static uint32_t universal_d1
[] = { 32 };
322 static uint32_t universal_d2
[] = { 32, 32 };
325 test_universal_string (void)
327 heim_universal_string s1
= { 1, universal_d1
};
328 heim_universal_string s2
= { 2, universal_d2
};
330 struct test_case tests
[] = {
331 {NULL
, 4, "\x00\x00\x00\x20"},
332 {NULL
, 8, "\x00\x00\x00\x20\x00\x00\x00\x20"}
334 int ntests
= sizeof(tests
) / sizeof(*tests
);
338 if (asprintf (&tests
[0].name
, "a universal string") < 0)
340 if (tests
[0].name
== NULL
)
343 if (asprintf (&tests
[1].name
, "second universal string") < 0)
345 if (tests
[1].name
== NULL
)
348 ret
= generic_test (tests
, ntests
, sizeof(heim_universal_string
),
349 (generic_encode
)der_put_universal_string
,
350 (generic_length
)der_length_universal_string
,
351 (generic_decode
)der_get_universal_string
,
352 (generic_free
)der_free_universal_string
,
353 cmp_universal_string
,
361 cmp_general_string (void *a
, void *b
)
363 char **sa
= (char **)a
;
364 char **sb
= (char **)b
;
366 return strcmp (*sa
, *sb
);
370 test_general_string (void)
372 char *s1
= "Test User 1";
374 struct test_case tests
[] = {
375 {NULL
, 11, "\x54\x65\x73\x74\x20\x55\x73\x65\x72\x20\x31"}
377 int ret
, ntests
= sizeof(tests
) / sizeof(*tests
);
380 if (asprintf (&tests
[0].name
, "the string \"%s\"", s1
) < 0)
382 if (tests
[0].name
== NULL
)
385 ret
= generic_test (tests
, ntests
, sizeof(unsigned char *),
386 (generic_encode
)der_put_general_string
,
387 (generic_length
)der_length_general_string
,
388 (generic_decode
)der_get_general_string
,
389 (generic_free
)der_free_general_string
,
397 cmp_generalized_time (void *a
, void *b
)
399 time_t *ta
= (time_t *)a
;
400 time_t *tb
= (time_t *)b
;
406 test_generalized_time (void)
408 struct test_case tests
[] = {
409 {NULL
, 15, "19700101000000Z"},
410 {NULL
, 15, "19851106210627Z"}
412 time_t values
[] = {0, 500159187};
414 int ntests
= sizeof(tests
) / sizeof(*tests
);
416 for (i
= 0; i
< ntests
; ++i
) {
417 tests
[i
].val
= &values
[i
];
418 if (asprintf (&tests
[i
].name
, "time %d", (int)values
[i
]) < 0)
420 if (tests
[i
].name
== NULL
)
424 ret
= generic_test (tests
, ntests
, sizeof(time_t),
425 (generic_encode
)der_put_generalized_time
,
426 (generic_length
)der_length_generalized_time
,
427 (generic_decode
)der_get_generalized_time
,
429 cmp_generalized_time
,
431 for (i
= 0; i
< ntests
; ++i
)
437 test_cmp_oid (void *a
, void *b
)
439 return der_heim_oid_cmp((heim_oid
*)a
, (heim_oid
*)b
);
442 static unsigned oid_comp1
[] = { 1, 1, 1 };
443 static unsigned oid_comp2
[] = { 1, 1 };
444 static unsigned oid_comp3
[] = { 6, 15, 1 };
445 static unsigned oid_comp4
[] = { 6, 15 };
450 struct test_case tests
[] = {
451 {NULL
, 2, "\x29\x01"},
453 {NULL
, 2, "\xff\x01"},
456 heim_oid values
[] = {
463 int ntests
= sizeof(tests
) / sizeof(*tests
);
465 for (i
= 0; i
< ntests
; ++i
) {
466 tests
[i
].val
= &values
[i
];
467 if (asprintf (&tests
[i
].name
, "oid %d", i
) < 0)
469 if (tests
[i
].name
== NULL
)
473 ret
= generic_test (tests
, ntests
, sizeof(heim_oid
),
474 (generic_encode
)der_put_oid
,
475 (generic_length
)der_length_oid
,
476 (generic_decode
)der_get_oid
,
477 (generic_free
)der_free_oid
,
480 for (i
= 0; i
< ntests
; ++i
)
486 test_cmp_bit_string (void *a
, void *b
)
488 return der_heim_bit_string_cmp((heim_bit_string
*)a
, (heim_bit_string
*)b
);
492 test_bit_string (void)
494 struct test_case tests
[] = {
497 heim_bit_string values
[] = {
501 int ntests
= sizeof(tests
) / sizeof(*tests
);
503 for (i
= 0; i
< ntests
; ++i
) {
504 tests
[i
].val
= &values
[i
];
505 if (asprintf (&tests
[i
].name
, "bit_string %d", i
) < 0)
507 if (tests
[i
].name
== NULL
)
511 ret
= generic_test (tests
, ntests
, sizeof(heim_bit_string
),
512 (generic_encode
)der_put_bit_string
,
513 (generic_length
)der_length_bit_string
,
514 (generic_decode
)der_get_bit_string
,
515 (generic_free
)der_free_bit_string
,
518 for (i
= 0; i
< ntests
; ++i
)
524 test_cmp_heim_integer (void *a
, void *b
)
526 return der_heim_integer_cmp((heim_integer
*)a
, (heim_integer
*)b
);
530 test_heim_integer (void)
532 struct test_case tests
[] = {
533 {NULL
, 2, "\xfe\x01"},
534 {NULL
, 2, "\xef\x01"},
535 {NULL
, 3, "\xff\x00\xff"},
536 {NULL
, 3, "\xff\x01\x00"},
539 {NULL
, 2, "\x00\x80"}
542 heim_integer values
[] = {
543 { 2, "\x01\xff", 1 },
544 { 2, "\x10\xff", 1 },
545 { 2, "\xff\x01", 1 },
546 { 2, "\xff\x00", 1 },
552 int ntests
= sizeof(tests
) / sizeof(tests
[0]);
556 for (i
= 0; i
< ntests
; ++i
) {
557 tests
[i
].val
= &values
[i
];
558 if (asprintf (&tests
[i
].name
, "heim_integer %d", i
) < 0)
560 if (tests
[i
].name
== NULL
)
564 ret
= generic_test (tests
, ntests
, sizeof(heim_integer
),
565 (generic_encode
)der_put_heim_integer
,
566 (generic_length
)der_length_heim_integer
,
567 (generic_decode
)der_get_heim_integer
,
568 (generic_free
)der_free_heim_integer
,
569 test_cmp_heim_integer
,
571 for (i
= 0; i
< ntests
; ++i
)
572 free (tests
[i
].name
);
576 /* test zero length integer (BER format) */
577 ret
= der_get_heim_integer(NULL
, 0, &i2
, &size
);
579 errx(1, "der_get_heim_integer");
581 errx(1, "der_get_heim_integer wrong length");
582 der_free_heim_integer(&i2
);
588 test_cmp_boolean (void *a
, void *b
)
590 return !!*(int *)a
!= !!*(int *)b
;
596 struct test_case tests
[] = {
601 int values
[] = { 1, 0 };
603 int ntests
= sizeof(tests
) / sizeof(tests
[0]);
607 for (i
= 0; i
< ntests
; ++i
) {
608 tests
[i
].val
= &values
[i
];
609 if (asprintf (&tests
[i
].name
, "heim_boolean %d", i
) < 0)
611 if (tests
[i
].name
== NULL
)
615 ret
= generic_test (tests
, ntests
, sizeof(int),
616 (generic_encode
)der_put_boolean
,
617 (generic_length
)der_length_boolean
,
618 (generic_decode
)der_get_boolean
,
622 for (i
= 0; i
< ntests
; ++i
)
623 free (tests
[i
].name
);
627 /* test zero length integer (BER format) */
628 ret
= der_get_heim_integer(NULL
, 0, &i2
, &size
);
630 errx(1, "der_get_heim_integer");
632 errx(1, "der_get_heim_integer wrong length");
633 der_free_heim_integer(&i2
);
639 check_fail_unsigned(void)
641 struct test_case tests
[] = {
642 {NULL
, sizeof(unsigned) + 1,
643 "\x01\x01\x01\x01\x01\x01\x01\x01\x01", "data overrun" }
645 int ntests
= sizeof(tests
) / sizeof(*tests
);
647 return generic_decode_fail(tests
, ntests
, sizeof(unsigned),
648 (generic_decode
)der_get_unsigned
);
652 check_fail_integer(void)
654 struct test_case tests
[] = {
655 {NULL
, sizeof(int) + 1,
656 "\x01\x01\x01\x01\x01\x01\x01\x01\x01", "data overrun" }
658 int ntests
= sizeof(tests
) / sizeof(*tests
);
660 return generic_decode_fail(tests
, ntests
, sizeof(int),
661 (generic_decode
)der_get_integer
);
665 check_fail_length(void)
667 struct test_case tests
[] = {
668 {NULL
, 0, "", "empty input data"},
669 {NULL
, 1, "\x82", "internal length overrun" }
671 int ntests
= sizeof(tests
) / sizeof(*tests
);
673 return generic_decode_fail(tests
, ntests
, sizeof(size_t),
674 (generic_decode
)der_get_length
);
678 check_fail_boolean(void)
680 struct test_case tests
[] = {
681 {NULL
, 0, "", "empty input data"}
683 int ntests
= sizeof(tests
) / sizeof(*tests
);
685 return generic_decode_fail(tests
, ntests
, sizeof(int),
686 (generic_decode
)der_get_boolean
);
690 check_fail_general_string(void)
692 struct test_case tests
[] = {
693 { NULL
, 3, "A\x00i", "NUL char in string"}
695 int ntests
= sizeof(tests
) / sizeof(*tests
);
697 return generic_decode_fail(tests
, ntests
, sizeof(heim_general_string
),
698 (generic_decode
)der_get_general_string
);
702 check_fail_bmp_string(void)
704 struct test_case tests
[] = {
705 {NULL
, 1, "\x00", "odd (1) length bmpstring"},
706 {NULL
, 3, "\x00\x00\x00", "odd (3) length bmpstring"}
708 int ntests
= sizeof(tests
) / sizeof(*tests
);
710 return generic_decode_fail(tests
, ntests
, sizeof(heim_bmp_string
),
711 (generic_decode
)der_get_bmp_string
);
715 check_fail_universal_string(void)
717 struct test_case tests
[] = {
718 {NULL
, 1, "\x00", "x & 3 == 1 universal string"},
719 {NULL
, 2, "\x00\x00", "x & 3 == 2 universal string"},
720 {NULL
, 3, "\x00\x00\x00", "x & 3 == 3 universal string"},
721 {NULL
, 5, "\x00\x00\x00\x00\x00", "x & 3 == 1 universal string"},
722 {NULL
, 6, "\x00\x00\x00\x00\x00\x00", "x & 3 == 2 universal string"},
723 {NULL
, 7, "\x00\x00\x00\x00\x00\x00\x00", "x & 3 == 3 universal string"}
725 int ntests
= sizeof(tests
) / sizeof(*tests
);
727 return generic_decode_fail(tests
, ntests
, sizeof(heim_universal_string
),
728 (generic_decode
)der_get_universal_string
);
732 check_fail_heim_integer(void)
735 struct test_case tests
[] = {
737 int ntests
= sizeof(tests
) / sizeof(*tests
);
739 return generic_decode_fail(tests
, ntests
, sizeof(heim_integer
),
740 (generic_decode
)der_get_heim_integer
);
747 check_fail_generalized_time(void)
749 struct test_case tests
[] = {
750 {NULL
, 1, "\x00", "no time"}
752 int ntests
= sizeof(tests
) / sizeof(*tests
);
754 return generic_decode_fail(tests
, ntests
, sizeof(time_t),
755 (generic_decode
)der_get_generalized_time
);
761 struct test_case tests
[] = {
762 {NULL
, 0, "", "empty input data"},
763 {NULL
, 2, "\x00\x80", "last byte continuation" },
764 {NULL
, 11, "\x00\x81\x80\x80\x80\x80\x80\x80\x80\x80\x00",
765 "oid element overflow" }
767 int ntests
= sizeof(tests
) / sizeof(*tests
);
769 return generic_decode_fail(tests
, ntests
, sizeof(heim_oid
),
770 (generic_decode
)der_get_oid
);
774 check_fail_bitstring(void)
776 struct test_case tests
[] = {
777 {NULL
, 0, "", "empty input data"},
778 {NULL
, 1, "\x08", "larger then 8 bits trailer"},
779 {NULL
, 1, "\x01", "to few bytes for bits"},
780 {NULL
, -2, "\x00", "length overrun"},
781 {NULL
, -1, "", "length to short"}
783 int ntests
= sizeof(tests
) / sizeof(*tests
);
785 return generic_decode_fail(tests
, ntests
, sizeof(heim_bit_string
),
786 (generic_decode
)der_get_bit_string
);
790 check_heim_integer_same(const char *p
, const char *norm_p
, heim_integer
*i
)
796 ret
= der_print_hex_heim_integer(i
, &str
);
798 errx(1, "der_print_hex_heim_integer: %d", ret
);
800 if (strcmp(str
, norm_p
) != 0)
801 errx(1, "der_print_hex_heim_integer: %s != %s", str
, p
);
803 ret
= der_parse_hex_heim_integer(str
, &i2
);
805 errx(1, "der_parse_hex_heim_integer: %d", ret
);
807 if (der_heim_integer_cmp(i
, &i2
) != 0)
808 errx(1, "der_heim_integer_cmp: p %s", p
);
810 der_free_heim_integer(&i2
);
813 ret
= der_parse_hex_heim_integer(p
, &i2
);
815 errx(1, "der_parse_hex_heim_integer: %d", ret
);
817 if (der_heim_integer_cmp(i
, &i2
) != 0)
818 errx(1, "der_heim_integer_cmp: norm");
820 der_free_heim_integer(&i2
);
826 test_heim_int_format(void)
828 heim_integer i
= { 1, "\x10", 0 };
829 heim_integer i2
= { 1, "\x10", 1 };
830 heim_integer i3
= { 1, "\01", 0 };
832 "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1"
833 "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD"
834 "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245"
835 "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED"
836 "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE65381"
837 "FFFFFFFF" "FFFFFFFF";
840 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC9\x0F\xDA\xA2"
841 "\x21\x68\xC2\x34\xC4\xC6\x62\x8B\x80\xDC\x1C\xD1"
842 "\x29\x02\x4E\x08\x8A\x67\xCC\x74\x02\x0B\xBE\xA6"
843 "\x3B\x13\x9B\x22\x51\x4A\x08\x79\x8E\x34\x04\xDD"
844 "\xEF\x95\x19\xB3\xCD\x3A\x43\x1B\x30\x2B\x0A\x6D"
845 "\xF2\x5F\x14\x37\x4F\xE1\x35\x6D\x6D\x51\xC2\x45"
846 "\xE4\x85\xB5\x76\x62\x5E\x7E\xC6\xF4\x4C\x42\xE9"
847 "\xA6\x37\xED\x6B\x0B\xFF\x5C\xB6\xF4\x06\xB7\xED"
848 "\xEE\x38\x6B\xFB\x5A\x89\x9F\xA5\xAE\x9F\x24\x11"
849 "\x7C\x4B\x1F\xE6\x49\x28\x66\x51\xEC\xE6\x53\x81"
850 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
856 ret
+= check_heim_integer_same(p
, p
, &bni
);
857 ret
+= check_heim_integer_same("10", "10", &i
);
858 ret
+= check_heim_integer_same("00000010", "10", &i
);
859 ret
+= check_heim_integer_same("-10", "-10", &i2
);
860 ret
+= check_heim_integer_same("-00000010", "-10", &i2
);
861 ret
+= check_heim_integer_same("01", "01", &i3
);
862 ret
+= check_heim_integer_same("1", "01", &i3
);
866 r
= der_parse_hex_heim_integer("-", &f
);
868 der_free_heim_integer(&f
);
871 /* used to cause UMR */
872 r
= der_parse_hex_heim_integer("00", &f
);
874 der_free_heim_integer(&f
);
883 test_heim_oid_format_same(const char *str
, const heim_oid
*oid
)
889 ret
= der_print_heim_oid(oid
, ' ', &p
);
891 printf("fail to print oid: %s\n", str
);
894 ret
= strcmp(p
, str
);
896 printf("oid %s != formated oid %s\n", str
, p
);
901 ret
= der_parse_heim_oid(p
, " ", &o2
);
903 printf("failed to parse %s\n", p
);
908 ret
= der_heim_oid_cmp(&o2
, oid
);
914 static unsigned sha1_oid_tree
[] = { 1, 3, 14, 3, 2, 26 };
917 test_heim_oid_format(void)
919 heim_oid sha1
= { 6, sha1_oid_tree
};
922 ret
+= test_heim_oid_format_same("1 3 14 3 2 26", &sha1
);
928 check_trailing_nul(void)
933 const unsigned char *p
;
938 { 1, (const unsigned char *)"foo\x00o", 5, NULL
, 0 },
939 { 1, (const unsigned char *)"\x00o", 2, NULL
, 0 },
940 { 0, (const unsigned char *)"\x00\x00\x00\x00\x00", 5, "", 5 },
941 { 0, (const unsigned char *)"\x00", 1, "", 1 },
942 { 0, (const unsigned char *)"", 0, "", 0 },
943 { 0, (const unsigned char *)"foo\x00\x00", 5, "foo", 5 },
944 { 0, (const unsigned char *)"foo\0", 4, "foo", 4 },
945 { 0, (const unsigned char *)"foo", 3, "foo", 3 }
948 for (i
= 0; i
< sizeof(foo
)/sizeof(foo
[0]); i
++) {
951 ret
= der_get_general_string(foo
[i
].p
, foo
[i
].len
, &s
, &size
);
954 errx(1, "check %d NULL didn't fail", i
);
958 errx(1, "NULL check %d der_get_general_string failed", i
);
959 if (foo
[i
].size
!= size
)
960 errx(1, "NUL check i = %d size failed", i
);
961 if (strcmp(foo
[i
].s
, s
) != 0)
962 errx(1, "NUL check i = %d content failed", i
);
973 /* diffrent lengths are diffrent */
975 const heim_octet_string os1
= { 1, "a" } , os2
= { 0, NULL
};
976 ret
= der_heim_octet_string_cmp(&os1
, &os2
);
980 /* diffrent data are diffrent */
982 const heim_octet_string os1
= { 1, "a" } , os2
= { 1, "b" };
983 ret
= der_heim_octet_string_cmp(&os1
, &os2
);
987 /* diffrent lengths are diffrent */
989 const heim_bit_string bs1
= { 8, "a" } , bs2
= { 7, "a" };
990 ret
= der_heim_bit_string_cmp(&bs1
, &bs2
);
994 /* diffrent data are diffrent */
996 const heim_bit_string bs1
= { 7, "\x0f" } , bs2
= { 7, "\x02" };
997 ret
= der_heim_bit_string_cmp(&bs1
, &bs2
);
1001 /* diffrent lengths are diffrent */
1004 heim_bmp_string bs1
= { 1, NULL
} , bs2
= { 0, NULL
};
1006 ret
= der_heim_bmp_string_cmp(&bs1
, &bs2
);
1010 /* diffrent lengths are diffrent */
1013 heim_universal_string us1
= { 1, NULL
} , us2
= { 0, NULL
};
1015 ret
= der_heim_universal_string_cmp(&us1
, &us2
);
1021 uint32_t data
= (uint32_t)'a';
1022 heim_universal_string us1
= { 1, NULL
} , us2
= { 1, NULL
};
1025 ret
= der_heim_universal_string_cmp(&us1
, &us2
);
1034 corner_generalized_time(void)
1036 const char *str
= "760520140000Z";
1041 ret
= der_get_generalized_time((const unsigned char*)str
, strlen(str
),
1058 { 0, "\xff\xff\xff\xff\xff\xff\xff\xff", 8 }
1066 for (i
= 0; i
< sizeof(tests
)/sizeof(tests
[0]); i
++) {
1067 ret
= der_get_tag((const unsigned char*)tests
[i
].ptr
,
1068 tests
[i
].len
, &cl
, &ty
, &tag
, &size
);
1071 errx(1, "failed while shouldn't");
1074 errx(1, "passed while shouldn't");
1081 main(int argc
, char **argv
)
1085 ret
+= test_integer ();
1086 ret
+= test_integer_more();
1087 ret
+= test_unsigned ();
1088 ret
+= test_octet_string ();
1089 ret
+= test_bmp_string ();
1090 ret
+= test_universal_string ();
1091 ret
+= test_general_string ();
1092 ret
+= test_generalized_time ();
1094 ret
+= test_bit_string();
1095 ret
+= test_heim_integer();
1096 ret
+= test_boolean();
1098 ret
+= check_fail_unsigned();
1099 ret
+= check_fail_integer();
1100 ret
+= check_fail_length();
1101 ret
+= check_fail_boolean();
1102 ret
+= check_fail_general_string();
1103 ret
+= check_fail_bmp_string();
1104 ret
+= check_fail_universal_string();
1105 ret
+= check_fail_heim_integer();
1106 ret
+= check_fail_generalized_time();
1107 ret
+= check_fail_oid();
1108 ret
+= check_fail_bitstring();
1109 ret
+= test_heim_int_format();
1110 ret
+= test_heim_oid_format();
1111 ret
+= check_trailing_nul();
1112 ret
+= test_misc_cmp();
1113 ret
+= corner_generalized_time();
1114 ret
+= corner_tag();