|
| 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 | +#include <unistd.h> |
| 15 | +#ifdef HAVE_CRYPT_H |
| 16 | + #include <crypt.h> |
| 17 | +#endif |
2 | 18 |
|
3 | 19 | #include <wolfssh/ssh.h> |
4 | 20 | #include <configuration.h> |
| 21 | +#include <auth.h> |
5 | 22 |
|
6 | 23 | #ifndef WOLFSSH_DEFAULT_LOG_WIDTH |
7 | 24 | #define WOLFSSH_DEFAULT_LOG_WIDTH 120 |
@@ -233,8 +250,62 @@ static int test_ParseConfigLine(void) |
233 | 250 | return ret; |
234 | 251 | } |
235 | 252 |
|
| 253 | +#if defined(WOLFSSH_HAVE_LIBCRYPT) || defined(WOLFSSH_HAVE_LIBLOGIN) |
| 254 | +/* Negative-path coverage for CheckPasswordHashUnix so mutation of the |
| 255 | + * ConstantCompare clause (the only substantive check once crypt() has |
| 256 | + * produced its fixed-length output) does not survive the test suite. */ |
| 257 | +static int test_CheckPasswordHashUnix(void) |
| 258 | +{ |
| 259 | + int ret = WS_SUCCESS; |
| 260 | + const char* correct = "wolfssh-test-pass"; |
| 261 | + const char* wrong = "wolfssh-test-wrong"; |
| 262 | + /* SHA-512 crypt salt; portable across glibc-based crypt() impls. */ |
| 263 | + const char* salt = "$6$wolfsshtestsalt$"; |
| 264 | + char stored[128]; |
| 265 | + char* hash; |
| 266 | + int rc; |
| 267 | + |
| 268 | + hash = crypt(correct, salt); |
| 269 | + if (hash == NULL || hash[0] == '*' || WSTRLEN(hash) == 0) { |
| 270 | + Log(" crypt() unavailable or refused salt, skipping.\n"); |
| 271 | + return WS_SUCCESS; |
| 272 | + } |
| 273 | + if (WSTRLEN(hash) >= sizeof(stored)) { |
| 274 | + return WS_FATAL_ERROR; |
| 275 | + } |
| 276 | + WMEMCPY(stored, hash, WSTRLEN(hash) + 1); |
| 277 | + |
| 278 | + Log(" Testing scenario: correct password authenticates."); |
| 279 | + rc = CheckPasswordHashUnix(correct, stored); |
| 280 | + if (rc == WSSHD_AUTH_SUCCESS) { |
| 281 | + Log(" PASSED.\n"); |
| 282 | + } |
| 283 | + else { |
| 284 | + Log(" FAILED.\n"); |
| 285 | + ret = WS_FATAL_ERROR; |
| 286 | + } |
| 287 | + |
| 288 | + if (ret == WS_SUCCESS) { |
| 289 | + Log(" Testing scenario: wrong password is rejected."); |
| 290 | + rc = CheckPasswordHashUnix(wrong, stored); |
| 291 | + if (rc == WSSHD_AUTH_FAILURE) { |
| 292 | + Log(" PASSED.\n"); |
| 293 | + } |
| 294 | + else { |
| 295 | + Log(" FAILED.\n"); |
| 296 | + ret = WS_FATAL_ERROR; |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + return ret; |
| 301 | +} |
| 302 | +#endif /* WOLFSSH_HAVE_LIBCRYPT || WOLFSSH_HAVE_LIBLOGIN */ |
| 303 | + |
236 | 304 | const TEST_CASE testCases[] = { |
237 | | - TEST_DECL(test_ParseConfigLine) |
| 305 | + TEST_DECL(test_ParseConfigLine), |
| 306 | +#if defined(WOLFSSH_HAVE_LIBCRYPT) || defined(WOLFSSH_HAVE_LIBLOGIN) |
| 307 | + TEST_DECL(test_CheckPasswordHashUnix), |
| 308 | +#endif |
238 | 309 | }; |
239 | 310 |
|
240 | 311 | int main(int argc, char** argv) |
|
0 commit comments