Debian Codegraph @

tests/unittests/torture_pki_sk.c

raw
1 /*
2 * torture_pki_sk.c - Torture tests for PKI security key functions
3 *
4 * This file is part of the SSH Library
5 *
6 * Copyright (c) 2025 Praneeth Sarode <[email protected]>
7 *
8 * The SSH Library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or (at your
11 * option) any later version.
12 *
13 * The SSH Library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 * License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with the SSH Library; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 * MA 02111-1307, USA.
22 */
23
24 #include "config.h"
25
26 #define LIBSSH_STATIC
27
28 #include "pki.c"
29 #include "sk_common.c"
30 #include "torture.h"
31 #include "torture_pki.h"
32 #include "torture_sk.h"
33
34 #include <string.h>
35
36 /**
37 * These tests can also be configured to run with the sk-usbhid callbacks
38 * instead of the default sk-dummy callbacks which can run in a CI
39 * environment.
40 *
41 * To run these tests with the sk-usbhid callbacks, at least one FIDO2 device
42 * must be connected and the environment variables TORTURE_SK_USBHID and
43 * TORTURE_SK_PIN must be set.
44 *
45 * The TORTURE_SK_PIN environment variable should contain the PIN used to
46 * unlock the FIDO2 device for operations.
47 *
48 * Note that these tests must be run in the order that they are defined in, as
49 * the signing tests rely on the output of the enrollment tests.
50 */
51
52 /* Test constants */
53
54 /* Default PIN value which will be overridden with the PIN set in the
55 * environment variable. */
56 static const char *test_pin = NULL;
57 static const char *test_application = "ssh:[email protected]";
58 static const unsigned char test_message[] = "Test signing data for SK keys";
59
60 static const char test_challenge[] = {
61 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
62 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
63 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20};
64
65 /* Global keys for testing */
66 static ssh_key g_ecdsa_key = NULL;
67 static ssh_key g_ed25519_key = NULL;
68
69 static const struct ssh_sk_callbacks_struct *g_sk_callbacks = NULL;
70 bool valid_sk_callbacks = false;
71
72 static int test_pin_callback(UNUSED_PARAM(const char *prompt),
73 char *buf,
74 size_t len,
75 UNUSED_PARAM(int echo),
76 UNUSED_PARAM(int verify),
77 UNUSED_PARAM(void *userdata))
78 {
79 size_t pin_len;
80
81 if (test_pin == NULL) {
82 return SSH_ERROR;
83 }
84
85 pin_len = strlen(test_pin);
86 if (pin_len + 1 > len) {
87 return -1; /* buffer too small */
88 }
89
90 memcpy(buf, test_pin, pin_len);
91 buf[pin_len] = '\0';
92 return SSH_OK;
93 }
94
95 static void torture_pki_sk_enroll_generic_key(enum ssh_keytypes_e key_type)
96 {
97 ssh_key pubkey = NULL, reimported_privkey = NULL, reimported_pubkey = NULL;
98 ssh_pki_ctx enroll_ctx = NULL;
99 const char *privkey_filename = NULL;
100 const char *pubkey_filename = NULL;
101 const char *test_user_id = NULL;
102 ssh_key *ptr_to_g_key = NULL;
103 ssh_auth_callback pin_callback = NULL;
104 int rc;
105
106 /* Conditions to skip the test */
107 if (!valid_sk_callbacks) {
108 skip();
109 }
110
111 if (key_type == SSH_KEYTYPE_SK_ED25519 && ssh_fips_mode()) {
112 skip();
113 }
114
115 /* Setup based on key type */
116 switch (key_type) {
117 case SSH_KEYTYPE_SK_ECDSA:
118 privkey_filename = "test_sk_ecdsa_private.key";
119 pubkey_filename = "test_sk_ecdsa_public.pub";
120 test_user_id = "libssh_test_ecdsa_sk";
121 ptr_to_g_key = &g_ecdsa_key;
122 break;
123
124 case SSH_KEYTYPE_SK_ED25519:
125 privkey_filename = "test_sk_ed25519_private.key";
126 pubkey_filename = "test_sk_ed25519_public.pub";
127 test_user_id = "libssh_test_ed25519_sk";
128 ptr_to_g_key = &g_ed25519_key;
129 break;
130
131 default:
132 /* Should never reach here */
133 assert_true(0);
134 return;
135 }
136
137 if (test_pin != NULL) {
138 pin_callback = test_pin_callback;
139 }
140
141 enroll_ctx = torture_create_sk_pki_ctx(test_application,
142 SSH_SK_USER_PRESENCE_REQD,
143 test_challenge,
144 sizeof(test_challenge),
145 pin_callback,
146 NULL,
147 test_user_id,
148 g_sk_callbacks);
149 assert_non_null(enroll_ctx);
150
151 rc = ssh_pki_generate_key(key_type, enroll_ctx, ptr_to_g_key);
152 assert_int_equal(rc, SSH_OK);
153 assert_sk_key_valid(*ptr_to_g_key, key_type, true);
154
155 /* Export private key to file */
156 rc = ssh_pki_export_privkey_file(*ptr_to_g_key,
157 NULL, /* no passphrase */
158 NULL, /* no auth callback */
159 NULL, /* no auth data */
160 privkey_filename);
161 assert_int_equal(rc, SSH_OK);
162
163 /* Extract public key from private key */
164 rc = ssh_pki_export_privkey_to_pubkey(*ptr_to_g_key, &pubkey);
165 assert_int_equal(rc, SSH_OK);
166 assert_non_null(pubkey);
167
168 /* Export public key to file */
169 rc = ssh_pki_export_pubkey_file(pubkey, pubkey_filename);
170 assert_int_equal(rc, SSH_OK);
171
172 /* Verify exported files by importing them back */
173 rc = ssh_pki_import_privkey_file(privkey_filename,
174 NULL, /* no passphrase */
175 NULL, /* no auth callback */
176 NULL, /* no auth data */
177 &reimported_privkey);
178 assert_int_equal(rc, SSH_OK);
179 assert_non_null(reimported_privkey);
180
181 rc = ssh_pki_import_pubkey_file(pubkey_filename, &reimported_pubkey);
182 assert_int_equal(rc, SSH_OK);
183 assert_non_null(reimported_pubkey);
184
185 /* Verify keys match */
186 rc = ssh_key_cmp(*ptr_to_g_key, reimported_privkey, SSH_KEY_CMP_PRIVATE);
187 assert_int_equal(rc, 0);
188
189 rc = ssh_key_cmp(pubkey, reimported_pubkey, SSH_KEY_CMP_PUBLIC);
190 assert_int_equal(rc, 0);
191
192 rc = ssh_key_cmp(*ptr_to_g_key, reimported_pubkey, SSH_KEY_CMP_PUBLIC);
193 assert_int_equal(rc, 0);
194
195 rc = ssh_key_cmp(reimported_privkey, pubkey, SSH_KEY_CMP_PUBLIC);
196 assert_int_equal(rc, 0);
197
198 /* Cleanup */
199 unlink(privkey_filename);
200 unlink(pubkey_filename);
201
202 SSH_KEY_FREE(pubkey);
203 SSH_KEY_FREE(reimported_privkey);
204 SSH_KEY_FREE(reimported_pubkey);
205 SSH_PKI_CTX_FREE(enroll_ctx);
206 }
207
208 static void torture_pki_sk_enroll_ecdsa_key(UNUSED_PARAM(void **state))
209 {
210 torture_pki_sk_enroll_generic_key(SSH_KEYTYPE_SK_ECDSA);
211 }
212
213 static void torture_pki_sk_enroll_ed25519_key(UNUSED_PARAM(void **state))
214 {
215 torture_pki_sk_enroll_generic_key(SSH_KEYTYPE_SK_ED25519);
216 }
217
218 static void
219 torture_pki_sk_enroll_generic_resident_key(enum ssh_keytypes_e key_type)
220 {
221 ssh_key resident_key = NULL;
222 ssh_pki_ctx enroll_ctx = NULL;
223 const char *test_user_id = NULL;
224 ssh_auth_callback pin_callback = NULL;
225 int rc, flags;
226
227 /* Conditions to skip the test */
228 if (!valid_sk_callbacks) {
229 skip();
230 }
231
232 if (key_type == SSH_KEYTYPE_SK_ED25519 && ssh_fips_mode()) {
233 skip();
234 }
235
236 /* Setup based on key type */
237 switch (key_type) {
238 case SSH_KEYTYPE_SK_ECDSA:
239 test_user_id = "libssh_test_ecdsa_sk";
240 break;
241
242 case SSH_KEYTYPE_SK_ED25519:
243 test_user_id = "libssh_test_ed25519_sk";
244 break;
245
246 default:
247 /* Should never reach here */
248 assert_true(0);
249 return;
250 }
251
252 flags = SSH_SK_USER_PRESENCE_REQD | SSH_SK_RESIDENT_KEY |
253 SSH_SK_FORCE_OPERATION;
254
255 if (test_pin != NULL) {
256 pin_callback = test_pin_callback;
257 }
258
259 enroll_ctx = torture_create_sk_pki_ctx(test_application,
260 flags,
261 test_challenge,
262 sizeof(test_challenge),
263 pin_callback,
264 NULL,
265 test_user_id,
266 g_sk_callbacks);
267 assert_non_null(enroll_ctx);
268
269 rc = ssh_pki_generate_key(key_type, enroll_ctx, &resident_key);
270 assert_int_equal(rc, SSH_OK);
271 assert_sk_key_valid(resident_key, key_type, true);
272
273 SSH_KEY_FREE(resident_key);
274 SSH_PKI_CTX_FREE(enroll_ctx);
275 }
276
277 static void torture_pki_sk_enroll_ecdsa_resident_key(UNUSED_PARAM(void **state))
278 {
279 torture_pki_sk_enroll_generic_resident_key(SSH_KEYTYPE_SK_ECDSA);
280 }
281
282 static void
283 torture_pki_sk_enroll_ed25519_resident_key(UNUSED_PARAM(void **state))
284 {
285 torture_pki_sk_enroll_generic_resident_key(SSH_KEYTYPE_SK_ED25519);
286 }
287
288 static void torture_pki_sk_sign_generic_key(enum ssh_keytypes_e key_type)
289 {
290 ssh_signature signature = NULL;
291 ssh_key public_key = NULL;
292 ssh_pki_ctx sign_ctx = NULL;
293 ssh_key *ptr_to_g_key = NULL;
294 ssh_auth_callback pin_callback = NULL;
295 int rc;
296
297 /* Conditions to skip the test */
298 if (!valid_sk_callbacks) {
299 skip();
300 }
301
302 if (key_type == SSH_KEYTYPE_SK_ED25519 && ssh_fips_mode()) {
303 skip();
304 }
305
306 /* Select the appropriate global key based on key type */
307 switch (key_type) {
308 case SSH_KEYTYPE_SK_ECDSA:
309 ptr_to_g_key = &g_ecdsa_key;
310 break;
311
312 case SSH_KEYTYPE_SK_ED25519:
313 ptr_to_g_key = &g_ed25519_key;
314 break;
315
316 default:
317 /* Should never reach here */
318 assert_true(0);
319 return;
320 }
321
322 assert_non_null(*ptr_to_g_key);
323
324 rc = ssh_pki_export_privkey_to_pubkey(*ptr_to_g_key, &public_key);
325 assert_int_equal(rc, SSH_OK);
326 assert_non_null(public_key);
327
328 if (test_pin != NULL) {
329 pin_callback = test_pin_callback;
330 }
331
332 sign_ctx = torture_create_sk_pki_ctx(test_application,
333 SSH_SK_USER_PRESENCE_REQD,
334 test_challenge,
335 sizeof(test_challenge),
336 pin_callback,
337 NULL,
338 NULL,
339 g_sk_callbacks);
340 assert_non_null(sign_ctx);
341
342 signature = pki_sk_do_sign(sign_ctx,
343 *ptr_to_g_key,
344 test_message,
345 sizeof(test_message) - 1);
346 assert_non_null(signature);
347 assert_sk_signature_valid(signature,
348 key_type,
349 public_key,
350 test_message,
351 sizeof(test_message) - 1);
352
353 SSH_SIGNATURE_FREE(signature);
354 SSH_KEY_FREE(public_key);
355 SSH_PKI_CTX_FREE(sign_ctx);
356 }
357
358 static void torture_pki_sk_sign_ecdsa_key(UNUSED_PARAM(void **state))
359 {
360 torture_pki_sk_sign_generic_key(SSH_KEYTYPE_SK_ECDSA);
361 }
362
363 static void torture_pki_sk_sign_ed25519_key(UNUSED_PARAM(void **state))
364 {
365 torture_pki_sk_sign_generic_key(SSH_KEYTYPE_SK_ED25519);
366 }
367
368 static void torture_pki_sk_load_resident_keys(UNUSED_PARAM(void **state))
369 {
370 ssh_pki_ctx load_ctx = NULL;
371 ssh_key *resident_keys = NULL;
372 size_t num_keys = 0;
373 size_t i;
374 int rc;
375
376 /* Conditions to skip the test */
377 if (!valid_sk_callbacks || torture_sk_is_using_sk_dummy()) {
378 skip();
379 }
380
381 load_ctx = ssh_pki_ctx_new();
382 assert_non_null(load_ctx);
383
384 assert_non_null(test_pin);
385 rc = ssh_pki_ctx_set_sk_pin_callback(load_ctx, test_pin_callback, NULL);
386 assert_int_equal(rc, SSH_OK);
387
388 if (g_sk_callbacks != NULL) {
389 rc = ssh_pki_ctx_options_set(load_ctx,
390 SSH_PKI_OPTION_SK_CALLBACKS,
391 g_sk_callbacks);
392 assert_int_equal(rc, SSH_OK);
393 }
394
395 rc = ssh_sk_resident_keys_load(load_ctx, &resident_keys, &num_keys);
396 assert_int_equal(rc, SSH_OK);
397 assert_non_null(resident_keys);
398 assert_true(num_keys > 0);
399
400 for (i = 0; i < num_keys; i++) {
401 ssh_key key = resident_keys[i];
402 assert_non_null(key);
403
404 assert_true(key->type == SSH_KEYTYPE_SK_ECDSA ||
405 key->type == SSH_KEYTYPE_SK_ED25519);
406
407 assert_true(key->sk_flags & SSH_SK_RESIDENT_KEY);
408
409 assert_true(key->sk_flags & SSH_SK_USER_PRESENCE_REQD);
410
411 if (key->type == SSH_KEYTYPE_SK_ECDSA) {
412 assert_sk_key_valid(key, SSH_KEYTYPE_SK_ECDSA, true);
413 } else if (key->type == SSH_KEYTYPE_SK_ED25519) {
414 if (!ssh_fips_mode()) {
415 assert_sk_key_valid(key, SSH_KEYTYPE_SK_ED25519, true);
416 }
417 }
418 }
419
420 for (i = 0; i < num_keys; i++) {
421 SSH_KEY_FREE(resident_keys[i]);
422 }
423 SAFE_FREE(resident_keys);
424
425 SSH_PKI_CTX_FREE(load_ctx);
426 }
427
428 static void
429 torture_pki_ctx_sk_callbacks_options_clear(UNUSED_PARAM(void **state))
430 {
431 ssh_pki_ctx ctx = NULL;
432 int rc;
433
434 /* Test with NULL context - should return SSH_ERROR */
435 rc = ssh_pki_ctx_sk_callbacks_options_clear(NULL);
436 assert_int_equal(rc, SSH_ERROR);
437
438 /* Create a new PKI context */
439 ctx = ssh_pki_ctx_new();
440 assert_non_null(ctx);
441
442 /* Test clearing options on a context with no options set - should succeed
443 */
444 rc = ssh_pki_ctx_sk_callbacks_options_clear(ctx);
445 assert_int_equal(rc, SSH_OK);
446
447 /* Add some options to the context */
448 rc = ssh_pki_ctx_sk_callbacks_option_set(ctx,
449 SSH_SK_OPTION_NAME_DEVICE_PATH,
450 "/dev/hidraw0",
451 false);
452 assert_int_equal(rc, SSH_OK);
453
454 rc = ssh_pki_ctx_sk_callbacks_option_set(ctx,
455 SSH_SK_OPTION_NAME_USER_ID,
456 "test_user",
457 true);
458 assert_int_equal(rc, SSH_OK);
459
460 /* Clear all options - should succeed */
461 rc = ssh_pki_ctx_sk_callbacks_options_clear(ctx);
462 assert_int_equal(rc, SSH_OK);
463
464 /* Verify that we can add options again after clearing */
465 rc = ssh_pki_ctx_sk_callbacks_option_set(ctx,
466 SSH_SK_OPTION_NAME_DEVICE_PATH,
467 "/dev/hidraw1",
468 false);
469 assert_int_equal(rc, SSH_OK);
470
471 /* Clear options again */
472 rc = ssh_pki_ctx_sk_callbacks_options_clear(ctx);
473 assert_int_equal(rc, SSH_OK);
474
475 /* Test multiple clears on same context - should succeed */
476 rc = ssh_pki_ctx_sk_callbacks_options_clear(ctx);
477 assert_int_equal(rc, SSH_OK);
478
479 SSH_PKI_CTX_FREE(ctx);
480 }
481
482 /* Setup function to run before all tests */
483 static int setup_global_state(UNUSED_PARAM(void **state))
484 {
485 const struct ssh_sk_callbacks_struct *sk_callbacks = NULL;
486 const char *test_pin_env = NULL;
487
488 sk_callbacks = torture_get_sk_callbacks();
489 if (sk_callbacks != NULL) {
490 g_sk_callbacks = sk_callbacks;
491 valid_sk_callbacks = true;
492 }
493
494 test_pin_env = torture_get_sk_pin();
495 if (test_pin_env != NULL) {
496 test_pin = test_pin_env;
497 }
498
499 return 0;
500 }
501
502 /* Teardown function to run after all tests */
503 static int teardown_global_state(UNUSED_PARAM(void **state))
504 {
505
506 /* Clean up global keys */
507 SSH_KEY_FREE(g_ecdsa_key);
508 SSH_KEY_FREE(g_ed25519_key);
509
510 return 0;
511 }
512
513 int torture_run_tests(void)
514 {
515 int rc;
516
517 struct CMUnitTest tests[] = {
518 cmocka_unit_test(torture_pki_sk_enroll_ecdsa_key),
519 cmocka_unit_test(torture_pki_sk_enroll_ed25519_key),
520 cmocka_unit_test(torture_pki_sk_enroll_ecdsa_resident_key),
521 cmocka_unit_test(torture_pki_sk_enroll_ed25519_resident_key),
522 cmocka_unit_test(torture_pki_sk_sign_ecdsa_key),
523 cmocka_unit_test(torture_pki_sk_sign_ed25519_key),
524 cmocka_unit_test(torture_pki_sk_load_resident_keys),
525 cmocka_unit_test(torture_pki_ctx_sk_callbacks_options_clear),
526 };
527
528 ssh_init();
529 torture_filter_tests(tests);
530 rc = cmocka_run_group_tests(tests,
531 setup_global_state,
532 teardown_global_state);
533 ssh_finalize();
534
535 return rc;
536 }