portfwd: check the host key against known_hosts

portfwd links the example client's common code and registers its host
key check, with the SSH server's host name as the context. -Y accepts
any key, with a warning.

- Pass -Y in fwd.test and fwd-bulk.test
- Cover a refused unknown key in client-hostkey.test

Issue: F-10587
master
John Safranek 2026-11-11 09:14:28 -08:00 committed by philljj
parent 799b9f38ca
commit e08d96dad9
6 changed files with 42 additions and 12 deletions

View File

@ -207,6 +207,8 @@ The portfwd tool accepts the following command line options:
-f <num> host port to forward from (REQUIRED)
-T <host> host to forward to, default to host
-t <num> port to forward to (REQUIRED)
-Y accept any host key, insecure; otherwise it must be in
~/.ssh/known_hosts
scpclient

View File

@ -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

View File

@ -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 <num> 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 host 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);
}
@ -498,7 +499,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 +553,10 @@ THREAD_RETURN WOLFSSH_THREAD portfwd_worker(void* args)
reverse = 1;
break;
case 'Y':
ClientAcceptAnyHostKey(1);
break;
case '?':
ShowUsage();
exit(EXIT_SUCCESS);
@ -619,7 +625,7 @@ 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.");
wolfSSH_SetPublicKeyCheckCtx(ssh, (void*)host);
wolfSSH_CTX_SetChannelOpenRespCb(ctx,
wsChannelOpenConfCb, wsChannelOpenFailCb);
wolfSSH_SetChannelOpenCtx(ssh, (void*)"port forward");

View File

@ -133,6 +133,26 @@ 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"
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

View File

@ -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=$!

View File

@ -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]