07d1ba
diff --color -rup a/sshconnect2.c b/sshconnect2.c
07d1ba
--- a/sshconnect2.c	2022-07-11 17:00:02.618575727 +0200
07d1ba
+++ b/sshconnect2.c	2022-07-11 17:03:05.096085690 +0200
07d1ba
@@ -2288,9 +2288,9 @@ userauth_hostbased(struct ssh *ssh)
07d1ba
 			if (authctxt->sensitive->keys[i] == NULL ||
07d1ba
 			    authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
07d1ba
 				continue;
07d1ba
-			if (match_pattern_list(
07d1ba
+			if (!sshkey_match_keyname_to_sigalgs(
07d1ba
 			    sshkey_ssh_name(authctxt->sensitive->keys[i]),
07d1ba
-			    authctxt->active_ktype, 0) != 1)
07d1ba
+			    authctxt->active_ktype))
07d1ba
 				continue;
07d1ba
 			/* we take and free the key */
07d1ba
 			private = authctxt->sensitive->keys[i];
07d1ba
@@ -2316,7 +2316,8 @@ userauth_hostbased(struct ssh *ssh)
07d1ba
 		error_f("sshkey_fingerprint failed");
07d1ba
 		goto out;
07d1ba
 	}
07d1ba
-	debug_f("trying hostkey %s %s", sshkey_ssh_name(private), fp);
07d1ba
+	debug_f("trying hostkey %s %s using sigalg %s",
07d1ba
+		sshkey_ssh_name(private), fp, authctxt->active_ktype);
07d1ba
 
07d1ba
 	/* figure out a name for the client host */
07d1ba
 	lname = get_local_name(ssh_packet_get_connection_in(ssh));
07d1ba
diff --color -rup a/sshkey.c b/sshkey.c
07d1ba
--- a/sshkey.c	2022-07-11 17:00:02.609575554 +0200
07d1ba
+++ b/sshkey.c	2022-07-11 17:12:30.905976443 +0200
07d1ba
@@ -252,6 +252,29 @@ sshkey_ecdsa_nid_from_name(const char *n
07d1ba
 	return -1;
07d1ba
 }
07d1ba
 
07d1ba
+int
07d1ba
+sshkey_match_keyname_to_sigalgs(const char *keyname, const char *sigalgs)
07d1ba
+{
07d1ba
+	int ktype;
07d1ba
+
07d1ba
+	if (sigalgs == NULL || *sigalgs == '\0' ||
07d1ba
+	    (ktype = sshkey_type_from_name(keyname)) == KEY_UNSPEC)
07d1ba
+		return 0;
07d1ba
+	else if (ktype == KEY_RSA) {
07d1ba
+		return match_pattern_list("ssh-rsa", sigalgs, 0) == 1 ||
07d1ba
+		    match_pattern_list("rsa-sha2-256", sigalgs, 0) == 1 ||
07d1ba
+		    match_pattern_list("rsa-sha2-512", sigalgs, 0) == 1;
07d1ba
+	} else if (ktype == KEY_RSA_CERT) {
07d1ba
+		return match_pattern_list("ssh-rsa-cert-v01@openssh.com",
07d1ba
+		    sigalgs, 0) == 1 ||
07d1ba
+		    match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
07d1ba
+		    sigalgs, 0) == 1 ||
07d1ba
+		    match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
07d1ba
+		    sigalgs, 0) == 1;
07d1ba
+	} else
07d1ba
+		return match_pattern_list(keyname, sigalgs, 0) == 1;
07d1ba
+}
07d1ba
+
07d1ba
 char *
07d1ba
 sshkey_alg_list(int certs_only, int plain_only, int include_sigonly, char sep)
07d1ba
 {
07d1ba
diff --color -rup a/sshkey.h b/sshkey.h
07d1ba
--- a/sshkey.h	2022-07-11 17:00:02.603575438 +0200
07d1ba
+++ b/sshkey.h	2022-07-11 17:13:01.052556879 +0200
07d1ba
@@ -194,6 +194,10 @@ int	 sshkey_is_cert(const struct sshkey
07d1ba
 int	 sshkey_is_sk(const struct sshkey *);
07d1ba
 int	 sshkey_type_is_cert(int);
07d1ba
 int	 sshkey_type_plain(int);
07d1ba
+
07d1ba
+/* Returns non-zero if key name match sigalgs pattern list. (handles RSA) */
07d1ba
+int	 sshkey_match_keyname_to_sigalgs(const char *, const char *);
07d1ba
+
07d1ba
 int	 sshkey_to_certified(struct sshkey *);
07d1ba
 int	 sshkey_drop_cert(struct sshkey *);
07d1ba
 int	 sshkey_cert_copy(const struct sshkey *, struct sshkey *);
07d1ba
diff --color -rup a/ssh-keysign.c b/ssh-keysign.c
07d1ba
--- a/ssh-keysign.c	2021-08-20 06:03:49.000000000 +0200
07d1ba
+++ b/ssh-keysign.c	2022-07-11 17:00:23.306973667 +0200
07d1ba
@@ -62,7 +62,7 @@
07d1ba
 extern char *__progname;
07d1ba
 
07d1ba
 static int
07d1ba
-valid_request(struct passwd *pw, char *host, struct sshkey **ret,
07d1ba
+valid_request(struct passwd *pw, char *host, struct sshkey **ret, char **pkalgp,
07d1ba
     u_char *data, size_t datalen)
07d1ba
 {
07d1ba
 	struct sshbuf *b;
07d1ba
@@ -75,6 +75,8 @@ valid_request(struct passwd *pw, char *h
07d1ba
 
07d1ba
 	if (ret != NULL)
07d1ba
 		*ret = NULL;
07d1ba
+	if (pkalgp != NULL)
07d1ba
+		*pkalgp = NULL;
07d1ba
 	fail = 0;
07d1ba
 
07d1ba
 	if ((b = sshbuf_from(data, datalen)) == NULL)
07d1ba
@@ -122,8 +124,6 @@ valid_request(struct passwd *pw, char *h
07d1ba
 		fail++;
07d1ba
 	} else if (key->type != pktype)
07d1ba
 		fail++;
07d1ba
-	free(pkalg);
07d1ba
-	free(pkblob);
07d1ba
 
07d1ba
 	/* client host name, handle trailing dot */
07d1ba
 	if ((r = sshbuf_get_cstring(b, &p, &len)) != 0)
07d1ba
@@ -154,8 +154,19 @@ valid_request(struct passwd *pw, char *h
07d1ba
 
07d1ba
 	if (fail)
07d1ba
 		sshkey_free(key);
07d1ba
-	else if (ret != NULL)
07d1ba
-		*ret = key;
07d1ba
+	else {
07d1ba
+		if (ret != NULL) {
07d1ba
+			*ret = key;
07d1ba
+			key = NULL;
07d1ba
+		}
07d1ba
+		if (pkalgp != NULL) {
07d1ba
+			*pkalgp = pkalg;
07d1ba
+			pkalg = NULL;
07d1ba
+		}
07d1ba
+	}
07d1ba
+	sshkey_free(key);
07d1ba
+	free(pkalg);
07d1ba
+	free(pkblob);
07d1ba
 
07d1ba
 	return (fail ? -1 : 0);
07d1ba
 }
07d1ba
@@ -170,7 +181,7 @@ main(int argc, char **argv)
07d1ba
 	struct passwd *pw;
07d1ba
 	int r, key_fd[NUM_KEYTYPES], i, found, version = 2, fd;
07d1ba
 	u_char *signature, *data, rver;
07d1ba
-	char *host, *fp;
07d1ba
+	char *host, *fp, *pkalg;
07d1ba
 	size_t slen, dlen;
07d1ba
 
07d1ba
 	if (pledge("stdio rpath getpw dns id", NULL) != 0)
07d1ba
@@ -258,7 +269,7 @@ main(int argc, char **argv)
07d1ba
 
07d1ba
 	if ((r = sshbuf_get_string(b, &data, &dlen)) != 0)
07d1ba
 		fatal_r(r, "%s: buffer error", __progname);
07d1ba
-	if (valid_request(pw, host, &key, data, dlen) < 0)
07d1ba
+	if (valid_request(pw, host, &key, &pkalg, data, dlen) < 0)
07d1ba
 		fatal("%s: not a valid request", __progname);
07d1ba
 	free(host);
07d1ba
 
07d1ba
@@ -279,7 +290,7 @@ main(int argc, char **argv)
07d1ba
 	}
07d1ba
 
07d1ba
 	if ((r = sshkey_sign(keys[i], &signature, &slen, data, dlen,
07d1ba
-	    NULL, NULL, NULL, 0)) != 0)
07d1ba
+	    pkalg, NULL, NULL, 0)) != 0)
07d1ba
 		fatal_r(r, "%s: sshkey_sign failed", __progname);
07d1ba
 	free(data);
07d1ba