07d1ba
diff --color -ru a/clientloop.c b/clientloop.c
07d1ba
--- a/clientloop.c	2022-06-29 16:35:06.677597259 +0200
07d1ba
+++ b/clientloop.c	2022-06-29 16:40:29.737926205 +0200
07d1ba
@@ -116,6 +116,9 @@
07d1ba
 #include "ssh-gss.h"
07d1ba
 #endif
9070b3
 
07d1ba
+/* Permitted RSA signature algorithms for UpdateHostkeys proofs */
07d1ba
+#define HOSTKEY_PROOF_RSA_ALGS	"rsa-sha2-512,rsa-sha2-256"
9070b3
+
07d1ba
 /* import options */
07d1ba
 extern Options options;
07d1ba
 
07d1ba
@@ -2110,8 +2113,10 @@
07d1ba
 	struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
07d1ba
 	size_t i, ndone;
07d1ba
 	struct sshbuf *signdata;
07d1ba
-	int r, kexsigtype, use_kexsigtype;
07d1ba
+	int r, plaintype;
07d1ba
 	const u_char *sig;
07d1ba
+	const char *rsa_kexalg = NULL;
07d1ba
+	char *alg = NULL;
07d1ba
 	size_t siglen;
07d1ba
 
07d1ba
 	if (ctx->nnew == 0)
07d1ba
@@ -2122,9 +2127,9 @@
07d1ba
 		hostkeys_update_ctx_free(ctx);
07d1ba
 		return;
07d1ba
 	}
07d1ba
-	kexsigtype = sshkey_type_plain(
07d1ba
-	    sshkey_type_from_name(ssh->kex->hostkey_alg));
07d1ba
-
07d1ba
+	if (sshkey_type_plain(sshkey_type_from_name(
07d1ba
+	    ssh->kex->hostkey_alg)) == KEY_RSA)
07d1ba
+		rsa_kexalg = ssh->kex->hostkey_alg;
07d1ba
 	if ((signdata = sshbuf_new()) == NULL)
07d1ba
 		fatal_f("sshbuf_new failed");
07d1ba
 	/*
07d1ba
@@ -2135,6 +2140,7 @@
07d1ba
 	for (ndone = i = 0; i < ctx->nkeys; i++) {
07d1ba
 		if (ctx->keys_match[i])
07d1ba
 			continue;
07d1ba
+		plaintype = sshkey_type_plain(ctx->keys[i]->type);
07d1ba
 		/* Prepare data to be signed: session ID, unique string, key */
07d1ba
 		sshbuf_reset(signdata);
07d1ba
 		if ( (r = sshbuf_put_cstring(signdata,
07d1ba
@@ -2148,19 +2154,33 @@
07d1ba
 			error_fr(r, "parse sig");
07d1ba
 			goto out;
07d1ba
 		}
07d1ba
+		if ((r = sshkey_get_sigtype(sig, siglen, &alg)) != 0) {
07d1ba
+			error_fr(r, "server gave unintelligible signature "
07d1ba
+				"for %s key %zu", sshkey_type(ctx->keys[i]), i);
07d1ba
+			goto out;
9070b3
+		}
07d1ba
 		/*
07d1ba
-		 * For RSA keys, prefer to use the signature type negotiated
07d1ba
-		 * during KEX to the default (SHA1).
07d1ba
+		 * Special case for RSA keys: if a RSA hostkey was negotiated,
07d1ba
+		 * then use its signature type for verification of RSA hostkey
07d1ba
+		 * proofs. Otherwise, accept only RSA-SHA256/512 signatures.
07d1ba
 		 */
07d1ba
-		use_kexsigtype = kexsigtype == KEY_RSA &&
07d1ba
-		    sshkey_type_plain(ctx->keys[i]->type) == KEY_RSA;
07d1ba
-		debug3_f("verify %s key %zu using %s sigalg",
07d1ba
-		    sshkey_type(ctx->keys[i]), i,
07d1ba
-		    use_kexsigtype ? ssh->kex->hostkey_alg : "default");
07d1ba
+		if (plaintype == KEY_RSA && rsa_kexalg == NULL &&
07d1ba
+		    match_pattern_list(alg, HOSTKEY_PROOF_RSA_ALGS, 0) != 1) {
07d1ba
+			debug_f("server used untrusted RSA signature algorithm "
07d1ba
+				"%s for key %zu, disregarding", alg, i);
07d1ba
+			free(alg);
07d1ba
+			/* zap the key from the list */
07d1ba
+			sshkey_free(ctx->keys[i]);
07d1ba
+			ctx->keys[i] = NULL;
07d1ba
+			ndone++;
07d1ba
+			continue;
9070b3
+		}
07d1ba
+		debug3_f("verify %s key %zu using sigalg %s",
07d1ba
+			sshkey_type(ctx->keys[i]), i, alg);
07d1ba
+		free(alg);
07d1ba
 		if ((r = sshkey_verify(ctx->keys[i], sig, siglen,
07d1ba
 		    sshbuf_ptr(signdata), sshbuf_len(signdata),
07d1ba
-		    use_kexsigtype ? ssh->kex->hostkey_alg : NULL, 0,
07d1ba
-		    NULL)) != 0) {
07d1ba
+		    plaintype == KEY_RSA ? rsa_kexalg : NULL, 0, NULL)) != 0) {
07d1ba
 			error_fr(r, "server gave bad signature for %s key %zu",
07d1ba
 			    sshkey_type(ctx->keys[i]), i);
07d1ba
 			goto out;
07d1ba
diff --git a/hostfile.c b/hostfile.c
07d1ba
index a035b381..bd49e3ac 100644
07d1ba
--- a/hostfile.c
07d1ba
+++ b/hostfile.c
07d1ba
@@ -642,7 +642,7 @@ hostfile_replace_entries(const char *filename, const char *host, const char *ip,
07d1ba
 	/* Re-add the requested keys */
07d1ba
 	want = HKF_MATCH_HOST | (ip == NULL ? 0 : HKF_MATCH_IP);
07d1ba
 	for (i = 0; i < nkeys; i++) {
07d1ba
-		if ((want & ctx.match_keys[i]) == want)
07d1ba
+		if (keys[i] == NULL || (want & ctx.match_keys[i]) == want)
07d1ba
 			continue;
07d1ba
 		if ((fp = sshkey_fingerprint(keys[i], hash_alg,
07d1ba
 		    SSH_FP_DEFAULT)) == NULL) {
07d1ba
diff --color -ru a/kex.c b/kex.c
07d1ba
--- a/kex.c	2022-06-29 16:35:06.775599179 +0200
07d1ba
+++ b/kex.c	2022-06-29 16:42:00.839710940 +0200
07d1ba
@@ -959,6 +959,18 @@
07d1ba
 	return (1);
07d1ba
 }
07d1ba
 
07d1ba
+/* returns non-zero if proposal contains any algorithm from algs */
07d1ba
+static int
07d1ba
+has_any_alg(const char *proposal, const char *algs)
07d1ba
+{
07d1ba
+	char *cp;
9070b3
+
07d1ba
+	if ((cp = match_list(proposal, algs, NULL)) == NULL)
07d1ba
+		return 0;
07d1ba
+	free(cp);
07d1ba
+	return 1;
07d1ba
+}
9070b3
+
07d1ba
 static int
07d1ba
 kex_choose_conf(struct ssh *ssh)
07d1ba
 {
07d1ba
@@ -994,6 +1006,16 @@
07d1ba
 		free(ext);
07d1ba
 	}
07d1ba
 
07d1ba
+	/* Check whether client supports rsa-sha2 algorithms */
07d1ba
+	if (kex->server && (kex->flags & KEX_INITIAL)) {
07d1ba
+		if (has_any_alg(peer[PROPOSAL_SERVER_HOST_KEY_ALGS],
07d1ba
+		    "rsa-sha2-256,rsa-sha2-256-cert-v01@openssh.com"))
07d1ba
+			kex->flags |= KEX_RSA_SHA2_256_SUPPORTED;
07d1ba
+		if (has_any_alg(peer[PROPOSAL_SERVER_HOST_KEY_ALGS],
07d1ba
+		    "rsa-sha2-512,rsa-sha2-512-cert-v01@openssh.com"))
9070b3
+			kex->flags |= KEX_RSA_SHA2_512_SUPPORTED;
9070b3
+	}
9070b3
+
9070b3
 	/* Algorithm Negotiation */
9070b3
 	if ((r = choose_kex(kex, cprop[PROPOSAL_KEX_ALGS],
9070b3
 	    sprop[PROPOSAL_KEX_ALGS])) != 0) {
07d1ba
diff --color -ru a/kex.h b/kex.h
07d1ba
--- a/kex.h	2022-06-29 16:35:06.766599003 +0200
07d1ba
+++ b/kex.h	2022-06-29 16:42:24.199168567 +0200
07d1ba
@@ -116,6 +116,8 @@
9070b3
 
9070b3
 #define KEX_INIT_SENT	0x0001
9070b3
 #define KEX_INITIAL	0x0002
07d1ba
+#define KEX_RSA_SHA2_256_SUPPORTED      0x0008 /* only set in server for now */
07d1ba
+#define KEX_RSA_SHA2_512_SUPPORTED      0x0010 /* only set in server for now */
9070b3
 
9070b3
 struct sshenc {
9070b3
 	char	*name;
07d1ba
diff --color -ru a/serverloop.c b/serverloop.c
07d1ba
--- a/serverloop.c	2021-08-20 06:03:49.000000000 +0200
07d1ba
+++ b/serverloop.c	2022-06-29 16:45:05.902336428 +0200
07d1ba
@@ -684,16 +684,18 @@
9070b3
 	struct sshbuf *resp = NULL;
9070b3
 	struct sshbuf *sigbuf = NULL;
9070b3
 	struct sshkey *key = NULL, *key_pub = NULL, *key_prv = NULL;
9070b3
-	int r, ndx, kexsigtype, use_kexsigtype, success = 0;
9070b3
+	int r, ndx, success = 0;
9070b3
 	const u_char *blob;
07d1ba
+	const char *sigalg, *kex_rsa_sigalg = NULL;
9070b3
 	u_char *sig = 0;
9070b3
 	size_t blen, slen;
07d1ba
 
9070b3
 	if ((resp = sshbuf_new()) == NULL || (sigbuf = sshbuf_new()) == NULL)
9070b3
 		fatal_f("sshbuf_new");
9070b3
 
9070b3
-	kexsigtype = sshkey_type_plain(
9070b3
-	    sshkey_type_from_name(ssh->kex->hostkey_alg));
07d1ba
+	if (sshkey_type_plain(sshkey_type_from_name(
07d1ba
+	    ssh->kex->hostkey_alg)) == KEY_RSA)
07d1ba
+		kex_rsa_sigalg = ssh->kex->hostkey_alg;
9070b3
 	while (ssh_packet_remaining(ssh) > 0) {
9070b3
 		sshkey_free(key);
9070b3
 		key = NULL;
07d1ba
@@ -726,16 +728,24 @@
9070b3
 		 * For RSA keys, prefer to use the signature type negotiated
9070b3
 		 * during KEX to the default (SHA1).
9070b3
 		 */
9070b3
-		use_kexsigtype = kexsigtype == KEY_RSA &&
9070b3
-		    sshkey_type_plain(key->type) == KEY_RSA;
07d1ba
+		sigalg = NULL;
9070b3
+		if (sshkey_type_plain(key->type) == KEY_RSA) {
07d1ba
+			if (kex_rsa_sigalg != NULL)
07d1ba
+				sigalg = kex_rsa_sigalg;
07d1ba
+			else if (ssh->kex->flags & KEX_RSA_SHA2_512_SUPPORTED)
07d1ba
+				sigalg = "rsa-sha2-512";
07d1ba
+			else if (ssh->kex->flags & KEX_RSA_SHA2_256_SUPPORTED)
07d1ba
+				sigalg = "rsa-sha2-256";
9070b3
+		}
07d1ba
+		debug3_f("sign %s key (index %d) using sigalg %s",
07d1ba
+		sshkey_type(key), ndx, sigalg == NULL ? "default" : sigalg);
9070b3
 		if ((r = sshbuf_put_cstring(sigbuf,
9070b3
 		    "hostkeys-prove-00@openssh.com")) != 0 ||
9070b3
 		    (r = sshbuf_put_stringb(sigbuf,
07d1ba
 		    ssh->kex->session_id)) != 0 ||
9070b3
 		    (r = sshkey_puts(key, sigbuf)) != 0 ||
9070b3
 		    (r = ssh->kex->sign(ssh, key_prv, key_pub, &sig, &slen,
07d1ba
-		    sshbuf_ptr(sigbuf), sshbuf_len(sigbuf),
9070b3
-		    use_kexsigtype ? ssh->kex->hostkey_alg : NULL)) != 0 ||
07d1ba
+		    sshbuf_ptr(sigbuf), sshbuf_len(sigbuf), sigalg)) != 0 ||
9070b3
 		    (r = sshbuf_put_string(resp, sig, slen)) != 0) {
9070b3
 			error_fr(r, "assemble signature");
9070b3
 			goto out;