diff --git a/apps/wolfssh/common.c b/apps/wolfssh/common.c index 3620b31d9..43706f77b 100644 --- a/apps/wolfssh/common.c +++ b/apps/wolfssh/common.c @@ -33,8 +33,12 @@ #include #include #include "apps/wolfssh/common.h" +#include #ifndef USE_WINDOWS_API #include + #include +#else + #include #endif #ifdef WOLFSSH_CERTS @@ -68,11 +72,13 @@ static inline void ato32(const byte* c, word32* u32) } -/* Reads the text file filename into a nul terminated buffer, so callers can - * treat it as a C string. The caller frees out. Returns 0 on success. */ +#define LOAD_TEXT_NONE 1 + +/* Reads the text file filename into a nul terminated buffer the caller + * frees. Returns 0, LOAD_TEXT_NONE if missing or empty, else -1. */ static int load_text_file(const char* filename, char** out, word32* outSz) { - WFILE* file; + WFILE* file = WBADFILE; char* in; long inSz; int ret; @@ -80,9 +86,10 @@ static int load_text_file(const char* filename, char** out, word32* outSz) if (filename == NULL || out == NULL || outSz == NULL) return -1; + errno = 0; ret = WFOPEN(NULL, &file, filename, "rb"); if (ret != 0 || file == WBADFILE) - return -1; + return (errno == ENOENT) ? LOAD_TEXT_NONE : -1; if (!WFSEEK_SUCCESS(WFSEEK(NULL, file, 0, WSEEK_END))) { WFCLOSE(NULL, file); @@ -93,7 +100,7 @@ static int load_text_file(const char* filename, char** out, word32* outSz) if (inSz <= 0) { WFCLOSE(NULL, file); - return -1; + return (inSz == 0) ? LOAD_TEXT_NONE : -1; } in = (char*)WMALLOC(inSz + 1, NULL, 0); @@ -236,6 +243,50 @@ static int AppendNeedsNewline(const char* filename) } +/* Creates only the last directory of filename, owner-only, like OpenSSH. + * Returns WS_SUCCESS only if it made the directory. */ +static int MakeParentDir(const char* filename) +{ + char* dir = NULL; + word32 len = 0, i; + int ret = WS_BAD_FILE_E; + + for (i = 0; filename[i] != '\0'; i++) { + if (filename[i] == '/') { + len = i; + } + } + + if (len > 0) { + dir = (char*)WMALLOC(len + 1, NULL, 0); + if (dir == NULL) { + ret = WS_MEMORY_E; + } + } + + if (dir != NULL) { + WMEMCPY(dir, filename, len); + dir[len] = '\0'; +#ifdef USE_WINDOWS_API + if (_mkdir(dir) == 0) +#else + if (mkdir(dir, 0700) == 0) +#endif + { + printf("Created directory '%s'.\n", dir); + ret = WS_SUCCESS; + } + else if (errno != EEXIST) { + fprintf(stderr, "Could not create directory '%s' (%s).\n", + dir, strerror(errno)); + } + WFREE(dir, NULL, 0); + } + + return ret; +} + + static int AppendKeyToFile(const char* filename, const char* name, const char* type, const char* key) { @@ -261,6 +312,11 @@ static int AppendKeyToFile(const char* filename, const char* name, * and known_hosts is conventionally LF-terminated regardless of * platform. */ ret = WFOPEN(NULL, &f, filename, "ab"); + if ((ret != 0 || f == WBADFILE) + && MakeParentDir(filename) == WS_SUCCESS) { + f = WBADFILE; + ret = WFOPEN(NULL, &f, filename, "ab"); + } if (ret == 0 && f != WBADFILE) { /* Check the write and the close so a failed or truncated entry * (for example on a full disk) is reported rather than appearing @@ -347,7 +403,6 @@ static int GetConfirmation(void) } -#define WOLFSSH_CLIENT_ENCKEY_SIZE_ESTIMATE 1200 #define WOLFSSH_CLIENT_PUBKEYTYPE_SIZE_ESTIMATE 54 #define WOLFSSH_CLIENT_FINGERPRINT_SIZE_ESTIMATE 56 @@ -366,40 +421,56 @@ int ClientPublicKeyCheck(const byte* pubKey, word32 pubKeySz, void* ctx) char *fp = NULL; char *knownType = NULL; int ret = 0, found = 0, badMatch = 0, otherMatch = 0; - word32 sz, lineCount = 0, knownLine = 0; + word32 sz, encSz = 0, lineCount = 0, knownLine = 0; { const char *defaultName = "/.ssh/known_hosts"; char *env; + /* An empty HOME would name /.ssh/known_hosts. */ env = getenv("HOME"); - if (env != NULL) { + if (env != NULL && env[0] != '\0') { sz = (word32)(WSTRLEN(env) + WSTRLEN(defaultName) + 1); knownHostsName = (char*)WMALLOC(sz, NULL, 0); if (knownHostsName != NULL) { WSTRCPY(knownHostsName, env); WSTRCAT(knownHostsName, defaultName); } + else + ret = -1; } else ret = -1; } if (ret == 0) { + /* Missing or empty means no entries. Unreadable rejects: it may + * hold this host's key. */ sz = 0; ret = load_text_file(knownHostsName, &knownHosts, &sz); + if (ret == LOAD_TEXT_NONE) { + knownHosts = NULL; + ret = 0; + } + else if (ret != 0) { + printf("Unable to read %s.\n", knownHostsName); + printf("Rejecting connection and closing.\n"); + knownHosts = NULL; + } } if (ret == 0) { - if (sz < sizeof(word32)) { - /* This file is too small. There must be at least a word32 - * length size. */ + /* Base64 size plus a nul */ + if (pubKeySz > MAX_PACKET_SZ) { ret = -1; } + else { + encSz = (pubKeySz + 2) / 3 * 4 + 1; + } } if (ret == 0) { - encodedKey = (char*)WMALLOC(WOLFSSH_CLIENT_ENCKEY_SIZE_ESTIMATE + encodedKey = (char*)WMALLOC(encSz + WOLFSSH_CLIENT_PUBKEYTYPE_SIZE_ESTIMATE + WOLFSSH_CLIENT_FINGERPRINT_SIZE_ESTIMATE, NULL, 0); if (encodedKey == NULL) { @@ -408,7 +479,7 @@ int ClientPublicKeyCheck(const byte* pubKey, word32 pubKeySz, void* ctx) } if (ret == 0) { - pubKeyType = encodedKey + WOLFSSH_CLIENT_ENCKEY_SIZE_ESTIMATE; + pubKeyType = encodedKey + encSz; fp = pubKeyType + WOLFSSH_CLIENT_PUBKEYTYPE_SIZE_ESTIMATE; encodedKey[0] = 0; @@ -447,7 +518,7 @@ int ClientPublicKeyCheck(const byte* pubKey, word32 pubKeySz, void* ctx) } if (ret == 0) { - sz = WOLFSSH_CLIENT_ENCKEY_SIZE_ESTIMATE; + sz = encSz; #ifdef WOLFSSL_BASE64_ENCODE ret = Base64_Encode_NoNl(pubKey, pubKeySz, (byte*)encodedKey, &sz); #else @@ -486,11 +557,10 @@ int ClientPublicKeyCheck(const byte* pubKey, word32 pubKeySz, void* ctx) if (keyTypeMatch) { if (keyMatch) { found = 1; + break; } - else { - badMatch = 1; - } - break; + /* Keep looking: a later line may hold this key. */ + badMatch = 1; } else if (knownType == NULL) { /* Host is pinned with another key type. Keep @@ -516,7 +586,7 @@ int ClientPublicKeyCheck(const byte* pubKey, word32 pubKeySz, void* ctx) } if (ret == 0) { - if (badMatch) { + if (badMatch && !found) { printf("That server is known, but that key is not.\n"); printf("Rejecting connection and closing.\n"); ret = -1; @@ -577,8 +647,8 @@ int ClientPublicKeyCheck(const byte* pubKey, word32 pubKeySz, void* ctx) #ifdef WOLFSSH_CERTS #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME) - /* try to parse the certificate and check it's IP address */ - if (pubKeySz > 0) { + /* Once known_hosts accepts the key, check a certificate's IP address. */ + if (ret == 0 && pubKeySz > 0) { DecodedCert dCert; byte* der = NULL; word32 derSz = 0; @@ -614,14 +684,13 @@ int ClientPublicKeyCheck(const byte* pubKey, word32 pubKeySz, void* ctx) } } - if (ipMatch == 0) { + if (ret == 0 && ipMatch == 0) { printf("IP did not match expected IP"); if (!IPOverride) { printf("\n"); ret = -1; } else { - ret = 0; printf("..overriding\n"); } } diff --git a/scripts/sshclient.test b/scripts/sshclient.test index 2feaa7754..ae8421256 100755 --- a/scripts/sshclient.test +++ b/scripts/sshclient.test @@ -142,7 +142,8 @@ wait_for_output() { # echoserver only ends the session when it receives a 0x03, so every # session has to send one or both ends wait for the other forever. # -# $1 - "confirm" when the client will ask about the unknown server key +# $1 - "confirm" when the client will ask about the unknown server key, +# "eof" for an empty stdin # rest - client arguments run_client() { confirm=$1 @@ -150,22 +151,28 @@ run_client() { rm -f "$input_file" "$client_out" touch "$client_out" - mkfifo "$input_file" || fail "couldn't create the input fifo" - ( - # GetConfirmation() reads a single character. A newline here would - # be left behind for the password prompt to read as an empty - # password. - [ "$confirm" = "confirm" ] && printf 'Y' - - wait_for_output "Password:" || exit 1 - printf 'upthehill\n' - printf 'hello\003' - ) > "$input_file" 2>/dev/null & - input_pid=$! + if [ "$confirm" = "eof" ]; then + client_in=/dev/null + else + client_in="$input_file" + mkfifo "$input_file" || fail "couldn't create the input fifo" + + ( + # GetConfirmation() reads a single character. A newline here + # would be left behind for the password prompt to read as an + # empty password. + [ "$confirm" = "confirm" ] && printf 'Y' + + wait_for_output "Password:" || exit 1 + printf 'upthehill\n' + printf 'hello\003' + ) > "$input_file" 2>/dev/null & + input_pid=$! + fi HOME="$work_dir" ./apps/wolfssh/wolfssh "$@" \ - < "$input_file" > "$client_out" 2>&1 & + < "$client_in" > "$client_out" 2>&1 & client_pid=$! # Poll rather than sleep through the whole limit. Killing a subshell @@ -189,19 +196,27 @@ run_client() { kill $killer_pid 2>/dev/null killer_pid=$no_pid - kill $input_pid 2>/dev/null - input_pid=$no_pid + if [ $input_pid != $no_pid ]; then + kill $input_pid 2>/dev/null + input_pid=$no_pid + fi cat "$client_out" return $client_status } -mkdir -p "$work_dir/.ssh" +mkdir -p "$work_dir" -# The known hosts check rejects a missing or empty file without asking, so -# seed the file with an entry for another host. The first connection then -# gets the "server is unknown" prompt and answers it. -echo "example.invalid ssh-rsa AAAA" > "$work_dir/.ssh/known_hosts" +# HOME starts with no ~/.ssh, so the client asks about the server. +echo "Test an unknown server with no terminal to ask" +start_server +run_client eof -p $port jill@127.0.0.1 "echo zero" +RESULT=$? +[ $RESULT -gt 128 ] && fail "the client had to be killed, prompt stuck" +[ $RESULT -eq 0 ] && fail "the client accepted a key nobody confirmed" +grep -q "Shall I add it to the known hosts?" "$client_out" \ + || fail "the client didn't ask about the unknown server" +[ -e "$work_dir/.ssh" ] && fail "a declined key created ~/.ssh" echo "Test learning the server's key" start_server @@ -217,6 +232,19 @@ grep -q "^127.0.0.1 " "$work_dir/.ssh/known_hosts" \ || fail "server key not added to the known hosts" grep -q "hello" "$client_out" \ || fail "the echoserver's reply didn't make it back" +ls -ld "$work_dir/.ssh" | grep -q "^drwx------" \ + || fail "~/.ssh was not created with mode 0700" + +echo "Test a stale known hosts entry ahead of the right one" +known_entry=`grep "^127.0.0.1 " "$work_dir/.ssh/known_hosts"` +set -- $known_entry +printf '%s %s AAAA%s\n%s\n' "$1" "$2" "$3" "$known_entry" \ + > "$work_dir/.ssh/known_hosts" +start_server +run_client "" -p $port jill@127.0.0.1 "echo stale" +[ $? -ne 0 ] && fail "a stale entry hid the server's key" +grep -q "hello" "$client_out" \ + || fail "the echoserver's reply didn't make it back" # With the server's key known, the client only prompts for the password. # The log file is empty unless the library has logging compiled in. @@ -292,6 +320,41 @@ run_client "" -E "$work_dir/appcommand.log" -p $port jill@127.0.0.1 "echo three" grep -q "hello" "$client_out" \ || fail "the app-driven echoserver's reply didn't make it back" +# The host is known with another key type, so the client asks. +echo "Test learning a large host key" +rm -f "$ready_file" +./examples/echoserver/echoserver -1 -f -k ssh-mldsa-87 -R "$ready_file" \ + > "$work_dir/server.log" 2>&1 & +server_pid=$! +counter=0 +while [ ! -s "$ready_file" ] && kill -0 $server_pid 2>/dev/null \ + && [ "$counter" -lt 100 ]; do + sleep 0.1 + counter=$((counter + 1)) +done +if [ -s "$ready_file" ]; then + port=`cat "$ready_file"` + run_client confirm -p $port jill@127.0.0.1 "echo mldsa" + [ $? -ne 0 ] && fail "failed to learn the ML-DSA host key" + grep -q "^127.0.0.1 ssh-mldsa-87 " "$work_dir/.ssh/known_hosts" \ + || fail "ML-DSA host key not added to the known hosts" + + start_server -k ssh-mldsa-87 + run_client "" -p $port jill@127.0.0.1 "echo mldsa" + [ $? -ne 0 ] && fail "the learned ML-DSA host key didn't match" +else + # Still running means slow, not missing the key type. Kill it so the + # wait can't block. + if kill -0 $server_pid 2>/dev/null; then + echo "echoserver didn't start with an ML-DSA host key, skipping" + kill -9 $server_pid 2>/dev/null + else + echo "echoserver has no ML-DSA host key, skipping" + fi + wait $server_pid 2>/dev/null + server_pid=$no_pid +fi + echo "Test the usage message" ./apps/wolfssh/wolfssh -Z 2>&1 | grep -q "usage:" \ || fail "no usage message for a bad option" diff --git a/tests/regress.c b/tests/regress.c index 6a03acdf9..850025079 100644 --- a/tests/regress.c +++ b/tests/regress.c @@ -23898,10 +23898,9 @@ static void TestAppendKeyToFile(void) AssertTrue(readSz > 0); AssertIntEQ(WSTRCMP(buf, "h\xC3\xA9st ssh-rsa AAAA\n"), 0); - /* When the file cannot be opened (here, a path under a directory that does - * not exist), the function reports the failure rather than claiming - * success. */ - AssertTrue(wolfSSH_TestAppendKeyToFile("regress_no_such_dir/known_hosts", + /* Two missing directories: only the last is created, so it fails. */ + AssertTrue(wolfSSH_TestAppendKeyToFile( + "regress_no_such_dir/sub/known_hosts", "host.example.com", "ssh-rsa", "AAAA") != WS_SUCCESS); (void)remove(path); @@ -24372,6 +24371,325 @@ static void TestKnownHostsOtherType(void) (void)rmdir(sshDir); (void)rmdir(homeDir); } + +/* Points stdin at path and clears its EOF. Returns 1 on success. */ +static int KnownHostsStdinFrom(const char* path) +{ + int fd, ok; + + fd = open(path, O_RDONLY); + ok = (fd >= 0) && (dup2(fd, STDIN_FILENO) >= 0); + if (fd >= 0) { + close(fd); + } + clearerr(stdin); + + return ok; +} + + +/* Fill blob with string(type) followed by filler up to blobSz bytes. */ +static void KnownHostsMakeBlob(byte* blob, word32 blobSz, const char* type) +{ + word32 typeSz = (word32)WSTRLEN(type); + word32 i; + + blob[0] = 0; + blob[1] = 0; + blob[2] = 0; + blob[3] = (byte)typeSz; + WMEMCPY(blob + 4, type, typeSz); + for (i = 4 + typeSz; i < blobSz; i++) { + blob[i] = (byte)(i * 7 + 1); + } +} + + +/* Base64 of blob into a new nul terminated buffer the caller frees. */ +static char* KnownHostsEncode(const byte* blob, word32 blobSz) +{ + word32 encSz = (blobSz + 2) / 3 * 4 + 1; + char* enc = (char*)WMALLOC(encSz, NULL, 0); + + AssertNotNull(enc); + if (enc != NULL) { + AssertIntEQ(Base64_Encode_NoNl(blob, blobSz, (byte*)enc, &encSz), 0); + enc[encSz] = 0; + } + + return enc; +} + + +/* known_hosts decisions match OpenSSH's StrictHostKeyChecking=ask. */ +static void TestKnownHostsAsk(void) +{ + char targetName[] = "ask.example.com"; + char homeDir[64]; + char sshDir[80]; + char hostsPath[112]; + char replyPath[80]; + char captured[1024]; + char* contents = NULL; + char* expected = NULL; + char* readBack = NULL; + char* goodKey = NULL; + char* bigKey = NULL; + char* staleKey; + char* savedHome = NULL; + const char* home; + byte blob[32]; + byte bigBlob[2000]; + word32 bigSz, readSz; + int savedStdin, ready; +#ifndef _WIN32 + struct stat st; +#endif + + WSNPRINTF(homeDir, sizeof(homeDir), "wolfssh_kha_%d.tmp", (int)getpid()); + WSNPRINTF(sshDir, sizeof(sshDir), "%s/.ssh", homeDir); + WSNPRINTF(hostsPath, sizeof(hostsPath), "%s/known_hosts", sshDir); + WSNPRINTF(replyPath, sizeof(replyPath), "%s/reply", homeDir); + + KnownHostsMakeBlob(blob, (word32)sizeof(blob), "ssh-ed25519"); + /* Over 1200 bytes once base64 encoded */ + KnownHostsMakeBlob(bigBlob, (word32)sizeof(bigBlob), "ssh-mldsa-87"); + goodKey = KnownHostsEncode(blob, (word32)sizeof(blob)); + bigKey = KnownHostsEncode(bigBlob, (word32)sizeof(bigBlob)); + bigSz = (word32)WSTRLEN(bigKey) + 64; + contents = (char*)WMALLOC(bigSz * 2, NULL, 0); + expected = (char*)WMALLOC(bigSz * 2, NULL, 0); + readBack = (char*)WMALLOC(bigSz * 2, NULL, 0); + AssertNotNull(contents); + AssertNotNull(expected); + AssertNotNull(readBack); + + home = getenv("HOME"); + if (home != NULL) { + savedHome = (char*)WMALLOC(WSTRLEN(home) + 1, NULL, 0); + AssertNotNull(savedHome); + WSTRCPY(savedHome, home); + } + + (void)remove(hostsPath); + (void)remove(replyPath); + (void)rmdir(sshDir); + (void)rmdir(homeDir); + + /* No ~/.ssh at all to start with. */ + ready = (goodKey != NULL) && (bigKey != NULL) && (contents != NULL) + && (expected != NULL) && (readBack != NULL) + && (TEST_MKDIR(homeDir, 0700) == 0) + && (TEST_SETENV("HOME", homeDir) == 0); + AssertTrue(ready); + + savedStdin = dup(STDIN_FILENO); + ready = ready && (savedStdin >= 0); + AssertTrue(ready); + WriteTextFile(replyPath, "Y"); + + if (ready) { + /* Missing file, the prompt reads EOF: refused, nothing created. */ + AssertTrue(KnownHostsStdinFrom(TEST_NULL_DEVICE)); + AssertTrue(KnownHostsCheckCapture(blob, (word32)sizeof(blob), + targetName, captured, (word32)sizeof(captured)) != 0); + AssertNotNull(WSTRSTR(captured, "The server is unknown")); + AssertNotNull(WSTRSTR(captured, "Shall I add it to the known hosts?")); + AssertIntEQ(LoadFileBuffer(hostsPath, (byte*)readBack, bigSz), 0); + #ifndef _WIN32 + AssertTrue(stat(sshDir, &st) != 0); + #endif + + /* Missing file, answered yes: ~/.ssh and the file are created. */ + AssertTrue(KnownHostsStdinFrom(replyPath)); + AssertIntEQ(KnownHostsCheckCapture(blob, (word32)sizeof(blob), + targetName, captured, (word32)sizeof(captured)), 0); + AssertNotNull(WSTRSTR(captured, "Shall I add it to the known hosts?")); + WSNPRINTF(expected, bigSz * 2, "%s ssh-ed25519 %s\n", + targetName, goodKey); + WMEMSET(readBack, 0, bigSz * 2); + readSz = LoadFileBuffer(hostsPath, (byte*)readBack, bigSz * 2 - 1); + AssertTrue(readSz > 0); + AssertStrEQ(readBack, expected); + #ifndef _WIN32 + AssertIntEQ(stat(sshDir, &st), 0); + AssertIntEQ((int)(st.st_mode & 0777), 0700); + + /* Empty HOME: refused before the prompt, nothing written under /. */ + AssertIntEQ(TEST_SETENV("HOME", ""), 0); + AssertTrue(KnownHostsStdinFrom(replyPath)); + AssertTrue(KnownHostsCheckCapture(blob, (word32)sizeof(blob), + targetName, captured, (word32)sizeof(captured)) != 0); + AssertNull(WSTRSTR(captured, "Shall I add it to the known hosts?")); + AssertIntEQ(TEST_SETENV("HOME", homeDir), 0); + #endif + + /* Empty file, the prompt reads EOF: asked, then refused. */ + WriteTextFile(hostsPath, ""); + AssertTrue(KnownHostsStdinFrom(TEST_NULL_DEVICE)); + AssertTrue(KnownHostsCheckCapture(blob, (word32)sizeof(blob), + targetName, captured, (word32)sizeof(captured)) != 0); + AssertNotNull(WSTRSTR(captured, "Shall I add it to the known hosts?")); + + /* A stale key for the host, then the right one: accepted. */ + staleKey = expected; + WSTRCPY(staleKey, goodKey); + staleKey[0] = (goodKey[0] == 'A') ? 'B' : 'A'; + WSNPRINTF(contents, bigSz * 2, "%s ssh-ed25519 %s\n%s ssh-ed25519 %s\n", + targetName, staleKey, targetName, goodKey); + WriteTextFile(hostsPath, contents); + AssertIntEQ(KnownHostsCheckCapture(blob, (word32)sizeof(blob), + targetName, captured, (word32)sizeof(captured)), 0); + AssertNull(WSTRSTR(captured, "That server is known, but")); + AssertNull(WSTRSTR(captured, "Shall I add it to the known hosts?")); + + /* The right key, then a stale one: still accepted. */ + WSNPRINTF(contents, bigSz * 2, "%s ssh-ed25519 %s\n%s ssh-ed25519 %s\n", + targetName, goodKey, targetName, staleKey); + WriteTextFile(hostsPath, contents); + AssertIntEQ(KnownHostsCheckCapture(blob, (word32)sizeof(blob), + targetName, captured, (word32)sizeof(captured)), 0); + + /* Only stale keys: rejected without a prompt. */ + WSNPRINTF(contents, bigSz * 2, "%s ssh-ed25519 %s\n%s ssh-ed25519 %s\n", + targetName, staleKey, targetName, staleKey); + WriteTextFile(hostsPath, contents); + AssertTrue(KnownHostsCheckCapture(blob, (word32)sizeof(blob), + targetName, captured, (word32)sizeof(captured)) != 0); + AssertNotNull(WSTRSTR(captured, + "That server is known, but that key is not.")); + AssertNull(WSTRSTR(captured, "Shall I add it to the known hosts?")); + + #ifndef _WIN32 + /* Unreadable: rejected, not learned over. Root can read it. */ + if (geteuid() != 0) { + AssertIntEQ(chmod(hostsPath, 0), 0); + AssertTrue(KnownHostsStdinFrom(replyPath)); + AssertTrue(KnownHostsCheckCapture(blob, (word32)sizeof(blob), + targetName, captured, (word32)sizeof(captured)) != 0); + AssertNotNull(WSTRSTR(captured, "Unable to read")); + AssertNull(WSTRSTR(captured, + "Shall I add it to the known hosts?")); + AssertIntEQ(chmod(hostsPath, 0600), 0); + WMEMSET(readBack, 0, bigSz * 2); + readSz = LoadFileBuffer(hostsPath, (byte*)readBack, + bigSz * 2 - 1); + AssertTrue(readSz > 0); + AssertStrEQ(readBack, contents); + } + #endif + + /* A large host key blob is learned, then matched. */ + (void)remove(hostsPath); + AssertTrue(KnownHostsStdinFrom(replyPath)); + AssertIntEQ(KnownHostsCheckCapture(bigBlob, (word32)sizeof(bigBlob), + targetName, captured, (word32)sizeof(captured)), 0); + WSNPRINTF(expected, bigSz * 2, "%s ssh-mldsa-87 %s\n", + targetName, bigKey); + WMEMSET(readBack, 0, bigSz * 2); + readSz = LoadFileBuffer(hostsPath, (byte*)readBack, bigSz * 2 - 1); + AssertTrue(readSz > 0); + AssertStrEQ(readBack, expected); + AssertTrue(KnownHostsStdinFrom(TEST_NULL_DEVICE)); + AssertIntEQ(KnownHostsCheckCapture(bigBlob, (word32)sizeof(bigBlob), + targetName, captured, (word32)sizeof(captured)), 0); + AssertNull(WSTRSTR(captured, "Shall I add it to the known hosts?")); + } + +#if defined(WOLFSSH_CERTS) && !defined(WOLFSSH_NO_ECDSA_SHA2_NISTP256) && \ + (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)) + if (ready) { + /* Cert with IP SAN 127.0.0.1, checked against another address + * with the IP override on. */ + const char* certType = "x509v3-ecdsa-sha2-nistp256"; + char certTarget[] = "192.0.2.1"; + byte* certBlob; + char* certKey; + word32 certSz, typeSz, certBlobSz, i; + + typeSz = (word32)WSTRLEN(certType); + certBlob = (byte*)WMALLOC(4096, NULL, 0); + AssertNotNull(certBlob); + certSz = LoadFileBuffer("keys/server-cert.der", certBlob + 12 + typeSz, + 4096 - 16 - typeSz); + AssertTrue(certSz > 0); + /* string(type), cert count 1, string(cert), OCSP count 0 */ + WMEMSET(certBlob, 0, 12 + typeSz); + certBlob[3] = (byte)typeSz; + WMEMCPY(certBlob + 4, certType, typeSz); + certBlob[7 + typeSz] = 1; + certBlob[10 + typeSz] = (byte)(certSz >> 8); + certBlob[11 + typeSz] = (byte)certSz; + i = 12 + typeSz + certSz; + WMEMSET(certBlob + i, 0, 4); + certBlobSz = i + 4; + certKey = KnownHostsEncode(certBlob, certBlobSz); + AssertNotNull(certKey); + + ClientIPOverride(1); + + /* Known host, wrong key: the override does not accept it. */ + WSNPRINTF(contents, bigSz * 2, "%s %s AAAA\n", certTarget, certType); + WriteTextFile(hostsPath, contents); + AssertTrue(KnownHostsCheckCapture(certBlob, certBlobSz, certTarget, + captured, (word32)sizeof(captured)) != 0); + AssertNotNull(WSTRSTR(captured, + "That server is known, but that key is not.")); + + /* Unknown host, prompt declined: the override does not accept it. */ + (void)remove(hostsPath); + AssertTrue(KnownHostsStdinFrom(TEST_NULL_DEVICE)); + AssertTrue(KnownHostsCheckCapture(certBlob, certBlobSz, certTarget, + captured, (word32)sizeof(captured)) != 0); + + /* Known host and key: the override waives only the IP mismatch. */ + WSNPRINTF(contents, bigSz * 2, "%s %s %s\n", + certTarget, certType, certKey); + WriteTextFile(hostsPath, contents); + AssertIntEQ(KnownHostsCheckCapture(certBlob, certBlobSz, certTarget, + captured, (word32)sizeof(captured)), 0); + AssertNotNull(WSTRSTR(captured, "..overriding")); + + ClientIPOverride(0); + AssertTrue(KnownHostsCheckCapture(certBlob, certBlobSz, certTarget, + captured, (word32)sizeof(captured)) != 0); + AssertNotNull(WSTRSTR(captured, "IP did not match expected IP")); + + WFREE(certKey, NULL, 0); + WFREE(certBlob, NULL, 0); + } +#endif + + if (savedStdin >= 0) { + AssertTrue(dup2(savedStdin, STDIN_FILENO) >= 0); + close(savedStdin); + clearerr(stdin); + } + + if (savedHome != NULL) { + AssertIntEQ(TEST_SETENV("HOME", savedHome), 0); + WFREE(savedHome, NULL, 0); + } + else { + TEST_UNSETENV("HOME"); + } + + (void)remove(hostsPath); + (void)remove(replyPath); + (void)rmdir(sshDir); + (void)rmdir(homeDir); + + if (readBack != NULL) + WFREE(readBack, NULL, 0); + if (expected != NULL) + WFREE(expected, NULL, 0); + if (contents != NULL) + WFREE(contents, NULL, 0); + if (bigKey != NULL) + WFREE(bigKey, NULL, 0); + if (goodKey != NULL) + WFREE(goodKey, NULL, 0); +} #endif /* WOLFSSL_BASE64_ENCODE */ @@ -24413,6 +24731,7 @@ int main(int argc, char** argv) #ifdef WOLFSSL_BASE64_ENCODE TestKnownHostsLastEntry(); TestKnownHostsOtherType(); + TestKnownHostsAsk(); #endif #ifndef NO_WOLFSSH_CLIENT TestAuthMessageBlockedDuringKeying(ssh);