xref: /freebsd/crypto/heimdal/lib/asn1/check-gen.c (revision b985c9ca)
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 check_tag_length64(void)
769 {
770     struct test_data td[] = {
771 	{ 1, 3, 3, "\x02\x01\x00"},
772 	{ 1, 7, 7, "\x02\x05\x01\xff\xff\xff\xff"},
773 	{ 1, 7, 7, "\x02\x05\x02\x00\x00\x00\x00"},
774 	{ 1, 9, 9, "\x02\x07\x7f\xff\xff\xff\xff\xff\xff"},
775 	{ 1, 10, 10, "\x02\x08\x00\x80\x00\x00\x00\x00\x00\x00"},
776 	{ 1, 10, 10, "\x02\x08\x7f\xff\xff\xff\xff\xff\xff\xff"},
777 	{ 1, 11, 11, "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff"},
778 	{ 0, 3, 0, "\x02\x02\x00"},
779 	{ 0, 3, 0, "\x02\x7f\x7f"},
780 	{ 0, 4, 0, "\x02\x03\x00\x80"},
781 	{ 0, 4, 0, "\x02\x7f\x01\x00"},
782 	{ 0, 5, 0, "\x02\xff\x7f\x02\x00"}
783     };
784     size_t sz;
785     TESTuint64 values[] = {0, 8589934591LL, 8589934592LL,
786 			   36028797018963967LL, 36028797018963968LL,
787 			   9223372036854775807LL, 18446744073709551615ULL,
788 			   0, 127, 128, 256, 512 };
789     TESTuint64 u;
790     int i, ret, failed = 0;
791     void *buf;
792 
793     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++) {
794 	struct map_page *page;
795 
796 	buf = map_alloc(OVERRUN, td[i].data, td[i].len, &page);
797 
798 	ret = decode_TESTuint64(buf, td[i].len, &u, &sz);
799 	if (ret) {
800 	    if (td[i].ok) {
801 		printf("failed with tag len test %d\n", i);
802 		printf("ret = %d\n", ret);
803 		failed = 1;
804 	    }
805 	} else {
806 	    if (td[i].ok == 0) {
807 		printf("failed with success for tag len test %d\n", i);
808 		failed = 1;
809 	    }
810 	    if (td[i].expected_len != sz) {
811 		printf("wrong expected size for tag test %d\n", i);
812 		printf("sz = %d\n", sz);
813 		failed = 1;
814 	    }
815 	    if (values[i] != u) {
816 		printf("wrong value for tag test %d\n", i);
817 		printf("Expected value: %lld\nActual value: %lld\n", values[i], u);
818 		failed = 1;
819 	    }
820 	}
821 	map_free(page, "test", "decode");
822     }
823     return failed;
824 }
825 
826 static int
827 check_tag_length64s(void)
828 {
829     struct test_data td[] = {
830 	{ 1, 3, 3, "\x02\x01\x00"},
831 	{ 1, 7, 7, "\x02\x05\xfe\x00\x00\x00\x01"},
832 	{ 1, 7, 7, "\x02\x05\xfe\x00\x00\x00\x00"},
833 	{ 1, 9, 9, "\x02\x07\x80\x00\x00\x00\x00\x00\x01"},
834 	{ 1, 9, 9, "\x02\x07\x80\x00\x00\x00\x00\x00\x00"},
835 	{ 1, 10, 10, "\x02\x08\x80\x00\x00\x00\x00\x00\x00\x01"},
836 	{ 1, 9, 9, "\x02\x07\x80\x00\x00\x00\x00\x00\x01"},
837 	{ 0, 3, 0, "\x02\x02\x00"},
838 	{ 0, 3, 0, "\x02\x7f\x7f"},
839 	{ 0, 4, 0, "\x02\x03\x00\x80"},
840 	{ 0, 4, 0, "\x02\x7f\x01\x00"},
841 	{ 0, 5, 0, "\x02\xff\x7f\x02\x00"}
842     };
843     size_t sz;
844     TESTint64 values[] = {0, -8589934591LL, -8589934592LL,
845 			   -36028797018963967LL, -36028797018963968LL,
846 			   -9223372036854775807LL, -36028797018963967LL,
847 			   0, 127, 128, 256, 512 };
848     TESTint64 u;
849     int i, ret, failed = 0;
850     void *buf;
851 
852     for (i = 0; i < sizeof(td)/sizeof(td[0]); i++) {
853 	struct map_page *page;
854 
855 	buf = map_alloc(OVERRUN, td[i].data, td[i].len, &page);
856 
857 	ret = decode_TESTint64(buf, td[i].len, &u, &sz);
858 	if (ret) {
859 	    if (td[i].ok) {
860 		printf("failed with tag len test %d\n", i);
861 		printf("ret = %d\n", ret);
862 		failed = 1;
863 	    }
864 	} else {
865 	    if (td[i].ok == 0) {
866 		printf("failed with success for tag len test %d\n", i);
867 		failed = 1;
868 	    }
869 	    if (td[i].expected_len != sz) {
870 		printf("wrong expected size for tag test %d\n", i);
871 		printf("sz = %d\n", sz);
872 		failed = 1;
873 	    }
874 	    if (values[i] != u) {
875 		printf("wrong value for tag test %d\n", i);
876 		printf("Expected value: %lld\nActual value: %lld\n", values[i], u);
877 		failed = 1;
878 	    }
879 	}
880 	map_free(page, "test", "decode");
881     }
882     return failed;
883 }
884 
885 static int
886 cmp_TESTChoice (void *a, void *b)
887 {
888     return 0;
889 }
890 
891 static int
892 test_choice (void)
893 {
894     struct test_case tests[] = {
895 	{ NULL,  5,  "\xa1\x03\x02\x01\x01", "large choice 1" },
896 	{ NULL,  5,  "\xa2\x03\x02\x01\x02", "large choice 2" }
897     };
898 
899     int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
900     TESTChoice1 c1;
901     TESTChoice1 c2_1;
902     TESTChoice2 c2_2;
903 
904     memset(&c1, 0, sizeof(c1));
905     c1.element = choice_TESTChoice1_i1;
906     c1.u.i1 = 1;
907     tests[0].val = &c1;
908 
909     memset(&c2_1, 0, sizeof(c2_1));
910     c2_1.element = choice_TESTChoice1_i2;
911     c2_1.u.i2 = 2;
912     tests[1].val = &c2_1;
913 
914     ret += generic_test (tests, ntests, sizeof(TESTChoice1),
915 			 (generic_encode)encode_TESTChoice1,
916 			 (generic_length)length_TESTChoice1,
917 			 (generic_decode)decode_TESTChoice1,
918 			 (generic_free)free_TESTChoice1,
919 			 cmp_TESTChoice,
920 			 (generic_copy)copy_TESTChoice1);
921 
922     memset(&c2_2, 0, sizeof(c2_2));
923     c2_2.element = choice_TESTChoice2_asn1_ellipsis;
924     c2_2.u.asn1_ellipsis.data = "\xa2\x03\x02\x01\x02";
925     c2_2.u.asn1_ellipsis.length = 5;
926     tests[1].val = &c2_2;
927 
928     ret += generic_test (tests, ntests, sizeof(TESTChoice2),
929 			 (generic_encode)encode_TESTChoice2,
930 			 (generic_length)length_TESTChoice2,
931 			 (generic_decode)decode_TESTChoice2,
932 			 (generic_free)free_TESTChoice2,
933 			 cmp_TESTChoice,
934 			 (generic_copy)copy_TESTChoice2);
935 
936     return ret;
937 }
938 
939 static int
940 cmp_TESTImplicit (void *a, void *b)
941 {
942     TESTImplicit *aa = a;
943     TESTImplicit *ab = b;
944 
945     COMPARE_INTEGER(aa,ab,ti1);
946     COMPARE_INTEGER(aa,ab,ti2.foo);
947     COMPARE_INTEGER(aa,ab,ti3);
948     return 0;
949 }
950 
951 /*
952 UNIV CONS Sequence 14
953   CONTEXT PRIM 0 1 00
954   CONTEXT CONS 1 6
955    CONTEXT CONS 127 3
956      UNIV PRIM Integer 1 02
957   CONTEXT PRIM 2 1 03
958 */
959 
960 static int
961 test_implicit (void)
962 {
963     struct test_case tests[] = {
964 	{ NULL,  16,
965 	  "\x30\x0e\x80\x01\x00\xa1\x06\xbf"
966 	  "\x7f\x03\x02\x01\x02\x82\x01\x03",
967 	  "implicit 1" }
968     };
969 
970     int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
971     TESTImplicit c0;
972 
973     memset(&c0, 0, sizeof(c0));
974     c0.ti1 = 0;
975     c0.ti2.foo = 2;
976     c0.ti3 = 3;
977     tests[0].val = &c0;
978 
979     ret += generic_test (tests, ntests, sizeof(TESTImplicit),
980 			 (generic_encode)encode_TESTImplicit,
981 			 (generic_length)length_TESTImplicit,
982 			 (generic_decode)decode_TESTImplicit,
983 			 (generic_free)free_TESTImplicit,
984 			 cmp_TESTImplicit,
985 			 (generic_copy)copy_TESTImplicit);
986 
987 #ifdef IMPLICIT_TAGGING_WORKS
988     ret += generic_test (tests, ntests, sizeof(TESTImplicit2),
989 			 (generic_encode)encode_TESTImplicit2,
990 			 (generic_length)length_TESTImplicit2,
991 			 (generic_decode)decode_TESTImplicit2,
992 			 (generic_free)free_TESTImplicit2,
993 			 cmp_TESTImplicit,
994 			 NULL);
995 
996 #endif /* IMPLICIT_TAGGING_WORKS */
997     return ret;
998 }
999 
1000 static int
1001 cmp_TESTAlloc (void *a, void *b)
1002 {
1003     TESTAlloc *aa = a;
1004     TESTAlloc *ab = b;
1005 
1006     IF_OPT_COMPARE(aa,ab,tagless) {
1007 	COMPARE_INTEGER(aa,ab,tagless->ai);
1008     }
1009 
1010     COMPARE_INTEGER(aa,ab,three);
1011 
1012     IF_OPT_COMPARE(aa,ab,tagless2) {
1013 	COMPARE_OPT_OCTECT_STRING(aa, ab, tagless2);
1014     }
1015 
1016     return 0;
1017 }
1018 
1019 /*
1020 UNIV CONS Sequence 12
1021   UNIV CONS Sequence 5
1022     CONTEXT CONS 0 3
1023       UNIV PRIM Integer 1 01
1024   CONTEXT CONS 1 3
1025     UNIV PRIM Integer 1 03
1026 
1027 UNIV CONS Sequence 5
1028   CONTEXT CONS 1 3
1029     UNIV PRIM Integer 1 03
1030 
1031 UNIV CONS Sequence 8
1032   CONTEXT CONS 1 3
1033     UNIV PRIM Integer 1 04
1034   UNIV PRIM Integer 1 05
1035 
1036 */
1037 
1038 static int
1039 test_taglessalloc (void)
1040 {
1041     struct test_case tests[] = {
1042 	{ NULL,  14,
1043 	  "\x30\x0c\x30\x05\xa0\x03\x02\x01\x01\xa1\x03\x02\x01\x03",
1044 	  "alloc 1" },
1045 	{ NULL,  7,
1046 	  "\x30\x05\xa1\x03\x02\x01\x03",
1047 	  "alloc 2" },
1048 	{ NULL,  10,
1049 	  "\x30\x08\xa1\x03\x02\x01\x04\x02\x01\x05",
1050 	  "alloc 3" }
1051     };
1052 
1053     int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
1054     TESTAlloc c1, c2, c3;
1055     heim_any any3;
1056 
1057     memset(&c1, 0, sizeof(c1));
1058     c1.tagless = ecalloc(1, sizeof(*c1.tagless));
1059     c1.tagless->ai = 1;
1060     c1.three = 3;
1061     tests[0].val = &c1;
1062 
1063     memset(&c2, 0, sizeof(c2));
1064     c2.tagless = NULL;
1065     c2.three = 3;
1066     tests[1].val = &c2;
1067 
1068     memset(&c3, 0, sizeof(c3));
1069     c3.tagless = NULL;
1070     c3.three = 4;
1071     c3.tagless2 = &any3;
1072     any3.data = "\x02\x01\x05";
1073     any3.length = 3;
1074     tests[2].val = &c3;
1075 
1076     ret += generic_test (tests, ntests, sizeof(TESTAlloc),
1077 			 (generic_encode)encode_TESTAlloc,
1078 			 (generic_length)length_TESTAlloc,
1079 			 (generic_decode)decode_TESTAlloc,
1080 			 (generic_free)free_TESTAlloc,
1081 			 cmp_TESTAlloc,
1082 			 (generic_copy)copy_TESTAlloc);
1083 
1084     free(c1.tagless);
1085 
1086     return ret;
1087 }
1088 
1089 static int
1090 cmp_TESTOptional (void *a, void *b)
1091 {
1092     TESTOptional *aa = a;
1093     TESTOptional *ab = b;
1094 
1095     IF_OPT_COMPARE(aa,ab,zero) {
1096 	COMPARE_OPT_INTEGER(aa,ab,zero);
1097     }
1098     IF_OPT_COMPARE(aa,ab,one) {
1099 	COMPARE_OPT_INTEGER(aa,ab,one);
1100     }
1101     return 0;
1102 }
1103 
1104 /*
1105 UNIV CONS Sequence 5
1106   CONTEXT CONS 0 3
1107     UNIV PRIM Integer 1 00
1108 
1109 UNIV CONS Sequence 5
1110   CONTEXT CONS 1 3
1111     UNIV PRIM Integer 1 03
1112 
1113 UNIV CONS Sequence 10
1114   CONTEXT CONS 0 3
1115     UNIV PRIM Integer 1 00
1116   CONTEXT CONS 1 3
1117     UNIV PRIM Integer 1 01
1118 
1119 */
1120 
1121 static int
1122 test_optional (void)
1123 {
1124     struct test_case tests[] = {
1125 	{ NULL,  2,
1126 	  "\x30\x00",
1127 	  "optional 0" },
1128 	{ NULL,  7,
1129 	  "\x30\x05\xa0\x03\x02\x01\x00",
1130 	  "optional 1" },
1131 	{ NULL,  7,
1132 	  "\x30\x05\xa1\x03\x02\x01\x01",
1133 	  "optional 2" },
1134 	{ NULL,  12,
1135 	  "\x30\x0a\xa0\x03\x02\x01\x00\xa1\x03\x02\x01\x01",
1136 	  "optional 3" }
1137     };
1138 
1139     int ret = 0, ntests = sizeof(tests) / sizeof(*tests);
1140     TESTOptional c0, c1, c2, c3;
1141     int zero = 0;
1142     int one = 1;
1143 
1144     c0.zero = NULL;
1145     c0.one = NULL;
1146     tests[0].val = &c0;
1147 
1148     c1.zero = &zero;
1149     c1.one = NULL;
1150     tests[1].val = &c1;
1151 
1152     c2.zero = NULL;
1153     c2.one = &one;
1154     tests[2].val = &c2;
1155 
1156     c3.zero = &zero;
1157     c3.one = &one;
1158     tests[3].val = &c3;
1159 
1160     ret += generic_test (tests, ntests, sizeof(TESTOptional),
1161 			 (generic_encode)encode_TESTOptional,
1162 			 (generic_length)length_TESTOptional,
1163 			 (generic_decode)decode_TESTOptional,
1164 			 (generic_free)free_TESTOptional,
1165 			 cmp_TESTOptional,
1166 			 (generic_copy)copy_TESTOptional);
1167 
1168     return ret;
1169 }
1170 
1171 static int
1172 check_fail_largetag(void)
1173 {
1174     struct test_case tests[] = {
1175 	{NULL, 14, "\x30\x0c\xbf\x87\xff\xff\xff\xff\xff\x7f\x03\x02\x01\x01",
1176 	 "tag overflow"},
1177 	{NULL, 0, "", "empty buffer"},
1178 	{NULL, 7, "\x30\x05\xa1\x03\x02\x02\x01",
1179 	 "one too short" },
1180 	{NULL, 7, "\x30\x04\xa1\x03\x02\x02\x01"
1181 	 "two too short" },
1182 	{NULL, 7, "\x30\x03\xa1\x03\x02\x02\x01",
1183 	 "three too short" },
1184 	{NULL, 7, "\x30\x02\xa1\x03\x02\x02\x01",
1185 	 "four too short" },
1186 	{NULL, 7, "\x30\x01\xa1\x03\x02\x02\x01",
1187 	 "five too short" },
1188 	{NULL, 7, "\x30\x00\xa1\x03\x02\x02\x01",
1189 	 "six too short" },
1190 	{NULL, 7, "\x30\x05\xa1\x04\x02\x02\x01",
1191 	 "inner one too long" },
1192 	{NULL, 7, "\x30\x00\xa1\x02\x02\x02\x01",
1193 	 "inner one too short" },
1194 	{NULL, 8, "\x30\x05\xbf\x7f\x03\x02\x02\x01",
1195 	 "inner one too short"},
1196 	{NULL, 8, "\x30\x06\xbf\x64\x03\x02\x01\x01",
1197 	 "wrong tag"},
1198 	{NULL, 10, "\x30\x08\xbf\x9a\x9b\x38\x03\x02\x01\x01",
1199 	 "still wrong tag"}
1200     };
1201     int ntests = sizeof(tests) / sizeof(*tests);
1202 
1203     return generic_decode_fail(tests, ntests, sizeof(TESTLargeTag),
1204 			       (generic_decode)decode_TESTLargeTag);
1205 }
1206 
1207 
1208 static int
1209 check_fail_sequence(void)
1210 {
1211     struct test_case tests[] = {
1212 	{NULL, 0, "", "empty buffer"},
1213 	{NULL, 24,
1214 	 "\x30\x16\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01\x01"
1215 	 "\x02\x01\x01\xa2\x03\x02\x01\x01"
1216 	 "missing one byte from the end, internal length ok"},
1217 	{NULL, 25,
1218 	 "\x30\x18\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01\x01"
1219 	 "\x02\x01\x01\xa2\x03\x02\x01\x01",
1220 	 "inner length one byte too long"},
1221 	{NULL, 24,
1222 	 "\x30\x17\xa0\x03\x02\x01\x01\xa1\x08\x30\x06\xbf\x7f\x03\x02\x01"
1223 	 "\x01\x02\x01\x01\xa2\x03\x02\x01\x01",
1224 	 "correct buffer but missing one too short"}
1225     };
1226     int ntests = sizeof(tests) / sizeof(*tests);
1227 
1228     return generic_decode_fail(tests, ntests, sizeof(TESTSeq),
1229 			       (generic_decode)decode_TESTSeq);
1230 }
1231 
1232 static int
1233 check_fail_choice(void)
1234 {
1235     struct test_case tests[] = {
1236 	{NULL, 6,
1237 	 "\xa1\x02\x02\x01\x01",
1238 	 "choice one too short"},
1239 	{NULL, 6,
1240 	 "\xa1\x03\x02\x02\x01",
1241 	 "choice one too short inner"}
1242     };
1243     int ntests = sizeof(tests) / sizeof(*tests);
1244 
1245     return generic_decode_fail(tests, ntests, sizeof(TESTChoice1),
1246 			       (generic_decode)decode_TESTChoice1);
1247 }
1248 
1249 static int
1250 check_seq(void)
1251 {
1252     TESTSeqOf seq;
1253     TESTInteger i;
1254     int ret;
1255 
1256     seq.val = NULL;
1257     seq.len = 0;
1258 
1259     ret = add_TESTSeqOf(&seq, &i);
1260     if (ret) { printf("failed adding\n"); goto out; }
1261     ret = add_TESTSeqOf(&seq, &i);
1262     if (ret) { printf("failed adding\n"); goto out; }
1263     ret = add_TESTSeqOf(&seq, &i);
1264     if (ret) { printf("failed adding\n"); goto out; }
1265     ret = add_TESTSeqOf(&seq, &i);
1266     if (ret) { printf("failed adding\n"); goto out; }
1267 
1268     ret = remove_TESTSeqOf(&seq, seq.len - 1);
1269     if (ret) { printf("failed removing\n"); goto out; }
1270     ret = remove_TESTSeqOf(&seq, 2);
1271     if (ret) { printf("failed removing\n"); goto out; }
1272     ret = remove_TESTSeqOf(&seq, 0);
1273     if (ret) { printf("failed removing\n"); goto out; }
1274     ret = remove_TESTSeqOf(&seq, 0);
1275     if (ret) { printf("failed removing\n"); goto out; }
1276     ret = remove_TESTSeqOf(&seq, 0);
1277     if (ret == 0) {
1278 	printf("can remove from empty list");
1279 	return 1;
1280     }
1281 
1282     if (seq.len != 0) {
1283 	printf("seq not empty!");
1284 	return 1;
1285     }
1286     free_TESTSeqOf(&seq);
1287     ret = 0;
1288 
1289 out:
1290 
1291     return ret;
1292 }
1293 
1294 #define test_seq_of(type, ok, ptr)					\
1295 {									\
1296     heim_octet_string os;						\
1297     size_t size;							\
1298     type decode;							\
1299     ASN1_MALLOC_ENCODE(type, os.data, os.length, ptr, &size, ret);	\
1300     if (ret)								\
1301 	return ret;							\
1302     if (os.length != size)						\
1303 	abort();							\
1304     ret = decode_##type(os.data, os.length, &decode, &size);		\
1305     free(os.data);							\
1306     if (ret) {								\
1307 	if (ok)								\
1308 	    return 1;							\
1309     } else {								\
1310 	free_##type(&decode);						\
1311 	if (!ok)							\
1312 	    return 1;							\
1313 	if (size != 0)							\
1314             return 1;							\
1315     }									\
1316     return 0;								\
1317 }
1318 
1319 static int
1320 check_seq_of_size(void)
1321 {
1322 #if 0 /* template */
1323     TESTInteger integers[4] = { 1, 2, 3, 4 };
1324     int ret;
1325 
1326     {
1327 	TESTSeqSizeOf1 ssof1f1 = { 1, integers };
1328 	TESTSeqSizeOf1 ssof1ok1 = { 2, integers };
1329 	TESTSeqSizeOf1 ssof1f2 = { 3, integers };
1330 
1331 	test_seq_of(TESTSeqSizeOf1, 0, &ssof1f1);
1332 	test_seq_of(TESTSeqSizeOf1, 1, &ssof1ok1);
1333 	test_seq_of(TESTSeqSizeOf1, 0, &ssof1f2);
1334     }
1335     {
1336 	TESTSeqSizeOf2 ssof2f1 = { 0, NULL };
1337 	TESTSeqSizeOf2 ssof2ok1 = { 1, integers };
1338 	TESTSeqSizeOf2 ssof2ok2 = { 2, integers };
1339 	TESTSeqSizeOf2 ssof2f2 = { 3, integers };
1340 
1341 	test_seq_of(TESTSeqSizeOf2, 0, &ssof2f1);
1342 	test_seq_of(TESTSeqSizeOf2, 1, &ssof2ok1);
1343 	test_seq_of(TESTSeqSizeOf2, 1, &ssof2ok2);
1344 	test_seq_of(TESTSeqSizeOf2, 0, &ssof2f2);
1345     }
1346     {
1347 	TESTSeqSizeOf3 ssof3f1 = { 0, NULL };
1348 	TESTSeqSizeOf3 ssof3ok1 = { 1, integers };
1349 	TESTSeqSizeOf3 ssof3ok2 = { 2, integers };
1350 
1351 	test_seq_of(TESTSeqSizeOf3, 0, &ssof3f1);
1352 	test_seq_of(TESTSeqSizeOf3, 1, &ssof3ok1);
1353 	test_seq_of(TESTSeqSizeOf3, 1, &ssof3ok2);
1354     }
1355     {
1356 	TESTSeqSizeOf4 ssof4ok1 = { 0, NULL };
1357 	TESTSeqSizeOf4 ssof4ok2 = { 1, integers };
1358 	TESTSeqSizeOf4 ssof4ok3 = { 2, integers };
1359 	TESTSeqSizeOf4 ssof4f1  = { 3, integers };
1360 
1361 	test_seq_of(TESTSeqSizeOf4, 1, &ssof4ok1);
1362 	test_seq_of(TESTSeqSizeOf4, 1, &ssof4ok2);
1363 	test_seq_of(TESTSeqSizeOf4, 1, &ssof4ok3);
1364 	test_seq_of(TESTSeqSizeOf4, 0, &ssof4f1);
1365    }
1366 #endif
1367     return 0;
1368 }
1369 
1370 static int
1371 check_TESTMechTypeList(void)
1372 {
1373     TESTMechTypeList tl;
1374     unsigned oid1[] =  { 1, 2, 840, 48018, 1, 2, 2};
1375     unsigned oid2[] =  { 1, 2, 840, 113554, 1, 2, 2};
1376     unsigned oid3[] =   { 1, 3, 6, 1, 4, 1, 311, 2, 2, 30};
1377     unsigned oid4[] =   { 1, 3, 6, 1, 4, 1, 311, 2, 2, 10};
1378     TESTMechType array[] = {{ 7, oid1 },
1379                             { 7, oid2 },
1380                             { 10, oid3 },
1381                             { 10, oid4 }};
1382     size_t size, len;
1383     void *ptr;
1384     int ret;
1385 
1386     tl.len = 4;
1387     tl.val = array;
1388 
1389     ASN1_MALLOC_ENCODE(TESTMechTypeList, ptr, len, &tl, &size, ret);
1390     if (ret)
1391 	errx(1, "TESTMechTypeList: %d", ret);
1392     if (len != size)
1393 	abort();
1394     return 0;
1395 }
1396 
1397 int
1398 main(int argc, char **argv)
1399 {
1400     int ret = 0;
1401 
1402     ret += test_principal ();
1403     ret += test_authenticator();
1404     ret += test_krb_error();
1405     ret += test_Name();
1406     ret += test_bit_string();
1407     ret += test_bit_string_rfc1510();
1408     ret += test_time();
1409     ret += test_cert();
1410 
1411     ret += check_tag_length();
1412     ret += check_tag_length64();
1413     ret += check_tag_length64s();
1414     ret += test_large_tag();
1415     ret += test_choice();
1416 
1417     ret += test_implicit();
1418     ret += test_taglessalloc();
1419     ret += test_optional();
1420 
1421     ret += check_fail_largetag();
1422     ret += check_fail_sequence();
1423     ret += check_fail_choice();
1424 
1425     ret += check_seq();
1426     ret += check_seq_of_size();
1427 
1428     ret += check_TESTMechTypeList();
1429 
1430     return ret;
1431 }
1432