|
| 1 | +/* Match auth.c's feature-test macros so crypt() is declared and so the |
| 2 | + * pre-existing CleanupWildcardTest code keeps seeing DT_DIR. Must come |
| 3 | + * before any system header is pulled in. */ |
| 4 | +#ifdef __linux__ |
| 5 | + #ifndef _XOPEN_SOURCE |
| 6 | + #define _XOPEN_SOURCE |
| 7 | + #endif |
| 8 | + #ifndef _GNU_SOURCE |
| 9 | + #define _GNU_SOURCE |
| 10 | + #endif |
| 11 | +#endif |
| 12 | + |
1 | 13 | #include <stdarg.h> |
| 14 | +#if defined(WOLFSSH_HAVE_LIBCRYPT) || defined(WOLFSSH_HAVE_LIBLOGIN) |
| 15 | + #include <unistd.h> |
| 16 | +#endif |
| 17 | +#ifdef HAVE_CRYPT_H |
| 18 | + #include <crypt.h> |
| 19 | +#endif |
2 | 20 |
|
3 | 21 | #include <wolfssh/ssh.h> |
4 | 22 | #include <configuration.h> |
| 23 | +#include <auth.h> |
5 | 24 |
|
6 | 25 | #ifndef WOLFSSH_DEFAULT_LOG_WIDTH |
7 | 26 | #define WOLFSSH_DEFAULT_LOG_WIDTH 120 |
@@ -396,10 +415,64 @@ static int test_ConfigFree(void) |
396 | 415 | return ret; |
397 | 416 | } |
398 | 417 |
|
| 418 | +#if defined(WOLFSSH_HAVE_LIBCRYPT) || defined(WOLFSSH_HAVE_LIBLOGIN) |
| 419 | +/* Negative-path coverage for CheckPasswordHashUnix so mutation of the |
| 420 | + * ConstantCompare clause (the only substantive check once crypt() has |
| 421 | + * produced its fixed-length output) does not survive the test suite. */ |
| 422 | +static int test_CheckPasswordHashUnix(void) |
| 423 | +{ |
| 424 | + int ret = WS_SUCCESS; |
| 425 | + const char* correct = "wolfssh-test-pass"; |
| 426 | + const char* wrong = "wolfssh-test-wrong"; |
| 427 | + /* SHA-512 crypt salt; portable across glibc-based crypt() impls. */ |
| 428 | + const char* salt = "$6$wolfsshtestsalt$"; |
| 429 | + char stored[128]; |
| 430 | + char* hash; |
| 431 | + int rc; |
| 432 | + |
| 433 | + hash = crypt(correct, salt); |
| 434 | + if (hash == NULL || hash[0] == '*' || WSTRLEN(hash) == 0) { |
| 435 | + Log(" crypt() unavailable or refused salt, skipping.\n"); |
| 436 | + return WS_SUCCESS; |
| 437 | + } |
| 438 | + if (WSTRLEN(hash) >= sizeof(stored)) { |
| 439 | + return WS_FATAL_ERROR; |
| 440 | + } |
| 441 | + WMEMCPY(stored, hash, WSTRLEN(hash) + 1); |
| 442 | + |
| 443 | + Log(" Testing scenario: correct password authenticates."); |
| 444 | + rc = CheckPasswordHashUnix(correct, stored); |
| 445 | + if (rc == WSSHD_AUTH_SUCCESS) { |
| 446 | + Log(" PASSED.\n"); |
| 447 | + } |
| 448 | + else { |
| 449 | + Log(" FAILED.\n"); |
| 450 | + ret = WS_FATAL_ERROR; |
| 451 | + } |
| 452 | + |
| 453 | + if (ret == WS_SUCCESS) { |
| 454 | + Log(" Testing scenario: wrong password is rejected."); |
| 455 | + rc = CheckPasswordHashUnix(wrong, stored); |
| 456 | + if (rc == WSSHD_AUTH_FAILURE) { |
| 457 | + Log(" PASSED.\n"); |
| 458 | + } |
| 459 | + else { |
| 460 | + Log(" FAILED.\n"); |
| 461 | + ret = WS_FATAL_ERROR; |
| 462 | + } |
| 463 | + } |
| 464 | + |
| 465 | + return ret; |
| 466 | +} |
| 467 | +#endif /* WOLFSSH_HAVE_LIBCRYPT || WOLFSSH_HAVE_LIBLOGIN */ |
| 468 | + |
399 | 469 | const TEST_CASE testCases[] = { |
400 | 470 | TEST_DECL(test_ParseConfigLine), |
401 | 471 | TEST_DECL(test_ConfigCopy), |
402 | 472 | TEST_DECL(test_ConfigFree), |
| 473 | +#if defined(WOLFSSH_HAVE_LIBCRYPT) || defined(WOLFSSH_HAVE_LIBLOGIN) |
| 474 | + TEST_DECL(test_CheckPasswordHashUnix), |
| 475 | +#endif |
403 | 476 | }; |
404 | 477 |
|
405 | 478 | int main(int argc, char** argv) |
|
0 commit comments