Debian Codegraph @

tests/unittests/torture_pki_rsa.c

raw
1
2 #include "config.h"
3 #include "libssh/libssh.h"
4
5 #define LIBSSH_STATIC
6
7 #include <sys/stat.h>
8 #include <fcntl.h>
9
10 #include "pki.c"
11 #include "torture.h"
12 #include "torture_pki.h"
13 #include "torture_key.h"
14
15 #define LIBSSH_RSA_TESTKEY "libssh_testkey.id_rsa"
16 #define LIBSSH_RSA_TESTKEY_PASSPHRASE "libssh_testkey_passphrase.id_rsa"
17
18 const char template[] = "temp_dir_XXXXXX";
19 const unsigned char INPUT[] = "1234567890123456789012345678901234567890"
20 "123456789012345678901234";
21
22 struct pki_st {
23 char *cwd;
24 char *temp_dir;
25 };
26
27 static int setup_rsa_key(void **state)
28 {
29 struct pki_st *test_state = NULL;
30 char *cwd = NULL;
31 char *tmp_dir = NULL;
32 int rc = 0;
33
34 test_state = (struct pki_st *)malloc(sizeof(struct pki_st));
35 assert_non_null(test_state);
36
37 cwd = torture_get_current_working_dir();
38 assert_non_null(cwd);
39
40 tmp_dir = torture_make_temp_dir(template);
41 assert_non_null(tmp_dir);
42
43 test_state->cwd = cwd;
44 test_state->temp_dir = tmp_dir;
45
46 *state = test_state;
47
48 rc = torture_change_dir(tmp_dir);
49 assert_int_equal(rc, 0);
50
51 printf("Changed directory to: %s\n", tmp_dir);
52
53 torture_write_file(LIBSSH_RSA_TESTKEY,
54 torture_get_testkey(SSH_KEYTYPE_RSA, 0));
55 torture_write_file(LIBSSH_RSA_TESTKEY_PASSPHRASE,
56 torture_get_testkey(SSH_KEYTYPE_RSA, 1));
57 torture_write_file(LIBSSH_RSA_TESTKEY ".pub",
58 torture_get_testkey_pub(SSH_KEYTYPE_RSA));
59 torture_write_file(LIBSSH_RSA_TESTKEY "-cert.pub",
60 torture_get_testkey_pub(SSH_KEYTYPE_RSA_CERT01));
61
62 return 0;
63 }
64
65 static int setup_openssh_rsa_key(void **state)
66 {
67 struct pki_st *test_state = NULL;
68 char *cwd = NULL;
69 char *tmp_dir = NULL;
70 int rc = 0;
71
72 test_state = (struct pki_st *)malloc(sizeof(struct pki_st));
73 assert_non_null(test_state);
74
75 cwd = torture_get_current_working_dir();
76 assert_non_null(cwd);
77
78 tmp_dir = torture_make_temp_dir(template);
79 assert_non_null(tmp_dir);
80
81 test_state->cwd = cwd;
82 test_state->temp_dir = tmp_dir;
83
84 *state = test_state;
85
86 rc = torture_change_dir(tmp_dir);
87 assert_int_equal(rc, 0);
88
89 torture_write_file(LIBSSH_RSA_TESTKEY,
90 torture_get_openssh_testkey(SSH_KEYTYPE_RSA, 0));
91 torture_write_file(LIBSSH_RSA_TESTKEY_PASSPHRASE,
92 torture_get_openssh_testkey(SSH_KEYTYPE_RSA, 1));
93 torture_write_file(LIBSSH_RSA_TESTKEY ".pub",
94 torture_get_testkey_pub(SSH_KEYTYPE_RSA));
95 torture_write_file(LIBSSH_RSA_TESTKEY "-cert.pub",
96 torture_get_testkey_pub(SSH_KEYTYPE_RSA_CERT01));
97
98 return 0;
99 }
100
101 static int teardown(void **state) {
102
103 struct pki_st *test_state = NULL;
104 int rc = 0;
105
106 test_state = *((struct pki_st **)state);
107
108 assert_non_null(test_state);
109 assert_non_null(test_state->cwd);
110 assert_non_null(test_state->temp_dir);
111
112 rc = torture_change_dir(test_state->cwd);
113 assert_int_equal(rc, 0);
114
115 rc = torture_rmdirs(test_state->temp_dir);
116 assert_int_equal(rc, 0);
117
118 SAFE_FREE(test_state->temp_dir);
119 SAFE_FREE(test_state->cwd);
120 SAFE_FREE(test_state);
121
122 return 0;
123 }
124
125 static void torture_pki_rsa_import_pubkey_file(void **state)
126 {
127 ssh_key pubkey = NULL;
128 int rc;
129
130 (void)state;
131
132 /* The key doesn't have the hostname as comment after the key */
133 rc = ssh_pki_import_pubkey_file(LIBSSH_RSA_TESTKEY ".pub", &pubkey);
134 assert_return_code(rc, errno);
135 assert_non_null(pubkey);
136
137 SSH_KEY_FREE(pubkey);
138 }
139
140 static void torture_pki_rsa_import_pubkey_from_openssh_privkey(void **state)
141 {
142 ssh_key pubkey = NULL;
143 int rc;
144
145 (void)state;
146
147 /* The key doesn't have the hostname as comment after the key */
148 rc = ssh_pki_import_pubkey_file(LIBSSH_RSA_TESTKEY_PASSPHRASE, &pubkey);
149 assert_return_code(rc, errno);
150 assert_non_null(pubkey);
151
152 SSH_KEY_FREE(pubkey);
153 }
154
155 static void torture_pki_rsa_import_privkey_base64_NULL_key(void **state)
156 {
157 int rc;
158 const char *passphrase = torture_get_testkey_passphrase();
159
160 (void) state; /* unused */
161
162 /* test if it returns -1 if key is NULL */
163 rc = ssh_pki_import_privkey_base64(torture_get_testkey(SSH_KEYTYPE_RSA, 0),
164 passphrase,
165 NULL,
166 NULL,
167 NULL);
168 assert_int_equal(rc, -1);
169
170 }
171
172 static void torture_pki_rsa_import_privkey_base64_NULL_str(void **state)
173 {
174 int rc;
175 ssh_key key = NULL;
176 const char *passphrase = torture_get_testkey_passphrase();
177
178 (void) state; /* unused */
179
180 /* test if it returns -1 if key_str is NULL */
181 rc = ssh_pki_import_privkey_base64(NULL, passphrase, NULL, NULL, &key);
182 assert_int_equal(rc, -1);
183
184 SSH_KEY_FREE(key);
185 }
186
187 static void
188 torture_pki_rsa_import_export_privkey_base64_format(void **state,
189 enum ssh_file_format_e format)
190 {
191 int rc;
192 char *key_str = NULL, *new_key_str = NULL;
193 ssh_key key = NULL, new_key = NULL;
194 const char *passphrase = torture_get_testkey_passphrase();
195 enum ssh_keytypes_e type;
196
197 (void) state; /* unused */
198
199 key_str = torture_pki_read_file(LIBSSH_RSA_TESTKEY);
200 assert_non_null(key_str);
201
202 /* Import test key */
203 rc = ssh_pki_import_privkey_base64(key_str, passphrase, NULL, NULL, &key);
204 assert_return_code(rc, errno);
205 assert_non_null(key);
206
207 type = ssh_key_type(key);
208 assert_int_equal(type, SSH_KEYTYPE_RSA);
209
210 rc = ssh_key_is_private(key);
211 assert_int_equal(rc, 1);
212
213 rc = ssh_key_is_public(key);
214 assert_int_equal(rc, 1);
215
216 /* Export */
217 rc = ssh_pki_export_privkey_base64_format(key,
218 passphrase,
219 NULL,
220 NULL,
221 &new_key_str,
222 format);
223 assert_int_equal(rc, SSH_OK);
224 assert_non_null(new_key_str);
225
226 /* and import again */
227 rc = ssh_pki_import_privkey_base64(new_key_str,
228 passphrase,
229 NULL,
230 NULL,
231 &new_key);
232 assert_int_equal(rc, 0);
233 assert_non_null(new_key);
234
235 type = ssh_key_type(new_key);
236 assert_int_equal(type, SSH_KEYTYPE_RSA);
237
238 rc = ssh_key_is_private(new_key);
239 assert_int_equal(rc, 1);
240
241 rc = ssh_key_is_public(new_key);
242 assert_int_equal(rc, 1);
243
244 rc = ssh_key_cmp(key, new_key, SSH_KEY_CMP_PRIVATE|SSH_KEY_CMP_PUBLIC);
245 assert_int_equal(rc, 0);
246
247 free(key_str);
248 free(new_key_str);
249 SSH_KEY_FREE(key);
250 SSH_KEY_FREE(new_key);
251 }
252
253 static void
254 torture_pki_rsa_import_export_privkey_base64(void **state)
255 {
256 torture_pki_rsa_import_export_privkey_base64_format(state,
257 SSH_FILE_FORMAT_DEFAULT);
258 }
259
260 static void torture_pki_rsa_import_privkey_base64_comment(void **state)
261 {
262 int rc, file_str_len;
263 const char *comment_str = "#this is line-comment\n#this is another\n";
264 char *key_str = NULL, *file_str = NULL;
265 ssh_key key = NULL;
266 const char *passphrase = torture_get_testkey_passphrase();
267 enum ssh_keytypes_e type;
268
269 (void) state; /* unused */
270
271 key_str = torture_pki_read_file(LIBSSH_RSA_TESTKEY);
272 assert_non_null(key_str);
273
274 file_str_len = strlen(comment_str) + strlen(key_str) + 1;
275 file_str = malloc(file_str_len);
276 assert_non_null(file_str);
277 rc = snprintf(file_str, file_str_len, "%s%s", comment_str, key_str);
278 assert_int_equal(rc, file_str_len - 1);
279
280 rc = ssh_pki_import_privkey_base64(file_str, passphrase, NULL, NULL, &key);
281 assert_return_code(rc, errno);
282 assert_non_null(key);
283
284 type = ssh_key_type(key);
285 assert_int_equal(type, SSH_KEYTYPE_RSA);
286
287 rc = ssh_key_is_private(key);
288 assert_int_equal(rc, 1);
289
290 rc = ssh_key_is_public(key);
291 assert_int_equal(rc, 1);
292
293 free(key_str);
294 free(file_str);
295 SSH_KEY_FREE(key);
296 }
297
298 static void torture_pki_rsa_import_privkey_base64_whitespace(void **state)
299 {
300 int rc, file_str_len;
301 const char *whitespace_str = " \n\t\t\t\t\t\n\n\n\n\n";
302 char *key_str = NULL, *file_str = NULL;
303 ssh_key key = NULL;
304 const char *passphrase = torture_get_testkey_passphrase();
305 enum ssh_keytypes_e type;
306
307 (void) state; /* unused */
308
309 key_str = torture_pki_read_file(LIBSSH_RSA_TESTKEY);
310 assert_non_null(key_str);
311
312 file_str_len = strlen(whitespace_str) + strlen(key_str) + 1;
313 file_str = malloc(file_str_len);
314 assert_non_null(file_str);
315 rc = snprintf(file_str, file_str_len, "%s%s", whitespace_str, key_str);
316 assert_int_equal(rc, file_str_len - 1);
317
318 rc = ssh_pki_import_privkey_base64(file_str, passphrase, NULL, NULL, &key);
319 assert_return_code(rc, errno);
320 assert_non_null(key);
321
322 type = ssh_key_type(key);
323 assert_int_equal(type, SSH_KEYTYPE_RSA);
324
325 rc = ssh_key_is_private(key);
326 assert_int_equal(rc, 1);
327
328 rc = ssh_key_is_public(key);
329 assert_int_equal(rc, 1);
330
331 free(key_str);
332 free(file_str);
333 SSH_KEY_FREE(key);
334 }
335
336 static void torture_pki_rsa_publickey_from_privatekey(void **state)
337 {
338 int rc;
339 ssh_key key = NULL;
340 ssh_key pubkey = NULL;
341 const char *passphrase = NULL;
342
343 (void) state; /* unused */
344
345 rc = ssh_pki_import_privkey_base64(torture_get_testkey(SSH_KEYTYPE_RSA, 0),
346 passphrase,
347 NULL,
348 NULL,
349 &key);
350 assert_return_code(rc, errno);
351 assert_non_null(key);
352
353 rc = ssh_key_is_private(key);
354 assert_int_equal(rc, 1);
355
356 rc = ssh_pki_export_privkey_to_pubkey(key, &pubkey);
357 assert_return_code(rc, errno);
358 assert_non_null(pubkey);
359
360 SSH_KEY_FREE(key);
361 SSH_KEY_FREE(pubkey);
362 }
363
364 static void torture_pki_rsa_copy_cert_to_privkey(void **state)
365 {
366 /*
367 * Tests copying a cert loaded into a public key to a private key.
368 * The function is encryption type agnostic, no need to run this against
369 * all supported key types.
370 */
371 int rc;
372 const char *passphrase = torture_get_testkey_passphrase();
373 ssh_key pubkey = NULL;
374 ssh_key privkey = NULL;
375 ssh_key cert = NULL;
376 enum ssh_keytypes_e type;
377
378 (void)state; /* unused */
379
380 /* Importing public key as cert should fail */
381 rc = ssh_pki_import_cert_file(LIBSSH_RSA_TESTKEY ".pub", &cert);
382 assert_int_equal(rc, SSH_ERROR);
383 assert_null(cert);
384
385 rc = ssh_pki_import_cert_file(LIBSSH_RSA_TESTKEY "-cert.pub", &cert);
386 assert_return_code(rc, errno);
387 assert_non_null(cert);
388
389 rc = ssh_pki_import_pubkey_file(LIBSSH_RSA_TESTKEY ".pub", &pubkey);
390 assert_return_code(rc, errno);
391 assert_non_null(pubkey);
392
393 type = ssh_key_type(cert);
394 assert_true(type == SSH_KEYTYPE_RSA_CERT01);
395
396 rc = ssh_key_is_public(cert);
397 assert_int_equal(rc, 1);
398
399 /* Import matching private key file and verify the pubkey matches */
400 rc = ssh_pki_import_privkey_base64(torture_get_testkey(SSH_KEYTYPE_RSA, 0),
401 passphrase,
402 NULL,
403 NULL,
404 &privkey);
405 assert_return_code(rc, errno);
406 assert_non_null(privkey);
407
408 type = ssh_key_type(privkey);
409 assert_true(type == SSH_KEYTYPE_RSA);
410
411 /* Basic sanity. */
412 rc = ssh_pki_copy_cert_to_privkey(NULL, privkey);
413 assert_int_equal(rc, SSH_ERROR);
414
415 rc = ssh_pki_copy_cert_to_privkey(pubkey, NULL);
416 assert_int_equal(rc, SSH_ERROR);
417
418 /* A public key doesn't have a cert, copy should fail. */
419 assert_null(pubkey->cert);
420 rc = ssh_pki_copy_cert_to_privkey(pubkey, privkey);
421 assert_int_equal(rc, SSH_ERROR);
422
423 /* Copying the cert to non-cert keys should work fine. */
424 rc = ssh_pki_copy_cert_to_privkey(cert, pubkey);
425 assert_return_code(rc, errno);
426 assert_non_null(pubkey->cert);
427 rc = ssh_pki_copy_cert_to_privkey(cert, privkey);
428 assert_return_code(rc, errno);
429 assert_non_null(privkey->cert);
430 assert_true(privkey->cert_type == SSH_KEYTYPE_RSA_CERT01);
431
432 assert_int_equal(ssh_key_cmp(privkey, cert, SSH_KEY_CMP_PUBLIC), 0);
433 assert_int_equal(ssh_key_cmp(cert, privkey, SSH_KEY_CMP_PUBLIC), 0);
434
435 /* The private key's cert is already set, another copy should fail. */
436 rc = ssh_pki_copy_cert_to_privkey(cert, privkey);
437 assert_int_equal(rc, SSH_ERROR);
438
439 SSH_KEY_FREE(privkey);
440 SSH_KEY_FREE(pubkey);
441
442 /* Generate different key and try to assign it this certificate */
443 rc = ssh_pki_generate(SSH_KEYTYPE_RSA, 2048, &privkey);
444 assert_return_code(rc, errno);
445 assert_non_null(privkey);
446 rc = ssh_pki_export_privkey_to_pubkey(privkey, &pubkey);
447 assert_return_code(rc, errno);
448 assert_non_null(pubkey);
449
450 rc = ssh_pki_copy_cert_to_privkey(cert, privkey);
451 assert_int_equal(rc, SSH_ERROR);
452 rc = ssh_pki_copy_cert_to_privkey(cert, pubkey);
453 assert_int_equal(rc, SSH_ERROR);
454
455 assert_int_equal(ssh_key_cmp(privkey, cert, SSH_KEY_CMP_PUBLIC), 1);
456 assert_int_equal(ssh_key_cmp(cert, privkey, SSH_KEY_CMP_PUBLIC), 1);
457
458 SSH_KEY_FREE(cert);
459 SSH_KEY_FREE(privkey);
460 SSH_KEY_FREE(pubkey);
461 }
462
463 static void torture_pki_rsa_import_cert_file(void **state) {
464 int rc;
465 ssh_key cert = NULL;
466 enum ssh_keytypes_e type;
467
468 (void) state; /* unused */
469
470 rc = ssh_pki_import_cert_file(LIBSSH_RSA_TESTKEY "-cert.pub", &cert);
471 assert_return_code(rc, errno);
472 assert_non_null(cert);
473
474 type = ssh_key_type(cert);
475 assert_int_equal(type, SSH_KEYTYPE_RSA_CERT01);
476
477 rc = ssh_key_is_public(cert);
478 assert_int_equal(rc, 1);
479
480 SSH_KEY_FREE(cert);
481 }
482
483 static void torture_pki_rsa_publickey_base64(void **state)
484 {
485 enum ssh_keytypes_e type;
486 char *b64_key = NULL, *key_buf = NULL, *p = NULL;
487 const char *q = NULL;
488 ssh_key key = NULL;
489 int rc;
490
491 (void) state; /* unused */
492
493 key_buf = strdup(torture_get_testkey_pub(SSH_KEYTYPE_RSA));
494 assert_non_null(key_buf);
495
496 q = p = key_buf;
497 while (p != NULL && *p != '\0' && *p != ' ') p++;
498 if (p != NULL) {
499 *p = '\0';
500 }
501
502 type = ssh_key_type_from_name(q);
503 assert_int_equal(type, SSH_KEYTYPE_RSA);
504
505 q = ++p;
506 while (p != NULL && *p != '\0' && *p != ' ') p++;
507 if (p != NULL) {
508 *p = '\0';
509 }
510
511 rc = ssh_pki_import_pubkey_base64(q, type, &key);
512 assert_return_code(rc, errno);
513 assert_non_null(key);
514
515 rc = ssh_pki_export_pubkey_base64(key, &b64_key);
516 assert_return_code(rc, errno);
517 assert_non_null(b64_key);
518
519 assert_string_equal(q, b64_key);
520
521 free(b64_key);
522 free(key_buf);
523 SSH_KEY_FREE(key);
524 }
525
526 static void torture_pki_rsa_generate_pubkey_from_privkey(void **state) {
527 char pubkey_generated[4096] = {0};
528 ssh_key privkey = NULL;
529 ssh_key pubkey = NULL;
530 int rc;
531 int len;
532
533 (void) state; /* unused */
534
535 /* remove the public key, generate it from the private key and write it. */
536 unlink(LIBSSH_RSA_TESTKEY ".pub");
537
538 rc = ssh_pki_import_privkey_file(LIBSSH_RSA_TESTKEY,
539 NULL,
540 NULL,
541 NULL,
542 &privkey);
543 assert_return_code(rc, errno);
544 assert_non_null(privkey);
545
546 rc = ssh_pki_export_privkey_to_pubkey(privkey, &pubkey);
547 assert_return_code(rc, errno);
548 assert_non_null(pubkey);
549
550 rc = ssh_pki_export_pubkey_file(pubkey, LIBSSH_RSA_TESTKEY ".pub");
551 assert_return_code(rc, errno);
552
553 rc = torture_read_one_line(LIBSSH_RSA_TESTKEY ".pub",
554 pubkey_generated,
555 sizeof(pubkey_generated));
556 assert_return_code(rc, errno);
557
558 len = torture_pubkey_len(torture_get_testkey_pub(SSH_KEYTYPE_RSA));
559 assert_memory_equal(torture_get_testkey_pub(SSH_KEYTYPE_RSA),
560 pubkey_generated,
561 len);
562
563 SSH_KEY_FREE(privkey);
564 SSH_KEY_FREE(pubkey);
565 }
566
567 static void torture_pki_rsa_duplicate_key(void **state)
568 {
569 int rc;
570 char *b64_key = NULL;
571 char *b64_key_gen = NULL;
572 ssh_key pubkey = NULL;
573 ssh_key pubkey_dup = NULL;
574 ssh_key privkey = NULL;
575 ssh_key privkey_dup = NULL;
576
577 (void) state;
578
579 rc = ssh_pki_import_pubkey_file(LIBSSH_RSA_TESTKEY ".pub", &pubkey);
580 assert_return_code(rc, errno);
581 assert_non_null(pubkey);
582
583 rc = ssh_pki_export_pubkey_base64(pubkey, &b64_key);
584 assert_return_code(rc, errno);
585 assert_non_null(b64_key);
586
587 rc = ssh_pki_import_privkey_file(LIBSSH_RSA_TESTKEY,
588 NULL,
589 NULL,
590 NULL,
591 &privkey);
592 assert_return_code(rc, errno);
593 assert_non_null(privkey);
594
595 privkey_dup = ssh_key_dup(privkey);
596 assert_non_null(privkey_dup);
597
598 rc = ssh_pki_export_privkey_to_pubkey(privkey, &pubkey_dup);
599 assert_return_code(rc, errno);
600 assert_non_null(pubkey_dup);
601
602 rc = ssh_pki_export_pubkey_base64(pubkey_dup, &b64_key_gen);
603 assert_return_code(rc, errno);
604 assert_non_null(b64_key_gen);
605
606 assert_string_equal(b64_key, b64_key_gen);
607
608 rc = ssh_key_cmp(privkey, privkey_dup, SSH_KEY_CMP_PRIVATE);
609 assert_return_code(rc, errno);
610
611 rc = ssh_key_cmp(pubkey, pubkey_dup, SSH_KEY_CMP_PUBLIC);
612 assert_return_code(rc, errno);
613
614 SSH_KEY_FREE(pubkey);
615 SSH_KEY_FREE(pubkey_dup);
616 SSH_KEY_FREE(privkey);
617 SSH_KEY_FREE(privkey_dup);
618 SSH_STRING_FREE_CHAR(b64_key);
619 SSH_STRING_FREE_CHAR(b64_key_gen);
620 }
621
622 /**
623 * @brief Test RSA key generation using the deprecated ssh_pki_generate API.
624 *
625 * This test is kept for backward compatibility testing of the legacy API.
626 * For testing the new context-based API, see torture_pki_generate_key_rsa().
627 */
628 static void torture_pki_generate_rsa_deprecated(void **state)
629 {
630 int rc;
631 ssh_key key = NULL, pubkey = NULL;
632 ssh_signature sign = NULL;
633 ssh_session session = ssh_new();
634 int verbosity = torture_libssh_verbosity();
635
636 (void) state;
637
638 ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
639
640 if (!ssh_fips_mode()) {
641 rc = ssh_pki_generate(SSH_KEYTYPE_RSA, 1024, &key);
642 assert_return_code(rc, errno);
643 assert_non_null(key);
644 rc = ssh_pki_export_privkey_to_pubkey(key, &pubkey);
645 assert_int_equal(rc, SSH_OK);
646 assert_non_null(pubkey);
647 sign = pki_do_sign(key, INPUT, sizeof(INPUT), SSH_DIGEST_SHA256);
648 assert_non_null(sign);
649 rc = ssh_pki_signature_verify(session, sign, pubkey, INPUT, sizeof(INPUT));
650 assert_return_code(rc, errno);
651 ssh_signature_free(sign);
652 SSH_KEY_FREE(key);
653 SSH_KEY_FREE(pubkey);
654 key = NULL;
655 pubkey = NULL;
656 }
657
658 rc = ssh_pki_generate(SSH_KEYTYPE_RSA, 2048, &key);
659 assert_return_code(rc, errno);
660 assert_non_null(key);
661 rc = ssh_pki_export_privkey_to_pubkey(key, &pubkey);
662 assert_int_equal(rc, SSH_OK);
663 assert_non_null(pubkey);
664 sign = pki_do_sign(key, INPUT, sizeof(INPUT), SSH_DIGEST_SHA256);
665 assert_non_null(sign);
666 rc = ssh_pki_signature_verify(session, sign, pubkey, INPUT, sizeof(INPUT));
667 assert_return_code(rc, errno);
668 ssh_signature_free(sign);
669 SSH_KEY_FREE(key);
670 SSH_KEY_FREE(pubkey);
671 key = NULL;
672 pubkey = NULL;
673
674 rc = ssh_pki_generate(SSH_KEYTYPE_RSA, 4096, &key);
675 assert_return_code(rc, errno);
676 assert_non_null(key);
677 rc = ssh_pki_export_privkey_to_pubkey(key, &pubkey);
678 assert_int_equal(rc, SSH_OK);
679 assert_non_null(pubkey);
680 sign = pki_do_sign(key, INPUT, sizeof(INPUT), SSH_DIGEST_SHA256);
681 assert_non_null(sign);
682 rc = ssh_pki_signature_verify(session, sign, pubkey, INPUT, sizeof(INPUT));
683 assert_return_code(rc, errno);
684 ssh_signature_free(sign);
685 SSH_KEY_FREE(key);
686 SSH_KEY_FREE(pubkey);
687 key = NULL;
688 pubkey = NULL;
689
690 ssh_free(session);
691 }
692
693 static void torture_pki_rsa_sha2(void **state)
694 {
695 int rc;
696 ssh_key key = NULL, cert = NULL, pubkey = NULL;
697 ssh_signature sign;
698 ssh_session session=ssh_new();
699 (void) state;
700
701 assert_non_null(session);
702
703 /* Setup */
704 rc = ssh_pki_import_privkey_file(LIBSSH_RSA_TESTKEY, NULL, NULL, NULL, &key);
705 assert_return_code(rc, errno);
706 assert_non_null(key);
707
708 rc = ssh_pki_import_cert_file(LIBSSH_RSA_TESTKEY "-cert.pub", &cert);
709 assert_return_code(rc, errno);
710 assert_non_null(cert);
711
712 /* Get the public key to verify signature */
713 rc = ssh_pki_export_privkey_to_pubkey(key, &pubkey);
714 assert_int_equal(rc, SSH_OK);
715 assert_non_null(pubkey);
716
717 if (!ssh_fips_mode()) {
718 /* Sign using old SHA1 digest */
719 sign = pki_do_sign(key, INPUT, sizeof(INPUT), SSH_DIGEST_SHA1);
720 assert_non_null(sign);
721 rc = ssh_pki_signature_verify(session, sign, pubkey, INPUT, sizeof(INPUT));
722 assert_ssh_return_code(session, rc);
723 rc = ssh_pki_signature_verify(session, sign, cert, INPUT, sizeof(INPUT));
724 assert_ssh_return_code(session, rc);
725 ssh_signature_free(sign);
726 }
727
728 /* Sign using new SHA256 digest */
729 sign = pki_do_sign(key, INPUT, sizeof(INPUT), SSH_DIGEST_SHA256);
730 assert_non_null(sign);
731 rc = ssh_pki_signature_verify(session, sign, pubkey, INPUT, sizeof(INPUT));
732 assert_ssh_return_code(session, rc);
733 rc = ssh_pki_signature_verify(session, sign, cert, INPUT, sizeof(INPUT));
734 assert_ssh_return_code(session, rc);
735 ssh_signature_free(sign);
736
737 /* Sign using rsa-sha2-512 algorithm */
738 sign = pki_do_sign(key, INPUT, sizeof(INPUT), SSH_DIGEST_SHA512);
739 assert_non_null(sign);
740 rc = ssh_pki_signature_verify(session, sign, pubkey, INPUT, sizeof(INPUT));
741 assert_ssh_return_code(session, rc);
742 rc = ssh_pki_signature_verify(session, sign, cert, INPUT, sizeof(INPUT));
743 assert_ssh_return_code(session, rc);
744 ssh_signature_free(sign);
745
746 /* Test that it fails when using DIGEST_AUTO */
747 sign = pki_do_sign(key, INPUT, sizeof(INPUT), SSH_DIGEST_AUTO);
748 assert_null(sign);
749
750 /* Test that it fails when using SHA384 */
751 sign = pki_do_sign(key, INPUT, sizeof(INPUT), SSH_DIGEST_SHA384);
752 assert_null(sign);
753
754 /* Cleanup */
755 SSH_KEY_FREE(key);
756 SSH_KEY_FREE(pubkey);
757 SSH_KEY_FREE(cert);
758 ssh_free(session);
759 }
760
761 static void torture_pki_rsa_key_size(void **state)
762 {
763 int rc;
764 ssh_key key = NULL, pubkey = NULL;
765 ssh_signature sign = NULL;
766 ssh_session session=ssh_new();
767 unsigned int length = 4096;
768
769 (void) state;
770
771 rc = ssh_pki_generate(SSH_KEYTYPE_RSA, 2048, &key);
772 assert_return_code(rc, errno);
773 assert_non_null(key);
774 rc = ssh_pki_export_privkey_to_pubkey(key, &pubkey);
775 assert_int_equal(rc, SSH_OK);
776 assert_non_null(pubkey);
777 sign = pki_do_sign(key, INPUT, sizeof(INPUT), SSH_DIGEST_SHA256);
778 assert_non_null(sign);
779 rc = ssh_pki_signature_verify(session, sign, pubkey, INPUT, sizeof(INPUT));
780 assert_ssh_return_code(session, rc);
781
782 /* Set the minimum RSA key size to 4k */
783 rc = ssh_options_set(session, SSH_OPTIONS_RSA_MIN_SIZE, &length);
784 assert_ssh_return_code(session, rc);
785
786 /* the verification should fail now */
787 rc = ssh_pki_signature_verify(session, sign, pubkey, INPUT, sizeof(INPUT));
788 assert_int_equal(rc, SSH_ERROR);
789
790 ssh_signature_free(sign);
791 SSH_KEY_FREE(key);
792 SSH_KEY_FREE(pubkey);
793 key = NULL;
794 pubkey = NULL;
795
796 ssh_free(session);
797 }
798
799 /**
800 * @brief Test RSA key generation using the new context-based
801 * ssh_pki_generate_key API.
802 *
803 * This test validates the new ssh_pki_ctx-based key generation API with
804 * both positive and negative test cases.
805 *
806 * For testing the old/deprecated API, see torture_pki_generate_rsa().
807 */
808 static void torture_pki_generate_key_rsa(void **state)
809 {
810 int rc;
811 ssh_key key = NULL, pubkey = NULL;
812 ssh_pki_ctx ctx = NULL;
813 int desired = 4096;
814 int invalid_size = 512;
815
816 (void)state;
817
818 /* Test with NULL context - should use default size */
819 rc = ssh_pki_generate_key(SSH_KEYTYPE_RSA, NULL, &key);
820 assert_int_equal(rc, SSH_OK);
821 assert_non_null(key);
822 assert_int_equal(ssh_key_type(key), SSH_KEYTYPE_RSA);
823 assert_int_equal(ssh_key_size(key), RSA_DEFAULT_KEY_SIZE);
824 SSH_KEY_FREE(key);
825
826 /* Test with NULL key pointer - should fail */
827 ctx = ssh_pki_ctx_new();
828 assert_non_null(ctx);
829
830 rc = ssh_pki_ctx_options_set(ctx, SSH_PKI_OPTION_RSA_KEY_SIZE, &desired);
831 assert_int_equal(rc, SSH_OK);
832
833 rc = ssh_pki_generate_key(SSH_KEYTYPE_RSA, ctx, NULL);
834 assert_int_equal(rc, SSH_ERROR);
835
836 /* Test with invalid RSA key size (too small) - should fail */
837 rc = ssh_pki_ctx_options_set(ctx,
838 SSH_PKI_OPTION_RSA_KEY_SIZE,
839 &invalid_size);
840 assert_int_equal(rc, SSH_ERROR);
841
842 rc = ssh_pki_ctx_options_set(ctx, SSH_PKI_OPTION_RSA_KEY_SIZE, &desired);
843 assert_int_equal(rc, SSH_OK);
844
845 rc = ssh_pki_generate_key(SSH_KEYTYPE_RSA, ctx, &key);
846 assert_int_equal(rc, SSH_OK);
847 assert_non_null(key);
848
849 assert_int_equal(ssh_key_type(key), SSH_KEYTYPE_RSA);
850 assert_int_equal(ssh_key_size(key), desired);
851
852 rc = ssh_pki_export_privkey_to_pubkey(key, &pubkey);
853 assert_int_equal(rc, SSH_OK);
854 assert_non_null(pubkey);
855
856 SSH_KEY_FREE(key);
857 SSH_KEY_FREE(pubkey);
858 SSH_PKI_CTX_FREE(ctx);
859 }
860
861 static int test_sign_verify_data(ssh_key key,
862 enum ssh_digest_e hash_type,
863 const unsigned char *input,
864 size_t input_len)
865 {
866 ssh_signature sig;
867 ssh_key pubkey = NULL;
868 int rc;
869
870 /* Get the public key to verify signature */
871 rc = ssh_pki_export_privkey_to_pubkey(key, &pubkey);
872 assert_int_equal(rc, SSH_OK);
873 assert_non_null(pubkey);
874
875 /* Sign the buffer */
876 sig = pki_sign_data(key, hash_type, input, input_len);
877 assert_non_null(sig);
878
879 /* Verify signature */
880 rc = pki_verify_data_signature(sig, pubkey, input, input_len);
881 assert_int_equal(rc, SSH_OK);
882
883 ssh_signature_free(sig);
884 SSH_KEY_FREE(pubkey);
885
886 return rc;
887 }
888
889 static void torture_pki_sign_data_rsa(void **state)
890 {
891 int rc;
892 ssh_key key = NULL;
893
894 (void) state;
895
896 /* Setup */
897 rc = ssh_pki_generate(SSH_KEYTYPE_RSA, 2048, &key);
898 assert_int_equal(rc, SSH_OK);
899 assert_non_null(key);
900
901 if (!ssh_fips_mode()) {
902 /* Test using SHA1 */
903 rc = test_sign_verify_data(key, SSH_DIGEST_SHA1, INPUT, sizeof(INPUT));
904 assert_int_equal(rc, SSH_OK);
905 }
906
907 /* Test using SHA256 */
908 rc = test_sign_verify_data(key, SSH_DIGEST_SHA256, INPUT, sizeof(INPUT));
909 assert_int_equal(rc, SSH_OK);
910
911 /* Test using SHA512 */
912 rc = test_sign_verify_data(key, SSH_DIGEST_SHA512, INPUT, sizeof(INPUT));
913 assert_int_equal(rc, SSH_OK);
914
915 /* Cleanup */
916 SSH_KEY_FREE(key);
917 }
918
919 static void torture_pki_fail_sign_with_incompatible_hash(void **state)
920 {
921 int rc;
922 ssh_key key = NULL;
923 ssh_key pubkey = NULL;
924 ssh_signature sig, bad_sig;
925
926 (void) state;
927
928 /* Setup */
929 rc = ssh_pki_generate(SSH_KEYTYPE_RSA, 2048, &key);
930 assert_int_equal(rc, SSH_OK);
931 assert_non_null(key);
932
933 /* Get the public key to verify signature */
934 rc = ssh_pki_export_privkey_to_pubkey(key, &pubkey);
935 assert_int_equal(rc, SSH_OK);
936 assert_non_null(pubkey);
937
938 /* Sign the buffer */
939 sig = pki_sign_data(key, SSH_DIGEST_SHA256, INPUT, sizeof(INPUT));
940 assert_non_null(sig);
941
942 /* Verify signature */
943 rc = pki_verify_data_signature(sig, pubkey, INPUT, sizeof(INPUT));
944 assert_int_equal(rc, SSH_OK);
945
946 /* Test if signature fails with SSH_DIGEST_AUTO */
947 bad_sig = pki_sign_data(key, SSH_DIGEST_AUTO, INPUT, sizeof(INPUT));
948 assert_null(bad_sig);
949
950 /* Test if verification fails with SSH_DIGEST_AUTO */
951 sig->hash_type = SSH_DIGEST_AUTO;
952 rc = pki_verify_data_signature(sig, pubkey, INPUT, sizeof(INPUT));
953 assert_int_not_equal(rc, SSH_OK);
954
955 /* Cleanup */
956 ssh_signature_free(sig);
957 SSH_KEY_FREE(pubkey);
958 SSH_KEY_FREE(key);
959 }
960
961 static void
962 torture_pki_rsa_write_privkey_format(void **state,
963 enum ssh_file_format_e format)
964 {
965 ssh_key origkey = NULL;
966 ssh_key privkey = NULL;
967 int rc;
968
969 (void) state; /* unused */
970
971 rc = ssh_pki_import_privkey_file(LIBSSH_RSA_TESTKEY,
972 NULL,
973 NULL,
974 NULL,
975 &origkey);
976 assert_return_code(rc, errno);
977 assert_non_null(origkey);
978
979 unlink(LIBSSH_RSA_TESTKEY);
980
981 rc = ssh_pki_export_privkey_file_format(origkey,
982 NULL,
983 NULL,
984 NULL,
985 LIBSSH_RSA_TESTKEY,
986 format);
987 assert_return_code(rc, errno);
988
989 rc = ssh_pki_import_privkey_file(LIBSSH_RSA_TESTKEY,
990 NULL,
991 NULL,
992 NULL,
993 &privkey);
994 assert_return_code(rc, errno);
995 assert_non_null(privkey);
996
997 rc = ssh_key_cmp(origkey, privkey, SSH_KEY_CMP_PRIVATE);
998 assert_return_code(rc, errno);
999
1000 SSH_KEY_FREE(origkey);
1001 SSH_KEY_FREE(privkey);
1002
1003 /* Test with passphrase */
1004 rc = ssh_pki_import_privkey_file(LIBSSH_RSA_TESTKEY_PASSPHRASE,
1005 torture_get_testkey_passphrase(),
1006 NULL,
1007 NULL,
1008 &origkey);
1009 assert_return_code(rc, errno);
1010 assert_non_null(origkey);
1011
1012 unlink(LIBSSH_RSA_TESTKEY_PASSPHRASE);
1013 rc = ssh_pki_export_privkey_file_format(origkey,
1014 torture_get_testkey_passphrase(),
1015 NULL,
1016 NULL,
1017 LIBSSH_RSA_TESTKEY_PASSPHRASE,
1018 format);
1019 assert_return_code(rc, errno);
1020
1021 /* Test with invalid passphrase */
1022 rc = ssh_pki_import_privkey_file(LIBSSH_RSA_TESTKEY_PASSPHRASE,
1023 "invalid secret",
1024 NULL,
1025 NULL,
1026 &privkey);
1027 assert_int_equal(rc, SSH_ERROR);
1028 assert_null(privkey);
1029
1030 rc = ssh_pki_import_privkey_file(LIBSSH_RSA_TESTKEY_PASSPHRASE,
1031 torture_get_testkey_passphrase(),
1032 NULL,
1033 NULL,
1034 &privkey);
1035 assert_return_code(rc, errno);
1036 assert_non_null(privkey);
1037
1038 rc = ssh_key_cmp(origkey, privkey, SSH_KEY_CMP_PRIVATE);
1039 assert_return_code(rc, errno);
1040
1041 SSH_KEY_FREE(origkey);
1042 SSH_KEY_FREE(privkey);
1043 }
1044
1045 static void
1046 torture_pki_rsa_write_privkey(void **state)
1047 {
1048 torture_pki_rsa_write_privkey_format(state, SSH_FILE_FORMAT_DEFAULT);
1049 }
1050
1051 #if defined(HAVE_LIBCRYPTO)
1052 static void
1053 torture_pki_rsa_write_privkey_pem(void **state)
1054 {
1055 torture_pki_rsa_write_privkey_format(state, SSH_FILE_FORMAT_PEM);
1056 }
1057
1058 static void
1059 torture_pki_rsa_write_privkey_openssh(void **state)
1060 {
1061 torture_pki_rsa_write_privkey_format(state, SSH_FILE_FORMAT_OPENSSH);
1062 }
1063
1064 static void
1065 torture_pki_rsa_import_export_privkey_base64_pem(void **state)
1066 {
1067 torture_pki_rsa_import_export_privkey_base64_format(state,
1068 SSH_FILE_FORMAT_PEM);
1069 }
1070 static void
1071 torture_pki_rsa_import_export_privkey_base64_openssh(void **state)
1072 {
1073 torture_pki_rsa_import_export_privkey_base64_format(state,
1074 SSH_FILE_FORMAT_OPENSSH);
1075 }
1076 #endif /* HAVE_LIBCRYPTO */
1077
1078 static void torture_pki_rsa_import_privkey_base64_passphrase(void **state)
1079 {
1080 int rc;
1081 ssh_key key = NULL;
1082 const char *passphrase = torture_get_testkey_passphrase();
1083
1084 (void) state; /* unused */
1085
1086
1087 rc = ssh_pki_import_privkey_base64(torture_get_testkey(SSH_KEYTYPE_RSA, 1),
1088 passphrase,
1089 NULL,
1090 NULL,
1091 &key);
1092 assert_return_code(rc, errno);
1093 assert_non_null(key);
1094
1095 rc = ssh_key_is_private(key);
1096 assert_int_equal(rc, 1);
1097
1098 SSH_KEY_FREE(key);
1099
1100 /* test if it returns -1 if passphrase is wrong */
1101 rc = ssh_pki_import_privkey_base64(torture_get_testkey(SSH_KEYTYPE_RSA, 1),
1102 "wrong passphrase !!",
1103 NULL,
1104 NULL,
1105 &key);
1106 assert_int_equal(rc, -1);
1107 SSH_KEY_FREE(key);
1108
1109 #ifndef HAVE_LIBCRYPTO
1110 /* test if it returns -1 if passphrase is NULL */
1111 /* libcrypto asks for a passphrase, so skip this test */
1112 rc = ssh_pki_import_privkey_base64(torture_get_testkey(SSH_KEYTYPE_RSA, 1),
1113 NULL,
1114 NULL,
1115 NULL,
1116 &key);
1117 assert_int_equal(rc, -1);
1118 SSH_KEY_FREE(key);
1119 #endif
1120 }
1121
1122 static void
1123 torture_pki_rsa_import_openssh_privkey_base64_passphrase(void **state)
1124 {
1125 int rc;
1126 ssh_key key = NULL;
1127 const char *passphrase = torture_get_testkey_passphrase();
1128 const char *keystring = NULL;
1129
1130 (void) state; /* unused */
1131
1132 keystring = torture_get_openssh_testkey(SSH_KEYTYPE_RSA, 1);
1133 assert_non_null(keystring);
1134
1135 rc = ssh_pki_import_privkey_base64(keystring,
1136 passphrase,
1137 NULL,
1138 NULL,
1139 &key);
1140 assert_return_code(rc, errno);
1141 assert_non_null(key);
1142
1143 rc = ssh_key_is_private(key);
1144 assert_int_equal(rc, 1);
1145
1146 SSH_KEY_FREE(key);
1147
1148 /* test if it returns -1 if passphrase is wrong */
1149 rc = ssh_pki_import_privkey_base64(keystring,
1150 "wrong passphrase !!",
1151 NULL,
1152 NULL,
1153 &key);
1154 assert_int_equal(rc, -1);
1155 SSH_KEY_FREE(key);
1156
1157 /* test if it returns -1 if passphrase is NULL */
1158 /* libcrypto asks for a passphrase, so skip this test */
1159 rc = ssh_pki_import_privkey_base64(keystring,
1160 NULL,
1161 NULL,
1162 NULL,
1163 &key);
1164 assert_int_equal(rc, -1);
1165 SSH_KEY_FREE(key);
1166 }
1167
1168 int torture_run_tests(void) {
1169 int rc;
1170 struct CMUnitTest tests[] = {
1171 cmocka_unit_test_setup_teardown(torture_pki_rsa_import_pubkey_file,
1172 setup_rsa_key,
1173 teardown),
1174 cmocka_unit_test_setup_teardown(
1175 torture_pki_rsa_import_pubkey_from_openssh_privkey,
1176 setup_openssh_rsa_key,
1177 teardown),
1178 cmocka_unit_test_setup_teardown(
1179 torture_pki_rsa_import_privkey_base64_NULL_key,
1180 setup_rsa_key,
1181 teardown),
1182 cmocka_unit_test_setup_teardown(
1183 torture_pki_rsa_import_privkey_base64_NULL_str,
1184 setup_rsa_key,
1185 teardown),
1186 cmocka_unit_test_setup_teardown(
1187 torture_pki_rsa_import_export_privkey_base64,
1188 setup_rsa_key,
1189 teardown),
1190 cmocka_unit_test_setup_teardown(
1191 torture_pki_rsa_import_privkey_base64_comment,
1192 setup_rsa_key,
1193 teardown),
1194 cmocka_unit_test_setup_teardown(
1195 torture_pki_rsa_import_privkey_base64_whitespace,
1196 setup_rsa_key,
1197 teardown),
1198 cmocka_unit_test_setup_teardown(
1199 torture_pki_rsa_import_export_privkey_base64,
1200 setup_openssh_rsa_key,
1201 teardown),
1202 cmocka_unit_test_setup_teardown(
1203 torture_pki_rsa_publickey_from_privatekey,
1204 setup_rsa_key,
1205 teardown),
1206 cmocka_unit_test(torture_pki_rsa_import_privkey_base64_passphrase),
1207 cmocka_unit_test(
1208 torture_pki_rsa_import_openssh_privkey_base64_passphrase),
1209 cmocka_unit_test_setup_teardown(torture_pki_rsa_copy_cert_to_privkey,
1210 setup_rsa_key,
1211 teardown),
1212 cmocka_unit_test_setup_teardown(torture_pki_rsa_import_cert_file,
1213 setup_rsa_key,
1214 teardown),
1215 cmocka_unit_test_setup_teardown(torture_pki_rsa_publickey_base64,
1216 setup_rsa_key,
1217 teardown),
1218 cmocka_unit_test_setup_teardown(
1219 torture_pki_rsa_generate_pubkey_from_privkey,
1220 setup_rsa_key,
1221 teardown),
1222 cmocka_unit_test_setup_teardown(torture_pki_rsa_duplicate_key,
1223 setup_rsa_key,
1224 teardown),
1225 cmocka_unit_test(torture_pki_generate_rsa_deprecated),
1226 cmocka_unit_test(torture_pki_rsa_key_size),
1227 cmocka_unit_test(torture_pki_generate_key_rsa),
1228 cmocka_unit_test_setup_teardown(torture_pki_rsa_write_privkey,
1229 setup_rsa_key,
1230 teardown),
1231 #if defined(HAVE_LIBCRYPTO)
1232 cmocka_unit_test_setup_teardown(torture_pki_rsa_write_privkey_pem,
1233 setup_rsa_key,
1234 teardown),
1235 cmocka_unit_test_setup_teardown(torture_pki_rsa_write_privkey_openssh,
1236 setup_rsa_key,
1237 teardown),
1238 cmocka_unit_test_setup_teardown(
1239 torture_pki_rsa_import_export_privkey_base64_pem,
1240 setup_openssh_rsa_key,
1241 teardown),
1242 cmocka_unit_test_setup_teardown(
1243 torture_pki_rsa_import_export_privkey_base64_openssh,
1244 setup_openssh_rsa_key,
1245 teardown),
1246 #endif /* HAVE_LIBCRYPTO */
1247 cmocka_unit_test(torture_pki_sign_data_rsa),
1248 cmocka_unit_test(torture_pki_fail_sign_with_incompatible_hash),
1249 cmocka_unit_test_setup_teardown(torture_pki_rsa_sha2,
1250 setup_rsa_key,
1251 teardown),
1252 };
1253
1254 ssh_init();
1255 torture_filter_tests(tests);
1256 rc = cmocka_run_group_tests(tests, NULL, NULL);
1257 ssh_finalize();
1258 return rc;
1259 }