diff --git a/.github/workflows/interop-mlkem.yml b/.github/workflows/interop-mlkem.yml index 2ddfef156..570aa9901 100644 --- a/.github/workflows/interop-mlkem.yml +++ b/.github/workflows/interop-mlkem.yml @@ -113,4 +113,4 @@ jobs: run: | mkdir -p /tmp/empty ${{ github.workspace }}/build-dir/sbin/sshd -p 22222 -o KbdInteractiveAuthentication=no -o PasswordAuthentication=no -o KexAlgorithms=mlkem768x25519-sha256 - ./examples/client/client -u $USER -i $HOME/.ssh/id_ed25519 -j $HOME/.ssh/id_ed25519.pub -c "ls /" + ./examples/client/client -Y -u $USER -i $HOME/.ssh/id_ed25519 -j $HOME/.ssh/id_ed25519.pub -c "ls /" diff --git a/.github/workflows/network-contention-test.yml b/.github/workflows/network-contention-test.yml index 26a1c06c2..d97460079 100644 --- a/.github/workflows/network-contention-test.yml +++ b/.github/workflows/network-contention-test.yml @@ -138,7 +138,7 @@ jobs: set testfile [lindex $argv 0] set outfile [lindex $argv 1] - spawn ./examples/sftpclient/wolfsftp -N -h 127.0.0.1 -p 22222 -u jill + spawn ./examples/sftpclient/wolfsftp -Y -N -h 127.0.0.1 -p 22222 -u jill expect "Password:" send "upthehill\r" expect "wolfSSH sftp>" diff --git a/.github/workflows/scp-test.yml b/.github/workflows/scp-test.yml index bfcfaecf0..77f0603d5 100644 --- a/.github/workflows/scp-test.yml +++ b/.github/workflows/scp-test.yml @@ -106,7 +106,7 @@ jobs: run: | mkdir /tmp/wolfssh echo "test file" > /tmp/wolfssh/test.txt - ./examples/scpclient/wolfscp -p 22 -u jak -P password -H 127.0.0.1 -L /tmp/wolfssh/test.txt:/tmp/non_existent_folder/ || true + ./examples/scpclient/wolfscp -Y -p 22 -u jak -P password -H 127.0.0.1 -L /tmp/wolfssh/test.txt:/tmp/non_existent_folder/ || true # check that the directory and file do not exist [ ! -d /tmp/non_existent_folder ] [ ! -f /tmp/non_existent_folder/test.txt ] diff --git a/.github/workflows/sftp-test.yml b/.github/workflows/sftp-test.yml index f3b022321..de040784e 100644 --- a/.github/workflows/sftp-test.yml +++ b/.github/workflows/sftp-test.yml @@ -119,7 +119,7 @@ jobs: cat > /tmp/sftp_test.exp << 'EOF' #!/usr/bin/expect -f set timeout 60 - spawn ./examples/sftpclient/wolfsftp -N -h 127.0.0.1 -p 22222 -u jill + spawn ./examples/sftpclient/wolfsftp -Y -N -h 127.0.0.1 -p 22222 -u jill expect "Password:" send "upthehill\r" expect "wolfSSH sftp>" @@ -209,7 +209,7 @@ jobs: working-directory: ./wolfssh/ run: | port=$(cat /tmp/echoserver_ready) - ./examples/sftpclient/wolfsftp -N -u jill -P upthehill -p "$port" \ + ./examples/sftpclient/wolfsftp -Y -N -u jill -P upthehill -p "$port" \ -G -l /tmp/large_test_copy.dat -r "$(pwd)/large_test.dat" - name: Verify file integrity diff --git a/.github/workflows/test-fatfs.yml b/.github/workflows/test-fatfs.yml index 6521d6fe4..931abc17d 100644 --- a/.github/workflows/test-fatfs.yml +++ b/.github/workflows/test-fatfs.yml @@ -121,7 +121,7 @@ jobs: cat > /tmp/sftp_test.exp << EOF #!/usr/bin/expect -f set timeout 60 - spawn ./wolfsftp -N -h localhost -p 22 -u testuser + spawn ./wolfsftp -Y -N -h localhost -p 22 -u testuser expect "Password:" send "password123\r" expect "wolfSSH sftp>" diff --git a/.github/workflows/tpm-ssh.yml b/.github/workflows/tpm-ssh.yml index 10ae482e3..d3476ed4f 100644 --- a/.github/workflows/tpm-ssh.yml +++ b/.github/workflows/tpm-ssh.yml @@ -216,6 +216,29 @@ jobs: grep -q "Listening on port" tpmcert_server_neg.txt grep -q "wolfSSH_connect failed" tpmcert_client_neg.txt + # Negative test: the trusted certificate names 127.0.0.1, not localhost. + - name: Test TPM X.509 host certificate rejects another host (${{ matrix.keytype }}) + if: matrix.hostkey == 'x509' + run: | + cd wolfssh + ./examples/tpmcertserver/tpmcertserver -k ${{ matrix.keytype }} \ + -p 22227 > tpmcert_server_host.txt 2>&1 & + echo "tpmcertserver (X.509 host ${{ matrix.keytype }}) PID: $!" + for i in $(seq 1 40); do + if ss -ltn 2>/dev/null | grep -q ':22227'; then break; fi + sleep 1 + done + if ./examples/tpmcertserver/tpmcertclient -A tpm-server-cert.der \ + -p 22227 -h localhost > tpmcert_client_host.txt 2>&1; then + echo "ERROR: client accepted a certificate for another host" + cat tpmcert_client_host.txt + exit 1 + fi + echo "----- server -----"; cat tpmcert_server_host.txt + echo "----- client -----"; cat tpmcert_client_host.txt + grep -q "The host certificate does not name localhost" \ + tpmcert_client_host.txt + # Client public-key authentication with a TPM-resident key (RSA only). - name: Test TPM client public-key auth if: matrix.keytype == 'rsa' && matrix.hostkey == 'raw' @@ -227,7 +250,7 @@ jobs: ./examples/echoserver/echoserver -1 -s key.ssh & echo "Echoserver (authorized TPM client key) PID: $!" sleep 2 - timeout 20 ./examples/client/client -i ../wolftpm/keyblob.bin \ + timeout 20 ./examples/client/client -Y -i ../wolftpm/keyblob.bin \ -u hansel -K ThisIsMyKeyAuth # The same TPM key offered as an OpenSSH user certificate. The certificate @@ -244,7 +267,7 @@ jobs: SERVER_PID=$! echo "Echoserver (TPM key behind an OpenSSH certificate) PID: $SERVER_PID" sleep 2 - timeout 20 ./examples/client/client -i ../wolftpm/keyblob.bin \ + timeout 20 ./examples/client/client -Y -i ../wolftpm/keyblob.bin \ -u hansel -K ThisIsMyKeyAuth -j tpmkey-cert.pub -p 22225 -x \ < /dev/null > ossh_cert_client.txt 2>&1 || { cat ossh_cert_client.txt @@ -271,7 +294,7 @@ jobs: SERVER_PID=$! echo "Echoserver (another key's certificate) PID: $SERVER_PID" sleep 2 - if timeout 20 ./examples/client/client -i ../wolftpm/keyblob.bin \ + if timeout 20 ./examples/client/client -Y -i ../wolftpm/keyblob.bin \ -u hansel -K ThisIsMyKeyAuth -j other-cert.pub -p 22226 -x \ < /dev/null > ossh_cert_client_neg.txt 2>&1; then echo "ERROR: a certificate for another key authenticated" @@ -309,7 +332,7 @@ jobs: # $1 blob, $2 expected client output, $3 optional key auth. reject() { echo "----- $1 -----" - if timeout 20 ./examples/client/client -i "$1" -u hansel \ + if timeout 20 ./examples/client/client -Y -i "$1" -u hansel \ -K "${3:-ThisIsMyKeyAuth}" < /dev/null > reject.txt 2>&1; then echo "ERROR: the client accepted $1" cat reject.txt diff --git a/.github/workflows/windows-cert-store-test.yml b/.github/workflows/windows-cert-store-test.yml index 6be94430e..49d37b237 100644 --- a/.github/workflows/windows-cert-store-test.yml +++ b/.github/workflows/windows-cert-store-test.yml @@ -975,7 +975,7 @@ jobs: quit "@ | Out-File -FilePath sftp_echo_commands.txt -Encoding ASCII - $sftpArgs = @("-u", "testuser", "-h", "localhost", "-p", "$testPort") + $sftpArgs = @("-Y", "-u", "testuser", "-h", "localhost", "-p", "$testPort") $caCertDer = (Resolve-Path "keys\ca-cert-ecc.der").Path if ("${{ matrix.client_key_source }}" -eq "store") { $sftpArgs += "-W", "My:$($env:CLIENT_CERT_SUBJECT):CURRENT_USER" @@ -1035,7 +1035,7 @@ jobs: quit "@ | Out-File -FilePath sftp_x509_commands.txt -Encoding ASCII - $sftpArgs = @("-u", "testuser", "-h", "localhost", "-p", "$testPort") + $sftpArgs = @("-Y", "-u", "testuser", "-h", "localhost", "-p", "$testPort") if ("${{ matrix.client_key_source }}" -eq "store") { $sftpArgs += "-W", "My:$($env:CLIENT_CERT_SUBJECT):CURRENT_USER" } else { @@ -1439,7 +1439,7 @@ jobs: quit "@ | Out-File -FilePath sftp_commands.txt -Encoding ASCII - $sftpArgs = @("-u", "testuser", "-h", "localhost", "-p", "$testPort") + $sftpArgs = @("-Y", "-u", "testuser", "-h", "localhost", "-p", "$testPort") $caCertDer = (Resolve-Path "keys\ca-cert-ecc.der").Path if ("${{ matrix.client_key_source }}" -eq "store") { $sftpArgs += "-W", "My:$($env:CLIENT_CERT_SUBJECT):CURRENT_USER" @@ -1538,7 +1538,7 @@ jobs: # Control connection: the exact-case user against the reconfigured # service must still succeed, so a later failure is attributable to # the case-differing name rather than to service state. - $args0 = @("-u", "testuser", "-h", "localhost", "-p", "$testPort", + $args0 = @("-Y", "-u", "testuser", "-h", "localhost", "-p", "$testPort", "-J", (Resolve-Path $env:CLIENT_CERT_FILE).Path, "-i", (Resolve-Path $env:CLIENT_KEY_FILE).Path, "-A", $caCertDer, "-X") @@ -1550,7 +1550,7 @@ jobs: $controlExit = $p.ExitCode # CN=wronguser presented for -u testuser must fail - $args1 = @("-u", "testuser", "-h", "localhost", "-p", "$testPort", + $args1 = @("-Y", "-u", "testuser", "-h", "localhost", "-p", "$testPort", "-J", (Resolve-Path "keys\wronguser-cert.der").Path, "-i", (Resolve-Path "keys\wronguser-key.der").Path, "-A", $caCertDer, "-X") @@ -1563,7 +1563,7 @@ jobs: # CN=testuser presented for -u TESTUSER must succeed: Windows account # names are case-insensitive and the CN match follows suit - $args2 = @("-u", "TESTUSER", "-h", "localhost", "-p", "$testPort", + $args2 = @("-Y", "-u", "TESTUSER", "-h", "localhost", "-p", "$testPort", "-J", (Resolve-Path $env:CLIENT_CERT_FILE).Path, "-i", (Resolve-Path $env:CLIENT_KEY_FILE).Path, "-A", $caCertDer, "-X") @@ -1636,7 +1636,7 @@ jobs: run: | $PSNativeCommandUseErrorActionPreference = $false "quit" | Out-File -FilePath sftp_wconflict_cmd.txt -Encoding ASCII - $conflictArgs = @("-u", "testuser", "-h", "localhost", + $conflictArgs = @("-Y", "-u", "testuser", "-h", "localhost", "-p", "${{env.TEST_PORT}}", "-W", "My:$($env:CLIENT_CERT_SUBJECT):CURRENT_USER", "-i", (Resolve-Path $env:CLIENT_KEY_FILE).Path) diff --git a/.github/workflows/windows-sftp.yml b/.github/workflows/windows-sftp.yml index ec8d648b7..7d89b1a09 100644 --- a/.github/workflows/windows-sftp.yml +++ b/.github/workflows/windows-sftp.yml @@ -365,7 +365,7 @@ jobs: $getCommands | Out-File -FilePath sftp_get_nonexistent_commands.txt -Encoding ASCII $proc = Start-Process -FilePath $sftpPath ` - -ArgumentList "-u", "testuser", "-P", $env:TESTUSER_PASSWORD, "-h", "localhost", "-p", "${{env.TEST_PORT}}" ` + -ArgumentList "-Y", "-u", "testuser", "-P", $env:TESTUSER_PASSWORD, "-h", "localhost", "-p", "${{env.TEST_PORT}}" ` -RedirectStandardInput "sftp_get_nonexistent_commands.txt" ` -RedirectStandardOutput "sftp_get_nonexistent_out.txt" ` -RedirectStandardError "sftp_get_nonexistent_err.txt" ` @@ -415,7 +415,7 @@ jobs: } $proc = Start-Process -FilePath $env:CLIENT_PATH ` - -ArgumentList "-u", "testuser", "-P", $env:TESTUSER_PASSWORD, "-h", "localhost", "-p", "${{env.TEST_PORT}}", "-c", "whoami" ` + -ArgumentList "-Y", "-u", "testuser", "-P", $env:TESTUSER_PASSWORD, "-h", "localhost", "-p", "${{env.TEST_PORT}}", "-c", "whoami" ` -RedirectStandardInput "exec_stdin.txt" ` -RedirectStandardOutput "exec_out.txt" ` -RedirectStandardError "exec_err.txt" ` @@ -500,7 +500,7 @@ jobs: function Start-Hold($script, $tag) { return Start-Process -FilePath $env:CLIENT_PATH -ArgumentList @( - "-u", "testuser2", "-P", $env:TESTUSER_PASSWORD, + "-Y", "-u", "testuser2", "-P", $env:TESTUSER_PASSWORD, "-h", "localhost", "-p", "${{env.TEST_PORT}}", "-c", $script) ` -RedirectStandardInput "hold_stdin.txt" ` -RedirectStandardOutput "hold_${tag}_out.txt" ` @@ -613,7 +613,7 @@ jobs: $testCommands | Out-File -FilePath sftp_commands.txt -Encoding ASCII $process = Start-Process -FilePath $sftpPath ` - -ArgumentList "-u", "testuser", "-P", $env:TESTUSER_PASSWORD, "-h", "localhost", "-p", "${{env.TEST_PORT}}" ` + -ArgumentList "-Y", "-u", "testuser", "-P", $env:TESTUSER_PASSWORD, "-h", "localhost", "-p", "${{env.TEST_PORT}}" ` -RedirectStandardInput "sftp_commands.txt" ` -RedirectStandardOutput "sftp_output.txt" ` -RedirectStandardError "sftp_error.txt" ` @@ -751,7 +751,7 @@ jobs: $putCommands = "put $largeFile large_test.dat`nquit" $putCommands | Out-File -FilePath (Join-Path $workDir "sftp_put_commands.txt") -Encoding ASCII $proc = Start-Process -FilePath $sftpPath ` - -ArgumentList "-u", "testuser", "-P", $env:TESTUSER_PASSWORD, "-h", "localhost", "-p", "${{env.TEST_PORT}}" ` + -ArgumentList "-Y", "-u", "testuser", "-P", $env:TESTUSER_PASSWORD, "-h", "localhost", "-p", "${{env.TEST_PORT}}" ` -WorkingDirectory $workDir ` -RedirectStandardInput (Join-Path $workDir "sftp_put_commands.txt") ` -RedirectStandardOutput (Join-Path $workDir "sftp_put_out.txt") ` @@ -774,7 +774,7 @@ jobs: $getCommands = "get large_test.dat large_test_copy.dat`nquit" $getCommands | Out-File -FilePath (Join-Path $workDir "sftp_get_commands.txt") -Encoding ASCII $proc2 = Start-Process -FilePath $sftpPath ` - -ArgumentList "-u", "testuser", "-P", $env:TESTUSER_PASSWORD, "-h", "localhost", "-p", "${{env.TEST_PORT}}" ` + -ArgumentList "-Y", "-u", "testuser", "-P", $env:TESTUSER_PASSWORD, "-h", "localhost", "-p", "${{env.TEST_PORT}}" ` -WorkingDirectory $workDir ` -RedirectStandardInput (Join-Path $workDir "sftp_get_commands.txt") ` -RedirectStandardOutput (Join-Path $workDir "sftp_get_out.txt") ` diff --git a/README.md b/README.md index d1a0d2776..89883bfea 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,17 @@ The client tool accepts the following command line options: -t use pseudo terminal -c executes remote command and pipe stdin/stdout -a Attempt to use SSH-AGENT + -Y accept any host key, insecure + +The client, scpclient and sftpclient refuse a server whose host key is not +on a `host type key` line of `~/.ssh/known_hosts`, and print the line to +add. As with OpenSSH, a server on a port other than 22 is named +`[host]:port`, with the bare host as a fallback when no `[host]:port` line +has a key of that type; a line may list several names separated by commas, +and hashed `|1|` names match. `-Y` skips that check, with a warning. A server's X.509 host +certificate must chain to the CA given with `-A` and name the host in a +subjectAltName, or its common name when it has none; `-X` accepts one +that names another host. portfwd @@ -199,6 +210,8 @@ The portfwd tool accepts the following command line options: -f host port to forward from (REQUIRED) -T host to forward to, default to host -t port to forward to (REQUIRED) + -Y accept any host key, insecure; otherwise it must be in + ~/.ssh/known_hosts scpclient @@ -215,6 +228,7 @@ The scpclient tool accepts the following command line options: -P password for username, prompted if omitted -L : copy from local to server -S : copy from server to local + -Y accept any host key, insecure sftpclient @@ -237,6 +251,7 @@ The sftpclient tool accepts the following command line options: -r remote filename -g put local filename as remote filename -G get remote filename as local filename + -Y accept any host key, insecure SCP diff --git a/apps/wolfssh-options.c b/apps/wolfssh-options.c index 9b03f9da9..4821ab989 100644 --- a/apps/wolfssh-options.c +++ b/apps/wolfssh-options.c @@ -198,5 +198,10 @@ int main(void) printf("TEST_BLOCK\n"); #endif + /* The examples default to ::1, not 127.0.0.1. */ +#ifdef TEST_IPV6 + printf("TEST_IPV6\n"); +#endif + return 0; } diff --git a/apps/wolfsshd/test/error_return.sh b/apps/wolfsshd/test/error_return.sh index 9240818bb..3c1d3bda6 100755 --- a/apps/wolfsshd/test/error_return.sh +++ b/apps/wolfsshd/test/error_return.sh @@ -18,8 +18,8 @@ if [ -z "$1" ] || [ -z "$2" ]; then exit 1 fi -echo "$TEST_CLIENT -c 'bash -c \"(exit 2)\"' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \"$1\" -p \"$2\"" -$TEST_CLIENT -c 'bash -c "(exit 2)"' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h "$1" -p "$2" +echo "$TEST_CLIENT -Y -c 'bash -c \"(exit 2)\"' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \"$1\" -p \"$2\"" +$TEST_CLIENT -Y -c 'bash -c "(exit 2)"' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h "$1" -p "$2" RESULT=$? if [ "$RESULT" != 2 ]; then echo "Expecting error return value of 2 for failed ls command, found $RESULT" diff --git a/apps/wolfsshd/test/run_all_sshd_tests.sh b/apps/wolfsshd/test/run_all_sshd_tests.sh index f3267ae77..b06af92c4 100755 --- a/apps/wolfsshd/test/run_all_sshd_tests.sh +++ b/apps/wolfsshd/test/run_all_sshd_tests.sh @@ -519,7 +519,7 @@ run_strictmodes_authkeys_negative_test() { # non-zero exit below can be attributed to the permission change rather than # an unrelated client/connection failure. chmod 0644 authorized_keys_test - ( cd ../../.. && $tmo ./examples/client/client -c 'exit' -u "$USER" \ + ( cd ../../.. && $tmo ./examples/client/client -Y -c 'exit' -u "$USER" \ -i ./keys/hansel-key-ecc.der -j ./keys/hansel-key-ecc.pub \ -h "$TEST_HOST" -p "$TEST_PORT" ) > /dev/null 2>&1 if [ "$?" != 0 ]; then @@ -539,7 +539,7 @@ run_strictmodes_authkeys_negative_test() { before=$(grep -c "failed StrictModes check" log.txt 2>/dev/null) before=${before:-0} chmod 0666 authorized_keys_test - ( cd ../../.. && $tmo ./examples/client/client -c 'exit' -u "$USER" \ + ( cd ../../.. && $tmo ./examples/client/client -Y -c 'exit' -u "$USER" \ -i ./keys/hansel-key-ecc.der -j ./keys/hansel-key-ecc.pub \ -h "$TEST_HOST" -p "$TEST_PORT" ) > /dev/null 2>&1 local result=$? diff --git a/apps/wolfsshd/test/sshd_authkeys_none_test.sh b/apps/wolfsshd/test/sshd_authkeys_none_test.sh index 953980df9..f4db1ba72 100755 --- a/apps/wolfsshd/test/sshd_authkeys_none_test.sh +++ b/apps/wolfsshd/test/sshd_authkeys_none_test.sh @@ -102,7 +102,7 @@ try_login() { echo "FAIL: daemon did not start with $1" exit 1 fi - RESULT=$( timeout 10 $TEST_CLIENT -c 'echo authkeys""_none_ran' \ + RESULT=$( timeout 10 $TEST_CLIENT -Y -c 'echo authkeys""_none_ran' \ -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT \ 2>&1 ) echo "$RESULT" @@ -237,7 +237,7 @@ try_cert_login() { echo "FAIL: daemon did not start with $1" exit 1 fi - RESULT=$( timeout 10 $TEST_CLIENT -X -c 'echo authkeys""_none_ran' \ + RESULT=$( timeout 10 $TEST_CLIENT -Y -X -c 'echo authkeys""_none_ran' \ -u $USER -i $CERT_KEY -J $CERT -A $CA_CERT \ -h $TEST_HOST -p $TEST_PORT 2>&1 ) echo "$RESULT" diff --git a/apps/wolfsshd/test/sshd_bad_sftp_test.sh b/apps/wolfsshd/test/sshd_bad_sftp_test.sh index c885b7011..75e6e7f52 100755 --- a/apps/wolfsshd/test/sshd_bad_sftp_test.sh +++ b/apps/wolfsshd/test/sshd_bad_sftp_test.sh @@ -27,10 +27,15 @@ fi mkdir test-$$ mkdir test-$$/subfolder -echo "$TEST_SFTP_CLIENT -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -g -l configure -r `pwd`/test-$$/subfolder/ -h \"$1\" -p \"$2\"" -"$TEST_SFTP_CLIENT -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -g -l configure -r `pwd`/test-$$/subfolder/ -h $1 -p $2" +echo "$TEST_SFTP_CLIENT -Y -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -g -l configure -r `pwd`/test-$$/subfolder/ -h \"$1\" -p \"$2\"" +"$TEST_SFTP_CLIENT" -Y -u "$USER" -i "$PRIVATE_KEY" -j "$PUBLIC_KEY" -g \ + -l configure -r "`pwd`/test-$$/subfolder/" -h "$1" -p "$2" RESULT=$? +if [ "$RESULT" = "126" ] || [ "$RESULT" = "127" ]; then + echo "Couldn't run $TEST_SFTP_CLIENT" + exit 1 +fi if [ "$RESULT" = "0" ]; then echo "Expecting to fail transfer to folder" exit 1 diff --git a/apps/wolfsshd/test/sshd_chroot_fail_test.sh b/apps/wolfsshd/test/sshd_chroot_fail_test.sh index 846dbd78b..43af5e634 100755 --- a/apps/wolfsshd/test/sshd_chroot_fail_test.sh +++ b/apps/wolfsshd/test/sshd_chroot_fail_test.sh @@ -193,7 +193,7 @@ run_case() { # the far side, and what matters is what the daemon did, not how the client # reported it. The output is kept, because a client that dies before opening # a socket leaves the daemon-side counts flat and looks like a daemon fault. - $TIMEOUT "$TEST_CLIENT" -c "echo $TOKEN" -u "$USER" -i "$PRIVATE_KEY" \ + $TIMEOUT "$TEST_CLIENT" -Y -c "echo $TOKEN" -u "$USER" -i "$PRIVATE_KEY" \ -j "$PUBLIC_KEY" -h "$TEST_HOST" -p "$TEST_PORT" \ > "$CLIENT_LOG" 2>&1 diff --git a/apps/wolfsshd/test/sshd_chroot_none_test.sh b/apps/wolfsshd/test/sshd_chroot_none_test.sh index 4a212e1b0..1e48b95d9 100755 --- a/apps/wolfsshd/test/sshd_chroot_none_test.sh +++ b/apps/wolfsshd/test/sshd_chroot_none_test.sh @@ -76,7 +76,7 @@ try_login() { echo "FAIL: daemon did not start with $1" exit 1 fi - RESULT=$( timeout 10 $TEST_CLIENT -c 'echo chroot""_none_ran' \ + RESULT=$( timeout 10 $TEST_CLIENT -Y -c 'echo chroot""_none_ran' \ -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT \ 2>&1 ) echo "$RESULT" diff --git a/apps/wolfsshd/test/sshd_empty_password_test.sh b/apps/wolfsshd/test/sshd_empty_password_test.sh index c54742900..2bd0f96f5 100755 --- a/apps/wolfsshd/test/sshd_empty_password_test.sh +++ b/apps/wolfsshd/test/sshd_empty_password_test.sh @@ -50,7 +50,7 @@ fi TEST_CLIENT="../../../examples/client/client" # Send an empty password -timeout 10 $TEST_CLIENT -u "$USER" -P "" -c 'true' \ +timeout 10 $TEST_CLIENT -Y -u "$USER" -P "" -c 'true' \ -h "$TEST_HOST" -p "$TEST_PORT" > /dev/null 2>&1 # Let wolfsshd flush the log before we stop it. diff --git a/apps/wolfsshd/test/sshd_exec_test.sh b/apps/wolfsshd/test/sshd_exec_test.sh index 6cdf20cd3..5aff543cb 100755 --- a/apps/wolfsshd/test/sshd_exec_test.sh +++ b/apps/wolfsshd/test/sshd_exec_test.sh @@ -19,8 +19,8 @@ if [ -z "$1" ] || [ -z "$2" ]; then fi set -e -echo "$TEST_CLIENT -c 'ls' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \"$1\" -p \"$2\"" -$TEST_CLIENT -c 'ls' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h "$1" -p "$2" +echo "$TEST_CLIENT -Y -c 'ls' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \"$1\" -p \"$2\"" +$TEST_CLIENT -Y -c 'ls' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h "$1" -p "$2" set +e diff --git a/apps/wolfsshd/test/sshd_forcedcmd_test.sh b/apps/wolfsshd/test/sshd_forcedcmd_test.sh index 05f035130..20eedba8e 100755 --- a/apps/wolfsshd/test/sshd_forcedcmd_test.sh +++ b/apps/wolfsshd/test/sshd_forcedcmd_test.sh @@ -48,7 +48,7 @@ check_scp_denied() { MARK=`log_mark` rm -f "$TESTDIR/scp_dst.dat" echo "scp payload" > "$TESTDIR/scp_src.dat" - $TEST_SCP -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY \ + $TEST_SCP -Y -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY \ -S"$TESTDIR/scp_src.dat:$TESTDIR/scp_dst.dat" \ -H $TEST_HOST -p $TEST_PORT > /dev/null 2>&1 || true if ! log_since $MARK | grep -q "denying SCP"; then @@ -86,7 +86,7 @@ TEST_SCP="./examples/scpclient/wolfscp" PRIVATE_KEY="./keys/hansel-key-ecc.der" PUBLIC_KEY="./keys/hansel-key-ecc.pub" -RESULT=$( $TEST_CLIENT -c 'echo bob' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT ) +RESULT=$( $TEST_CLIENT -Y -c 'echo bob' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT ) echo $RESULT echo $RESULT | grep bob RESULT=$? @@ -96,7 +96,7 @@ if [ "$RESULT" == 0 ]; then fi set -e -echo exit | $TEST_SFTP -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT +echo exit | $TEST_SFTP -Y -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT check_scp_denied "ForceCommand internal-sftp" @@ -128,7 +128,7 @@ cd ../../.. # Running "internal-sftp -u 0002" as a command would print no "bob" either, # so the refusal is read from the log. MARK=`log_mark` -RESULT=$( $TEST_CLIENT -c 'echo bob' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT ) || true +RESULT=$( $TEST_CLIENT -Y -c 'echo bob' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT ) || true if echo $RESULT | grep bob || ! log_since $MARK | grep -q "Only SFTP connections allowed"; then echo "Shell login should fail with internal-sftp and options" @@ -136,7 +136,7 @@ if echo $RESULT | grep bob || fi MARK=`log_mark` -if echo exit | $TEST_SFTP -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY \ +if echo exit | $TEST_SFTP -Y -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY \ -h $TEST_HOST -p $TEST_PORT; then echo "SFTP should fail with internal-sftp and options" exit 1 @@ -158,7 +158,7 @@ cd ../../.. # Refused as a forced command, not as internal-sftp with an option "d". MARK=`log_mark` -if echo exit | $TEST_SFTP -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY \ +if echo exit | $TEST_SFTP -Y -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY \ -h $TEST_HOST -p $TEST_PORT; then echo "SFTP should fail with a command named like internal-sftp" exit 1 @@ -171,7 +171,7 @@ fi # The shell runs it as the forced command, without the internal-sftp refusal. MARK=`log_mark` -$TEST_CLIENT -c 'echo bob' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY \ +$TEST_CLIENT -Y -c 'echo bob' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY \ -h $TEST_HOST -p $TEST_PORT > /dev/null 2>&1 || true if log_since $MARK | grep -q "Only SFTP connections allowed"; then echo "Shell refused internal-sftpd as internal-sftp" @@ -202,7 +202,7 @@ EOF start_wolfsshd "sshd_config_test_forcedcmd_sftp" cd ../../.. -if echo exit | $TEST_SFTP -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY \ +if echo exit | $TEST_SFTP -Y -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY \ -h $TEST_HOST -p $TEST_PORT; then echo "SFTP should fail with a forced command other than internal-sftp" exit 1 diff --git a/apps/wolfsshd/test/sshd_fork_fail_test.sh b/apps/wolfsshd/test/sshd_fork_fail_test.sh index 050295ee2..73b3303af 100755 --- a/apps/wolfsshd/test/sshd_fork_fail_test.sh +++ b/apps/wolfsshd/test/sshd_fork_fail_test.sh @@ -115,7 +115,7 @@ fi # Runs one exec session; output in $CLIENT_LOG, exit status in $CLIENT_RET. run_client() { CLIENT_LOG="$TESTDIR/fork_client_$1.log" - $TIMEOUT "$TEST_CLIENT" -q -c "echo $TOKEN" -u "$USER" -i "$PRIVATE_KEY" \ + $TIMEOUT "$TEST_CLIENT" -Y -q -c "echo $TOKEN" -u "$USER" -i "$PRIVATE_KEY" \ -j "$PUBLIC_KEY" -h "$TEST_HOST" -p "$TEST_PORT" \ > "$CLIENT_LOG" 2>&1 CLIENT_RET=$? diff --git a/apps/wolfsshd/test/sshd_large_sftp_test.sh b/apps/wolfsshd/test/sshd_large_sftp_test.sh index a87ce4776..3e2aeb427 100755 --- a/apps/wolfsshd/test/sshd_large_sftp_test.sh +++ b/apps/wolfsshd/test/sshd_large_sftp_test.sh @@ -55,8 +55,8 @@ trap 'rm -f "$LOCAL_FILE" "$REMOTE_FILE"' EXIT head -c 4400000010 < /dev/random > "$LOCAL_FILE" set -e -echo "$TEST_SFTP_CLIENT -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -g -l $LOCAL_FILE -r $REMOTE_FILE -h \"$1\" -p \"$2\"" -$TEST_SFTP_CLIENT -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -g -l "$LOCAL_FILE" -r "$REMOTE_FILE" -h "$1" -p "$2" +echo "$TEST_SFTP_CLIENT -Y -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -g -l $LOCAL_FILE -r $REMOTE_FILE -h \"$1\" -p \"$2\"" +$TEST_SFTP_CLIENT -Y -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -g -l "$LOCAL_FILE" -r "$REMOTE_FILE" -h "$1" -p "$2" cmp "$LOCAL_FILE" "$REMOTE_FILE" RESULT=$? diff --git a/apps/wolfsshd/test/sshd_login_grace_test.sh b/apps/wolfsshd/test/sshd_login_grace_test.sh index e93fdba99..898d06c2e 100755 --- a/apps/wolfsshd/test/sshd_login_grace_test.sh +++ b/apps/wolfsshd/test/sshd_login_grace_test.sh @@ -42,7 +42,7 @@ TEST_CLIENT="./examples/client/client" PRIVATE_KEY="./keys/hansel-key-ecc.der" PUBLIC_KEY="./keys/hansel-key-ecc.pub" -RESULT=`$TEST_CLIENT -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT -c 'sleep 6 && echo still connected && exit'` +RESULT=`$TEST_CLIENT -Y -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT -c 'sleep 6 && echo still connected && exit'` echo "$RESULT" | grep "still connected" RESULT=$? if [ "$RESULT" != 0 ]; then diff --git a/apps/wolfsshd/test/sshd_long_shell_test.sh b/apps/wolfsshd/test/sshd_long_shell_test.sh index 86b756442..869da7eff 100755 --- a/apps/wolfsshd/test/sshd_long_shell_test.sh +++ b/apps/wolfsshd/test/sshd_long_shell_test.sh @@ -91,7 +91,7 @@ fi # -q keeps a debug build's log, which echoes the command text, out of RESULT. # The expected line only exists once the shell has expanded $0. -RESULT=$(timeout 10 $TEST_CLIENT -q -c 'echo LONG SHELL RAN as $0' \ +RESULT=$(timeout 10 $TEST_CLIENT -Y -q -c 'echo LONG SHELL RAN as $0' \ -u "$LONG_USER" -i $PRIVATE_KEY -j $PUBLIC_KEY \ -h "$TEST_HOST" -p "$TEST_PORT" 2>&1) CLIENT_RESULT=$? diff --git a/apps/wolfsshd/test/sshd_match_overlap_test.sh b/apps/wolfsshd/test/sshd_match_overlap_test.sh index a5e09f85e..b4ad0cfa2 100755 --- a/apps/wolfsshd/test/sshd_match_overlap_test.sh +++ b/apps/wolfsshd/test/sshd_match_overlap_test.sh @@ -91,7 +91,7 @@ EOF check_forced_cmd() { start_wolfsshd "$1" - RESULT=$( $TEST_CLIENT -c 'echo overlap""_shell_ran' -u $USER \ + RESULT=$( $TEST_CLIENT -Y -c 'echo overlap""_shell_ran' -u $USER \ -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT ) echo $RESULT echo $RESULT | grep overlap_shell_ran diff --git a/apps/wolfsshd/test/sshd_mldsa_composite_test.sh b/apps/wolfsshd/test/sshd_mldsa_composite_test.sh index 5215f2f79..d43e706bc 100755 --- a/apps/wolfsshd/test/sshd_mldsa_composite_test.sh +++ b/apps/wolfsshd/test/sshd_mldsa_composite_test.sh @@ -34,21 +34,21 @@ skip() { echo "$1"; cd $PWD0; exit 77; } # -E lists the compiled-in algorithms before connecting, so point it at a closed # port to keep the probe from opening a session. -ALGOS=`$TEST_CLIENT -E -u $USER -h 127.0.0.1 -p 1 2>/dev/null` +ALGOS=`$TEST_CLIENT -Y -E -u $USER -h 127.0.0.1 -p 1 2>/dev/null` echo "$ALGOS" | grep -q "$ALGO" || skip "no $ALGO support in this build, skipping" # Positive: with only the composite offered, a successful login means it is what # was negotiated. set -e -echo "$TEST_CLIENT -k $ALGO -c 'ls' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \"$1\" -p \"$2\"" -$TEST_CLIENT -k "$ALGO" -c 'ls' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h "$1" -p "$2" +echo "$TEST_CLIENT -Y -k $ALGO -c 'ls' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \"$1\" -p \"$2\"" +$TEST_CLIENT -Y -k "$ALGO" -c 'ls' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h "$1" -p "$2" set +e # Negative control: offering only ECDSA must fail, proving the pass above was # not some other host key the daemon happened to have loaded. if echo "$ALGOS" | grep -q "$OTHER_ALGO"; then - echo "$TEST_CLIENT -k $OTHER_ALGO -c 'ls' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \"$1\" -p \"$2\"" - $TEST_CLIENT -k "$OTHER_ALGO" -c 'ls' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY \ + echo "$TEST_CLIENT -Y -k $OTHER_ALGO -c 'ls' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \"$1\" -p \"$2\"" + $TEST_CLIENT -Y -k "$OTHER_ALGO" -c 'ls' -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY \ -h "$1" -p "$2" if [ $? -eq 0 ]; then echo "expected $OTHER_ALGO to fail against the composite host key" diff --git a/apps/wolfsshd/test/sshd_ossh_cert_test.sh b/apps/wolfsshd/test/sshd_ossh_cert_test.sh index 43fc588de..27ae63fe3 100755 --- a/apps/wolfsshd/test/sshd_ossh_cert_test.sh +++ b/apps/wolfsshd/test/sshd_ossh_cert_test.sh @@ -280,7 +280,7 @@ timed_out() { # label rc # Connect with the wolfSSH example client: -i private key, -j certificate. connect_client() { # user-key cert remote-command - $TIMEOUT "$CLIENT" -u "$LOGINUSER" -i "$1" -j "$2" \ + $TIMEOUT "$CLIENT" -Y -u "$LOGINUSER" -i "$1" -j "$2" \ -h 127.0.0.1 -p $PORT -c "$3" >/dev/null 2>&1 } @@ -370,7 +370,7 @@ echo "scp payload" > "$SCPSRC" # command so a granted session exits cleanly with no transfer). The clients # report a non-zero exit code when the subsystem request is denied. sftp_client() { # user-key cert - echo "exit" | $TIMEOUT "$SFTP" -u "$LOGINUSER" -i "$1" -j "$2" \ + echo "exit" | $TIMEOUT "$SFTP" -Y -u "$LOGINUSER" -i "$1" -j "$2" \ -h 127.0.0.1 -p $PORT >/dev/null 2>&1 } sftp_ssh() { # user-key cert @@ -420,7 +420,7 @@ scp_check() { # label user-key cert expect(0=allowed,1=denied) [ -x "$SCP" ] || { echo " ($1: wolfscp unavailable, skipping)"; return; } start_daemon || { printf " %-20s %s\n" "$1" "*** FAIL (no running daemon)" FAIL=1; return; } - $TIMEOUT "$SCP" -u "$LOGINUSER" -i "$2" -j "$3" \ + $TIMEOUT "$SCP" -Y -u "$LOGINUSER" -i "$2" -j "$3" \ -S"$SCPSRC:$SCPDST" -H 127.0.0.1 -p $PORT >/dev/null 2>&1 if ! daemon_alive "$DPID"; then printf " %-20s %s\n" "$1" "*** FAIL (wolfsshd exited while serving)" diff --git a/apps/wolfsshd/test/sshd_permitroot_forced_cmd.sh b/apps/wolfsshd/test/sshd_permitroot_forced_cmd.sh index 02b997b5a..386e08342 100755 --- a/apps/wolfsshd/test/sshd_permitroot_forced_cmd.sh +++ b/apps/wolfsshd/test/sshd_permitroot_forced_cmd.sh @@ -52,7 +52,7 @@ if [ -z "$PID" ]; then exit 1 fi -timeout 10 $TEST_CLIENT -u "$USER" -P "somepassword" -c 'true' \ +timeout 10 $TEST_CLIENT -Y -u "$USER" -P "somepassword" -c 'true' \ -h "$TEST_HOST" -p "$TEST_PORT" > /dev/null 2>&1 PASS_RESULT=$? @@ -61,7 +61,7 @@ if [ "$PASS_RESULT" = 0 ]; then exit 1 fi -timeout 10 $TEST_CLIENT -u "$USER" -c 'true' \ +timeout 10 $TEST_CLIENT -Y -u "$USER" -c 'true' \ -i $PWD/../../../keys/hansel-key-ecc.der -j $PWD/../../../keys/hansel-key-ecc.pub \ -h "$TEST_HOST" -p "$TEST_PORT" > /dev/null 2>&1 NOCMD_PUBKEY_RESULT=$? @@ -114,7 +114,7 @@ if [ -z "$PID" ]; then exit 1 fi -timeout 10 $TEST_CLIENT -u "$USER" -c 'true' \ +timeout 10 $TEST_CLIENT -Y -u "$USER" -c 'true' \ -i $PWD/../../../keys/hansel-key-ecc.der -j $PWD/../../../keys/hansel-key-ecc.pub \ -h "$TEST_HOST" -p "$TEST_PORT" > /dev/null 2>&1 WITHCMD_PUBKEY_RESULT=$? diff --git a/apps/wolfsshd/test/sshd_permitroot_prohibit_password.sh b/apps/wolfsshd/test/sshd_permitroot_prohibit_password.sh index edd07f8f7..605655ddc 100755 --- a/apps/wolfsshd/test/sshd_permitroot_prohibit_password.sh +++ b/apps/wolfsshd/test/sshd_permitroot_prohibit_password.sh @@ -53,7 +53,7 @@ fi TEST_CLIENT="../../../examples/client/client" # Attempt password login as root, which should be rejected -timeout 10 $TEST_CLIENT -u "$USER" -P "somepassword" -c 'true' \ +timeout 10 $TEST_CLIENT -Y -u "$USER" -P "somepassword" -c 'true' \ -h "$TEST_HOST" -p "$TEST_PORT" > /dev/null 2>&1 PASS_RESULT=$? @@ -64,7 +64,7 @@ fi # Attempt public-key login as root, which should still be permitted under # prohibit-password (only password auth is restricted for root). -timeout 10 $TEST_CLIENT -u "$USER" -c 'true' \ +timeout 10 $TEST_CLIENT -Y -u "$USER" -c 'true' \ -i $PWD/../../../keys/hansel-key-ecc.der -j $PWD/../../../keys/hansel-key-ecc.pub \ -h "$TEST_HOST" -p "$TEST_PORT" > /dev/null 2>&1 PUBKEY_RESULT=$? diff --git a/apps/wolfsshd/test/sshd_permitroot_test.sh b/apps/wolfsshd/test/sshd_permitroot_test.sh index 11ce55bb8..d31ff5734 100755 --- a/apps/wolfsshd/test/sshd_permitroot_test.sh +++ b/apps/wolfsshd/test/sshd_permitroot_test.sh @@ -65,7 +65,7 @@ attempt_login() { echo "FAIL: could not start wolfsshd" exit 1 fi - timeout 10 $TEST_CLIENT -u "$2" -P "wolfsshd-dummy-pw" -c 'true' \ + timeout 10 $TEST_CLIENT -Y -u "$2" -P "wolfsshd-dummy-pw" -c 'true' \ -h "$TEST_HOST" -p "$TEST_PORT" > /dev/null 2>&1 # Let wolfsshd flush the log before we stop it. sleep 1 diff --git a/apps/wolfsshd/test/sshd_privdrop_fail_test.sh b/apps/wolfsshd/test/sshd_privdrop_fail_test.sh index 96f3082e5..9b4f2511d 100755 --- a/apps/wolfsshd/test/sshd_privdrop_fail_test.sh +++ b/apps/wolfsshd/test/sshd_privdrop_fail_test.sh @@ -191,18 +191,18 @@ check_subsystem() { # SHELL_Subsystem, via an exec session. check_subsystem "exec" \ - "$TEST_CLIENT" -c 'echo privdrop' -u "$USER" -i "$PRIVATE_KEY" \ + "$TEST_CLIENT" -Y -c 'echo privdrop' -u "$USER" -i "$PRIVATE_KEY" \ -j "$PUBLIC_KEY" -h "$TEST_HOST" -p "$TEST_PORT" # SFTP_Subsystem. -g is a one-shot put, so the client cannot sit at a prompt. check_subsystem "sftp" \ - "$SFTP_CLIENT" -u "$USER" -i "$PRIVATE_KEY" -j "$PUBLIC_KEY" \ + "$SFTP_CLIENT" -Y -u "$USER" -i "$PRIVATE_KEY" -j "$PUBLIC_KEY" \ -g -l "$PAYLOAD" -r "/tmp/privdrop_remote_$$.txt" \ -h "$TEST_HOST" -p "$TEST_PORT" # SCP_Subsystem. check_subsystem "scp" \ - "$SCP_CLIENT" -u "$USER" -i "$PRIVATE_KEY" -j "$PUBLIC_KEY" \ + "$SCP_CLIENT" -Y -u "$USER" -i "$PRIVATE_KEY" -j "$PUBLIC_KEY" \ -S"$PAYLOAD:." -H "$TEST_HOST" -p "$TEST_PORT" rm -f "$TESTDIR"/privdrop_client_*.log diff --git a/apps/wolfsshd/test/sshd_pubkey_reject_test.sh b/apps/wolfsshd/test/sshd_pubkey_reject_test.sh index 44005719d..5081af9ed 100755 --- a/apps/wolfsshd/test/sshd_pubkey_reject_test.sh +++ b/apps/wolfsshd/test/sshd_pubkey_reject_test.sh @@ -62,7 +62,7 @@ cd ../../.. # Positive control: the authorized key must work against this daemon, so the # failure below can be attributed to the key not being authorized rather than # to an unrelated connection or client problem. -timeout 10 $TEST_CLIENT -c 'exit' -u "$TEST_USER" \ +timeout 10 $TEST_CLIENT -Y -c 'exit' -u "$TEST_USER" \ -i "$GOOD_PRIVATE_KEY" -j "$GOOD_PUBLIC_KEY" \ -h "$TEST_HOST" -p "$TEST_PORT" > /dev/null 2>&1 GOOD_RESULT=$? @@ -73,9 +73,9 @@ if [ "$GOOD_RESULT" != 0 ]; then exit 1 fi -echo "$TEST_CLIENT -c 'exit' -u $TEST_USER -i $BAD_PRIVATE_KEY" \ +echo "$TEST_CLIENT -Y -c 'exit' -u $TEST_USER -i $BAD_PRIVATE_KEY" \ "-j $BAD_PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT (expecting rejection)" -timeout 10 $TEST_CLIENT -c 'exit' -u "$TEST_USER" \ +timeout 10 $TEST_CLIENT -Y -c 'exit' -u "$TEST_USER" \ -i "$BAD_PRIVATE_KEY" -j "$BAD_PUBLIC_KEY" \ -h "$TEST_HOST" -p "$TEST_PORT" > /dev/null 2>&1 BAD_RESULT=$? diff --git a/apps/wolfsshd/test/sshd_scp_fail.sh b/apps/wolfsshd/test/sshd_scp_fail.sh index 04382faa6..1efa80bd6 100755 --- a/apps/wolfsshd/test/sshd_scp_fail.sh +++ b/apps/wolfsshd/test/sshd_scp_fail.sh @@ -30,8 +30,8 @@ OUTDIR="`pwd`/test-$$" dd if=/dev/random of=$OUTDIR/test.dat bs=1024 count=512 -echo "$TEST_SCP_CLIENT -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -S$OUTDIR/test.dat:. -H $1 -p $2" -$TEST_SCP_CLIENT -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -S$OUTDIR/test.dat:. -H $1 -p $2 +echo "$TEST_SCP_CLIENT -Y -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -S$OUTDIR/test.dat:. -H $1 -p $2" +$TEST_SCP_CLIENT -Y -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -S$OUTDIR/test.dat:. -H $1 -p $2 RESULT=$? if [ "$RESULT" != "0" ]; then diff --git a/apps/wolfsshd/test/sshd_scp_home_test.sh b/apps/wolfsshd/test/sshd_scp_home_test.sh index 15e16eb0b..4c2fbc2f4 100755 --- a/apps/wolfsshd/test/sshd_scp_home_test.sh +++ b/apps/wolfsshd/test/sshd_scp_home_test.sh @@ -48,8 +48,8 @@ if ! dd if=/dev/urandom of="$OUTDIR/$NAME" bs=1024 count=64 2>/dev/null; then exit 1 fi -echo "$TEST_SCP_CLIENT -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -L$OUTDIR/$NAME:. -H $1 -p $2" -$TEST_SCP_CLIENT -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -L"$OUTDIR/$NAME":. \ +echo "$TEST_SCP_CLIENT -Y -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -L$OUTDIR/$NAME:. -H $1 -p $2" +$TEST_SCP_CLIENT -Y -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -L"$OUTDIR/$NAME":. \ -H $1 -p $2 if [ "$?" != "0" ]; then echo "Expecting to pass transfer" diff --git a/apps/wolfsshd/test/sshd_sftp_idle_cpu_test.sh b/apps/wolfsshd/test/sshd_sftp_idle_cpu_test.sh index 8161a5f35..6eea9130e 100755 --- a/apps/wolfsshd/test/sshd_sftp_idle_cpu_test.sh +++ b/apps/wolfsshd/test/sshd_sftp_idle_cpu_test.sh @@ -77,7 +77,7 @@ new_wolfsshd_pid() { mkfifo "$FIFO" || exit 1 sleep $((SAMPLE_SECONDS + 30)) > "$FIFO" & HOLDER=$! -"$TEST_SFTP_CLIENT" -u "$3" -i "$PRIVATE_KEY" -j "$PUBLIC_KEY" \ +"$TEST_SFTP_CLIENT" -Y -u "$3" -i "$PRIVATE_KEY" -j "$PUBLIC_KEY" \ -h "$1" -p "$2" < "$FIFO" > /dev/null 2>&1 & CLIENT=$! diff --git a/apps/wolfsshd/test/sshd_stderr_drain_test.sh b/apps/wolfsshd/test/sshd_stderr_drain_test.sh index e09970ad4..e4cf5769f 100755 --- a/apps/wolfsshd/test/sshd_stderr_drain_test.sh +++ b/apps/wolfsshd/test/sshd_stderr_drain_test.sh @@ -15,7 +15,7 @@ # A pty session has no stderr pipe; the last check covers its exit. # # The client's own "Error reading stdin" lands in the same capture, possibly -# mid-line, so it is cut out before the compare. +# mid-line, so it is cut out before the compare, as is the -Y warning. if [ -z "$1" ] || [ -z "$2" ]; then echo "expecting host and port as arguments" @@ -123,7 +123,7 @@ RESULT=0 for i in `seq 1 $TEST_ITERS`; do # The child exits with its output still queued and nothing slows the # peer, so the loop has to relay the rest on its own. - $TIMEOUT $TEST_CLIENT -q -c "perl $TEST_CHILD $TEST_FILE" \ + $TIMEOUT $TEST_CLIENT -Y -q -c "perl $TEST_CHILD $TEST_FILE" \ -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT \ < /dev/null > /dev/null 2> "$TEST_RESULT_FILE" CLIENT_RESULT=$? @@ -144,8 +144,9 @@ for i in `seq 1 $TEST_ITERS`; do break fi - perl -0777 -pe 's/Error reading stdin\n//g' "$TEST_RESULT_FILE" \ - > "$TEST_STRIPPED_FILE" + perl -0777 -pe 's/Error reading stdin\n//g; + s/^Warning: accepting the \S+ host key for .*\n//mg' \ + "$TEST_RESULT_FILE" > "$TEST_STRIPPED_FILE" if ! cmp -s "$TEST_STRIPPED_FILE" "$TEST_FILE"; then GOT=`grep -c "^$TEST_LINE\$" "$TEST_STRIPPED_FILE"` echo "pass $i of $TEST_ITERS lost or altered the child's stderr" @@ -160,7 +161,7 @@ done # exit never ends the session. The exit status proves the session ended and # carried the shell's own code back. if [ "$RESULT" = 0 ]; then - printf 'exit 3\n' | $PTY_TIMEOUT $TEST_CLIENT -t \ + printf 'exit 3\n' | $PTY_TIMEOUT $TEST_CLIENT -Y -t \ -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT \ > /dev/null 2>&1 CLIENT_RESULT=$? diff --git a/apps/wolfsshd/test/sshd_stderr_eof_test.sh b/apps/wolfsshd/test/sshd_stderr_eof_test.sh index 00c0ca7b6..1bc01084a 100755 --- a/apps/wolfsshd/test/sshd_stderr_eof_test.sh +++ b/apps/wolfsshd/test/sshd_stderr_eof_test.sh @@ -103,7 +103,7 @@ for i in `seq 1 $TEST_ITERS`; do # The inner client cats the file through the outer session, so the shell # loop is the one relaying it. Stalling the outer client's reader fills # the window and leaves the loop holding a backlog. - $TIMEOUT $TEST_CLIENT -q -c "cd $PWD; $TEST_CLIENT -q -c \"cat $TEST_FILE\" -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT" \ + $TIMEOUT $TEST_CLIENT -Y -q -c "cd $PWD; $TEST_CLIENT -Y -q -c \"cat $TEST_FILE\" -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT" \ -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT 2>/dev/null \ | { sleep $TEST_STALL; cat; } > "$TEST_RESULT_FILE" # The client's own status, not the reader's. 124 is the deadline above, diff --git a/apps/wolfsshd/test/sshd_term_close_test.sh b/apps/wolfsshd/test/sshd_term_close_test.sh index 65e4a0c5f..e024a47b0 100755 --- a/apps/wolfsshd/test/sshd_term_close_test.sh +++ b/apps/wolfsshd/test/sshd_term_close_test.sh @@ -31,7 +31,7 @@ new_wolfsshd_pid() { return 1 } -timeout 3 $TEST_CLIENT -p $2 -i $PRIVATE_KEY -j $PUBLIC_KEY -h $1 -c '/bin/sleep 10' -u $3 & +timeout 3 $TEST_CLIENT -Y -p $2 -i $PRIVATE_KEY -j $PUBLIC_KEY -h $1 -c '/bin/sleep 10' -u $3 & CLIENT_PID=$! # Poll for the child rather than sampling a fixed second in. NewConnection() diff --git a/apps/wolfsshd/test/sshd_term_size_test.sh b/apps/wolfsshd/test/sshd_term_size_test.sh index 806766880..0fcabfe2d 100755 --- a/apps/wolfsshd/test/sshd_term_size_test.sh +++ b/apps/wolfsshd/test/sshd_term_size_test.sh @@ -71,8 +71,8 @@ get_size_line() { } echo "Creating tmux session at $PWD with command :" -echo "tmux new-session -d -s $TMUX_SESSION \"$TEST_CLIENT -q -t -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \"$1\" -p \"$2\"\"" -tmux new-session -d -s "$TMUX_SESSION" "$TEST_CLIENT -q -t -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \"$1\" -p \"$2\"" +echo "tmux new-session -d -s $TMUX_SESSION \"$TEST_CLIENT -Y -q -t -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \"$1\" -p \"$2\"\"" +tmux new-session -d -s "$TMUX_SESSION" "$TEST_CLIENT -Y -q -t -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \"$1\" -p \"$2\"" echo "Result of tmux new-session = $?" wait_for_session || exit 1 @@ -112,8 +112,8 @@ tmux kill-session -t "$TMUX_SESSION" set -e echo "Starting another session with a smaller window size" -echo "tmux new-session -d -x 50 -y 10 -s $TMUX_SESSION \"$TEST_CLIENT -q -t -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \"$1\" -p \"$2\"\"" -tmux new-session -d -x 50 -y 10 -s "$TMUX_SESSION" "$TEST_CLIENT -q -t -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \"$1\" -p \"$2\"" +echo "tmux new-session -d -x 50 -y 10 -s $TMUX_SESSION \"$TEST_CLIENT -Y -q -t -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \"$1\" -p \"$2\"\"" +tmux new-session -d -x 50 -y 10 -s "$TMUX_SESSION" "$TEST_CLIENT -Y -q -t -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h \"$1\" -p \"$2\"" wait_for_session || exit 1 diff --git a/apps/wolfsshd/test/sshd_window_full_test.sh b/apps/wolfsshd/test/sshd_window_full_test.sh index dd915d7a6..3105f8bb9 100755 --- a/apps/wolfsshd/test/sshd_window_full_test.sh +++ b/apps/wolfsshd/test/sshd_window_full_test.sh @@ -42,9 +42,9 @@ PUBLIC_KEY="./keys/hansel-key-ecc.pub" head -c 1G /dev/urandom > random-test.txt PWD=`pwd` -echo "$TEST_CLIENT -q -c \"cd $PWD; $TEST_CLIENT -q -c \"cat $PWD/random-test.txt\" -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT\" -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT > random-test-result.txt" +echo "$TEST_CLIENT -Y -q -c \"cd $PWD; $TEST_CLIENT -Y -q -c \"cat $PWD/random-test.txt\" -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT\" -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT > random-test-result.txt" -$TEST_CLIENT -q -c "cd $PWD; $TEST_CLIENT -q -c \"cat $PWD/random-test.txt\" -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT" -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT > random-test-result.txt +$TEST_CLIENT -Y -q -c "cd $PWD; $TEST_CLIENT -Y -q -c \"cat $PWD/random-test.txt\" -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT" -u $USER -i $PRIVATE_KEY -j $PUBLIC_KEY -h $TEST_HOST -p $TEST_PORT > random-test-result.txt ls -la random-test* diff --git a/apps/wolfsshd/test/sshd_x509_test.sh b/apps/wolfsshd/test/sshd_x509_test.sh index c54863532..40d8a70f2 100755 --- a/apps/wolfsshd/test/sshd_x509_test.sh +++ b/apps/wolfsshd/test/sshd_x509_test.sh @@ -28,16 +28,16 @@ PUBLIC_KEY="./keys/$3-cert.der" CA_CERT="./keys/ca-cert-ecc.der" set -e -echo "$TEST_CLIENT -X -c 'pwd' -u $3 -i $PRIVATE_KEY -J $PUBLIC_KEY -A $CA_CERT -h \"$1\" -p \"$2\"" -$TEST_CLIENT -X -c 'pwd' -u $3 -i "$PRIVATE_KEY" -J "$PUBLIC_KEY" -A "$CA_CERT" -h "$1" -p "$2" +echo "$TEST_CLIENT -Y -X -c 'pwd' -u $3 -i $PRIVATE_KEY -J $PUBLIC_KEY -A $CA_CERT -h \"$1\" -p \"$2\"" +$TEST_CLIENT -Y -X -c 'pwd' -u $3 -i "$PRIVATE_KEY" -J "$PUBLIC_KEY" -A "$CA_CERT" -h "$1" -p "$2" set +e rm -f error.txt -echo "$TEST_CLIENT -X -c 'ls error' -u $3 -i $PRIVATE_KEY -J $PUBLIC_KEY -A $CA_CERT -h \"$1\" -p \"$2\" 2> error.txt" -$TEST_CLIENT -X -c 'ls error' -u $3 -i "$PRIVATE_KEY" -J "$PUBLIC_KEY" -A "$CA_CERT" -h "$1" -p "$2" 2> error.txt +echo "$TEST_CLIENT -Y -X -c 'ls error' -u $3 -i $PRIVATE_KEY -J $PUBLIC_KEY -A $CA_CERT -h \"$1\" -p \"$2\" 2> error.txt" +$TEST_CLIENT -Y -X -c 'ls error' -u $3 -i "$PRIVATE_KEY" -J "$PUBLIC_KEY" -A "$CA_CERT" -h "$1" -p "$2" 2> error.txt -# check stderr output was caught -if [ ! -s error.txt ]; then +# check the remote ls's stderr was caught, not just the -Y warning +if ! grep -q "ls: .*error" error.txt; then echo "No stderr data was found when expected!!" cd "$TESTDIR" exit 1 diff --git a/apps/wolfsshd/test/sshd_x509_upn_fail.sh b/apps/wolfsshd/test/sshd_x509_upn_fail.sh index bd89db07c..21f869313 100755 --- a/apps/wolfsshd/test/sshd_x509_upn_fail.sh +++ b/apps/wolfsshd/test/sshd_x509_upn_fail.sh @@ -43,8 +43,8 @@ PRIVATE_KEY="./keys/$3-key.der" PUBLIC_KEY="./keys/$3-cert.der" CA_CERT="./keys/ca-cert-ecc.der" -echo "$TEST_CLIENT -X -c 'pwd' -u $3 -i $PRIVATE_KEY -J $PUBLIC_KEY -A $CA_CERT -h \"$1\" -p \"$2\" (expecting rejection)" -$TEST_CLIENT -X -c 'pwd' -u "$3" -i "$PRIVATE_KEY" -J "$PUBLIC_KEY" -A "$CA_CERT" -h "$1" -p "$2" +echo "$TEST_CLIENT -Y -X -c 'pwd' -u $3 -i $PRIVATE_KEY -J $PUBLIC_KEY -A $CA_CERT -h \"$1\" -p \"$2\" (expecting rejection)" +$TEST_CLIENT -Y -X -c 'pwd' -u "$3" -i "$PRIVATE_KEY" -J "$PUBLIC_KEY" -A "$CA_CERT" -h "$1" -p "$2" RESULT=$? # Back to the test dir: the log counted below is the one here, so a failed cd diff --git a/examples/client/client.c b/examples/client/client.c index b7e35519d..b8346d377 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -124,8 +124,11 @@ static void ShowUsage(void) printf(" Certificate example : client -u orange \\\n"); printf(" -J orange-cert.der -i orange-key.der\n"); printf(" -A filename for DER CA certificate to verify host\n"); - printf(" -X Ignore IP checks on peer vs peer certificate\n"); + printf(" -X accept a host certificate for another host\n"); #endif + printf(" -Y accept any host key, insecure; otherwise it must " + "be in\n" + " ~/.ssh/known_hosts\n"); printf(" -E List all possible algos\n"); printf(" -k set the list of key algos\n"); printf(" -C set the list of encrypt algos\n"); @@ -858,6 +861,7 @@ THREAD_RETURN WOLFSSH_THREAD client_test(void* args) int userEcc = 0; word16 port = wolfSshPort; char* host = (char*)wolfSshIp; + ClientHostCtx hostCtx; const char* username = NULL; const char* password = NULL; const char* tpmKeyAuth = NULL; @@ -883,7 +887,8 @@ THREAD_RETURN WOLFSSH_THREAD client_test(void* args) (void)keepOpen; - while ((ch = mygetopt(argc, argv, "?ac:C:h:i:j:p:tu:xzNP:RJ:A:XeEk:qK:")) != -1) { + ClientAcceptAnyHostKey(0); + while ((ch = mygetopt(argc, argv, "?ac:C:h:i:j:p:tu:xzNP:RJ:A:XYeEk:qK:")) != -1) { switch (ch) { case 'h': host = myoptarg; @@ -938,13 +943,15 @@ THREAD_RETURN WOLFSSH_THREAD client_test(void* args) caCert = myoptarg; break; - #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME) case 'X': ClientIPOverride(1); break; - #endif #endif + case 'Y': + ClientAcceptAnyHostKey(1); + break; + case 'E': listAlgos = 1; break; @@ -1118,7 +1125,9 @@ THREAD_RETURN WOLFSSH_THREAD client_test(void* args) wolfSSH_set_agent_cb_ctx(ssh, &agentCbCtx); } #endif - wolfSSH_SetPublicKeyCheckCtx(ssh, (void*)host); + hostCtx.host = host; + hostCtx.port = port; + wolfSSH_SetPublicKeyCheckCtx(ssh, &hostCtx); ret = wolfSSH_SetUsername(ssh, username); if (ret != WS_SUCCESS) diff --git a/examples/client/common.c b/examples/client/common.c index ae2f4974b..c7e45bdf1 100644 --- a/examples/client/common.c +++ b/examples/client/common.c @@ -26,6 +26,7 @@ #define WOLFSSH_TEST_CLIENT #include +#include #include #include #include @@ -46,8 +47,15 @@ #include #endif +#include +#include #ifdef WOLFSSH_CERTS #include + #ifdef USE_WINDOWS_API + #include + #else + #include + #endif #endif static byte userPublicKeyBuf[512]; @@ -293,15 +301,46 @@ static const unsigned int hanselPrivateEd25519Sz = #endif -#if defined(WOLFSSH_CERTS) +/* Parse a port number. Text that is not wholly a number in 1..65535 is + * rejected with WS_BAD_ARGUMENT rather than truncated, and *port is left + * untouched. */ +int ClientParsePort(const char* in, word16* port) +{ + char* endptr = NULL; + long portVal; + + if (in == NULL || port == NULL) { + return WS_BAD_ARGUMENT; + } + + portVal = strtol(in, &endptr, 10); + if (endptr == in || *endptr != '\0' || portVal < 1 || portVal > 65535) { + return WS_BAD_ARGUMENT; + } + *port = (word16)portVal; + + return WS_SUCCESS; +} + -#if (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)) static inline void ato32(const byte* c, word32* u32) { - *u32 = (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]; + *u32 = ((word32)c[0] << 24) | ((word32)c[1] << 16) | + ((word32)c[2] << 8) | c[3]; +} + +/* when set, accept a host key without checking known_hosts */ +static int acceptAnyHostKey = 0; + +void ClientAcceptAnyHostKey(int flag) +{ + acceptAnyHostKey = flag; } -/* when set as true then ignore miss matching IP addresses */ + +#if defined(WOLFSSH_CERTS) + +/* when set as true then ignore a host certificate that names another host */ static int IPOverride = 0; static int ParseRFC6187(const byte* in, word32 inSz, byte** leafOut, @@ -311,7 +350,7 @@ static int ParseRFC6187(const byte* in, word32 inSz, byte** leafOut, word32 l = 0, m = 0; if (inSz < sizeof(word32)) { - printf("inSz %d too small for holding cert name\n", inSz); + fprintf(stderr, "inSz %d too small for holding cert name\n", inSz); return WS_BUFFER_E; } @@ -361,108 +400,464 @@ void ClientIPOverride(int flag) { IPOverride = flag; } -#endif /* OPENSSL_ALL || WOLFSSL_IP_ALT_NAME */ + + +/* Returns 1 when name, len bytes long, is host, ignoring case. */ +static int CertNameMatch(const char* name, int len, const char* host) +{ + return name != NULL && len > 0 && (word32)len == WSTRLEN(host) + && WSTRNCASECMP(name, host, len) == 0; +} + + +/* Returns 1 when the IP SAN name, len raw address bytes, is host. */ +static int CertIpMatch(const char* name, int len, const char* host) +{ + byte addr[16]; + int family = (len == 4) ? AF_INET : AF_INET6; + + return name != NULL && (len == 4 || len == 16) + && inet_pton(family, host, addr) == 1 + && WMEMCMP(name, addr, len) == 0; +} + + +/* Checks the leaf names host: a DNS or IP SAN, or the CN when it has no + * SAN. The library checks the chain. */ +static int ClientCertHostCheck(const byte* pubKey, word32 pubKeySz, + const char* host) +{ + DecodedCert dCert; + DNS_entry* current; + byte* der = NULL; + word32 derSz = 0; + int ret = 0, match = 0; + + if (ParseRFC6187(pubKey, pubKeySz, &der, &derSz) != WS_SUCCESS) { + fprintf(stderr, "Couldn't parse the host certificate.\n"); + return -1; + } + + wc_InitDecodedCert(&dCert, der, derSz, NULL); + if (wc_ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL) != 0) { + fprintf(stderr, "Couldn't parse the host certificate.\n"); + ret = -1; + } + + if (ret == 0) { + for (current = dCert.altNames; current != NULL && !match; + current = current->next) { + if (current->type == ASN_DNS_TYPE) { + match = CertNameMatch(current->name, current->len, host); + } + else if (current->type == ASN_IP_TYPE) { + match = CertIpMatch(current->name, current->len, host); + } + } + + if (!dCert.extSubjAltNameSet) { + match = CertNameMatch(dCert.subjectCN, dCert.subjectCNLen, host); + } + + if (!match) { + fprintf(stderr, "The host certificate does not name %s", host); + if (!IPOverride) { + fprintf(stderr, "\n"); + ret = -1; + } + else { + fprintf(stderr, "..overriding\n"); + } + } + } + wc_FreeDecodedCert(&dCert); + + return ret; +} #endif /* WOLFSSH_CERTS */ -/* Parse a port number. Text that is not wholly a number in 1..65535 is - * rejected with WS_BAD_ARGUMENT rather than truncated, and *port is left - * untouched. */ -int ClientParsePort(const char* in, word16* port) +#define WOLFSSH_CLIENT_PUBKEYTYPE_SIZE_ESTIMATE 54 +#define WOLFSSH_CLIENT_FINGERPRINT_SIZE_ESTIMATE 56 + +#if !defined(NO_FILESYSTEM) && defined(WOLFSSL_BASE64_ENCODE) +/* Reads filename into a nul terminated buffer the caller frees. Returns 0 + * on success. */ +static int LoadTextFile(const char* filename, char** out) { - char* endptr = NULL; - long portVal; + WFILE* file; + char* in; + long inSz; + int ret; - if (in == NULL || port == NULL) { - return WS_BAD_ARGUMENT; + ret = WFOPEN(NULL, &file, filename, "rb"); + if (ret != 0 || file == WBADFILE) + return -1; + + if (!WFSEEK_SUCCESS(WFSEEK(NULL, file, 0, WSEEK_END))) { + WFCLOSE(NULL, file); + return -1; } + inSz = WFTELL(NULL, file); + WREWIND(NULL, file); - portVal = strtol(in, &endptr, 10); - if (endptr == in || *endptr != '\0' || portVal < 1 || portVal > 65535) { - return WS_BAD_ARGUMENT; + if (inSz <= 0) { + WFCLOSE(NULL, file); + return -1; } - *port = (word16)portVal; - return WS_SUCCESS; + in = (char*)WMALLOC(inSz + 1, NULL, 0); + if (in == NULL) { + WFCLOSE(NULL, file); + return -1; + } + + ret = (int)WFREAD(NULL, in, 1, inSz, file); + WFCLOSE(NULL, file); + + if (ret <= 0 || (long)ret != inSz) { + WFREE(in, NULL, 0); + return -1; + } + + in[inSz] = '\0'; + *out = in; + + return 0; } +/* Writes the SHA256 fingerprint of the key blob to out, which must hold + * WOLFSSH_CLIENT_FINGERPRINT_SIZE_ESTIMATE bytes. */ +static int FingerprintKey(const byte* pubKey, word32 pubKeySz, char* out) +{ +#ifdef NO_SHA256 + (void)pubKey; + (void)pubKeySz; + WMEMCPY(out, "unavailable", sizeof("unavailable")); + return 0; +#else + wc_Sha256 sha; + byte digest[WC_SHA256_DIGEST_SIZE]; + word32 fpSz = WOLFSSH_CLIENT_FINGERPRINT_SIZE_ESTIMATE - 8; + int ret; + + ret = wc_InitSha256(&sha); + if (ret == 0) { + ret = wc_Sha256Update(&sha, pubKey, pubKeySz); + if (ret == 0) + ret = wc_Sha256Final(&sha, digest); + wc_Sha256Free(&sha); + } + + if (ret == 0) { + WMEMCPY(out, "SHA256:", 7); + ret = Base64_Encode_NoNl(digest, sizeof(digest), (byte*)out + 7, + &fpSz); + } + + if (ret == 0) { + /* Like OpenSSH, drop the padding. */ + while (fpSz > 0 && out[7 + fpSz - 1] == '=') + fpSz--; + out[7 + fpSz] = '\0'; + } + + return ret; +#endif /* NO_SHA256 */ +} + + +/* strsep() for one delimiter; not every C library has strsep(). */ +static char* NextField(char** s, char delim) +{ + char* start = *s; + char* p; + + if (start != NULL) { + p = WSTRCHR(start, delim); + if (p != NULL) + *p++ = '\0'; + *s = p; + } + + return start; +} + + +#if !defined(NO_HMAC) && !defined(NO_SHA) +/* Returns 1 when a hashed name's "salt|hash" is HMAC-SHA1(salt, host). */ +static int KnownHostsHashMatch(char* saltHash, const char* host) +{ + Hmac hmac; + byte salt[WC_SHA_DIGEST_SIZE]; + byte hash[WC_SHA_DIGEST_SIZE]; + byte digest[WC_SHA_DIGEST_SIZE]; + word32 saltSz = (word32)sizeof(salt); + word32 hashSz = (word32)sizeof(hash); + char* saltStr; + char* hashStr; + int ret; + + saltStr = NextField(&saltHash, '|'); + hashStr = saltHash; + if (saltStr == NULL || hashStr == NULL + || Base64_Decode((byte*)saltStr, (word32)WSTRLEN(saltStr), + salt, &saltSz) != 0 + || Base64_Decode((byte*)hashStr, (word32)WSTRLEN(hashStr), + hash, &hashSz) != 0 + || saltSz != sizeof(salt) || hashSz != sizeof(hash)) { + return 0; + } + + ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID); + if (ret == 0) { + ret = wc_HmacSetKey(&hmac, WC_SHA, salt, saltSz); + if (ret == 0) + ret = wc_HmacUpdate(&hmac, (const byte*)host, + (word32)WSTRLEN(host)); + if (ret == 0) + ret = wc_HmacFinal(&hmac, digest); + wc_HmacFree(&hmac); + } + + return ret == 0 && WMEMCMP(digest, hash, sizeof(hash)) == 0; +} +#endif + + +/* Returns 1 when a known_hosts name field, a comma list of names or a + * hashed "|1|salt|hash" name, holds host. */ +static int KnownHostsNameMatch(char* names, const char* host) +{ + char* entry; + + while ((entry = NextField(&names, ',')) != NULL) { + if (WSTRNCMP(entry, "|1|", 3) == 0) { + #if !defined(NO_HMAC) && !defined(NO_SHA) + if (KnownHostsHashMatch(entry + 3, host)) + return 1; + #endif + } + else if (WSTRCMP(entry, host) == 0) { + return 1; + } + } + + return 0; +} + + +/* Returns 0 when a known_hosts line holds the key, 1 when the host has + * only other keys of this type, else -1. */ +static int KnownHostsLookup(const char* host, const char* keyType, + const char* key, char** fileName) +{ + const char* defaultName = "/.ssh/known_hosts"; + char* env; + char* knownHosts = NULL; + char* cursor; + char* line; + char* name; + char* type; + char* value; + word32 sz; + int ret = -1; + + *fileName = NULL; +#ifdef _MSC_VER + { + size_t envSz; + + /* MSVC deprecates getenv() for _dupenv_s(), which allocates. */ + if (_dupenv_s(&env, &envSz, "HOME") != 0 || env == NULL) { + env = NULL; + if (_dupenv_s(&env, &envSz, "USERPROFILE") != 0) + env = NULL; + } + } +#elif defined(WOLFSSH_ZEPHYR) + env = NULL; /* no getenv() in the minimal libc */ +#else + env = getenv("HOME"); + #ifdef USE_WINDOWS_API + if (env == NULL) + env = getenv("USERPROFILE"); + #endif +#endif + if (env != NULL) { + sz = (word32)(WSTRLEN(env) + WSTRLEN(defaultName) + 1); + *fileName = (char*)WMALLOC(sz, NULL, 0); + if (*fileName != NULL) { + WSNPRINTF(*fileName, sz, "%s%s", env, defaultName); + if (LoadTextFile(*fileName, &knownHosts) != 0) + knownHosts = NULL; + } + #ifdef _MSC_VER + free(env); + #endif + } + + cursor = knownHosts; + while (cursor != NULL && ret != 0) { + line = NextField(&cursor, '\n'); + if (line == NULL || *line == '\0') + continue; + sz = (word32)WSTRLEN(line); + if (line[sz - 1] == '\r') + line[sz - 1] = '\0'; + + name = NextField(&line, ' '); + type = NextField(&line, ' '); + value = NextField(&line, ' '); + if (name != NULL && type != NULL && value != NULL + && WSTRCMP(type, keyType) == 0 + && KnownHostsNameMatch(name, host)) { + /* Keep looking past a stale line: a later one may hold the key. */ + ret = (WSTRCMP(value, key) == 0) ? 0 : 1; + } + } + + if (knownHosts != NULL) + WFREE(knownHosts, NULL, 0); + + return ret; +} +#endif /* !NO_FILESYSTEM && WOLFSSL_BASE64_ENCODE */ + + int ClientPublicKeyCheck(const byte* pubKey, word32 pubKeySz, void* ctx) { + const ClientHostCtx* hostCtx = (const ClientHostCtx*)ctx; + const char* host = (hostCtx != NULL) ? hostCtx->host : NULL; + char keyType[WOLFSSH_CLIENT_PUBKEYTYPE_SIZE_ESTIMATE]; + word32 sz = 0, i; int ret = 0; - #ifdef DEBUG_WOLFSSH - printf("Sample public key check callback\n" - " public key = %p\n" - " public key size = %u\n" - " ctx = %s\n", pubKey, pubKeySz, (const char*)ctx); - #else - (void)pubKey; - (void)pubKeySz; - (void)ctx; - #endif + if (pubKey == NULL || host == NULL || pubKeySz < sizeof(word32)) { + ret = -1; + } + + /* The server's key type is printed, so require printable ASCII. */ + if (ret == 0) { + ato32(pubKey, &sz); + if (sz == 0 || sz > pubKeySz - sizeof(word32) + || sz > sizeof(keyType) - 1) { + ret = -1; + } + } + if (ret == 0) { + WMEMCPY(keyType, pubKey + sizeof(word32), sz); + keyType[sz] = '\0'; + for (i = 0; i < sz; i++) { + if ((byte)keyType[i] < 0x21 || (byte)keyType[i] > 0x7e) + ret = -1; + } + } + if (ret != 0) { + fprintf(stderr, "Rejecting a malformed host key.\n"); + return ret; + } #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) { - DecodedCert dCert; - byte* der = NULL; - word32 derSz = 0; - - if (ParseRFC6187(pubKey, pubKeySz, &der, &derSz) == WS_SUCCESS) { - wc_InitDecodedCert(&dCert, der, derSz, NULL); - if (wc_ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL) != 0) { - WLOG(WS_LOG_DEBUG, "public key not a cert"); - } - else { - int ipMatch = 0; - DNS_entry* current = dCert.altNames; + /* The library has already checked an x509v3 key's chain to the CA. */ + if (WSTRNCMP(keyType, "x509v3-", 7) == 0) { + return ClientCertHostCheck(pubKey, pubKeySz, host); + } +#endif - if (ctx == NULL) { - WLOG(WS_LOG_ERROR, "No host IP set to check against!"); - ret = -1; - } + if (acceptAnyHostKey) { + fprintf(stderr, "Warning: accepting the %s host key for %s " + "without checking it.\n", keyType, host); + return 0; + } - if (ret == 0) { - while (current != NULL) { - if (current->type == ASN_IP_TYPE) { - WLOG(WS_LOG_DEBUG, "host cert alt. name IP : %s", - current->ipString); - WLOG(WS_LOG_DEBUG, - "\texpecting host IP : %s", (char*)ctx); - if (XSTRCMP((const char*)ctx, - current->ipString) == 0) { - WLOG(WS_LOG_DEBUG, "\tmatched!"); - ipMatch = 1; - } - } - current = current->next; - } - } +#if !defined(NO_FILESYSTEM) && defined(WOLFSSL_BASE64_ENCODE) + { + char* encodedKey = NULL; + char* fileName = NULL; + char* portName = NULL; + char* otherName = NULL; + const char* name = host; + char fp[WOLFSSH_CLIENT_FINGERPRINT_SIZE_ESTIMATE]; + word32 encSz = 0; + + /* OpenSSH names a host on any port but 22 as [host]:port. */ + if (hostCtx->port != 22) { + sz = (word32)WSTRLEN(host) + 9; + portName = (char*)WMALLOC(sz, NULL, 0); + if (portName == NULL) + ret = -1; + else { + WSNPRINTF(portName, sz, "[%s]:%u", host, + (unsigned int)hostCtx->port); + name = portName; + } + } - if (ipMatch == 0) { - printf("IP did not match expected IP"); - if (!IPOverride) { - printf("\n"); - ret = -1; - } - else { - ret = 0; - printf("..overriding\n"); - } - } + /* Base64 size plus a nul */ + if (pubKeySz > MAX_PACKET_SZ) { + ret = -1; + } + if (ret == 0) { + encSz = (pubKeySz + 2) / 3 * 4 + 1; + encodedKey = (char*)WMALLOC(encSz, NULL, 0); + if (encodedKey == NULL) + ret = -1; + } + if (ret == 0) { + ret = Base64_Encode_NoNl(pubKey, pubKeySz, (byte*)encodedKey, + &encSz); + } + if (ret == 0) { + encodedKey[encSz] = '\0'; + ret = FingerprintKey(pubKey, pubKeySz, fp); + } + if (ret == 0) { + ret = KnownHostsLookup(name, keyType, encodedKey, &fileName); + /* As OpenSSH does, take a bare host line when no [host]:port + * line has a key of this type. */ + if (ret < 0 && portName != NULL + && KnownHostsLookup(host, keyType, encodedKey, + &otherName) == 0) { + ret = 0; + } + if (ret == 1) { + fprintf(stderr, "The %s host key for %s has changed!\n" + "Fingerprint: %s\n" + "Rejecting the connection.\n", keyType, host, fp); + } + else if (ret != 0) { + fprintf(stderr, "The %s host key for %s is unknown.\n" + "Fingerprint: %s\n" + "To trust it, add this line to %s:\n" + "%s %s %s\n" + "Or use -Y to accept any host key, insecurely.\n", + keyType, host, fp, + fileName != NULL ? fileName : "~/.ssh/known_hosts", + name, keyType, encodedKey); } - FreeDecodedCert(&dCert); } + else { + fprintf(stderr, "Couldn't encode the host key.\n"); + } + + if (encodedKey != NULL) + WFREE(encodedKey, NULL, 0); + if (fileName != NULL) + WFREE(fileName, NULL, 0); + if (otherName != NULL) + WFREE(otherName, NULL, 0); + if (portName != NULL) + WFREE(portName, NULL, 0); } #else - WLOG(WS_LOG_DEBUG, "wolfSSL not built with OPENSSL_ALL or WOLFSSL_IP_ALT_NAME"); - WLOG(WS_LOG_DEBUG, "\tnot checking IP address from peer's cert"); -#endif + fprintf(stderr, "No known_hosts support. Use -Y to accept any host " + "key, insecurely.\n"); + ret = -1; #endif - return ret; + return (ret == 0) ? 0 : -1; } diff --git a/examples/client/common.h b/examples/client/common.h index 2198a8ace..b5c865039 100644 --- a/examples/client/common.h +++ b/examples/client/common.h @@ -38,7 +38,18 @@ int ClientSetEcho(int type); void ClientUnbufferStdin(void); int ClientUserAuth(byte authType, WS_UserAuthData* authData, void* ctx); +/* The host and port connected to, the ClientPublicKeyCheck() ctx. */ +typedef struct ClientHostCtx { + const char* host; + word16 port; +} ClientHostCtx; +/* Host key check callback; ctx is a ClientHostCtx. A plain key must be in + * ~/.ssh/known_hosts, under [host]:port when the port is not 22, else + * under the bare host; a certificate must name the host. */ int ClientPublicKeyCheck(const byte* pubKey, word32 pubKeySz, void* ctx); +/* A nonzero flag accepts any plain host key, insecurely, with a warning. */ +void ClientAcceptAnyHostKey(int flag); +/* A nonzero flag accepts a host certificate that names another host. */ void ClientIPOverride(int flag); int ClientParsePort(const char* in, word16* port); /* Wipes the password and keyboard-interactive responses ClientUserAuth() diff --git a/examples/portfwd/include.am b/examples/portfwd/include.am index 62984b55c..f393230c7 100644 --- a/examples/portfwd/include.am +++ b/examples/portfwd/include.am @@ -4,7 +4,9 @@ if BUILD_FWD noinst_PROGRAMS += examples/portfwd/portfwd examples_portfwd_portfwd_SOURCES = examples/portfwd/portfwd.c \ - examples/portfwd/wolfssh_portfwd.h + examples/portfwd/wolfssh_portfwd.h \ + examples/client/common.c \ + examples/client/common.h examples_portfwd_portfwd_LDADD = src/libwolfssh.la examples_portfwd_portfwd_DEPENDENCIES = src/libwolfssh.la endif diff --git a/examples/portfwd/portfwd.c b/examples/portfwd/portfwd.c index ee84d5cb3..130c9b7a0 100644 --- a/examples/portfwd/portfwd.c +++ b/examples/portfwd/portfwd.c @@ -48,6 +48,7 @@ #ifndef NO_WOLFSSH_CLIENT #include "examples/portfwd/wolfssh_portfwd.h" +#include "examples/client/common.h" /* The portfwd tool will be a client or server in the port forwarding @@ -112,7 +113,10 @@ static void ShowUsage(void) " -t port to forward to (REQUIRED)\n" " -r remote (reverse) forward: ask the SSH server to\n" " listen on -F/-f and tunnel connections back to\n" - " the local -T/-t target\n", + " the local -T/-t target\n" + " -Y accept any host key, insecure; otherwise it must " + "be in\n" + " ~/.ssh/known_hosts\n", LIBWOLFSSH_VERSION_STRING, LIBWOLFSSL_VERSION_STRING, wolfSshIp, wolfSshPort, defaultFwdFromHost); @@ -228,13 +232,10 @@ static int wsUserAuth(byte authType, } +/* ctx is the ClientHostCtx connected to, checked against known_hosts. */ static int wsPublicKeyCheck(const byte* pubKey, word32 pubKeySz, void* ctx) { - printf("Sample public key check callback\n" - " public key = %p\n" - " public key size = %u\n" - " ctx = %s\n", pubKey, pubKeySz, (const char*)ctx); - return 0; + return ClientPublicKeyCheck(pubKey, pubKeySz, ctx); } @@ -447,6 +448,7 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args) WOLFSSH* ssh; WOLFSSH_CTX* ctx; char* host = (char*)wolfSshIp; + ClientHostCtx hostCtx; word16 port = wolfSshPort; word16 fwdFromPort = INVALID_FWD_PORT; word16 fwdToPort = INVALID_FWD_PORT; @@ -498,7 +500,8 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args) memset(&fwdState, 0, sizeof(fwdState)); fwdState.appFd = (SOCKET_T)-1; - while ((ch = mygetopt(argc, argv, "?rf:h:p:t:u:F:P:R:T:")) != -1) { + ClientAcceptAnyHostKey(0); + while ((ch = mygetopt(argc, argv, "?rf:h:p:t:u:F:P:R:T:Y")) != -1) { switch (ch) { case 'h': host = myoptarg; @@ -551,6 +554,10 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args) reverse = 1; break; + case 'Y': + ClientAcceptAnyHostKey(1); + break; + case '?': ShowUsage(); exit(EXIT_SUCCESS); @@ -619,7 +626,9 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args) wolfSSH_SetUserAuthCtx(ssh, (void*)password); wolfSSH_CTX_SetPublicKeyCheck(ctx, wsPublicKeyCheck); - wolfSSH_SetPublicKeyCheckCtx(ssh, (void*)"You've been sampled."); + hostCtx.host = host; + hostCtx.port = port; + wolfSSH_SetPublicKeyCheckCtx(ssh, &hostCtx); wolfSSH_CTX_SetChannelOpenRespCb(ctx, wsChannelOpenConfCb, wsChannelOpenFailCb); wolfSSH_SetChannelOpenCtx(ssh, (void*)"port forward"); diff --git a/examples/scpclient/scpclient.c b/examples/scpclient/scpclient.c index db633b5bf..c762f183d 100644 --- a/examples/scpclient/scpclient.c +++ b/examples/scpclient/scpclient.c @@ -81,8 +81,11 @@ static void ShowUsage(void) printf(" -%c %-" USAGE_WIDE "s %s\n", 'A', "", "filename for DER CA certificate to verify host"); printf(" -%c %-" USAGE_WIDE "s %s\n", 'X', "", - "Ignore IP checks on peer vs peer certificate"); + "accept a host certificate for another host"); #endif + printf(" -%c %-" USAGE_WIDE "s %s\n", 'Y', "", + "accept any host key, insecure; otherwise it must be in"); + printf(" %-" USAGE_WIDE "s %s\n", "", "~/.ssh/known_hosts"); } @@ -106,6 +109,7 @@ THREAD_RETURN WOLFSSH_THREAD scp_client(void* args) const char* username = NULL; const char* password = NULL; char* host = (char*)wolfSshIp; + ClientHostCtx hostCtx; char* path1 = NULL; char* path2 = NULL; word16 port = wolfSshPort; @@ -120,7 +124,8 @@ THREAD_RETURN WOLFSSH_THREAD scp_client(void* args) ((func_args*)args)->return_code = 0; - while ((ch = mygetopt(argc, argv, "H:L:NP:S:hp:u:XJ:A:i:j:")) != -1) { + ClientAcceptAnyHostKey(0); + while ((ch = mygetopt(argc, argv, "H:L:NP:S:hp:u:XYJ:A:i:j:")) != -1) { switch (ch) { case 'H': host = myoptarg; @@ -177,13 +182,15 @@ THREAD_RETURN WOLFSSH_THREAD scp_client(void* args) caCert = myoptarg; break; - #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME) case 'X': ClientIPOverride(1); break; - #endif #endif + case 'Y': + ClientAcceptAnyHostKey(1); + break; + default: ShowUsage(); exit(MY_EX_USAGE); @@ -265,7 +272,9 @@ THREAD_RETURN WOLFSSH_THREAD scp_client(void* args) if (ssh == NULL) err_sys("Couldn't create wolfSSH session."); - wolfSSH_SetPublicKeyCheckCtx(ssh, (void*)host); + hostCtx.host = host; + hostCtx.port = port; + wolfSSH_SetPublicKeyCheckCtx(ssh, &hostCtx); if (password != NULL) wolfSSH_SetUserAuthCtx(ssh, (void*)password); diff --git a/examples/sftpclient/sftpclient.c b/examples/sftpclient/sftpclient.c index 768df5218..5ca92c9bf 100644 --- a/examples/sftpclient/sftpclient.c +++ b/examples/sftpclient/sftpclient.c @@ -57,7 +57,7 @@ #else #define SFTPC_OPTLIST_STORE "" #endif -#define SFTPC_OPTLIST "?d:gh:i:j:k:l:p:r:u:EGNP:J:A:X" SFTPC_OPTLIST_STORE +#define SFTPC_OPTLIST "?d:gh:i:j:k:l:p:r:u:EGNP:J:A:XY" SFTPC_OPTLIST_STORE #if defined(WOLFSSH_SFTP) && !defined(NO_WOLFSSH_CLIENT) @@ -431,8 +431,11 @@ static void ShowUsage(void) printf(" Certificate example : client -u orange \\\n"); printf(" -J orange-cert.der -i orange-key.der\n"); printf(" -A filename for DER CA certificate to verify host\n"); - printf(" -X Ignore IP checks on peer vs peer certificate\n"); + printf(" -X accept a host certificate for another host\n"); #endif + printf(" -Y accept any host key, insecure; otherwise it must " + "be in\n" + " ~/.ssh/known_hosts\n"); ShowCommands(); } @@ -1601,6 +1604,7 @@ THREAD_RETURN WOLFSSH_THREAD sftpclient_test(void* args) /* int peerEcc = 0; */ word16 port = wolfSshPort; char* host = (char*)wolfSshIp; + ClientHostCtx hostCtx; const char* username = NULL; const char* password = NULL; const char* defaultSftpPath = NULL; @@ -1622,6 +1626,7 @@ THREAD_RETURN WOLFSSH_THREAD sftpclient_test(void* args) char** argv = ((func_args*)args)->argv; ((func_args*)args)->return_code = 0; + ClientAcceptAnyHostKey(0); while ((ch = mygetopt(argc, argv, SFTPC_OPTLIST)) != -1) { switch (ch) { case 'd': @@ -1694,13 +1699,15 @@ THREAD_RETURN WOLFSSH_THREAD sftpclient_test(void* args) caCert = myoptarg; break; - #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME) case 'X': ClientIPOverride(1); break; - #endif #endif + case 'Y': + ClientAcceptAnyHostKey(1); + break; + #ifdef WOLFSSH_WINDOWS_CERT_STORE case 'W': certStoreSpec = myoptarg; @@ -1872,7 +1879,9 @@ THREAD_RETURN WOLFSSH_THREAD sftpclient_test(void* args) if (password != NULL) wolfSSH_SetUserAuthCtx(ssh, (void*)password); - wolfSSH_SetPublicKeyCheckCtx(ssh, (void*)host); + hostCtx.host = host; + hostCtx.port = port; + wolfSSH_SetPublicKeyCheckCtx(ssh, &hostCtx); ret = wolfSSH_SetUsername(ssh, username); if (ret != WS_SUCCESS) diff --git a/examples/tpmcertserver/include.am b/examples/tpmcertserver/include.am index 0fc68d0f8..b381106ed 100644 --- a/examples/tpmcertserver/include.am +++ b/examples/tpmcertserver/include.am @@ -13,7 +13,9 @@ endif if BUILD_EXAMPLE_CLIENTS noinst_PROGRAMS += examples/tpmcertserver/tpmcertclient examples_tpmcertserver_tpmcertclient_SOURCES = \ - examples/tpmcertserver/tpmcertclient.c + examples/tpmcertserver/tpmcertclient.c \ + examples/client/common.c \ + examples/client/common.h examples_tpmcertserver_tpmcertclient_LDADD = src/libwolfssh.la examples_tpmcertserver_tpmcertclient_DEPENDENCIES = src/libwolfssh.la endif diff --git a/examples/tpmcertserver/tpmcertclient.c b/examples/tpmcertserver/tpmcertclient.c index bed92c1f4..1ae9708c6 100644 --- a/examples/tpmcertserver/tpmcertclient.c +++ b/examples/tpmcertserver/tpmcertclient.c @@ -26,6 +26,7 @@ #include #include +#include "examples/client/common.h" #if defined(WOLFSSH_CERTS) && !defined(NO_FILESYSTEM) && \ !defined(WOLFSSH_USER_FILESYSTEM) @@ -54,18 +55,11 @@ static int TpmCcUserAuth(byte authType, WS_UserAuthData* authData, void* ctx) } -/* Host key acceptance callback. wolfSSH verifies the server's X.509 certificate - * chain against the root CA loaded with wolfSSH_CTX_AddRootCert_file() later, - * during the key exchange, when it extracts the public key from the certificate. - * Because the client only accepts x509v3 host key algorithms, that CA - * verification is always performed. This callback just accepts the presented - * host key blob. */ +/* ctx is the ClientHostCtx. wolfSSH verifies the chain to the root CA; this + * checks the certificate names the host. */ static int TpmCcHostKeyCheck(const byte* pubKey, word32 pubKeySz, void* ctx) { - WOLFSSH_UNUSED(pubKey); - WOLFSSH_UNUSED(pubKeySz); - WOLFSSH_UNUSED(ctx); - return 0; + return ClientPublicKeyCheck(pubKey, pubKeySz, ctx); } @@ -75,6 +69,7 @@ int main(int argc, char* argv[]) int i; word16 port = TPMCC_DEFAULT_PORT; const char* host = "127.0.0.1"; + ClientHostCtx hostCtx; const char* caFile = "tpm-server-cert.der"; WOLFSSH_CTX* ctx = NULL; WOLFSSH* ssh = NULL; @@ -134,6 +129,11 @@ int main(int argc, char* argv[]) ssh = wolfSSH_new(ctx); if (ssh == NULL) ret = -1; + else { + hostCtx.host = host; + hostCtx.port = port; + wolfSSH_SetPublicKeyCheckCtx(ssh, &hostCtx); + } } if (ret == 0) diff --git a/keys/include.am b/keys/include.am index 292f3dfd0..64689e16e 100644 --- a/keys/include.am +++ b/keys/include.am @@ -23,6 +23,7 @@ EXTRA_DIST+= \ keys/ca-cert-ecc.pem keys/ca-key-ecc.der keys/ca-key-ecc.pem \ keys/server-cert.der keys/server-cert.pem \ keys/server-cert-ed25519.der keys/server-cert-ed25519.pem \ + keys/server-cert-email-san.der keys/server-cert-email-san.pem \ keys/fred-cert.der keys/fred-cert.pem \ keys/server-key.pem keys/fred-key.der keys/fred-key.pem \ keys/id_ecdsa keys/id_ecdsa.pub keys/id_rsa keys/id_rsa.pub \ diff --git a/keys/renewcerts.cnf b/keys/renewcerts.cnf index e6746888a..6b2d39f51 100644 --- a/keys/renewcerts.cnf +++ b/keys/renewcerts.cnf @@ -52,6 +52,11 @@ subjectKeyIdentifier=hash authorityKeyIdentifier=keyid:always,issuer:always subjectAltName = DNS:example, IP:127.0.0.1 +[ v3_server_email ] +subjectKeyIdentifier=hash +authorityKeyIdentifier=keyid:always,issuer:always +subjectAltName = email:server@example.com + [ req_distinguished_name ] diff --git a/keys/renewcerts.sh b/keys/renewcerts.sh index d6ba8d146..eff010cb0 100755 --- a/keys/renewcerts.sh +++ b/keys/renewcerts.sh @@ -44,6 +44,13 @@ openssl x509 -req -in server-cert-ed25519.csr -days 3650 -extfile "$CONFIG" -ext openssl x509 -in server-cert-ed25519.pem -outform DER -out server-cert-ed25519.der rm -f server-cert-ed25519-key.pem server-cert-ed25519.csr +# renew server-cert-email-san, whose only subjectAltName is an email +openssl req -subj '/C=US/ST=Washington/L=Seattle/O=Eliptic/OU=ECC/CN=www.wolfssl.com/emailAddress=server@example.com' -key server-key.pem -out server-cert-email-san.csr -config "$CONFIG" -new -nodes + +openssl x509 -req -in server-cert-email-san.csr -days 3650 -extfile "$CONFIG" -extensions v3_server_email -CA ca-cert-ecc.pem -CAkey ca-key-ecc.pem -text -out server-cert-email-san.pem -set_serial 10 +openssl x509 -in server-cert-email-san.pem -outform DER -out server-cert-email-san.der +rm -f server-cert-email-san.csr + rm index.* if [ -n "$1" ]; then rm -f "$CONFIG" diff --git a/keys/server-cert-email-san.der b/keys/server-cert-email-san.der new file mode 100644 index 000000000..8d2cdaa45 Binary files /dev/null and b/keys/server-cert-email-san.der differ diff --git a/keys/server-cert-email-san.pem b/keys/server-cert-email-san.pem new file mode 100644 index 000000000..13a96fff0 --- /dev/null +++ b/keys/server-cert-email-san.pem @@ -0,0 +1,54 @@ +Certificate: + Data: + Version: 3 (0x2) + Serial Number: 10 (0xa) + Signature Algorithm: ecdsa-with-SHA256 + Issuer: C=US, ST=Washington, L=Seattle, O=wolfSSL, OU=Development, CN=www.wolfssl.com, emailAddress=ca@example.com + Validity + Not Before: Nov 11 18:00:24 2026 GMT + Not After : Nov 8 18:00:24 2036 GMT + Subject: C=US, ST=Washington, L=Seattle, O=Eliptic, OU=ECC, CN=www.wolfssl.com, emailAddress=server@example.com + Subject Public Key Info: + Public Key Algorithm: id-ecPublicKey + Public-Key: (256 bit field, 128 bit security level) + pub: + 04:bb:33:ac:4c:27:50:4a:c6:4a:a5:04:c3:3c:de:9f: + 36:db:72:2d:ce:94:ea:2b:fa:cb:20:09:39:2c:16:e8: + 61:02:e9:af:4d:d3:02:93:9a:31:5b:97:92:21:7f:f0: + cf:18:da:91:11:02:34:86:e8:20:58:33:0b:80:34:89: + d8 + ASN1 OID: prime256v1 + NIST CURVE: P-256 + X509v3 extensions: + X509v3 Subject Key Identifier: + 5D:5D:26:EF:AC:7E:36:F9:9B:76:15:2B:4A:25:02:23:EF:B2:89:30 + X509v3 Authority Key Identifier: + keyid:56:8E:9A:C3:F0:42:DE:18:B9:45:55:6E:F9:93:CF:EA:C3:F3:A5:21 + DirName:/C=US/ST=Washington/L=Seattle/O=wolfSSL/OU=Development/CN=www.wolfssl.com/emailAddress=ca@example.com + serial:6 + X509v3 Subject Alternative Name: + email:server@example.com + Signature Algorithm: ecdsa-with-SHA256 + Signature Value: + 30:44:02:20:6a:16:79:1b:9d:bd:75:59:c3:4f:b1:50:d0:f3:54:94:df:53:2d:97: + 13:93:9e:99:a4:a1:ab:8e:14:35:60:16:02:20:45:51:63:91:ff:21:bd:52:f6:6b: + 78:21:5c:87:8a:fd:bd:7d:c3:f1:a6:c1:7d:3b:67:de:09:87:eb:dc:e5:23 +-----BEGIN CERTIFICATE----- +MIIDHzCCAsagAwIBAgIBCjAKBggqhkjOPQQDAjCBlTELMAkGA1UEBhMCVVMxEzAR +BgNVBAgMCldhc2hpbmd0b24xEDAOBgNVBAcMB1NlYXR0bGUxEDAOBgNVBAoMB3dv +bGZTU0wxFDASBgNVBAsMC0RldmVsb3BtZW50MRgwFgYDVQQDDA93d3cud29sZnNz +bC5jb20xHTAbBgkqhkiG9w0BCQEWDmNhQGV4YW1wbGUuY29tMB4XDTI2MTExMTE4 +MDAyNFoXDTM2MTEwODE4MDAyNFowgZExCzAJBgNVBAYTAlVTMRMwEQYDVQQIDApX +YXNoaW5ndG9uMRAwDgYDVQQHDAdTZWF0dGxlMRAwDgYDVQQKDAdFbGlwdGljMQww +CgYDVQQLDANFQ0MxGDAWBgNVBAMMD3d3dy53b2xmc3NsLmNvbTEhMB8GCSqGSIb3 +DQEJARYSc2VydmVyQGV4YW1wbGUuY29tMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcD +QgAEuzOsTCdQSsZKpQTDPN6fNttyLc6U6iv6yyAJOSwW6GEC6a9N0wKTmjFbl5Ih +f/DPGNqREQI0huggWDMLgDSJ2KOCAQcwggEDMB0GA1UdDgQWBBRdXSbvrH42+Zt2 +FStKJQIj77KJMDCBwgYDVR0jBIG6MIG3gBRWjprD8ELeGLlFVW75k8/qw/OlIaGB +m6SBmDCBlTELMAkGA1UEBhMCVVMxEzARBgNVBAgMCldhc2hpbmd0b24xEDAOBgNV +BAcMB1NlYXR0bGUxEDAOBgNVBAoMB3dvbGZTU0wxFDASBgNVBAsMC0RldmVsb3Bt +ZW50MRgwFgYDVQQDDA93d3cud29sZnNzbC5jb20xHTAbBgkqhkiG9w0BCQEWDmNh +QGV4YW1wbGUuY29tggEGMB0GA1UdEQQWMBSBEnNlcnZlckBleGFtcGxlLmNvbTAK +BggqhkjOPQQDAgNHADBEAiBqFnkbnb11WcNPsVDQ81SU31MtlxOTnpmkoauOFDVg +FgIgRVFjkf8hvVL2a3ghXIeK/b19w/GmwX07Z94Jh+vc5SM= +-----END CERTIFICATE----- diff --git a/scripts/client-hostkey.test b/scripts/client-hostkey.test new file mode 100755 index 000000000..2e01e3a15 --- /dev/null +++ b/scripts/client-hostkey.test @@ -0,0 +1,216 @@ +#!/bin/sh + +# Example client host key check test +# +# HOME is a scratch dir. An unknown or changed host key must be refused +# unless -Y. The echoserver never listens on 22, so the client looks up +# [host]:port first, then the bare host. + +no_pid=-1 +server_pid=$no_pid +work_dir="`pwd`/client_hostkey_test$$" +ready_file="$work_dir/ready" +known_hosts="$work_dir/.ssh/known_hosts" +client_out="$work_dir/client.out" +port=0 + +[ ! -x ./examples/client/client ] \ + && echo "example client doesn't exist, skipping" && exit 77 +[ ! -x ./examples/echoserver/echoserver ] \ + && echo "echoserver doesn't exist, skipping" && exit 77 + +WOLFSSH_OPTIONS=`./apps/wolfssh-options` || { + echo "fail: could not run ./apps/wolfssh-options" + exit 1 +} +echo "$WOLFSSH_OPTIONS" | grep -qx "TEST_BLOCK" \ + && echo "WOLFSSH_TEST_BLOCK build, skipping" && exit 77 + +# The address the examples connect to by default. +host="127.0.0.1" +echo "$WOLFSSH_OPTIONS" | grep -qx "TEST_IPV6" && host="::1" + +do_cleanup() { + if [ $server_pid != $no_pid ]; then + kill -9 $server_pid 2>/dev/null + wait $server_pid 2>/dev/null + server_pid=$no_pid + fi + rm -rf "$work_dir" +} + +do_trap() { + do_cleanup + exit 1 +} + +trap do_trap INT TERM + +fail() { + echo "FAIL: $1" + [ -f "$client_out" ] && cat "$client_out" + do_cleanup + exit 1 +} + +start_server() { + rm -f "$ready_file" + ./examples/echoserver/echoserver -1 -f -R "$ready_file" \ + > "$work_dir/server.log" 2>&1 & + server_pid=$! + counter=0 + while [ ! -s "$ready_file" ] && [ "$counter" -lt 100 ]; do + sleep 0.1 + counter=$((counter + 1)) + done + [ -s "$ready_file" ] || fail "echoserver never started" + port=`cat "$ready_file"` +} + +# kh_lines - when set, written to known_hosts once the server is up, with +# @PORT@ replaced by its port +# kh_hash - when set, the known_hosts names are hashed after that +# $@ - extra client arguments +run_client() { + start_server + if [ -n "$kh_lines" ]; then + printf '%s\n' "$kh_lines" | sed "s/@PORT@/$port/g" > "$known_hosts" + if [ -n "$kh_hash" ]; then + ssh-keygen -H -f "$known_hosts" > /dev/null 2>&1 \ + || fail "ssh-keygen couldn't hash known_hosts" + rm -f "$known_hosts.old" + fi + fi + HOME="$work_dir" ./examples/client/client -u jill -P upthehill \ + -p $port "$@" < /dev/null > "$client_out" 2>&1 + status=$? + kill -9 $server_pid 2>/dev/null + wait $server_pid 2>/dev/null + server_pid=$no_pid + return $status +} + +mkdir -p "$work_dir/.ssh" || fail "couldn't make the work dir" + +echo "Test an unknown host key is refused" +run_client && fail "the client accepted an unknown host key" +if grep -q "No known_hosts support" "$client_out"; then + echo "client built without known_hosts support, skipping" + do_cleanup + exit 77 +fi +grep -qF "host key for $host is unknown" "$client_out" \ + || fail "the client didn't report the unknown host key" +grep -q "Server said" "$client_out" \ + && fail "the client talked to an unknown server" + +# The refusal prints the line that trusts the key, named [host]:port. +entry=`awk -v h="[$host]:$port" '$1 == h' "$client_out"` +[ -n "$entry" ] || fail "the client didn't print a [host]:port line" +set -- $entry +type="$2" +key="$3" +pkey="[$host]:@PORT@ $type $key" + +echo "Test -Y accepts an unknown host key with a warning" +run_client -Y || fail "the client refused the host key with -Y" +grep -q "Warning: accepting the" "$client_out" \ + || fail "the client didn't warn about -Y" +grep -q "Server said" "$client_out" \ + || fail "the echoserver's reply didn't make it back" + +echo "Test a known [host]:port host key is accepted" +kh_lines="$pkey" +run_client || fail "the client refused a known host key" +grep -q "Server said" "$client_out" \ + || fail "the echoserver's reply didn't make it back" + +echo "Test a known bare host key is accepted on another port" +kh_lines="$host $type $key" +run_client || fail "the client refused a bare host line" + +echo "Test a name in a comma list is accepted" +kh_lines="example.invalid,[$host]:@PORT@ $type $key" +run_client || fail "the client refused a name in a comma list" + +if command -v ssh-keygen > /dev/null 2>&1; then + echo "Test a hashed [host]:port name is accepted" + kh_lines="$pkey" + kh_hash=1 + run_client || fail "the client refused a hashed name" + grep -q '^|1|' "$known_hosts" || fail "ssh-keygen didn't hash the name" + + echo "Test a hashed name for another port is refused" + kh_lines="[$host]:1 $type $key" + run_client && fail "the client accepted a hashed name for another port" + kh_hash= +fi + +if [ "`id -u`" != 0 ]; then + echo "Test an unreadable known_hosts is refused" + kh_lines= + chmod 000 "$known_hosts" + run_client && fail "the client accepted a host it couldn't look up" + chmod 600 "$known_hosts" +fi + +echo "Test a stale entry ahead of the right one" +kh_lines="[$host]:@PORT@ $type AAAA$key +$pkey" +run_client || fail "a stale entry hid the host key" + +echo "Test a changed host key is refused" +kh_lines="[$host]:@PORT@ $type AAAA$key" +run_client && fail "the client accepted a changed host key" +grep -qF "host key for $host has changed" "$client_out" \ + || fail "the client didn't report the changed host key" + +echo "Test a changed [host]:port key is not masked by a bare host line" +kh_lines="[$host]:@PORT@ $type AAAA$key +$host $type $key" +run_client && fail "a bare host line masked a changed host key" +grep -qF "host key for $host has changed" "$client_out" \ + || fail "the client didn't report the changed host key" + +echo "Test a changed bare host key is unknown on another port" +kh_lines="$host $type AAAA$key" +run_client && fail "the client accepted a changed bare host key" +grep -qF "host key for $host is unknown" "$client_out" \ + || fail "the client didn't report the unknown host key" + +echo "Test a key known only for another port is refused" +kh_lines="[$host]:1 $type $key" +run_client && fail "the client accepted another port's key" +grep -q "is unknown" "$client_out" \ + || fail "the client didn't report the unknown host key" + +echo "Test a key known only for another host is refused" +kh_lines="example.invalid $type $key" +run_client && fail "the client accepted another host's key" +grep -q "is unknown" "$client_out" \ + || fail "the client didn't report the unknown host key" +kh_lines= + +if [ -x ./examples/portfwd/portfwd ] \ + && ! ./examples/portfwd/portfwd '-?' 2>&1 | grep -q "does not exist" +then + echo "Test portfwd refuses an unknown host key" + rm -f "$known_hosts" + start_server + HOME="$work_dir" ./examples/portfwd/portfwd -u jill -P upthehill \ + -p $port -f 0 -r -t 1 < /dev/null > "$client_out" 2>&1 & + fwd_pid=$! + counter=0 + while kill -0 $fwd_pid 2>/dev/null && [ "$counter" -lt 100 ]; do + sleep 0.1 + counter=$((counter + 1)) + done + kill -9 $fwd_pid 2>/dev/null && fail "portfwd kept an unknown host" + wait $fwd_pid && fail "portfwd accepted an unknown host key" + grep -qF "host key for $host is unknown" "$client_out" \ + || fail "portfwd didn't report the unknown host key" +fi + +do_cleanup +echo "All tests passed" +exit 0 diff --git a/scripts/fwd-bulk.test b/scripts/fwd-bulk.test index b39af564c..3acdec84a 100755 --- a/scripts/fwd-bulk.test +++ b/scripts/fwd-bulk.test @@ -264,7 +264,7 @@ do_tail_attempt() { [ -s "$tail_ready_file" ] \ || do_fail "no ready file, echoserver didn't start" - ./examples/portfwd/portfwd -u jill -P upthehill \ + ./examples/portfwd/portfwd -Y -u jill -P upthehill \ -p `cat "$tail_ready_file"` \ -f $tail_entry_port -t $tail_target_port -R "$tail_fwd_ready_file" \ > "$tail_portfwd_log" 2>&1 & @@ -345,7 +345,7 @@ done [ -s "$ready_file" ] || do_fail "no ready file, echoserver didn't start" port=`cat "$ready_file"` -./examples/portfwd/portfwd -u jill -P upthehill -p "$port" \ +./examples/portfwd/portfwd -Y -u jill -P upthehill -p "$port" \ -f $entry_port -t $target_port -R "$fwd_ready_file" \ > "$portfwd_log" 2>&1 & portfwd_pid=$! diff --git a/scripts/fwd.test.expect b/scripts/fwd.test.expect index 1ef36384b..102bd78d8 100755 --- a/scripts/fwd.test.expect +++ b/scripts/fwd.test.expect @@ -210,7 +210,7 @@ proc run_phase {label reverse entry_port target_port} { puts "\n\[3\] Starting wolfssh client (plain:$entry_port -> $target_port)..." set reverse_flag "" } - spawn sh -c "exec ./examples/portfwd/portfwd -u jill -P upthehill -p $ssh_port \ + spawn sh -c "exec ./examples/portfwd/portfwd -Y -u jill -P upthehill -p $ssh_port \ $reverse_flag -f $entry_port -t $target_port -R $clt_ready >$clt_log 2>&1" set wolfssh_clt_id $spawn_id set wolfssh_clt_pid [exp_pid] diff --git a/scripts/get-put.test b/scripts/get-put.test index 1c29fc79b..922bfc30b 100755 --- a/scripts/get-put.test +++ b/scripts/get-put.test @@ -91,7 +91,7 @@ echo "This is some sample test to make a file to copy back and forth." > sample1 echo "This is a different set of sample text to copy back and forth." > sample2.txt # Get test. -if ! ./examples/sftpclient/wolfsftp -u jill -P upthehill -p "$PORT" \ +if ! ./examples/sftpclient/wolfsftp -Y -u jill -P upthehill -p "$PORT" \ -G -l sample1-copy.txt -r sample1.txt then echo "Unable to get file." @@ -101,7 +101,7 @@ fi # Test fail on file that does not exist rm -rf sample1-does-not-exist -if ./examples/sftpclient/wolfsftp -u jill -P upthehill -p "$PORT" \ +if ./examples/sftpclient/wolfsftp -Y -u jill -P upthehill -p "$PORT" \ -G -l sample1-copy.txt -r sample1-does-not-exist then echo "Success when expecting fail to get file." @@ -110,7 +110,7 @@ then fi # Put test. -if ! ./examples/sftpclient/wolfsftp -u jill -P upthehill -p "$PORT" \ +if ! ./examples/sftpclient/wolfsftp -Y -u jill -P upthehill -p "$PORT" \ -g -l sample2.txt -r sample2-copy.txt then echo "Unable to put file." @@ -135,7 +135,7 @@ fi # using full path test. rm -rf sample2-copy.txt PWD=`pwd` -if ! ./examples/sftpclient/wolfsftp -u jill -P upthehill -p "$PORT" \ +if ! ./examples/sftpclient/wolfsftp -Y -u jill -P upthehill -p "$PORT" \ -g -l sample2.txt -r $PWD/sample2-copy.txt then echo "Unable to put file using full path." diff --git a/scripts/include.am b/scripts/include.am index 472aa6565..c542233f2 100644 --- a/scripts/include.am +++ b/scripts/include.am @@ -16,6 +16,9 @@ if BUILD_SCP dist_noinst_SCRIPTS+= scripts/scp.test endif +# Skips itself when the example client wasn't built. +dist_noinst_SCRIPTS+= scripts/client-hostkey.test + # Not gated on BUILD_SSHCLIENT. The script skips itself when the client # app wasn't built. dist_noinst_SCRIPTS+= scripts/sshclient.test diff --git a/scripts/openssh-interop.test b/scripts/openssh-interop.test index 92dc16b02..57f3234e4 100755 --- a/scripts/openssh-interop.test +++ b/scripts/openssh-interop.test @@ -612,6 +612,14 @@ ssh-keygen -q -t rsa -b 3072 -N '' -f "$work_dir/hostkey-rsa" < /dev/null \ ssh-keygen -q -t ed25519 -N '' -f "$work_dir/hostkey-ed25519" < /dev/null \ || fail "couldn't make an Ed25519 host key for sshd" +# The client's HOME is the work dir; trust sshd's host keys there. +mkdir -p "$work_dir/.ssh" || fail "couldn't make the client's ~/.ssh" +for hostkey in hostkey hostkey-rsa hostkey-ed25519; do + printf '127.0.0.1 ' + cut -d' ' -f1,2 "$work_dir/$hostkey.pub" +done > "$work_dir/.ssh/known_hosts" \ + || fail "couldn't write the client's known_hosts" + # sshd takes a fixed port, so try a few. -f /dev/null keeps the machine's # own sshd_config out of it, and -ddd both raises the log level and keeps # sshd in the foreground for one connection. ForceCommand stands in for the diff --git a/scripts/scp.test b/scripts/scp.test index 35785b5ff..f7442a151 100755 --- a/scripts/scp.test +++ b/scripts/scp.test @@ -107,7 +107,7 @@ echo "Test basic copy from server to local" ./examples/echoserver/echoserver -1 -R $ready_file & server_pid=$! create_port -$run_client ./examples/scpclient/wolfscp -u jill -P upthehill -p $port -S $PWD/scripts/scp.test:$PWD/scp.test +$run_client ./examples/scpclient/wolfscp -Y -u jill -P upthehill -p $port -S $PWD/scripts/scp.test:$PWD/scp.test RESULT=$? remove_ready_file stop_server @@ -125,7 +125,7 @@ echo "Test basic copy from local to server" ./examples/echoserver/echoserver -1 -R $ready_file & server_pid=$! create_port -$run_client ./examples/scpclient/wolfscp -u jill -P upthehill -p $port -L $PWD/scripts/scp.test:$PWD/scp.test +$run_client ./examples/scpclient/wolfscp -Y -u jill -P upthehill -p $port -L $PWD/scripts/scp.test:$PWD/scp.test RESULT=$? remove_ready_file stop_server @@ -145,7 +145,7 @@ echo "Test copy from local to server directory ." ./examples/echoserver/echoserver -1 -R $ready_file & server_pid=$! create_port -$run_client ./examples/scpclient/wolfscp -u jill -P upthehill -p $port -L $PWD/scripts/scp.test:. +$run_client ./examples/scpclient/wolfscp -Y -u jill -P upthehill -p $port -L $PWD/scripts/scp.test:. RESULT=$? remove_ready_file stop_server @@ -163,7 +163,7 @@ echo "Test copy from server to local directory ." ./examples/echoserver/echoserver -1 -R $ready_file & server_pid=$! create_port -$run_client ./examples/scpclient/wolfscp -u jill -P upthehill -p $port -S $PWD/scripts/scp.test:. +$run_client ./examples/scpclient/wolfscp -Y -u jill -P upthehill -p $port -S $PWD/scripts/scp.test:. RESULT=$? remove_ready_file stop_server @@ -186,7 +186,7 @@ echo "Test basic copy from server to local, app-driven server" ./examples/echoserver/echoserver -A -N -1 -R $ready_file & server_pid=$! create_port -$run_client ./examples/scpclient/wolfscp -u jill -P upthehill -p $port -S $PWD/scripts/scp.test:$PWD/scp.test +$run_client ./examples/scpclient/wolfscp -Y -u jill -P upthehill -p $port -S $PWD/scripts/scp.test:$PWD/scp.test RESULT=$? remove_ready_file stop_server @@ -204,7 +204,7 @@ echo "Test basic copy from local to server, app-driven server" ./examples/echoserver/echoserver -A -N -1 -R $ready_file & server_pid=$! create_port -$run_client ./examples/scpclient/wolfscp -u jill -P upthehill -p $port -L $PWD/scripts/scp.test:$PWD/scp.test +$run_client ./examples/scpclient/wolfscp -Y -u jill -P upthehill -p $port -L $PWD/scripts/scp.test:$PWD/scp.test RESULT=$? remove_ready_file stop_server @@ -224,7 +224,7 @@ echo "Test basic copy from server to local, blocking app-driven server" ./examples/echoserver/echoserver -A -1 -R $ready_file & server_pid=$! create_port -$run_client ./examples/scpclient/wolfscp -u jill -P upthehill -p $port -S $PWD/scripts/scp.test:$PWD/scp.test +$run_client ./examples/scpclient/wolfscp -Y -u jill -P upthehill -p $port -S $PWD/scripts/scp.test:$PWD/scp.test RESULT=$? remove_ready_file stop_server @@ -243,7 +243,7 @@ touch $PWD/scripts/empty ./examples/echoserver/echoserver -1 -R $ready_file & server_pid=$! create_port -$run_client ./examples/scpclient/wolfscp -u jill -P upthehill -p $port -S $PWD/scripts/empty:$PWD/empty +$run_client ./examples/scpclient/wolfscp -Y -u jill -P upthehill -p $port -S $PWD/scripts/empty:$PWD/empty RESULT=$? remove_ready_file stop_server @@ -263,7 +263,7 @@ touch $PWD/scripts/empty ./examples/echoserver/echoserver -1 -R $ready_file & server_pid=$! create_port -$run_client ./examples/scpclient/wolfscp -u jill -P upthehill -p $port -L $PWD/scripts/empty:$PWD/empty +$run_client ./examples/scpclient/wolfscp -Y -u jill -P upthehill -p $port -L $PWD/scripts/empty:$PWD/empty RESULT=$? remove_ready_file stop_server @@ -283,7 +283,7 @@ touch $PWD/scripts/empty ./examples/echoserver/echoserver -1 -R $ready_file & server_pid=$! create_port -$run_client ./examples/scpclient/wolfscp -u jill -P upthehill -p $port -L $PWD/does-not-exist:$PWD/empty +$run_client ./examples/scpclient/wolfscp -Y -u jill -P upthehill -p $port -L $PWD/does-not-exist:$PWD/empty RESULT=$? remove_ready_file stop_server @@ -325,7 +325,7 @@ else ./examples/echoserver/echoserver -1 -R $ready_file & server_pid=$! create_port - $run_client ./examples/scpclient/wolfscp -u jill -P upthehill -p $port -S "$scp_symlink:$scp_symlink_out" + $run_client ./examples/scpclient/wolfscp -Y -u jill -P upthehill -p $port -S "$scp_symlink:$scp_symlink_out" RESULT=$? remove_ready_file stop_server @@ -354,7 +354,7 @@ else ./examples/echoserver/echoserver -1 -R $ready_file & server_pid=$! create_port - $run_client ./examples/scpclient/wolfscp -u jill -P upthehill -p $port -L "$PWD/scripts/scp.test:$scp_symlink" + $run_client ./examples/scpclient/wolfscp -Y -u jill -P upthehill -p $port -L "$PWD/scripts/scp.test:$scp_symlink" RESULT=$? remove_ready_file stop_server diff --git a/scripts/sftp.test b/scripts/sftp.test index f629f5f1b..aa1954b67 100755 --- a/scripts/sftp.test +++ b/scripts/sftp.test @@ -86,7 +86,7 @@ if [ "$nonblockingOnly" = 0 ]; then ./examples/echoserver/echoserver -1 -R "$ready_file" & server_pid=$! create_port - echo "exit" | ./examples/sftpclient/wolfsftp -u jill -P upthehill -p "$port" + echo "exit" | ./examples/sftpclient/wolfsftp -Y -u jill -P upthehill -p "$port" RESULT=$? remove_ready_file if [ $RESULT -ne 0 ]; then @@ -102,7 +102,7 @@ echo "Test non blocking connection" ./examples/echoserver/echoserver -N -1 -R "$ready_file" & server_pid=$! create_port -echo "exit" | ./examples/sftpclient/wolfsftp -N -u jill -P upthehill -p "$port" +echo "exit" | ./examples/sftpclient/wolfsftp -Y -N -u jill -P upthehill -p "$port" RESULT=$? remove_ready_file if [ $RESULT -ne 0 ]; then @@ -120,7 +120,7 @@ if [ "$nonblockingOnly" = 0 ]; then ./examples/echoserver/echoserver -A -1 -R "$ready_file" & server_pid=$! create_port - echo "exit" | ./examples/sftpclient/wolfsftp -u jill -P upthehill -p "$port" + echo "exit" | ./examples/sftpclient/wolfsftp -Y -u jill -P upthehill -p "$port" RESULT=$? remove_ready_file if [ $RESULT -ne 0 ]; then @@ -137,7 +137,7 @@ echo "Test non blocking connection to an app-driven server" ./examples/echoserver/echoserver -A -N -1 -R "$ready_file" & server_pid=$! create_port -echo "exit" | ./examples/sftpclient/wolfsftp -N -u jill -P upthehill -p "$port" +echo "exit" | ./examples/sftpclient/wolfsftp -Y -N -u jill -P upthehill -p "$port" RESULT=$? remove_ready_file if [ $RESULT -ne 0 ]; then @@ -153,7 +153,7 @@ if [ "$nonblockingOnly" = 0 ]; then ./examples/echoserver/echoserver -H -N -1 -R "$ready_file" & server_pid=$! create_port - ./examples/sftpclient/wolfsftp -N -u jill -P upthehill -p "$port" -g -r "$PWD/README.md-2" -l "$PWD/README.md" + ./examples/sftpclient/wolfsftp -Y -N -u jill -P upthehill -p "$port" -g -r "$PWD/README.md-2" -l "$PWD/README.md" RESULT=$? remove_ready_file rm -f "$PWD/README.md-2" @@ -171,7 +171,7 @@ if [ "$nonblockingOnly" = 0 ]; then ./examples/echoserver/echoserver -d "$PWD/examples" -1 -R "$ready_file" & server_pid=$! create_port - echo "exit" | ./examples/sftpclient/wolfsftp -N -u jill -P upthehill -p "$port" + echo "exit" | ./examples/sftpclient/wolfsftp -Y -N -u jill -P upthehill -p "$port" RESULT=$? remove_ready_file if [ $RESULT -ne 0 ]; then @@ -188,7 +188,7 @@ if [ "$nonblockingOnly" = 0 ]; then ./examples/echoserver/echoserver -N -1 -R "$ready_file" & server_pid=$! create_port - ./examples/sftpclient/wolfsftp -N -u jill -P upthehill -p "$port" -G -r "$PWD/this_file_does_not_exist_12345.txt" -l "$PWD/test_output.txt" > sftp_status_failure.log 2>&1 + ./examples/sftpclient/wolfsftp -Y -N -u jill -P upthehill -p "$port" -G -r "$PWD/this_file_does_not_exist_12345.txt" -l "$PWD/test_output.txt" > sftp_status_failure.log 2>&1 RESULT=$? grep -q "Unable to copy remote file" sftp_status_failure.log RESULT_MSG=$? diff --git a/src/internal.c b/src/internal.c index 71c540ee5..40062126b 100644 --- a/src/internal.c +++ b/src/internal.c @@ -7684,7 +7684,7 @@ static int CheckRsaKeyBits(RsaKey* key) #if !defined(WOLFSSH_NO_RSA) || !defined(WOLFSSH_NO_ED25519) \ - || !defined(WOLFSSH_NO_MLDSA) + || !defined(WOLFSSH_NO_MLDSA) || defined(WOLFSSH_CERTS) /* Compare the format identifier at the start of a public key blob * against the expected name (RFC 4253 section 6.6). */ static int GetPubKeyFormat(const char* expected, const byte* pubKey, @@ -8092,8 +8092,16 @@ static int ParsePubKeyCert(WOLFSSH* ssh, byte* in, word32 inSz, byte** out, int ret; byte* leaf = NULL; word32 leafSz = 0; + word32 fmtIdx = 0; + const char* fmt = IdToName(ssh->handshake->pubKeyId); - ret = ParseCertChainVerify(ssh, in, inSz, &leaf, &leafSz); + /* The blob's name must be the negotiated algorithm, as for plain keys; + * the host key callback reads it to tell a certificate. */ + if (fmt == NULL) + return WS_INVALID_ALGO_ID; + ret = GetPubKeyFormat(fmt, in, inSz, &fmtIdx); + if (ret == WS_SUCCESS) + ret = ParseCertChainVerify(ssh, in, inSz, &leaf, &leafSz); if (ret == WS_SUCCESS) { int error = 0; struct DecodedCert dCert; diff --git a/tests/kex.c b/tests/kex.c index 6c9231b5d..06487026a 100644 --- a/tests/kex.c +++ b/tests/kex.c @@ -260,6 +260,7 @@ static int wolfSSH_KexTest_Connect(const char* kex) WaitTcpReady(&ready); ADD_ARG(clientArgv, clientArgc, "client"); + ADD_ARG(clientArgv, clientArgc, "-Y"); ADD_ARG(clientArgv, clientArgc, "-u"); ADD_ARG(clientArgv, clientArgc, "jill"); ADD_ARG(clientArgv, clientArgc, "-C"); @@ -347,6 +348,7 @@ static int wolfSSH_KexTest_Ed25519HostKey(void) WaitTcpReady(&ready); ADD_ARG(clientArgv, clientArgc, "client"); + ADD_ARG(clientArgv, clientArgc, "-Y"); ADD_ARG(clientArgv, clientArgc, "-u"); ADD_ARG(clientArgv, clientArgc, "jill"); #if !defined(USE_WINDOWS_API) && !defined(WOLFSSH_ZEPHYR) @@ -423,6 +425,7 @@ static int wolfSSH_KexTest_MlDsaHostKey(const char* keyName) WaitTcpReady(&ready); ADD_ARG(clientArgv, clientArgc, "client"); + ADD_ARG(clientArgv, clientArgc, "-Y"); ADD_ARG(clientArgv, clientArgc, "-u"); ADD_ARG(clientArgv, clientArgc, "jill"); #if !defined(USE_WINDOWS_API) && !defined(WOLFSSH_ZEPHYR) diff --git a/tests/sftp.c b/tests/sftp.c index 681fc9c6f..a8fcdd1be 100644 --- a/tests/sftp.c +++ b/tests/sftp.c @@ -516,6 +516,7 @@ int wolfSSH_SftpTest(int flag) argsCount = 0; argsClient[argsCount++] = "."; + argsClient[argsCount++] = "-Y"; argsClient[argsCount++] = "-u"; argsClient[argsCount++] = "jill"; argsClient[argsCount++] = "-P"; diff --git a/tests/testsuite.c b/tests/testsuite.c index 77764bdba..419cc3f13 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -42,6 +42,7 @@ #include #include "examples/echoserver/echoserver.h" #include "examples/client/client.h" +#include "examples/client/common.h" #include "tests/testsuite.h" #if defined(WOLFSSH_SFTP) && !defined(SINGLE_THREADED) @@ -90,7 +91,7 @@ static int tsClientUserAuth(byte authType, WS_UserAuthData* authData, void* ctx) } -#define NUMARGS 5 +#define NUMARGS 6 #define ARGLEN 64 static void wolfSSH_EchoTest(void) @@ -104,7 +105,7 @@ static void wolfSSH_EchoTest(void) { sA[0], sA[1], sA[2], sA[3], sA[4] }; char cA[NUMARGS][ARGLEN]; char *clientArgv[NUMARGS] = - { cA[0], cA[1], cA[2], cA[3], cA[4] }; + { cA[0], cA[1], cA[2], cA[3], cA[4], cA[5] }; int serverArgc = 0; int clientArgc = 0; @@ -128,6 +129,7 @@ static void wolfSSH_EchoTest(void) WaitTcpReady(&ready); WSTRNCPY(cA[clientArgc++], "client", ARGLEN); + WSTRNCPY(cA[clientArgc++], "-Y", ARGLEN); WSTRNCPY(cA[clientArgc++], "-u", ARGLEN); WSTRNCPY(cA[clientArgc++], "jill", ARGLEN); #if !defined(USE_WINDOWS_API) && !defined(WOLFSSH_ZEPHYR) @@ -191,6 +193,7 @@ static void wolfSSH_EchoTest_MultiPubKey(void) WaitTcpReady(&ready); WSTRNCPY(cA[clientArgc++], "client", ARGLEN); + WSTRNCPY(cA[clientArgc++], "-Y", ARGLEN); WSTRNCPY(cA[clientArgc++], "-u", ARGLEN); WSTRNCPY(cA[clientArgc++], "hansel", ARGLEN); WSTRNCPY(cA[clientArgc++], "-j", ARGLEN); @@ -228,6 +231,98 @@ static void wolfSSH_EchoTest_MultiPubKey(void) #endif /* WOLFSSH_SHELL */ +#if defined(WOLFSSH_CERTS) && !defined(NO_FILESYSTEM) && \ + !defined(WOLFSSH_ZEPHYR) + +static void PutU32(word32 u32, byte* c) +{ + c[0] = (byte)(u32 >> 24); + c[1] = (byte)(u32 >> 16); + c[2] = (byte)(u32 >> 8); + c[3] = (byte)u32; +} + +/* Checks an RFC 6187 blob of certName against host, expecting expect. */ +static void CertHostCheck(const char* certName, const char* host, int expect) +{ + static const char keyType[] = "x509v3-ecdsa-sha2-nistp256"; + byte blob[2048]; + byte cert[1536]; + word32 certSz, typeSz = (word32)sizeof(keyType) - 1, idx = 0; + WFILE* f = WBADFILE; + ClientHostCtx hostCtx; + + if (WFOPEN(NULL, &f, certName, "rb") != 0 || f == WBADFILE) + err_sys("couldn't open the test certificate"); + certSz = (word32)WFREAD(NULL, cert, 1, sizeof(cert), f); + WFCLOSE(NULL, f); + if (certSz == 0 || certSz == sizeof(cert)) + err_sys("couldn't read the test certificate"); + + PutU32(typeSz, blob + idx); + idx += 4; + WMEMCPY(blob + idx, keyType, typeSz); + idx += typeSz; + PutU32(1, blob + idx); /* certificate count */ + idx += 4; + PutU32(certSz, blob + idx); + idx += 4; + WMEMCPY(blob + idx, cert, certSz); + idx += certSz; + PutU32(0, blob + idx); /* OCSP response count */ + idx += 4; + + hostCtx.host = host; + hostCtx.port = 22; + if (ClientPublicKeyCheck(blob, idx, &hostCtx) != expect) { + printf("host certificate %s checked for %s, expected %d\n", + certName, host, expect); + err_sys("host certificate check failed"); + } +} + +/* The leaf certificate must name the host. */ +static void wolfSSH_CertHostTest(void) +{ + /* DNS:example, IP Address:127.0.0.1, CN=www.wolfssl.com */ + const char* sanCert = "./keys/server-cert.der"; + /* No subjectAltName, CN=www.wolfssl.com */ + const char* cnCert = "./keys/ca-cert-ecc.der"; + /* email:server@example.com only, CN=www.wolfssl.com */ + const char* emailCert = "./keys/server-cert-email-san.der"; + + printf("testing host certificate names\n"); + CertHostCheck(sanCert, "example", 0); + CertHostCheck(sanCert, "EXAMPLE", 0); +#if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME) || \ + (LIBWOLFSSL_VERSION_HEX >= 0x05009002) + /* Older wolfSSL drops an IP SAN unless asked to keep it. */ + CertHostCheck(sanCert, "127.0.0.1", 0); +#endif + CertHostCheck(sanCert, "127.0.0.2", -1); + CertHostCheck(sanCert, "::ffff:127.0.0.1", -1); + CertHostCheck(sanCert, "examples", -1); + CertHostCheck(sanCert, "exampl", -1); + /* A SAN rules out the CN. */ + CertHostCheck(sanCert, "www.wolfssl.com", -1); + CertHostCheck(cnCert, "www.wolfssl.com", 0); + CertHostCheck(cnCert, "example", -1); + /* So does an email-only SAN. */ + CertHostCheck(emailCert, "www.wolfssl.com", -1); + + /* -X accepts a certificate for another host. */ + ClientIPOverride(1); + CertHostCheck(sanCert, "127.0.0.2", 0); + ClientIPOverride(0); + + /* -Y does not waive the certificate's name. */ + ClientAcceptAnyHostKey(1); + CertHostCheck(sanCert, "127.0.0.2", -1); + ClientAcceptAnyHostKey(0); +} +#endif + + int wolfSSH_TestsuiteTest(int argc, char** argv) { (void)argc; @@ -254,6 +349,11 @@ int wolfSSH_TestsuiteTest(int argc, char** argv) ChangeToWolfSshRoot(); #endif +#if defined(WOLFSSH_CERTS) && !defined(NO_FILESYSTEM) && \ + !defined(WOLFSSH_ZEPHYR) + wolfSSH_CertHostTest(); +#endif + #ifdef WOLFSSH_SHELL wolfSSH_EchoTest(); wolfSSH_EchoTest_MultiPubKey(); diff --git a/tests/unit.c b/tests/unit.c index 7d3ec5911..522195f52 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -26170,9 +26170,23 @@ static int test_ParseECCPubKeyCert(void) result = -5; } } + /* a chain not named for the negotiated algorithm is rejected: the + * host key callback reads the name to decide whether it is a cert */ + if (result == 0) { + WMEMCPY(blob + LENGTH_SZ, "X", 1); + ssh->handshake->pubKeyId = ID_X509V3_ECDSA_SHA2_NISTP256; + ret = wolfSSH_TestParseECCPubKeyCert(ssh, blob, blobSz); + if (ret != WS_INVALID_ALGO_ID) { + printf("ParseECCPubKeyCert: relabelled blob ret %d\n", ret); + result = -8; + } + WMEMCPY(blob + LENGTH_SZ, "x", 1); + } #ifndef WOLFSSH_NO_ECDSA_SHA2_NISTP384 - /* P-256 leaf offered for a negotiated P-384 algorithm is rejected */ + /* P-256 leaf offered for a negotiated P-384 algorithm, and named for + * it, is rejected */ if (result == 0) { + WMEMCPY(blob + LENGTH_SZ + WSTRLEN(algoName) - 3, "384", 3); ssh->handshake->pubKeyId = ID_X509V3_ECDSA_SHA2_NISTP384; ret = wolfSSH_TestParseECCPubKeyCert(ssh, blob, blobSz); if (ret != WS_INVALID_PRIME_CURVE) {