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
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
53 main (int argc
, char *argv
[])
63 if (strcmp (argv
[argc
- 1], "-v") == 0 ||
64 strcmp (argv
[argc
- 1], "--verbose") == 0)
66 else if (strcmp (argv
[argc
- 1], "-d") == 0 ||
67 strcmp (argv
[argc
- 1], "--debug") == 0)
69 else if (strcmp (argv
[argc
- 1], "-b") == 0 ||
70 strcmp (argv
[argc
- 1], "--break-on-error") == 0)
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]);
85 fail ("Could not initialize shishi\n");
90 shishi_cfg (handle
, strdup ("verbose"));
92 escapeprint (NULL
, 0);
96 /* shishi_authenticator() */
97 a
= shishi_authenticator (handle
);
99 printf ("shishi_authenticator () => `%X'.\n", a
);
101 success ("shishi_authenticator() OK\n");
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");
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");
117 fail ("shishi_authenticator_client_set() failed\n");
119 /* shishi_authenticator_cname_get() */
121 res
= shishi_authenticator_cname_get (handle
, a
, buffer
, &n
);
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");
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");
135 fail ("shishi_authenticator_client_set() failed\n");
137 /* shishi_authenticator_cname_get() */
139 res
= shishi_authenticator_cname_get (handle
, a
, buffer
, &n
);
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");
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");
153 fail ("shishi_authenticator_set_crealm() failed\n");
155 /* shishi_authenticator_cnamerealm_get() */
157 res
= shishi_authenticator_cnamerealm_get (handle
, a
, buffer
, &n
);
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");
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");
171 fail ("shishi_authenticator_add_authorizationdata() failed\n");
173 /* shishi_authenticator_authorizationdata() */
175 res
= shishi_authenticator_authorizationdata (handle
, a
, &n
, buffer
, &m
, 1);
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");
181 fail ("shishi_authenticator_authorizationdata() failed\n");
183 /* shishi_authenticator_authorizationdata() */
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");
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");
196 fail ("shishi_authenticator_remove_cksum() failed\n");
200 res
= shishi_a2d (handle
, a
, buffer
, &n
);
201 if (res
== SHISHI_OK
)
202 success ("shishi_a2d() OK\n");
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");
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");
219 fail ("shishi_asn1_done() failed\n");
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");
229 fail ("shishi_authenticator_from_file() failed\n");
233 /* shishi_authenticator_print() */
234 res
= shishi_authenticator_print (handle
, stdout
, a
);
235 if (res
== SHISHI_OK
)
236 success ("shishi_authenticator_print() OK\n");
238 fail ("shishi_authenticator_print() failed\n");
242 m
= sizeof (buffer2
);
243 res
= shishi_a2d (handle
, a
, buffer2
, &m
);
244 if (res
== SHISHI_OK
)
245 success ("shishi_a2d() OK\n");
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");
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");
260 fail ("shishi_authenticator_cusec_set() failed\n");
262 /* shishi_authenticator_cusec_get() */
263 res
= shishi_authenticator_cusec_get (handle
, a
, &n
);
265 printf ("shishi_authenticator_cusec_get () => `%d'.\n", n
);
266 if (res
== SHISHI_OK
&& n
== 4711)
267 success ("shishi_authenticator_cusec_get() OK\n");
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");
276 fail ("shishi_authenticator_ctime_set() failed\n");
278 /* shishi_authenticator_ctime_get() */
279 res
= shishi_authenticator_ctime_get (handle
, a
, buffer
);
281 escapeprint (buffer
, 15);
282 if (res
== SHISHI_OK
&& memcmp (buffer
, "19700101011831Z", 15) == 0)
283 success ("shishi_authenticator_ctime_get() OK\n");
285 fail ("shishi_authenticator_ctime_get() failed\n");
289 res
= shishi_a2d (handle
, a
, buffer
, &n
);
290 if (res
== SHISHI_OK
)
291 success ("shishi_a2d() OK\n");
293 n
= 0, fail ("shishi_a2d() failed\n");
296 shishi_authenticator_print (handle
, stdout
, a
);
297 hexprint (buffer
, n
);
299 hexprint (authenticator
, sizeof (authenticator
));
302 if (n
== sizeof (authenticator
) &&
303 n
== AUTHENTICATOR_LEN
&& memcmp (authenticator
, buffer
, n
) == 0)
304 success ("DER comparison OK\n");
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");
313 fail ("shishi_authenticator_clear_authorizationdata() failed\n");
317 res
= shishi_a2d (handle
, a
, buffer
, &n
);
318 if (res
== SHISHI_OK
)
319 success ("shishi_a2d() OK\n");
321 n
= 0, fail ("shishi_a2d() failed\n");
324 shishi_authenticator_print (handle
, stdout
, a
);
325 hexprint (buffer
, n
);
327 hexprint (authenticator2
, sizeof (authenticator2
));
330 if (n
== sizeof (authenticator2
) &&
331 n
== AUTHENTICATOR2_LEN
&& memcmp (authenticator2
, buffer
, n
) == 0)
332 success ("DER comparison OK\n");
334 fail ("DER comparison failed\n");
337 res
= unlink ("authenticator.tmp");
339 success ("unlink() OK\n");
341 fail ("unlink() failed\n");
343 shishi_done (handle
);
346 printf ("Authenticator self tests done with %d errors\n", error_count
);
348 return error_count
? 1 : 0;