Debian Codegraph @

tests/unittests/torture_pki_sk_ed25519.c

raw
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2025 Praneeth Sarode <[email protected]>
5 *
6 * The SSH Library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or (at your
9 * option) any later version.
10 *
11 * The SSH Library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 * License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with the SSH Library; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 */
21
22 #include "config.h"
23
24 #define LIBSSH_STATIC
25
26 #include "pki.c"
27 #include "torture.h"
28 #include "torture_key.h"
29 #include "torture_pki.h"
30 #include "torture_sk.h"
31
32 /* Test constants */
33 #define LIBSSH_SK_ED25519_TESTKEY "libssh_testkey.id_ed25519_sk"
34 #define LIBSSH_SK_ED25519_TESTKEY_PASSPHRASE \
35 "libssh_testkey_passphrase.id_ed25519_sk"
36
37 const char template[] = "temp_dir_XXXXXX";
38
39 struct pki_st {
40 char *cwd;
41 char *temp_dir;
42 };
43
44 static int setup_sk_ed25519_key(void **state)
45 {
46 const char *keystring = NULL;
47 struct pki_st *test_state = NULL;
48 char *cwd = NULL;
49 char *tmp_dir = NULL;
50 int rc = 0;
51
52 test_state = (struct pki_st *)malloc(sizeof(struct pki_st));
53 assert_non_null(test_state);
54
55 cwd = torture_get_current_working_dir();
56 assert_non_null(cwd);
57
58 tmp_dir = torture_make_temp_dir(template);
59 assert_non_null(tmp_dir);
60
61 test_state->cwd = cwd;
62 test_state->temp_dir = tmp_dir;
63
64 *state = test_state;
65
66 rc = torture_change_dir(tmp_dir);
67 assert_int_equal(rc, 0);
68
69 printf("Changed directory to: %s\n", tmp_dir);
70
71 keystring = torture_get_openssh_testkey(SSH_KEYTYPE_SK_ED25519, 0);
72 torture_write_file(LIBSSH_SK_ED25519_TESTKEY, keystring);
73
74 keystring = torture_get_openssh_testkey(SSH_KEYTYPE_SK_ED25519, 1);
75 torture_write_file(LIBSSH_SK_ED25519_TESTKEY_PASSPHRASE, keystring);
76
77 keystring = torture_get_testkey_pub(SSH_KEYTYPE_SK_ED25519);
78 torture_write_file(LIBSSH_SK_ED25519_TESTKEY ".pub", keystring);
79
80 return 0;
81 }
82
83 static int teardown(void **state)
84 {
85 struct pki_st *test_state = NULL;
86 int rc = 0;
87
88 test_state = *((struct pki_st **)state);
89
90 assert_non_null(test_state);
91 assert_non_null(test_state->cwd);
92 assert_non_null(test_state->temp_dir);
93
94 rc = torture_change_dir(test_state->cwd);
95 assert_int_equal(rc, 0);
96
97 rc = torture_rmdirs(test_state->temp_dir);
98 assert_int_equal(rc, 0);
99
100 SAFE_FREE(test_state->temp_dir);
101 SAFE_FREE(test_state->cwd);
102 SAFE_FREE(test_state);
103
104 return 0;
105 }
106
107 static void torture_pki_sk_ed25519_import_pubkey_file(void **state)
108 {
109 ssh_key pubkey = NULL;
110 int rc;
111
112 (void)state; /* unused */
113
114 /* Skip test if in FIPS mode */
115 if (ssh_fips_mode()) {
116 skip();
117 }
118
119 rc = ssh_pki_import_pubkey_file(LIBSSH_SK_ED25519_TESTKEY ".pub", &pubkey);
120 assert_return_code(rc, errno);
121 assert_sk_key_valid(pubkey, SSH_KEYTYPE_SK_ED25519, false);
122
123 SSH_KEY_FREE(pubkey);
124 }
125
126 static void
127 torture_pki_sk_ed25519_import_pubkey_from_openssh_privkey(void **state)
128 {
129 ssh_key pubkey = NULL;
130 int rc;
131
132 (void)state; /* unused */
133
134 /* Skip test if in FIPS mode */
135 if (ssh_fips_mode()) {
136 skip();
137 }
138
139 rc = ssh_pki_import_pubkey_file(LIBSSH_SK_ED25519_TESTKEY_PASSPHRASE,
140 &pubkey);
141 assert_return_code(rc, errno);
142 assert_non_null(pubkey);
143 assert_sk_key_valid(pubkey, SSH_KEYTYPE_SK_ED25519, false);
144
145 SSH_KEY_FREE(pubkey);
146 }
147
148 static void torture_pki_sk_ed25519_import_privkey_base64(void **state)
149 {
150 ssh_key privkey = NULL;
151 char *keystring = NULL;
152 int rc;
153
154 (void)state; /* unused */
155
156 /* Skip test if in FIPS mode */
157 if (ssh_fips_mode()) {
158 skip();
159 }
160
161 keystring = torture_pki_read_file(LIBSSH_SK_ED25519_TESTKEY);
162 assert_non_null(keystring);
163
164 rc = ssh_pki_import_privkey_base64(keystring,
165 NULL, /* no passphrase */
166 NULL, /* no auth callback */
167 NULL, /* no auth data */
168 &privkey);
169 assert_return_code(rc, errno);
170 assert_sk_key_valid(privkey, SSH_KEYTYPE_SK_ED25519, true);
171
172 SAFE_FREE(keystring);
173 SSH_KEY_FREE(privkey);
174 }
175
176 static void torture_pki_sk_ed25519_import_privkey_base64_comment(void **state)
177 {
178 int rc, file_str_len;
179 const char *comment_str = "#this is line-comment\n#this is another\n";
180 char *file_str = NULL;
181 ssh_key key = NULL;
182 char *keystring = NULL;
183
184 (void)state; /* unused */
185
186 /* Skip test if in FIPS mode */
187 if (ssh_fips_mode()) {
188 skip();
189 }
190
191 keystring = torture_pki_read_file(LIBSSH_SK_ED25519_TESTKEY);
192 assert_non_null(keystring);
193
194 file_str_len = strlen(comment_str) + strlen(keystring) + 1;
195 file_str = malloc(file_str_len);
196 assert_non_null(file_str);
197 rc = snprintf(file_str, file_str_len, "%s%s", comment_str, keystring);
198 assert_int_equal(rc, file_str_len - 1);
199
200 rc = ssh_pki_import_privkey_base64(file_str, NULL, NULL, NULL, &key);
201 assert_return_code(rc, errno);
202 assert_sk_key_valid(key, SSH_KEYTYPE_SK_ED25519, true);
203
204 SAFE_FREE(keystring);
205 SAFE_FREE(file_str);
206 SSH_KEY_FREE(key);
207 }
208
209 static void
210 torture_pki_sk_ed25519_import_privkey_base64_whitespace(void **state)
211 {
212 int rc, file_str_len;
213 const char *whitespace_str = " \t\t\t\n\n\n";
214 char *file_str = NULL;
215 ssh_key key = NULL;
216 char *keystring = NULL;
217
218 (void)state; /* unused */
219
220 /* Skip test if in FIPS mode */
221 if (ssh_fips_mode()) {
222 skip();
223 }
224
225 keystring = torture_pki_read_file(LIBSSH_SK_ED25519_TESTKEY);
226 assert_non_null(keystring);
227
228 file_str_len = 2 * strlen(whitespace_str) + strlen(keystring) + 1;
229 file_str = malloc(file_str_len);
230 assert_non_null(file_str);
231 rc = snprintf(file_str,
232 file_str_len,
233 "%s%s%s",
234 whitespace_str,
235 keystring,
236 whitespace_str);
237 assert_int_equal(rc, file_str_len - 1);
238
239 rc = ssh_pki_import_privkey_base64(file_str, NULL, NULL, NULL, &key);
240 assert_return_code(rc, errno);
241 assert_sk_key_valid(key, SSH_KEYTYPE_SK_ED25519, true);
242
243 SAFE_FREE(keystring);
244 SAFE_FREE(file_str);
245 SSH_KEY_FREE(key);
246 }
247
248 static void torture_pki_sk_ed25519_import_export_privkey_base64(void **state)
249 {
250 ssh_key origkey = NULL;
251 ssh_key privkey = NULL;
252 char *key_buf = NULL;
253 const char *keystring = NULL;
254 int rc;
255
256 (void)state; /* unused */
257
258 /* Skip test if in FIPS mode */
259 if (ssh_fips_mode()) {
260 skip();
261 }
262
263 keystring = torture_get_openssh_testkey(SSH_KEYTYPE_SK_ED25519, 0);
264 assert_non_null(keystring);
265
266 rc = ssh_pki_import_privkey_base64(keystring, NULL, NULL, NULL, &origkey);
267 assert_return_code(rc, errno);
268 assert_sk_key_valid(origkey, SSH_KEYTYPE_SK_ED25519, true);
269
270 rc = ssh_pki_export_privkey_base64(origkey, NULL, NULL, NULL, &key_buf);
271 assert_return_code(rc, errno);
272 assert_non_null(key_buf);
273
274 rc = ssh_pki_import_privkey_base64(key_buf, NULL, NULL, NULL, &privkey);
275 assert_return_code(rc, errno);
276 assert_sk_key_valid(privkey, SSH_KEYTYPE_SK_ED25519, true);
277
278 rc = ssh_key_cmp(origkey, privkey, SSH_KEY_CMP_PRIVATE);
279 assert_int_equal(rc, 0);
280
281 SSH_KEY_FREE(origkey);
282 SSH_KEY_FREE(privkey);
283 SSH_STRING_FREE_CHAR(key_buf);
284 }
285
286 static void torture_pki_sk_ed25519_publickey_from_privatekey(void **state)
287 {
288 ssh_key privkey = NULL;
289 ssh_key pubkey = NULL;
290 const char *keystring = NULL;
291 int rc;
292
293 (void)state; /* unused */
294
295 /* Skip test if in FIPS mode */
296 if (ssh_fips_mode()) {
297 skip();
298 }
299
300 keystring = torture_get_openssh_testkey(SSH_KEYTYPE_SK_ED25519, 0);
301 assert_non_null(keystring);
302
303 rc = ssh_pki_import_privkey_base64(keystring,
304 NULL, /* no passphrase */
305 NULL, /* no auth callback */
306 NULL, /* no auth data */
307 &privkey);
308 assert_return_code(rc, errno);
309 assert_sk_key_valid(privkey, SSH_KEYTYPE_SK_ED25519, true);
310
311 rc = ssh_pki_export_privkey_to_pubkey(privkey, &pubkey);
312 assert_return_code(rc, errno);
313 assert_sk_key_valid(pubkey, SSH_KEYTYPE_SK_ED25519, false);
314
315 rc = ssh_key_cmp(privkey, pubkey, SSH_KEY_CMP_PUBLIC);
316 assert_int_equal(rc, 0);
317
318 SSH_KEY_FREE(privkey);
319 SSH_KEY_FREE(pubkey);
320 }
321
322 static void
323 torture_pki_sk_ed25519_import_privkey_base64_passphrase(void **state)
324 {
325 ssh_key privkey = NULL;
326 const char *keystring = NULL;
327 const char *passphrase = NULL;
328 int rc;
329
330 (void)state; /* unused */
331
332 /* Skip test if in FIPS mode */
333 if (ssh_fips_mode()) {
334 skip();
335 }
336
337 keystring = torture_get_openssh_testkey(SSH_KEYTYPE_SK_ED25519, 1);
338 assert_non_null(keystring);
339
340 passphrase = torture_get_testkey_passphrase();
341 assert_non_null(passphrase);
342
343 /* Import with a passphrase */
344 rc = ssh_pki_import_privkey_base64(keystring,
345 passphrase,
346 NULL,
347 NULL,
348 &privkey);
349 assert_return_code(rc, errno);
350 assert_sk_key_valid(privkey, SSH_KEYTYPE_SK_ED25519, true);
351 SSH_KEY_FREE(privkey);
352
353 rc = ssh_pki_import_privkey_base64(keystring,
354 "wrong passphrase",
355 NULL,
356 NULL,
357 &privkey);
358 assert_int_equal(rc, SSH_ERROR);
359 assert_null(privkey);
360 }
361
362 static void torture_pki_sk_ed25519_duplicate_key(void **state)
363 {
364 ssh_key privkey = NULL;
365 ssh_key duplicated = NULL;
366 const char *keystring = NULL;
367 int rc;
368
369 (void)state; /* unused */
370
371 /* Skip test if in FIPS mode */
372 if (ssh_fips_mode()) {
373 skip();
374 }
375
376 keystring = torture_get_openssh_testkey(SSH_KEYTYPE_SK_ED25519, 0);
377 assert_non_null(keystring);
378
379 rc = ssh_pki_import_privkey_base64(keystring,
380 NULL, /* no passphrase */
381 NULL, /* no auth callback */
382 NULL, /* no auth data */
383 &privkey);
384 assert_return_code(rc, errno);
385 assert_sk_key_valid(privkey, SSH_KEYTYPE_SK_ED25519, true);
386
387 duplicated = ssh_key_dup(privkey);
388 assert_sk_key_valid(duplicated, SSH_KEYTYPE_SK_ED25519, true);
389
390 rc = ssh_key_cmp(privkey, duplicated, SSH_KEY_CMP_PRIVATE);
391 assert_int_equal(rc, 0);
392
393 SSH_KEY_FREE(privkey);
394 SSH_KEY_FREE(duplicated);
395 }
396
397 static void torture_pki_sk_ed25519_import_pubkey_base64(void **state)
398 {
399 ssh_key key = NULL;
400 ssh_key pubkey = NULL;
401 char *b64_key = NULL;
402 const char *keystring = NULL;
403 int rc;
404
405 (void)state; /* unused */
406
407 /* Skip test if in FIPS mode */
408 if (ssh_fips_mode()) {
409 skip();
410 }
411
412 keystring = torture_get_openssh_testkey(SSH_KEYTYPE_SK_ED25519, 0);
413 assert_non_null(keystring);
414
415 /* Import private key to extract public key */
416 rc = ssh_pki_import_privkey_base64(keystring, NULL, NULL, NULL, &key);
417 assert_return_code(rc, errno);
418 assert_sk_key_valid(key, SSH_KEYTYPE_SK_ED25519, true);
419
420 rc = ssh_pki_export_privkey_to_pubkey(key, &pubkey);
421 assert_return_code(rc, errno);
422 assert_sk_key_valid(pubkey, SSH_KEYTYPE_SK_ED25519, false);
423
424 /* Export public key to base64 */
425 rc = ssh_pki_export_pubkey_base64(pubkey, &b64_key);
426 assert_return_code(rc, errno);
427 assert_non_null(b64_key);
428
429 SSH_KEY_FREE(key);
430 SSH_KEY_FREE(pubkey);
431
432 /* Import public key from base64 */
433 rc = ssh_pki_import_pubkey_base64(b64_key, SSH_KEYTYPE_SK_ED25519, &pubkey);
434 assert_return_code(rc, errno);
435 assert_sk_key_valid(pubkey, SSH_KEYTYPE_SK_ED25519, false);
436
437 SSH_KEY_FREE(pubkey);
438 SSH_STRING_FREE_CHAR(b64_key);
439 }
440
441 static void torture_pki_sk_ed25519_pubkey_blob(void **state)
442 {
443 ssh_key privkey = NULL;
444 ssh_key pubkey = NULL;
445 ssh_key imported_pubkey = NULL;
446 ssh_string pub_blob = NULL;
447 const char *keystring = NULL;
448 int rc;
449
450 (void)state; /* unused */
451
452 /* Skip test if in FIPS mode */
453 if (ssh_fips_mode()) {
454 skip();
455 }
456
457 keystring = torture_get_openssh_testkey(SSH_KEYTYPE_SK_ED25519, 0);
458 assert_non_null(keystring);
459
460 rc = ssh_pki_import_privkey_base64(keystring, NULL, NULL, NULL, &privkey);
461 assert_return_code(rc, errno);
462 assert_sk_key_valid(privkey, SSH_KEYTYPE_SK_ED25519, true);
463
464 rc = ssh_pki_export_privkey_to_pubkey(privkey, &pubkey);
465 assert_return_code(rc, errno);
466 assert_sk_key_valid(pubkey, SSH_KEYTYPE_SK_ED25519, false);
467
468 /* Export public key to blob */
469 rc = ssh_pki_export_pubkey_blob(pubkey, &pub_blob);
470 assert_int_equal(rc, SSH_OK);
471 assert_non_null(pub_blob);
472
473 /* Import public key from blob */
474 rc = ssh_pki_import_pubkey_blob(pub_blob, &imported_pubkey);
475 assert_int_equal(rc, SSH_OK);
476 assert_sk_key_valid(imported_pubkey, SSH_KEYTYPE_SK_ED25519, false);
477
478 /* Compare keys */
479 rc = ssh_key_cmp(pubkey, imported_pubkey, SSH_KEY_CMP_PUBLIC);
480 assert_int_equal(rc, 0);
481
482 ssh_string_free(pub_blob);
483 SSH_KEY_FREE(privkey);
484 SSH_KEY_FREE(pubkey);
485 SSH_KEY_FREE(imported_pubkey);
486 }
487
488 int torture_run_tests(void)
489 {
490 int rc;
491
492 struct CMUnitTest tests[] = {
493 cmocka_unit_test_setup_teardown(
494 torture_pki_sk_ed25519_import_pubkey_file,
495 setup_sk_ed25519_key,
496 teardown),
497 cmocka_unit_test_setup_teardown(
498 torture_pki_sk_ed25519_import_pubkey_from_openssh_privkey,
499 setup_sk_ed25519_key,
500 teardown),
501 cmocka_unit_test_setup_teardown(
502 torture_pki_sk_ed25519_import_privkey_base64,
503 setup_sk_ed25519_key,
504 teardown),
505 cmocka_unit_test_setup_teardown(
506 torture_pki_sk_ed25519_import_privkey_base64_comment,
507 setup_sk_ed25519_key,
508 teardown),
509 cmocka_unit_test_setup_teardown(
510 torture_pki_sk_ed25519_import_privkey_base64_whitespace,
511 setup_sk_ed25519_key,
512 teardown),
513 cmocka_unit_test_setup_teardown(
514 torture_pki_sk_ed25519_import_export_privkey_base64,
515 setup_sk_ed25519_key,
516 teardown),
517 cmocka_unit_test_setup_teardown(
518 torture_pki_sk_ed25519_publickey_from_privatekey,
519 setup_sk_ed25519_key,
520 teardown),
521 cmocka_unit_test_setup_teardown(
522 torture_pki_sk_ed25519_import_pubkey_base64,
523 setup_sk_ed25519_key,
524 teardown),
525 cmocka_unit_test_setup_teardown(
526 torture_pki_sk_ed25519_import_privkey_base64_passphrase,
527 setup_sk_ed25519_key,
528 teardown),
529 cmocka_unit_test_setup_teardown(torture_pki_sk_ed25519_duplicate_key,
530 setup_sk_ed25519_key,
531 teardown),
532 cmocka_unit_test_setup_teardown(torture_pki_sk_ed25519_pubkey_blob,
533 setup_sk_ed25519_key,
534 teardown),
535 };
536
537 ssh_init();
538 torture_filter_tests(tests);
539 rc = cmocka_run_group_tests(tests, NULL, NULL);
540 ssh_finalize();
541
542 return rc;
543 }