| 1 |
/* |
| 2 |
* This file is part of the SSH Library |
| 3 |
* |
| 4 |
* Copyright (c) 2021 by Anderson Toshiyuki Sasaki - Red Hat, Inc. |
| 5 |
* |
| 6 |
* The SSH Library is free software: you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation, either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* The SSH Library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with the SSH Library; see the file COPYING. If not, |
| 18 |
* see <https://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include "torture.h" |
| 24 |
#include "libssh/libssh.h" |
| 25 |
#include "libssh/priv.h" |
| 26 |
#include "libssh/session.h" |
| 27 |
|
| 28 |
#include <errno.h> |
| 29 |
#include <sys/types.h> |
| 30 |
#include <pwd.h> |
| 31 |
|
| 32 |
#include "chacha20_override.h" |
| 33 |
#include "curve25519_override.h" |
| 34 |
#include "ed25519_override.h" |
| 35 |
#include "mlkem768_override.h" |
| 36 |
#include "poly1305_override.h" |
| 37 |
#include "sntrup761_override.h" |
| 38 |
|
| 39 |
const char template[] = "temp_dir_XXXXXX"; |
| 40 |
|
| 41 |
struct test_st { |
| 42 |
char *temp_dir; |
| 43 |
char *orig_dir; |
| 44 |
}; |
| 45 |
|
| 46 |
static int sshd_setup(void **state) |
| 47 |
{ |
| 48 |
struct torture_state *s; |
| 49 |
struct test_st *test_state = NULL; |
| 50 |
char *temp_dir; |
| 51 |
int rc; |
| 52 |
|
| 53 |
torture_setup_sshd_server(state, false); |
| 54 |
|
| 55 |
test_state = malloc(sizeof(struct test_st)); |
| 56 |
assert_non_null(test_state); |
| 57 |
|
| 58 |
s = *((struct torture_state **)state); |
| 59 |
s->private_data = test_state; |
| 60 |
|
| 61 |
test_state->orig_dir = torture_get_current_working_dir(); |
| 62 |
assert_non_null(test_state->orig_dir); |
| 63 |
|
| 64 |
temp_dir = torture_make_temp_dir(template); |
| 65 |
assert_non_null(temp_dir); |
| 66 |
|
| 67 |
rc = torture_change_dir(temp_dir); |
| 68 |
assert_int_equal(rc, 0); |
| 69 |
|
| 70 |
test_state->temp_dir = temp_dir; |
| 71 |
|
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
static int sshd_teardown(void **state) |
| 76 |
{ |
| 77 |
struct torture_state *s = *state; |
| 78 |
struct test_st *test_state = s->private_data; |
| 79 |
int rc; |
| 80 |
|
| 81 |
rc = torture_change_dir(test_state->orig_dir); |
| 82 |
assert_int_equal(rc, 0); |
| 83 |
|
| 84 |
rc = torture_rmdirs(test_state->temp_dir); |
| 85 |
assert_int_equal(rc, 0); |
| 86 |
|
| 87 |
SAFE_FREE(test_state->temp_dir); |
| 88 |
SAFE_FREE(test_state->orig_dir); |
| 89 |
SAFE_FREE(test_state); |
| 90 |
|
| 91 |
torture_teardown_sshd_server(state); |
| 92 |
|
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
static int session_setup(void **state) |
| 97 |
{ |
| 98 |
struct torture_state *s = *state; |
| 99 |
int verbosity = torture_libssh_verbosity(); |
| 100 |
struct passwd *pwd; |
| 101 |
bool false_v = false; |
| 102 |
int rc; |
| 103 |
|
| 104 |
pwd = getpwnam("bob"); |
| 105 |
assert_non_null(pwd); |
| 106 |
|
| 107 |
rc = setuid(pwd->pw_uid); |
| 108 |
assert_return_code(rc, errno); |
| 109 |
|
| 110 |
s->ssh.session = ssh_new(); |
| 111 |
assert_non_null(s->ssh.session); |
| 112 |
|
| 113 |
ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); |
| 114 |
ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER); |
| 115 |
/* Prevent parsing configuration files that can introduce different |
| 116 |
* algorithms then we want to test */ |
| 117 |
ssh_options_set(s->ssh.session, SSH_OPTIONS_PROCESS_CONFIG, &false_v); |
| 118 |
|
| 119 |
reset_chacha20_function_called(); |
| 120 |
reset_poly1305_function_called(); |
| 121 |
reset_curve25519_function_called(); |
| 122 |
reset_ed25519_function_called(); |
| 123 |
reset_sntrup761_function_called(); |
| 124 |
reset_mlkem768_function_called(); |
| 125 |
|
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
static int session_teardown(void **state) |
| 130 |
{ |
| 131 |
struct torture_state *s = *state; |
| 132 |
|
| 133 |
ssh_disconnect(s->ssh.session); |
| 134 |
ssh_free(s->ssh.session); |
| 135 |
|
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
static void test_algorithm(ssh_session session, |
| 140 |
const char *kex, |
| 141 |
const char *cipher, |
| 142 |
const char *hostkey) |
| 143 |
{ |
| 144 |
char data[256] = {0}; |
| 145 |
int rc; |
| 146 |
|
| 147 |
if (kex != NULL) { |
| 148 |
rc = ssh_options_set(session, SSH_OPTIONS_KEY_EXCHANGE, kex); |
| 149 |
assert_ssh_return_code(session, rc); |
| 150 |
} |
| 151 |
|
| 152 |
if (cipher != NULL) { |
| 153 |
rc = ssh_options_set(session, SSH_OPTIONS_CIPHERS_C_S, cipher); |
| 154 |
assert_ssh_return_code(session, rc); |
| 155 |
rc = ssh_options_set(session, SSH_OPTIONS_CIPHERS_S_C, cipher); |
| 156 |
assert_ssh_return_code(session, rc); |
| 157 |
} |
| 158 |
|
| 159 |
if (hostkey != NULL) { |
| 160 |
rc = ssh_options_set(session, SSH_OPTIONS_HOSTKEYS, hostkey); |
| 161 |
assert_ssh_return_code(session, rc); |
| 162 |
} |
| 163 |
|
| 164 |
rc = ssh_connect(session); |
| 165 |
assert_ssh_return_code(session, rc); |
| 166 |
|
| 167 |
/* send ignore packets of all sizes */ |
| 168 |
memset(data, 'A', sizeof(data) - 1); |
| 169 |
ssh_send_ignore(session, data); |
| 170 |
ssh_handle_packets(session, 50); |
| 171 |
|
| 172 |
rc = ssh_userauth_none(session, NULL); |
| 173 |
if (rc != SSH_OK) { |
| 174 |
rc = ssh_get_error_code(session); |
| 175 |
assert_int_equal(rc, SSH_REQUEST_DENIED); |
| 176 |
} |
| 177 |
|
| 178 |
ssh_disconnect(session); |
| 179 |
} |
| 180 |
|
| 181 |
#ifdef OPENSSH_CHACHA20_POLY1305_OPENSSH_COM |
| 182 |
static void torture_override_chacha20_poly1305(void **state) |
| 183 |
{ |
| 184 |
struct torture_state *s = *state; |
| 185 |
|
| 186 |
bool internal_chacha20_called; |
| 187 |
bool internal_poly1305_called; |
| 188 |
|
| 189 |
if (ssh_fips_mode()) { |
| 190 |
skip(); |
| 191 |
} |
| 192 |
|
| 193 |
test_algorithm(s->ssh.session, |
| 194 |
NULL, /* kex */ |
| 195 |
"[email protected]", |
| 196 |
NULL /* hostkey */); |
| 197 |
|
| 198 |
internal_chacha20_called = internal_chacha20_function_called(); |
| 199 |
internal_poly1305_called = internal_poly1305_function_called(); |
| 200 |
|
| 201 |
#if SHOULD_CALL_INTERNAL_CHACHAPOLY |
| 202 |
assert_true(internal_chacha20_called); |
| 203 |
assert_true(internal_poly1305_called); |
| 204 |
#else |
| 205 |
assert_false(internal_chacha20_called || |
| 206 |
internal_poly1305_called); |
| 207 |
#endif |
| 208 |
|
| 209 |
} |
| 210 |
#endif /* OPENSSH_CHACHA20_POLY1305_OPENSSH_COM */ |
| 211 |
|
| 212 |
#ifdef OPENSSH_CURVE25519_SHA256 |
| 213 |
static void torture_override_ecdh_curve25519_sha256(void **state) |
| 214 |
{ |
| 215 |
struct torture_state *s = *state; |
| 216 |
bool internal_curve25519_called; |
| 217 |
|
| 218 |
if (ssh_fips_mode()) { |
| 219 |
skip(); |
| 220 |
} |
| 221 |
|
| 222 |
test_algorithm(s->ssh.session, |
| 223 |
"curve25519-sha256", |
| 224 |
NULL, /* cipher */ |
| 225 |
NULL /* hostkey */); |
| 226 |
|
| 227 |
internal_curve25519_called = internal_curve25519_function_called(); |
| 228 |
|
| 229 |
#if SHOULD_CALL_INTERNAL_CURVE25519 |
| 230 |
assert_true(internal_curve25519_called); |
| 231 |
#else |
| 232 |
assert_false(internal_curve25519_called); |
| 233 |
#endif |
| 234 |
} |
| 235 |
#endif /* OPENSSH_CURVE25519_SHA256 */ |
| 236 |
|
| 237 |
#ifdef OPENSSH_CURVE25519_SHA256_LIBSSH_ORG |
| 238 |
static void torture_override_ecdh_curve25519_sha256_libssh_org(void **state) |
| 239 |
{ |
| 240 |
struct torture_state *s = *state; |
| 241 |
bool internal_curve25519_called; |
| 242 |
|
| 243 |
if (ssh_fips_mode()) { |
| 244 |
skip(); |
| 245 |
} |
| 246 |
|
| 247 |
test_algorithm(s->ssh.session, |
| 248 |
"[email protected]", |
| 249 |
NULL, /* cipher */ |
| 250 |
NULL /* hostkey */); |
| 251 |
|
| 252 |
internal_curve25519_called = internal_curve25519_function_called(); |
| 253 |
|
| 254 |
#if SHOULD_CALL_INTERNAL_CURVE25519 |
| 255 |
assert_true(internal_curve25519_called); |
| 256 |
#else |
| 257 |
assert_false(internal_curve25519_called); |
| 258 |
#endif |
| 259 |
} |
| 260 |
#endif /* OPENSSH_CURVE25519_SHA256_LIBSSH_ORG */ |
| 261 |
|
| 262 |
#ifdef OPENSSH_SNTRUP761X25519_SHA512_OPENSSH_COM |
| 263 |
static void |
| 264 |
torture_override_ecdh_sntrup761x25519_sha512_openssh_com(void **state) |
| 265 |
{ |
| 266 |
struct torture_state *s = *state; |
| 267 |
bool internal_curve25519_called; |
| 268 |
bool internal_sntrup761_called; |
| 269 |
|
| 270 |
if (ssh_fips_mode()) { |
| 271 |
skip(); |
| 272 |
} |
| 273 |
|
| 274 |
test_algorithm(s->ssh.session, |
| 275 |
"[email protected]", |
| 276 |
NULL, /* cipher */ |
| 277 |
NULL /* hostkey */); |
| 278 |
|
| 279 |
internal_curve25519_called = internal_curve25519_function_called(); |
| 280 |
internal_sntrup761_called = internal_sntrup761_function_called(); |
| 281 |
|
| 282 |
#if SHOULD_CALL_INTERNAL_SNTRUP761 |
| 283 |
assert_true(internal_sntrup761_called); |
| 284 |
#else |
| 285 |
assert_false(internal_sntrup761_called); |
| 286 |
#endif |
| 287 |
|
| 288 |
#if SHOULD_CALL_INTERNAL_CURVE25519 |
| 289 |
assert_true(internal_curve25519_called); |
| 290 |
#else |
| 291 |
assert_false(internal_curve25519_called); |
| 292 |
#endif |
| 293 |
} |
| 294 |
#endif /* OPENSSH_SNTRUP761X25519_SHA512_OPENSSH_COM */ |
| 295 |
|
| 296 |
#ifdef OPENSSH_SNTRUP761X25519_SHA512 |
| 297 |
static void |
| 298 |
torture_override_ecdh_sntrup761x25519_sha512(void **state) |
| 299 |
{ |
| 300 |
struct torture_state *s = *state; |
| 301 |
bool internal_curve25519_called; |
| 302 |
bool internal_sntrup761_called; |
| 303 |
|
| 304 |
if (ssh_fips_mode()) { |
| 305 |
skip(); |
| 306 |
} |
| 307 |
|
| 308 |
test_algorithm(s->ssh.session, |
| 309 |
"sntrup761x25519-sha512", |
| 310 |
NULL, /* cipher */ |
| 311 |
NULL /* hostkey */); |
| 312 |
|
| 313 |
internal_curve25519_called = internal_curve25519_function_called(); |
| 314 |
internal_sntrup761_called = internal_sntrup761_function_called(); |
| 315 |
|
| 316 |
#if SHOULD_CALL_INTERNAL_SNTRUP761 |
| 317 |
assert_true(internal_sntrup761_called); |
| 318 |
#else |
| 319 |
assert_false(internal_sntrup761_called); |
| 320 |
#endif |
| 321 |
|
| 322 |
#if SHOULD_CALL_INTERNAL_CURVE25519 |
| 323 |
assert_true(internal_curve25519_called); |
| 324 |
#else |
| 325 |
assert_false(internal_curve25519_called); |
| 326 |
#endif |
| 327 |
} |
| 328 |
#endif /* OPENSSH_SNTRUP761X25519_SHA512 */ |
| 329 |
|
| 330 |
#ifdef OPENSSH_MLKEM768X25519_SHA256 |
| 331 |
static void torture_override_mlkem768x25519_sha256(void **state) |
| 332 |
{ |
| 333 |
struct torture_state *s = *state; |
| 334 |
bool internal_curve25519_called; |
| 335 |
bool internal_mlkem768_called; |
| 336 |
|
| 337 |
if (ssh_fips_mode()) { |
| 338 |
skip(); |
| 339 |
} |
| 340 |
|
| 341 |
test_algorithm(s->ssh.session, |
| 342 |
"mlkem768x25519-sha256", |
| 343 |
NULL, /* cipher */ |
| 344 |
NULL /* hostkey */); |
| 345 |
|
| 346 |
internal_curve25519_called = internal_curve25519_function_called(); |
| 347 |
internal_mlkem768_called = internal_mlkem768_function_called(); |
| 348 |
|
| 349 |
#if SHOULD_CALL_INTERNAL_MLKEM |
| 350 |
assert_true(internal_mlkem768_called); |
| 351 |
#else |
| 352 |
assert_false(internal_mlkem768_called); |
| 353 |
#endif |
| 354 |
|
| 355 |
#if SHOULD_CALL_INTERNAL_CURVE25519 |
| 356 |
assert_true(internal_curve25519_called); |
| 357 |
#else |
| 358 |
assert_false(internal_curve25519_called); |
| 359 |
#endif |
| 360 |
} |
| 361 |
#endif /* OPENSSH_MLKEM768X25519_SHA256 */ |
| 362 |
|
| 363 |
#ifdef OPENSSH_MLKEM768NISTP256_SHA256 |
| 364 |
static void torture_override_mlkem768nistp256_sha256(void **state) |
| 365 |
{ |
| 366 |
struct torture_state *s = *state; |
| 367 |
bool internal_mlkem768_called; |
| 368 |
|
| 369 |
if (ssh_fips_mode()) { |
| 370 |
skip(); |
| 371 |
} |
| 372 |
|
| 373 |
test_algorithm(s->ssh.session, |
| 374 |
"mlkem768nistp256-sha256", |
| 375 |
NULL, /* cipher */ |
| 376 |
NULL /* hostkey */); |
| 377 |
|
| 378 |
internal_mlkem768_called = internal_mlkem768_function_called(); |
| 379 |
|
| 380 |
#if SHOULD_CALL_INTERNAL_MLKEM |
| 381 |
assert_true(internal_mlkem768_called); |
| 382 |
#else |
| 383 |
assert_false(internal_mlkem768_called); |
| 384 |
#endif |
| 385 |
} |
| 386 |
#endif /* OPENSSH_MLKEM768NISTP256_SHA256 */ |
| 387 |
|
| 388 |
#ifdef OPENSSH_SSH_ED25519 |
| 389 |
static void torture_override_ed25519(void **state) |
| 390 |
{ |
| 391 |
struct torture_state *s = *state; |
| 392 |
bool internal_ed25519_called; |
| 393 |
|
| 394 |
if (ssh_fips_mode()) { |
| 395 |
skip(); |
| 396 |
} |
| 397 |
|
| 398 |
test_algorithm(s->ssh.session, |
| 399 |
NULL, /* kex */ |
| 400 |
NULL, /* cipher */ |
| 401 |
"ssh-ed25519"); |
| 402 |
|
| 403 |
internal_ed25519_called = internal_ed25519_function_called(); |
| 404 |
|
| 405 |
#if SHOULD_CALL_INTERNAL_ED25519 |
| 406 |
assert_true(internal_ed25519_called); |
| 407 |
#else |
| 408 |
assert_false(internal_ed25519_called); |
| 409 |
#endif |
| 410 |
} |
| 411 |
#endif /* OPENSSH_SSH_ED25519 */ |
| 412 |
|
| 413 |
int torture_run_tests(void) |
| 414 |
{ |
| 415 |
int rc; |
| 416 |
struct CMUnitTest tests[] = { |
| 417 |
#ifdef OPENSSH_CHACHA20_POLY1305_OPENSSH_COM |
| 418 |
cmocka_unit_test_setup_teardown(torture_override_chacha20_poly1305, |
| 419 |
session_setup, |
| 420 |
session_teardown), |
| 421 |
#endif /* OPENSSH_CHACHA20_POLY1305_OPENSSH_COM */ |
| 422 |
#ifdef OPENSSH_CURVE25519_SHA256 |
| 423 |
cmocka_unit_test_setup_teardown(torture_override_ecdh_curve25519_sha256, |
| 424 |
session_setup, |
| 425 |
session_teardown), |
| 426 |
#endif /* OPENSSH_CURVE25519_SHA256 */ |
| 427 |
#ifdef OPENSSH_CURVE25519_SHA256_LIBSSH_ORG |
| 428 |
cmocka_unit_test_setup_teardown(torture_override_ecdh_curve25519_sha256_libssh_org, |
| 429 |
session_setup, |
| 430 |
session_teardown), |
| 431 |
#endif /* OPENSSH_CURVE25519_SHA256_LIBSSH_ORG */ |
| 432 |
#ifdef OPENSSH_SNTRUP761X25519_SHA512_OPENSSH_COM |
| 433 |
cmocka_unit_test_setup_teardown(torture_override_ecdh_sntrup761x25519_sha512_openssh_com, |
| 434 |
session_setup, |
| 435 |
session_teardown), |
| 436 |
#endif /* OPENSSH_SNTRUP761X25519_SHA512_OPENSSH_COM */ |
| 437 |
#ifdef OPENSSH_SNTRUP761X25519_SHA512 |
| 438 |
cmocka_unit_test_setup_teardown(torture_override_ecdh_sntrup761x25519_sha512, |
| 439 |
session_setup, |
| 440 |
session_teardown), |
| 441 |
#endif /* OPENSSH_SNTRUP761X25519_SHA512 */ |
| 442 |
#ifdef OPENSSH_MLKEM768X25519_SHA256 |
| 443 |
cmocka_unit_test_setup_teardown(torture_override_mlkem768x25519_sha256, |
| 444 |
session_setup, |
| 445 |
session_teardown), |
| 446 |
#endif /* OPENSSH_MLKEM768X25519_SHA256 */ |
| 447 |
#ifdef OPENSSH_MLKEM768NISTP256_SHA256 |
| 448 |
cmocka_unit_test_setup_teardown(torture_override_mlkem768nistp256_sha256, |
| 449 |
session_setup, |
| 450 |
session_teardown), |
| 451 |
#endif /* OPENSSH_MLKEM768NISTP256_SHA256 */ |
| 452 |
#ifdef OPENSSH_SSH_ED25519 |
| 453 |
cmocka_unit_test_setup_teardown(torture_override_ed25519, |
| 454 |
session_setup, |
| 455 |
session_teardown), |
| 456 |
#endif /* OPENSSH_SSH_ED25519 */ |
| 457 |
}; |
| 458 |
|
| 459 |
ssh_init(); |
| 460 |
|
| 461 |
torture_filter_tests(tests); |
| 462 |
rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown); |
| 463 |
if (rc != 0) { |
| 464 |
return rc; |
| 465 |
} |
| 466 |
|
| 467 |
ssh_finalize(); |
| 468 |
|
| 469 |
return rc; |
| 470 |
} |