Debian Codegraph @

tests/unittests/torture_pki_sk_ecdsa.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_ECDSA_TESTKEY "libssh_testkey.id_ecdsa_sk"
34 #define LIBSSH_SK_ECDSA_TESTKEY_PASSPHRASE \
35 "libssh_testkey_passphrase.id_ecdsa_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_ecdsa_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_ECDSA, 0);
72 torture_write_file(LIBSSH_SK_ECDSA_TESTKEY, keystring);
73
74 keystring = torture_get_openssh_testkey(SSH_KEYTYPE_SK_ECDSA, 1);
75 torture_write_file(LIBSSH_SK_ECDSA_TESTKEY_PASSPHRASE, keystring);
76
77 keystring = torture_get_testkey_pub(SSH_KEYTYPE_SK_ECDSA);
78 torture_write_file(LIBSSH_SK_ECDSA_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_ecdsa_import_pubkey_file(void **state)
108 {
109 ssh_key pubkey = NULL;
110 int rc;
111
112 (void)state; /* unused */
113
114 rc = ssh_pki_import_pubkey_file(LIBSSH_SK_ECDSA_TESTKEY ".pub", &pubkey);
115 assert_return_code(rc, errno);
116 assert_sk_key_valid(pubkey, SSH_KEYTYPE_SK_ECDSA, false);
117
118 SSH_KEY_FREE(pubkey);
119 }
120
121 static void
122 torture_pki_sk_ecdsa_import_pubkey_from_openssh_privkey(void **state)
123 {
124 ssh_key pubkey = NULL;
125 int rc;
126
127 (void)state; /* unused */
128
129 rc =
130 ssh_pki_import_pubkey_file(LIBSSH_SK_ECDSA_TESTKEY_PASSPHRASE, &pubkey);
131 assert_return_code(rc, errno);
132 assert_non_null(pubkey);
133 assert_sk_key_valid(pubkey, SSH_KEYTYPE_SK_ECDSA, false);
134
135 SSH_KEY_FREE(pubkey);
136 }
137
138 static void torture_pki_sk_ecdsa_import_privkey_base64(void **state)
139 {
140 ssh_key privkey = NULL;
141 char *keystring = NULL;
142 int rc;
143
144 (void)state; /* unused */
145
146 keystring = torture_pki_read_file(LIBSSH_SK_ECDSA_TESTKEY);
147 assert_non_null(keystring);
148
149 rc = ssh_pki_import_privkey_base64(keystring,
150 NULL, /* no passphrase */
151 NULL, /* no auth callback */
152 NULL, /* no auth data */
153 &privkey);
154 assert_return_code(rc, errno);
155 assert_sk_key_valid(privkey, SSH_KEYTYPE_SK_ECDSA, true);
156
157 SAFE_FREE(keystring);
158 SSH_KEY_FREE(privkey);
159 }
160
161 static void torture_pki_sk_ecdsa_import_privkey_base64_comment(void **state)
162 {
163 int rc, file_str_len;
164 const char *comment_str = "#this is line-comment\n#this is another\n";
165 char *file_str = NULL;
166 ssh_key key = NULL;
167 char *keystring = NULL;
168
169 (void)state; /* unused */
170
171 keystring = torture_pki_read_file(LIBSSH_SK_ECDSA_TESTKEY);
172 assert_non_null(keystring);
173
174 file_str_len = strlen(comment_str) + strlen(keystring) + 1;
175 file_str = malloc(file_str_len);
176 assert_non_null(file_str);
177 rc = snprintf(file_str, file_str_len, "%s%s", comment_str, keystring);
178 assert_int_equal(rc, file_str_len - 1);
179
180 rc = ssh_pki_import_privkey_base64(file_str, NULL, NULL, NULL, &key);
181 assert_return_code(rc, errno);
182 assert_sk_key_valid(key, SSH_KEYTYPE_SK_ECDSA, true);
183
184 SAFE_FREE(keystring);
185 SAFE_FREE(file_str);
186 SSH_KEY_FREE(key);
187 }
188
189 static void torture_pki_sk_ecdsa_import_privkey_base64_whitespace(void **state)
190 {
191 int rc, file_str_len;
192 const char *whitespace_str = " \t\t\t\n\n\n";
193 char *file_str = NULL;
194 ssh_key key = NULL;
195 char *keystring = NULL;
196
197 (void)state; /* unused */
198
199 keystring = torture_pki_read_file(LIBSSH_SK_ECDSA_TESTKEY);
200 assert_non_null(keystring);
201
202 file_str_len = 2 * strlen(whitespace_str) + strlen(keystring) + 1;
203 file_str = malloc(file_str_len);
204 assert_non_null(file_str);
205 rc = snprintf(file_str,
206 file_str_len,
207 "%s%s%s",
208 whitespace_str,
209 keystring,
210 whitespace_str);
211 assert_int_equal(rc, file_str_len - 1);
212
213 rc = ssh_pki_import_privkey_base64(file_str, NULL, NULL, NULL, &key);
214 assert_return_code(rc, errno);
215 assert_sk_key_valid(key, SSH_KEYTYPE_SK_ECDSA, true);
216
217 SAFE_FREE(keystring);
218 SAFE_FREE(file_str);
219 SSH_KEY_FREE(key);
220 }
221
222 static void torture_pki_sk_ecdsa_import_export_privkey_base64(void **state)
223 {
224 ssh_key origkey = NULL;
225 ssh_key privkey = NULL;
226 char *key_buf = NULL;
227 const char *keystring = NULL;
228 int rc;
229
230 (void)state; /* unused */
231
232 keystring = torture_get_openssh_testkey(SSH_KEYTYPE_SK_ECDSA, 0);
233 assert_non_null(keystring);
234
235 rc = ssh_pki_import_privkey_base64(keystring, NULL, NULL, NULL, &origkey);
236 assert_return_code(rc, errno);
237 assert_sk_key_valid(origkey, SSH_KEYTYPE_SK_ECDSA, true);
238
239 rc = ssh_pki_export_privkey_base64(origkey, NULL, NULL, NULL, &key_buf);
240 assert_return_code(rc, errno);
241 assert_non_null(key_buf);
242
243 rc = ssh_pki_import_privkey_base64(key_buf, NULL, NULL, NULL, &privkey);
244 assert_return_code(rc, errno);
245 assert_sk_key_valid(privkey, SSH_KEYTYPE_SK_ECDSA, true);
246
247 rc = ssh_key_cmp(origkey, privkey, SSH_KEY_CMP_PRIVATE);
248 assert_int_equal(rc, 0);
249
250 SSH_KEY_FREE(origkey);
251 SSH_KEY_FREE(privkey);
252 SSH_STRING_FREE_CHAR(key_buf);
253 }
254
255 static void torture_pki_sk_ecdsa_publickey_from_privatekey(void **state)
256 {
257 ssh_key privkey = NULL;
258 ssh_key pubkey = NULL;
259 const char *keystring = NULL;
260 int rc;
261
262 (void)state; /* unused */
263
264 keystring = torture_get_openssh_testkey(SSH_KEYTYPE_SK_ECDSA, 0);
265 assert_non_null(keystring);
266
267 rc = ssh_pki_import_privkey_base64(keystring,
268 NULL, /* no passphrase */
269 NULL, /* no auth callback */
270 NULL, /* no auth data */
271 &privkey);
272 assert_return_code(rc, errno);
273 assert_sk_key_valid(privkey, SSH_KEYTYPE_SK_ECDSA, true);
274
275 rc = ssh_pki_export_privkey_to_pubkey(privkey, &pubkey);
276 assert_return_code(rc, errno);
277 assert_sk_key_valid(pubkey, SSH_KEYTYPE_SK_ECDSA, false);
278
279 rc = ssh_key_cmp(privkey, pubkey, SSH_KEY_CMP_PUBLIC);
280 assert_int_equal(rc, 0);
281
282 SSH_KEY_FREE(privkey);
283 SSH_KEY_FREE(pubkey);
284 }
285
286 static void torture_pki_sk_ecdsa_import_privkey_base64_passphrase(void **state)
287 {
288 ssh_key privkey = NULL;
289 const char *keystring = NULL;
290 const char *passphrase = NULL;
291 int rc;
292
293 (void)state; /* unused */
294
295 keystring = torture_get_openssh_testkey(SSH_KEYTYPE_SK_ECDSA, 1);
296 assert_non_null(keystring);
297
298 passphrase = torture_get_testkey_passphrase();
299 assert_non_null(passphrase);
300
301 /* Import with a passphrase */
302 rc = ssh_pki_import_privkey_base64(keystring,
303 passphrase,
304 NULL,
305 NULL,
306 &privkey);
307 assert_return_code(rc, errno);
308 assert_sk_key_valid(privkey, SSH_KEYTYPE_SK_ECDSA, true);
309 SSH_KEY_FREE(privkey);
310
311 rc = ssh_pki_import_privkey_base64(keystring,
312 "wrong passphrase",
313 NULL,
314 NULL,
315 &privkey);
316 assert_int_equal(rc, SSH_ERROR);
317 assert_null(privkey);
318 }
319
320 static void torture_pki_sk_ecdsa_duplicate_key(void **state)
321 {
322 ssh_key privkey = NULL;
323 ssh_key duplicated = NULL;
324 const char *keystring = NULL;
325 int rc;
326
327 (void)state; /* unused */
328
329 keystring = torture_get_openssh_testkey(SSH_KEYTYPE_SK_ECDSA, 0);
330 assert_non_null(keystring);
331
332 rc = ssh_pki_import_privkey_base64(keystring,
333 NULL, /* no passphrase */
334 NULL, /* no auth callback */
335 NULL, /* no auth data */
336 &privkey);
337 assert_return_code(rc, errno);
338 assert_sk_key_valid(privkey, SSH_KEYTYPE_SK_ECDSA, true);
339
340 duplicated = ssh_key_dup(privkey);
341 assert_sk_key_valid(duplicated, SSH_KEYTYPE_SK_ECDSA, true);
342
343 rc = ssh_key_cmp(privkey, duplicated, SSH_KEY_CMP_PRIVATE);
344 assert_int_equal(rc, 0);
345
346 SSH_KEY_FREE(privkey);
347 SSH_KEY_FREE(duplicated);
348 }
349
350 static void torture_pki_sk_ecdsa_import_pubkey_base64(void **state)
351 {
352 ssh_key key = NULL;
353 ssh_key pubkey = NULL;
354 char *b64_key = NULL;
355 const char *keystring = NULL;
356 int rc;
357
358 (void)state; /* unused */
359
360 keystring = torture_get_openssh_testkey(SSH_KEYTYPE_SK_ECDSA, 0);
361 assert_non_null(keystring);
362
363 /* Import private key to extract public key */
364 rc = ssh_pki_import_privkey_base64(keystring, NULL, NULL, NULL, &key);
365 assert_return_code(rc, errno);
366 assert_sk_key_valid(key, SSH_KEYTYPE_SK_ECDSA, true);
367
368 rc = ssh_pki_export_privkey_to_pubkey(key, &pubkey);
369 assert_return_code(rc, errno);
370 assert_sk_key_valid(pubkey, SSH_KEYTYPE_SK_ECDSA, false);
371
372 /* Export public key to base64 */
373 rc = ssh_pki_export_pubkey_base64(pubkey, &b64_key);
374 assert_return_code(rc, errno);
375 assert_non_null(b64_key);
376
377 SSH_KEY_FREE(key);
378 SSH_KEY_FREE(pubkey);
379
380 /* Import public key from base64 */
381 rc = ssh_pki_import_pubkey_base64(b64_key, SSH_KEYTYPE_SK_ECDSA, &pubkey);
382 assert_return_code(rc, errno);
383 assert_sk_key_valid(pubkey, SSH_KEYTYPE_SK_ECDSA, false);
384
385 SSH_KEY_FREE(pubkey);
386 SSH_STRING_FREE_CHAR(b64_key);
387 }
388
389 static void torture_pki_sk_ecdsa_pubkey_blob(void **state)
390 {
391 ssh_key privkey = NULL;
392 ssh_key pubkey = NULL;
393 ssh_key imported_pubkey = NULL;
394 ssh_string pub_blob = NULL;
395 const char *keystring = NULL;
396 int rc;
397
398 (void)state; /* unused */
399
400 keystring = torture_get_openssh_testkey(SSH_KEYTYPE_SK_ECDSA, 0);
401 assert_non_null(keystring);
402
403 rc = ssh_pki_import_privkey_base64(keystring, NULL, NULL, NULL, &privkey);
404 assert_return_code(rc, errno);
405 assert_sk_key_valid(privkey, SSH_KEYTYPE_SK_ECDSA, true);
406
407 rc = ssh_pki_export_privkey_to_pubkey(privkey, &pubkey);
408 assert_return_code(rc, errno);
409 assert_sk_key_valid(pubkey, SSH_KEYTYPE_SK_ECDSA, false);
410
411 /* Export public key to blob */
412 rc = ssh_pki_export_pubkey_blob(pubkey, &pub_blob);
413 assert_int_equal(rc, SSH_OK);
414 assert_non_null(pub_blob);
415
416 /* Import public key from blob */
417 rc = ssh_pki_import_pubkey_blob(pub_blob, &imported_pubkey);
418 assert_int_equal(rc, SSH_OK);
419 assert_sk_key_valid(imported_pubkey, SSH_KEYTYPE_SK_ECDSA, false);
420
421 /* Compare keys */
422 rc = ssh_key_cmp(pubkey, imported_pubkey, SSH_KEY_CMP_PUBLIC);
423 assert_int_equal(rc, 0);
424
425 ssh_string_free(pub_blob);
426 SSH_KEY_FREE(privkey);
427 SSH_KEY_FREE(pubkey);
428 SSH_KEY_FREE(imported_pubkey);
429 }
430
431 int torture_run_tests(void)
432 {
433 int rc;
434
435 struct CMUnitTest tests[] = {
436 cmocka_unit_test_setup_teardown(torture_pki_sk_ecdsa_import_pubkey_file,
437 setup_sk_ecdsa_key,
438 teardown),
439 cmocka_unit_test_setup_teardown(
440 torture_pki_sk_ecdsa_import_pubkey_from_openssh_privkey,
441 setup_sk_ecdsa_key,
442 teardown),
443 cmocka_unit_test_setup_teardown(
444 torture_pki_sk_ecdsa_import_privkey_base64,
445 setup_sk_ecdsa_key,
446 teardown),
447 cmocka_unit_test_setup_teardown(
448 torture_pki_sk_ecdsa_import_privkey_base64_comment,
449 setup_sk_ecdsa_key,
450 teardown),
451 cmocka_unit_test_setup_teardown(
452 torture_pki_sk_ecdsa_import_privkey_base64_whitespace,
453 setup_sk_ecdsa_key,
454 teardown),
455 cmocka_unit_test_setup_teardown(
456 torture_pki_sk_ecdsa_import_export_privkey_base64,
457 setup_sk_ecdsa_key,
458 teardown),
459 cmocka_unit_test_setup_teardown(
460 torture_pki_sk_ecdsa_publickey_from_privatekey,
461 setup_sk_ecdsa_key,
462 teardown),
463 cmocka_unit_test_setup_teardown(
464 torture_pki_sk_ecdsa_import_pubkey_base64,
465 setup_sk_ecdsa_key,
466 teardown),
467 cmocka_unit_test_setup_teardown(
468 torture_pki_sk_ecdsa_import_privkey_base64_passphrase,
469 setup_sk_ecdsa_key,
470 teardown),
471 cmocka_unit_test_setup_teardown(torture_pki_sk_ecdsa_duplicate_key,
472 setup_sk_ecdsa_key,
473 teardown),
474
475 cmocka_unit_test_setup_teardown(torture_pki_sk_ecdsa_pubkey_blob,
476 setup_sk_ecdsa_key,
477 teardown),
478
479 };
480
481 ssh_init();
482 torture_filter_tests(tests);
483 rc = cmocka_run_group_tests(tests, NULL, NULL);
484 ssh_finalize();
485
486 return rc;
487 }