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 () => `%p'.\n", a
);
101 success ("shishi_authenticator() OK\n");
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");
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");
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");
123 fail ("shishi_authenticator_client_set() failed\n");
125 /* shishi_authenticator_cname_get() */
127 res
= shishi_authenticator_cname_get (handle
, a
, buffer
, &n
);
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");
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");
141 fail ("shishi_authenticator_client_set() failed\n");
143 /* shishi_authenticator_cname_get() */
145 res
= shishi_authenticator_cname_get (handle
, a
, buffer
, &n
);
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");
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");
159 fail ("shishi_authenticator_set_crealm() failed\n");
161 /* shishi_authenticator_cnamerealm_get() */
163 res
= shishi_authenticator_cnamerealm_get (handle
, a
, buffer
, &n
);
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");
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");
177 fail ("shishi_authenticator_add_authorizationdata() failed\n");
179 /* shishi_authenticator_authorizationdata() */
181 res
= shishi_authenticator_authorizationdata (handle
, a
, &n
, buffer
, &m
, 1);
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");
187 fail ("shishi_authenticator_authorizationdata() failed\n");
189 /* shishi_authenticator_authorizationdata() */
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");
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");
202 fail ("shishi_authenticator_remove_cksum() failed\n");
206 res
= shishi_a2d (handle
, a
, buffer
, &n
);
207 if (res
== SHISHI_OK
)
208 success ("shishi_a2d() OK\n");
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");
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");
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");
232 fail ("shishi_authenticator_from_file() failed\n");
236 /* shishi_authenticator_print() */
237 res
= shishi_authenticator_print (handle
, stdout
, a
);
238 if (res
== SHISHI_OK
)
239 success ("shishi_authenticator_print() OK\n");
241 fail ("shishi_authenticator_print() failed\n");
245 m
= sizeof (buffer2
);
246 res
= shishi_a2d (handle
, a
, buffer2
, &m
);
247 if (res
== SHISHI_OK
)
248 success ("shishi_a2d() OK\n");
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");
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");
263 fail ("shishi_authenticator_cusec_set() failed\n");
265 /* shishi_authenticator_cusec_get() */
266 res
= shishi_authenticator_cusec_get (handle
, a
, &n
);
268 printf ("shishi_authenticator_cusec_get () => `%d'.\n", n
);
269 if (res
== SHISHI_OK
&& n
== 4711)
270 success ("shishi_authenticator_cusec_get() OK\n");
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");
279 fail ("shishi_authenticator_ctime_set() failed\n");
281 /* shishi_authenticator_ctime() */
282 res
= shishi_authenticator_ctime (handle
, a
, &p
);
284 escapeprint (p
, strlen (p
));
285 if (res
== SHISHI_OK
&& memcmp (p
, "19700101011831Z", 15) == 0)
286 success ("shishi_authenticator_ctime() OK\n");
288 fail ("shishi_authenticator_ctime() failed\n");
292 res
= shishi_a2d (handle
, a
, buffer
, &n
);
293 if (res
== SHISHI_OK
)
294 success ("shishi_a2d() OK\n");
296 n
= 0, fail ("shishi_a2d() failed\n");
299 shishi_authenticator_print (handle
, stdout
, a
);
300 hexprint (buffer
, n
);
302 hexprint (authenticator
, sizeof (authenticator
));
305 if (n
== sizeof (authenticator
) &&
306 n
== AUTHENTICATOR_LEN
&& memcmp (authenticator
, buffer
, n
) == 0)
307 success ("DER comparison OK\n");
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");
316 fail ("shishi_authenticator_clear_authorizationdata() failed\n");
320 res
= shishi_a2d (handle
, a
, buffer
, &n
);
321 if (res
== SHISHI_OK
)
322 success ("shishi_a2d() OK\n");
324 n
= 0, fail ("shishi_a2d() failed\n");
327 shishi_authenticator_print (handle
, stdout
, a
);
328 hexprint (buffer
, n
);
330 hexprint (authenticator2
, sizeof (authenticator2
));
333 if (n
== sizeof (authenticator2
) &&
334 n
== AUTHENTICATOR2_LEN
&& memcmp (authenticator2
, buffer
, n
) == 0)
335 success ("DER comparison OK\n");
337 fail ("DER comparison failed\n");
340 res
= unlink ("authenticator.tmp");
342 success ("unlink() OK\n");
344 fail ("unlink() failed\n");
346 shishi_done (handle
);
349 printf ("Authenticator self tests done with %d errors\n", error_count
);
351 return error_count
? 1 : 0;