1 /* authenticator.c Shishi authenticator self tests.
2 * Copyright (C) 2002, 2003, 2004, 2006 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 static const char authenticator
[] = {
25 '\x62', '\x4b', '\x30', '\x49', '\xa0', '\x03', '\x02', '\x01',
26 '\x05', '\xa1', '\x05', '\x1b', '\x03', '\x62', '\x61', '\x72',
27 '\xa2', '\x10', '\x30', '\x0e', '\xa0', '\x03', '\x02', '\x01',
28 '\x00', '\xa1', '\x07', '\x30', '\x05', '\x1b', '\x03', '\x66',
29 '\x6f', '\x6f', '\xa4', '\x04', '\x02', '\x02', '\x12', '\x67',
30 '\xa5', '\x11', '\x18', '\x0f', '\x31', '\x39', '\x37', '\x30',
31 '\x30', '\x31', '\x30', '\x31', '\x30', '\x31', '\x31', '\x38',
32 '\x33', '\x31', '\x5a', '\xa8', '\x10', '\x30', '\x0e', '\x30',
33 '\x0c', '\xa0', '\x03', '\x02', '\x01', '\x2a', '\xa1', '\x05',
34 '\x04', '\x03', '\x62', '\x61', '\x7a'
37 static const char authenticator2
[] = {
38 '\x62', '\x39', '\x30', '\x37', '\xa0', '\x03', '\x02', '\x01',
39 '\x05', '\xa1', '\x05', '\x1b', '\x03', '\x62', '\x61', '\x72',
40 '\xa2', '\x10', '\x30', '\x0e', '\xa0', '\x03', '\x02', '\x01',
41 '\x00', '\xa1', '\x07', '\x30', '\x05', '\x1b', '\x03', '\x66',
42 '\x6f', '\x6f', '\xa4', '\x04', '\x02', '\x02', '\x12', '\x67',
43 '\xa5', '\x11', '\x18', '\x0f', '\x31', '\x39', '\x37', '\x30',
44 '\x30', '\x31', '\x30', '\x31', '\x30', '\x31', '\x31', '\x38',
45 '\x33', '\x31', '\x5a'
48 #define AUTHENTICATOR_LEN 77
49 #define AUTHENTICATOR2_LEN 59
52 test (Shishi
* handle
)
61 /* shishi_authenticator */
62 a
= shishi_authenticator (handle
);
64 printf ("shishi_authenticator () => `%p'.\n", a
);
66 success ("shishi_authenticator() OK\n");
68 fail ("shishi_authenticator() failed\n");
71 shishi_authenticator_print (handle
, stdout
, a
);
73 res
= shishi_authenticator_remove_subkey (handle
, a
);
75 success ("shishi_authenticator_remove_subkey() OK\n");
77 fail ("shishi_authenticator_remove_subkey() failed\n");
79 /* shishi_authenticator_seqnumber_get */
80 res
= shishi_authenticator_seqnumber_get (handle
, a
, &s
);
82 success ("shishi_authenticator_seqnumber_get() OK\n");
84 fail ("shishi_authenticator_seqnumber_get() failed\n");
86 /* shishi_authenticator_seqnumber_set */
87 res
= shishi_authenticator_seqnumber_set (handle
, a
, 42);
89 success ("shishi_authenticator_seqnumber_set() OK\n");
91 fail ("shishi_authenticator_seqnumber_set() failed\n");
93 /* shishi_authenticator_seqnumber_get */
94 res
= shishi_authenticator_seqnumber_get (handle
, a
, &s
);
95 if (res
== SHISHI_OK
&& s
== 42)
96 success ("shishi_authenticator_seqnumber_get() OK\n");
98 fail ("shishi_authenticator_seqnumber_get() failed\n");
100 /* shishi_authenticator_seqnumber_remove */
101 res
= shishi_authenticator_seqnumber_remove (handle
, a
);
102 if (res
== SHISHI_OK
)
103 success ("shishi_authenticator_seqnumber_remove() OK\n");
105 fail ("shishi_authenticator_seqnumber_remove() failed\n");
107 /* shishi_authenticator_set_crealm */
108 res
= shishi_authenticator_set_crealm (handle
, a
, "foo");
109 if (res
== SHISHI_OK
)
110 success ("shishi_authenticator_set_crealm() OK\n");
112 fail ("shishi_authenticator_set_crealm() failed\n");
114 /* shishi_authenticator_client_set */
115 res
= shishi_authenticator_client_set (handle
, a
, "foo/bar/baz");
116 if (res
== SHISHI_OK
)
117 success ("shishi_authenticator_client_set() OK\n");
119 fail ("shishi_authenticator_client_set() failed\n");
121 /* shishi_authenticator_client */
122 res
= shishi_authenticator_client (handle
, a
, &buf
, &n
);
124 escapeprint (buf
, n
);
125 if (res
== SHISHI_OK
&&
126 n
== strlen ("foo/bar/baz") && memcmp (buf
, "foo/bar/baz", n
) == 0)
127 success ("shishi_authenticator_client() OK\n");
129 fail ("shishi_authenticator_client() failed\n");
130 if (res
== SHISHI_OK
)
133 /* shishi_authenticator_client_set */
134 res
= shishi_authenticator_client_set (handle
, a
, "foo");
135 if (res
== SHISHI_OK
)
136 success ("shishi_authenticator_client_set() OK\n");
138 fail ("shishi_authenticator_client_set() failed\n");
140 /* shishi_authenticator_client */
141 res
= shishi_authenticator_client (handle
, a
, &buf
, &n
);
143 escapeprint (buf
, n
);
144 if (res
== SHISHI_OK
&& n
== strlen ("foo") && memcmp (buf
, "foo", n
) == 0)
145 success ("shishi_authenticator_client() OK\n");
147 fail ("shishi_authenticator_client() failed\n");
148 if (res
== SHISHI_OK
)
151 /* shishi_authenticator_set_crealm */
152 res
= shishi_authenticator_set_crealm (handle
, a
, "bar");
153 if (res
== SHISHI_OK
)
154 success ("shishi_authenticator_set_crealm() OK\n");
156 fail ("shishi_authenticator_set_crealm() failed\n");
158 /* shishi_authenticator_clientrealm */
159 res
= shishi_authenticator_clientrealm (handle
, a
, &buf
, &n
);
161 escapeprint (buf
, n
);
162 if (res
== SHISHI_OK
&&
163 n
== strlen ("foo@bar") && memcmp (buf
, "foo@bar", n
) == 0)
164 success ("shishi_authenticator_clientrealm() OK\n");
166 fail ("shishi_authenticator_clientrealm() failed\n");
167 if (res
== SHISHI_OK
)
170 /* shishi_authenticator_add_authorizationdata */
171 res
= shishi_authenticator_add_authorizationdata (handle
, a
, 42, "baz", 3);
172 if (res
== SHISHI_OK
)
173 success ("shishi_authenticator_add_authorizationdata() OK\n");
175 fail ("shishi_authenticator_add_authorizationdata() failed\n");
177 /* shishi_authenticator_authorizationdata */
178 res
= shishi_authenticator_authorizationdata (handle
, a
, &t
, &buf
, &m
, 1);
180 escapeprint (buf
, m
);
181 if (res
== SHISHI_OK
&& t
== 42 && m
== 3 && memcmp (buf
, "baz", 3) == 0)
182 success ("shishi_authenticator_authorizationdata() OK\n");
184 fail ("shishi_authenticator_authorizationdata() failed\n");
185 if (res
== SHISHI_OK
)
188 /* shishi_authenticator_authorizationdata */
189 res
= shishi_authenticator_authorizationdata (handle
, a
, &t
, &buf
, &m
, 2);
190 if (res
== SHISHI_OK
)
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");
204 /* shishi_asn1_to_der */
205 res
= shishi_asn1_to_der (handle
, a
, &buf
, &n
);
206 if (res
== SHISHI_OK
)
207 success ("shishi_asn1_to_der() OK\n");
209 n
= 0, fail ("shishi_asn1_to_der() failed\n");
211 /* shishi_authenticator_to_file */
212 res
= shishi_authenticator_to_file (handle
, a
, SHISHI_FILETYPE_TEXT
,
213 "authenticator.tmp");
214 if (res
== SHISHI_OK
)
215 success ("shishi_authenticator_to_file() OK\n");
217 fail ("shishi_authenticator_to_file() failed\n");
219 /* shishi_asn1_done */
220 shishi_asn1_done (handle
, a
);
221 success ("shishi_asn1_done() OK\n");
225 /* shishi_authenticator_from_file */
226 res
= shishi_authenticator_from_file (handle
, &a
, SHISHI_FILETYPE_TEXT
,
227 "authenticator.tmp");
228 if (res
== SHISHI_OK
)
229 success ("shishi_authenticator_from_file() OK\n");
231 fail ("shishi_authenticator_from_file() failed\n");
235 /* shishi_authenticator_print */
236 res
= shishi_authenticator_print (handle
, stdout
, a
);
237 if (res
== SHISHI_OK
)
238 success ("shishi_authenticator_print() OK\n");
240 fail ("shishi_authenticator_print() failed\n");
243 /* shishi_asn1_to_der */
244 res
= shishi_asn1_to_der (handle
, a
, &buf2
, &m
);
245 if (res
== SHISHI_OK
)
246 success ("shishi_asn1_to_der() OK\n");
248 n
= 0, fail ("shishi_asn1_to_der() failed\n");
250 /* Compare DER encodings of authenticators */
251 if (n
> 0 && m
> 0 && n
== m
&& memcmp (buf
, buf2
, n
) == 0)
252 success ("DER comparison OK\n");
254 fail ("DER comparison failed\n");
259 /* shishi_authenticator_cusec_set */
260 res
= shishi_authenticator_cusec_set (handle
, a
, 4711);
261 if (res
== SHISHI_OK
)
262 success ("shishi_authenticator_cusec_set() OK\n");
264 fail ("shishi_authenticator_cusec_set() failed\n");
266 /* shishi_authenticator_cusec_get */
267 res
= shishi_authenticator_cusec_get (handle
, a
, &s
);
269 printf ("shishi_authenticator_cusec_get () => `%d'.\n", t
);
270 if (res
== SHISHI_OK
&& s
== 4711)
271 success ("shishi_authenticator_cusec_get() OK\n");
273 fail ("shishi_authenticator_cusec_get() failed\n");
275 /* shishi_authenticator_ctime_set */
276 res
= shishi_authenticator_ctime_set (handle
, a
, "19700101011831Z");
277 if (res
== SHISHI_OK
)
278 success ("shishi_authenticator_ctime_set() OK\n");
280 fail ("shishi_authenticator_ctime_set() failed\n");
282 /* shishi_authenticator_ctime */
283 res
= shishi_authenticator_ctime (handle
, a
, &p
);
285 escapeprint (p
, strlen (p
));
286 if (res
== SHISHI_OK
&& memcmp (p
, "19700101011831Z", 15) == 0)
287 success ("shishi_authenticator_ctime() OK\n");
289 fail ("shishi_authenticator_ctime() failed\n");
290 if (res
== SHISHI_OK
)
293 /* shishi_asn1_to_der */
294 res
= shishi_asn1_to_der (handle
, a
, &buf
, &n
);
295 if (res
== SHISHI_OK
)
296 success ("shishi_asn1_to_der() OK\n");
298 n
= 0, fail ("shishi_asn1_to_der() failed\n");
301 shishi_authenticator_print (handle
, stdout
, a
);
304 hexprint (authenticator
, sizeof (authenticator
));
307 if (n
== sizeof (authenticator
) &&
308 n
== AUTHENTICATOR_LEN
&& memcmp (authenticator
, buf
, n
) == 0)
309 success ("DER comparison OK\n");
311 fail ("DER comparison failed\n");
315 /* shishi_authenticator_clear_authorizationdata */
316 res
= shishi_authenticator_clear_authorizationdata (handle
, a
);
317 if (res
== SHISHI_OK
)
318 success ("shishi_authenticator_clear_authorizationdata() OK\n");
320 fail ("shishi_authenticator_clear_authorizationdata() failed\n");
322 /* shishi_asn1_to_der */
323 res
= shishi_asn1_to_der (handle
, a
, &buf
, &n
);
324 if (res
== SHISHI_OK
)
325 success ("shishi_asn1_to_der() OK\n");
327 n
= 0, fail ("shishi_asn1_to_der() failed\n");
330 shishi_authenticator_print (handle
, stdout
, a
);
333 hexprint (authenticator2
, sizeof (authenticator2
));
336 if (n
== sizeof (authenticator2
) &&
337 n
== AUTHENTICATOR2_LEN
&& memcmp (authenticator2
, buf
, n
) == 0)
338 success ("DER comparison OK\n");
340 fail ("DER comparison failed\n");
345 res
= unlink ("authenticator.tmp");
347 success ("unlink() OK\n");
349 fail ("unlink() failed\n");