Debian Codegraph @

tests/client/torture_knownhosts.c

raw
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2010 by Aris Adamantiadis
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 "torture.h"
27 #include "torture_key.h"
28
29 #include <sys/types.h>
30 #include <pwd.h>
31 #include <errno.h>
32
33 #include "session.c"
34 #include "known_hosts.c"
35
36 #define TMP_FILE_TEMPLATE "known_hosts_XXXXXX"
37
38 #define BADRSA "AAAAB3NzaC1yc2EAAAADAQABAAABAQChm5" \
39 "a6Av65O8cKtx5YXOnui3wJnYE6A6J/I4kZSAibbn14Jcl+34VJQwv96f25AxNmo" \
40 "NwoiZV93IzdypQmiuieh6s6wB9WhYjU9K/6CkIpNhpCxswA90b3ePjS7LnR9B9J" \
41 "slPSbG1H0KC1c5lb7G3utXteXtM+4YvCvpN5VdC4CpghT+p0cwN2Na8Md5vRItz" \
42 "YgIytryNn7LLiwYfoSxvWigFrTTZsrVtCOYyNgklmffpGdzuC43wdANvTewfI9G" \
43 "o71r8EXmEc228CrYPmb8Scv3mpXFK/BosohSGkPlEHu9lf3YjnknBicDaVtJOYp" \
44 "wnXJPjZo2EhG79HxDRpjJHH"
45 #define BADED25519 "AAAAC3NzaC1lZDI1NTE5AAAAIE74wHmKKkrxpW/dZ69pKPlMoWG9VvWfrNnUkWRQqaDa"
46
47 static int sshd_setup(void **state)
48 {
49 torture_setup_sshd_server(state, false);
50
51 return 0;
52 }
53
54 static int sshd_teardown(void **state) {
55 torture_teardown_sshd_server(state);
56
57 return 0;
58 }
59
60 static int session_setup(void **state)
61 {
62 struct torture_state *s = *state;
63 int verbosity = torture_libssh_verbosity();
64 struct passwd *pwd;
65 bool process_config = false;
66 int rc;
67
68 pwd = getpwnam("bob");
69 assert_non_null(pwd);
70
71 rc = setuid(pwd->pw_uid);
72 assert_return_code(rc, errno);
73
74 s->ssh.session = ssh_new();
75 assert_non_null(s->ssh.session);
76
77 ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
78 ssh_options_set(s->ssh.session, SSH_OPTIONS_PROCESS_CONFIG,
79 &process_config);
80 ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
81 ssh_options_set(s->ssh.session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
82
83 return 0;
84 }
85
86 static int session_teardown(void **state)
87 {
88 struct torture_state *s = *state;
89
90 ssh_disconnect(s->ssh.session);
91 ssh_free(s->ssh.session);
92
93 return 0;
94 }
95
96
97 static void torture_knownhosts_port(void **state) {
98 struct torture_state *s = *state;
99 ssh_session session = s->ssh.session;
100 char tmp_file[1024] = {0};
101 char *known_hosts_file = NULL;
102 char buffer[200];
103 char *p;
104 FILE *file;
105 int rc;
106 bool process_config = false;
107
108 snprintf(tmp_file,
109 sizeof(tmp_file),
110 "%s/%s",
111 s->socket_dir,
112 TMP_FILE_TEMPLATE);
113
114 known_hosts_file = torture_create_temp_file(tmp_file);
115 assert_non_null(known_hosts_file);
116
117 rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
118 assert_ssh_return_code(session, rc);
119
120 rc = ssh_connect(session);
121 assert_ssh_return_code(session, rc);
122
123 session->opts.port = 1234;
124 rc = ssh_write_knownhost(session);
125 assert_ssh_return_code(session, rc);
126
127 file = fopen(known_hosts_file, "r");
128 assert_non_null(file);
129 p = fgets(buffer, sizeof(buffer), file);
130 assert_non_null(p);
131 fclose(file);
132 buffer[sizeof(buffer) - 1] = '\0';
133 assert_non_null(strstr(buffer,"[127.0.0.10]:1234 "));
134
135 ssh_disconnect(session);
136 ssh_free(session);
137
138 /* Now, connect back to the ssh server and verify the known host line */
139 s->ssh.session = session = ssh_new();
140
141 ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &process_config);
142 ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
143 ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
144 free(known_hosts_file);
145
146 rc = ssh_connect(session);
147 assert_ssh_return_code(session, rc);
148
149 session->opts.port = 1234;
150 rc = ssh_is_server_known(session);
151 assert_int_equal(rc, SSH_SERVER_KNOWN_OK);
152 }
153
154 static void torture_knownhosts_wildcard(void **state)
155 {
156 struct torture_state *s = *state;
157 ssh_session session = s->ssh.session;
158 char tmp_file[1024] = {0};
159 char *known_hosts_file = NULL;
160 const char *key = NULL;
161 FILE *file;
162 int rc;
163
164 snprintf(tmp_file,
165 sizeof(tmp_file),
166 "%s/%s",
167 s->socket_dir,
168 TMP_FILE_TEMPLATE);
169
170 known_hosts_file = torture_create_temp_file(tmp_file);
171 assert_non_null(known_hosts_file);
172
173 file = fopen(known_hosts_file, "w");
174 assert_non_null(file);
175 key = torture_get_testkey_pub(SSH_KEYTYPE_RSA);
176 fprintf(file, "[127.0.0.10]:* %s\n", key);
177 fclose(file);
178
179 rc = ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
180 assert_ssh_return_code(session, rc);
181 rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
182 assert_ssh_return_code(session, rc);
183 free(known_hosts_file);
184
185 rc = ssh_connect(session);
186 assert_ssh_return_code(session, rc);
187
188 rc = ssh_is_server_known(session);
189 assert_int_equal(rc, SSH_SERVER_KNOWN_OK);
190 }
191
192 static void torture_knownhosts_standard_port(void **state)
193 {
194 struct torture_state *s = *state;
195 ssh_session session = s->ssh.session;
196 char tmp_file[1024] = {0};
197 char *known_hosts_file = NULL;
198 const char *key = NULL;
199 FILE *file;
200 int rc;
201
202 snprintf(tmp_file,
203 sizeof(tmp_file),
204 "%s/%s",
205 s->socket_dir,
206 TMP_FILE_TEMPLATE);
207
208 known_hosts_file = torture_create_temp_file(tmp_file);
209 assert_non_null(known_hosts_file);
210
211 file = fopen(known_hosts_file, "w");
212 assert_non_null(file);
213 key = torture_get_testkey_pub(SSH_KEYTYPE_RSA);
214 fprintf(file, "[127.0.0.10]:22 %s\n", key);
215 fclose(file);
216
217 rc = ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
218 assert_ssh_return_code(session, rc);
219 rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
220 assert_ssh_return_code(session, rc);
221 free(known_hosts_file);
222
223 rc = ssh_connect(session);
224 assert_ssh_return_code(session, rc);
225
226 rc = ssh_is_server_known(session);
227 assert_int_equal(rc, SSH_SERVER_KNOWN_OK);
228 }
229
230 static void torture_knownhosts_fail(void **state) {
231 struct torture_state *s = *state;
232 ssh_session session = s->ssh.session;
233 char tmp_file[1024] = {0};
234 char *known_hosts_file = NULL;
235 FILE *file;
236 int rc;
237
238 snprintf(tmp_file,
239 sizeof(tmp_file),
240 "%s/%s",
241 s->socket_dir,
242 TMP_FILE_TEMPLATE);
243
244 known_hosts_file = torture_create_temp_file(tmp_file);
245 assert_non_null(known_hosts_file);
246
247 rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
248 assert_ssh_return_code(session, rc);
249
250 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "rsa-sha2-256");
251 assert_ssh_return_code(session, rc);
252
253 file = fopen(known_hosts_file, "w");
254 assert_non_null(file);
255 free(known_hosts_file);
256
257 fprintf(file, "127.0.0.10 ssh-rsa %s\n", BADRSA);
258 fclose(file);
259
260 rc = ssh_connect(session);
261 assert_ssh_return_code(session, rc);
262
263 rc = ssh_is_server_known(session);
264 assert_int_equal(rc, SSH_SERVER_KNOWN_CHANGED);
265 }
266
267 static void torture_knownhosts_other(void **state) {
268 struct torture_state *s = *state;
269 ssh_session session = s->ssh.session;
270 char tmp_file[1024] = {0};
271 char *known_hosts_file = NULL;
272 FILE *file;
273 int rc;
274
275 snprintf(tmp_file,
276 sizeof(tmp_file),
277 "%s/%s",
278 s->socket_dir,
279 TMP_FILE_TEMPLATE);
280
281 known_hosts_file = torture_create_temp_file(tmp_file);
282 assert_non_null(known_hosts_file);
283
284 rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
285 assert_ssh_return_code(session, rc);
286
287 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "ecdsa-sha2-nistp521");
288 assert_ssh_return_code(session, rc);
289
290 file = fopen(known_hosts_file, "w");
291 assert_non_null(file);
292 free(known_hosts_file);
293
294 fprintf(file, "127.0.0.10 ssh-rsa %s\n", BADRSA);
295 fclose(file);
296
297 rc = ssh_connect(session);
298 assert_ssh_return_code(session, rc);
299
300 rc = ssh_is_server_known(session);
301 assert_int_equal(rc, SSH_SERVER_FOUND_OTHER);
302 }
303
304 static void torture_knownhosts_other_auto(void **state) {
305 struct torture_state *s = *state;
306 ssh_session session = s->ssh.session;
307 char tmp_file[1024] = {0};
308 char *known_hosts_file = NULL;
309 int rc;
310 bool process_config = false;
311
312 snprintf(tmp_file,
313 sizeof(tmp_file),
314 "%s/%s",
315 s->socket_dir,
316 TMP_FILE_TEMPLATE);
317
318 known_hosts_file = torture_create_temp_file(tmp_file);
319 assert_non_null(known_hosts_file);
320
321 rc = ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
322 assert_ssh_return_code(session, rc);
323
324 rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
325 assert_ssh_return_code(session, rc);
326
327 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "ecdsa-sha2-nistp521");
328 assert_ssh_return_code(session, rc);
329
330 rc = ssh_connect(session);
331 assert_ssh_return_code(session, rc);
332
333 rc = ssh_is_server_known(session);
334 assert_int_equal(rc, SSH_SERVER_NOT_KNOWN);
335
336 rc = ssh_write_knownhost(session);
337 assert_ssh_return_code(session, rc);
338
339 ssh_disconnect(session);
340 ssh_free(session);
341
342 /* connect again and check host key */
343 session = ssh_new();
344 assert_non_null(session);
345
346 s->ssh.session = session;
347
348 rc = ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &process_config);
349 assert_ssh_return_code(session, rc);
350
351 rc = ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
352 assert_ssh_return_code(session, rc);
353
354 rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
355 assert_ssh_return_code(session, rc);
356
357 rc = ssh_connect(session);
358 assert_ssh_return_code(session, rc);
359
360 /* ssh-rsa is the default but libssh should try ssh-ed25519 instead */
361 rc = ssh_is_server_known(session);
362 assert_int_equal(rc, SSH_SERVER_KNOWN_OK);
363
364 /* session will be freed by session_teardown() */
365 free(known_hosts_file);
366 }
367
368 static void torture_knownhosts_conflict(void **state) {
369 struct torture_state *s = *state;
370 ssh_session session = s->ssh.session;
371 char tmp_file[1024] = {0};
372 char *known_hosts_file = NULL;
373 FILE *file;
374 int rc;
375 bool process_config = false;
376
377 snprintf(tmp_file,
378 sizeof(tmp_file),
379 "%s/%s",
380 s->socket_dir,
381 TMP_FILE_TEMPLATE);
382
383 known_hosts_file = torture_create_temp_file(tmp_file);
384 assert_non_null(known_hosts_file);
385
386 rc = ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
387 assert_ssh_return_code(session, rc);
388
389 rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
390 assert_ssh_return_code(session, rc);
391
392 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "rsa-sha2-256");
393 assert_ssh_return_code(session, rc);
394
395 file = fopen(known_hosts_file, "w");
396 assert_non_null(file);
397 fprintf(file, "127.0.0.10 ssh-rsa %s\n", BADRSA);
398 fprintf(file, "127.0.0.10 ssh-ed25519 %s\n", BADED25519);
399 fclose(file);
400
401 rc = ssh_connect(session);
402 assert_ssh_return_code(session, rc);
403
404 rc = ssh_is_server_known(session);
405 assert_int_equal(rc, SSH_SERVER_KNOWN_CHANGED);
406
407 rc = ssh_write_knownhost(session);
408 assert_ssh_return_code(session, rc);
409
410 ssh_disconnect(session);
411 ssh_free(session);
412
413 /* connect again and check host key */
414 session = ssh_new();
415 assert_non_null(session);
416
417 s->ssh.session = session;
418
419 rc = ssh_options_set(session, SSH_OPTIONS_PROCESS_CONFIG, &process_config);
420 assert_ssh_return_code(session, rc);
421
422 ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
423 ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
424 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "rsa-sha2-256");
425 assert_ssh_return_code(session, rc);
426
427 rc = ssh_connect(session);
428 assert_ssh_return_code(session, rc);
429
430 rc = ssh_is_server_known(session);
431 assert_int_equal(rc, SSH_SERVER_KNOWN_OK);
432
433 /* session will be freed by session_teardown() */
434 free(known_hosts_file);
435 }
436
437 static void torture_knownhosts_no_hostkeychecking(void **state)
438 {
439
440 struct torture_state *s = *state;
441 ssh_session session = s->ssh.session;
442 char tmp_file[1024] = {0};
443 char *known_hosts_file = NULL;
444 enum ssh_known_hosts_e found;
445 int strict_host_key_checking = 0;
446 int rc;
447
448 snprintf(tmp_file,
449 sizeof(tmp_file),
450 "%s/%s",
451 s->socket_dir,
452 TMP_FILE_TEMPLATE);
453
454 known_hosts_file = torture_create_temp_file(tmp_file);
455 assert_non_null(known_hosts_file);
456
457 rc = ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, known_hosts_file);
458 assert_ssh_return_code(session, rc);
459 free(known_hosts_file);
460
461 rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, "ecdsa-sha2-nistp521");
462 assert_ssh_return_code(session, rc);
463
464 rc = ssh_connect(session);
465 assert_ssh_return_code(session, rc);
466
467 found = ssh_session_is_known_server(session);
468 assert_int_equal(found, SSH_KNOWN_HOSTS_UNKNOWN);
469
470 rc = ssh_options_set(session, SSH_OPTIONS_STRICTHOSTKEYCHECK, &strict_host_key_checking);
471 assert_ssh_return_code(session, rc);
472
473 found = ssh_session_is_known_server(session);
474 assert_int_equal(found, SSH_KNOWN_HOSTS_OK);
475 }
476
477 int torture_run_tests(void) {
478 int rc;
479 struct CMUnitTest tests[] = {
480 cmocka_unit_test_setup_teardown(torture_knownhosts_wildcard,
481 session_setup,
482 session_teardown),
483 cmocka_unit_test_setup_teardown(torture_knownhosts_standard_port,
484 session_setup,
485 session_teardown),
486 cmocka_unit_test_setup_teardown(torture_knownhosts_port,
487 session_setup,
488 session_teardown),
489 cmocka_unit_test_setup_teardown(torture_knownhosts_fail,
490 session_setup,
491 session_teardown),
492 cmocka_unit_test_setup_teardown(torture_knownhosts_other,
493 session_setup,
494 session_teardown),
495 cmocka_unit_test_setup_teardown(torture_knownhosts_other_auto,
496 session_setup,
497 session_teardown),
498 cmocka_unit_test_setup_teardown(torture_knownhosts_conflict,
499 session_setup,
500 session_teardown),
501 cmocka_unit_test_setup_teardown(torture_knownhosts_no_hostkeychecking,
502 session_setup,
503 session_teardown),
504 };
505
506 ssh_init();
507
508 torture_filter_tests(tests);
509 rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
510
511 ssh_finalize();
512 return rc;
513 }