Waste less stack.
[shishi.git] / tests / authenticator.c
blob1b0a6caac635a6fdeca8415a1e2fc769be907344
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, *q;
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 () => `%X'.\n", a);
100 if (a)
101 success ("shishi_authenticator() OK\n");
102 else
103 fail ("shishi_authenticator() failed\n");
105 /* shishi_authenticator_set_crealm() */
106 res = shishi_authenticator_set_crealm (handle, a, "foo");
107 if (res == SHISHI_OK)
108 success ("shishi_authenticator_set_crealm() OK\n");
109 else
110 fail ("shishi_authenticator_set_crealm() failed\n");
112 /* shishi_authenticator_client_set() */
113 res = shishi_authenticator_client_set (handle, a, "foo/bar/baz");
114 if (res == SHISHI_OK)
115 success ("shishi_authenticator_client_set() OK\n");
116 else
117 fail ("shishi_authenticator_client_set() failed\n");
119 /* shishi_authenticator_cname_get() */
120 n = sizeof (buffer);
121 res = shishi_authenticator_cname_get (handle, a, buffer, &n);
122 if (debug)
123 escapeprint (buffer, n);
124 if (res == SHISHI_OK &&
125 n == strlen ("foo/bar/baz") && memcmp (buffer, "foo/bar/baz", n) == 0)
126 success ("shishi_authenticator_cname_get() OK\n");
127 else
128 fail ("shishi_authenticator_cname_get() failed\n");
130 /* shishi_authenticator_client_set() */
131 res = shishi_authenticator_client_set (handle, a, "foo");
132 if (res == SHISHI_OK)
133 success ("shishi_authenticator_client_set() OK\n");
134 else
135 fail ("shishi_authenticator_client_set() failed\n");
137 /* shishi_authenticator_cname_get() */
138 n = sizeof (buffer);
139 res = shishi_authenticator_cname_get (handle, a, buffer, &n);
140 if (debug)
141 escapeprint (buffer, n);
142 if (res == SHISHI_OK && n == strlen ("foo")
143 && memcmp (buffer, "foo", n) == 0)
144 success ("shishi_authenticator_cname_get() OK\n");
145 else
146 fail ("shishi_authenticator_cname_get() failed\n");
148 /* shishi_authenticator_set_crealm() */
149 res = shishi_authenticator_set_crealm (handle, a, "bar");
150 if (res == SHISHI_OK)
151 success ("shishi_authenticator_set_crealm() OK\n");
152 else
153 fail ("shishi_authenticator_set_crealm() failed\n");
155 /* shishi_authenticator_cnamerealm_get() */
156 n = sizeof (buffer);
157 res = shishi_authenticator_cnamerealm_get (handle, a, buffer, &n);
158 if (debug)
159 escapeprint (buffer, n);
160 if (res == SHISHI_OK &&
161 n == strlen ("foo@bar") && memcmp (buffer, "foo@bar", n) == 0)
162 success ("shishi_authenticator_cnamerealm_get() OK\n");
163 else
164 fail ("shishi_authenticator_cnamerealm_get() failed\n");
166 /* shishi_authenticator_add_authorizationdata() */
167 res = shishi_authenticator_add_authorizationdata (handle, a, 42, "baz", 3);
168 if (res == SHISHI_OK)
169 success ("shishi_authenticator_add_authorizationdata() OK\n");
170 else
171 fail ("shishi_authenticator_add_authorizationdata() failed\n");
173 /* shishi_authenticator_authorizationdata() */
174 m = sizeof (buffer);
175 res = shishi_authenticator_authorizationdata (handle, a, &n, buffer, &m, 1);
176 if (debug)
177 escapeprint (buffer, m);
178 if (res == SHISHI_OK && n == 42 && m == 3 && memcmp (buffer, "baz", 3) == 0)
179 success ("shishi_authenticator_authorizationdata() OK\n");
180 else
181 fail ("shishi_authenticator_authorizationdata() failed\n");
183 /* shishi_authenticator_authorizationdata() */
184 m = sizeof (buffer);
185 res = shishi_authenticator_authorizationdata (handle, a, &n, buffer, &m, 2);
186 if (res == SHISHI_OUT_OF_RANGE)
187 success ("shishi_authenticator_authorizationdata() OK\n");
188 else
189 fail ("shishi_authenticator_authorizationdata() failed\n");
191 /* shishi_authenticator_remove_cksum() */
192 res = shishi_authenticator_remove_cksum (handle, a);
193 if (res == SHISHI_OK)
194 success ("shishi_authenticator_remove_cksum() OK\n");
195 else
196 fail ("shishi_authenticator_remove_cksum() failed\n");
198 /* shishi_a2d() */
199 n = sizeof (buffer);
200 res = shishi_a2d (handle, a, buffer, &n);
201 if (res == SHISHI_OK)
202 success ("shishi_a2d() OK\n");
203 else
204 n = 0, fail ("shishi_a2d() failed\n");
206 /* shishi_authenticator_to_file() */
207 res = shishi_authenticator_to_file (handle, a, SHISHI_FILETYPE_TEXT,
208 "authenticator.tmp");
209 if (res == SHISHI_OK)
210 success ("shishi_authenticator_to_file() OK\n");
211 else
212 fail ("shishi_authenticator_to_file() failed\n");
214 /* shishi_asn1_done() */
215 res = shishi_asn1_done (handle, a);
216 if (res == SHISHI_OK)
217 success ("shishi_asn1_done() OK\n");
218 else
219 fail ("shishi_asn1_done() failed\n");
221 a = NULL;
223 /* shishi_authenticator_from_file() */
224 res = shishi_authenticator_from_file (handle, &a, SHISHI_FILETYPE_TEXT,
225 "authenticator.tmp");
226 if (res == SHISHI_OK)
227 success ("shishi_authenticator_from_file() OK\n");
228 else
229 fail ("shishi_authenticator_from_file() failed\n");
231 if (debug)
233 /* shishi_authenticator_print() */
234 res = shishi_authenticator_print (handle, stdout, a);
235 if (res == SHISHI_OK)
236 success ("shishi_authenticator_print() OK\n");
237 else
238 fail ("shishi_authenticator_print() failed\n");
241 /* shishi_a2d() */
242 m = sizeof (buffer2);
243 res = shishi_a2d (handle, a, buffer2, &m);
244 if (res == SHISHI_OK)
245 success ("shishi_a2d() OK\n");
246 else
247 n = 0, fail ("shishi_a2d() failed\n");
249 /* Compare DER encodings of authenticators */
250 if (n > 0 && m > 0 && n == m && memcmp (buffer, buffer2, n) == 0)
251 success ("DER comparison OK\n");
252 else
253 fail ("DER comparison failed\n");
255 /* shishi_authenticator_cusec_set() */
256 res = shishi_authenticator_cusec_set (handle, a, 4711);
257 if (res == SHISHI_OK)
258 success ("shishi_authenticator_cusec_set() OK\n");
259 else
260 fail ("shishi_authenticator_cusec_set() failed\n");
262 /* shishi_authenticator_cusec_get() */
263 res = shishi_authenticator_cusec_get (handle, a, &n);
264 if (debug)
265 printf ("shishi_authenticator_cusec_get () => `%d'.\n", n);
266 if (res == SHISHI_OK && n == 4711)
267 success ("shishi_authenticator_cusec_get() OK\n");
268 else
269 fail ("shishi_authenticator_cusec_get() failed\n");
271 /* shishi_authenticator_ctime_set() */
272 res = shishi_authenticator_ctime_set (handle, a, "19700101011831Z");
273 if (res == SHISHI_OK)
274 success ("shishi_authenticator_ctime_set() OK\n");
275 else
276 fail ("shishi_authenticator_ctime_set() failed\n");
278 /* shishi_authenticator_ctime_get() */
279 res = shishi_authenticator_ctime_get (handle, a, buffer);
280 if (debug)
281 escapeprint (buffer, 15);
282 if (res == SHISHI_OK && memcmp (buffer, "19700101011831Z", 15) == 0)
283 success ("shishi_authenticator_ctime_get() OK\n");
284 else
285 fail ("shishi_authenticator_ctime_get() failed\n");
287 /* shishi_a2d() */
288 n = sizeof (buffer);
289 res = shishi_a2d (handle, a, buffer, &n);
290 if (res == SHISHI_OK)
291 success ("shishi_a2d() OK\n");
292 else
293 n = 0, fail ("shishi_a2d() failed\n");
294 if (debug)
296 shishi_authenticator_print (handle, stdout, a);
297 hexprint (buffer, n);
298 puts ("");
299 hexprint (authenticator, sizeof (authenticator));
300 puts ("");
302 if (n == sizeof (authenticator) &&
303 n == AUTHENTICATOR_LEN && memcmp (authenticator, buffer, n) == 0)
304 success ("DER comparison OK\n");
305 else
306 fail ("DER comparison failed\n");
308 /* shishi_authenticator_clear_authorizationdata() */
309 res = shishi_authenticator_clear_authorizationdata (handle, a);
310 if (res == SHISHI_OK)
311 success ("shishi_authenticator_clear_authorizationdata() OK\n");
312 else
313 fail ("shishi_authenticator_clear_authorizationdata() failed\n");
315 /* shishi_a2d() */
316 n = sizeof (buffer);
317 res = shishi_a2d (handle, a, buffer, &n);
318 if (res == SHISHI_OK)
319 success ("shishi_a2d() OK\n");
320 else
321 n = 0, fail ("shishi_a2d() failed\n");
322 if (debug)
324 shishi_authenticator_print (handle, stdout, a);
325 hexprint (buffer, n);
326 puts ("");
327 hexprint (authenticator2, sizeof (authenticator2));
328 puts ("");
330 if (n == sizeof (authenticator2) &&
331 n == AUTHENTICATOR2_LEN && memcmp (authenticator2, buffer, n) == 0)
332 success ("DER comparison OK\n");
333 else
334 fail ("DER comparison failed\n");
336 /* unlink() */
337 res = unlink ("authenticator.tmp");
338 if (res == 0)
339 success ("unlink() OK\n");
340 else
341 fail ("unlink() failed\n");
343 shishi_done (handle);
345 if (verbose)
346 printf ("Authenticator self tests done with %d errors\n", error_count);
348 return error_count ? 1 : 0;