xref: /freebsd/crypto/heimdal/lib/asn1/check-gen.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*
2  * Copyright (c) 1999 - 2005 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
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.
18  *
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.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39 #include <stdio.h>
40 #include <string.h>
41 #include <err.h>
42 #include <roken.h>
43 
44 #include <asn1-common.h>
45 #include <asn1_err.h>
46 #include <der.h>
47 #include <krb5_asn1.h>
48 #include <heim_asn1.h>
49 #include <rfc2459_asn1.h>
50 #include <test_asn1.h>
51 
52 #include "check-common.h"
53 
54 RCSID("$Id$");
55 
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" };
60 
61 
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)
76 
77 static int
78 cmp_principal (void *a, void *b)
79 {
80     Principal *pa = a;
81     Principal *pb = b;
82     int i;
83 
84     COMPARE_STRING(pa,pb,realm);
85     COMPARE_INTEGER(pa,pb,name.name_type);
86     COMPARE_INTEGER(pa,pb,name.name_string.len);
87 
88     for (i = 0; i < pa->name.name_string.len; i++)
89 	COMPARE_STRING(pa,pb,name.name_string.val[i]);
90 
91     return 0;
92 }
93 
94 static int
95 test_principal (void)
96 {
97 
98     struct test_case tests[] = {
99 	{ NULL, 29,
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"
102 	},
103 	{ NULL, 35,
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"
106 	  "\x2e\x53\x45"
107 	},
108 	{ NULL, 54,
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"
113 	}
114     };
115 
116 
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" }
121     };
122     int i, ret;
123     int ntests = sizeof(tests) / sizeof(*tests);
124 
125     for (i = 0; i < ntests; ++i) {
126 	tests[i].val = &values[i];
127 	if (asprintf (&tests[i].name, "Principal %d", i) < 0)
128 	    errx(1, "malloc");
129 	if (tests[i].name == NULL)
130 	    errx(1, "malloc");
131     }
132 
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,
138 			cmp_principal,
139 			NULL);
140     for (i = 0; i < ntests; ++i)
141 	free (tests[i].name);
142 
143     return ret;
144 }
145 
146 static int
147 cmp_authenticator (void *a, void *b)
148 {
149     Authenticator *aa = a;
150     Authenticator *ab = b;
151     int i;
152 
153     COMPARE_INTEGER(aa,ab,authenticator_vno);
154     COMPARE_STRING(aa,ab,crealm);
155 
156     COMPARE_INTEGER(aa,ab,cname.name_type);
157     COMPARE_INTEGER(aa,ab,cname.name_string.len);
158 
159     for (i = 0; i < aa->cname.name_string.len; i++)
160 	COMPARE_STRING(aa,ab,cname.name_string.val[i]);
161 
162     return 0;
163 }
164 
165 static int
166 test_authenticator (void)
167 {
168     struct test_case tests[] = {
169 	{ NULL, 63,
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"
175 	},
176 	{ NULL, 67,
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"
182 	  "\x39\x5a"
183 	}
184     };
185 
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 }
191     };
192     int i, ret;
193     int ntests = sizeof(tests) / sizeof(*tests);
194 
195     for (i = 0; i < ntests; ++i) {
196 	tests[i].val = &values[i];
197 	if (asprintf (&tests[i].name, "Authenticator %d", i) < 0)
198 	    errx(1, "malloc");
199 	if (tests[i].name == NULL)
200 	    errx(1, "malloc");
201     }
202 
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,
208 			cmp_authenticator,
209 			(generic_copy)copy_Authenticator);
210     for (i = 0; i < ntests; ++i)
211 	free(tests[i].name);
212 
213     return ret;
214 }
215 
216 static int
217 cmp_KRB_ERROR (void *a, void *b)
218 {
219     KRB_ERROR *aa = a;
220     KRB_ERROR *ab = b;
221     int i;
222 
223     COMPARE_INTEGER(aa,ab,pvno);
224     COMPARE_INTEGER(aa,ab,msg_type);
225 
226     IF_OPT_COMPARE(aa,ab,ctime) {
227 	COMPARE_INTEGER(aa,ab,ctime);
228     }
229     IF_OPT_COMPARE(aa,ab,cusec) {
230 	COMPARE_INTEGER(aa,ab,cusec);
231     }
232     COMPARE_INTEGER(aa,ab,stime);
233     COMPARE_INTEGER(aa,ab,susec);
234     COMPARE_INTEGER(aa,ab,error_code);
235 
236     IF_OPT_COMPARE(aa,ab,crealm) {
237 	COMPARE_OPT_STRING(aa,ab,crealm);
238     }
239 #if 0
240     IF_OPT_COMPARE(aa,ab,cname) {
241 	COMPARE_OPT_STRING(aa,ab,cname);
242     }
243 #endif
244     COMPARE_STRING(aa,ab,realm);
245 
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]);
249 
250     IF_OPT_COMPARE(aa,ab,e_text) {
251 	COMPARE_OPT_STRING(aa,ab,e_text);
252     }
253     IF_OPT_COMPARE(aa,ab,e_data) {
254 	/* COMPARE_OPT_OCTECT_STRING(aa,ab,e_data); */
255     }
256 
257     return 0;
258 }
259 
260 static int
261 test_krb_error (void)
262 {
263     struct test_case tests[] = {
264 	{ NULL, 127,
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",
273 	  "KRB-ERROR Test 1"
274 	}
275     };
276     int ntests = sizeof(tests) / sizeof(*tests);
277     KRB_ERROR e1;
278     PrincipalName lhaprincipalname = { 1, { 1, lha_principal } };
279     PrincipalName tgtprincipalname = { 1, { 2, nada_tgt_principal } };
280     char *realm = "NADA.KTH.SE";
281 
282     e1.pvno = 5;
283     e1.msg_type = 30;
284     e1.ctime = NULL;
285     e1.cusec = NULL;
286     e1.stime = 1069632679;
287     e1.susec = 322981;
288     e1.error_code = 31;
289     e1.crealm = &realm;
290     e1.cname = &lhaprincipalname;
291     e1.realm = "NADA.KTH.SE";
292     e1.sname = tgtprincipalname;
293     e1.e_text = NULL;
294     e1.e_data = NULL;
295 
296     tests[0].val = &e1;
297 
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,
303 			 cmp_KRB_ERROR,
304 			 (generic_copy)copy_KRB_ERROR);
305 }
306 
307 static int
308 cmp_Name (void *a, void *b)
309 {
310     Name *aa = a;
311     Name *ab = b;
312 
313     COMPARE_INTEGER(aa,ab,element);
314 
315     return 0;
316 }
317 
318 static int
319 test_Name (void)
320 {
321     struct test_case tests[] = {
322 	{ NULL, 35,
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"
325 	  "\x4f\x4c\x4d",
326 	  "Name CN=Love+L=STOCKHOLM"
327 	},
328 	{ NULL, 35,
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"
331 	  "\x4f\x4c\x4d",
332 	  "Name L=STOCKHOLM+CN=Love"
333 	}
334     };
335 
336     int ntests = sizeof(tests) / sizeof(*tests);
337     Name n1, n2;
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 };
344 
345     /* n1 */
346     n1.element = choice_Name_rdnSequence;
347     n1.u.rdnSequence.val = rdn1;
348     n1.u.rdnSequence.len = sizeof(rdn1)/sizeof(rdn1[0]);
349     rdn1[0].val = atv1;
350     rdn1[0].len = sizeof(atv1)/sizeof(atv1[0]);
351 
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;
357 
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;
363 
364     /* n2 */
365     n2.element = choice_Name_rdnSequence;
366     n2.u.rdnSequence.val = rdn2;
367     n2.u.rdnSequence.len = sizeof(rdn2)/sizeof(rdn2[0]);
368     rdn2[0].val = atv2;
369     rdn2[0].len = sizeof(atv2)/sizeof(atv2[0]);
370 
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;
376 
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;
382 
383     /* */
384     tests[0].val = &n1;
385     tests[1].val = &n2;
386 
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,
392 			 cmp_Name,
393 			 (generic_copy)copy_Name);
394 }
395 
396 static int
397 cmp_KeyUsage (void *a, void *b)
398 {
399     KeyUsage *aa = a;
400     KeyUsage *ab = b;
401 
402     return KeyUsage2int(*aa) != KeyUsage2int(*ab);
403 }
404 
405 static int
406 test_bit_string (void)
407 {
408     struct test_case tests[] = {
409 	{ NULL, 4,
410 	  "\x03\x02\x07\x80",
411 	  "bitstring 1"
412 	},
413 	{ NULL, 4,
414 	  "\x03\x02\x05\xa0",
415 	  "bitstring 2"
416 	},
417 	{ NULL, 5,
418 	  "\x03\x03\x07\x00\x80",
419 	  "bitstring 3"
420 	},
421 	{ NULL, 3,
422 	  "\x03\x01\x00",
423 	  "bitstring 4"
424 	}
425     };
426 
427     int ntests = sizeof(tests) / sizeof(*tests);
428     KeyUsage ku1, ku2, ku3, ku4;
429 
430     memset(&ku1, 0, sizeof(ku1));
431     ku1.digitalSignature = 1;
432     tests[0].val = &ku1;
433 
434     memset(&ku2, 0, sizeof(ku2));
435     ku2.digitalSignature = 1;
436     ku2.keyEncipherment = 1;
437     tests[1].val = &ku2;
438 
439     memset(&ku3, 0, sizeof(ku3));
440     ku3.decipherOnly = 1;
441     tests[2].val = &ku3;
442 
443     memset(&ku4, 0, sizeof(ku4));
444     tests[3].val = &ku4;
445 
446 
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,
452 			 cmp_KeyUsage,
453 			 (generic_copy)copy_KeyUsage);
454 }
455 
456 static int
457 cmp_TicketFlags (void *a, void *b)
458 {
459     TicketFlags *aa = a;
460     TicketFlags *ab = b;
461 
462     return TicketFlags2int(*aa) != TicketFlags2int(*ab);
463 }
464 
465 static int
466 test_bit_string_rfc1510 (void)
467 {
468     struct test_case tests[] = {
469 	{ NULL, 7,
470 	  "\x03\x05\x00\x80\x00\x00\x00",
471 	  "TF bitstring 1"
472 	},
473 	{ NULL, 7,
474 	  "\x03\x05\x00\x40\x20\x00\x00",
475 	  "TF bitstring 2"
476 	},
477 	{ NULL, 7,
478 	  "\x03\x05\x00\x00\x20\x00\x00",
479 	  "TF bitstring 3"
480 	},
481 	{ NULL, 7,
482 	  "\x03\x05\x00\x00\x00\x00\x00",
483 	  "TF bitstring 4"
484 	}
485     };
486 
487     int ntests = sizeof(tests) / sizeof(*tests);
488     TicketFlags tf1, tf2, tf3, tf4;
489 
490     memset(&tf1, 0, sizeof(tf1));
491     tf1.reserved = 1;
492     tests[0].val = &tf1;
493 
494     memset(&tf2, 0, sizeof(tf2));
495     tf2.forwardable = 1;
496     tf2.pre_authent = 1;
497     tests[1].val = &tf2;
498 
499     memset(&tf3, 0, sizeof(tf3));
500     tf3.pre_authent = 1;
501     tests[2].val = &tf3;
502 
503     memset(&tf4, 0, sizeof(tf4));
504     tests[3].val = &tf4;
505 
506 
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,
512 			 cmp_TicketFlags,
513 			 (generic_copy)copy_TicketFlags);
514 }
515 
516 static int
517 cmp_KerberosTime (void *a, void *b)
518 {
519     KerberosTime *aa = a;
520     KerberosTime *ab = b;
521 
522     return *aa != *ab;
523 }
524 
525 static int
526 test_time (void)
527 {
528     struct test_case tests[] = {
529 	{ NULL,  17,
530 	  "\x18\x0f\x31\x39\x37\x30\x30\x31\x30\x31\x30\x31\x31\x38\x33\x31"
531 	  "\x5a",
532 	  "time 1" },
533 	{ NULL,  17,
534 	  "\x18\x0f\x32\x30\x30\x39\x30\x35\x32\x34\x30\x32\x30\x32\x34\x30"
535 	  "\x5a"
536 	  "time 2" }
537     };
538 
539     int ntests = sizeof(tests) / sizeof(*tests);
540     KerberosTime times[] = {
541 	4711,
542 	1243130560
543     };
544 
545     tests[0].val = &times[0];
546     tests[1].val = &times[1];
547 
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,
553 			 cmp_KerberosTime,
554 			 (generic_copy)copy_KerberosTime);
555 }
556 
557 struct {
558     const char *cert;
559     size_t len;
560 } certs[] = {
561     {
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",
601 	624 },
602     {
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",
641 	600
642     }
643 };
644 
645 static int
646 test_cert(void)
647 {
648     Certificate c, c2;
649     size_t size;
650     size_t i;
651     int ret;
652 
653     for (i = 0; i < sizeof(certs)/sizeof(certs[0]); i++) {
654 
655 	ret = decode_Certificate((unsigned char *)certs[i].cert,
656 				 certs[i].len, &c, &size);
657 	if (ret)
658 	    return ret;
659 
660 	ret = copy_Certificate(&c, &c2);
661 	free_Certificate(&c);
662 	if (ret)
663 	    return ret;
664 
665 	free_Certificate(&c2);
666     }
667 
668     return 0;
669 }
670 
671 
672 static int
673 cmp_TESTLargeTag (void *a, void *b)
674 {
675     TESTLargeTag *aa = a;
676     TESTLargeTag *ab = b;
677 
678     COMPARE_INTEGER(aa,ab,foo);
679     COMPARE_INTEGER(aa,ab,bar);
680     return 0;
681 }
682 
683 static int
684 test_large_tag (void)
685 {
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" }
688     };
689 
690     int ntests = sizeof(tests) / sizeof(*tests);
691     TESTLargeTag lt1;
692 
693     memset(&lt1, 0, sizeof(lt1));
694     lt1.foo = 1;
695     lt1.bar = 2;
696 
697     tests[0].val = &lt1;
698 
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,
704 			 cmp_TESTLargeTag,
705 			 (generic_copy)copy_TESTLargeTag);
706 }
707 
708 struct test_data {
709     int ok;
710     size_t len;
711     size_t expected_len;
712     void *data;
713 };
714 
715 static int
716 check_tag_length(void)
717 {
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"}
729     };
730     size_t sz;
731     TESTuint32 values[] = {0, 127, 128, 256, 512,
732 			 0, 127, 128, 256, 512 };
733     TESTuint32 u;
734     int i, ret, failed = 0;
735     void *buf;
736 
737     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++) {
738 	struct map_page *page;
739 
740 	buf = map_alloc(OVERRUN, td[i].data, td[i].len, &page);
741 
742 	ret = decode_TESTuint32(buf, td[i].len, &u, &sz);
743 	if (ret) {
744 	    if (td[i].ok) {
745 		printf("failed with tag len test %d\n", i);
746 		failed = 1;
747 	    }
748 	} else {
749 	    if (td[i].ok == 0) {
750 		printf("failed with success for tag len test %d\n", i);
751 		failed = 1;
752 	    }
753 	    if (td[i].expected_len != sz) {
754 		printf("wrong expected size for tag test %d\n", i);
755 		failed = 1;
756 	    }
757 	    if (values[i] != u) {
758 		printf("wrong value for tag test %d\n", i);
759 		failed = 1;
760 	    }
761 	}
762 	map_free(page, "test", "decode");
763     }
764     return failed;
765 }
766 
767 static int
768 cmp_TESTChoice (void *a, void *b)
769 {
770     return 0;
771 }
772 
773 static int
774 test_choice (void)
775 {
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" }
779     };
780 
781     int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
782     TESTChoice1 c1;
783     TESTChoice1 c2_1;
784     TESTChoice2 c2_2;
785 
786     memset(&c1, 0, sizeof(c1));
787     c1.element = choice_TESTChoice1_i1;
788     c1.u.i1 = 1;
789     tests[0].val = &c1;
790 
791     memset(&c2_1, 0, sizeof(c2_1));
792     c2_1.element = choice_TESTChoice1_i2;
793     c2_1.u.i2 = 2;
794     tests[1].val = &c2_1;
795 
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,
801 			 cmp_TESTChoice,
802 			 (generic_copy)copy_TESTChoice1);
803 
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;
809 
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,
815 			 cmp_TESTChoice,
816 			 (generic_copy)copy_TESTChoice2);
817 
818     return ret;
819 }
820 
821 static int
822 cmp_TESTImplicit (void *a, void *b)
823 {
824     TESTImplicit *aa = a;
825     TESTImplicit *ab = b;
826 
827     COMPARE_INTEGER(aa,ab,ti1);
828     COMPARE_INTEGER(aa,ab,ti2.foo);
829     COMPARE_INTEGER(aa,ab,ti3);
830     return 0;
831 }
832 
833 /*
834 UNIV CONS Sequence 14
835   CONTEXT PRIM 0 1 00
836   CONTEXT CONS 1 6
837    CONTEXT CONS 127 3
838      UNIV PRIM Integer 1 02
839   CONTEXT PRIM 2 1 03
840 */
841 
842 static int
843 test_implicit (void)
844 {
845     struct test_case tests[] = {
846 	{ NULL,  16,
847 	  "\x30\x0e\x80\x01\x00\xa1\x06\xbf"
848 	  "\x7f\x03\x02\x01\x02\x82\x01\x03",
849 	  "implicit 1" }
850     };
851 
852     int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
853     TESTImplicit c0;
854 
855     memset(&c0, 0, sizeof(c0));
856     c0.ti1 = 0;
857     c0.ti2.foo = 2;
858     c0.ti3 = 3;
859     tests[0].val = &c0;
860 
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,
866 			 cmp_TESTImplicit,
867 			 (generic_copy)copy_TESTImplicit);
868 
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,
875 			 cmp_TESTImplicit,
876 			 NULL);
877 
878 #endif /* IMPLICIT_TAGGING_WORKS */
879     return ret;
880 }
881 
882 static int
883 cmp_TESTAlloc (void *a, void *b)
884 {
885     TESTAlloc *aa = a;
886     TESTAlloc *ab = b;
887 
888     IF_OPT_COMPARE(aa,ab,tagless) {
889 	COMPARE_INTEGER(aa,ab,tagless->ai);
890     }
891 
892     COMPARE_INTEGER(aa,ab,three);
893 
894     IF_OPT_COMPARE(aa,ab,tagless2) {
895 	COMPARE_OPT_OCTECT_STRING(aa, ab, tagless2);
896     }
897 
898     return 0;
899 }
900 
901 /*
902 UNIV CONS Sequence 12
903   UNIV CONS Sequence 5
904     CONTEXT CONS 0 3
905       UNIV PRIM Integer 1 01
906   CONTEXT CONS 1 3
907     UNIV PRIM Integer 1 03
908 
909 UNIV CONS Sequence 5
910   CONTEXT CONS 1 3
911     UNIV PRIM Integer 1 03
912 
913 UNIV CONS Sequence 8
914   CONTEXT CONS 1 3
915     UNIV PRIM Integer 1 04
916   UNIV PRIM Integer 1 05
917 
918 */
919 
920 static int
921 test_taglessalloc (void)
922 {
923     struct test_case tests[] = {
924 	{ NULL,  14,
925 	  "\x30\x0c\x30\x05\xa0\x03\x02\x01\x01\xa1\x03\x02\x01\x03",
926 	  "alloc 1" },
927 	{ NULL,  7,
928 	  "\x30\x05\xa1\x03\x02\x01\x03",
929 	  "alloc 2" },
930 	{ NULL,  10,
931 	  "\x30\x08\xa1\x03\x02\x01\x04\x02\x01\x05",
932 	  "alloc 3" }
933     };
934 
935     int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
936     TESTAlloc c1, c2, c3;
937     heim_any any3;
938 
939     memset(&c1, 0, sizeof(c1));
940     c1.tagless = ecalloc(1, sizeof(*c1.tagless));
941     c1.tagless->ai = 1;
942     c1.three = 3;
943     tests[0].val = &c1;
944 
945     memset(&c2, 0, sizeof(c2));
946     c2.tagless = NULL;
947     c2.three = 3;
948     tests[1].val = &c2;
949 
950     memset(&c3, 0, sizeof(c3));
951     c3.tagless = NULL;
952     c3.three = 4;
953     c3.tagless2 = &any3;
954     any3.data = "\x02\x01\x05";
955     any3.length = 3;
956     tests[2].val = &c3;
957 
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,
963 			 cmp_TESTAlloc,
964 			 (generic_copy)copy_TESTAlloc);
965 
966     free(c1.tagless);
967 
968     return ret;
969 }
970 
971 static int
972 cmp_TESTOptional (void *a, void *b)
973 {
974     TESTOptional *aa = a;
975     TESTOptional *ab = b;
976 
977     IF_OPT_COMPARE(aa,ab,zero) {
978 	COMPARE_OPT_INTEGER(aa,ab,zero);
979     }
980     IF_OPT_COMPARE(aa,ab,one) {
981 	COMPARE_OPT_INTEGER(aa,ab,one);
982     }
983     return 0;
984 }
985 
986 /*
987 UNIV CONS Sequence 5
988   CONTEXT CONS 0 3
989     UNIV PRIM Integer 1 00
990 
991 UNIV CONS Sequence 5
992   CONTEXT CONS 1 3
993     UNIV PRIM Integer 1 03
994 
995 UNIV CONS Sequence 10
996   CONTEXT CONS 0 3
997     UNIV PRIM Integer 1 00
998   CONTEXT CONS 1 3
999     UNIV PRIM Integer 1 01
1000 
1001 */
1002 
1003 static int
1004 test_optional (void)
1005 {
1006     struct test_case tests[] = {
1007 	{ NULL,  2,
1008 	  "\x30\x00",
1009 	  "optional 0" },
1010 	{ NULL,  7,
1011 	  "\x30\x05\xa0\x03\x02\x01\x00",
1012 	  "optional 1" },
1013 	{ NULL,  7,
1014 	  "\x30\x05\xa1\x03\x02\x01\x01",
1015 	  "optional 2" },
1016 	{ NULL,  12,
1017 	  "\x30\x0a\xa0\x03\x02\x01\x00\xa1\x03\x02\x01\x01",
1018 	  "optional 3" }
1019     };
1020 
1021     int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
1022     TESTOptional c0, c1, c2, c3;
1023     int zero = 0;
1024     int one = 1;
1025 
1026     c0.zero = NULL;
1027     c0.one = NULL;
1028     tests[0].val = &c0;
1029 
1030     c1.zero = &zero;
1031     c1.one = NULL;
1032     tests[1].val = &c1;
1033 
1034     c2.zero = NULL;
1035     c2.one = &one;
1036     tests[2].val = &c2;
1037 
1038     c3.zero = &zero;
1039     c3.one = &one;
1040     tests[3].val = &c3;
1041 
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,
1047 			 cmp_TESTOptional,
1048 			 (generic_copy)copy_TESTOptional);
1049 
1050     return ret;
1051 }
1052 
1053 static int
1054 check_fail_largetag(void)
1055 {
1056     struct test_case tests[] = {
1057 	{NULL, 14, "\x30\x0c\xbf\x87\xff\xff\xff\xff\xff\x7f\x03\x02\x01\x01",
1058 	 "tag overflow"},
1059 	{NULL, 0, "", "empty buffer"},
1060 	{NULL, 7, "\x30\x05\xa1\x03\x02\x02\x01",
1061 	 "one too short" },
1062 	{NULL, 7, "\x30\x04\xa1\x03\x02\x02\x01"
1063 	 "two too short" },
1064 	{NULL, 7, "\x30\x03\xa1\x03\x02\x02\x01",
1065 	 "three too short" },
1066 	{NULL, 7, "\x30\x02\xa1\x03\x02\x02\x01",
1067 	 "four too short" },
1068 	{NULL, 7, "\x30\x01\xa1\x03\x02\x02\x01",
1069 	 "five too short" },
1070 	{NULL, 7, "\x30\x00\xa1\x03\x02\x02\x01",
1071 	 "six too short" },
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",
1079 	 "wrong tag"},
1080 	{NULL, 10, "\x30\x08\xbf\x9a\x9b\x38\x03\x02\x01\x01",
1081 	 "still wrong tag"}
1082     };
1083     int ntests = sizeof(tests) / sizeof(*tests);
1084 
1085     return generic_decode_fail(tests, ntests, sizeof(TESTLargeTag),
1086 			       (generic_decode)decode_TESTLargeTag);
1087 }
1088 
1089 
1090 static int
1091 check_fail_sequence(void)
1092 {
1093     struct test_case tests[] = {
1094 	{NULL, 0, "", "empty buffer"},
1095 	{NULL, 24,
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"},
1099 	{NULL, 25,
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"},
1103 	{NULL, 24,
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"}
1107     };
1108     int ntests = sizeof(tests) / sizeof(*tests);
1109 
1110     return generic_decode_fail(tests, ntests, sizeof(TESTSeq),
1111 			       (generic_decode)decode_TESTSeq);
1112 }
1113 
1114 static int
1115 check_fail_choice(void)
1116 {
1117     struct test_case tests[] = {
1118 	{NULL, 6,
1119 	 "\xa1\x02\x02\x01\x01",
1120 	 "choice one too short"},
1121 	{NULL, 6,
1122 	 "\xa1\x03\x02\x02\x01",
1123 	 "choice one too short inner"}
1124     };
1125     int ntests = sizeof(tests) / sizeof(*tests);
1126 
1127     return generic_decode_fail(tests, ntests, sizeof(TESTChoice1),
1128 			       (generic_decode)decode_TESTChoice1);
1129 }
1130 
1131 static int
1132 check_seq(void)
1133 {
1134     TESTSeqOf seq;
1135     TESTInteger i;
1136     int ret;
1137 
1138     seq.val = NULL;
1139     seq.len = 0;
1140 
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; }
1149 
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);
1159     if (ret == 0) {
1160 	printf("can remove from empty list");
1161 	return 1;
1162     }
1163 
1164     if (seq.len != 0) {
1165 	printf("seq not empty!");
1166 	return 1;
1167     }
1168     free_TESTSeqOf(&seq);
1169     ret = 0;
1170 
1171 out:
1172 
1173     return ret;
1174 }
1175 
1176 #define test_seq_of(type, ok, ptr)					\
1177 {									\
1178     heim_octet_string os;						\
1179     size_t size;							\
1180     type decode;							\
1181     ASN1_MALLOC_ENCODE(type, os.data, os.length, ptr, &size, ret);	\
1182     if (ret)								\
1183 	return ret;							\
1184     if (os.length != size)						\
1185 	abort();							\
1186     ret = decode_##type(os.data, os.length, &decode, &size);		\
1187     free(os.data);							\
1188     if (ret) {								\
1189 	if (ok)								\
1190 	    return 1;							\
1191     } else {								\
1192 	free_##type(&decode);						\
1193 	if (!ok)							\
1194 	    return 1;							\
1195 	if (size != 0)							\
1196             return 1;							\
1197     }									\
1198     return 0;								\
1199 }
1200 
1201 static int
1202 check_seq_of_size(void)
1203 {
1204 #if 0 /* template */
1205     TESTInteger integers[4] = { 1, 2, 3, 4 };
1206     int ret;
1207 
1208     {
1209 	TESTSeqSizeOf1 ssof1f1 = { 1, integers };
1210 	TESTSeqSizeOf1 ssof1ok1 = { 2, integers };
1211 	TESTSeqSizeOf1 ssof1f2 = { 3, integers };
1212 
1213 	test_seq_of(TESTSeqSizeOf1, 0, &ssof1f1);
1214 	test_seq_of(TESTSeqSizeOf1, 1, &ssof1ok1);
1215 	test_seq_of(TESTSeqSizeOf1, 0, &ssof1f2);
1216     }
1217     {
1218 	TESTSeqSizeOf2 ssof2f1 = { 0, NULL };
1219 	TESTSeqSizeOf2 ssof2ok1 = { 1, integers };
1220 	TESTSeqSizeOf2 ssof2ok2 = { 2, integers };
1221 	TESTSeqSizeOf2 ssof2f2 = { 3, integers };
1222 
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);
1227     }
1228     {
1229 	TESTSeqSizeOf3 ssof3f1 = { 0, NULL };
1230 	TESTSeqSizeOf3 ssof3ok1 = { 1, integers };
1231 	TESTSeqSizeOf3 ssof3ok2 = { 2, integers };
1232 
1233 	test_seq_of(TESTSeqSizeOf3, 0, &ssof3f1);
1234 	test_seq_of(TESTSeqSizeOf3, 1, &ssof3ok1);
1235 	test_seq_of(TESTSeqSizeOf3, 1, &ssof3ok2);
1236     }
1237     {
1238 	TESTSeqSizeOf4 ssof4ok1 = { 0, NULL };
1239 	TESTSeqSizeOf4 ssof4ok2 = { 1, integers };
1240 	TESTSeqSizeOf4 ssof4ok3 = { 2, integers };
1241 	TESTSeqSizeOf4 ssof4f1  = { 3, integers };
1242 
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);
1247    }
1248 #endif
1249     return 0;
1250 }
1251 
1252 static int
1253 check_TESTMechTypeList(void)
1254 {
1255     TESTMechTypeList tl;
1256     unsigned oid1[] =  { 1, 2, 840, 48018, 1, 2, 2};
1257     unsigned oid2[] =  { 1, 2, 840, 113554, 1, 2, 2};
1258     unsigned oid3[] =   { 1, 3, 6, 1, 4, 1, 311, 2, 2, 30};
1259     unsigned oid4[] =   { 1, 3, 6, 1, 4, 1, 311, 2, 2, 10};
1260     TESTMechType array[] = {{ 7, oid1 },
1261                             { 7, oid2 },
1262                             { 10, oid3 },
1263                             { 10, oid4 }};
1264     size_t size, len;
1265     void *ptr;
1266     int ret;
1267 
1268     tl.len = 4;
1269     tl.val = array;
1270 
1271     ASN1_MALLOC_ENCODE(TESTMechTypeList, ptr, len, &tl, &size, ret);
1272     if (ret)
1273 	errx(1, "TESTMechTypeList: %d", ret);
1274     if (len != size)
1275 	abort();
1276     return 0;
1277 }
1278 
1279 int
1280 main(int argc, char **argv)
1281 {
1282     int ret = 0;
1283 
1284     ret += test_principal ();
1285     ret += test_authenticator();
1286     ret += test_krb_error();
1287     ret += test_Name();
1288     ret += test_bit_string();
1289     ret += test_bit_string_rfc1510();
1290     ret += test_time();
1291     ret += test_cert();
1292 
1293     ret += check_tag_length();
1294     ret += test_large_tag();
1295     ret += test_choice();
1296 
1297     ret += test_implicit();
1298     ret += test_taglessalloc();
1299     ret += test_optional();
1300 
1301     ret += check_fail_largetag();
1302     ret += check_fail_sequence();
1303     ret += check_fail_choice();
1304 
1305     ret += check_seq();
1306     ret += check_seq_of_size();
1307 
1308     ret += check_TESTMechTypeList();
1309 
1310     return ret;
1311 }
1312