Debian Codegraph @

tests/unittests/torture_pki_sshsig.c

raw
1 #include "config.h"
2
3 #define LIBSSH_STATIC
4
5 #include "libssh/pki.h"
6 #include "pki.c"
7 #include "torture.h"
8 #include "torture_key.h"
9 #include "torture_pki.h"
10
11 #ifdef WITH_FIDO2
12 #include "libssh/libssh.h"
13 #include "torture_sk.h"
14 #endif
15
16 #include <fcntl.h>
17 #include <sys/stat.h>
18 #include <sys/wait.h>
19
20 /**
21 * The tests for the sk-type keys can also be configured to run with
22 * the sk-usbhid callbacks instead of the default sk-dummy callbacks which can
23 * run in a CI environment.
24 *
25 * To run these tests with the sk-usbhid callbacks, at least one FIDO2 device
26 * must be connected and the environment variables TORTURE_SK_USBHID must be
27 * set.
28 */
29
30 static const char template[] = "tmp_XXXXXX";
31 static const char input[] = "Test input\0string with null byte";
32 static const size_t input_len = sizeof(input) - 1; /* -1 to exclude final \0 */
33 static const char *test_namespace = "file";
34
35 struct key_hash_combo {
36 enum ssh_keytypes_e key_type;
37 enum sshsig_digest_e hash_alg;
38 const char *key_name;
39 };
40
41 struct sshsig_st {
42 /*
43 * The original current working directory at the start of the test.
44 *
45 * During setup, the current working directory is changed to a newly
46 * created temporary directory (temp_dir).
47 *
48 * During cleanup, the current working directory is restored back
49 * to original_cwd.
50 */
51 char *original_cwd;
52 char *temp_dir;
53 ssh_key rsa_key;
54 ssh_key ed25519_key;
55 ssh_key ecdsa_key;
56
57 #ifdef WITH_FIDO2
58 ssh_pki_ctx pki_ctx;
59 ssh_key sk_ecdsa_key;
60 ssh_key sk_ed25519_key;
61 #endif
62
63 const char *ssh_keygen_path;
64 const struct key_hash_combo *test_combinations;
65 size_t num_combinations;
66 };
67
68 static struct key_hash_combo test_combinations[] = {
69 {SSH_KEYTYPE_RSA, SSHSIG_DIGEST_SHA2_256, "rsa"},
70 {SSH_KEYTYPE_RSA, SSHSIG_DIGEST_SHA2_512, "rsa"},
71 {SSH_KEYTYPE_ED25519, SSHSIG_DIGEST_SHA2_256, "ed25519"},
72 {SSH_KEYTYPE_ED25519, SSHSIG_DIGEST_SHA2_512, "ed25519"},
73 #ifdef HAVE_ECC
74 {SSH_KEYTYPE_ECDSA_P256, SSHSIG_DIGEST_SHA2_256, "ecdsa"},
75 {SSH_KEYTYPE_ECDSA_P256, SSHSIG_DIGEST_SHA2_512, "ecdsa"},
76 # ifdef WITH_FIDO2
77 {SSH_KEYTYPE_SK_ECDSA, SSHSIG_DIGEST_SHA2_256, "sk_ecdsa"},
78 {SSH_KEYTYPE_SK_ECDSA, SSHSIG_DIGEST_SHA2_512, "sk_ecdsa"},
79 # endif /* WITH_FIDO2 */
80 #endif /* HAVE_ECC */
81
82 #ifdef WITH_FIDO2
83 {SSH_KEYTYPE_SK_ED25519, SSHSIG_DIGEST_SHA2_256, "sk_ed25519"},
84 {SSH_KEYTYPE_SK_ED25519, SSHSIG_DIGEST_SHA2_512, "sk_ed25519"},
85 #endif
86 };
87
88 static ssh_key get_test_key(struct sshsig_st *test_state,
89 enum ssh_keytypes_e type)
90 {
91 switch (type) {
92 case SSH_KEYTYPE_RSA:
93 return test_state->rsa_key;
94 case SSH_KEYTYPE_ED25519:
95 if (ssh_fips_mode()) {
96 return NULL;
97 } else {
98 return test_state->ed25519_key;
99 }
100 #ifdef HAVE_ECC
101 case SSH_KEYTYPE_ECDSA_P256:
102 return test_state->ecdsa_key;
103 # ifdef WITH_FIDO2
104 case SSH_KEYTYPE_SK_ECDSA:
105 return test_state->sk_ecdsa_key;
106 # endif /* WITH_FIDO2 */
107 #endif /* HAVE_ECC */
108
109 #ifdef WITH_FIDO2
110 case SSH_KEYTYPE_SK_ED25519:
111 if (ssh_fips_mode()) {
112 return NULL;
113 } else {
114 return test_state->sk_ed25519_key;
115 }
116 #endif
117 default:
118 return NULL;
119 }
120 }
121
122 static int setup_sshsig_compat(void **state)
123 {
124 struct sshsig_st *test_state = NULL;
125 char *original_cwd = NULL;
126 char *temp_dir = NULL;
127 int rc = 0;
128
129 #ifdef WITH_FIDO2
130 const struct ssh_sk_callbacks_struct *sk_callbacks = NULL;
131 #endif
132
133 test_state = calloc(1, sizeof(struct sshsig_st));
134 assert_non_null(test_state);
135
136 original_cwd = torture_get_current_working_dir();
137 assert_non_null(original_cwd);
138
139 temp_dir = torture_make_temp_dir(template);
140 assert_non_null(temp_dir);
141
142 test_state->original_cwd = original_cwd;
143 test_state->temp_dir = temp_dir;
144 test_state->test_combinations = test_combinations;
145 test_state->num_combinations =
146 sizeof(test_combinations) / sizeof(test_combinations[0]);
147
148 *state = test_state;
149
150 rc = torture_change_dir(temp_dir);
151 assert_int_equal(rc, 0);
152
153 /* Check if openssh is available and supports SSH signatures */
154 #ifdef OPENSSH_SUPPORTS_SSHSIG
155 test_state->ssh_keygen_path = SSH_KEYGEN_EXECUTABLE;
156 #else
157 test_state->ssh_keygen_path = NULL;
158 printf("OpenSSH version does not support SSH signatures (requires "
159 "8.1+), skipping compatibility tests\n");
160 #endif /* OPENSSH_SUPPORTS_SSHSIG */
161
162 /* Load pre-generated test keys using torture functions */
163 rc = ssh_pki_import_privkey_base64(torture_get_testkey(SSH_KEYTYPE_RSA, 0),
164 NULL,
165 NULL,
166 NULL,
167 &test_state->rsa_key);
168 assert_int_equal(rc, SSH_OK);
169
170 /* Skip ed25519 if in FIPS mode */
171 if (!ssh_fips_mode()) {
172 /* mbedtls and libgcrypt don't fully support PKCS#8 PEM */
173 /* thus parse the key with OpenSSH */
174 rc = ssh_pki_import_privkey_base64(
175 torture_get_openssh_testkey(SSH_KEYTYPE_ED25519, 0),
176 NULL,
177 NULL,
178 NULL,
179 &test_state->ed25519_key);
180 assert_int_equal(rc, SSH_OK);
181 }
182
183 #ifdef HAVE_ECC
184 rc = ssh_pki_import_privkey_base64(
185 torture_get_testkey(SSH_KEYTYPE_ECDSA_P256, 0),
186 NULL,
187 NULL,
188 NULL,
189 &test_state->ecdsa_key);
190 assert_int_equal(rc, SSH_OK);
191 #endif
192
193 #ifdef WITH_FIDO2
194 /* Create and configure PKI context for SK operations */
195 sk_callbacks = torture_get_sk_callbacks();
196 if (sk_callbacks != NULL) {
197 test_state->pki_ctx = ssh_pki_ctx_new();
198 assert_non_null(test_state->pki_ctx);
199
200 rc = ssh_pki_ctx_options_set(test_state->pki_ctx,
201 SSH_PKI_OPTION_SK_CALLBACKS,
202 sk_callbacks);
203 assert_int_equal(rc, SSH_OK);
204
205 # ifdef HAVE_ECC
206 rc = ssh_pki_generate_key(SSH_KEYTYPE_SK_ECDSA,
207 test_state->pki_ctx,
208 &test_state->sk_ecdsa_key);
209 assert_int_equal(rc, SSH_OK);
210 # endif /* HAVE_ECC */
211
212 if (!ssh_fips_mode()) {
213 rc = ssh_pki_generate_key(SSH_KEYTYPE_SK_ED25519,
214 test_state->pki_ctx,
215 &test_state->sk_ed25519_key);
216 assert_int_equal(rc, SSH_OK);
217 }
218 }
219
220 #endif /* WITH_FIDO2 */
221
222 /* Write keys to files for openssh compatibility testing */
223 if (test_state->ssh_keygen_path != NULL) {
224 torture_write_file("test_rsa", torture_get_testkey(SSH_KEYTYPE_RSA, 0));
225 torture_write_file("test_rsa.pub",
226 torture_get_testkey_pub(SSH_KEYTYPE_RSA));
227
228 if (!ssh_fips_mode()) {
229 torture_write_file(
230 "test_ed25519",
231 torture_get_openssh_testkey(SSH_KEYTYPE_ED25519, 0));
232 torture_write_file("test_ed25519.pub",
233 torture_get_testkey_pub(SSH_KEYTYPE_ED25519));
234 }
235
236 #ifdef HAVE_ECC
237 torture_write_file("test_ecdsa",
238 torture_get_testkey(SSH_KEYTYPE_ECDSA_P256, 0));
239 torture_write_file("test_ecdsa.pub",
240 torture_get_testkey_pub(SSH_KEYTYPE_ECDSA_P256));
241 #endif /* HAVE_ECC */
242
243 #ifdef WITH_FIDO2
244 # ifdef HAVE_ECC
245 /* Write SK keys to files if they were successfully generated */
246 if (test_state->sk_ecdsa_key != NULL) {
247 char *sk_ecdsa_priv = NULL;
248 char *sk_ecdsa_pub = NULL;
249
250 rc = ssh_pki_export_privkey_base64(test_state->sk_ecdsa_key,
251 NULL,
252 NULL,
253 NULL,
254 &sk_ecdsa_priv);
255 assert_int_equal(rc, SSH_OK);
256
257 rc = ssh_pki_export_pubkey_base64(test_state->sk_ecdsa_key,
258 &sk_ecdsa_pub);
259 assert_int_equal(rc, SSH_OK);
260
261 torture_write_file("test_sk_ecdsa", sk_ecdsa_priv);
262 torture_write_file("test_sk_ecdsa.pub", sk_ecdsa_pub);
263
264 SAFE_FREE(sk_ecdsa_priv);
265 SAFE_FREE(sk_ecdsa_pub);
266 }
267 # endif /* HAVE_ECC */
268
269 if (!ssh_fips_mode() && test_state->sk_ed25519_key != NULL) {
270 char *sk_ed25519_priv = NULL;
271 char *sk_ed25519_pub = NULL;
272
273 rc = ssh_pki_export_privkey_base64(test_state->sk_ed25519_key,
274 NULL,
275 NULL,
276 NULL,
277 &sk_ed25519_priv);
278 assert_int_equal(rc, SSH_OK);
279
280 rc = ssh_pki_export_pubkey_base64(test_state->sk_ed25519_key,
281 &sk_ed25519_pub);
282 assert_int_equal(rc, SSH_OK);
283
284 torture_write_file("test_sk_ed25519", sk_ed25519_priv);
285 torture_write_file("test_sk_ed25519.pub", sk_ed25519_pub);
286
287 SAFE_FREE(sk_ed25519_priv);
288 SAFE_FREE(sk_ed25519_pub);
289 }
290 #endif /* WITH_FIDO2 */
291
292 rc = chmod("test_rsa", 0600);
293 assert_return_code(rc, errno);
294 if (!ssh_fips_mode()) {
295 rc = chmod("test_ed25519", 0600);
296 assert_return_code(rc, errno);
297 }
298 #ifdef HAVE_ECC
299 rc = chmod("test_ecdsa", 0600);
300 assert_return_code(rc, errno);
301 #endif
302
303 #ifdef WITH_FIDO2
304 /* Set permissions for SK key files */
305 # ifdef HAVE_ECC
306 if (test_state->sk_ecdsa_key != NULL) {
307 rc = chmod("test_sk_ecdsa", 0600);
308 assert_return_code(rc, errno);
309 }
310 # endif /* HAVE_ECC */
311 if (!ssh_fips_mode() && test_state->sk_ed25519_key != NULL) {
312 rc = chmod("test_sk_ed25519", 0600);
313 assert_return_code(rc, errno);
314 }
315 #endif /* WITH_FIDO2 */
316 }
317
318 return 0;
319 }
320
321 static int teardown_sshsig_compat(void **state)
322 {
323 struct sshsig_st *test_state = *state;
324 int rc = 0;
325
326 assert_non_null(test_state);
327
328 ssh_key_free(test_state->rsa_key);
329 ssh_key_free(test_state->ed25519_key);
330 ssh_key_free(test_state->ecdsa_key);
331
332 #ifdef WITH_FIDO2
333 SSH_PKI_CTX_FREE(test_state->pki_ctx);
334 ssh_key_free(test_state->sk_ecdsa_key);
335 ssh_key_free(test_state->sk_ed25519_key);
336 #endif
337
338 rc = torture_change_dir(test_state->original_cwd);
339 assert_int_equal(rc, 0);
340
341 rc = torture_rmdirs(test_state->temp_dir);
342 assert_int_equal(rc, 0);
343
344 SAFE_FREE(test_state->temp_dir);
345 SAFE_FREE(test_state->original_cwd);
346 SAFE_FREE(test_state);
347
348 return 0;
349 }
350
351 static int run_openssh_command(const char *cmd)
352 {
353 char full_cmd[2048];
354 int rc;
355
356 #if defined(WITH_FIDO2) && defined(SK_DUMMY_LIBRARY_PATH)
357 /* Set SSH_SK_PROVIDER to sk-dummy library when using sk-dummy callbacks */
358 if (torture_sk_is_using_sk_dummy()) {
359 snprintf(full_cmd,
360 sizeof(full_cmd),
361 "SSH_SK_PROVIDER=\"%s\" %s",
362 SK_DUMMY_LIBRARY_PATH,
363 cmd);
364 } else {
365 snprintf(full_cmd, sizeof(full_cmd), "%s", cmd);
366 }
367 #else
368 snprintf(full_cmd, sizeof(full_cmd), "%s", cmd);
369 #endif
370
371 rc = system(full_cmd);
372 return WIFEXITED(rc) ? WEXITSTATUS(rc) : -1;
373 }
374
375 static void torture_pki_sshsig_armor_dearmor(UNUSED_PARAM(void **state))
376 {
377 ssh_buffer test_buffer = NULL;
378 ssh_buffer dearmored_buffer = NULL;
379 char *armored_sig = NULL;
380 const char test_data[] = "test signature data";
381 int rc;
382
383 test_buffer = ssh_buffer_new();
384 assert_non_null(test_buffer);
385
386 rc = ssh_buffer_add_data(test_buffer, test_data, strlen(test_data));
387 assert_int_equal(rc, SSH_OK);
388
389 rc = sshsig_armor(test_buffer, &armored_sig);
390 assert_int_equal(rc, SSH_OK);
391 assert_non_null(armored_sig);
392
393 /* Test with NULL armored_sig */
394 rc = sshsig_armor(test_buffer, NULL);
395 assert_int_equal(rc, SSH_ERROR);
396
397 assert_non_null(strstr(armored_sig, SSHSIG_BEGIN_SIGNATURE));
398 assert_non_null(strstr(armored_sig, SSHSIG_END_SIGNATURE));
399
400 /* Test with NULL dearmored_buffer */
401 rc = sshsig_dearmor(armored_sig, NULL);
402 assert_int_equal(rc, SSH_ERROR);
403
404 rc = sshsig_dearmor(armored_sig, &dearmored_buffer);
405 assert_int_equal(rc, SSH_OK);
406 assert_non_null(dearmored_buffer);
407
408 assert_int_equal(ssh_buffer_get_len(test_buffer),
409 ssh_buffer_get_len(dearmored_buffer));
410 assert_memory_equal(ssh_buffer_get(test_buffer),
411 ssh_buffer_get(dearmored_buffer),
412 ssh_buffer_get_len(test_buffer));
413
414 ssh_buffer_free(test_buffer);
415 ssh_buffer_free(dearmored_buffer);
416 free(armored_sig);
417 }
418
419 static void torture_pki_sshsig_armor_dearmor_invalid(UNUSED_PARAM(void **state))
420 {
421 ssh_buffer dearmored_buffer = NULL;
422 char *armored_sig = NULL;
423 int rc;
424 const char *invalid_sig = "-----BEGIN INVALID SIGNATURE-----\n"
425 "data\n"
426 "-----END INVALID SIGNATURE-----\n";
427
428 const char *incomplete_sig = "-----BEGIN SSH SIGNATURE----\n"
429 "U1NIU0lH\n";
430
431 /* Test with NULL buffer */
432 rc = sshsig_armor(NULL, &armored_sig);
433 assert_int_equal(rc, SSH_ERROR);
434
435 /* Test dearmoring with invalid signature */
436 rc = sshsig_dearmor(invalid_sig, &dearmored_buffer);
437 assert_int_equal(rc, SSH_ERROR);
438
439 /* Test dearmoring with NULL input */
440 rc = sshsig_dearmor(NULL, &dearmored_buffer);
441 assert_int_equal(rc, SSH_ERROR);
442
443 /* Test dearmoring with missing end marker */
444 rc = sshsig_dearmor(incomplete_sig, &dearmored_buffer);
445 assert_int_equal(rc, SSH_ERROR);
446 }
447
448 static void test_libssh_sign_verify_combo(struct sshsig_st *test_state,
449 const struct key_hash_combo *combo)
450 {
451 char *signature = NULL;
452 ssh_key verify_key = NULL;
453 ssh_key test_key = NULL;
454 ssh_pki_ctx pki_context = NULL;
455 int rc;
456
457 if ((combo->key_type == SSH_KEYTYPE_ED25519 ||
458 combo->key_type == SSH_KEYTYPE_SK_ED25519) &&
459 ssh_fips_mode()) {
460 skip();
461 }
462
463 test_key = get_test_key(test_state, combo->key_type);
464 if (is_sk_key_type(combo->key_type) && test_key == NULL) {
465 /* Skip if SK key type is requested but SK callbacks are not available
466 */
467 skip();
468 }
469
470 assert_non_null(test_key);
471
472 #ifdef WITH_FIDO2
473 /* Use PKI context for SK keys */
474 if (is_sk_key_type(combo->key_type)) {
475 pki_context = test_state->pki_ctx;
476 }
477 #endif
478
479 rc = sshsig_sign(input,
480 input_len,
481 test_key,
482 pki_context,
483 test_namespace,
484 combo->hash_alg,
485 &signature);
486 assert_int_equal(rc, SSH_OK);
487 assert_non_null(signature);
488
489 rc =
490 sshsig_verify(input, input_len, signature, test_namespace, &verify_key);
491 assert_int_equal(rc, SSH_OK);
492 assert_non_null(verify_key);
493
494 rc = ssh_key_cmp(test_key, verify_key, SSH_KEY_CMP_PUBLIC);
495 assert_int_equal(rc, 0);
496
497 ssh_key_free(verify_key);
498 free(signature);
499 }
500
501 static void
502 test_openssh_sign_libssh_verify_combo(struct sshsig_st *test_state,
503 const struct key_hash_combo *combo)
504 {
505 char cmd[1024];
506 char *openssh_sig = NULL;
507 ssh_key verify_key = NULL;
508 ssh_key test_key = NULL;
509 FILE *fp = NULL;
510 int rc;
511
512 if ((combo->key_type == SSH_KEYTYPE_ED25519 ||
513 combo->key_type == SSH_KEYTYPE_SK_ED25519) &&
514 ssh_fips_mode()) {
515 skip();
516 }
517
518 test_key = get_test_key(test_state, combo->key_type);
519 if (is_sk_key_type(combo->key_type) && test_key == NULL) {
520 /* Skip if SK key type is requested but SK callbacks are not available
521 */
522 skip();
523 }
524
525 fp = fopen("test_message.txt", "wb");
526 assert_non_null(fp);
527 /* Write binary data including null byte */
528 rc = fwrite(input, input_len, 1, fp);
529 assert_return_code(rc, errno);
530 rc = fclose(fp);
531 assert_return_code(rc, errno);
532
533 snprintf(cmd,
534 sizeof(cmd),
535 "%s -Y sign -f test_%s -n %s test_message.txt",
536 test_state->ssh_keygen_path,
537 combo->key_name,
538 test_namespace);
539 rc = run_openssh_command(cmd);
540
541 assert_int_equal(rc, 0);
542 openssh_sig = torture_pki_read_file("test_message.txt.sig");
543 assert_non_null(openssh_sig);
544
545 rc = sshsig_verify(input,
546 input_len,
547 openssh_sig,
548 test_namespace,
549 &verify_key);
550 assert_int_equal(rc, SSH_OK);
551 assert_non_null(verify_key);
552
553 ssh_key_free(verify_key);
554 free(openssh_sig);
555 rc = unlink("test_message.txt.sig");
556 assert_return_code(rc, errno);
557 rc = unlink("test_message.txt");
558 assert_return_code(rc, errno);
559 }
560
561 static void
562 test_libssh_sign_openssh_verify_combo(struct sshsig_st *test_state,
563 const struct key_hash_combo *combo)
564 {
565 char *libssh_sig = NULL;
566 char cmd[1024];
567 FILE *fp = NULL;
568 int rc;
569 char *pubkey_b64 = NULL;
570 ssh_key test_key = NULL;
571 ssh_pki_ctx pki_context = NULL;
572
573 if ((combo->key_type == SSH_KEYTYPE_ED25519 ||
574 combo->key_type == SSH_KEYTYPE_SK_ED25519) &&
575 ssh_fips_mode()) {
576 skip();
577 }
578
579 printf("Testing key type: %s\n", combo->key_name);
580 test_key = get_test_key(test_state, combo->key_type);
581 if (is_sk_key_type(combo->key_type) && test_key == NULL) {
582 /* Skip if SK key type is requested but SK callbacks are not available
583 */
584 skip();
585 }
586 assert_non_null(test_key);
587
588 #ifdef WITH_FIDO2
589 /* Use PKI context for SK keys */
590 if (is_sk_key_type(combo->key_type)) {
591 pki_context = test_state->pki_ctx;
592 }
593 #endif
594
595 fp = fopen("test_message.txt", "wb");
596 assert_non_null(fp);
597 /* Write binary data including null byte */
598 rc = fwrite(input, input_len, 1, fp);
599 assert_return_code(rc, errno);
600 rc = fclose(fp);
601 assert_return_code(rc, errno);
602
603 rc = sshsig_sign(input,
604 input_len,
605 test_key,
606 pki_context,
607 test_namespace,
608 combo->hash_alg,
609 &libssh_sig);
610 assert_int_equal(rc, SSH_OK);
611 assert_non_null(libssh_sig);
612
613 fp = fopen("test_message.txt.sig", "w");
614 assert_non_null(fp);
615 rc = fputs(libssh_sig, fp);
616 assert_return_code(rc, errno);
617 rc = fclose(fp);
618 assert_return_code(rc, errno);
619
620 rc = ssh_pki_export_pubkey_base64(test_key, &pubkey_b64);
621 assert_int_equal(rc, SSH_OK);
622
623 fp = fopen("allowed_signers", "w");
624 assert_non_null(fp);
625 rc = fprintf(fp, "test %s %s\n", test_key->type_c, pubkey_b64);
626 assert_return_code(rc, errno);
627 rc = fclose(fp);
628 assert_return_code(rc, errno);
629
630 snprintf(cmd,
631 sizeof(cmd),
632 "%s -Y verify -f allowed_signers -I test -n %s -s "
633 "test_message.txt.sig < test_message.txt",
634 test_state->ssh_keygen_path,
635 test_namespace);
636 rc = run_openssh_command(cmd);
637 assert_int_equal(rc, 0);
638
639 free(libssh_sig);
640 free(pubkey_b64);
641 rc = unlink("test_message.txt.sig");
642 assert_return_code(rc, errno);
643 rc = unlink("allowed_signers");
644 assert_return_code(rc, errno);
645 rc = unlink("test_message.txt");
646 assert_return_code(rc, errno);
647 }
648
649 static void torture_sshsig_libssh_all_combinations(void **state)
650 {
651 struct sshsig_st *test_state = *state;
652 size_t i;
653
654 for (i = 0; i < test_state->num_combinations; i++) {
655 test_libssh_sign_verify_combo(test_state,
656 &test_state->test_combinations[i]);
657 }
658 }
659
660 static void torture_sshsig_openssh_libssh_all_combinations(void **state)
661 {
662 struct sshsig_st *test_state = *state;
663 size_t i;
664
665 if (test_state->ssh_keygen_path == NULL) {
666 skip();
667 }
668
669 for (i = 0; i < test_state->num_combinations; i++) {
670 test_openssh_sign_libssh_verify_combo(
671 test_state,
672 &test_state->test_combinations[i]);
673 }
674 }
675
676 static void torture_sshsig_libssh_openssh_all_combinations(void **state)
677 {
678 struct sshsig_st *test_state = *state;
679 size_t i;
680
681 if (test_state->ssh_keygen_path == NULL) {
682 skip();
683 }
684
685 for (i = 0; i < test_state->num_combinations; i++) {
686 test_libssh_sign_openssh_verify_combo(
687 test_state,
688 &test_state->test_combinations[i]);
689 }
690 }
691
692 static void torture_sshsig_error_cases_all_combinations(void **state)
693 {
694 struct sshsig_st *test_state = *state;
695 char *signature = NULL;
696 ssh_key verify_key = NULL;
697 int rc;
698 size_t i;
699 char tampered_data[] = "Tampered\0data";
700
701 for (i = 0; i < test_state->num_combinations; i++) {
702 const struct key_hash_combo *combo = &test_state->test_combinations[i];
703 ssh_key test_key = NULL;
704 ssh_pki_ctx pki_context = NULL;
705
706 if ((combo->key_type == SSH_KEYTYPE_ED25519 ||
707 combo->key_type == SSH_KEYTYPE_SK_ED25519) &&
708 ssh_fips_mode()) {
709 continue;
710 }
711
712 test_key = get_test_key(test_state, combo->key_type);
713 if (is_sk_key_type(combo->key_type) && test_key == NULL) {
714 /* Skip if SK key type is requested but SK callbacks are not
715 * available */
716 continue;
717 }
718 assert_non_null(test_key);
719
720 #ifdef WITH_FIDO2
721 if (is_sk_key_type(combo->key_type)) {
722 pki_context = test_state->pki_ctx;
723 }
724 #endif
725
726 rc = sshsig_sign(input,
727 input_len,
728 test_key,
729 pki_context,
730 "", /* Test empty string namespace */
731 combo->hash_alg,
732 &signature);
733 assert_int_equal(rc, SSH_ERROR);
734 assert_null(signature);
735
736 rc = sshsig_sign(input,
737 input_len,
738 test_key,
739 pki_context,
740 test_namespace,
741 combo->hash_alg,
742 &signature);
743 assert_int_equal(rc, SSH_OK);
744 assert_non_null(signature);
745
746 rc = sshsig_verify(input,
747 input_len,
748 signature,
749 "wrong_namespace",
750 &verify_key);
751 assert_int_equal(rc, SSH_ERROR);
752 assert_null(verify_key);
753
754 rc = sshsig_verify(input,
755 input_len,
756 signature,
757 "", /* Test empty string namespace */
758 &verify_key);
759 assert_int_equal(rc, SSH_ERROR);
760 assert_null(verify_key);
761
762 rc = sshsig_verify(tampered_data,
763 sizeof(tampered_data) - 1,
764 signature,
765 test_namespace,
766 &verify_key);
767 assert_int_equal(rc, SSH_ERROR);
768 assert_null(verify_key);
769
770 free(signature);
771 signature = NULL;
772 }
773
774 /* Test invalid hash algorithm */
775 rc = sshsig_sign(input,
776 input_len,
777 test_state->rsa_key,
778 NULL, /* pki_context */
779 test_namespace,
780 2,
781 &signature);
782 assert_int_equal(rc, SSH_ERROR);
783
784 /* Test NULL parameters */
785 rc = sshsig_sign(input,
786 input_len,
787 NULL,
788 NULL, /* pki_context */
789 test_namespace,
790 SSHSIG_DIGEST_SHA2_256,
791 &signature);
792 assert_int_equal(rc, SSH_ERROR);
793
794 rc =
795 sshsig_verify(input, input_len, "invalid", test_namespace, &verify_key);
796 assert_int_equal(rc, SSH_ERROR);
797 }
798
799 int torture_run_tests(void)
800 {
801 int rc;
802 struct CMUnitTest tests[] = {
803 cmocka_unit_test(torture_pki_sshsig_armor_dearmor),
804 cmocka_unit_test(torture_pki_sshsig_armor_dearmor_invalid),
805 /* Comprehensive combination tests */
806 cmocka_unit_test_setup_teardown(torture_sshsig_libssh_all_combinations,
807 setup_sshsig_compat,
808 teardown_sshsig_compat),
809 cmocka_unit_test_setup_teardown(
810 torture_sshsig_openssh_libssh_all_combinations,
811 setup_sshsig_compat,
812 teardown_sshsig_compat),
813 cmocka_unit_test_setup_teardown(
814 torture_sshsig_libssh_openssh_all_combinations,
815 setup_sshsig_compat,
816 teardown_sshsig_compat),
817
818 /* Comprehensive error case testing */
819 cmocka_unit_test_setup_teardown(
820 torture_sshsig_error_cases_all_combinations,
821 setup_sshsig_compat,
822 teardown_sshsig_compat),
823 };
824
825 ssh_init();
826 torture_filter_tests(tests);
827 rc = cmocka_run_group_tests(tests, NULL, NULL);
828 ssh_finalize();
829
830 return rc;
831 }