bmh10 / rpms / openssh

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