Debian Codegraph @

tests/server/torture_server_algorithms.c

raw
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2019 by Red Hat, Inc.
5 *
6 * Author: Anderson Toshiyuki Sasaki <[email protected]>
7 *
8 * The SSH Library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or (at your
11 * option) any later version.
12 *
13 * The SSH Library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 * License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with the SSH Library; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 * MA 02111-1307, USA.
22 */
23
24 #include "config.h"
25
26 #define LIBSSH_STATIC
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <errno.h>
31 #include <pwd.h>
32
33 #include "torture.h"
34 #include "torture_key.h"
35 #include "libssh/libssh.h"
36 #include "libssh/priv.h"
37 #include "libssh/session.h"
38 #include "libssh/token.h"
39
40 #include "test_server.h"
41 #include "default_cb.h"
42
43 const char template[] = "temp_dir_XXXXXX";
44
45 struct test_server_st {
46 struct torture_state *state;
47 char *cwd;
48 char *temp_dir;
49 char rsa_hostkey[1024];
50 };
51
52 static int setup_files(void **state)
53 {
54 struct test_server_st *tss;
55 struct torture_state *s;
56 char sshd_path[1024];
57 char log_file[1024];
58
59 int rc;
60
61 tss = (struct test_server_st*)calloc(1, sizeof(struct test_server_st));
62 assert_non_null(tss);
63
64 torture_setup_socket_dir((void **)&s);
65 assert_non_null(s->socket_dir);
66
67 /* Set the default interface for the server */
68 setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "10", 1);
69
70 snprintf(sshd_path,
71 sizeof(sshd_path),
72 "%s/sshd",
73 s->socket_dir);
74
75 rc = mkdir(sshd_path, 0755);
76 assert_return_code(rc, errno);
77
78 snprintf(log_file, sizeof(log_file), "%s/sshd/log", s->socket_dir);
79
80 snprintf(tss->rsa_hostkey,
81 sizeof(tss->rsa_hostkey),
82 "%s/sshd/ssh_host_rsa_key",
83 s->socket_dir);
84 torture_write_file(tss->rsa_hostkey, torture_get_testkey(SSH_KEYTYPE_RSA, 0));
85
86 /* not to mix up the client and server messages */
87 s->log_file = strdup(log_file);
88
89 tss->state = s;
90 *state = tss;
91
92 return 0;
93 }
94
95 static int teardown_files(void **state)
96 {
97 struct torture_state *s;
98 struct test_server_st *tss;
99
100 tss = *state;
101 assert_non_null(tss);
102
103 s = tss->state;
104 assert_non_null(s);
105
106 torture_teardown_socket_dir((void **)&s);
107 SAFE_FREE(tss);
108
109 return 0;
110 }
111
112 static int setup_temp_dir(void **state)
113 {
114 struct test_server_st *tss = *state;
115 struct torture_state *s;
116
117 char *cwd = NULL;
118 char *tmp_dir = NULL;
119
120 assert_non_null(tss);
121
122 s = tss->state;
123 assert_non_null(s);
124
125 cwd = torture_get_current_working_dir();
126 assert_non_null(cwd);
127
128 tmp_dir = torture_make_temp_dir(template);
129 assert_non_null(tmp_dir);
130
131 tss->cwd = cwd;
132 tss->temp_dir = tmp_dir;
133
134 return 0;
135 }
136
137 static int teardown_temp_dir(void **state)
138 {
139 struct test_server_st *tss = *state;
140 int rc;
141
142 assert_non_null(tss);
143
144 rc = torture_change_dir(tss->cwd);
145 assert_int_equal(rc, 0);
146
147 rc = torture_rmdirs(tss->temp_dir);
148 assert_int_equal(rc, 0);
149
150 SAFE_FREE(tss->temp_dir);
151 SAFE_FREE(tss->cwd);
152
153 return 0;
154 }
155
156 static int start_server(void **state)
157 {
158 struct test_server_st *tss = *state;
159 struct torture_state *s;
160
161 assert_non_null(tss);
162
163 s = tss->state;
164 assert_non_null(s);
165
166 /* Start the server using the default values */
167 torture_setup_libssh_server((void **)&s, "./test_server/test_server");
168 assert_non_null(s);
169
170 return 0;
171 }
172
173 static int stop_server(void **state)
174 {
175 struct torture_state *s;
176 struct test_server_st *tss;
177
178 int rc;
179
180 tss = *state;
181 assert_non_null(tss);
182
183 s = tss->state;
184 assert_non_null(s);
185
186 rc = torture_terminate_process(s->srv_pidfile);
187 assert_return_code(rc, errno);
188
189 unlink(s->srv_pidfile);
190
191 return 0;
192 }
193
194 static int session_setup(void **state)
195 {
196 struct test_server_st *tss = *state;
197 struct torture_state *s;
198 int verbosity = torture_libssh_verbosity();
199 struct passwd *pwd;
200 bool b = false;
201 int rc;
202
203 assert_non_null(tss);
204
205 /* Make sure we do not test the agent */
206 unsetenv("SSH_AUTH_SOCK");
207
208 s = tss->state;
209 assert_non_null(s);
210
211 pwd = getpwnam("bob");
212 assert_non_null(pwd);
213
214 rc = setuid(pwd->pw_uid);
215 assert_return_code(rc, errno);
216
217 s->ssh.session = ssh_new();
218 assert_non_null(s->ssh.session);
219
220 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
221 assert_ssh_return_code(s->ssh.session, rc);
222 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
223 assert_ssh_return_code(s->ssh.session, rc);
224 /* Make sure no other configuration options from system will get used */
225 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_PROCESS_CONFIG, &b);
226 assert_ssh_return_code(s->ssh.session, rc);
227
228 return 0;
229 }
230
231 static int session_teardown(void **state)
232 {
233 struct test_server_st *tss = *state;
234 struct torture_state *s;
235
236 assert_non_null(tss);
237
238 s = tss->state;
239 assert_non_null(s);
240
241 ssh_disconnect(s->ssh.session);
242 ssh_free(s->ssh.session);
243
244 return 0;
245 }
246
247 /*
248 * Check that the handshake works with an AEAD cipher configured
249 * but with no overlap for HMACs. AEAD ciphers have an implied HMAC
250 * so no HMAC overlap in the handshake should not fail the connection.
251 */
252 static void test_algorithm_no_hmac_overlap(void **state, const char *algorithm)
253 {
254 struct test_server_st *tss = *state;
255 struct torture_state *s = NULL;
256 char config_content[4096];
257
258 ssh_session session = NULL;
259
260 int rc;
261
262 assert_non_null(tss);
263 s = tss->state;
264 assert_non_null(s);
265
266 /* Prepare config file */
267 snprintf(config_content,
268 sizeof(config_content),
269 "HostKey %s\nCiphers %s\nMACs %s\n",
270 tss->rsa_hostkey,
271 algorithm,
272 "[email protected]");
273
274 assert_non_null(s->srv_config);
275 torture_write_file(s->srv_config, config_content);
276
277 SSH_LOG(SSH_LOG_TRACE,
278 "Config file %s content: \n\n%s\n",
279 s->srv_config,
280 config_content);
281
282 /* Start server */
283 rc = start_server(state);
284 assert_int_equal(rc, 0);
285
286 /* Setup session */
287 rc = session_setup(state);
288 assert_int_equal(rc, 0);
289
290 session = s->ssh.session;
291 assert_non_null(session);
292
293 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
294 assert_int_equal(rc, SSH_OK);
295
296 rc = ssh_options_set(session, SSH_OPTIONS_CIPHERS_C_S, algorithm);
297 assert_int_equal(rc, SSH_OK);
298
299 rc = ssh_options_set(session, SSH_OPTIONS_CIPHERS_S_C, algorithm);
300 assert_int_equal(rc, SSH_OK);
301
302 rc = ssh_options_set(session, SSH_OPTIONS_HMAC_C_S, "hmac-sha2-512");
303 assert_int_equal(rc, SSH_OK);
304
305 rc = ssh_options_set(session, SSH_OPTIONS_HMAC_S_C, "hmac-sha2-512");
306 assert_int_equal(rc, SSH_OK);
307
308 rc = ssh_connect(session);
309 assert_int_equal(rc, SSH_OK);
310
311 rc = ssh_userauth_none(session, NULL);
312 /* This request should return a SSH_REQUEST_DENIED error */
313 if (rc == SSH_ERROR) {
314 assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED);
315 }
316 rc = ssh_userauth_list(session, NULL);
317 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
318
319 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
320 assert_ssh_return_code(session, rc);
321
322 rc = session_teardown(state);
323 assert_int_equal(rc, 0);
324
325 rc = stop_server(state);
326 assert_int_equal(rc, 0);
327
328 SAFE_FREE(s->srv_additional_config);
329 }
330
331 static void torture_algorithm_chacha20_with_no_hmac_overlap(void **state)
332 {
333 if (ssh_fips_mode()) {
334 skip();
335 }
336 test_algorithm_no_hmac_overlap(state, "[email protected]");
337 }
338
339 static void torture_algorithm_aes256gcm_with_no_hmac_overlap(void **state)
340 {
341 test_algorithm_no_hmac_overlap(state, "[email protected]");
342 }
343
344 static void torture_algorithm_aes128gcm_with_no_hmac_overlap(void **state)
345 {
346 test_algorithm_no_hmac_overlap(state, "[email protected]");
347 }
348
349 /*
350 * Check the self-compatibility of a given key exchange method.
351 */
352 static void test_kex_self_compat(void **state, const char *kex)
353 {
354 struct test_server_st *tss = *state;
355 struct torture_state *s = NULL;
356 ssh_session session = NULL;
357 char config_content[4096];
358 int rc;
359
360 assert_non_null(tss);
361 s = tss->state;
362 assert_non_null(s);
363
364 /* Prepare config file */
365 snprintf(config_content,
366 sizeof(config_content),
367 "HostKey %s\nKexAlgorithms %s\n",
368 tss->rsa_hostkey,
369 kex);
370
371 assert_non_null(s->srv_config);
372 torture_write_file(s->srv_config, config_content);
373
374 SSH_LOG(SSH_LOG_TRACE,
375 "Config file %s content: \n\n%s\n",
376 s->srv_config,
377 config_content);
378
379 rc = start_server(state);
380 assert_int_equal(rc, 0);
381
382 rc = session_setup(state);
383 assert_int_equal(rc, 0);
384
385 session = s->ssh.session;
386 assert_non_null(session);
387
388 rc = ssh_options_set(session, SSH_OPTIONS_KEY_EXCHANGE, kex);
389 assert_int_equal(rc, SSH_OK);
390
391 rc = ssh_connect(session);
392 assert_int_equal(rc, SSH_OK);
393
394 rc = ssh_userauth_publickey_auto(session, NULL, NULL);
395 assert_ssh_return_code(session, rc);
396
397 rc = session_teardown(state);
398 assert_int_equal(rc, 0);
399
400 rc = stop_server(state);
401 assert_int_equal(rc, 0);
402 }
403
404 static void torture_algorithm_mlkem768x25119_self_compat(void **state)
405 {
406 if (ssh_fips_mode()) {
407 skip();
408 }
409 test_kex_self_compat(state, "mlkem768x25519-sha256");
410 }
411
412 static void torture_algorithm_mlkem768nistp256_self_compat(void **state)
413 {
414 if (ssh_fips_mode()) {
415 skip();
416 }
417 test_kex_self_compat(state, "mlkem768nistp256-sha256");
418 }
419
420 #ifdef HAVE_MLKEM1024
421 static void torture_algorithm_mlkem1024nistp384_self_compat(void **state)
422 {
423 if (ssh_fips_mode()) {
424 skip();
425 }
426 test_kex_self_compat(state, "mlkem1024nistp384-sha384");
427 }
428 #endif /* HAVE_MLKEM1024 */
429
430 int torture_run_tests(void)
431 {
432 int rc;
433 struct CMUnitTest tests[] = {
434 cmocka_unit_test_setup_teardown(torture_algorithm_chacha20_with_no_hmac_overlap,
435 setup_temp_dir, teardown_temp_dir),
436 cmocka_unit_test_setup_teardown(torture_algorithm_aes256gcm_with_no_hmac_overlap,
437 setup_temp_dir, teardown_temp_dir),
438 cmocka_unit_test_setup_teardown(torture_algorithm_aes128gcm_with_no_hmac_overlap,
439 setup_temp_dir, teardown_temp_dir),
440 cmocka_unit_test_setup_teardown(torture_algorithm_mlkem768x25119_self_compat,
441 setup_temp_dir, teardown_temp_dir),
442 cmocka_unit_test_setup_teardown(torture_algorithm_mlkem768nistp256_self_compat,
443 setup_temp_dir, teardown_temp_dir),
444 #ifdef HAVE_MLKEM1024
445 cmocka_unit_test_setup_teardown(torture_algorithm_mlkem1024nistp384_self_compat,
446 setup_temp_dir, teardown_temp_dir),
447 #endif /* HAVE_MLKEM1024 */
448 };
449
450 ssh_init();
451
452 torture_filter_tests(tests);
453 rc = cmocka_run_group_tests(tests,
454 setup_files,
455 teardown_files);
456
457 ssh_finalize();
458
459 return rc;
460 }