Branch data Line data Source code
1 : : /* authenticator.c --- Shishi authenticator self tests.
2 : : * Copyright (C) 2002, 2003, 2004, 2006, 2007 Simon Josefsson
3 : : *
4 : : * This file is part of Shishi.
5 : : *
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 3 of the License, or
9 : : * (at your option) any later version.
10 : : *
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.
15 : : *
16 : : * You should have received a copy of the GNU General Public License
17 : : * along with Shishi; if not, see http://www.gnu.org/licenses or write
18 : : * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
19 : : * Floor, Boston, MA 02110-1301, USA
20 : : *
21 : : */
22 : :
23 : : #include "utils.c"
24 : :
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'
36 : : };
37 : :
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'
47 : : };
48 : :
49 : : #define AUTHENTICATOR_LEN 77
50 : : #define AUTHENTICATOR2_LEN 59
51 : :
52 : : void
53 : 1 : test (Shishi * handle)
54 : : {
55 : : Shishi_asn1 a;
56 : : char *p, *buf, *buf2;
57 : : int res;
58 : : size_t n, m;
59 : : int32_t t;
60 : : uint32_t s;
61 : :
62 : : /* shishi_authenticator */
63 : 1 : a = shishi_authenticator (handle);
64 [ - + ]: 1 : if (debug)
65 : 0 : printf ("shishi_authenticator () => `%p'.\n", a);
66 [ + - ]: 1 : if (a)
67 : 1 : success ("shishi_authenticator() OK\n");
68 : : else
69 : 0 : fail ("shishi_authenticator() failed\n");
70 : :
71 [ - + ]: 1 : if (debug)
72 : 0 : shishi_authenticator_print (handle, stdout, a);
73 : :
74 : 1 : res = shishi_authenticator_remove_subkey (handle, a);
75 [ + - ]: 1 : if (res == SHISHI_OK)
76 : 1 : success ("shishi_authenticator_remove_subkey() OK\n");
77 : : else
78 : 0 : fail ("shishi_authenticator_remove_subkey() failed\n");
79 : :
80 : : /* shishi_authenticator_seqnumber_get */
81 : 1 : res = shishi_authenticator_seqnumber_get (handle, a, &s);
82 [ + - ]: 1 : if (res == SHISHI_OK)
83 : 1 : success ("shishi_authenticator_seqnumber_get() OK\n");
84 : : else
85 : 0 : fail ("shishi_authenticator_seqnumber_get() failed\n");
86 : :
87 : : /* shishi_authenticator_seqnumber_set */
88 : 1 : res = shishi_authenticator_seqnumber_set (handle, a, 42);
89 [ + - ]: 1 : if (res == SHISHI_OK)
90 : 1 : success ("shishi_authenticator_seqnumber_set() OK\n");
91 : : else
92 : 0 : fail ("shishi_authenticator_seqnumber_set() failed\n");
93 : :
94 : : /* shishi_authenticator_seqnumber_get */
95 : 1 : res = shishi_authenticator_seqnumber_get (handle, a, &s);
96 [ + - + - ]: 2 : if (res == SHISHI_OK && s == 42)
97 : 1 : success ("shishi_authenticator_seqnumber_get() OK\n");
98 : : else
99 : 0 : fail ("shishi_authenticator_seqnumber_get() failed\n");
100 : :
101 : : /* shishi_authenticator_seqnumber_remove */
102 : 1 : res = shishi_authenticator_seqnumber_remove (handle, a);
103 [ + - ]: 1 : if (res == SHISHI_OK)
104 : 1 : success ("shishi_authenticator_seqnumber_remove() OK\n");
105 : : else
106 : 0 : fail ("shishi_authenticator_seqnumber_remove() failed\n");
107 : :
108 : : /* shishi_authenticator_set_crealm */
109 : 1 : res = shishi_authenticator_set_crealm (handle, a, "foo");
110 [ + - ]: 1 : if (res == SHISHI_OK)
111 : 1 : success ("shishi_authenticator_set_crealm() OK\n");
112 : : else
113 : 0 : fail ("shishi_authenticator_set_crealm() failed\n");
114 : :
115 : : /* shishi_authenticator_client_set */
116 : 1 : res = shishi_authenticator_client_set (handle, a, "foo/bar/baz");
117 [ + - ]: 1 : if (res == SHISHI_OK)
118 : 1 : success ("shishi_authenticator_client_set() OK\n");
119 : : else
120 : 0 : fail ("shishi_authenticator_client_set() failed\n");
121 : :
122 : : /* shishi_authenticator_client */
123 : 1 : res = shishi_authenticator_client (handle, a, &buf, &n);
124 [ - + ]: 1 : if (debug)
125 : 0 : escapeprint (buf, n);
126 [ + - ][ + - ]: 2 : if (res == SHISHI_OK &&
[ + - ]
127 : 2 : n == strlen ("foo/bar/baz") && memcmp (buf, "foo/bar/baz", n) == 0)
128 : 1 : success ("shishi_authenticator_client() OK\n");
129 : : else
130 : 0 : fail ("shishi_authenticator_client() failed\n");
131 [ + - ]: 1 : if (res == SHISHI_OK)
132 : 1 : free (buf);
133 : :
134 : : /* shishi_authenticator_client_set */
135 : 1 : res = shishi_authenticator_client_set (handle, a, "foo");
136 [ + - ]: 1 : if (res == SHISHI_OK)
137 : 1 : success ("shishi_authenticator_client_set() OK\n");
138 : : else
139 : 0 : fail ("shishi_authenticator_client_set() failed\n");
140 : :
141 : : /* shishi_authenticator_client */
142 : 1 : res = shishi_authenticator_client (handle, a, &buf, &n);
143 [ - + ]: 1 : if (debug)
144 : 0 : escapeprint (buf, n);
145 [ + - ][ + - ]: 2 : if (res == SHISHI_OK && n == strlen ("foo") && memcmp (buf, "foo", n) == 0)
[ + - ]
146 : 1 : success ("shishi_authenticator_client() OK\n");
147 : : else
148 : 0 : fail ("shishi_authenticator_client() failed\n");
149 [ + - ]: 1 : if (res == SHISHI_OK)
150 : 1 : free (buf);
151 : :
152 : : /* shishi_authenticator_set_crealm */
153 : 1 : res = shishi_authenticator_set_crealm (handle, a, "bar");
154 [ + - ]: 1 : if (res == SHISHI_OK)
155 : 1 : success ("shishi_authenticator_set_crealm() OK\n");
156 : : else
157 : 0 : fail ("shishi_authenticator_set_crealm() failed\n");
158 : :
159 : : /* shishi_authenticator_clientrealm */
160 : 1 : res = shishi_authenticator_clientrealm (handle, a, &buf, &n);
161 [ - + ]: 1 : if (debug)
162 : 0 : escapeprint (buf, n);
163 [ + - ][ + - ]: 2 : if (res == SHISHI_OK &&
[ + - ]
164 : 2 : n == strlen ("foo@bar") && memcmp (buf, "foo@bar", n) == 0)
165 : 1 : success ("shishi_authenticator_clientrealm() OK\n");
166 : : else
167 : 0 : fail ("shishi_authenticator_clientrealm() failed\n");
168 [ + - ]: 1 : if (res == SHISHI_OK)
169 : 1 : free (buf);
170 : :
171 : : /* shishi_authenticator_add_authorizationdata */
172 : 1 : res = shishi_authenticator_add_authorizationdata (handle, a, 42, "baz", 3);
173 [ + - ]: 1 : if (res == SHISHI_OK)
174 : 1 : success ("shishi_authenticator_add_authorizationdata() OK\n");
175 : : else
176 : 0 : fail ("shishi_authenticator_add_authorizationdata() failed\n");
177 : :
178 : : /* shishi_authenticator_authorizationdata */
179 : 1 : res = shishi_authenticator_authorizationdata (handle, a, &t, &buf, &m, 1);
180 [ - + ]: 1 : if (debug)
181 : 0 : escapeprint (buf, m);
182 [ + - ][ + - ]: 2 : if (res == SHISHI_OK && t == 42 && m == 3 && memcmp (buf, "baz", 3) == 0)
[ + - ][ + - ]
183 : 1 : success ("shishi_authenticator_authorizationdata() OK\n");
184 : : else
185 : 0 : fail ("shishi_authenticator_authorizationdata() failed\n");
186 [ + - ]: 1 : if (res == SHISHI_OK)
187 : 1 : free (buf);
188 : :
189 : : /* shishi_authenticator_authorizationdata */
190 : 1 : res = shishi_authenticator_authorizationdata (handle, a, &t, &buf, &m, 2);
191 [ - + ]: 1 : if (res == SHISHI_OK)
192 : 0 : free (buf);
193 [ + - ]: 1 : if (res == SHISHI_OUT_OF_RANGE)
194 : 1 : success ("shishi_authenticator_authorizationdata() OK\n");
195 : : else
196 : 0 : fail ("shishi_authenticator_authorizationdata() failed\n");
197 : :
198 : : /* shishi_authenticator_remove_cksum */
199 : 1 : res = shishi_authenticator_remove_cksum (handle, a);
200 [ + - ]: 1 : if (res == SHISHI_OK)
201 : 1 : success ("shishi_authenticator_remove_cksum() OK\n");
202 : : else
203 : 0 : fail ("shishi_authenticator_remove_cksum() failed\n");
204 : :
205 : : /* shishi_asn1_to_der */
206 : 1 : res = shishi_asn1_to_der (handle, a, &buf, &n);
207 [ + - ]: 1 : if (res == SHISHI_OK)
208 : 1 : success ("shishi_asn1_to_der() OK\n");
209 : : else
210 : 0 : n = 0, fail ("shishi_asn1_to_der() failed\n");
211 : :
212 : : /* shishi_authenticator_to_file */
213 : 1 : res = shishi_authenticator_to_file (handle, a, SHISHI_FILETYPE_TEXT,
214 : : "authenticator.tmp");
215 [ + - ]: 1 : if (res == SHISHI_OK)
216 : 1 : success ("shishi_authenticator_to_file() OK\n");
217 : : else
218 : 0 : fail ("shishi_authenticator_to_file() failed\n");
219 : :
220 : : /* shishi_asn1_done */
221 : 1 : shishi_asn1_done (handle, a);
222 : 1 : success ("shishi_asn1_done() OK\n");
223 : :
224 : 1 : a = NULL;
225 : :
226 : : /* shishi_authenticator_from_file */
227 : 1 : res = shishi_authenticator_from_file (handle, &a, SHISHI_FILETYPE_TEXT,
228 : : "authenticator.tmp");
229 [ + - ]: 1 : if (res == SHISHI_OK)
230 : 1 : success ("shishi_authenticator_from_file() OK\n");
231 : : else
232 : 0 : fail ("shishi_authenticator_from_file() failed\n");
233 : :
234 [ - + ]: 1 : if (debug)
235 : : {
236 : : /* shishi_authenticator_print */
237 : 0 : res = shishi_authenticator_print (handle, stdout, a);
238 [ # # ]: 0 : if (res == SHISHI_OK)
239 : 0 : success ("shishi_authenticator_print() OK\n");
240 : : else
241 : 0 : fail ("shishi_authenticator_print() failed\n");
242 : : }
243 : :
244 : : /* shishi_asn1_to_der */
245 : 1 : res = shishi_asn1_to_der (handle, a, &buf2, &m);
246 [ + - ]: 1 : if (res == SHISHI_OK)
247 : 1 : success ("shishi_asn1_to_der() OK\n");
248 : : else
249 : 0 : n = 0, fail ("shishi_asn1_to_der() failed\n");
250 : :
251 : : /* Compare DER encodings of authenticators */
252 [ + - ][ + - ]: 2 : if (n > 0 && m > 0 && n == m && memcmp (buf, buf2, n) == 0)
[ + - ][ + - ]
253 : 1 : success ("DER comparison OK\n");
254 : : else
255 : 0 : fail ("DER comparison failed\n");
256 : :
257 : 1 : free (buf);
258 : 1 : free (buf2);
259 : :
260 : : /* shishi_authenticator_cusec_set */
261 : 1 : res = shishi_authenticator_cusec_set (handle, a, 4711);
262 [ + - ]: 1 : if (res == SHISHI_OK)
263 : 1 : success ("shishi_authenticator_cusec_set() OK\n");
264 : : else
265 : 0 : fail ("shishi_authenticator_cusec_set() failed\n");
266 : :
267 : : /* shishi_authenticator_cusec_get */
268 : 1 : res = shishi_authenticator_cusec_get (handle, a, &s);
269 [ - + ]: 1 : if (debug)
270 : 0 : printf ("shishi_authenticator_cusec_get () => `%d'.\n", t);
271 [ + - ][ + - ]: 2 : if (res == SHISHI_OK && s == 4711)
272 : 1 : success ("shishi_authenticator_cusec_get() OK\n");
273 : : else
274 : 0 : fail ("shishi_authenticator_cusec_get() failed\n");
275 : :
276 : : /* shishi_authenticator_ctime_set */
277 : 1 : res = shishi_authenticator_ctime_set (handle, a, "19700101011831Z");
278 [ + - ]: 1 : if (res == SHISHI_OK)
279 : 1 : success ("shishi_authenticator_ctime_set() OK\n");
280 : : else
281 : 0 : fail ("shishi_authenticator_ctime_set() failed\n");
282 : :
283 : : /* shishi_authenticator_ctime */
284 : 1 : res = shishi_authenticator_ctime (handle, a, &p);
285 [ - + ]: 1 : if (debug)
286 : 0 : escapeprint (p, strlen (p));
287 [ + - ][ + - ]: 2 : if (res == SHISHI_OK && memcmp (p, "19700101011831Z", 15) == 0)
288 : 1 : success ("shishi_authenticator_ctime() OK\n");
289 : : else
290 : 0 : fail ("shishi_authenticator_ctime() failed\n");
291 [ + - ]: 1 : if (res == SHISHI_OK)
292 : 1 : free (p);
293 : :
294 : : /* shishi_asn1_to_der */
295 : 1 : res = shishi_asn1_to_der (handle, a, &buf, &n);
296 [ + - ]: 1 : if (res == SHISHI_OK)
297 : 1 : success ("shishi_asn1_to_der() OK\n");
298 : : else
299 : 0 : n = 0, fail ("shishi_asn1_to_der() failed\n");
300 [ - + ]: 1 : if (debug)
301 : : {
302 : 0 : shishi_authenticator_print (handle, stdout, a);
303 : 0 : hexprint (buf, n);
304 : 0 : puts ("");
305 : 0 : hexprint (authenticator, sizeof (authenticator));
306 : 0 : puts ("");
307 : : }
308 [ + - ][ + - ]: 2 : if (n == sizeof (authenticator) &&
[ + - ]
309 : 2 : n == AUTHENTICATOR_LEN && memcmp (authenticator, buf, n) == 0)
310 : 1 : success ("DER comparison OK\n");
311 : : else
312 : 0 : fail ("DER comparison failed\n");
313 : :
314 : 1 : free (buf);
315 : :
316 : : /* shishi_authenticator_clear_authorizationdata */
317 : 1 : res = shishi_authenticator_clear_authorizationdata (handle, a);
318 [ + - ]: 1 : if (res == SHISHI_OK)
319 : 1 : success ("shishi_authenticator_clear_authorizationdata() OK\n");
320 : : else
321 : 0 : fail ("shishi_authenticator_clear_authorizationdata() failed\n");
322 : :
323 : : /* shishi_asn1_to_der */
324 : 1 : res = shishi_asn1_to_der (handle, a, &buf, &n);
325 [ + - ]: 1 : if (res == SHISHI_OK)
326 : 1 : success ("shishi_asn1_to_der() OK\n");
327 : : else
328 : 0 : n = 0, fail ("shishi_asn1_to_der() failed\n");
329 [ - + ]: 1 : if (debug)
330 : : {
331 : 0 : shishi_authenticator_print (handle, stdout, a);
332 : 0 : hexprint (buf, n);
333 : 0 : puts ("");
334 : 0 : hexprint (authenticator2, sizeof (authenticator2));
335 : 0 : puts ("");
336 : : }
337 [ + - ][ + - ]: 2 : if (n == sizeof (authenticator2) &&
[ + - ]
338 : 2 : n == AUTHENTICATOR2_LEN && memcmp (authenticator2, buf, n) == 0)
339 : 1 : success ("DER comparison OK\n");
340 : : else
341 : 0 : fail ("DER comparison failed\n");
342 : :
343 : 1 : free (buf);
344 : :
345 : : /* unlink */
346 : 1 : res = unlink ("authenticator.tmp");
347 [ + - ]: 1 : if (res == 0)
348 : 1 : success ("unlink() OK\n");
349 : : else
350 : 0 : fail ("unlink() failed\n");
351 : 1 : }
|