Fix.
[shishi.git] / tests / authenticator.c
blobf68e47eec126d05d6b0f073e6f079b0a1081cd6f
1 /* authenticator.c Shishi authenticator self tests.
2 * Copyright (C) 2002, 2003 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "utils.c"
23 #include <shishi.h>
25 static const char authenticator[] = {
26 '\x62', '\x4b', '\x30', '\x49', '\xa0', '\x03', '\x02', '\x01',
27 '\x05', '\xa1', '\x05', '\x1b', '\x03', '\x62', '\x61', '\x72',
28 '\xa2', '\x10', '\x30', '\x0e', '\xa0', '\x03', '\x02', '\x01',
29 '\x00', '\xa1', '\x07', '\x30', '\x05', '\x1b', '\x03', '\x66',
30 '\x6f', '\x6f', '\xa4', '\x04', '\x02', '\x02', '\x12', '\x67',
31 '\xa5', '\x11', '\x18', '\x0f', '\x31', '\x39', '\x37', '\x30',
32 '\x30', '\x31', '\x30', '\x31', '\x30', '\x31', '\x31', '\x38',
33 '\x33', '\x31', '\x5a', '\xa8', '\x10', '\x30', '\x0e', '\x30',
34 '\x0c', '\xa0', '\x03', '\x02', '\x01', '\x2a', '\xa1', '\x05',
35 '\x04', '\x03', '\x62', '\x61', '\x7a'
38 static const char authenticator2[] = {
39 '\x62', '\x39', '\x30', '\x37', '\xa0', '\x03', '\x02', '\x01',
40 '\x05', '\xa1', '\x05', '\x1b', '\x03', '\x62', '\x61', '\x72',
41 '\xa2', '\x10', '\x30', '\x0e', '\xa0', '\x03', '\x02', '\x01',
42 '\x00', '\xa1', '\x07', '\x30', '\x05', '\x1b', '\x03', '\x66',
43 '\x6f', '\x6f', '\xa4', '\x04', '\x02', '\x02', '\x12', '\x67',
44 '\xa5', '\x11', '\x18', '\x0f', '\x31', '\x39', '\x37', '\x30',
45 '\x30', '\x31', '\x30', '\x31', '\x30', '\x31', '\x31', '\x38',
46 '\x33', '\x31', '\x5a'
49 #define AUTHENTICATOR_LEN 77
50 #define AUTHENTICATOR2_LEN 59
52 int
53 main (int argc, char *argv[])
55 Shishi *handle;
56 Shishi_asn1 a;
57 char buffer[BUFSIZ];
58 char buffer2[BUFSIZ];
59 char *p;
60 int n, m, res;
63 if (strcmp (argv[argc - 1], "-v") == 0 ||
64 strcmp (argv[argc - 1], "--verbose") == 0)
65 verbose = 1;
66 else if (strcmp (argv[argc - 1], "-d") == 0 ||
67 strcmp (argv[argc - 1], "--debug") == 0)
68 debug = 1;
69 else if (strcmp (argv[argc - 1], "-b") == 0 ||
70 strcmp (argv[argc - 1], "--break-on-error") == 0)
71 break_on_error = 1;
72 else if (strcmp (argv[argc - 1], "-h") == 0 ||
73 strcmp (argv[argc - 1], "-?") == 0 ||
74 strcmp (argv[argc - 1], "--help") == 0)
76 printf ("Usage: %s [-vdbh?] [--verbose] [--debug] "
77 "[--break-on-error] [--help]\n", argv[0]);
78 return 1;
80 while (argc-- > 1);
82 handle = shishi ();
83 if (handle == NULL)
85 fail ("Could not initialize shishi\n");
86 return 1;
89 if (debug)
90 shishi_cfg (handle, strdup ("verbose"));
92 escapeprint (NULL, 0);
93 hexprint (NULL, 0);
94 binprint (NULL, 0);
96 /* shishi_authenticator() */
97 a = shishi_authenticator (handle);
98 if (debug)
99 printf ("shishi_authenticator () => `%p'.\n", a);
100 if (a)
101 success ("shishi_authenticator() OK\n");
102 else
103 fail ("shishi_authenticator() failed\n");
105 res = shishi_authenticator_remove_subkey (handle, a);
106 if (res == SHISHI_OK)
107 success ("shishi_authenticator_remove_subkey() OK\n");
108 else
109 fail ("shishi_authenticator_remove_subkey() failed\n");
111 /* shishi_authenticator_set_crealm() */
112 res = shishi_authenticator_set_crealm (handle, a, "foo");
113 if (res == SHISHI_OK)
114 success ("shishi_authenticator_set_crealm() OK\n");
115 else
116 fail ("shishi_authenticator_set_crealm() failed\n");
118 /* shishi_authenticator_client_set() */
119 res = shishi_authenticator_client_set (handle, a, "foo/bar/baz");
120 if (res == SHISHI_OK)
121 success ("shishi_authenticator_client_set() OK\n");
122 else
123 fail ("shishi_authenticator_client_set() failed\n");
125 /* shishi_authenticator_cname_get() */
126 n = sizeof (buffer);
127 res = shishi_authenticator_cname_get (handle, a, buffer, &n);
128 if (debug)
129 escapeprint (buffer, n);
130 if (res == SHISHI_OK &&
131 n == strlen ("foo/bar/baz") && memcmp (buffer, "foo/bar/baz", n) == 0)
132 success ("shishi_authenticator_cname_get() OK\n");
133 else
134 fail ("shishi_authenticator_cname_get() failed\n");
136 /* shishi_authenticator_client_set() */
137 res = shishi_authenticator_client_set (handle, a, "foo");
138 if (res == SHISHI_OK)
139 success ("shishi_authenticator_client_set() OK\n");
140 else
141 fail ("shishi_authenticator_client_set() failed\n");
143 /* shishi_authenticator_cname_get() */
144 n = sizeof (buffer);
145 res = shishi_authenticator_cname_get (handle, a, buffer, &n);
146 if (debug)
147 escapeprint (buffer, n);
148 if (res == SHISHI_OK && n == strlen ("foo")
149 && memcmp (buffer, "foo", n) == 0)
150 success ("shishi_authenticator_cname_get() OK\n");
151 else
152 fail ("shishi_authenticator_cname_get() failed\n");
154 /* shishi_authenticator_set_crealm() */
155 res = shishi_authenticator_set_crealm (handle, a, "bar");
156 if (res == SHISHI_OK)
157 success ("shishi_authenticator_set_crealm() OK\n");
158 else
159 fail ("shishi_authenticator_set_crealm() failed\n");
161 /* shishi_authenticator_cnamerealm_get() */
162 n = sizeof (buffer);
163 res = shishi_authenticator_cnamerealm_get (handle, a, buffer, &n);
164 if (debug)
165 escapeprint (buffer, n);
166 if (res == SHISHI_OK &&
167 n == strlen ("foo@bar") && memcmp (buffer, "foo@bar", n) == 0)
168 success ("shishi_authenticator_cnamerealm_get() OK\n");
169 else
170 fail ("shishi_authenticator_cnamerealm_get() failed\n");
172 /* shishi_authenticator_add_authorizationdata() */
173 res = shishi_authenticator_add_authorizationdata (handle, a, 42, "baz", 3);
174 if (res == SHISHI_OK)
175 success ("shishi_authenticator_add_authorizationdata() OK\n");
176 else
177 fail ("shishi_authenticator_add_authorizationdata() failed\n");
179 /* shishi_authenticator_authorizationdata() */
180 m = sizeof (buffer);
181 res = shishi_authenticator_authorizationdata (handle, a, &n, buffer, &m, 1);
182 if (debug)
183 escapeprint (buffer, m);
184 if (res == SHISHI_OK && n == 42 && m == 3 && memcmp (buffer, "baz", 3) == 0)
185 success ("shishi_authenticator_authorizationdata() OK\n");
186 else
187 fail ("shishi_authenticator_authorizationdata() failed\n");
189 /* shishi_authenticator_authorizationdata() */
190 m = sizeof (buffer);
191 res = shishi_authenticator_authorizationdata (handle, a, &n, buffer, &m, 2);
192 if (res == SHISHI_OUT_OF_RANGE)
193 success ("shishi_authenticator_authorizationdata() OK\n");
194 else
195 fail ("shishi_authenticator_authorizationdata() failed\n");
197 /* shishi_authenticator_remove_cksum() */
198 res = shishi_authenticator_remove_cksum (handle, a);
199 if (res == SHISHI_OK)
200 success ("shishi_authenticator_remove_cksum() OK\n");
201 else
202 fail ("shishi_authenticator_remove_cksum() failed\n");
204 /* shishi_a2d() */
205 n = sizeof (buffer);
206 res = shishi_a2d (handle, a, buffer, &n);
207 if (res == SHISHI_OK)
208 success ("shishi_a2d() OK\n");
209 else
210 n = 0, fail ("shishi_a2d() failed\n");
212 /* shishi_authenticator_to_file() */
213 res = shishi_authenticator_to_file (handle, a, SHISHI_FILETYPE_TEXT,
214 "authenticator.tmp");
215 if (res == SHISHI_OK)
216 success ("shishi_authenticator_to_file() OK\n");
217 else
218 fail ("shishi_authenticator_to_file() failed\n");
220 /* shishi_asn1_done() */
221 shishi_asn1_done (handle, a);
222 success ("shishi_asn1_done() OK\n");
224 a = NULL;
226 /* shishi_authenticator_from_file() */
227 res = shishi_authenticator_from_file (handle, &a, SHISHI_FILETYPE_TEXT,
228 "authenticator.tmp");
229 if (res == SHISHI_OK)
230 success ("shishi_authenticator_from_file() OK\n");
231 else
232 fail ("shishi_authenticator_from_file() failed\n");
234 if (debug)
236 /* shishi_authenticator_print() */
237 res = shishi_authenticator_print (handle, stdout, a);
238 if (res == SHISHI_OK)
239 success ("shishi_authenticator_print() OK\n");
240 else
241 fail ("shishi_authenticator_print() failed\n");
244 /* shishi_a2d() */
245 m = sizeof (buffer2);
246 res = shishi_a2d (handle, a, buffer2, &m);
247 if (res == SHISHI_OK)
248 success ("shishi_a2d() OK\n");
249 else
250 n = 0, fail ("shishi_a2d() failed\n");
252 /* Compare DER encodings of authenticators */
253 if (n > 0 && m > 0 && n == m && memcmp (buffer, buffer2, n) == 0)
254 success ("DER comparison OK\n");
255 else
256 fail ("DER comparison failed\n");
258 /* shishi_authenticator_cusec_set() */
259 res = shishi_authenticator_cusec_set (handle, a, 4711);
260 if (res == SHISHI_OK)
261 success ("shishi_authenticator_cusec_set() OK\n");
262 else
263 fail ("shishi_authenticator_cusec_set() failed\n");
265 /* shishi_authenticator_cusec_get() */
266 res = shishi_authenticator_cusec_get (handle, a, &n);
267 if (debug)
268 printf ("shishi_authenticator_cusec_get () => `%d'.\n", n);
269 if (res == SHISHI_OK && n == 4711)
270 success ("shishi_authenticator_cusec_get() OK\n");
271 else
272 fail ("shishi_authenticator_cusec_get() failed\n");
274 /* shishi_authenticator_ctime_set() */
275 res = shishi_authenticator_ctime_set (handle, a, "19700101011831Z");
276 if (res == SHISHI_OK)
277 success ("shishi_authenticator_ctime_set() OK\n");
278 else
279 fail ("shishi_authenticator_ctime_set() failed\n");
281 /* shishi_authenticator_ctime() */
282 res = shishi_authenticator_ctime (handle, a, &p);
283 if (debug)
284 escapeprint (p, strlen (p));
285 if (res == SHISHI_OK && memcmp (p, "19700101011831Z", 15) == 0)
286 success ("shishi_authenticator_ctime() OK\n");
287 else
288 fail ("shishi_authenticator_ctime() failed\n");
290 /* shishi_a2d() */
291 n = sizeof (buffer);
292 res = shishi_a2d (handle, a, buffer, &n);
293 if (res == SHISHI_OK)
294 success ("shishi_a2d() OK\n");
295 else
296 n = 0, fail ("shishi_a2d() failed\n");
297 if (debug)
299 shishi_authenticator_print (handle, stdout, a);
300 hexprint (buffer, n);
301 puts ("");
302 hexprint (authenticator, sizeof (authenticator));
303 puts ("");
305 if (n == sizeof (authenticator) &&
306 n == AUTHENTICATOR_LEN && memcmp (authenticator, buffer, n) == 0)
307 success ("DER comparison OK\n");
308 else
309 fail ("DER comparison failed\n");
311 /* shishi_authenticator_clear_authorizationdata() */
312 res = shishi_authenticator_clear_authorizationdata (handle, a);
313 if (res == SHISHI_OK)
314 success ("shishi_authenticator_clear_authorizationdata() OK\n");
315 else
316 fail ("shishi_authenticator_clear_authorizationdata() failed\n");
318 /* shishi_a2d() */
319 n = sizeof (buffer);
320 res = shishi_a2d (handle, a, buffer, &n);
321 if (res == SHISHI_OK)
322 success ("shishi_a2d() OK\n");
323 else
324 n = 0, fail ("shishi_a2d() failed\n");
325 if (debug)
327 shishi_authenticator_print (handle, stdout, a);
328 hexprint (buffer, n);
329 puts ("");
330 hexprint (authenticator2, sizeof (authenticator2));
331 puts ("");
333 if (n == sizeof (authenticator2) &&
334 n == AUTHENTICATOR2_LEN && memcmp (authenticator2, buffer, n) == 0)
335 success ("DER comparison OK\n");
336 else
337 fail ("DER comparison failed\n");
339 /* unlink() */
340 res = unlink ("authenticator.tmp");
341 if (res == 0)
342 success ("unlink() OK\n");
343 else
344 fail ("unlink() failed\n");
346 shishi_done (handle);
348 if (verbose)
349 printf ("Authenticator self tests done with %d errors\n", error_count);
351 return error_count ? 1 : 0;