Debian Codegraph @

pkcs11/wrap-layer/test-login-auto.c

raw
1 /*
2 * gnome-keyring
3 *
4 * Copyright (C) 2010 Stefan Walter
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * 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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see
18 * <http://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #include "wrap-layer/gkm-wrap-layer.h"
24 #include "wrap-layer/gkm-wrap-prompt.h"
25
26 #include "egg/egg-testing.h"
27
28 #include "gkm/gkm-mock.h"
29 #include "gkm/gkm-test.h"
30
31 #include <gcr/gcr-base.h>
32
33 #include <glib-object.h>
34
35 #include <string.h>
36
37 extern CK_FUNCTION_LIST mock_secret_store;
38
39 typedef struct {
40 CK_FUNCTION_LIST functions;
41 CK_FUNCTION_LIST_PTR module;
42 CK_SESSION_HANDLE session;
43 CK_OBJECT_HANDLE key;
44 CK_OBJECT_HANDLE collection;
45 CK_MECHANISM mech;
46 } Test;
47
48 static void
49 setup (Test *test, gconstpointer unused)
50 {
51 CK_SLOT_ID slot_id;
52 CK_ULONG n_slots = 1;
53 CK_ULONG count;
54 const gchar *prompter;
55 CK_RV rv;
56
57 CK_BBOOL always = TRUE;
58 CK_ATTRIBUTE kattrs[] = {
59 { CKA_ALWAYS_AUTHENTICATE, &always, sizeof (always) }
60 };
61
62 CK_OBJECT_CLASS fklass = CKO_G_COLLECTION;
63 CK_ATTRIBUTE fattrs[] = {
64 { CKA_CLASS, &fklass, sizeof (fklass) },
65 { CKA_ID, "other", 5 },
66 };
67
68 test->mech.mechanism = CKM_MOCK_PREFIX;
69
70 /* Always start off with test test->functions */
71 memcpy (&test->functions, &mock_secret_store, sizeof (test->functions));
72
73 gkm_wrap_layer_reset_modules ();
74 gkm_wrap_layer_add_module (&test->functions);
75 test->module = gkm_wrap_layer_get_functions ();
76
77 prompter = gcr_mock_prompter_start ();
78 gkm_wrap_prompt_set_prompter_name (prompter);
79
80 /* Open a test->session */
81 rv = (test->module->C_Initialize) (NULL);
82 gkm_assert_cmprv (rv, ==, CKR_OK);
83
84 rv = (test->module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots);
85 gkm_assert_cmprv (rv, ==, CKR_OK);
86
87 rv = (test->module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &test->session);
88 gkm_assert_cmprv (rv, ==, CKR_OK);
89
90 /* Find keyring object */
91 rv = (test->module->C_FindObjectsInit) (test->session, fattrs, 1);
92 gkm_assert_cmprv (rv, ==, CKR_OK);
93 rv = (test->module->C_FindObjects) (test->session, &test->collection, 1, &count);
94 gkm_assert_cmprv (rv, ==, CKR_OK);
95 gkm_assert_cmpulong (count, ==, 1);
96 gkm_assert_cmpulong (test->collection, !=, 0);
97 rv = (test->module->C_FindObjectsFinal) (test->session);
98 gkm_assert_cmprv (rv, ==, CKR_OK);
99
100 /* Find the test->key object */
101 rv = (test->module->C_FindObjectsInit) (test->session, kattrs, 1);
102 gkm_assert_cmprv (rv, ==, CKR_OK);
103 rv = (test->module->C_FindObjects) (test->session, &test->key, 1, &count);
104 gkm_assert_cmprv (rv, ==, CKR_OK);
105 gkm_assert_cmpulong (count, ==, 1);
106 gkm_assert_cmpulong (test->key, !=, 0);
107 rv = (test->module->C_FindObjectsFinal) (test->session);
108 gkm_assert_cmprv (rv, ==, CKR_OK);
109
110 /* Start a signing operation, that needs to be authenticated */
111 rv = (test->module->C_SignInit) (test->session, &test->mech, test->key);
112 gkm_assert_cmprv (rv, ==, CKR_OK);
113 }
114
115 static void
116 teardown (Test *test, gconstpointer unused)
117 {
118 CK_RV rv;
119
120 g_assert (!gcr_mock_prompter_is_expecting ());
121 gcr_mock_prompter_stop ();
122
123 test->key = 0;
124 test->collection = 0;
125
126 rv = (test->module->C_CloseSession) (test->session);
127 gkm_assert_cmprv (rv, ==, CKR_OK);
128 test->session = 0;
129
130 rv = (test->module->C_Finalize) (NULL);
131 gkm_assert_cmprv (rv, ==, CKR_OK);
132 test->module = NULL;
133 }
134
135 static void
136 test_specific (Test *test, gconstpointer unused)
137 {
138 CK_RV rv;
139
140 /* Login with prompt */
141 gcr_mock_prompter_expect_password_ok ("booo",
142 "choice-label", "Automatically unlock this key whenever I’m logged in",
143 "choice-chosen", TRUE,
144 NULL);
145
146 rv = (test->module->C_Login) (test->session, CKU_CONTEXT_SPECIFIC, NULL, 0);
147 gkm_assert_cmprv (rv, ==, CKR_OK);
148
149 /* Start a signing operation, that needs to be authenticated */
150 rv = (test->module->C_SignInit) (test->session, &test->mech, test->key);
151 gkm_assert_cmprv (rv, ==, CKR_OK);
152
153 /* No further prompting should be shown, uses stored password */
154 g_assert (!gcr_mock_prompter_is_expecting ());
155
156 rv = (test->module->C_Login) (test->session, CKU_CONTEXT_SPECIFIC, NULL, 0);
157 gkm_assert_cmprv (rv, ==, CKR_OK);
158
159 /* Change the password */
160 gkm_mock_module_set_pin ("other");
161
162 /* Start a signing operation, that needs to be authenticated */
163 rv = (test->module->C_SignInit) (test->session, &test->mech, test->key);
164 gkm_assert_cmprv (rv, ==, CKR_OK);
165
166 /* This should prompt again, as stored password is now wrong */
167 gcr_mock_prompter_expect_password_ok ("other", NULL);
168
169 rv = (test->module->C_Login) (test->session, CKU_CONTEXT_SPECIFIC, NULL, 0);
170 gkm_assert_cmprv (rv, ==, CKR_OK);
171 }
172
173 static void
174 test_user_token (Test *test, gconstpointer unused)
175 {
176 CK_RV rv;
177
178 /* Login with prompt */
179 gcr_mock_prompter_expect_password_ok ("booo",
180 "choice-label", "Automatically unlock whenever I’m logged in",
181 "choice-chosen", TRUE,
182 NULL);
183
184 rv = (test->module->C_Login) (test->session, CKU_USER, NULL, 0);
185 gkm_assert_cmprv (rv, ==, CKR_OK);
186 rv = (test->module->C_Logout) (test->session);
187 gkm_assert_cmprv (rv, ==, CKR_OK);
188
189 /* No further prompting should be shown, uses stored password */
190 g_assert (!gcr_mock_prompter_is_expecting ());
191
192 rv = (test->module->C_Login) (test->session, CKU_USER, NULL, 0);
193 gkm_assert_cmprv (rv, ==, CKR_OK);
194 rv = (test->module->C_Logout) (test->session);
195 gkm_assert_cmprv (rv, ==, CKR_OK);
196
197 /* Change the password */
198 gkm_mock_module_set_pin ("other");
199
200 /* This should prompt again, as stored password is now wrong */
201 gcr_mock_prompter_expect_password_ok ("other", NULL);
202
203 rv = (test->module->C_Login) (test->session, CKU_USER, NULL, 0);
204 gkm_assert_cmprv (rv, ==, CKR_OK);
205 }
206
207 static void
208 test_unlock_keyring (Test *test, gconstpointer unused)
209 {
210 CK_OBJECT_HANDLE credential;
211 CK_RV rv;
212
213 CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL;
214 CK_ATTRIBUTE attrs[] = {
215 { CKA_CLASS, &klass, sizeof (klass) },
216 { CKA_VALUE, NULL, 0 },
217 { CKA_G_OBJECT, &test->collection, sizeof (test->collection) },
218 };
219
220 /* Create credential with prompt */
221 gcr_mock_prompter_expect_password_ok ("booo",
222 "choice-label", "Automatically unlock this keyring whenever I’m logged in",
223 "choice-chosen", TRUE,
224 NULL);
225
226 rv = (test->module->C_CreateObject) (test->session, attrs, G_N_ELEMENTS (attrs), &credential);
227 gkm_assert_cmprv (rv, ==, CKR_OK);
228 rv = (test->module->C_DestroyObject) (test->session, credential);
229 gkm_assert_cmprv (rv, ==, CKR_OK);
230
231 /* No further prompting should be shown, uses stored password */
232 g_assert (!gcr_mock_prompter_is_expecting ());
233
234 rv = (test->module->C_CreateObject) (test->session, attrs, G_N_ELEMENTS (attrs), &credential);
235 gkm_assert_cmprv (rv, ==, CKR_OK);
236
237 /* Change the password */
238 gkm_mock_module_set_pin ("other");
239
240 /* This should prompt again, as stored password is now wrong */
241 gcr_mock_prompter_expect_password_ok ("other", NULL);
242
243 rv = (test->module->C_CreateObject) (test->session, attrs, G_N_ELEMENTS (attrs), &credential);
244 gkm_assert_cmprv (rv, ==, CKR_OK);
245 }
246
247 int
248 main (int argc, char **argv)
249 {
250 #if !GLIB_CHECK_VERSION(2,35,0)
251 g_type_init ();
252 #endif
253 g_test_init (&argc, &argv, NULL);
254
255 g_test_add ("/wrap-layer/login-auto/specific", Test, NULL, setup, test_specific, teardown);
256 g_test_add ("/wrap-layer/login-auto/user_token", Test, NULL, setup, test_user_token, teardown);
257 g_test_add ("/wrap-layer/login-auto/unlock_keyring", Test, NULL, setup, test_unlock_keyring, teardown);
258
259 return egg_tests_run_in_thread_with_loop ();
260 }