xref: /freebsd/crypto/heimdal/lib/asn1/check-gen.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*
2  * Copyright (c) 1999 - 2003 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37 #include <stdio.h>
38 #include <string.h>
39 #include <err.h>
40 #include <roken.h>
41 
42 #include <asn1-common.h>
43 #include <asn1_err.h>
44 #include <der.h>
45 #include <krb5_asn1.h>
46 
47 #include "check-common.h"
48 
49 RCSID("$Id: check-gen.c,v 1.2.2.1 2003/05/06 16:49:57 joda Exp $");
50 
51 static char *lha_princ[] = { "lha" };
52 static char *lharoot_princ[] = { "lha", "root" };
53 static char *datan_princ[] = { "host", "nutcracker.e.kth.se" };
54 
55 
56 #define COMPARE_STRING(ac,bc,e) \
57 	do { if (strcmp((ac)->e, (bc)->e) != 0) return 1; } while(0)
58 #define COMPARE_INTEGER(ac,bc,e) \
59 	do { if ((ac)->e != (bc)->e) return 1; } while(0)
60 #define COMPARE_MEM(ac,bc,e,len) \
61 	do { if (memcmp((ac)->e, (bc)->e,len) != 0) return 1; } while(0)
62 
63 static int
64 cmp_principal (void *a, void *b)
65 {
66     Principal *pa = a;
67     Principal *pb = b;
68     int i;
69 
70     COMPARE_STRING(pa,pb,realm);
71     COMPARE_INTEGER(pa,pb,name.name_type);
72     COMPARE_INTEGER(pa,pb,name.name_string.len);
73 
74     for (i = 0; i < pa->name.name_string.len; i++)
75 	COMPARE_STRING(pa,pb,name.name_string.val[i]);
76 
77     return 0;
78 }
79 
80 static int
81 test_principal (void)
82 {
83 
84     struct test_case tests[] = {
85 	{ NULL, 29,
86 	  (unsigned char*)"\x30\x1b\xa0\x10\x30\x0e\xa0\x03\x02\x01\x01\xa1\x07\x30\x05\x1b"
87 	  "\x03\x6c\x68\x61\xa1\x07\x1b\x05\x53\x55\x2e\x53\x45"
88 	},
89 	{ NULL, 35,
90 	  (unsigned char*)"\x30\x21\xa0\x16\x30\x14\xa0\x03\x02\x01\x01\xa1\x0d\x30\x0b\x1b"
91 	  "\x03\x6c\x68\x61\x1b\x04\x72\x6f\x6f\x74\xa1\x07\x1b\x05\x53\x55"
92 	  "\x2e\x53\x45"
93 	},
94 	{ NULL, 54,
95 	  (unsigned char*)"\x30\x34\xa0\x26\x30\x24\xa0\x03\x02\x01\x03\xa1\x1d\x30\x1b\x1b"
96 	  "\x04\x68\x6f\x73\x74\x1b\x13\x6e\x75\x74\x63\x72\x61\x63\x6b\x65"
97 	  "\x72\x2e\x65\x2e\x6b\x74\x68\x2e\x73\x65\xa1\x0a\x1b\x08\x45\x2e"
98 	  "\x4b\x54\x48\x2e\x53\x45"
99 	}
100     };
101 
102 
103     Principal values[] = {
104 	{ { KRB5_NT_PRINCIPAL, { 1, lha_princ } },  "SU.SE" },
105 	{ { KRB5_NT_PRINCIPAL, { 2, lharoot_princ } },  "SU.SE" },
106 	{ { KRB5_NT_SRV_HST, { 2, datan_princ } },  "E.KTH.SE" }
107     };
108     int i;
109     int ntests = sizeof(tests) / sizeof(*tests);
110 
111     for (i = 0; i < ntests; ++i) {
112 	tests[i].val = &values[i];
113 	asprintf (&tests[i].name, "Principal %d", i);
114     }
115 
116     return generic_test (tests, ntests, sizeof(Principal),
117 			 (generic_encode)encode_Principal,
118 			 (generic_length)length_Principal,
119 			 (generic_decode)decode_Principal,
120 			 cmp_principal);
121 }
122 
123 static int
124 cmp_authenticator (void *a, void *b)
125 {
126     Authenticator *aa = a;
127     Authenticator *ab = b;
128     int i;
129 
130     COMPARE_INTEGER(aa,ab,authenticator_vno);
131     COMPARE_STRING(aa,ab,crealm);
132 
133     COMPARE_INTEGER(aa,ab,cname.name_type);
134     COMPARE_INTEGER(aa,ab,cname.name_string.len);
135 
136     for (i = 0; i < aa->cname.name_string.len; i++)
137 	COMPARE_STRING(aa,ab,cname.name_string.val[i]);
138 
139     return 0;
140 }
141 
142 static int
143 test_authenticator (void)
144 {
145     struct test_case tests[] = {
146 	{ NULL, 63,
147 	  (unsigned char*)"\x62\x3d\x30\x3b\xa0\x03\x02\x01\x05\xa1\x0a\x1b\x08"
148 	  "\x45\x2e\x4b\x54\x48\x2e\x53\x45\xa2\x10\x30\x0e\xa0"
149 	  "\x03\x02\x01\x01\xa1\x07\x30\x05\x1b\x03\x6c\x68\x61"
150 	  "\xa4\x03\x02\x01\x0a\xa5\x11\x18\x0f\x31\x39\x37\x30"
151 	  "\x30\x31\x30\x31\x30\x30\x30\x31\x33\x39\x5a"
152 	},
153 	{ NULL, 67,
154 	  (unsigned char*)"\x62\x41\x30\x3f\xa0\x03\x02\x01\x05\xa1\x07\x1b\x05"
155 	  "\x53\x55\x2e\x53\x45\xa2\x16\x30\x14\xa0\x03\x02\x01"
156 	  "\x01\xa1\x0d\x30\x0b\x1b\x03\x6c\x68\x61\x1b\x04\x72"
157 	  "\x6f\x6f\x74\xa4\x04\x02\x02\x01\x24\xa5\x11\x18\x0f"
158 	  "\x31\x39\x37\x30\x30\x31\x30\x31\x30\x30\x31\x36\x33"
159 	  "\x39\x5a"
160 	}
161     };
162 
163     Authenticator values[] = {
164 	{ 5, "E.KTH.SE", { KRB5_NT_PRINCIPAL, { 1, lha_princ } },
165 	  NULL, 10, 99, NULL, NULL, NULL },
166 	{ 5, "SU.SE", { KRB5_NT_PRINCIPAL, { 2, lharoot_princ } },
167 	  NULL, 292, 999, NULL, NULL, NULL }
168     };
169     int i;
170     int ntests = sizeof(tests) / sizeof(*tests);
171 
172     for (i = 0; i < ntests; ++i) {
173 	tests[i].val = &values[i];
174 	asprintf (&tests[i].name, "Authenticator %d", i);
175     }
176 
177     return generic_test (tests, ntests, sizeof(Authenticator),
178 			 (generic_encode)encode_Authenticator,
179 			 (generic_length)length_Authenticator,
180 			 (generic_decode)decode_Authenticator,
181 			 cmp_authenticator);
182 }
183 
184 int
185 main(int argc, char **argv)
186 {
187     int ret = 0;
188 
189     ret += test_principal ();
190     ret += test_authenticator();
191 
192     return ret;
193 }
194