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