f561c68
diff -up openssh-9.3p1/regress/hostkey-agent.sh.xxx openssh-9.3p1/regress/hostkey-agent.sh
f561c68
--- openssh-9.3p1/regress/hostkey-agent.sh.xxx	2023-05-29 18:15:56.311236887 +0200
f561c68
+++ openssh-9.3p1/regress/hostkey-agent.sh	2023-05-29 18:16:07.598503551 +0200
f561c68
@@ -17,8 +17,21 @@ trace "make CA key"
03150f6
 
03150f6
 ${SSHKEYGEN} -qt ed25519 -f $OBJ/agent-ca -N '' || fatal "ssh-keygen CA"
9fd6981
 
9fd6981
+PUBKEY_ACCEPTED_ALGOS=`$SSH -G "example.com" | \
9fd6981
+    grep -i "PubkeyAcceptedAlgorithms" | cut -d ' ' -f2- | tr "," "|"`
9fd6981
+SSH_ACCEPTED_KEYTYPES=`echo "$SSH_KEYTYPES" | egrep "$PUBKEY_ACCEPTED_ALGOS"`
f561c68
+echo $PUBKEY_ACCEPTED_ALGOS | grep "rsa"
f561c68
+r=$?
f561c68
+if [ $r == 0 ]; then
f561c68
+echo $SSH_ACCEPTED_KEYTYPES | grep "rsa"
f561c68
+r=$?
f561c68
+if [ $r -ne 0 ]; then
f561c68
+SSH_ACCEPTED_KEYTYPES="$SSH_ACCEPTED_KEYTYPES ssh-rsa"
f561c68
+fi
f561c68
+fi
9fd6981
+
9fd6981
 trace "load hostkeys"
9fd6981
-for k in $SSH_KEYTYPES ; do
9fd6981
+for k in $SSH_ACCEPTED_KEYTYPES ; do
f561c68
 	${SSHKEYGEN} -qt $k -f $OBJ/agent-key.$k -N '' || fatal "ssh-keygen $k"
f561c68
 	${SSHKEYGEN} -s $OBJ/agent-ca -qh -n localhost-with-alias \
f561c68
 		-I localhost-with-alias $OBJ/agent-key.$k.pub || \
f561c68
@@ -32,12 +48,16 @@ rm $OBJ/agent-ca # Don't need CA private
03150f6
 
9fd6981
 unset SSH_AUTH_SOCK
9fd6981
 
03150f6
-for k in $SSH_KEYTYPES ; do
03150f6
+for k in $SSH_ACCEPTED_KEYTYPES ; do
f561c68
 	verbose "key type $k"
f561c68
+	hka=$k
f561c68
+	if [ $k = "ssh-rsa" ]; then
f561c68
+	   hka="rsa-sha2-512"
f561c68
+	fi
f561c68
 	cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy
f561c68
-	echo "HostKeyAlgorithms $k" >> $OBJ/sshd_proxy
f561c68
+	echo "HostKeyAlgorithms $hka" >> $OBJ/sshd_proxy
f561c68
 	echo "Hostkey $OBJ/agent-key.${k}" >> $OBJ/sshd_proxy
f561c68
-	opts="-oHostKeyAlgorithms=$k -F $OBJ/ssh_proxy"
f561c68
+	opts="-oHostKeyAlgorithms=$hka -F $OBJ/ssh_proxy"
f561c68
 	( printf 'localhost-with-alias,127.0.0.1,::1 ' ;
f561c68
 	  cat $OBJ/agent-key.$k.pub) > $OBJ/known_hosts
f561c68
 	SSH_CONNECTION=`${SSH} $opts host 'echo $SSH_CONNECTION'`
f561c68
@@ -50,15 +70,16 @@ for k in $SSH_KEYTYPES ; do
f561c68
 done
f561c68
 
f561c68
 SSH_CERTTYPES=`ssh -Q key-sig | grep 'cert-v01@openssh.com'`
f561c68
+SSH_ACCEPTED_CERTTYPES=`echo "$SSH_CERTTYPES" | egrep "$PUBKEY_ACCEPTED_ALGOS"`
f561c68
 
f561c68
 # Prepare sshd_proxy for certificates.
f561c68
 cp $OBJ/sshd_proxy.orig $OBJ/sshd_proxy
f561c68
 HOSTKEYALGS=""
f561c68
-for k in $SSH_CERTTYPES ; do
f561c68
+for k in $SSH_ACCEPTED_CERTTYPES ; do
f561c68
 	test -z "$HOSTKEYALGS" || HOSTKEYALGS="${HOSTKEYALGS},"
f561c68
 	HOSTKEYALGS="${HOSTKEYALGS}${k}"
f561c68
 done
f561c68
-for k in $SSH_KEYTYPES ; do
f561c68
+for k in $SSH_ACCEPTED_KEYTYPES ; do
f561c68
 	echo "Hostkey $OBJ/agent-key.${k}.pub" >> $OBJ/sshd_proxy
f561c68
 	echo "HostCertificate $OBJ/agent-key.${k}-cert.pub" >> $OBJ/sshd_proxy
f561c68
 	test -f $OBJ/agent-key.${k}.pub || fatal "no $k key"
f561c68
@@ -70,7 +93,7 @@ echo "HostKeyAlgorithms $HOSTKEYALGS" >>
f561c68
 ( printf '@cert-authority localhost-with-alias ' ;
f561c68
   cat $OBJ/agent-ca.pub) > $OBJ/known_hosts
f561c68
 
f561c68
-for k in $SSH_CERTTYPES ; do
f561c68
+for k in $SSH_ACCEPTED_CERTTYPES ; do
f561c68
 	verbose "cert type $k"
f561c68
 	opts="-oHostKeyAlgorithms=$k -F $OBJ/ssh_proxy"
f561c68
 	SSH_CONNECTION=`${SSH} $opts host 'echo $SSH_CONNECTION'`
f561c68
diff -up openssh-9.3p1/sshconnect2.c.xxx openssh-9.3p1/sshconnect2.c
f561c68
--- openssh-9.3p1/sshconnect2.c.xxx	2023-04-26 17:37:35.100827792 +0200
f561c68
+++ openssh-9.3p1/sshconnect2.c	2023-04-26 17:50:31.860748877 +0200
f561c68
@@ -221,7 +221,7 @@ ssh_kex2(struct ssh *ssh, char *host, st
f561c68
     const struct ssh_conn_info *cinfo)
9fd6981
 {
f561c68
 	char *myproposal[PROPOSAL_MAX];
f561c68
-	char *s, *all_key, *hkalgs = NULL;
f561c68
+	char *s, *all_key, *hkalgs = NULL, *filtered_algs = NULL;
9fd6981
 	int r, use_known_hosts_order = 0;
9fd6981
 
f561c68
 #if defined(GSSAPI) && defined(WITH_OPENSSL)
f561c68
@@ -260,9 +260,21 @@ ssh_kex2(struct ssh *ssh, char *host, st
f561c68
 	if (use_known_hosts_order)
f561c68
 		hkalgs = order_hostkeyalgs(host, hostaddr, port, cinfo);
f561c68
 
f561c68
+	filtered_algs = hkalgs ? match_filter_allowlist(hkalgs, options.pubkey_accepted_algos)
f561c68
+		               : match_filter_allowlist(options.hostkeyalgorithms,
f561c68
+				 options.pubkey_accepted_algos);
f561c68
+	if (filtered_algs == NULL) {
f561c68
+		if (hkalgs)
f561c68
+			fatal_f("No match between algorithms for %s (host %s) and pubkey accepted algorithms %s",
f561c68
+			       hkalgs, host, options.pubkey_accepted_algos);
f561c68
+		else
f561c68
+			fatal_f("No match between host key algorithms %s and pubkey accepted algorithms %s",
f561c68
+			        options.hostkeyalgorithms, options.pubkey_accepted_algos);
f561c68
+	}
f561c68
+
f561c68
 	kex_proposal_populate_entries(ssh, myproposal, s, options.ciphers,
f561c68
 	    options.macs, compression_alg_list(options.compression),
f561c68
-	    hkalgs ? hkalgs : options.hostkeyalgorithms);
f561c68
+	    filtered_algs);
9fd6981
 
9fd6981
 #if defined(GSSAPI) && defined(WITH_OPENSSL)
9fd6981
 	if (options.gss_keyex) {
f561c68
@@ -303,6 +315,7 @@ ssh_kex2(struct ssh *ssh, char *host, st
f561c68
 #endif
f561c68
 
f561c68
 	free(hkalgs);
f561c68
+	free(filtered_algs);
f561c68
 
f561c68
 	/* start key exchange */
f561c68
 	if ((r = kex_setup(ssh, myproposal)) != 0)