2 * Copyright (c) 1999 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
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
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
44 #include <asn1-common.h>
47 #include <krb5_asn1.h>
48 #include <heim_asn1.h>
49 #include <rfc2459_asn1.h>
50 #include <test_asn1.h>
52 #include "check-common.h"
56 static char *lha_principal
[] = { "lha" };
57 static char *lharoot_princ
[] = { "lha", "root" };
58 static char *datan_princ
[] = { "host", "nutcracker.e.kth.se" };
59 static char *nada_tgt_principal
[] = { "krbtgt", "NADA.KTH.SE" };
62 #define IF_OPT_COMPARE(ac,bc,e) \
63 if (((ac)->e == NULL && (bc)->e != NULL) || (((ac)->e != NULL && (bc)->e == NULL))) return 1; if ((ab)->e)
64 #define COMPARE_OPT_STRING(ac,bc,e) \
65 do { if (strcmp(*(ac)->e, *(bc)->e) != 0) return 1; } while(0)
66 #define COMPARE_OPT_OCTECT_STRING(ac,bc,e) \
67 do { if ((ac)->e->length != (bc)->e->length || memcmp((ac)->e->data, (bc)->e->data, (ac)->e->length) != 0) return 1; } while(0)
68 #define COMPARE_STRING(ac,bc,e) \
69 do { if (strcmp((ac)->e, (bc)->e) != 0) return 1; } while(0)
70 #define COMPARE_INTEGER(ac,bc,e) \
71 do { if ((ac)->e != (bc)->e) return 1; } while(0)
72 #define COMPARE_OPT_INTEGER(ac,bc,e) \
73 do { if (*(ac)->e != *(bc)->e) return 1; } while(0)
74 #define COMPARE_MEM(ac,bc,e,len) \
75 do { if (memcmp((ac)->e, (bc)->e,len) != 0) return 1; } while(0)
78 cmp_principal (void *a
, void *b
)
84 COMPARE_STRING(pa
,pb
,realm
);
85 COMPARE_INTEGER(pa
,pb
,name
.name_type
);
86 COMPARE_INTEGER(pa
,pb
,name
.name_string
.len
);
88 for (i
= 0; i
< pa
->name
.name_string
.len
; i
++)
89 COMPARE_STRING(pa
,pb
,name
.name_string
.val
[i
]);
98 struct test_case tests
[] = {
100 "\x30\x1b\xa0\x10\x30\x0e\xa0\x03\x02\x01\x01\xa1\x07\x30\x05\x1b"
101 "\x03\x6c\x68\x61\xa1\x07\x1b\x05\x53\x55\x2e\x53\x45"
104 "\x30\x21\xa0\x16\x30\x14\xa0\x03\x02\x01\x01\xa1\x0d\x30\x0b\x1b"
105 "\x03\x6c\x68\x61\x1b\x04\x72\x6f\x6f\x74\xa1\x07\x1b\x05\x53\x55"
109 "\x30\x34\xa0\x26\x30\x24\xa0\x03\x02\x01\x03\xa1\x1d\x30\x1b\x1b"
110 "\x04\x68\x6f\x73\x74\x1b\x13\x6e\x75\x74\x63\x72\x61\x63\x6b\x65"
111 "\x72\x2e\x65\x2e\x6b\x74\x68\x2e\x73\x65\xa1\x0a\x1b\x08\x45\x2e"
112 "\x4b\x54\x48\x2e\x53\x45"
117 Principal values
[] = {
118 { { KRB5_NT_PRINCIPAL
, { 1, lha_principal
} }, "SU.SE" },
119 { { KRB5_NT_PRINCIPAL
, { 2, lharoot_princ
} }, "SU.SE" },
120 { { KRB5_NT_SRV_HST
, { 2, datan_princ
} }, "E.KTH.SE" }
123 int ntests
= sizeof(tests
) / sizeof(*tests
);
125 for (i
= 0; i
< ntests
; ++i
) {
126 tests
[i
].val
= &values
[i
];
127 if (asprintf (&tests
[i
].name
, "Principal %d", i
) < 0)
129 if (tests
[i
].name
== NULL
)
133 ret
= generic_test (tests
, ntests
, sizeof(Principal
),
134 (generic_encode
)encode_Principal
,
135 (generic_length
)length_Principal
,
136 (generic_decode
)decode_Principal
,
137 (generic_free
)free_Principal
,
140 for (i
= 0; i
< ntests
; ++i
)
141 free (tests
[i
].name
);
147 cmp_authenticator (void *a
, void *b
)
149 Authenticator
*aa
= a
;
150 Authenticator
*ab
= b
;
153 COMPARE_INTEGER(aa
,ab
,authenticator_vno
);
154 COMPARE_STRING(aa
,ab
,crealm
);
156 COMPARE_INTEGER(aa
,ab
,cname
.name_type
);
157 COMPARE_INTEGER(aa
,ab
,cname
.name_string
.len
);
159 for (i
= 0; i
< aa
->cname
.name_string
.len
; i
++)
160 COMPARE_STRING(aa
,ab
,cname
.name_string
.val
[i
]);
166 test_authenticator (void)
168 struct test_case tests
[] = {
170 "\x62\x3d\x30\x3b\xa0\x03\x02\x01\x05\xa1\x0a\x1b\x08"
171 "\x45\x2e\x4b\x54\x48\x2e\x53\x45\xa2\x10\x30\x0e\xa0"
172 "\x03\x02\x01\x01\xa1\x07\x30\x05\x1b\x03\x6c\x68\x61"
173 "\xa4\x03\x02\x01\x0a\xa5\x11\x18\x0f\x31\x39\x37\x30"
174 "\x30\x31\x30\x31\x30\x30\x30\x31\x33\x39\x5a"
177 "\x62\x41\x30\x3f\xa0\x03\x02\x01\x05\xa1\x07\x1b\x05"
178 "\x53\x55\x2e\x53\x45\xa2\x16\x30\x14\xa0\x03\x02\x01"
179 "\x01\xa1\x0d\x30\x0b\x1b\x03\x6c\x68\x61\x1b\x04\x72"
180 "\x6f\x6f\x74\xa4\x04\x02\x02\x01\x24\xa5\x11\x18\x0f"
181 "\x31\x39\x37\x30\x30\x31\x30\x31\x30\x30\x31\x36\x33"
186 Authenticator values
[] = {
187 { 5, "E.KTH.SE", { KRB5_NT_PRINCIPAL
, { 1, lha_principal
} },
188 NULL
, 10, 99, NULL
, NULL
, NULL
},
189 { 5, "SU.SE", { KRB5_NT_PRINCIPAL
, { 2, lharoot_princ
} },
190 NULL
, 292, 999, NULL
, NULL
, NULL
}
193 int ntests
= sizeof(tests
) / sizeof(*tests
);
195 for (i
= 0; i
< ntests
; ++i
) {
196 tests
[i
].val
= &values
[i
];
197 if (asprintf (&tests
[i
].name
, "Authenticator %d", i
) < 0)
199 if (tests
[i
].name
== NULL
)
203 ret
= generic_test (tests
, ntests
, sizeof(Authenticator
),
204 (generic_encode
)encode_Authenticator
,
205 (generic_length
)length_Authenticator
,
206 (generic_decode
)decode_Authenticator
,
207 (generic_free
)free_Authenticator
,
209 (generic_copy
)copy_Authenticator
);
210 for (i
= 0; i
< ntests
; ++i
)
217 cmp_KRB_ERROR (void *a
, void *b
)
223 COMPARE_INTEGER(aa
,ab
,pvno
);
224 COMPARE_INTEGER(aa
,ab
,msg_type
);
226 IF_OPT_COMPARE(aa
,ab
,ctime
) {
227 COMPARE_INTEGER(aa
,ab
,ctime
);
229 IF_OPT_COMPARE(aa
,ab
,cusec
) {
230 COMPARE_INTEGER(aa
,ab
,cusec
);
232 COMPARE_INTEGER(aa
,ab
,stime
);
233 COMPARE_INTEGER(aa
,ab
,susec
);
234 COMPARE_INTEGER(aa
,ab
,error_code
);
236 IF_OPT_COMPARE(aa
,ab
,crealm
) {
237 COMPARE_OPT_STRING(aa
,ab
,crealm
);
240 IF_OPT_COMPARE(aa
,ab
,cname
) {
241 COMPARE_OPT_STRING(aa
,ab
,cname
);
244 COMPARE_STRING(aa
,ab
,realm
);
246 COMPARE_INTEGER(aa
,ab
,sname
.name_string
.len
);
247 for (i
= 0; i
< aa
->sname
.name_string
.len
; i
++)
248 COMPARE_STRING(aa
,ab
,sname
.name_string
.val
[i
]);
250 IF_OPT_COMPARE(aa
,ab
,e_text
) {
251 COMPARE_OPT_STRING(aa
,ab
,e_text
);
253 IF_OPT_COMPARE(aa
,ab
,e_data
) {
254 /* COMPARE_OPT_OCTECT_STRING(aa,ab,e_data); */
261 test_krb_error (void)
263 struct test_case tests
[] = {
265 "\x7e\x7d\x30\x7b\xa0\x03\x02\x01\x05\xa1\x03\x02\x01\x1e\xa4\x11"
266 "\x18\x0f\x32\x30\x30\x33\x31\x31\x32\x34\x30\x30\x31\x31\x31\x39"
267 "\x5a\xa5\x05\x02\x03\x04\xed\xa5\xa6\x03\x02\x01\x1f\xa7\x0d\x1b"
268 "\x0b\x4e\x41\x44\x41\x2e\x4b\x54\x48\x2e\x53\x45\xa8\x10\x30\x0e"
269 "\xa0\x03\x02\x01\x01\xa1\x07\x30\x05\x1b\x03\x6c\x68\x61\xa9\x0d"
270 "\x1b\x0b\x4e\x41\x44\x41\x2e\x4b\x54\x48\x2e\x53\x45\xaa\x20\x30"
271 "\x1e\xa0\x03\x02\x01\x01\xa1\x17\x30\x15\x1b\x06\x6b\x72\x62\x74"
272 "\x67\x74\x1b\x0b\x4e\x41\x44\x41\x2e\x4b\x54\x48\x2e\x53\x45",
276 int ntests
= sizeof(tests
) / sizeof(*tests
);
278 PrincipalName lhaprincipalname
= { 1, { 1, lha_principal
} };
279 PrincipalName tgtprincipalname
= { 1, { 2, nada_tgt_principal
} };
280 char *realm
= "NADA.KTH.SE";
286 e1
.stime
= 1069632679;
290 e1
.cname
= &lhaprincipalname
;
291 e1
.realm
= "NADA.KTH.SE";
292 e1
.sname
= tgtprincipalname
;
298 return generic_test (tests
, ntests
, sizeof(KRB_ERROR
),
299 (generic_encode
)encode_KRB_ERROR
,
300 (generic_length
)length_KRB_ERROR
,
301 (generic_decode
)decode_KRB_ERROR
,
302 (generic_free
)free_KRB_ERROR
,
304 (generic_copy
)copy_KRB_ERROR
);
308 cmp_Name (void *a
, void *b
)
313 COMPARE_INTEGER(aa
,ab
,element
);
321 struct test_case tests
[] = {
323 "\x30\x21\x31\x1f\x30\x0b\x06\x03\x55\x04\x03\x13\x04\x4c\x6f\x76"
324 "\x65\x30\x10\x06\x03\x55\x04\x07\x13\x09\x53\x54\x4f\x43\x4b\x48"
326 "Name CN=Love+L=STOCKHOLM"
329 "\x30\x21\x31\x1f\x30\x0b\x06\x03\x55\x04\x03\x13\x04\x4c\x6f\x76"
330 "\x65\x30\x10\x06\x03\x55\x04\x07\x13\x09\x53\x54\x4f\x43\x4b\x48"
332 "Name L=STOCKHOLM+CN=Love"
336 int ntests
= sizeof(tests
) / sizeof(*tests
);
338 RelativeDistinguishedName rdn1
[1];
339 RelativeDistinguishedName rdn2
[1];
340 AttributeTypeAndValue atv1
[2];
341 AttributeTypeAndValue atv2
[2];
342 unsigned cmp_CN
[] = { 2, 5, 4, 3 };
343 unsigned cmp_L
[] = { 2, 5, 4, 7 };
346 n1
.element
= choice_Name_rdnSequence
;
347 n1
.u
.rdnSequence
.val
= rdn1
;
348 n1
.u
.rdnSequence
.len
= sizeof(rdn1
)/sizeof(rdn1
[0]);
350 rdn1
[0].len
= sizeof(atv1
)/sizeof(atv1
[0]);
352 atv1
[0].type
.length
= sizeof(cmp_CN
)/sizeof(cmp_CN
[0]);
353 atv1
[0].type
.components
= cmp_CN
;
354 atv1
[0].value
.element
= choice_DirectoryString_printableString
;
355 atv1
[0].value
.u
.printableString
.data
= "Love";
356 atv1
[0].value
.u
.printableString
.length
= 4;
358 atv1
[1].type
.length
= sizeof(cmp_L
)/sizeof(cmp_L
[0]);
359 atv1
[1].type
.components
= cmp_L
;
360 atv1
[1].value
.element
= choice_DirectoryString_printableString
;
361 atv1
[1].value
.u
.printableString
.data
= "STOCKHOLM";
362 atv1
[1].value
.u
.printableString
.length
= 9;
365 n2
.element
= choice_Name_rdnSequence
;
366 n2
.u
.rdnSequence
.val
= rdn2
;
367 n2
.u
.rdnSequence
.len
= sizeof(rdn2
)/sizeof(rdn2
[0]);
369 rdn2
[0].len
= sizeof(atv2
)/sizeof(atv2
[0]);
371 atv2
[0].type
.length
= sizeof(cmp_L
)/sizeof(cmp_L
[0]);
372 atv2
[0].type
.components
= cmp_L
;
373 atv2
[0].value
.element
= choice_DirectoryString_printableString
;
374 atv2
[0].value
.u
.printableString
.data
= "STOCKHOLM";
375 atv2
[0].value
.u
.printableString
.length
= 9;
377 atv2
[1].type
.length
= sizeof(cmp_CN
)/sizeof(cmp_CN
[0]);
378 atv2
[1].type
.components
= cmp_CN
;
379 atv2
[1].value
.element
= choice_DirectoryString_printableString
;
380 atv2
[1].value
.u
.printableString
.data
= "Love";
381 atv2
[1].value
.u
.printableString
.length
= 4;
387 return generic_test (tests
, ntests
, sizeof(Name
),
388 (generic_encode
)encode_Name
,
389 (generic_length
)length_Name
,
390 (generic_decode
)decode_Name
,
391 (generic_free
)free_Name
,
393 (generic_copy
)copy_Name
);
397 cmp_KeyUsage (void *a
, void *b
)
402 return KeyUsage2int(*aa
) != KeyUsage2int(*ab
);
406 test_bit_string (void)
408 struct test_case tests
[] = {
418 "\x03\x03\x07\x00\x80",
427 int ntests
= sizeof(tests
) / sizeof(*tests
);
428 KeyUsage ku1
, ku2
, ku3
, ku4
;
430 memset(&ku1
, 0, sizeof(ku1
));
431 ku1
.digitalSignature
= 1;
434 memset(&ku2
, 0, sizeof(ku2
));
435 ku2
.digitalSignature
= 1;
436 ku2
.keyEncipherment
= 1;
439 memset(&ku3
, 0, sizeof(ku3
));
440 ku3
.decipherOnly
= 1;
443 memset(&ku4
, 0, sizeof(ku4
));
447 return generic_test (tests
, ntests
, sizeof(KeyUsage
),
448 (generic_encode
)encode_KeyUsage
,
449 (generic_length
)length_KeyUsage
,
450 (generic_decode
)decode_KeyUsage
,
451 (generic_free
)free_KeyUsage
,
453 (generic_copy
)copy_KeyUsage
);
457 cmp_TicketFlags (void *a
, void *b
)
462 return TicketFlags2int(*aa
) != TicketFlags2int(*ab
);
466 test_bit_string_rfc1510 (void)
468 struct test_case tests
[] = {
470 "\x03\x05\x00\x80\x00\x00\x00",
474 "\x03\x05\x00\x40\x20\x00\x00",
478 "\x03\x05\x00\x00\x20\x00\x00",
482 "\x03\x05\x00\x00\x00\x00\x00",
487 int ntests
= sizeof(tests
) / sizeof(*tests
);
488 TicketFlags tf1
, tf2
, tf3
, tf4
;
490 memset(&tf1
, 0, sizeof(tf1
));
494 memset(&tf2
, 0, sizeof(tf2
));
499 memset(&tf3
, 0, sizeof(tf3
));
503 memset(&tf4
, 0, sizeof(tf4
));
507 return generic_test (tests
, ntests
, sizeof(TicketFlags
),
508 (generic_encode
)encode_TicketFlags
,
509 (generic_length
)length_TicketFlags
,
510 (generic_decode
)decode_TicketFlags
,
511 (generic_free
)free_TicketFlags
,
513 (generic_copy
)copy_TicketFlags
);
517 cmp_KerberosTime (void *a
, void *b
)
519 KerberosTime
*aa
= a
;
520 KerberosTime
*ab
= b
;
528 struct test_case tests
[] = {
530 "\x18\x0f\x31\x39\x37\x30\x30\x31\x30\x31\x30\x31\x31\x38\x33\x31"
534 "\x18\x0f\x32\x30\x30\x39\x30\x35\x32\x34\x30\x32\x30\x32\x34\x30"
539 int ntests
= sizeof(tests
) / sizeof(*tests
);
540 KerberosTime times
[] = {
545 tests
[0].val
= ×
[0];
546 tests
[1].val
= ×
[1];
548 return generic_test (tests
, ntests
, sizeof(KerberosTime
),
549 (generic_encode
)encode_KerberosTime
,
550 (generic_length
)length_KerberosTime
,
551 (generic_decode
)decode_KerberosTime
,
552 (generic_free
)free_KerberosTime
,
554 (generic_copy
)copy_KerberosTime
);
562 "\x30\x82\x02\x6c\x30\x82\x01\xd5\xa0\x03\x02\x01\x02\x02\x09\x00"
563 "\x99\x32\xde\x61\x0e\x40\x19\x8a\x30\x0d\x06\x09\x2a\x86\x48\x86"
564 "\xf7\x0d\x01\x01\x05\x05\x00\x30\x2a\x31\x1b\x30\x19\x06\x03\x55"
565 "\x04\x03\x0c\x12\x68\x78\x35\x30\x39\x20\x54\x65\x73\x74\x20\x52"
566 "\x6f\x6f\x74\x20\x43\x41\x31\x0b\x30\x09\x06\x03\x55\x04\x06\x13"
567 "\x02\x53\x45\x30\x1e\x17\x0d\x30\x39\x30\x34\x32\x36\x32\x30\x32"
568 "\x39\x34\x30\x5a\x17\x0d\x31\x39\x30\x34\x32\x34\x32\x30\x32\x39"
569 "\x34\x30\x5a\x30\x2a\x31\x1b\x30\x19\x06\x03\x55\x04\x03\x0c\x12"
570 "\x68\x78\x35\x30\x39\x20\x54\x65\x73\x74\x20\x52\x6f\x6f\x74\x20"
571 "\x43\x41\x31\x0b\x30\x09\x06\x03\x55\x04\x06\x13\x02\x53\x45\x30"
572 "\x81\x9f\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01\x05"
573 "\x00\x03\x81\x8d\x00\x30\x81\x89\x02\x81\x81\x00\xb9\xd3\x1b\x67"
574 "\x1c\xf7\x5e\x26\x81\x3b\x82\xff\x03\xa4\x43\xb5\xb2\x63\x0b\x89"
575 "\x58\x43\xfe\x3d\xe0\x38\x7d\x93\x74\xbb\xad\x21\xa4\x29\xd9\x34"
576 "\x79\xf3\x1c\x8c\x5a\xd6\xb0\xd7\x19\xea\xcc\xaf\xe0\xa8\x40\x02"
577 "\x1d\x91\xf1\xac\x36\xb0\xfb\x08\xbd\xcc\x9a\xe1\xb7\x6e\xee\x0a"
578 "\x69\xbf\x6d\x2b\xee\x20\x82\x61\x06\xf2\x18\xcc\x89\x11\x64\x7e"
579 "\xb2\xff\x47\xd1\x3b\x52\x73\xeb\x5a\xc0\x03\xa6\x4b\xc7\x40\x7e"
580 "\xbc\xe1\x0e\x65\x44\x3f\x40\x8b\x02\x82\x54\x04\xd9\xcc\x2c\x67"
581 "\x01\xb6\x16\x82\xd8\x33\x53\x17\xd7\xde\x8d\x5d\x02\x03\x01\x00"
582 "\x01\xa3\x81\x99\x30\x81\x96\x30\x1d\x06\x03\x55\x1d\x0e\x04\x16"
583 "\x04\x14\x6e\x48\x13\xdc\xbf\x8b\x95\x4c\x13\xf3\x1f\x97\x30\xdd"
584 "\x27\x96\x59\x9b\x0e\x68\x30\x5a\x06\x03\x55\x1d\x23\x04\x53\x30"
585 "\x51\x80\x14\x6e\x48\x13\xdc\xbf\x8b\x95\x4c\x13\xf3\x1f\x97\x30"
586 "\xdd\x27\x96\x59\x9b\x0e\x68\xa1\x2e\xa4\x2c\x30\x2a\x31\x1b\x30"
587 "\x19\x06\x03\x55\x04\x03\x0c\x12\x68\x78\x35\x30\x39\x20\x54\x65"
588 "\x73\x74\x20\x52\x6f\x6f\x74\x20\x43\x41\x31\x0b\x30\x09\x06\x03"
589 "\x55\x04\x06\x13\x02\x53\x45\x82\x09\x00\x99\x32\xde\x61\x0e\x40"
590 "\x19\x8a\x30\x0c\x06\x03\x55\x1d\x13\x04\x05\x30\x03\x01\x01\xff"
591 "\x30\x0b\x06\x03\x55\x1d\x0f\x04\x04\x03\x02\x01\xe6\x30\x0d\x06"
592 "\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x05\x05\x00\x03\x81\x81\x00"
593 "\x52\x9b\xe4\x0e\xee\xc2\x5d\xb7\xf1\xba\x47\xe3\xfe\xaf\x3d\x51"
594 "\x10\xfd\xe8\x0d\x14\x58\x05\x36\xa7\xeb\xd8\x05\xe5\x27\x6f\x51"
595 "\xb8\xec\x90\xd9\x03\xe1\xbc\x9c\x93\x38\x21\x5c\xaf\x4e\x6c\x7b"
596 "\x6c\x65\xa9\x92\xcd\x94\xef\xa8\xae\x90\x12\x14\x78\x2d\xa3\x15"
597 "\xaa\x42\xf1\xd9\x44\x64\x2c\x3c\xc0\xbd\x3a\x48\xd8\x80\x45\x8b"
598 "\xd1\x79\x82\xe0\x0f\xdf\x08\x3c\x60\x21\x6f\x31\x47\x98\xae\x2f"
599 "\xcb\xb1\xa1\xb9\xc1\xa3\x71\x5e\x4a\xc2\x67\xdf\x66\x0a\x51\xb5"
600 "\xad\x60\x05\xdb\x02\xd4\x1a\xd2\xb9\x4e\x01\x08\x2b\xc3\x57\xaf",
603 "\x30\x82\x02\x54\x30\x82\x01\xbd\xa0\x03\x02\x01\x02\x02\x01\x08"
604 "\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x05\x05\x00\x30"
605 "\x2a\x31\x1b\x30\x19\x06\x03\x55\x04\x03\x0c\x12\x68\x78\x35\x30"
606 "\x39\x20\x54\x65\x73\x74\x20\x52\x6f\x6f\x74\x20\x43\x41\x31\x0b"
607 "\x30\x09\x06\x03\x55\x04\x06\x13\x02\x53\x45\x30\x1e\x17\x0d\x30"
608 "\x39\x30\x34\x32\x36\x32\x30\x32\x39\x34\x30\x5a\x17\x0d\x31\x39"
609 "\x30\x34\x32\x34\x32\x30\x32\x39\x34\x30\x5a\x30\x1b\x31\x0b\x30"
610 "\x09\x06\x03\x55\x04\x06\x13\x02\x53\x45\x31\x0c\x30\x0a\x06\x03"
611 "\x55\x04\x03\x0c\x03\x6b\x64\x63\x30\x81\x9f\x30\x0d\x06\x09\x2a"
612 "\x86\x48\x86\xf7\x0d\x01\x01\x01\x05\x00\x03\x81\x8d\x00\x30\x81"
613 "\x89\x02\x81\x81\x00\xd2\x41\x7a\xf8\x4b\x55\xb2\xaf\x11\xf9\x43"
614 "\x9b\x43\x81\x09\x3b\x9a\x94\xcf\x00\xf4\x85\x75\x92\xd7\x2a\xa5"
615 "\x11\xf1\xa8\x50\x6e\xc6\x84\x74\x24\x17\xda\x84\xc8\x03\x37\xb2"
616 "\x20\xf3\xba\xb5\x59\x36\x21\x4d\xab\x70\xe2\xc3\x09\x93\x68\x14"
617 "\x12\x79\xc5\xbb\x9e\x1b\x4a\xf0\xc6\x24\x59\x25\xc3\x1c\xa8\x70"
618 "\x66\x5b\x3e\x41\x8e\xe3\x25\x71\x9a\x94\xa0\x5b\x46\x91\x6f\xdd"
619 "\x58\x14\xec\x89\xe5\x8c\x96\xc5\x38\x60\xe4\xab\xf2\x75\xee\x6e"
620 "\x62\xfc\xe1\xbd\x03\x47\xff\xc4\xbe\x0f\xca\x70\x73\xe3\x74\x58"
621 "\x3a\x2f\x04\x2d\x39\x02\x03\x01\x00\x01\xa3\x81\x98\x30\x81\x95"
622 "\x30\x09\x06\x03\x55\x1d\x13\x04\x02\x30\x00\x30\x0b\x06\x03\x55"
623 "\x1d\x0f\x04\x04\x03\x02\x05\xe0\x30\x12\x06\x03\x55\x1d\x25\x04"
624 "\x0b\x30\x09\x06\x07\x2b\x06\x01\x05\x02\x03\x05\x30\x1d\x06\x03"
625 "\x55\x1d\x0e\x04\x16\x04\x14\x3a\xd3\x73\xff\xab\xdb\x7d\x8d\xc6"
626 "\x3a\xa2\x26\x3e\xae\x78\x95\x80\xc9\xe6\x31\x30\x48\x06\x03\x55"
627 "\x1d\x11\x04\x41\x30\x3f\xa0\x3d\x06\x06\x2b\x06\x01\x05\x02\x02"
628 "\xa0\x33\x30\x31\xa0\x0d\x1b\x0b\x54\x45\x53\x54\x2e\x48\x35\x4c"
629 "\x2e\x53\x45\xa1\x20\x30\x1e\xa0\x03\x02\x01\x01\xa1\x17\x30\x15"
630 "\x1b\x06\x6b\x72\x62\x74\x67\x74\x1b\x0b\x54\x45\x53\x54\x2e\x48"
631 "\x35\x4c\x2e\x53\x45\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01"
632 "\x01\x05\x05\x00\x03\x81\x81\x00\x83\xf4\x14\xa7\x6e\x59\xff\x80"
633 "\x64\xe7\xfa\xcf\x13\x80\x86\xe1\xed\x02\x38\xad\x96\x72\x25\xe5"
634 "\x06\x7a\x9a\xbc\x24\x74\xa9\x75\x55\xb2\x49\x80\x69\x45\x95\x4a"
635 "\x4c\x76\xa9\xe3\x4e\x49\xd3\xc2\x69\x5a\x95\x03\xeb\xba\x72\x23"
636 "\x9c\xfd\x3d\x8b\xc6\x07\x82\x3b\xf4\xf3\xef\x6c\x2e\x9e\x0b\xac"
637 "\x9e\x6c\xbb\x37\x4a\xa1\x9e\x73\xd1\xdc\x97\x61\xba\xfc\xd3\x49"
638 "\xa6\xc2\x4c\x55\x2e\x06\x37\x76\xb5\xef\x57\xe7\x57\x58\x8a\x71"
639 "\x63\xf3\xeb\xe7\x55\x68\x0d\xf6\x46\x4c\xfb\xf9\x43\xbb\x0c\x92"
640 "\x4f\x4e\x22\x7b\x63\xe8\x4f\x9c",
653 for (i
= 0; i
< sizeof(certs
)/sizeof(certs
[0]); i
++) {
655 ret
= decode_Certificate((unsigned char *)certs
[i
].cert
,
656 certs
[i
].len
, &c
, &size
);
660 ret
= copy_Certificate(&c
, &c2
);
661 free_Certificate(&c
);
665 free_Certificate(&c2
);
673 cmp_TESTLargeTag (void *a
, void *b
)
675 TESTLargeTag
*aa
= a
;
676 TESTLargeTag
*ab
= b
;
678 COMPARE_INTEGER(aa
,ab
,foo
);
679 COMPARE_INTEGER(aa
,ab
,bar
);
684 test_large_tag (void)
686 struct test_case tests
[] = {
687 { NULL
, 15, "\x30\x0d\xbf\x7f\x03\x02\x01\x01\xbf\x81\x00\x03\x02\x01\x02", "large tag 1" }
690 int ntests
= sizeof(tests
) / sizeof(*tests
);
693 memset(<1
, 0, sizeof(lt1
));
699 return generic_test (tests
, ntests
, sizeof(TESTLargeTag
),
700 (generic_encode
)encode_TESTLargeTag
,
701 (generic_length
)length_TESTLargeTag
,
702 (generic_decode
)decode_TESTLargeTag
,
703 (generic_free
)free_TESTLargeTag
,
705 (generic_copy
)copy_TESTLargeTag
);
716 check_tag_length(void)
718 struct test_data td
[] = {
719 { 1, 3, 3, "\x02\x01\x00"},
720 { 1, 3, 3, "\x02\x01\x7f"},
721 { 1, 4, 4, "\x02\x02\x00\x80"},
722 { 1, 4, 4, "\x02\x02\x01\x00"},
723 { 1, 4, 4, "\x02\x02\x02\x00"},
724 { 0, 3, 0, "\x02\x02\x00"},
725 { 0, 3, 0, "\x02\x7f\x7f"},
726 { 0, 4, 0, "\x02\x03\x00\x80"},
727 { 0, 4, 0, "\x02\x7f\x01\x00"},
728 { 0, 5, 0, "\x02\xff\x7f\x02\x00"}
731 TESTuint32 values
[] = {0, 127, 128, 256, 512,
732 0, 127, 128, 256, 512 };
734 int i
, ret
, failed
= 0;
737 for (i
= 0; i
< sizeof(td
)/sizeof(td
[0]); i
++) {
738 struct map_page
*page
;
740 buf
= map_alloc(OVERRUN
, td
[i
].data
, td
[i
].len
, &page
);
742 ret
= decode_TESTuint32(buf
, td
[i
].len
, &u
, &sz
);
745 printf("failed with tag len test %d\n", i
);
750 printf("failed with success for tag len test %d\n", i
);
753 if (td
[i
].expected_len
!= sz
) {
754 printf("wrong expected size for tag test %d\n", i
);
757 if (values
[i
] != u
) {
758 printf("wrong value for tag test %d\n", i
);
762 map_free(page
, "test", "decode");
768 cmp_TESTChoice (void *a
, void *b
)
776 struct test_case tests
[] = {
777 { NULL
, 5, "\xa1\x03\x02\x01\x01", "large choice 1" },
778 { NULL
, 5, "\xa2\x03\x02\x01\x02", "large choice 2" }
781 int ret
= 0, ntests
= sizeof(tests
) / sizeof(*tests
);
786 memset(&c1
, 0, sizeof(c1
));
787 c1
.element
= choice_TESTChoice1_i1
;
791 memset(&c2_1
, 0, sizeof(c2_1
));
792 c2_1
.element
= choice_TESTChoice1_i2
;
794 tests
[1].val
= &c2_1
;
796 ret
+= generic_test (tests
, ntests
, sizeof(TESTChoice1
),
797 (generic_encode
)encode_TESTChoice1
,
798 (generic_length
)length_TESTChoice1
,
799 (generic_decode
)decode_TESTChoice1
,
800 (generic_free
)free_TESTChoice1
,
802 (generic_copy
)copy_TESTChoice1
);
804 memset(&c2_2
, 0, sizeof(c2_2
));
805 c2_2
.element
= choice_TESTChoice2_asn1_ellipsis
;
806 c2_2
.u
.asn1_ellipsis
.data
= "\xa2\x03\x02\x01\x02";
807 c2_2
.u
.asn1_ellipsis
.length
= 5;
808 tests
[1].val
= &c2_2
;
810 ret
+= generic_test (tests
, ntests
, sizeof(TESTChoice2
),
811 (generic_encode
)encode_TESTChoice2
,
812 (generic_length
)length_TESTChoice2
,
813 (generic_decode
)decode_TESTChoice2
,
814 (generic_free
)free_TESTChoice2
,
816 (generic_copy
)copy_TESTChoice2
);
822 cmp_TESTImplicit (void *a
, void *b
)
824 TESTImplicit
*aa
= a
;
825 TESTImplicit
*ab
= b
;
827 COMPARE_INTEGER(aa
,ab
,ti1
);
828 COMPARE_INTEGER(aa
,ab
,ti2
.foo
);
829 COMPARE_INTEGER(aa
,ab
,ti3
);
834 UNIV CONS Sequence 14
838 UNIV PRIM Integer 1 02
845 struct test_case tests
[] = {
847 "\x30\x0e\x80\x01\x00\xa1\x06\xbf"
848 "\x7f\x03\x02\x01\x02\x82\x01\x03",
852 int ret
= 0, ntests
= sizeof(tests
) / sizeof(*tests
);
855 memset(&c0
, 0, sizeof(c0
));
861 ret
+= generic_test (tests
, ntests
, sizeof(TESTImplicit
),
862 (generic_encode
)encode_TESTImplicit
,
863 (generic_length
)length_TESTImplicit
,
864 (generic_decode
)decode_TESTImplicit
,
865 (generic_free
)free_TESTImplicit
,
867 (generic_copy
)copy_TESTImplicit
);
869 #ifdef IMPLICIT_TAGGING_WORKS
870 ret
+= generic_test (tests
, ntests
, sizeof(TESTImplicit2
),
871 (generic_encode
)encode_TESTImplicit2
,
872 (generic_length
)length_TESTImplicit2
,
873 (generic_decode
)decode_TESTImplicit2
,
874 (generic_free
)free_TESTImplicit2
,
878 #endif /* IMPLICIT_TAGGING_WORKS */
883 cmp_TESTAlloc (void *a
, void *b
)
888 IF_OPT_COMPARE(aa
,ab
,tagless
) {
889 COMPARE_INTEGER(aa
,ab
,tagless
->ai
);
892 COMPARE_INTEGER(aa
,ab
,three
);
894 IF_OPT_COMPARE(aa
,ab
,tagless2
) {
895 COMPARE_OPT_OCTECT_STRING(aa
, ab
, tagless2
);
902 UNIV CONS Sequence 12
905 UNIV PRIM Integer 1 01
907 UNIV PRIM Integer 1 03
911 UNIV PRIM Integer 1 03
915 UNIV PRIM Integer 1 04
916 UNIV PRIM Integer 1 05
921 test_taglessalloc (void)
923 struct test_case tests
[] = {
925 "\x30\x0c\x30\x05\xa0\x03\x02\x01\x01\xa1\x03\x02\x01\x03",
928 "\x30\x05\xa1\x03\x02\x01\x03",
931 "\x30\x08\xa1\x03\x02\x01\x04\x02\x01\x05",
935 int ret
= 0, ntests
= sizeof(tests
) / sizeof(*tests
);
936 TESTAlloc c1
, c2
, c3
;
939 memset(&c1
, 0, sizeof(c1
));
940 c1
.tagless
= ecalloc(1, sizeof(*c1
.tagless
));
945 memset(&c2
, 0, sizeof(c2
));
950 memset(&c3
, 0, sizeof(c3
));
954 any3
.data
= "\x02\x01\x05";
958 ret
+= generic_test (tests
, ntests
, sizeof(TESTAlloc
),
959 (generic_encode
)encode_TESTAlloc
,
960 (generic_length
)length_TESTAlloc
,
961 (generic_decode
)decode_TESTAlloc
,
962 (generic_free
)free_TESTAlloc
,
964 (generic_copy
)copy_TESTAlloc
);
972 cmp_TESTOptional (void *a
, void *b
)
974 TESTOptional
*aa
= a
;
975 TESTOptional
*ab
= b
;
977 IF_OPT_COMPARE(aa
,ab
,zero
) {
978 COMPARE_OPT_INTEGER(aa
,ab
,zero
);
980 IF_OPT_COMPARE(aa
,ab
,one
) {
981 COMPARE_OPT_INTEGER(aa
,ab
,one
);
989 UNIV PRIM Integer 1 00
993 UNIV PRIM Integer 1 03
995 UNIV CONS Sequence 10
997 UNIV PRIM Integer 1 00
999 UNIV PRIM Integer 1 01
1004 test_optional (void)
1006 struct test_case tests
[] = {
1011 "\x30\x05\xa0\x03\x02\x01\x00",
1014 "\x30\x05\xa1\x03\x02\x01\x01",
1017 "\x30\x0a\xa0\x03\x02\x01\x00\xa1\x03\x02\x01\x01",
1021 int ret
= 0, ntests
= sizeof(tests
) / sizeof(*tests
);
1022 TESTOptional c0
, c1
, c2
, c3
;
1042 ret
+= generic_test (tests
, ntests
, sizeof(TESTOptional
),
1043 (generic_encode
)encode_TESTOptional
,
1044 (generic_length
)length_TESTOptional
,
1045 (generic_decode
)decode_TESTOptional
,
1046 (generic_free
)free_TESTOptional
,
1048 (generic_copy
)copy_TESTOptional
);
1054 check_fail_largetag(void)
1056 struct test_case tests
[] = {
1057 {NULL
, 14, "\x30\x0c\xbf\x87\xff\xff\xff\xff\xff\x7f\x03\x02\x01\x01",
1059 {NULL
, 0, "", "empty buffer"},
1060 {NULL
, 7, "\x30\x05\xa1\x03\x02\x02\x01",
1062 {NULL
, 7, "\x30\x04\xa1\x03\x02\x02\x01"
1064 {NULL
, 7, "\x30\x03\xa1\x03\x02\x02\x01",
1065 "three too short" },
1066 {NULL
, 7, "\x30\x02\xa1\x03\x02\x02\x01",
1068 {NULL
, 7, "\x30\x01\xa1\x03\x02\x02\x01",
1070 {NULL
, 7, "\x30\x00\xa1\x03\x02\x02\x01",
1072 {NULL
, 7, "\x30\x05\xa1\x04\x02\x02\x01",
1073 "inner one too long" },
1074 {NULL
, 7, "\x30\x00\xa1\x02\x02\x02\x01",
1075 "inner one too short" },
1076 {NULL
, 8, "\x30\x05\xbf\x7f\x03\x02\x02\x01",
1077 "inner one too short"},
1078 {NULL
, 8, "\x30\x06\xbf\x64\x03\x02\x01\x01",
1080 {NULL
, 10, "\x30\x08\xbf\x9a\x9b\x38\x03\x02\x01\x01",
1083 int ntests
= sizeof(tests
) / sizeof(*tests
);
1085 return generic_decode_fail(tests
, ntests
, sizeof(TESTLargeTag
),
1086 (generic_decode
)decode_TESTLargeTag
);
1091 check_fail_sequence(void)
1093 struct test_case tests
[] = {
1094 {NULL
, 0, "", "empty buffer"},
1096 "\x30\x16\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01\x01"
1097 "\x02\x01\x01\xa2\x03\x02\x01\x01"
1098 "missing one byte from the end, internal length ok"},
1100 "\x30\x18\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01\x01"
1101 "\x02\x01\x01\xa2\x03\x02\x01\x01",
1102 "inner length one byte too long"},
1104 "\x30\x17\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01"
1105 "\x01\x02\x01\x01\xa2\x03\x02\x01\x01",
1106 "correct buffer but missing one too short"}
1108 int ntests
= sizeof(tests
) / sizeof(*tests
);
1110 return generic_decode_fail(tests
, ntests
, sizeof(TESTSeq
),
1111 (generic_decode
)decode_TESTSeq
);
1115 check_fail_choice(void)
1117 struct test_case tests
[] = {
1119 "\xa1\x02\x02\x01\x01",
1120 "choice one too short"},
1122 "\xa1\x03\x02\x02\x01",
1123 "choice one too short inner"}
1125 int ntests
= sizeof(tests
) / sizeof(*tests
);
1127 return generic_decode_fail(tests
, ntests
, sizeof(TESTChoice1
),
1128 (generic_decode
)decode_TESTChoice1
);
1141 ret
= add_TESTSeqOf(&seq
, &i
);
1142 if (ret
) { printf("failed adding\n"); goto out
; }
1143 ret
= add_TESTSeqOf(&seq
, &i
);
1144 if (ret
) { printf("failed adding\n"); goto out
; }
1145 ret
= add_TESTSeqOf(&seq
, &i
);
1146 if (ret
) { printf("failed adding\n"); goto out
; }
1147 ret
= add_TESTSeqOf(&seq
, &i
);
1148 if (ret
) { printf("failed adding\n"); goto out
; }
1150 ret
= remove_TESTSeqOf(&seq
, seq
.len
- 1);
1151 if (ret
) { printf("failed removing\n"); goto out
; }
1152 ret
= remove_TESTSeqOf(&seq
, 2);
1153 if (ret
) { printf("failed removing\n"); goto out
; }
1154 ret
= remove_TESTSeqOf(&seq
, 0);
1155 if (ret
) { printf("failed removing\n"); goto out
; }
1156 ret
= remove_TESTSeqOf(&seq
, 0);
1157 if (ret
) { printf("failed removing\n"); goto out
; }
1158 ret
= remove_TESTSeqOf(&seq
, 0);
1160 printf("can remove from empty list");
1165 printf("seq not empty!");
1168 free_TESTSeqOf(&seq
);
1176 #define test_seq_of(type, ok, ptr) \
1178 heim_octet_string os; \
1181 ASN1_MALLOC_ENCODE(type, os.data, os.length, ptr, &size, ret); \
1184 if (os.length != size) \
1186 ret = decode_##type(os.data, os.length, &decode, &size); \
1192 free_##type(&decode); \
1202 check_seq_of_size(void)
1204 #if 0 /* template */
1205 TESTInteger integers
[4] = { 1, 2, 3, 4 };
1209 TESTSeqSizeOf1 ssof1f1
= { 1, integers
};
1210 TESTSeqSizeOf1 ssof1ok1
= { 2, integers
};
1211 TESTSeqSizeOf1 ssof1f2
= { 3, integers
};
1213 test_seq_of(TESTSeqSizeOf1
, 0, &ssof1f1
);
1214 test_seq_of(TESTSeqSizeOf1
, 1, &ssof1ok1
);
1215 test_seq_of(TESTSeqSizeOf1
, 0, &ssof1f2
);
1218 TESTSeqSizeOf2 ssof2f1
= { 0, NULL
};
1219 TESTSeqSizeOf2 ssof2ok1
= { 1, integers
};
1220 TESTSeqSizeOf2 ssof2ok2
= { 2, integers
};
1221 TESTSeqSizeOf2 ssof2f2
= { 3, integers
};
1223 test_seq_of(TESTSeqSizeOf2
, 0, &ssof2f1
);
1224 test_seq_of(TESTSeqSizeOf2
, 1, &ssof2ok1
);
1225 test_seq_of(TESTSeqSizeOf2
, 1, &ssof2ok2
);
1226 test_seq_of(TESTSeqSizeOf2
, 0, &ssof2f2
);
1229 TESTSeqSizeOf3 ssof3f1
= { 0, NULL
};
1230 TESTSeqSizeOf3 ssof3ok1
= { 1, integers
};
1231 TESTSeqSizeOf3 ssof3ok2
= { 2, integers
};
1233 test_seq_of(TESTSeqSizeOf3
, 0, &ssof3f1
);
1234 test_seq_of(TESTSeqSizeOf3
, 1, &ssof3ok1
);
1235 test_seq_of(TESTSeqSizeOf3
, 1, &ssof3ok2
);
1238 TESTSeqSizeOf4 ssof4ok1
= { 0, NULL
};
1239 TESTSeqSizeOf4 ssof4ok2
= { 1, integers
};
1240 TESTSeqSizeOf4 ssof4ok3
= { 2, integers
};
1241 TESTSeqSizeOf4 ssof4f1
= { 3, integers
};
1243 test_seq_of(TESTSeqSizeOf4
, 1, &ssof4ok1
);
1244 test_seq_of(TESTSeqSizeOf4
, 1, &ssof4ok2
);
1245 test_seq_of(TESTSeqSizeOf4
, 1, &ssof4ok3
);
1246 test_seq_of(TESTSeqSizeOf4
, 0, &ssof4f1
);
1253 check_TESTMechTypeList(void)
1255 TESTMechTypeList tl
;
1256 unsigned oid1
[] = { 1, 2, 840, 48018, 1, 2, 2};
1257 TESTMechType t1
= { 7, oid1
};
1258 unsigned oid2
[] = { 1, 2, 840, 113554, 1, 2, 2};
1259 TESTMechType t2
= { 7, oid2
};
1260 unsigned oid3
[] = { 1, 3, 6, 1, 4, 1, 311, 2, 2, 30};
1261 TESTMechType t3
= { 10, oid3
};
1262 unsigned oid4
[] = { 1, 3, 6, 1, 4, 1, 311, 2, 2, 10};
1263 TESTMechType t4
= { 10, oid4
};
1264 TESTMechType array
[4] = { t1
, t2
, t3
, t4
};
1272 ASN1_MALLOC_ENCODE(TESTMechTypeList
, ptr
, len
, &tl
, &size
, ret
);
1274 errx(1, "TESTMechTypeList: %d", ret
);
1281 main(int argc
, char **argv
)
1285 ret
+= test_principal ();
1286 ret
+= test_authenticator();
1287 ret
+= test_krb_error();
1289 ret
+= test_bit_string();
1290 ret
+= test_bit_string_rfc1510();
1294 ret
+= check_tag_length();
1295 ret
+= test_large_tag();
1296 ret
+= test_choice();
1298 ret
+= test_implicit();
1299 ret
+= test_taglessalloc();
1300 ret
+= test_optional();
1302 ret
+= check_fail_largetag();
1303 ret
+= check_fail_sequence();
1304 ret
+= check_fail_choice();
1307 ret
+= check_seq_of_size();
1309 ret
+= check_TESTMechTypeList();