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