Debian Codegraph @

tests/unittests/torture_knownhosts_parsing.c

raw
1 #include "config.h"
2
3 #include <sys/stat.h>
4
5 #define LIBSSH_STATIC
6 #include <libssh/priv.h>
7
8 #include "knownhosts.c"
9
10 #include "torture.h"
11 #if (defined _WIN32) || (defined _WIN64)
12 #ifndef S_IRWXO
13 #define S_IRWXO 0
14 #endif
15 #ifndef S_IRWXG
16 #define S_IRWXG 0
17 #endif
18 #endif
19
20 #define LOCALHOST_DSS_LINE "localhost,127.0.0.1 ssh-dss AAAAB3NzaC1kc3MAAACBAIK3RTEWBw+rAPcYUM2Qq4kEw59gXpUQ/WvkdeY7QDO64MHaaorySj8xsraNudmQFh4xb/i5Q1EMnNchOFxtilfU5bUJgdTvetyZEWFL+2HxqBs8GaWRyB1vtSFAw3GO8VUEnjF844N3dNyLoc0NX8IvzwNIaQho6KTsueQlG1X9AAAAFQCXUl4a5UvElL4thi/8QlxR5PtEewAAAIBqNpl5MTBxKQu5jT0+WASa7pAqwT53ofv7ZTDIEokYRb57/nwzDgkcs1fsBRrI6eczJ/VlXWwKbsgkx2Nh3ZiWYwC+HY5uqRpDaj3HERC6LMn4dzdcl29fYeziEibCbRjJX5lZF2vIaA1Ewv8yT0UlunyHZRiyw4WlEglkf/NITAAAAIBxLsdBBXn+8qEYwWK9KT+arRqNXC/lrl0Fp5YyxGNGCv82JcnuOShGGTzhYf8AtTCY1u5oixiW9kea6KXGAKgTjfJShr7n47SZVfOPOrBT3VLhRdGGO3GblDUppzfL8wsEdoqXjzrJuxSdrGnkFu8S9QjkPn9dCtScvWEcluHqMw=="
21 #define LOCALHOST_RSA_LINE "localhost,127.0.0.1 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDD7g+vV5cvxxGN0Ldmda4WZCPgRaxV1tV+1KRZoGUNUI61h0X4bmmGaAPRQBCz4G1d9bawqDqEqnpFWazrxBU5cQtISSjzuDJKovLGliky/ShTszee1Thszg3qVNk9gGOWj7jn/HDaOxRlp003Bp47MOdnMnK/oftllFDfY2fF5IRpE6sSIGtg2ZDtF95TV5/9W2oMOIAy8u/83tuibYlNPa1X/von5LgdaPLn6Bk16bQKIhAhlMtFZH8MBYEWe4ZtOGaSWKOsK9MM/RTMlwPi6PkfoHNl4MCMupjx+CdLXwbQEt9Ww+bBIaCui2VWBEiruVbIgJh0W2Tal0e2BzYZ What a Wurst!"
22 #define LOCALHOST_ECDSA_SHA1_NISTP256_LINE "localhost ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBFWmI0n0Tn5+zR7pPGcKYszRbJ/T0T3QfzRBSMMiyebGKRY8tjkU5h2l/UMugzOrOyWqMGQDgQn+a0aMunhKMg0="
23 #define LOCALHOST_DEFAULT_ED25519 "localhost ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIA7M22fXD7OiS7kGMXP+OoIjCa+J+5sq8SgAZfIOmDgM"
24 #define LOCALHOST_PORT_ED25519 "[localhost]:2222 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIA7M22fXD7OiS7kGMXP+OoIjCa+J+5sq8SgAZfIOmDgM"
25 #define LOCALHOST_PATTERN_ED25519 "local* ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIA7M22fXD7OiS7kGMXP+OoIjCa+J+5sq8SgAZfIOmDgM"
26 #define LOCALHOST_HASHED_ED25519 "|1|ayWjmTf9mYgj7PuQNVOa7Lqkj5s=|hkbEh8FN6IkLo6t6GQGuBwamgsM= ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIA7M22fXD7OiS7kGMXP+OoIjCa+J+5sq8SgAZfIOmDgM"
27 #define LOCALHOST_PORT_WILDCARD "[localhost]:* ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIA7M22fXD7OiS7kGMXP+OoIjCa+J+5sq8SgAZfIOmDgM"
28 #define LOCALHOST_STANDARD_PORT "[localhost]:22 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIA7M22fXD7OiS7kGMXP+OoIjCa+J+5sq8SgAZfIOmDgM"
29
30 #define TMP_FILE_NAME "/tmp/known_hosts_XXXXXX"
31
32 const char template[] = "temp_dir_XXXXXX";
33
34 static int setup_knownhosts_file(void **state)
35 {
36 char *tmp_file = NULL;
37 size_t nwritten;
38 FILE *fp = NULL;
39 int rc = 0;
40
41 tmp_file = torture_create_temp_file(TMP_FILE_NAME);
42 assert_non_null(tmp_file);
43
44 *state = tmp_file;
45
46 fp = fopen(tmp_file, "w");
47 assert_non_null(fp);
48
49 nwritten = fwrite(LOCALHOST_PATTERN_ED25519,
50 sizeof(char),
51 strlen(LOCALHOST_PATTERN_ED25519),
52 fp);
53 if (nwritten != strlen(LOCALHOST_PATTERN_ED25519)) {
54 rc = -1;
55 goto close_fp;
56 }
57
58 nwritten = fwrite("\n", sizeof(char), 1, fp);
59 if (nwritten != 1) {
60 rc = -1;
61 goto close_fp;
62 }
63
64 nwritten = fwrite(LOCALHOST_RSA_LINE,
65 sizeof(char),
66 strlen(LOCALHOST_RSA_LINE),
67 fp);
68 if (nwritten != strlen(LOCALHOST_RSA_LINE)) {
69 rc = -1;
70 goto close_fp;
71 }
72
73 close_fp:
74 fclose(fp);
75
76 return rc;
77 }
78
79 static int setup_knownhosts_file_duplicate(void **state)
80 {
81 char *tmp_file = NULL;
82 size_t nwritten;
83 FILE *fp = NULL;
84 int rc = 0;
85
86 tmp_file = torture_create_temp_file(TMP_FILE_NAME);
87 assert_non_null(tmp_file);
88
89 *state = tmp_file;
90
91 fp = fopen(tmp_file, "w");
92 assert_non_null(fp);
93
94 /* ed25519 key */
95 nwritten = fwrite(LOCALHOST_PATTERN_ED25519,
96 sizeof(char),
97 strlen(LOCALHOST_PATTERN_ED25519),
98 fp);
99 if (nwritten != strlen(LOCALHOST_PATTERN_ED25519)) {
100 rc = -1;
101 goto close_fp;
102 }
103
104 nwritten = fwrite("\n", sizeof(char), 1, fp);
105 if (nwritten != 1) {
106 rc = -1;
107 goto close_fp;
108 }
109
110 /* RSA key */
111 nwritten = fwrite(LOCALHOST_RSA_LINE,
112 sizeof(char),
113 strlen(LOCALHOST_RSA_LINE),
114 fp);
115 if (nwritten != strlen(LOCALHOST_RSA_LINE)) {
116 rc = -1;
117 goto close_fp;
118 }
119
120 nwritten = fwrite("\n", sizeof(char), 1, fp);
121 if (nwritten != 1) {
122 rc = -1;
123 goto close_fp;
124 }
125
126 /* ed25519 key again */
127 nwritten = fwrite(LOCALHOST_PATTERN_ED25519,
128 sizeof(char),
129 strlen(LOCALHOST_PATTERN_ED25519),
130 fp);
131 if (nwritten != strlen(LOCALHOST_PATTERN_ED25519)) {
132 rc = -1;
133 goto close_fp;
134 }
135
136 nwritten = fwrite("\n", sizeof(char), 1, fp);
137 if (nwritten != 1) {
138 rc = -1;
139 goto close_fp;
140 }
141
142 close_fp:
143 fclose(fp);
144
145 return rc;
146 }
147
148 static int setup_knownhosts_file_unsupported_type(void **state)
149 {
150 char *tmp_file = NULL;
151 size_t nwritten;
152 FILE *fp = NULL;
153 int rc = 0;
154
155 tmp_file = torture_create_temp_file(TMP_FILE_NAME);
156 assert_non_null(tmp_file);
157
158 *state = tmp_file;
159
160 fp = fopen(tmp_file, "w");
161 assert_non_null(fp);
162
163 nwritten = fwrite(LOCALHOST_DSS_LINE,
164 sizeof(char),
165 strlen(LOCALHOST_DSS_LINE),
166 fp);
167 if (nwritten != strlen(LOCALHOST_DSS_LINE)) {
168 rc = -1;
169 goto close_fp;
170 }
171
172 close_fp:
173 fclose(fp);
174
175 return rc;
176 }
177
178 static int teardown_knownhosts_file(void **state)
179 {
180 char *tmp_file = *state;
181
182 if (tmp_file == NULL) {
183 return -1;
184 }
185
186 unlink(tmp_file);
187 SAFE_FREE(tmp_file);
188
189 return 0;
190 }
191
192 static void torture_knownhosts_parse_line_rsa(void **state) {
193 struct ssh_knownhosts_entry *entry = NULL;
194 int rc;
195
196 (void) state;
197
198 rc = ssh_known_hosts_parse_line("localhost",
199 LOCALHOST_RSA_LINE,
200 &entry);
201 assert_int_equal(rc, SSH_OK);
202
203 assert_string_equal(entry->hostname, "localhost");
204 assert_non_null(entry->unparsed);
205 assert_non_null(entry->publickey);
206 assert_int_equal(ssh_key_type(entry->publickey), SSH_KEYTYPE_RSA);
207 assert_string_equal(entry->comment, "What a Wurst!");
208
209 SSH_KNOWNHOSTS_ENTRY_FREE(entry);
210
211 rc = ssh_known_hosts_parse_line("127.0.0.1",
212 LOCALHOST_RSA_LINE,
213 &entry);
214 assert_int_equal(rc, SSH_OK);
215
216 assert_string_equal(entry->hostname, "127.0.0.1");
217 assert_non_null(entry->unparsed);
218 assert_non_null(entry->publickey);
219 assert_int_equal(ssh_key_type(entry->publickey), SSH_KEYTYPE_RSA);
220 assert_string_equal(entry->comment, "What a Wurst!");
221
222 SSH_KNOWNHOSTS_ENTRY_FREE(entry);
223 }
224
225 static void torture_knownhosts_parse_line_ecdsa(void **state) {
226 struct ssh_knownhosts_entry *entry = NULL;
227 int rc;
228
229 (void) state;
230
231 rc = ssh_known_hosts_parse_line("localhost",
232 LOCALHOST_ECDSA_SHA1_NISTP256_LINE,
233 &entry);
234 assert_int_equal(rc, SSH_OK);
235
236 assert_string_equal(entry->hostname, "localhost");
237 assert_non_null(entry->unparsed);
238 assert_non_null(entry->publickey);
239 assert_int_equal(ssh_key_type(entry->publickey), SSH_KEYTYPE_ECDSA_P256);
240
241 SSH_KNOWNHOSTS_ENTRY_FREE(entry);
242 }
243
244 static void torture_knownhosts_parse_line_default_ed25519(void **state) {
245 struct ssh_knownhosts_entry *entry = NULL;
246 int rc;
247
248 (void) state;
249
250 rc = ssh_known_hosts_parse_line("localhost",
251 LOCALHOST_DEFAULT_ED25519,
252 &entry);
253 assert_int_equal(rc, SSH_OK);
254
255 assert_string_equal(entry->hostname, "localhost");
256 assert_non_null(entry->unparsed);
257 assert_non_null(entry->publickey);
258 assert_int_equal(ssh_key_type(entry->publickey), SSH_KEYTYPE_ED25519);
259
260 SSH_KNOWNHOSTS_ENTRY_FREE(entry);
261 }
262
263 static void torture_knownhosts_parse_line_port_ed25519(void **state) {
264 struct ssh_knownhosts_entry *entry = NULL;
265 int rc;
266
267 (void) state;
268
269 rc = ssh_known_hosts_parse_line("[localhost]:2222",
270 LOCALHOST_PORT_ED25519,
271 &entry);
272 assert_int_equal(rc, SSH_OK);
273
274 assert_string_equal(entry->hostname, "[localhost]:2222");
275 assert_non_null(entry->unparsed);
276 assert_non_null(entry->publickey);
277 assert_int_equal(ssh_key_type(entry->publickey), SSH_KEYTYPE_ED25519);
278
279 SSH_KNOWNHOSTS_ENTRY_FREE(entry);
280 }
281
282 static void torture_knownhosts_parse_line_port_wildcard(void **state)
283 {
284 struct ssh_knownhosts_entry *entry = NULL;
285 int rc;
286
287 (void) state;
288
289 rc = ssh_known_hosts_parse_line("localhost",
290 LOCALHOST_PORT_WILDCARD,
291 &entry);
292 assert_int_equal(rc, SSH_OK);
293
294 assert_string_equal(entry->hostname, "localhost");
295 assert_non_null(entry->unparsed);
296 assert_non_null(entry->publickey);
297 assert_int_equal(ssh_key_type(entry->publickey), SSH_KEYTYPE_ED25519);
298
299 SSH_KNOWNHOSTS_ENTRY_FREE(entry);
300 }
301
302 static void torture_knownhosts_parse_line_standard_port(void **state)
303 {
304 struct ssh_knownhosts_entry *entry = NULL;
305 int rc;
306
307 (void) state;
308
309 rc = ssh_known_hosts_parse_line("localhost",
310 LOCALHOST_STANDARD_PORT,
311 &entry);
312 assert_int_equal(rc, SSH_OK);
313
314 assert_string_equal(entry->hostname, "localhost");
315 assert_non_null(entry->unparsed);
316 assert_non_null(entry->publickey);
317 assert_int_equal(ssh_key_type(entry->publickey), SSH_KEYTYPE_ED25519);
318
319 SSH_KNOWNHOSTS_ENTRY_FREE(entry);
320 }
321
322 static void torture_knownhosts_parse_line_pattern_ed25519(void **state) {
323 struct ssh_knownhosts_entry *entry = NULL;
324 int rc;
325
326 (void) state;
327
328 rc = ssh_known_hosts_parse_line("localhost",
329 LOCALHOST_PATTERN_ED25519,
330 &entry);
331 assert_int_equal(rc, SSH_OK);
332
333 assert_string_equal(entry->hostname, "localhost");
334 assert_non_null(entry->unparsed);
335 assert_non_null(entry->publickey);
336 assert_int_equal(ssh_key_type(entry->publickey), SSH_KEYTYPE_ED25519);
337
338 SSH_KNOWNHOSTS_ENTRY_FREE(entry);
339 }
340
341 static void torture_knownhosts_parse_line_hashed_ed25519(void **state) {
342 struct ssh_knownhosts_entry *entry = NULL;
343 int rc;
344
345 (void) state;
346
347 rc = ssh_known_hosts_parse_line("localhost",
348 LOCALHOST_HASHED_ED25519,
349 &entry);
350 assert_int_equal(rc, SSH_OK);
351
352 assert_string_equal(entry->hostname, "localhost");
353 assert_non_null(entry->unparsed);
354 assert_non_null(entry->publickey);
355 assert_int_equal(ssh_key_type(entry->publickey), SSH_KEYTYPE_ED25519);
356
357 SSH_KNOWNHOSTS_ENTRY_FREE(entry);
358 }
359
360 static void torture_knownhosts_read_file(void **state)
361 {
362 const char *knownhosts_file = *state;
363 struct ssh_list *entry_list = NULL;
364 struct ssh_iterator *it = NULL;
365 struct ssh_knownhosts_entry *entry = NULL;
366 enum ssh_keytypes_e type;
367 int rc;
368
369 rc = ssh_known_hosts_read_entries("localhost",
370 knownhosts_file,
371 &entry_list);
372 assert_int_equal(rc, SSH_OK);
373 assert_non_null(entry_list);
374 it = ssh_list_get_iterator(entry_list);
375 assert_non_null(it);
376
377 /* First key in known hosts file is ED25519 */
378 entry = ssh_iterator_value(struct ssh_knownhosts_entry *, it);
379 assert_non_null(entry);
380
381 assert_string_equal(entry->hostname, "localhost");
382 type = ssh_key_type(entry->publickey);
383 assert_int_equal(type, SSH_KEYTYPE_ED25519);
384 assert_non_null(it->next);
385
386 it = it->next;
387
388 /* Second key in known hosts file is RSA */
389 entry = ssh_iterator_value(struct ssh_knownhosts_entry *, it);
390 assert_non_null(entry);
391
392 assert_string_equal(entry->hostname, "localhost");
393 type = ssh_key_type(entry->publickey);
394 assert_int_equal(type, SSH_KEYTYPE_RSA);
395 assert_null(it->next);
396
397 it = ssh_list_get_iterator(entry_list);
398 for (;it != NULL; it = it->next) {
399 entry = ssh_iterator_value(struct ssh_knownhosts_entry *, it);
400 SSH_KNOWNHOSTS_ENTRY_FREE(entry);
401 }
402 ssh_list_free(entry_list);
403 }
404
405 static void torture_knownhosts_get_algorithms_names(void **state)
406 {
407 const char *knownhosts_file = *state;
408 ssh_session session;
409 const char *expect = "ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa";
410 char *names = NULL;
411 bool process_config = false;
412
413 session = ssh_new();
414 assert_non_null(session);
415
416 /* This makes sure the global configuration file is not processed */
417 ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &process_config);
418
419 ssh_options_set(session, SSH_OPTIONS_HOST, "localhost");
420 ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, knownhosts_file);
421
422 names = ssh_known_hosts_get_algorithms_names(session);
423 assert_non_null(names);
424 assert_string_equal(names, expect);
425
426 SAFE_FREE(names);
427 ssh_free(session);
428 }
429
430 /* Do not remove this test if we completely remove DSA support! */
431 static void torture_knownhosts_get_algorithms_names_unsupported(void **state)
432 {
433 const char *knownhosts_file = *state;
434 ssh_session session;
435 char *names = NULL;
436 bool process_config = false;
437
438 session = ssh_new();
439 assert_non_null(session);
440
441 /* This makes sure the global configuration file is not processed */
442 ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &process_config);
443
444 ssh_options_set(session, SSH_OPTIONS_HOST, "localhost");
445 ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, knownhosts_file);
446
447 names = ssh_known_hosts_get_algorithms_names(session);
448 assert_null(names);
449
450 ssh_free(session);
451 }
452
453 static void torture_knownhosts_algorithms_wanted(void **state)
454 {
455 const char *knownhosts_file = *state;
456 char *algo_list = NULL;
457 ssh_session session;
458 bool process_config = false;
459 const char *wanted = "ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,"
460 "rsa-sha2-256,ecdsa-sha2-nistp521";
461 const char *expect = "rsa-sha2-256,ecdsa-sha2-nistp384,"
462 "ecdsa-sha2-nistp256,ecdsa-sha2-nistp521";
463 int verbose = 4;
464
465 session = ssh_new();
466 assert_non_null(session);
467
468 ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbose);
469
470 /* This makes sure the global configuration file is not processed */
471 ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &process_config);
472
473 /* Set the wanted list of hostkeys, ordered by preference */
474 ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, wanted);
475
476 ssh_options_set(session, SSH_OPTIONS_HOST, "localhost");
477 ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, knownhosts_file);
478
479 algo_list = ssh_client_select_hostkeys(session);
480 assert_non_null(algo_list);
481 assert_string_equal(algo_list, expect);
482 free(algo_list);
483
484 ssh_free(session);
485 }
486
487 static void torture_knownhosts_algorithms_negative(UNUSED_PARAM(void **state))
488 {
489 const char *wanted = NULL;
490 const char *expect = NULL;
491
492 char *algo_list = NULL;
493
494 char *cwd = NULL;
495 char *tmp_dir = NULL;
496
497 bool process_config = false;
498 int verbose = 4;
499 int rc = 0;
500
501 ssh_session session;
502 /* Create temporary directory */
503 cwd = torture_get_current_working_dir();
504 assert_non_null(cwd);
505
506 tmp_dir = torture_make_temp_dir(template);
507 assert_non_null(tmp_dir);
508
509 rc = torture_change_dir(tmp_dir);
510 assert_int_equal(rc, 0);
511
512 session = ssh_new();
513 assert_non_null(session);
514
515 ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbose);
516 ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &process_config);
517 ssh_options_set(session, SSH_OPTIONS_HOST, "localhost");
518
519 /* Test with unknown key type in known_hosts */
520 wanted = "rsa-sha2-256";
521 ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, wanted);
522 torture_write_file("unknown_key_type", "localhost unknown AAAABBBBCCCC");
523 ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, "unknown_key_type");
524 algo_list = ssh_client_select_hostkeys(session);
525 assert_non_null(algo_list);
526 assert_string_equal(algo_list, wanted);
527 SAFE_FREE(algo_list);
528
529 /* Test with unsupported, but existing types */
530 wanted = "[email protected],"
531 "[email protected]";
532 ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, wanted);
533 algo_list = ssh_client_select_hostkeys(session);
534 assert_null(algo_list);
535
536 /* In FIPS mode, test filtering keys not allowed */
537 if (ssh_fips_mode()) {
538 wanted = "ssh-ed25519,rsa-sha2-256,ssh-rsa";
539 expect = "rsa-sha2-256";
540 ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, wanted);
541 torture_write_file("no_fips", LOCALHOST_DEFAULT_ED25519);
542 ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, "no_fips");
543 algo_list = ssh_client_select_hostkeys(session);
544 assert_non_null(algo_list);
545 assert_string_equal(algo_list, expect);
546 SAFE_FREE(algo_list);
547 }
548
549 ssh_free(session);
550
551 /* Teardown */
552 rc = torture_change_dir(cwd);
553 assert_int_equal(rc, 0);
554
555 rc = torture_rmdirs(tmp_dir);
556 assert_int_equal(rc, 0);
557
558 SAFE_FREE(tmp_dir);
559 SAFE_FREE(cwd);
560 }
561
562 #ifndef _WIN32 /* There is no /dev/null on Windows */
563 static void torture_knownhosts_host_exists(void **state)
564 {
565 const char *knownhosts_file = *state;
566 enum ssh_known_hosts_e found;
567 ssh_session session;
568
569 session = ssh_new();
570 assert_non_null(session);
571
572 ssh_options_set(session, SSH_OPTIONS_HOST, "localhost");
573 ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, knownhosts_file);
574
575 /* This makes sure the system's known_hosts are not used */
576 ssh_options_set(session, SSH_OPTIONS_GLOBAL_KNOWNHOSTS, "/dev/null");
577 found = ssh_session_has_known_hosts_entry(session);
578 assert_int_equal(found, SSH_KNOWN_HOSTS_OK);
579
580 /* This makes sure the check will not fail when the system's known_hosts is
581 * not accessible*/
582 ssh_options_set(session, SSH_OPTIONS_GLOBAL_KNOWNHOSTS, "./unaccessible");
583 found = ssh_session_has_known_hosts_entry(session);
584 assert_int_equal(found, SSH_KNOWN_HOSTS_OK);
585
586 /* This makes sure the check will fail for an unknown host */
587 ssh_options_set(session, SSH_OPTIONS_HOST, "wurstbrot");
588 found = ssh_session_has_known_hosts_entry(session);
589 assert_int_equal(found, SSH_KNOWN_HOSTS_UNKNOWN);
590
591 ssh_free(session);
592 }
593
594 static void torture_knownhosts_host_exists_global(void **state)
595 {
596 const char *knownhosts_file = *state;
597 enum ssh_known_hosts_e found;
598 ssh_session session;
599
600 session = ssh_new();
601 assert_non_null(session);
602
603 ssh_options_set(session, SSH_OPTIONS_HOST, "localhost");
604 ssh_options_set(session, SSH_OPTIONS_GLOBAL_KNOWNHOSTS, knownhosts_file);
605
606 /* This makes sure the user's known_hosts are not used */
607 ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, "/dev/null");
608 found = ssh_session_has_known_hosts_entry(session);
609 assert_int_equal(found, SSH_KNOWN_HOSTS_OK);
610
611 /* This makes sure the check will not fail when the user's known_hosts is
612 * not accessible*/
613 ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, "./unaccessible");
614 found = ssh_session_has_known_hosts_entry(session);
615 assert_int_equal(found, SSH_KNOWN_HOSTS_OK);
616
617 /* This makes sure the check will fail for an unknown host */
618 ssh_options_set(session, SSH_OPTIONS_HOST, "wurstbrot");
619 found = ssh_session_has_known_hosts_entry(session);
620 assert_int_equal(found, SSH_KNOWN_HOSTS_UNKNOWN);
621
622 ssh_free(session);
623 }
624
625 static void torture_knownhosts_algorithms(void **state)
626 {
627 const char *knownhosts_file = *state;
628 char *algo_list = NULL;
629 ssh_session session;
630 bool process_config = false;
631 const char *expect = "ssh-ed25519,rsa-sha2-512,rsa-sha2-256,"
632 "ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,"
633 "ecdsa-sha2-nistp256,"
634 "[email protected],"
635 "[email protected]";
636 const char *expect_fips = "rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp521,"
637 "ecdsa-sha2-nistp384,ecdsa-sha2-nistp256";
638
639 session = ssh_new();
640 assert_non_null(session);
641
642 /* This makes sure the global configuration file is not processed */
643 ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &process_config);
644
645 ssh_options_set(session, SSH_OPTIONS_HOST, "localhost");
646 ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, knownhosts_file);
647 /* This makes sure the system's known_hosts are not used */
648 ssh_options_set(session, SSH_OPTIONS_GLOBAL_KNOWNHOSTS, "/dev/null");
649
650 algo_list = ssh_client_select_hostkeys(session);
651 assert_non_null(algo_list);
652 if (ssh_fips_mode()) {
653 assert_string_equal(algo_list, expect_fips);
654 } else {
655 assert_string_equal(algo_list, expect);
656 }
657 free(algo_list);
658
659 ssh_free(session);
660 }
661
662 static void torture_knownhosts_algorithms_global(void **state)
663 {
664 const char *knownhosts_file = *state;
665 char *algo_list = NULL;
666 ssh_session session;
667 bool process_config = false;
668 const char *expect = "ssh-ed25519,rsa-sha2-512,rsa-sha2-256,"
669 "ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,"
670 "ecdsa-sha2-nistp256,"
671 "[email protected],"
672 "[email protected]";
673 const char *expect_fips = "rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp521,"
674 "ecdsa-sha2-nistp384,ecdsa-sha2-nistp256";
675
676 session = ssh_new();
677 assert_non_null(session);
678
679 /* This makes sure the global configuration file is not processed */
680 ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &process_config);
681
682 ssh_options_set(session, SSH_OPTIONS_HOST, "localhost");
683 /* This makes sure the current-user's known hosts are not used */
684 ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, "/dev/null");
685 ssh_options_set(session, SSH_OPTIONS_GLOBAL_KNOWNHOSTS, knownhosts_file);
686
687 algo_list = ssh_client_select_hostkeys(session);
688 assert_non_null(algo_list);
689 if (ssh_fips_mode()) {
690 assert_string_equal(algo_list, expect_fips);
691 } else {
692 assert_string_equal(algo_list, expect);
693 }
694 free(algo_list);
695
696 ssh_free(session);
697 }
698
699 static int setup_bad_knownhosts_file(void **state)
700 {
701 char *tmp_file = NULL;
702 size_t nwritten;
703 FILE *fp = NULL;
704 int rc = 0;
705
706 tmp_file = torture_create_temp_file(TMP_FILE_NAME);
707 assert_non_null(tmp_file);
708
709 *state = tmp_file;
710
711 fp = fopen(tmp_file, "w");
712 assert_non_null(fp);
713
714 nwritten = fwrite(LOCALHOST_DEFAULT_ED25519,
715 sizeof(char),
716 strlen(LOCALHOST_DEFAULT_ED25519),
717 fp);
718 if (nwritten != strlen(LOCALHOST_DEFAULT_ED25519)) {
719 rc = -1;
720 goto close_fp;
721 }
722
723 nwritten = fwrite("\n", sizeof(char), 1, fp);
724 if (nwritten != 1) {
725 rc = -1;
726 goto close_fp;
727 }
728
729 #define LOCALHOST_BAD_LINE "localhost \n"
730 nwritten = fwrite(LOCALHOST_BAD_LINE,
731 sizeof(char),
732 strlen(LOCALHOST_BAD_LINE),
733 fp);
734 if (nwritten != strlen(LOCALHOST_BAD_LINE)) {
735 rc = -1;
736 goto close_fp;
737 }
738
739 close_fp:
740 fclose(fp);
741
742 return rc;
743 }
744
745 static void torture_knownhosts_has_entry(void **state)
746 {
747 const char *knownhosts_file = *state;
748 enum ssh_known_hosts_e found;
749 ssh_session session;
750 bool process_config = false;
751 struct ssh_knownhosts_entry *entry = NULL;
752
753 session = ssh_new();
754 assert_non_null(session);
755
756 /* This makes sure the global configuration file is not processed */
757 ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &process_config);
758
759 ssh_options_set(session, SSH_OPTIONS_HOST, "localhost");
760 /* This makes sure the current-user's known hosts are not used */
761 ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, "/dev/null");
762 ssh_options_set(session, SSH_OPTIONS_GLOBAL_KNOWNHOSTS, knownhosts_file);
763
764 /* Error is expected -- this tests the memory is not leaked from this
765 * test case */
766 found = ssh_session_has_known_hosts_entry(session);
767 assert_int_equal(found, SSH_KNOWN_HOSTS_ERROR);
768
769 found = ssh_session_get_known_hosts_entry(session, &entry);
770 assert_int_equal(found, SSH_KNOWN_HOSTS_ERROR);
771 assert_null(entry);
772
773 ssh_free(session);
774 }
775 #endif /* _WIN32 There is no /dev/null on Windows */
776
777 int torture_run_tests(void) {
778 int rc;
779 struct CMUnitTest tests[] = {
780 cmocka_unit_test(torture_knownhosts_parse_line_rsa),
781 cmocka_unit_test(torture_knownhosts_parse_line_ecdsa),
782 cmocka_unit_test(torture_knownhosts_parse_line_default_ed25519),
783 cmocka_unit_test(torture_knownhosts_parse_line_port_ed25519),
784 cmocka_unit_test(torture_knownhosts_parse_line_port_wildcard),
785 cmocka_unit_test(torture_knownhosts_parse_line_standard_port),
786 cmocka_unit_test(torture_knownhosts_parse_line_pattern_ed25519),
787 cmocka_unit_test(torture_knownhosts_parse_line_hashed_ed25519),
788 cmocka_unit_test_setup_teardown(torture_knownhosts_read_file,
789 setup_knownhosts_file,
790 teardown_knownhosts_file),
791 cmocka_unit_test_setup_teardown(torture_knownhosts_read_file,
792 setup_knownhosts_file_duplicate,
793 teardown_knownhosts_file),
794 cmocka_unit_test_setup_teardown(torture_knownhosts_get_algorithms_names,
795 setup_knownhosts_file,
796 teardown_knownhosts_file),
797 cmocka_unit_test_setup_teardown(torture_knownhosts_get_algorithms_names_unsupported,
798 setup_knownhosts_file_unsupported_type,
799 teardown_knownhosts_file),
800 cmocka_unit_test_setup_teardown(torture_knownhosts_algorithms_wanted,
801 setup_knownhosts_file,
802 teardown_knownhosts_file),
803 cmocka_unit_test(torture_knownhosts_algorithms_negative),
804 #ifndef _WIN32
805 cmocka_unit_test_setup_teardown(torture_knownhosts_host_exists,
806 setup_knownhosts_file,
807 teardown_knownhosts_file),
808 cmocka_unit_test_setup_teardown(torture_knownhosts_host_exists_global,
809 setup_knownhosts_file,
810 teardown_knownhosts_file),
811 cmocka_unit_test_setup_teardown(torture_knownhosts_algorithms,
812 setup_knownhosts_file,
813 teardown_knownhosts_file),
814 cmocka_unit_test_setup_teardown(torture_knownhosts_algorithms_global,
815 setup_knownhosts_file,
816 teardown_knownhosts_file),
817 cmocka_unit_test_setup_teardown(torture_knownhosts_has_entry,
818 setup_bad_knownhosts_file,
819 teardown_knownhosts_file),
820 #endif
821 };
822
823 ssh_init();
824 torture_filter_tests(tests);
825 rc = cmocka_run_group_tests(tests, NULL, NULL);
826 ssh_finalize();
827 return rc;
828 }