Debian Codegraph @

tests/unittests/torture_pki_ed25519.c

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