Debian Codegraph @

tests/client/torture_rekey.c

raw
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2018 by Red Hat, Inc.
5 *
6 * Authors: Jakub Jelen <[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 "torture.h"
29 #include "libssh/sftp.h"
30 #include "libssh/libssh.h"
31 #include "libssh/priv.h"
32 #include "libssh/session.h"
33 #include "libssh/crypto.h"
34 #include "libssh/token.h"
35
36 #include <errno.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <pwd.h>
41
42 #define KEX_RETRY 32
43
44 static uint64_t bytes = 2048; /* 2KB (more than the authentication phase) */
45
46 static int sshd_setup(void **state)
47 {
48 torture_setup_sshd_server(state, false);
49
50 return 0;
51 }
52
53 static int sshd_teardown(void **state)
54 {
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 b = 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_HOST, TORTURE_SSH_SERVER);
79
80 /* Authenticate as alice with bob's pubkey */
81 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE);
82 assert_int_equal(rc, SSH_OK);
83
84 /* Make sure no other configuration options from system will get used */
85 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_PROCESS_CONFIG, &b);
86 assert_ssh_return_code(s->ssh.session, rc);
87
88 /* Make sure we do not interfere with another ssh-agent */
89 unsetenv("SSH_AUTH_SOCK");
90 unsetenv("SSH_AGENT_PID");
91
92 return 0;
93 }
94
95 static int session_teardown(void **state)
96 {
97 struct torture_state *s = *state;
98
99 ssh_free(s->ssh.session);
100 s->ssh.session = NULL;
101
102 return 0;
103 }
104
105 /* Check that the default limits for rekeying are enforced.
106 * the limits are too high for testsuite to verify so
107 * we should be fine with checking the values in internal
108 * structures
109 */
110 static void torture_rekey_default(void **state)
111 {
112 struct torture_state *s = *state;
113 int rc;
114 struct ssh_crypto_struct *c = NULL;
115
116 /* Define preferred ciphers: */
117 if (ssh_fips_mode()) {
118 /* We do not have any FIPS allowed cipher with different block size */
119 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_CIPHERS_C_S,
120 "[email protected]");
121 } else {
122 /* (out) C->S has 8B block */
123 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_CIPHERS_C_S,
124 "[email protected]");
125 }
126 assert_ssh_return_code(s->ssh.session, rc);
127 /* (in) S->C has 16B block */
128 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_CIPHERS_S_C,
129 "aes128-cbc");
130 assert_ssh_return_code(s->ssh.session, rc);
131
132 rc = ssh_connect(s->ssh.session);
133 assert_ssh_return_code(s->ssh.session, rc);
134
135 c = s->ssh.session->current_crypto;
136 /* The blocks limit is set correctly */
137 /* For S->C (in) we have 16B block => 2**(L/4) blocks */
138 assert_int_equal(c->in_cipher->max_blocks,
139 (uint64_t)1 << (2 * c->in_cipher->blocksize));
140 if (ssh_fips_mode()) {
141 /* We do not have any FIPS allowed cipher with different block size */
142 assert_int_equal(c->in_cipher->max_blocks,
143 (uint64_t)1 << (2 * c->in_cipher->blocksize));
144 } else {
145 /* The C->S (out) we have 8B block => 1 GB limit */
146 assert_int_equal(c->out_cipher->max_blocks,
147 ((uint64_t)1 << 30) / c->out_cipher->blocksize);
148 }
149
150 ssh_disconnect(s->ssh.session);
151 }
152
153 static void sanity_check_session_size(void **state, uint64_t rekey_limit)
154 {
155 struct torture_state *s = *state;
156 struct ssh_crypto_struct *c = NULL;
157
158 c = s->ssh.session->current_crypto;
159 assert_non_null(c);
160 assert_int_equal(c->in_cipher->max_blocks,
161 rekey_limit / c->in_cipher->blocksize);
162 assert_int_equal(c->out_cipher->max_blocks,
163 rekey_limit / c->out_cipher->blocksize);
164 /* when strict kex is used, the newkeys reset the sequence number */
165 if ((s->ssh.session->flags & SSH_SESSION_FLAG_KEX_STRICT) != 0) {
166 assert_int_equal(c->out_cipher->packets, s->ssh.session->send_seq);
167 assert_int_equal(c->in_cipher->packets, s->ssh.session->recv_seq);
168 } else {
169 /* Otherwise we have less encrypted packets than transferred
170 * (first are not encrypted) */
171 assert_true(c->out_cipher->packets < s->ssh.session->send_seq);
172 assert_true(c->in_cipher->packets < s->ssh.session->recv_seq);
173 }
174 }
175 static void sanity_check_session(void **state)
176 {
177 sanity_check_session_size(state, bytes);
178 }
179
180 /* We lower the rekey limits manually and check that the rekey
181 * really happens when sending data
182 */
183 static void torture_rekey_send(void **state)
184 {
185 struct torture_state *s = *state;
186 int rc;
187 char data[256];
188 unsigned int i;
189 struct ssh_crypto_struct *c = NULL;
190 unsigned char *secret_hash = NULL;
191
192 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_REKEY_DATA, &bytes);
193 assert_ssh_return_code(s->ssh.session, rc);
194
195 rc = ssh_connect(s->ssh.session);
196 assert_ssh_return_code(s->ssh.session, rc);
197
198 sanity_check_session(state);
199 /* Copy the initial secret hash = session_id so we know we changed keys later */
200 c = s->ssh.session->current_crypto;
201 assert_non_null(c);
202 secret_hash = malloc(c->digest_len);
203 assert_non_null(secret_hash);
204 memcpy(secret_hash, c->secret_hash, c->digest_len);
205
206 /* OpenSSH can not rekey before authentication so authenticate here */
207 rc = ssh_userauth_none(s->ssh.session, NULL);
208 /* This request should return a SSH_REQUEST_DENIED error */
209 if (rc == SSH_ERROR) {
210 assert_int_equal(ssh_get_error_code(s->ssh.session), SSH_REQUEST_DENIED);
211 }
212 rc = ssh_userauth_list(s->ssh.session, NULL);
213 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
214
215 rc = ssh_userauth_publickey_auto(s->ssh.session, NULL, NULL);
216 assert_int_equal(rc, SSH_AUTH_SUCCESS);
217
218 /* send ignore packets of up to 1KB to trigger rekey. Send little bit more
219 * to make sure it completes with all different ciphers */
220 memset(data, 0, sizeof(data));
221 memset(data, 'A', 128);
222 for (i = 0; i < KEX_RETRY; i++) {
223 ssh_send_ignore(s->ssh.session, data);
224 ssh_handle_packets(s->ssh.session, 50);
225 }
226
227 /* The rekey limit was restored in the new crypto to the same value */
228 c = s->ssh.session->current_crypto;
229 assert_int_equal(c->in_cipher->max_blocks, bytes / c->in_cipher->blocksize);
230 assert_int_equal(c->out_cipher->max_blocks, bytes / c->out_cipher->blocksize);
231 /* Check that the secret hash is different than initially */
232 assert_memory_not_equal(secret_hash, c->secret_hash, c->digest_len);
233 free(secret_hash);
234
235 ssh_disconnect(s->ssh.session);
236 }
237
238 #ifdef WITH_SFTP
239 static void session_setup_sftp(void **state)
240 {
241 struct torture_state *s = *state;
242 int rc;
243
244 rc = ssh_connect(s->ssh.session);
245 assert_ssh_return_code(s->ssh.session, rc);
246
247 /* OpenSSH can not rekey before authentication so authenticate here */
248 rc = ssh_userauth_none(s->ssh.session, NULL);
249 /* This request should return a SSH_REQUEST_DENIED error */
250 if (rc == SSH_ERROR) {
251 assert_int_equal(ssh_get_error_code(s->ssh.session), SSH_REQUEST_DENIED);
252 }
253 rc = ssh_userauth_list(s->ssh.session, NULL);
254 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
255
256 rc = ssh_userauth_publickey_auto(s->ssh.session, NULL, NULL);
257 assert_int_equal(rc, SSH_AUTH_SUCCESS);
258
259 /* Initialize SFTP session */
260 s->ssh.tsftp = torture_sftp_session(s->ssh.session);
261 assert_non_null(s->ssh.tsftp);
262 }
263
264 static int session_setup_sftp_client(void **state)
265 {
266 struct torture_state *s = *state;
267 int rc;
268
269 session_setup(state);
270
271 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_REKEY_DATA, &bytes);
272 assert_ssh_return_code(s->ssh.session, rc);
273
274 session_setup_sftp(state);
275
276 return 0;
277 }
278
279 #define MAX_XFER_BUF_SIZE 16384
280
281 /* To trigger rekey by receiving data, the easiest thing is probably to
282 * use sftp
283 */
284 static void torture_rekey_recv_size(void **state, uint64_t rekey_limit)
285 {
286 struct torture_state *s = *state;
287 struct ssh_crypto_struct *c = NULL;
288 unsigned char *secret_hash = NULL;
289
290 char libssh_tmp_file[] = "/tmp/libssh_sftp_test_XXXXXX";
291 char buf[MAX_XFER_BUF_SIZE];
292 ssize_t bytesread;
293 ssize_t byteswritten;
294 int fd;
295 sftp_file file;
296 mode_t mask;
297 int rc;
298
299 sanity_check_session_size(state, rekey_limit);
300 /* Copy the initial secret hash = session_id so we know we changed keys later */
301 c = s->ssh.session->current_crypto;
302 assert_non_null(c);
303 secret_hash = malloc(c->digest_len);
304 assert_non_null(secret_hash);
305 memcpy(secret_hash, c->secret_hash, c->digest_len);
306
307 /* Download a file */
308 file = sftp_open(s->ssh.tsftp->sftp, SSH_EXECUTABLE, O_RDONLY, 0);
309 assert_non_null(file);
310
311 mask = umask(S_IRWXO | S_IRWXG);
312 fd = mkstemp(libssh_tmp_file);
313 umask(mask);
314 unlink(libssh_tmp_file);
315
316 for (;;) {
317 bytesread = sftp_read(file, buf, MAX_XFER_BUF_SIZE);
318 if (bytesread == 0) {
319 break; /* EOF */
320 }
321 assert_false(bytesread < 0);
322
323 byteswritten = write(fd, buf, bytesread);
324 assert_int_equal(byteswritten, bytesread);
325 }
326
327 rc = sftp_close(file);
328 assert_int_equal(rc, SSH_NO_ERROR);
329 close(fd);
330
331 /* The rekey limit was restored in the new crypto to the same value */
332 c = s->ssh.session->current_crypto;
333 assert_int_equal(c->in_cipher->max_blocks,
334 rekey_limit / c->in_cipher->blocksize);
335 assert_int_equal(c->out_cipher->max_blocks,
336 rekey_limit / c->out_cipher->blocksize);
337 /* Check that the secret hash is different than initially */
338 assert_memory_not_equal(secret_hash, c->secret_hash, c->digest_len);
339 free(secret_hash);
340
341 torture_sftp_close(s->ssh.tsftp);
342 ssh_disconnect(s->ssh.session);
343 }
344
345 static void torture_rekey_recv(void **state)
346 {
347 torture_rekey_recv_size(state, bytes);
348 }
349 #endif /* WITH_SFTP */
350
351 /* Rekey time requires rekey after specified time and is off by default.
352 * Setting the time to small enough value and waiting, we should trigger
353 * rekey on the first sent packet afterward.
354 */
355 static void torture_rekey_time(void **state)
356 {
357 struct torture_state *s = *state;
358 int rc;
359 char data[256];
360 unsigned int i;
361 uint32_t time = 3; /* 3 seconds */
362 struct ssh_crypto_struct *c = NULL;
363 unsigned char *secret_hash = NULL;
364
365 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_REKEY_TIME, &time);
366 assert_ssh_return_code(s->ssh.session, rc);
367 /* The time is internally stored in microseconds */
368 assert_int_equal(time * 1000, s->ssh.session->opts.rekey_time);
369
370 rc = ssh_connect(s->ssh.session);
371 assert_ssh_return_code(s->ssh.session, rc);
372
373 /* Copy the initial secret hash = session_id so we know we changed keys later */
374 c = s->ssh.session->current_crypto;
375 secret_hash = malloc(c->digest_len);
376 assert_non_null(secret_hash);
377 memcpy(secret_hash, c->secret_hash, c->digest_len);
378
379 /* OpenSSH can not rekey before authentication so authenticate here */
380 rc = ssh_userauth_none(s->ssh.session, NULL);
381 /* This request should return a SSH_REQUEST_DENIED error */
382 if (rc == SSH_ERROR) {
383 assert_int_equal(ssh_get_error_code(s->ssh.session), SSH_REQUEST_DENIED);
384 }
385 rc = ssh_userauth_list(s->ssh.session, NULL);
386 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
387
388 rc = ssh_userauth_publickey_auto(s->ssh.session, NULL, NULL);
389 assert_int_equal(rc, SSH_AUTH_SUCCESS);
390
391 /* Send some data. This should not trigger rekey yet */
392 memset(data, 0, sizeof(data));
393 memset(data, 'A', 8);
394 for (i = 0; i < 3; i++) {
395 ssh_send_ignore(s->ssh.session, data);
396 ssh_handle_packets(s->ssh.session, 50);
397 }
398
399 /* Check that the secret hash is the same */
400 c = s->ssh.session->current_crypto;
401 assert_memory_equal(secret_hash, c->secret_hash, c->digest_len);
402
403 /* Wait some more time */
404 sleep(3);
405
406 /* send some more data to trigger rekey and handle the
407 * key exchange "in background" */
408 for (i = 0; i < 8; i++) {
409 ssh_send_ignore(s->ssh.session, data);
410 ssh_handle_packets(s->ssh.session, 50);
411 }
412
413 /* Check that the secret hash is different than initially */
414 c = s->ssh.session->current_crypto;
415 assert_memory_not_equal(secret_hash, c->secret_hash, c->digest_len);
416 free(secret_hash);
417
418 ssh_disconnect(s->ssh.session);
419 }
420
421 /* We lower the rekey limits manually and check that the rekey
422 * really happens when sending data
423 */
424 static void torture_rekey_server_send(void **state)
425 {
426 struct torture_state *s = *state;
427 int rc;
428 char data[256];
429 unsigned int i;
430 struct ssh_crypto_struct *c = NULL;
431 unsigned char *secret_hash = NULL;
432 const char *sshd_config = "RekeyLimit 2K none";
433
434 torture_update_sshd_config(state, sshd_config);
435
436 rc = ssh_connect(s->ssh.session);
437 assert_ssh_return_code(s->ssh.session, rc);
438
439 /* Copy the initial secret hash = session_id so we know we changed keys later */
440 c = s->ssh.session->current_crypto;
441 secret_hash = malloc(c->digest_len);
442 assert_non_null(secret_hash);
443 memcpy(secret_hash, c->secret_hash, c->digest_len);
444
445 /* OpenSSH can not rekey before authentication so authenticate here */
446 rc = ssh_userauth_none(s->ssh.session, NULL);
447 /* This request should return a SSH_REQUEST_DENIED error */
448 if (rc == SSH_ERROR) {
449 assert_int_equal(ssh_get_error_code(s->ssh.session), SSH_REQUEST_DENIED);
450 }
451 rc = ssh_userauth_list(s->ssh.session, NULL);
452 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
453
454 rc = ssh_userauth_publickey_auto(s->ssh.session, NULL, NULL);
455 assert_int_equal(rc, SSH_AUTH_SUCCESS);
456
457 /* send ignore packets of up to 1KB to trigger rekey */
458 memset(data, 0, sizeof(data));
459 memset(data, 'A', 128);
460 for (i = 0; i < 20; i++) {
461 ssh_send_ignore(s->ssh.session, data);
462 ssh_handle_packets(s->ssh.session, 50);
463 }
464
465 /* Check that the secret hash is different than initially */
466 c = s->ssh.session->current_crypto;
467 assert_memory_not_equal(secret_hash, c->secret_hash, c->digest_len);
468 free(secret_hash);
469
470 ssh_disconnect(s->ssh.session);
471 }
472
473 static void torture_rekey_different_kex(void **state)
474 {
475 struct torture_state *s = *state;
476 int rc;
477 char data[256];
478 unsigned int i;
479 struct ssh_crypto_struct *c = NULL;
480 unsigned char *secret_hash = NULL;
481 size_t secret_hash_len = 0;
482 const char *kex1 = "diffie-hellman-group14-sha256,curve25519-sha256,ecdh-sha2-nistp256";
483 const char *kex2 = "diffie-hellman-group18-sha512,diffie-hellman-group16-sha512,ecdh-sha2-nistp521";
484
485 /* Use short digest for initial key exchange */
486 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_KEY_EXCHANGE, kex1);
487 assert_ssh_return_code(s->ssh.session, rc);
488
489 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_REKEY_DATA, &bytes);
490 assert_ssh_return_code(s->ssh.session, rc);
491
492 rc = ssh_connect(s->ssh.session);
493 assert_ssh_return_code(s->ssh.session, rc);
494
495 /* The blocks limit is set correctly */
496 sanity_check_session(state);
497 /* Copy the initial secret hash = session_id so we know we changed keys later */
498 c = s->ssh.session->current_crypto;
499 assert_non_null(c);
500 secret_hash = malloc(c->digest_len);
501 assert_non_null(secret_hash);
502 memcpy(secret_hash, c->secret_hash, c->digest_len);
503 secret_hash_len = c->digest_len;
504 assert_int_equal(secret_hash_len, 32); /* SHA256 len */
505
506 /* OpenSSH can not rekey before authentication so authenticate here */
507 rc = ssh_userauth_none(s->ssh.session, NULL);
508 /* This request should return a SSH_REQUEST_DENIED error */
509 if (rc == SSH_ERROR) {
510 assert_int_equal(ssh_get_error_code(s->ssh.session), SSH_REQUEST_DENIED);
511 }
512 rc = ssh_userauth_list(s->ssh.session, NULL);
513 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
514
515 rc = ssh_userauth_publickey_auto(s->ssh.session, NULL, NULL);
516 assert_int_equal(rc, SSH_AUTH_SUCCESS);
517
518 /* Now try to change preference of key exchange algorithm to something with larger digest */
519 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_KEY_EXCHANGE, kex2);
520 assert_ssh_return_code(s->ssh.session, rc);
521
522 /* send ignore packets of up to 1KB to trigger rekey. Send little bit more
523 * to make sure the rekey it completes with all different ciphers (paddings */
524 memset(data, 0, sizeof(data));
525 memset(data, 'A', 128);
526 for (i = 0; i < KEX_RETRY; i++) {
527 ssh_send_ignore(s->ssh.session, data);
528 ssh_handle_packets(s->ssh.session, 1000);
529
530 c = s->ssh.session->current_crypto;
531 /* SHA256 len */
532 if (c->digest_len != 32) {
533 break;
534 }
535 }
536
537 /* The rekey limit was restored in the new crypto to the same value */
538 c = s->ssh.session->current_crypto;
539 assert_int_equal(c->in_cipher->max_blocks, bytes / c->in_cipher->blocksize);
540 assert_int_equal(c->out_cipher->max_blocks, bytes / c->out_cipher->blocksize);
541 /* Check that the secret hash is different than initially */
542 assert_int_equal(c->digest_len, 64); /* SHA512 len */
543 assert_memory_not_equal(secret_hash, c->secret_hash, secret_hash_len);
544 /* Session ID stays same after one rekey */
545 assert_memory_equal(secret_hash, c->session_id, secret_hash_len);
546 free(secret_hash);
547
548 assert_int_equal(ssh_is_connected(s->ssh.session), 1);
549 assert_int_equal(s->ssh.session->session_state, SSH_SESSION_STATE_AUTHENTICATED);
550
551 ssh_disconnect(s->ssh.session);
552 }
553
554 static void torture_rekey_server_different_kex(void **state)
555 {
556 struct torture_state *s = *state;
557 int rc;
558 char data[256];
559 unsigned int i;
560 struct ssh_crypto_struct *c = NULL;
561 unsigned char *secret_hash = NULL;
562 size_t secret_hash_len = 0;
563 const char *sshd_config = "RekeyLimit 2K none";
564 const char *kex1 = "diffie-hellman-group14-sha256,curve25519-sha256,ecdh-sha2-nistp256";
565 const char *kex2 = "diffie-hellman-group18-sha512,diffie-hellman-group16-sha512";
566
567 /* Use short digest for initial key exchange */
568 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_KEY_EXCHANGE, kex1);
569 assert_ssh_return_code(s->ssh.session, rc);
570
571 torture_update_sshd_config(state, sshd_config);
572
573 rc = ssh_connect(s->ssh.session);
574 assert_ssh_return_code(s->ssh.session, rc);
575
576 /* Copy the initial secret hash = session_id so we know we changed keys later */
577 c = s->ssh.session->current_crypto;
578 secret_hash = malloc(c->digest_len);
579 assert_non_null(secret_hash);
580 memcpy(secret_hash, c->secret_hash, c->digest_len);
581 secret_hash_len = c->digest_len;
582 assert_int_equal(secret_hash_len, 32); /* SHA256 len */
583
584 /* OpenSSH can not rekey before authentication so authenticate here */
585 rc = ssh_userauth_none(s->ssh.session, NULL);
586 /* This request should return a SSH_REQUEST_DENIED error */
587 if (rc == SSH_ERROR) {
588 assert_int_equal(ssh_get_error_code(s->ssh.session), SSH_REQUEST_DENIED);
589 }
590 rc = ssh_userauth_list(s->ssh.session, NULL);
591 assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY);
592
593 rc = ssh_userauth_publickey_auto(s->ssh.session, NULL, NULL);
594 assert_int_equal(rc, SSH_AUTH_SUCCESS);
595
596 /* Now try to change preference of key exchange algorithm to something with larger digest */
597 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_KEY_EXCHANGE, kex2);
598 assert_ssh_return_code(s->ssh.session, rc);
599
600 /* send ignore packets of up to 1KB to trigger rekey. Send little bit more
601 * to make sure the rekey it completes with all different ciphers (paddings */
602 memset(data, 0, sizeof(data));
603 memset(data, 'A', 128);
604 for (i = 0; i < KEX_RETRY; i++) {
605 ssh_send_ignore(s->ssh.session, data);
606 ssh_handle_packets(s->ssh.session, 1000);
607
608 c = s->ssh.session->current_crypto;
609 /* SHA256 len */
610 if (c->digest_len != 32) {
611 break;
612 }
613 }
614
615 /* Check that the secret hash is different than initially */
616 c = s->ssh.session->current_crypto;
617 assert_int_equal(c->digest_len, 64); /* SHA512 len */
618 assert_memory_not_equal(secret_hash, c->secret_hash, secret_hash_len);
619 /* Session ID stays same after one rekey */
620 assert_memory_equal(secret_hash, c->session_id, secret_hash_len);
621 free(secret_hash);
622
623 ssh_disconnect(s->ssh.session);
624 }
625
626
627 #ifdef WITH_SFTP
628 static int session_setup_sftp_server(void **state)
629 {
630 const char *sshd_config = "RekeyLimit 2K none";
631
632 session_setup(state);
633
634 torture_update_sshd_config(state, sshd_config);
635
636 session_setup_sftp(state);
637
638 return 0;
639 }
640
641 static void torture_rekey_server_recv(void **state)
642 {
643 struct torture_state *s = *state;
644 struct ssh_crypto_struct *c = NULL;
645 unsigned char *secret_hash = NULL;
646 char libssh_tmp_file[] = "/tmp/libssh_sftp_test_XXXXXX";
647 char buf[MAX_XFER_BUF_SIZE];
648 ssize_t bytesread;
649 ssize_t byteswritten;
650 int fd;
651 sftp_file file;
652 mode_t mask;
653 int rc;
654
655 /* Copy the initial secret hash = session_id so we know we changed keys later */
656 c = s->ssh.session->current_crypto;
657 secret_hash = malloc(c->digest_len);
658 assert_non_null(secret_hash);
659 memcpy(secret_hash, c->secret_hash, c->digest_len);
660
661 /* Download a file */
662 file = sftp_open(s->ssh.tsftp->sftp, SSH_EXECUTABLE, O_RDONLY, 0);
663 assert_non_null(file);
664
665 mask = umask(S_IRWXO | S_IRWXG);
666 fd = mkstemp(libssh_tmp_file);
667 umask(mask);
668 unlink(libssh_tmp_file);
669
670 for (;;) {
671 bytesread = sftp_read(file, buf, MAX_XFER_BUF_SIZE);
672 if (bytesread == 0) {
673 break; /* EOF */
674 }
675 assert_false(bytesread < 0);
676
677 byteswritten = write(fd, buf, bytesread);
678 assert_int_equal(byteswritten, bytesread);
679 }
680
681 rc = sftp_close(file);
682 assert_int_equal(rc, SSH_NO_ERROR);
683 close(fd);
684
685 /* Check that the secret hash is different than initially */
686 c = s->ssh.session->current_crypto;
687 assert_memory_not_equal(secret_hash, c->secret_hash, c->digest_len);
688 free(secret_hash);
689
690 torture_sftp_close(s->ssh.tsftp);
691 ssh_disconnect(s->ssh.session);
692 }
693 #endif /* WITH_SFTP */
694
695 #ifdef WITH_ZLIB
696 /* This is disabled by OpenSSH since OpenSSH 7.4p1 */
697 #if (OPENSSH_VERSION_MAJOR == 7 && OPENSSH_VERSION_MINOR < 4) || OPENSSH_VERSION_MAJOR < 7
698 /* Compression can be funky to get right after rekey
699 */
700 static void torture_rekey_send_compression(void **state)
701 {
702 struct torture_state *s = *state;
703 const char *comp = "zlib";
704 int rc;
705
706 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_COMPRESSION_C_S, comp);
707 assert_ssh_return_code(s->ssh.session, rc);
708
709 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_COMPRESSION_S_C, comp);
710 assert_ssh_return_code(s->ssh.session, rc);
711
712 torture_rekey_send(state);
713 }
714
715 #ifdef WITH_SFTP
716 static void torture_rekey_recv_compression(void **state)
717 {
718 struct torture_state *s = *state;
719 const char *comp = "zlib";
720 int rc;
721
722 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_COMPRESSION_C_S, comp);
723 assert_ssh_return_code(s->ssh.session, rc);
724
725 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_COMPRESSION_S_C, comp);
726 assert_ssh_return_code(s->ssh.session, rc);
727
728 torture_rekey_recv(state);
729 }
730 #endif /* WITH_SFTP */
731 #endif
732
733 /* Especially the delayed compression by openssh.
734 */
735 static void torture_rekey_send_compression_delayed(void **state)
736 {
737 struct torture_state *s = *state;
738 const char *comp = "[email protected]";
739 int rc;
740
741 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_COMPRESSION_C_S, comp);
742 assert_ssh_return_code(s->ssh.session, rc);
743
744 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_COMPRESSION_S_C, comp);
745 assert_ssh_return_code(s->ssh.session, rc);
746
747 torture_rekey_send(state);
748 }
749
750 #ifdef WITH_SFTP
751 static void torture_rekey_recv_compression_delayed(void **state)
752 {
753 struct torture_state *s = *state;
754 const char *comp = "[email protected]";
755 int rc;
756
757 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_COMPRESSION_C_S, comp);
758 assert_ssh_return_code(s->ssh.session, rc);
759
760 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_COMPRESSION_S_C, comp);
761 assert_ssh_return_code(s->ssh.session, rc);
762
763 torture_rekey_recv(state);
764 }
765 #endif /* WITH_SFTP */
766 #endif /* WITH_ZLIB */
767
768 static void setup_server_for_good_guess(void *state)
769 {
770 const char *default_sshd_config = "KexAlgorithms curve25519-sha256";
771 const char *fips_sshd_config = "KexAlgorithms ecdh-sha2-nistp256";
772 const char *sshd_config = default_sshd_config;
773
774 if (ssh_fips_mode()) {
775 sshd_config = fips_sshd_config;
776 }
777 /* This sets an only supported kex algorithm that we do not have as a first
778 * option */
779 torture_update_sshd_config(state, sshd_config);
780 }
781
782 static void torture_rekey_guess_send(void **state)
783 {
784 struct torture_state *s = *state;
785
786 setup_server_for_good_guess(state);
787
788 /* Make the client send the first_kex_packet_follows flag during key
789 * exchange as well as during the rekey */
790 s->ssh.session->send_first_kex_follows = true;
791
792 torture_rekey_send(state);
793 }
794
795 static void torture_rekey_guess_wrong_send(void **state)
796 {
797 struct torture_state *s = *state;
798 const char *sshd_config = "KexAlgorithms diffie-hellman-group14-sha256";
799
800 /* This sets an only supported kex algorithm that we do not have as a first
801 * option */
802 torture_update_sshd_config(state, sshd_config);
803
804 /* Make the client send the first_kex_packet_follows flag during key
805 * exchange as well as during the rekey */
806 s->ssh.session->send_first_kex_follows = true;
807
808 torture_rekey_send(state);
809 }
810
811 #ifdef WITH_SFTP
812 static void torture_rekey_guess_recv(void **state)
813 {
814 struct torture_state *s = *state;
815 int rc;
816
817 setup_server_for_good_guess(state);
818
819 /* Make the client send the first_kex_packet_follows flag during key
820 * exchange as well as during the rekey */
821 s->ssh.session->send_first_kex_follows = true;
822
823 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_REKEY_DATA, &bytes);
824 assert_ssh_return_code(s->ssh.session, rc);
825
826 session_setup_sftp(state);
827
828 torture_rekey_recv(state);
829 }
830
831 static void torture_rekey_guess_wrong_recv(void **state)
832 {
833 struct torture_state *s = *state;
834 const char *sshd_config = "KexAlgorithms diffie-hellman-group14-sha256";
835 int rc;
836
837 /* This sets an only supported kex algorithm that we do not have as a first
838 * option */
839 torture_update_sshd_config(state, sshd_config);
840
841 /* Make the client send the first_kex_packet_follows flag during key
842 * exchange as well as during the rekey */
843 s->ssh.session->send_first_kex_follows = true;
844
845 rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_REKEY_DATA, &bytes);
846 assert_ssh_return_code(s->ssh.session, rc);
847
848 session_setup_sftp(state);
849
850 torture_rekey_recv(state);
851 }
852
853 static void torture_rekey_guess_all_combinations(void **state)
854 {
855 struct torture_state *s = *state;
856 char sshd_config[256] = "";
857 char client_kex[256] = "";
858 const char *supported = NULL;
859 struct ssh_tokens_st *s_tok = NULL;
860 uint64_t rekey_limit = 0;
861 char *p = NULL;
862 int rc, i, j;
863
864 /* The rekey limit is 1/2 of the transferred file size so we will likely get
865 * 2 rekeys per test, which still runs for acceptable time */
866 rekey_limit = atoll(SSH_EXECUTABLE_SIZE);
867 rekey_limit /= 2;
868
869 if (ssh_fips_mode()) {
870 supported = ssh_kex_get_fips_methods(SSH_KEX);
871 } else {
872 supported = ssh_kex_get_supported_method(SSH_KEX);
873 }
874 assert_non_null(supported);
875
876 s_tok = ssh_tokenize(supported, ',');
877 assert_non_null(s_tok);
878 for (i = 0; s_tok->tokens[i]; i++) {
879 /* Skip algorithms not supported by the OpenSSH server.
880 * Check also for prefix matches to distinguish vendor-specific names
881 * such as sntrup761x25519-sha512 and the @openssh.com alias */
882 if ((p = strstr(OPENSSH_KEX, s_tok->tokens[i])) == NULL ||
883 (*(p + strlen(s_tok->tokens[i])) != ',' &&
884 *(p + strlen(s_tok->tokens[i])) != '\0')) {
885 SSH_LOG(SSH_LOG_INFO, "Server: %s [skipping]", s_tok->tokens[i]);
886 continue;
887 }
888 SSH_LOG(SSH_LOG_INFO, "Server: %s", s_tok->tokens[i]);
889 snprintf(sshd_config,
890 sizeof(sshd_config),
891 "KexAlgorithms %s",
892 s_tok->tokens[i]);
893 /* This sets an only supported kex algorithm that we do not have as
894 * a first option in the client */
895 torture_update_sshd_config(state, sshd_config);
896
897 for (j = 0; s_tok->tokens[j]; j++) {
898 if (i == j) {
899 continue;
900 }
901
902 session_setup(state);
903 /* Make the client send the first_kex_packet_follows flag during key
904 * exchange as well as during the rekey */
905 s->ssh.session->send_first_kex_follows = true;
906
907 rc = ssh_options_set(s->ssh.session,
908 SSH_OPTIONS_REKEY_DATA,
909 &rekey_limit);
910 assert_ssh_return_code(s->ssh.session, rc);
911
912 /* Client kex preference will have the second of the pair and the
913 * server one as a second to negotiate on the second attempt */
914 snprintf(client_kex,
915 sizeof(client_kex),
916 "%s,%s",
917 s_tok->tokens[j],
918 s_tok->tokens[i]);
919 SSH_LOG(SSH_LOG_INFO, "Client: %s", client_kex);
920 rc = ssh_options_set(s->ssh.session,
921 SSH_OPTIONS_KEY_EXCHANGE,
922 client_kex);
923 assert_ssh_return_code(s->ssh.session, rc);
924 session_setup_sftp(state);
925 torture_rekey_recv_size(state, rekey_limit);
926 session_teardown(state);
927 }
928 }
929
930 ssh_tokens_free(s_tok);
931 }
932 #endif /* WITH_SFTP */
933
934 int torture_run_tests(void) {
935 int rc;
936 struct CMUnitTest tests[] = {
937 cmocka_unit_test_setup_teardown(torture_rekey_default,
938 session_setup,
939 session_teardown),
940 cmocka_unit_test_setup_teardown(torture_rekey_time,
941 session_setup,
942 session_teardown),
943 #ifdef WITH_SFTP
944 cmocka_unit_test_setup_teardown(torture_rekey_recv,
945 session_setup_sftp_client,
946 session_teardown),
947 #endif /* WITH_SFTP */
948 cmocka_unit_test_setup_teardown(torture_rekey_send,
949 session_setup,
950 session_teardown),
951 cmocka_unit_test_setup_teardown(torture_rekey_different_kex,
952 session_setup,
953 session_teardown),
954 #ifdef WITH_ZLIB
955 #if (OPENSSH_VERSION_MAJOR == 7 && OPENSSH_VERSION_MINOR < 4) || OPENSSH_VERSION_MAJOR < 7
956 cmocka_unit_test_setup_teardown(torture_rekey_send_compression,
957 session_setup,
958 session_teardown),
959 #ifdef WITH_SFTP
960 cmocka_unit_test_setup_teardown(torture_rekey_recv_compression,
961 session_setup_sftp_client,
962 session_teardown),
963 #endif /* WITH_SFTP */
964 #endif
965 cmocka_unit_test_setup_teardown(torture_rekey_send_compression_delayed,
966 session_setup,
967 session_teardown),
968 #ifdef WITH_SFTP
969 cmocka_unit_test_setup_teardown(torture_rekey_recv_compression_delayed,
970 session_setup_sftp_client,
971 session_teardown),
972 #endif /* WITH_SFTP */
973 #endif /* WITH_ZLIB */
974 /* TODO verify the two rekey are possible and the states are not broken after rekey */
975
976 cmocka_unit_test_setup_teardown(torture_rekey_server_different_kex,
977 session_setup,
978 session_teardown),
979 /* Note, that these tests modify the sshd_config so follow-up tests
980 * might get unexpected behavior if they do not update the server with
981 * torture_update_sshd_config() too */
982 cmocka_unit_test_setup_teardown(torture_rekey_server_send,
983 session_setup,
984 session_teardown),
985 cmocka_unit_test_setup_teardown(torture_rekey_guess_send,
986 session_setup,
987 session_teardown),
988 cmocka_unit_test_setup_teardown(torture_rekey_guess_wrong_send,
989 session_setup,
990 session_teardown),
991 #ifdef WITH_SFTP
992 cmocka_unit_test_setup_teardown(torture_rekey_server_recv,
993 session_setup_sftp_server,
994 session_teardown),
995 cmocka_unit_test_setup_teardown(torture_rekey_guess_recv,
996 session_setup,
997 session_teardown),
998 cmocka_unit_test_setup_teardown(torture_rekey_guess_wrong_recv,
999 session_setup,
1000 session_teardown),
1001 cmocka_unit_test(torture_rekey_guess_all_combinations),
1002 #endif /* WITH_SFTP */
1003 };
1004
1005 ssh_init();
1006
1007 torture_filter_tests(tests);
1008 rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
1009
1010 ssh_finalize();
1011
1012 return rc;
1013 }