Debian Codegraph @

tests/unittests/torture_sk_usbhid.c

raw
1 /*
2 * torture_sk_usbhid.c - Torture tests for security key USB-HID
3 * callbacks.
4 *
5 * This file is part of the SSH Library
6 *
7 * Copyright (c) 2025 Praneeth Sarode <[email protected]>
8 *
9 * The SSH Library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or (at your
12 * option) any later version.
13 *
14 * The SSH Library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 * License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with the SSH Library; see the file COPYING. If not, write to
21 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * MA 02111-1307, USA.
23 */
24
25 #include "config.h"
26
27 #define LIBSSH_STATIC
28
29 #include "libssh/sk_common.h"
30 #include "torture.h"
31 #include "torture_sk.h"
32
33 /**
34 * These tests require at least one FIDO2 device to be connected
35 * and the environment variables TORTURE_SK_USBHID and TORTURE_SK_PIN to be set.
36 *
37 * If TORTURE_SK_USBHID is not set, these tests will be skipped.
38 * To enable these tests, set both environment variables before running:
39 *
40 * export TORTURE_SK_USBHID=1
41 * export TORTURE_SK_PIN=your_device_pin
42 *
43 * The TORTURE_SK_PIN environment variable should contain the PIN used to
44 * unlock the FIDO2 device for operations.
45 *
46 * Note that these tests must be run in the order that they are defined in, as
47 * the signing tests rely on the output of the enrollment tests.
48 */
49
50 static const char *test_pin = NULL;
51 static const char *test_application = "ssh:[email protected]";
52
53 static const uint8_t dummy_data[] = {
54 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
55 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
56 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20};
57
58 /* Global variables to store key handles for signing tests */
59 static uint8_t *ecdsa_key_handle = NULL;
60 static size_t ecdsa_key_handle_len = 0;
61 static uint8_t *ed25519_key_handle = NULL;
62 static size_t ed25519_key_handle_len = 0;
63
64 /* Check if tests should run */
65 static bool should_run_tests(void)
66 {
67 char *env = getenv("TORTURE_SK_USBHID");
68 return (env != NULL && env[0] != '\0');
69 }
70
71 static struct sk_option **create_user_id_option(const char *user_id)
72 {
73 struct sk_option **array = NULL, *option = NULL;
74
75 array = calloc(2, sizeof(struct sk_option *));
76 assert_non_null(array);
77
78 option = calloc(1, sizeof(struct sk_option));
79 assert_non_null(option);
80
81 option->name = strdup(SSH_SK_OPTION_NAME_USER_ID);
82 assert_non_null(option->name);
83 option->value = strdup(user_id);
84 assert_non_null(option->value);
85 option->required = 0;
86
87 array[0] = option;
88 array[1] = NULL;
89
90 return array;
91 }
92
93 static void torture_sk_usbhid_enroll_generic_key(enum ssh_keytypes_e key_type)
94 {
95 const struct ssh_sk_callbacks_struct *callbacks = NULL;
96 struct sk_enroll_response *response = NULL;
97 struct sk_option **options = NULL;
98 const char *user_id = NULL;
99 uint8_t **key_handle_out = NULL;
100 size_t *key_handle_len_out = NULL;
101 int rc, flags;
102
103 callbacks = ssh_sk_get_default_callbacks();
104 assert_non_null(callbacks);
105 assert_true(ssh_callbacks_exists(callbacks, enroll));
106
107 /* Setup based on key type */
108 switch (key_type) {
109 case SSH_SK_ECDSA:
110 user_id = "libssh_test_ecdsa_sk";
111 key_handle_out = &ecdsa_key_handle;
112 key_handle_len_out = &ecdsa_key_handle_len;
113 break;
114 case SSH_SK_ED25519:
115 user_id = "libssh_test_ed25519_sk";
116 key_handle_out = &ed25519_key_handle;
117 key_handle_len_out = &ed25519_key_handle_len;
118 break;
119 default:
120 /* Should never reach here */
121 assert_true(0);
122 return;
123 }
124
125 options = create_user_id_option(user_id);
126
127 /* Enroll non-resident key */
128 flags = SSH_SK_USER_PRESENCE_REQD;
129 rc = callbacks->enroll(key_type,
130 dummy_data,
131 sizeof(dummy_data),
132 test_application,
133 flags,
134 test_pin,
135 options,
136 &response);
137 assert_int_equal(rc, SSH_OK);
138 assert_sk_enroll_response(response, flags);
139
140 /* Store the non-resident key handle for signing tests */
141 *key_handle_out = calloc(response->key_handle_len, 1);
142 assert_non_null(*key_handle_out);
143 memcpy(*key_handle_out, response->key_handle, response->key_handle_len);
144 *key_handle_len_out = response->key_handle_len;
145
146 SK_ENROLL_RESPONSE_FREE(response);
147 SK_OPTIONS_FREE(options);
148 }
149
150 static void
151 torture_sk_usbhid_enroll_generic_resident_key(enum ssh_keytypes_e key_type)
152 {
153 const struct ssh_sk_callbacks_struct *callbacks = NULL;
154 struct sk_enroll_response *response = NULL;
155 struct sk_option **options = NULL;
156 const char *user_id = NULL;
157 int rc, flags;
158
159 callbacks = ssh_sk_get_default_callbacks();
160 assert_non_null(callbacks);
161 assert_true(ssh_callbacks_exists(callbacks, enroll));
162
163 /* Setup based on key type */
164 switch (key_type) {
165 case SSH_SK_ECDSA:
166 user_id = "libssh_test_ecdsa_sk";
167 break;
168 case SSH_SK_ED25519:
169 user_id = "libssh_test_ed25519_sk";
170 break;
171 default:
172 /* Should never reach here */
173 assert_true(0);
174 return;
175 }
176
177 options = create_user_id_option(user_id);
178
179 /* Enroll first resident key */
180 flags = SSH_SK_USER_PRESENCE_REQD | SSH_SK_RESIDENT_KEY |
181 SSH_SK_FORCE_OPERATION;
182 rc = callbacks->enroll(key_type,
183 dummy_data,
184 sizeof(dummy_data),
185 test_application,
186 flags,
187 test_pin,
188 options,
189 &response);
190 assert_int_equal(rc, SSH_OK);
191 assert_sk_enroll_response(response, flags);
192 SK_ENROLL_RESPONSE_FREE(response);
193
194 /* Try to enroll same resident key again - should fail with
195 * SSH_SK_ERR_CREDENTIAL_EXISTS */
196 flags = SSH_SK_USER_PRESENCE_REQD | SSH_SK_RESIDENT_KEY;
197 rc = callbacks->enroll(key_type,
198 dummy_data,
199 sizeof(dummy_data),
200 test_application,
201 flags,
202 test_pin,
203 options,
204 &response);
205 assert_int_equal(rc, SSH_SK_ERR_CREDENTIAL_EXISTS);
206 SK_ENROLL_RESPONSE_FREE(response);
207
208 /* The force operation flag should overwrite the existing resident key with
209 * new one */
210 flags = SSH_SK_USER_PRESENCE_REQD | SSH_SK_RESIDENT_KEY |
211 SSH_SK_FORCE_OPERATION;
212 rc = callbacks->enroll(key_type,
213 dummy_data,
214 sizeof(dummy_data),
215 test_application,
216 flags,
217 test_pin,
218 options,
219 &response);
220 assert_int_equal(rc, SSH_OK);
221 assert_sk_enroll_response(response, flags);
222 SK_ENROLL_RESPONSE_FREE(response);
223 SK_OPTIONS_FREE(options);
224 }
225
226 static void torture_sk_usbhid_enroll_ecdsa_key(UNUSED_PARAM(void **state))
227 {
228 torture_sk_usbhid_enroll_generic_key(SSH_SK_ECDSA);
229 }
230
231 static void torture_sk_usbhid_enroll_ed25519_key(UNUSED_PARAM(void **state))
232 {
233 torture_sk_usbhid_enroll_generic_key(SSH_SK_ED25519);
234 }
235
236 static void
237 torture_sk_usbhid_enroll_ecdsa_resident_key(UNUSED_PARAM(void **state))
238 {
239 torture_sk_usbhid_enroll_generic_resident_key(SSH_SK_ECDSA);
240 }
241
242 static void
243 torture_sk_usbhid_enroll_ed25519_resident_key(UNUSED_PARAM(void **state))
244 {
245 torture_sk_usbhid_enroll_generic_resident_key(SSH_SK_ED25519);
246 }
247
248 static void torture_sk_usbhid_sign_generic(enum ssh_keytypes_e key_type)
249 {
250 const struct ssh_sk_callbacks_struct *callbacks;
251 struct sk_sign_response *response = NULL;
252 uint8_t *key_handle = NULL;
253 size_t key_handle_len = 0;
254 int rc, flags;
255
256 /* Setup based on key type */
257 switch (key_type) {
258 case SSH_SK_ECDSA:
259 key_handle = ecdsa_key_handle;
260 key_handle_len = ecdsa_key_handle_len;
261 break;
262 case SSH_SK_ED25519:
263 key_handle = ed25519_key_handle;
264 key_handle_len = ed25519_key_handle_len;
265 break;
266 default:
267 /* Should never reach here */
268 assert_true(0);
269 return;
270 }
271
272 assert_non_null(key_handle);
273 assert_true(key_handle_len > 0);
274
275 callbacks = ssh_sk_get_default_callbacks();
276 assert_non_null(callbacks);
277 assert_true(ssh_callbacks_exists(callbacks, sign));
278
279 flags = SSH_SK_USER_PRESENCE_REQD;
280 rc = callbacks->sign(key_type,
281 dummy_data,
282 sizeof(dummy_data),
283 test_application,
284 key_handle,
285 key_handle_len,
286 flags,
287 test_pin,
288 NULL,
289 &response);
290 assert_int_equal(rc, SSH_OK);
291 assert_sk_sign_response(response, key_type);
292
293 SK_SIGN_RESPONSE_FREE(response);
294 }
295
296 static void torture_sk_usbhid_sign_ecdsa(UNUSED_PARAM(void **state))
297 {
298 torture_sk_usbhid_sign_generic(SSH_SK_ECDSA);
299 }
300
301 static void torture_sk_usbhid_sign_ed25519(UNUSED_PARAM(void **state))
302 {
303 torture_sk_usbhid_sign_generic(SSH_SK_ED25519);
304 }
305
306 static void torture_sk_usbhid_load_resident_keys(UNUSED_PARAM(void **state))
307 {
308 const struct ssh_sk_callbacks_struct *callbacks;
309 struct sk_resident_key **resident_keys = NULL;
310 size_t num_keys = 0;
311 int rc;
312
313 callbacks = ssh_sk_get_default_callbacks();
314 assert_non_null(callbacks);
315 assert_true(ssh_callbacks_exists(callbacks, load_resident_keys));
316
317 rc = callbacks->load_resident_keys(test_pin,
318 NULL,
319 &resident_keys,
320 &num_keys);
321 assert_int_equal(rc, SSH_OK);
322 assert_non_null(resident_keys);
323 assert_true(num_keys > 0);
324
325 for (size_t i = 0; i < num_keys; i++) {
326 assert_sk_resident_key(resident_keys[i]);
327 SK_RESIDENT_KEY_FREE(resident_keys[i]);
328 }
329
330 free(resident_keys);
331 }
332
333 static int setup(UNUSED_PARAM(void **state))
334 {
335 const char *test_pin_env = NULL;
336
337 test_pin_env = torture_get_sk_pin();
338 if (test_pin_env != NULL) {
339 test_pin = test_pin_env;
340 }
341
342 return 0;
343 }
344
345 static int cleanup(UNUSED_PARAM(void **state))
346 {
347 SAFE_FREE(ecdsa_key_handle);
348 SAFE_FREE(ed25519_key_handle);
349
350 return 0;
351 }
352
353 int torture_run_tests(void)
354 {
355 int rc;
356 bool should_run;
357
358 struct CMUnitTest tests[] = {
359 cmocka_unit_test(torture_sk_usbhid_enroll_ecdsa_key),
360 cmocka_unit_test(torture_sk_usbhid_enroll_ed25519_key),
361 cmocka_unit_test(torture_sk_usbhid_enroll_ecdsa_resident_key),
362 cmocka_unit_test(torture_sk_usbhid_enroll_ed25519_resident_key),
363 cmocka_unit_test(torture_sk_usbhid_sign_ecdsa),
364 cmocka_unit_test(torture_sk_usbhid_sign_ed25519),
365 cmocka_unit_test(torture_sk_usbhid_load_resident_keys),
366 };
367
368 /*
369 * Only run tests if TORTURE_SK_USBHID environment variable is set
370 * and we expect a FIDO2 device to be available.
371 */
372 should_run = should_run_tests();
373 if (!should_run) {
374 printf("Skipping sk_usbhid tests: TORTURE_SK_USBHID not set\n");
375 return 0; /* Success, but no tests run */
376 }
377
378 ssh_init();
379 rc = cmocka_run_group_tests(tests, setup, cleanup);
380 ssh_finalize();
381
382 return rc;
383 }