| 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 ed25519_hostkey[1024]; |
| 50 |
char rsa_hostkey[1024]; |
| 51 |
char ecdsa_521_hostkey[1024]; |
| 52 |
char ecdsa_384_hostkey[1024]; |
| 53 |
char ecdsa_256_hostkey[1024]; |
| 54 |
}; |
| 55 |
|
| 56 |
static int setup_files(void **state) |
| 57 |
{ |
| 58 |
struct test_server_st *tss; |
| 59 |
struct torture_state *s; |
| 60 |
char sshd_path[1024]; |
| 61 |
|
| 62 |
int rc; |
| 63 |
|
| 64 |
tss = (struct test_server_st*)calloc(1, sizeof(struct test_server_st)); |
| 65 |
assert_non_null(tss); |
| 66 |
|
| 67 |
torture_setup_socket_dir((void **)&s); |
| 68 |
assert_non_null(s->socket_dir); |
| 69 |
|
| 70 |
/* Set the default interface for the server */ |
| 71 |
setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "10", 1); |
| 72 |
setenv("PAM_WRAPPER", "1", 1); |
| 73 |
|
| 74 |
snprintf(sshd_path, |
| 75 |
sizeof(sshd_path), |
| 76 |
"%s/sshd", |
| 77 |
s->socket_dir); |
| 78 |
|
| 79 |
rc = mkdir(sshd_path, 0755); |
| 80 |
assert_return_code(rc, errno); |
| 81 |
|
| 82 |
snprintf(tss->rsa_hostkey, |
| 83 |
sizeof(tss->rsa_hostkey), |
| 84 |
"%s/sshd/ssh_host_rsa_key", |
| 85 |
s->socket_dir); |
| 86 |
torture_write_file(tss->rsa_hostkey, torture_get_testkey(SSH_KEYTYPE_RSA, 0)); |
| 87 |
|
| 88 |
snprintf(tss->ecdsa_521_hostkey, |
| 89 |
sizeof(tss->ecdsa_521_hostkey), |
| 90 |
"%s/sshd/ssh_host_ecdsa_521_key", |
| 91 |
s->socket_dir); |
| 92 |
torture_write_file(tss->ecdsa_521_hostkey, |
| 93 |
torture_get_testkey(SSH_KEYTYPE_ECDSA_P521, 0)); |
| 94 |
|
| 95 |
snprintf(tss->ecdsa_384_hostkey, |
| 96 |
sizeof(tss->ecdsa_384_hostkey), |
| 97 |
"%s/sshd/ssh_host_ecdsa_384_key", |
| 98 |
s->socket_dir); |
| 99 |
torture_write_file(tss->ecdsa_384_hostkey, |
| 100 |
torture_get_testkey(SSH_KEYTYPE_ECDSA_P384, 0)); |
| 101 |
|
| 102 |
snprintf(tss->ecdsa_256_hostkey, |
| 103 |
sizeof(tss->ecdsa_256_hostkey), |
| 104 |
"%s/sshd/ssh_host_ecdsa_256_key", |
| 105 |
s->socket_dir); |
| 106 |
torture_write_file(tss->ecdsa_256_hostkey, |
| 107 |
torture_get_testkey(SSH_KEYTYPE_ECDSA_P256, 0)); |
| 108 |
|
| 109 |
if (!ssh_fips_mode()) { |
| 110 |
snprintf(tss->ed25519_hostkey, |
| 111 |
sizeof(tss->ed25519_hostkey), |
| 112 |
"%s/sshd/ssh_host_ed25519_key", |
| 113 |
s->socket_dir); |
| 114 |
torture_write_file(tss->ed25519_hostkey, |
| 115 |
torture_get_openssh_testkey(SSH_KEYTYPE_ED25519, 0)); |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
tss->state = s; |
| 120 |
*state = tss; |
| 121 |
|
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
static int teardown_files(void **state) |
| 126 |
{ |
| 127 |
struct torture_state *s; |
| 128 |
struct test_server_st *tss; |
| 129 |
|
| 130 |
tss = *state; |
| 131 |
assert_non_null(tss); |
| 132 |
|
| 133 |
s = tss->state; |
| 134 |
assert_non_null(s); |
| 135 |
|
| 136 |
torture_teardown_socket_dir((void **)&s); |
| 137 |
SAFE_FREE(tss); |
| 138 |
|
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
static int setup_temp_dir(void **state) |
| 143 |
{ |
| 144 |
struct test_server_st *tss = *state; |
| 145 |
struct torture_state *s; |
| 146 |
|
| 147 |
char *cwd = NULL; |
| 148 |
char *tmp_dir = NULL; |
| 149 |
|
| 150 |
assert_non_null(tss); |
| 151 |
|
| 152 |
s = tss->state; |
| 153 |
assert_non_null(s); |
| 154 |
|
| 155 |
cwd = torture_get_current_working_dir(); |
| 156 |
assert_non_null(cwd); |
| 157 |
|
| 158 |
tmp_dir = torture_make_temp_dir(template); |
| 159 |
assert_non_null(tmp_dir); |
| 160 |
|
| 161 |
tss->cwd = cwd; |
| 162 |
tss->temp_dir = tmp_dir; |
| 163 |
|
| 164 |
return 0; |
| 165 |
} |
| 166 |
|
| 167 |
static int teardown_temp_dir(void **state) |
| 168 |
{ |
| 169 |
struct test_server_st *tss = *state; |
| 170 |
int rc; |
| 171 |
|
| 172 |
assert_non_null(tss); |
| 173 |
|
| 174 |
rc = torture_change_dir(tss->cwd); |
| 175 |
assert_int_equal(rc, 0); |
| 176 |
|
| 177 |
rc = torture_rmdirs(tss->temp_dir); |
| 178 |
assert_int_equal(rc, 0); |
| 179 |
|
| 180 |
SAFE_FREE(tss->temp_dir); |
| 181 |
SAFE_FREE(tss->cwd); |
| 182 |
|
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
static int start_server(void **state) |
| 187 |
{ |
| 188 |
struct test_server_st *tss = *state; |
| 189 |
struct torture_state *s; |
| 190 |
|
| 191 |
assert_non_null(tss); |
| 192 |
|
| 193 |
s = tss->state; |
| 194 |
assert_non_null(s); |
| 195 |
|
| 196 |
/* Start the server using the default values */ |
| 197 |
torture_setup_libssh_server((void **)&s, "./test_server/test_server"); |
| 198 |
assert_non_null(s); |
| 199 |
|
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
static int stop_server(void **state) |
| 204 |
{ |
| 205 |
struct torture_state *s; |
| 206 |
struct test_server_st *tss; |
| 207 |
|
| 208 |
int rc; |
| 209 |
|
| 210 |
tss = *state; |
| 211 |
assert_non_null(tss); |
| 212 |
|
| 213 |
s = tss->state; |
| 214 |
assert_non_null(s); |
| 215 |
|
| 216 |
rc = torture_terminate_process(s->srv_pidfile); |
| 217 |
assert_return_code(rc, errno); |
| 218 |
|
| 219 |
unlink(s->srv_pidfile); |
| 220 |
|
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
static int session_setup(void **state) |
| 225 |
{ |
| 226 |
struct test_server_st *tss = *state; |
| 227 |
struct torture_state *s; |
| 228 |
int verbosity = torture_libssh_verbosity(); |
| 229 |
const char *compat_hostkeys = ssh_get_supported_methods(SSH_HOSTKEYS); |
| 230 |
struct passwd *pwd; |
| 231 |
bool b = false; |
| 232 |
int rc; |
| 233 |
|
| 234 |
assert_non_null(tss); |
| 235 |
|
| 236 |
/* Make sure we do not test the agent */ |
| 237 |
unsetenv("SSH_AUTH_SOCK"); |
| 238 |
|
| 239 |
s = tss->state; |
| 240 |
assert_non_null(s); |
| 241 |
|
| 242 |
pwd = getpwnam("bob"); |
| 243 |
assert_non_null(pwd); |
| 244 |
|
| 245 |
rc = setuid(pwd->pw_uid); |
| 246 |
assert_return_code(rc, errno); |
| 247 |
|
| 248 |
s->ssh.session = ssh_new(); |
| 249 |
assert_non_null(s->ssh.session); |
| 250 |
|
| 251 |
rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); |
| 252 |
assert_ssh_return_code(s->ssh.session, rc); |
| 253 |
rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER); |
| 254 |
assert_ssh_return_code(s->ssh.session, rc); |
| 255 |
/* Make sure no other configuration options from system will get used */ |
| 256 |
rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_PROCESS_CONFIG, &b); |
| 257 |
assert_ssh_return_code(s->ssh.session, rc); |
| 258 |
rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_HOSTKEYS, compat_hostkeys); |
| 259 |
assert_ssh_return_code(s->ssh.session, rc); |
| 260 |
|
| 261 |
return 0; |
| 262 |
} |
| 263 |
|
| 264 |
static int session_teardown(void **state) |
| 265 |
{ |
| 266 |
struct test_server_st *tss = *state; |
| 267 |
struct torture_state *s; |
| 268 |
|
| 269 |
assert_non_null(tss); |
| 270 |
|
| 271 |
s = tss->state; |
| 272 |
assert_non_null(s); |
| 273 |
|
| 274 |
ssh_disconnect(s->ssh.session); |
| 275 |
ssh_free(s->ssh.session); |
| 276 |
|
| 277 |
return 0; |
| 278 |
} |
| 279 |
|
| 280 |
static int try_config_content(void **state, const char *config_content, |
| 281 |
bool parse_global) |
| 282 |
{ |
| 283 |
struct test_server_st *tss = *state; |
| 284 |
struct torture_state *s; |
| 285 |
int rc; |
| 286 |
|
| 287 |
ssh_session session; |
| 288 |
|
| 289 |
assert_non_null(tss); |
| 290 |
|
| 291 |
s = tss->state; |
| 292 |
assert_non_null(s); |
| 293 |
|
| 294 |
assert_non_null(s->srv_config); |
| 295 |
|
| 296 |
if (parse_global) { |
| 297 |
fprintf(stderr, "Using system-wide configuration\n"); |
| 298 |
} else { |
| 299 |
/* The string is duplicated to not break the cleanup on error */ |
| 300 |
s->srv_additional_config = strdup("-g"); |
| 301 |
} |
| 302 |
|
| 303 |
torture_write_file(s->srv_config, config_content); |
| 304 |
|
| 305 |
fprintf(stderr, "Config file %s content: \n\n%s\n", s->srv_config, |
| 306 |
config_content); |
| 307 |
|
| 308 |
rc = start_server(state); |
| 309 |
assert_int_equal(rc, 0); |
| 310 |
|
| 311 |
rc = session_setup(state); |
| 312 |
assert_int_equal(rc, 0); |
| 313 |
|
| 314 |
session = s->ssh.session; |
| 315 |
assert_non_null(session); |
| 316 |
|
| 317 |
/* Authenticate as alice with bob */ |
| 318 |
rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE); |
| 319 |
assert_ssh_return_code(session, rc); |
| 320 |
|
| 321 |
rc = ssh_connect(session); |
| 322 |
assert_ssh_return_code(session, rc); |
| 323 |
|
| 324 |
rc = ssh_userauth_none(session,NULL); |
| 325 |
/* This request should return a SSH_REQUEST_DENIED error */ |
| 326 |
if (rc == SSH_ERROR) { |
| 327 |
assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED); |
| 328 |
} |
| 329 |
rc = ssh_userauth_list(session, NULL); |
| 330 |
assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY); |
| 331 |
|
| 332 |
rc = ssh_userauth_publickey_auto(session, NULL, NULL); |
| 333 |
assert_int_equal(rc, SSH_AUTH_SUCCESS); |
| 334 |
|
| 335 |
rc = session_teardown(state); |
| 336 |
assert_int_equal(rc, 0); |
| 337 |
|
| 338 |
rc = stop_server(state); |
| 339 |
assert_int_equal(rc, 0); |
| 340 |
|
| 341 |
SAFE_FREE(s->srv_additional_config); |
| 342 |
|
| 343 |
return 0; |
| 344 |
} |
| 345 |
|
| 346 |
static char *hostkey_files[6] = {0}; |
| 347 |
|
| 348 |
static size_t setup_hostkey_files(struct test_server_st *tss) |
| 349 |
{ |
| 350 |
size_t num_hostkey_files = 1; |
| 351 |
|
| 352 |
hostkey_files[0] = tss->rsa_hostkey; |
| 353 |
|
| 354 |
#ifdef TEST_ALL_CRYPTO_COMBINATIONS |
| 355 |
hostkey_files[1] = tss->ecdsa_256_hostkey; |
| 356 |
hostkey_files[2] = tss->ecdsa_384_hostkey; |
| 357 |
hostkey_files[3] = tss->ecdsa_521_hostkey; |
| 358 |
|
| 359 |
num_hostkey_files = 4; |
| 360 |
|
| 361 |
if (!ssh_fips_mode()) { |
| 362 |
hostkey_files[4] = tss->ed25519_hostkey; |
| 363 |
num_hostkey_files++; |
| 364 |
} |
| 365 |
#endif /* TEST_ALL_CRYPTO_COMBINATIONS */ |
| 366 |
|
| 367 |
return num_hostkey_files; |
| 368 |
} |
| 369 |
|
| 370 |
static void torture_server_config_hostkey(void **state) |
| 371 |
{ |
| 372 |
struct test_server_st *tss = *state; |
| 373 |
size_t i, num_hostkey_files; |
| 374 |
char config_content[4096]; |
| 375 |
|
| 376 |
int rc; |
| 377 |
|
| 378 |
assert_non_null(tss); |
| 379 |
|
| 380 |
num_hostkey_files = setup_hostkey_files(tss); |
| 381 |
|
| 382 |
for (i = 0; i < num_hostkey_files; i++) { |
| 383 |
snprintf(config_content, |
| 384 |
sizeof(config_content), |
| 385 |
"HostKey %s\n", |
| 386 |
hostkey_files[i]); |
| 387 |
|
| 388 |
rc = try_config_content(state, config_content, false); |
| 389 |
assert_int_equal(rc, 0); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
static void torture_server_config_ciphers(void **state) |
| 394 |
{ |
| 395 |
struct test_server_st *tss = *state; |
| 396 |
size_t i, j, num_hostkey_files = 1; |
| 397 |
char config_content[4096]; |
| 398 |
|
| 399 |
const char *ciphers; |
| 400 |
|
| 401 |
struct ssh_tokens_st *tokens; |
| 402 |
|
| 403 |
int rc; |
| 404 |
|
| 405 |
assert_non_null(tss); |
| 406 |
|
| 407 |
num_hostkey_files = setup_hostkey_files(tss); |
| 408 |
|
| 409 |
if (ssh_fips_mode()) { |
| 410 |
ciphers = ssh_kex_get_fips_methods(SSH_CRYPT_S_C); |
| 411 |
assert_non_null(ciphers); |
| 412 |
} else { |
| 413 |
ciphers = ssh_kex_get_default_methods(SSH_CRYPT_S_C); |
| 414 |
assert_non_null(ciphers); |
| 415 |
} |
| 416 |
|
| 417 |
tokens = ssh_tokenize(ciphers, ','); |
| 418 |
assert_non_null(tokens); |
| 419 |
|
| 420 |
for (i = 0; i < num_hostkey_files; i++) { |
| 421 |
/* Try setting all default algorithms */ |
| 422 |
snprintf(config_content, |
| 423 |
sizeof(config_content), |
| 424 |
"HostKey %s\nCiphers %s\n", |
| 425 |
hostkey_files[i], ciphers); |
| 426 |
|
| 427 |
rc = try_config_content(state, config_content, false); |
| 428 |
assert_int_equal(rc, 0); |
| 429 |
|
| 430 |
/* Try each algorithm individually */ |
| 431 |
j = 0; |
| 432 |
while(tokens->tokens[j] != NULL) { |
| 433 |
snprintf(config_content, |
| 434 |
sizeof(config_content), |
| 435 |
"HostKey %s\nCiphers %s\n", |
| 436 |
hostkey_files[i], tokens->tokens[j]); |
| 437 |
|
| 438 |
rc = try_config_content(state, config_content, false); |
| 439 |
assert_int_equal(rc, 0); |
| 440 |
|
| 441 |
j++; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
ssh_tokens_free(tokens); |
| 446 |
} |
| 447 |
|
| 448 |
static void torture_server_config_macs(void **state) |
| 449 |
{ |
| 450 |
struct test_server_st *tss = *state; |
| 451 |
size_t i, j, num_hostkey_files = 1; |
| 452 |
char config_content[4096]; |
| 453 |
|
| 454 |
const char *macs; |
| 455 |
|
| 456 |
struct ssh_tokens_st *tokens; |
| 457 |
|
| 458 |
int rc; |
| 459 |
|
| 460 |
assert_non_null(tss); |
| 461 |
|
| 462 |
num_hostkey_files = setup_hostkey_files(tss); |
| 463 |
|
| 464 |
if (ssh_fips_mode()) { |
| 465 |
macs = ssh_kex_get_fips_methods(SSH_MAC_S_C); |
| 466 |
assert_non_null(macs); |
| 467 |
} else { |
| 468 |
macs = ssh_kex_get_default_methods(SSH_MAC_S_C); |
| 469 |
assert_non_null(macs); |
| 470 |
} |
| 471 |
|
| 472 |
tokens = ssh_tokenize(macs, ','); |
| 473 |
assert_non_null(tokens); |
| 474 |
|
| 475 |
for (i = 0; i < num_hostkey_files; i++) { |
| 476 |
/* Try setting all default algorithms */ |
| 477 |
snprintf(config_content, |
| 478 |
sizeof(config_content), |
| 479 |
"HostKey %s\nMACs %s\n", |
| 480 |
hostkey_files[i], macs); |
| 481 |
|
| 482 |
rc = try_config_content(state, config_content, false); |
| 483 |
assert_int_equal(rc, 0); |
| 484 |
|
| 485 |
/* Try each algorithm individually */ |
| 486 |
j = 0; |
| 487 |
while(tokens->tokens[j] != NULL) { |
| 488 |
snprintf(config_content, |
| 489 |
sizeof(config_content), |
| 490 |
"HostKey %s\nMACs %s\n", |
| 491 |
hostkey_files[i], tokens->tokens[j]); |
| 492 |
|
| 493 |
rc = try_config_content(state, config_content, false); |
| 494 |
assert_int_equal(rc, 0); |
| 495 |
|
| 496 |
j++; |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
ssh_tokens_free(tokens); |
| 501 |
} |
| 502 |
|
| 503 |
static void torture_server_config_kex(void **state) |
| 504 |
{ |
| 505 |
struct test_server_st *tss = *state; |
| 506 |
size_t i, j, num_hostkey_files = 1; |
| 507 |
char config_content[4096]; |
| 508 |
|
| 509 |
const char *kex; |
| 510 |
|
| 511 |
struct ssh_tokens_st *tokens; |
| 512 |
|
| 513 |
int rc; |
| 514 |
|
| 515 |
assert_non_null(tss); |
| 516 |
|
| 517 |
num_hostkey_files = setup_hostkey_files(tss); |
| 518 |
|
| 519 |
if (ssh_fips_mode()) { |
| 520 |
kex = ssh_kex_get_fips_methods(SSH_KEX); |
| 521 |
assert_non_null(kex); |
| 522 |
} else { |
| 523 |
kex = ssh_kex_get_default_methods(SSH_KEX); |
| 524 |
assert_non_null(kex); |
| 525 |
} |
| 526 |
|
| 527 |
tokens = ssh_tokenize(kex, ','); |
| 528 |
assert_non_null(tokens); |
| 529 |
|
| 530 |
for (i = 0; i < num_hostkey_files; i++) { |
| 531 |
/* Try setting all default algorithms */ |
| 532 |
snprintf(config_content, |
| 533 |
sizeof(config_content), |
| 534 |
"HostKey %s\nKexAlgorithms %s\n", |
| 535 |
hostkey_files[i], kex); |
| 536 |
|
| 537 |
rc = try_config_content(state, config_content, false); |
| 538 |
assert_int_equal(rc, 0); |
| 539 |
|
| 540 |
/* Try each algorithm individually */ |
| 541 |
j = 0; |
| 542 |
while(tokens->tokens[j] != NULL) { |
| 543 |
snprintf(config_content, |
| 544 |
sizeof(config_content), |
| 545 |
"HostKey %s\nKexAlgorithms %s\n", |
| 546 |
hostkey_files[i], tokens->tokens[j]); |
| 547 |
|
| 548 |
rc = try_config_content(state, config_content, false); |
| 549 |
assert_int_equal(rc, 0); |
| 550 |
|
| 551 |
j++; |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
ssh_tokens_free(tokens); |
| 556 |
} |
| 557 |
|
| 558 |
static void torture_server_config_hostkey_algorithms(void **state) |
| 559 |
{ |
| 560 |
struct test_server_st *tss = *state; |
| 561 |
size_t i, num_hostkey_files = 5; |
| 562 |
char config_content[4096]; |
| 563 |
|
| 564 |
const char *allowed; |
| 565 |
|
| 566 |
int rc; |
| 567 |
|
| 568 |
assert_non_null(tss); |
| 569 |
|
| 570 |
num_hostkey_files = setup_hostkey_files(tss); |
| 571 |
|
| 572 |
if (ssh_fips_mode()) { |
| 573 |
allowed = ssh_kex_get_fips_methods(SSH_HOSTKEYS); |
| 574 |
assert_non_null(allowed); |
| 575 |
} else { |
| 576 |
allowed = ssh_kex_get_default_methods(SSH_HOSTKEYS); |
| 577 |
assert_non_null(allowed); |
| 578 |
} |
| 579 |
|
| 580 |
for (i = 0; i < num_hostkey_files; i++) { |
| 581 |
/* Should work with all allowed */ |
| 582 |
snprintf(config_content, |
| 583 |
sizeof(config_content), |
| 584 |
"HostKey %s\nHostKeyAlgorithms %s\n", |
| 585 |
hostkey_files[i], allowed); |
| 586 |
|
| 587 |
rc = try_config_content(state, config_content, false); |
| 588 |
assert_int_equal(rc, 0); |
| 589 |
} |
| 590 |
|
| 591 |
/* Should work with matching hostkey and allowed algorithm */ |
| 592 |
|
| 593 |
if (!ssh_fips_mode()) { |
| 594 |
/* ed25519 */ |
| 595 |
snprintf(config_content, |
| 596 |
sizeof(config_content), |
| 597 |
"HostKey %s\nHostkeyAlgorithms %s\n", |
| 598 |
tss->ed25519_hostkey, "ssh-ed25519"); |
| 599 |
|
| 600 |
rc = try_config_content(state, config_content, false); |
| 601 |
assert_int_equal(rc, 0); |
| 602 |
|
| 603 |
/* ssh-rsa */ |
| 604 |
snprintf(config_content, |
| 605 |
sizeof(config_content), |
| 606 |
"HostKey %s\nHostkeyAlgorithms %s\n", |
| 607 |
tss->rsa_hostkey, "ssh-rsa"); |
| 608 |
|
| 609 |
rc = try_config_content(state, config_content, false); |
| 610 |
assert_int_equal(rc, 0); |
| 611 |
} |
| 612 |
|
| 613 |
/* rsa-sha2-256 */ |
| 614 |
snprintf(config_content, |
| 615 |
sizeof(config_content), |
| 616 |
"HostKey %s\nHostkeyAlgorithms %s\n", |
| 617 |
tss->rsa_hostkey, "rsa-sha2-256"); |
| 618 |
|
| 619 |
rc = try_config_content(state, config_content, false); |
| 620 |
assert_int_equal(rc, 0); |
| 621 |
|
| 622 |
/* ssh-sha2-512 */ |
| 623 |
snprintf(config_content, |
| 624 |
sizeof(config_content), |
| 625 |
"HostKey %s\nHostkeyAlgorithms %s\n", |
| 626 |
tss->rsa_hostkey, "rsa-sha2-512"); |
| 627 |
|
| 628 |
rc = try_config_content(state, config_content, false); |
| 629 |
assert_int_equal(rc, 0); |
| 630 |
|
| 631 |
/* ecdsa-sha2-nistp256 */ |
| 632 |
snprintf(config_content, |
| 633 |
sizeof(config_content), |
| 634 |
"HostKey %s\nHostkeyAlgorithms %s\n", |
| 635 |
tss->ecdsa_256_hostkey, "ecdsa-sha2-nistp256"); |
| 636 |
|
| 637 |
rc = try_config_content(state, config_content, false); |
| 638 |
assert_int_equal(rc, 0); |
| 639 |
|
| 640 |
/* ecdsa-sha2-nistp384 */ |
| 641 |
snprintf(config_content, |
| 642 |
sizeof(config_content), |
| 643 |
"HostKey %s\nHostkeyAlgorithms %s\n", |
| 644 |
tss->ecdsa_384_hostkey, "ecdsa-sha2-nistp384"); |
| 645 |
|
| 646 |
rc = try_config_content(state, config_content, false); |
| 647 |
assert_int_equal(rc, 0); |
| 648 |
|
| 649 |
/* ecdsa-sha2-nistp521 */ |
| 650 |
snprintf(config_content, |
| 651 |
sizeof(config_content), |
| 652 |
"HostKey %s\nHostkeyAlgorithms %s\n", |
| 653 |
tss->ecdsa_521_hostkey, "ecdsa-sha2-nistp521"); |
| 654 |
|
| 655 |
rc = try_config_content(state, config_content, false); |
| 656 |
assert_int_equal(rc, 0); |
| 657 |
|
| 658 |
} |
| 659 |
|
| 660 |
static void torture_server_config_unknown(void **state) |
| 661 |
{ |
| 662 |
struct test_server_st *tss = *state; |
| 663 |
char config_content[4096]; |
| 664 |
|
| 665 |
int rc; |
| 666 |
|
| 667 |
assert_non_null(tss); |
| 668 |
assert_non_null(tss->rsa_hostkey); |
| 669 |
|
| 670 |
snprintf(config_content, |
| 671 |
sizeof(config_content), |
| 672 |
"HostKey %s\nUnknownOption unknown-value1,unknown-value2\n", |
| 673 |
tss->rsa_hostkey); |
| 674 |
|
| 675 |
rc = try_config_content(state, config_content, false); |
| 676 |
assert_int_equal(rc, 0); |
| 677 |
} |
| 678 |
|
| 679 |
/* |
| 680 |
* Check that the server returns the correct signature when the negotiated host |
| 681 |
* key is RSA but the signature algorithm is not the server's preferred |
| 682 |
* algorithm (e.g. when the client prefers ssh-rsa over rsa-sha2-256 or |
| 683 |
* rsa-sha2-512). |
| 684 |
* |
| 685 |
* Related: T191, T240 |
| 686 |
*/ |
| 687 |
static void torture_server_config_rsa_hostkey_order(void **state) |
| 688 |
{ |
| 689 |
struct test_server_st *tss = *state; |
| 690 |
struct torture_state *s = NULL; |
| 691 |
char config_content[4096]; |
| 692 |
size_t num_hostkey_files; |
| 693 |
const char *allowed = NULL; |
| 694 |
|
| 695 |
ssh_session session = NULL; |
| 696 |
|
| 697 |
int rc; |
| 698 |
|
| 699 |
assert_non_null(tss); |
| 700 |
s = tss->state; |
| 701 |
assert_non_null(s); |
| 702 |
|
| 703 |
/* Prepare key files */ |
| 704 |
num_hostkey_files = setup_hostkey_files(tss); |
| 705 |
assert_true(num_hostkey_files > 0); |
| 706 |
|
| 707 |
/* Create the server configuration file */ |
| 708 |
if (ssh_fips_mode()) { |
| 709 |
allowed = "rsa-sha2-512,rsa-sha2-256"; |
| 710 |
} else { |
| 711 |
allowed = "rsa-sha2-256,ssh-rsa"; |
| 712 |
} |
| 713 |
|
| 714 |
snprintf(config_content, |
| 715 |
sizeof(config_content), |
| 716 |
"HostKey %s\nHostkeyAlgorithms %s\n", |
| 717 |
tss->rsa_hostkey, allowed); |
| 718 |
|
| 719 |
assert_non_null(s->srv_config); |
| 720 |
torture_write_file(s->srv_config, config_content); |
| 721 |
|
| 722 |
fprintf(stderr, "Config file %s content: \n\n%s\n", s->srv_config, |
| 723 |
config_content); |
| 724 |
fflush(stderr); |
| 725 |
|
| 726 |
/* Start server */ |
| 727 |
rc = start_server(state); |
| 728 |
assert_int_equal(rc, 0); |
| 729 |
|
| 730 |
/* Setup session */ |
| 731 |
rc = session_setup(state); |
| 732 |
assert_int_equal(rc, 0); |
| 733 |
|
| 734 |
session = s->ssh.session; |
| 735 |
assert_non_null(session); |
| 736 |
|
| 737 |
rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_ALICE); |
| 738 |
assert_int_equal(rc, SSH_OK); |
| 739 |
|
| 740 |
/* Set client order of preference different from the server */ |
| 741 |
if (ssh_fips_mode()) { |
| 742 |
/* Set the host keys with rsa-sha2-256 before rsa-sha2-512 */ |
| 743 |
rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, |
| 744 |
"rsa-sha2-256,rsa-sha2-512"); |
| 745 |
assert_int_equal(rc, SSH_OK); |
| 746 |
} else { |
| 747 |
/* Set the host keys with ssh-rsa before rsa-sha2-256 */ |
| 748 |
rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, |
| 749 |
"ssh-rsa,rsa-sha2-256"); |
| 750 |
assert_int_equal(rc, SSH_OK); |
| 751 |
} |
| 752 |
|
| 753 |
rc = ssh_connect(session); |
| 754 |
assert_int_equal(rc, SSH_OK); |
| 755 |
|
| 756 |
rc = ssh_userauth_none(session, NULL); |
| 757 |
/* This request should return a SSH_REQUEST_DENIED error */ |
| 758 |
if (rc == SSH_ERROR) { |
| 759 |
assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED); |
| 760 |
} |
| 761 |
rc = ssh_userauth_list(session, NULL); |
| 762 |
assert_true(rc & SSH_AUTH_METHOD_PUBLICKEY); |
| 763 |
|
| 764 |
rc = ssh_userauth_publickey_auto(session, NULL, NULL); |
| 765 |
assert_ssh_return_code(session, rc); |
| 766 |
|
| 767 |
rc = session_teardown(state); |
| 768 |
assert_int_equal(rc, 0); |
| 769 |
|
| 770 |
rc = stop_server(state); |
| 771 |
assert_int_equal(rc, 0); |
| 772 |
|
| 773 |
SAFE_FREE(s->srv_additional_config); |
| 774 |
} |
| 775 |
|
| 776 |
int torture_run_tests(void) { |
| 777 |
int rc; |
| 778 |
struct CMUnitTest tests[] = { |
| 779 |
cmocka_unit_test_setup_teardown(torture_server_config_hostkey, |
| 780 |
setup_temp_dir, teardown_temp_dir), |
| 781 |
cmocka_unit_test_setup_teardown(torture_server_config_ciphers, |
| 782 |
setup_temp_dir, teardown_temp_dir), |
| 783 |
cmocka_unit_test_setup_teardown(torture_server_config_macs, |
| 784 |
setup_temp_dir, teardown_temp_dir), |
| 785 |
cmocka_unit_test_setup_teardown(torture_server_config_kex, |
| 786 |
setup_temp_dir, teardown_temp_dir), |
| 787 |
cmocka_unit_test_setup_teardown(torture_server_config_hostkey_algorithms, |
| 788 |
setup_temp_dir, teardown_temp_dir), |
| 789 |
cmocka_unit_test_setup_teardown(torture_server_config_unknown, |
| 790 |
setup_temp_dir, teardown_temp_dir), |
| 791 |
cmocka_unit_test_setup_teardown(torture_server_config_rsa_hostkey_order, |
| 792 |
setup_temp_dir, teardown_temp_dir), |
| 793 |
}; |
| 794 |
|
| 795 |
ssh_init(); |
| 796 |
|
| 797 |
torture_filter_tests(tests); |
| 798 |
rc = cmocka_run_group_tests(tests, |
| 799 |
setup_files, |
| 800 |
teardown_files); |
| 801 |
|
| 802 |
ssh_finalize(); |
| 803 |
|
| 804 |
return rc; |
| 805 |
} |