Branch data Line data Source code
1 : : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : : /*
3 : : Copyright (C) 2010 Stefan Walter
4 : :
5 : : The Gnome Keyring Library is free software; you can redistribute it and/or
6 : : modify it under the terms of the GNU Library General Public License as
7 : : published by the Free Software Foundation; either version 2 of the
8 : : License, or (at your option) any later version.
9 : :
10 : : The Gnome Keyring Library is distributed in the hope that it will be useful,
11 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : : Library General Public License for more details.
14 : :
15 : : You should have received a copy of the GNU Library General Public
16 : : License along with the Gnome Library; see the file COPYING.LIB. If not,
17 : : see <http://www.gnu.org/licenses/>.
18 : :
19 : : Author: Stef Walter <stef@memberwebs.com>
20 : : */
21 : :
22 : : #include "config.h"
23 : :
24 : : #include "gcr/gcr.h"
25 : : #include "gcr/gcr-internal.h"
26 : :
27 : : #include "gck/gck-mock.h"
28 : : #include "gck/gck-test.h"
29 : : #include <p11-kit/pkcs11.h>
30 : : #include "gck/pkcs11n.h"
31 : : #include "gck/pkcs11x.h"
32 : :
33 : : #include "egg/egg-testing.h"
34 : :
35 : : #include <glib.h>
36 : :
37 : : #include <errno.h>
38 : :
39 : : typedef struct {
40 : : CK_FUNCTION_LIST funcs;
41 : : GcrCertificate *certificate;
42 : : } Test;
43 : :
44 : : static void
45 : 12 : setup (Test *test, gconstpointer unused)
46 : : {
47 : 12 : GList *modules = NULL;
48 : : CK_FUNCTION_LIST_PTR f;
49 : : GckModule *module;
50 : : gchar *contents;
51 : : const gchar *uris[2];
52 : : gsize len;
53 : : CK_RV rv;
54 : :
55 [ - + ]: 12 : if (!g_file_get_contents (SRCDIR "/gcr/fixtures/der-certificate.crt", &contents, &len, NULL))
56 : 0 : g_assert_not_reached ();
57 [ - + ]: 12 : g_assert (contents);
58 : :
59 : 12 : test->certificate = gcr_simple_certificate_new ((const guchar *)contents, len);
60 : 12 : g_free (contents);
61 : :
62 : 12 : rv = gck_mock_C_GetFunctionList (&f);
63 [ - + ]: 12 : gck_assert_cmprv (rv, ==, CKR_OK);
64 : 12 : memcpy (&test->funcs, f, sizeof (test->funcs));
65 : :
66 : : /* Open a session */
67 : 12 : rv = (test->funcs.C_Initialize) (NULL);
68 [ - + ]: 12 : gck_assert_cmprv (rv, ==, CKR_OK);
69 : :
70 [ - + ]: 12 : g_assert (!modules);
71 : 12 : module = gck_module_new (&test->funcs);
72 : 12 : modules = g_list_prepend (modules, module);
73 : 12 : gcr_pkcs11_set_modules (modules);
74 [ + - + - ]: 12 : g_clear_list (&modules, g_object_unref);
75 : :
76 : 12 : uris[0] = GCK_MOCK_SLOT_ONE_URI;
77 : 12 : uris[1] = NULL;
78 : :
79 : 12 : gcr_pkcs11_set_trust_store_uri (GCK_MOCK_SLOT_ONE_URI);
80 : 12 : gcr_pkcs11_set_trust_lookup_uris (uris);
81 : 12 : }
82 : :
83 : : static void
84 : 12 : teardown (Test *test, gconstpointer unused)
85 : : {
86 : : CK_RV rv;
87 : :
88 : 12 : g_object_unref (test->certificate);
89 : :
90 : 12 : rv = (test->funcs.C_Finalize) (NULL);
91 [ - + ]: 12 : gck_assert_cmprv (rv, ==, CKR_OK);
92 : :
93 : 12 : _gcr_uninitialize_library ();
94 : 12 : }
95 : :
96 : : static void
97 : 1 : test_is_pinned_none (Test *test, gconstpointer unused)
98 : : {
99 : 1 : GError *error = NULL;
100 : : gboolean trust;
101 : :
102 : 1 : trust = gcr_trust_is_certificate_pinned (test->certificate, GCR_PURPOSE_EMAIL, "host", NULL, &error);
103 [ - + ]: 1 : g_assert_cmpint (trust, ==, FALSE);
104 [ - + ]: 1 : g_assert (error == NULL);
105 : 1 : }
106 : :
107 : : static void
108 : 1 : test_add_and_is_pinned (Test *test, gconstpointer unused)
109 : : {
110 : 1 : GError *error = NULL;
111 : : gboolean trust;
112 : : gboolean ret;
113 : :
114 : 1 : trust = gcr_trust_is_certificate_pinned (test->certificate, GCR_PURPOSE_EMAIL, "host", NULL, &error);
115 [ - + ]: 1 : g_assert_cmpint (trust, ==, FALSE);
116 [ - + ]: 1 : g_assert (error == NULL);
117 : :
118 : 1 : ret = gcr_trust_add_pinned_certificate (test->certificate, GCR_PURPOSE_EMAIL, "host", NULL, &error);
119 [ - + ]: 1 : g_assert (ret == TRUE);
120 [ - + ]: 1 : g_assert (error == NULL);
121 : :
122 : 1 : trust = gcr_trust_is_certificate_pinned (test->certificate, GCR_PURPOSE_EMAIL, "host", NULL, &error);
123 [ - + ]: 1 : g_assert_cmpint (trust, ==, TRUE);
124 [ - + ]: 1 : g_assert (error == NULL);
125 : 1 : }
126 : :
127 : : static void
128 : 1 : test_add_certificate_pinned_fail (Test *test, gconstpointer unused)
129 : : {
130 : 1 : GError *error = NULL;
131 : : gboolean ret;
132 : :
133 : : /* Make this function fail */
134 : 1 : test->funcs.C_CreateObject = gck_mock_fail_C_CreateObject;
135 : :
136 : 1 : ret = gcr_trust_add_pinned_certificate (test->certificate, GCR_PURPOSE_CLIENT_AUTH, "peer", NULL, &error);
137 [ - + ]: 1 : g_assert (ret == FALSE);
138 [ + - + - : 1 : g_assert_error (error, GCK_ERROR, CKR_FUNCTION_FAILED);
- + ]
139 : 1 : g_clear_error (&error);
140 : 1 : }
141 : :
142 : : static void
143 : 1 : test_add_and_remov_pinned (Test *test, gconstpointer unused)
144 : : {
145 : 1 : GError *error = NULL;
146 : : gboolean trust;
147 : : gboolean ret;
148 : :
149 : 1 : ret = gcr_trust_add_pinned_certificate (test->certificate, GCR_PURPOSE_EMAIL, "host", NULL, &error);
150 [ - + ]: 1 : g_assert (ret == TRUE);
151 [ - + ]: 1 : g_assert (error == NULL);
152 : :
153 : 1 : trust = gcr_trust_is_certificate_pinned (test->certificate, GCR_PURPOSE_EMAIL, "host", NULL, &error);
154 [ - + ]: 1 : g_assert_cmpint (trust, ==, TRUE);
155 [ - + ]: 1 : g_assert (error == NULL);
156 : :
157 : 1 : ret = gcr_trust_remove_pinned_certificate (test->certificate, GCR_PURPOSE_EMAIL, "host", NULL, &error);
158 [ - + ]: 1 : g_assert (ret == TRUE);
159 [ - + ]: 1 : g_assert (error == NULL);
160 : :
161 : 1 : trust = gcr_trust_is_certificate_pinned (test->certificate, GCR_PURPOSE_EMAIL, "host", NULL, &error);
162 [ - + ]: 1 : g_assert_cmpint (trust, ==, FALSE);
163 [ - + ]: 1 : g_assert (error == NULL);
164 : 1 : }
165 : :
166 : : static void
167 : 9 : fetch_async_result (GObject *source, GAsyncResult *result, gpointer user_data)
168 : : {
169 : 9 : *((GAsyncResult**)user_data) = result;
170 : 9 : g_object_ref (result);
171 : 9 : egg_test_wait_stop ();
172 : 9 : }
173 : :
174 : : static void
175 : 1 : test_add_and_is_pinned_async (Test *test, gconstpointer unused)
176 : : {
177 : 1 : GAsyncResult *result = NULL;
178 : 1 : GError *error = NULL;
179 : : gboolean trust;
180 : : gboolean ret;
181 : :
182 : 1 : gcr_trust_is_certificate_pinned_async (test->certificate, GCR_PURPOSE_EMAIL, "host", NULL, fetch_async_result, &result);
183 : 1 : egg_test_wait_until (500);
184 [ - + ]: 1 : g_assert (result);
185 : 1 : trust = gcr_trust_is_certificate_pinned_finish (result, &error);
186 [ - + ]: 1 : g_assert (trust == FALSE);
187 [ - + ]: 1 : g_assert (error == NULL);
188 : 1 : g_object_unref (result);
189 : 1 : result = NULL;
190 : :
191 : 1 : gcr_trust_add_pinned_certificate_async (test->certificate, GCR_PURPOSE_EMAIL, "host",
192 : : NULL, fetch_async_result, &result);
193 : 1 : egg_test_wait_until (500);
194 [ - + ]: 1 : g_assert (result);
195 : 1 : ret = gcr_trust_add_pinned_certificate_finish (result, &error);
196 [ - + ]: 1 : g_assert (ret == TRUE);
197 [ - + ]: 1 : g_assert (error == NULL);
198 : 1 : g_object_unref (result);
199 : 1 : result = NULL;
200 : :
201 : 1 : gcr_trust_is_certificate_pinned_async (test->certificate, GCR_PURPOSE_EMAIL, "host", NULL, fetch_async_result, &result);
202 : 1 : egg_test_wait_until (500);
203 [ - + ]: 1 : g_assert (result);
204 : 1 : trust = gcr_trust_is_certificate_pinned_finish (result, &error);
205 [ - + ]: 1 : g_assert (trust == TRUE);
206 [ - + ]: 1 : g_assert (error == NULL);
207 : 1 : g_object_unref (result);
208 : 1 : result = NULL;
209 : 1 : }
210 : :
211 : : static void
212 : 1 : test_add_and_remov_pinned_async (Test *test, gconstpointer unused)
213 : : {
214 : 1 : GAsyncResult *result = NULL;
215 : 1 : GError *error = NULL;
216 : : gboolean trust;
217 : : gboolean ret;
218 : :
219 : 1 : gcr_trust_add_pinned_certificate_async (test->certificate, GCR_PURPOSE_EMAIL, "host", NULL, fetch_async_result, &result);
220 : 1 : egg_test_wait_until (500);
221 [ - + ]: 1 : g_assert (result);
222 : 1 : ret = gcr_trust_add_pinned_certificate_finish (result, &error);
223 [ - + ]: 1 : g_assert (ret == TRUE);
224 [ - + ]: 1 : g_assert (error == NULL);
225 : 1 : g_object_unref (result);
226 : 1 : result = NULL;
227 : :
228 : 1 : gcr_trust_is_certificate_pinned_async (test->certificate, GCR_PURPOSE_EMAIL, "host", NULL, fetch_async_result, &result);
229 : 1 : egg_test_wait_until (500);
230 [ - + ]: 1 : g_assert (result);
231 : 1 : trust = gcr_trust_is_certificate_pinned_finish (result, &error);
232 [ - + ]: 1 : g_assert (trust == TRUE);
233 [ - + ]: 1 : g_assert (error == NULL);
234 : 1 : g_object_unref (result);
235 : 1 : result = NULL;
236 : :
237 : 1 : gcr_trust_remove_pinned_certificate_async (test->certificate, GCR_PURPOSE_EMAIL, "host", NULL, fetch_async_result, &result);
238 : 1 : egg_test_wait_until (500);
239 [ - + ]: 1 : g_assert (result);
240 : 1 : ret = gcr_trust_remove_pinned_certificate_finish (result, &error);
241 [ - + ]: 1 : g_assert (ret == TRUE);
242 [ - + ]: 1 : g_assert (error == NULL);
243 : 1 : g_object_unref (result);
244 : 1 : result = NULL;
245 : :
246 : 1 : gcr_trust_is_certificate_pinned_async (test->certificate, GCR_PURPOSE_EMAIL, "host", NULL, fetch_async_result, &result);
247 : 1 : egg_test_wait_until (500);
248 [ - + ]: 1 : g_assert (result);
249 : 1 : trust = gcr_trust_is_certificate_pinned_finish (result, &error);
250 [ - + ]: 1 : g_assert (trust == FALSE);
251 [ - + ]: 1 : g_assert (error == NULL);
252 : 1 : g_object_unref (result);
253 : 1 : result = NULL;
254 : 1 : }
255 : :
256 : : static void
257 : 1 : test_is_certificate_anchored_not (Test *test, gconstpointer unused)
258 : : {
259 : 1 : GError *error = NULL;
260 : : gboolean ret;
261 : :
262 : 1 : ret = gcr_trust_is_certificate_anchored (test->certificate, GCR_PURPOSE_CLIENT_AUTH, NULL, &error);
263 [ - + ]: 1 : g_assert (ret == FALSE);
264 [ - + ]: 1 : g_assert (error == NULL);
265 : 1 : }
266 : :
267 : : static void
268 : 1 : test_is_certificate_anchored_yes (Test *test, gconstpointer unused)
269 : : {
270 : 1 : GckBuilder builder = GCK_BUILDER_INIT;
271 : 1 : GError *error = NULL;
272 : : gconstpointer der;
273 : : gsize n_der;
274 : : gboolean ret;
275 : :
276 : : /* Create a certificate root trust */
277 : 1 : der = gcr_certificate_get_der_data (test->certificate, &n_der);
278 : 1 : gck_builder_add_data (&builder, CKA_X_CERTIFICATE_VALUE, der, n_der);
279 : 1 : gck_builder_add_ulong (&builder, CKA_CLASS, CKO_X_TRUST_ASSERTION);
280 : 1 : gck_builder_add_boolean (&builder, CKA_TOKEN, TRUE);
281 : 1 : gck_builder_add_string (&builder, CKA_X_PURPOSE, GCR_PURPOSE_CLIENT_AUTH);
282 : 1 : gck_builder_add_ulong (&builder, CKA_X_ASSERTION_TYPE, CKT_X_ANCHORED_CERTIFICATE);
283 : 1 : gck_mock_module_add_object (gck_builder_end (&builder));
284 : :
285 : 1 : ret = gcr_trust_is_certificate_anchored (test->certificate, GCR_PURPOSE_CLIENT_AUTH, NULL, &error);
286 [ - + ]: 1 : g_assert (ret == TRUE);
287 [ - + ]: 1 : g_assert (error == NULL);
288 : 1 : }
289 : :
290 : : static void
291 : 1 : test_is_certificate_anchored_async (Test *test, gconstpointer unused)
292 : : {
293 : 1 : GAsyncResult *result = NULL;
294 : 1 : GError *error = NULL;
295 : : gboolean ret;
296 : :
297 : 1 : gcr_trust_is_certificate_anchored_async (test->certificate, GCR_PURPOSE_CLIENT_AUTH, NULL, fetch_async_result, &result);
298 : 1 : egg_test_wait_until (500);
299 [ - + ]: 1 : g_assert (result);
300 : :
301 : 1 : ret = gcr_trust_is_certificate_anchored_finish (result, &error);
302 [ - + ]: 1 : g_assert (ret == FALSE);
303 [ - + ]: 1 : g_assert (error == NULL);
304 : :
305 : 1 : g_object_unref (result);
306 : 1 : }
307 : :
308 : : static void
309 : 1 : test_is_certificate_distrusted_not (Test *test, gconstpointer unused)
310 : : {
311 : 1 : guchar *serial_nr = NULL;
312 : : size_t serial_nr_len;
313 : 1 : guchar *issuer = NULL;
314 : : size_t issuer_len;
315 : 1 : GError *error = NULL;
316 : : gboolean ret;
317 : :
318 : 1 : serial_nr = gcr_certificate_get_serial_number (test->certificate, &serial_nr_len);
319 : 1 : issuer = gcr_certificate_get_issuer_raw (test->certificate, &issuer_len);
320 : :
321 : 1 : ret = gcr_trust_is_certificate_distrusted (serial_nr, serial_nr_len,
322 : : issuer, issuer_len,
323 : : NULL, &error);
324 [ - + ]: 1 : g_assert_false (ret);
325 [ - + ]: 1 : g_assert_no_error (error);
326 : :
327 : 1 : g_free (issuer);
328 : 1 : g_free (serial_nr);
329 : 1 : }
330 : :
331 : : static void
332 : 1 : test_is_certificate_distrusted_yes (Test *test, gconstpointer unused)
333 : : {
334 : 1 : GckBuilder builder = GCK_BUILDER_INIT;
335 : 1 : guchar *serial_nr = NULL;
336 : : size_t serial_nr_len;
337 : 1 : guchar *issuer = NULL;
338 : : size_t issuer_len;
339 : 1 : GError *error = NULL;
340 : : gboolean ret;
341 : :
342 : 1 : serial_nr = gcr_certificate_get_serial_number (test->certificate, &serial_nr_len);
343 : 1 : issuer = gcr_certificate_get_issuer_raw (test->certificate, &issuer_len);
344 : :
345 : : /* Create "distrusted certificate" trust assertion */
346 : 1 : gck_builder_add_ulong (&builder, CKA_CLASS, CKO_X_TRUST_ASSERTION);
347 : 1 : gck_builder_add_ulong (&builder, CKA_X_ASSERTION_TYPE, CKT_X_DISTRUSTED_CERTIFICATE);
348 : 1 : gck_builder_add_data (&builder, CKA_SERIAL_NUMBER, serial_nr, serial_nr_len);
349 : 1 : gck_builder_add_data (&builder, CKA_ISSUER, issuer, issuer_len);
350 : 1 : gck_mock_module_add_object (gck_builder_end (&builder));
351 : :
352 : 1 : ret = gcr_trust_is_certificate_distrusted (serial_nr, serial_nr_len,
353 : : issuer, issuer_len,
354 : : NULL, &error);
355 [ - + ]: 1 : g_assert_true (ret);
356 [ - + ]: 1 : g_assert_no_error (error);
357 : :
358 : 1 : g_free (issuer);
359 : 1 : g_free (serial_nr);
360 : 1 : }
361 : :
362 : : static void
363 : 1 : test_is_certificate_distrusted_async (Test *test, gconstpointer unused)
364 : : {
365 : 1 : guchar *serial_nr = NULL;
366 : : size_t serial_nr_len;
367 : 1 : guchar *issuer = NULL;
368 : : size_t issuer_len;
369 : 1 : GAsyncResult *result = NULL;
370 : 1 : GError *error = NULL;
371 : : gboolean ret;
372 : :
373 : 1 : serial_nr = gcr_certificate_get_serial_number (test->certificate, &serial_nr_len);
374 : 1 : issuer = gcr_certificate_get_issuer_raw (test->certificate, &issuer_len);
375 : :
376 : 1 : gcr_trust_is_certificate_distrusted_async (serial_nr, serial_nr_len,
377 : : issuer, issuer_len,
378 : : NULL, fetch_async_result, &result);
379 : 1 : egg_test_wait_until (500);
380 [ - + ]: 1 : g_assert_nonnull (result);
381 : :
382 : 1 : ret = gcr_trust_is_certificate_distrusted_finish (result, &error);
383 [ - + ]: 1 : g_assert_false (ret);
384 [ - + ]: 1 : g_assert_no_error (error);
385 : :
386 : 1 : g_free (issuer);
387 : 1 : g_free (serial_nr);
388 : 1 : g_object_unref (result);
389 : 1 : }
390 : :
391 : : int
392 : 1 : main (int argc, char **argv)
393 : : {
394 : 1 : g_test_init (&argc, &argv, NULL);
395 : 1 : g_set_prgname ("test-trust");
396 : :
397 : 1 : g_test_add ("/gcr/trust/is_pinned_none", Test, NULL, setup, test_is_pinned_none, teardown);
398 : 1 : g_test_add ("/gcr/trust/add_and_is_pinned", Test, NULL, setup, test_add_and_is_pinned, teardown);
399 : 1 : g_test_add ("/gcr/trust/add_certificate_pinned_fail", Test, NULL, setup, test_add_certificate_pinned_fail, teardown);
400 : 1 : g_test_add ("/gcr/trust/add_and_remov_pinned", Test, NULL, setup, test_add_and_remov_pinned, teardown);
401 : 1 : g_test_add ("/gcr/trust/add_and_is_pinned_async", Test, NULL, setup, test_add_and_is_pinned_async, teardown);
402 : 1 : g_test_add ("/gcr/trust/add_and_remov_pinned_async", Test, NULL, setup, test_add_and_remov_pinned_async, teardown);
403 : 1 : g_test_add ("/gcr/trust/is_certificate_anchored_not", Test, NULL, setup, test_is_certificate_anchored_not, teardown);
404 : 1 : g_test_add ("/gcr/trust/is_certificate_anchored_yes", Test, NULL, setup, test_is_certificate_anchored_yes, teardown);
405 : 1 : g_test_add ("/gcr/trust/is_certificate_anchored_async", Test, NULL, setup, test_is_certificate_anchored_async, teardown);
406 : 1 : g_test_add ("/gcr/trust/is_certificate_distrusted_not", Test, NULL, setup, test_is_certificate_distrusted_not, teardown);
407 : 1 : g_test_add ("/gcr/trust/is_certificate_distrusted_yes", Test, NULL, setup, test_is_certificate_distrusted_yes, teardown);
408 : 1 : g_test_add ("/gcr/trust/is_certificate_distrusted_async", Test, NULL, setup, test_is_certificate_distrusted_async, teardown);
409 : :
410 : 1 : return egg_tests_run_with_loop ();
411 : : }
|