bmh10 / rpms / openssh

Forked from rpms/openssh 10 days ago
Clone

Blame SOURCES/openssh-8.7p1-minimize-sha1-use.patch

3a2fd7
diff --color -ru a/kex.c b/kex.c
3a2fd7
--- a/kex.c	2022-06-23 10:25:29.529922670 +0200
3a2fd7
+++ b/kex.c	2022-06-23 10:26:12.911762100 +0200
3a2fd7
@@ -906,6 +906,18 @@
3a2fd7
 	return (1);
3a2fd7
 }
3a2fd7
 
3a2fd7
+/* returns non-zero if proposal contains any algorithm from algs */
3a2fd7
+static int
3a2fd7
+has_any_alg(const char *proposal, const char *algs)
3a2fd7
+{
3a2fd7
+	char *cp;
3a2fd7
+
3a2fd7
+	if ((cp = match_list(proposal, algs, NULL)) == NULL)
3a2fd7
+		return 0;
3a2fd7
+	free(cp);
3a2fd7
+	return 1;
3a2fd7
+}
3a2fd7
+
3a2fd7
 static int
3a2fd7
 kex_choose_conf(struct ssh *ssh)
3a2fd7
 {
3a2fd7
@@ -941,6 +953,16 @@
3a2fd7
 		free(ext);
3a2fd7
 	}
3a2fd7
 
3a2fd7
+	/* Check whether client supports rsa-sha2 algorithms */
3a2fd7
+	if (kex->server && (kex->flags & KEX_INITIAL)) {
3a2fd7
+		if (has_any_alg(peer[PROPOSAL_SERVER_HOST_KEY_ALGS],
3a2fd7
+		    "rsa-sha2-256,rsa-sha2-256-cert-v01@openssh.com"))
3a2fd7
+			kex->flags |= KEX_RSA_SHA2_256_SUPPORTED;
3a2fd7
+		if (has_any_alg(peer[PROPOSAL_SERVER_HOST_KEY_ALGS],
3a2fd7
+		    "rsa-sha2-512,rsa-sha2-512-cert-v01@openssh.com"))
3a2fd7
+			kex->flags |= KEX_RSA_SHA2_512_SUPPORTED;
3a2fd7
+	}
3a2fd7
+
3a2fd7
 	/* Algorithm Negotiation */
3a2fd7
 	if ((r = choose_kex(kex, cprop[PROPOSAL_KEX_ALGS],
3a2fd7
 	    sprop[PROPOSAL_KEX_ALGS])) != 0) {
3a2fd7
diff --color -ru a/kex.h b/kex.h
3a2fd7
--- a/kex.h	2022-06-23 10:25:29.511922322 +0200
3a2fd7
+++ b/kex.h	2022-06-23 10:26:12.902761926 +0200
3a2fd7
@@ -117,6 +117,8 @@
3a2fd7
 
3a2fd7
 #define KEX_INIT_SENT	0x0001
3a2fd7
 #define KEX_INITIAL	0x0002
3a2fd7
+#define KEX_RSA_SHA2_256_SUPPORTED 	0x0008 /* only set in server for now */
3a2fd7
+#define KEX_RSA_SHA2_512_SUPPORTED 	0x0010 /* only set in server for now */
3a2fd7
 
3a2fd7
 struct sshenc {
3a2fd7
 	char	*name;
3a2fd7
diff --color -ru a/serverloop.c b/serverloop.c
3a2fd7
--- a/serverloop.c	2022-06-23 10:25:29.537922825 +0200
3a2fd7
+++ b/serverloop.c	2022-06-23 10:26:12.918762235 +0200
3a2fd7
@@ -736,16 +736,17 @@
3a2fd7
 	struct sshbuf *resp = NULL;
3a2fd7
 	struct sshbuf *sigbuf = NULL;
3a2fd7
 	struct sshkey *key = NULL, *key_pub = NULL, *key_prv = NULL;
3a2fd7
-	int r, ndx, kexsigtype, use_kexsigtype, success = 0;
3a2fd7
+	int r, ndx, success = 0;
3a2fd7
 	const u_char *blob;
3a2fd7
+	const char *sigalg, *kex_rsa_sigalg = NULL;
3a2fd7
 	u_char *sig = 0;
3a2fd7
 	size_t blen, slen;
3a2fd7
 
3a2fd7
 	if ((resp = sshbuf_new()) == NULL || (sigbuf = sshbuf_new()) == NULL)
3a2fd7
 		fatal("%s: sshbuf_new", __func__);
3a2fd7
-
3a2fd7
-	kexsigtype = sshkey_type_plain(
3a2fd7
-	    sshkey_type_from_name(ssh->kex->hostkey_alg));
3a2fd7
+	if (sshkey_type_plain(sshkey_type_from_name(
3a2fd7
+	    ssh->kex->hostkey_alg)) == KEY_RSA)
3a2fd7
+		kex_rsa_sigalg = ssh->kex->hostkey_alg;
3a2fd7
 	while (ssh_packet_remaining(ssh) > 0) {
3a2fd7
 		sshkey_free(key);
3a2fd7
 		key = NULL;
3a2fd7
@@ -780,16 +781,24 @@
3a2fd7
 		 * For RSA keys, prefer to use the signature type negotiated
3a2fd7
 		 * during KEX to the default (SHA1).
3a2fd7
 		 */
3a2fd7
-		use_kexsigtype = kexsigtype == KEY_RSA &&
3a2fd7
-		    sshkey_type_plain(key->type) == KEY_RSA;
3a2fd7
+		sigalg = NULL;
3a2fd7
+		if (sshkey_type_plain(key->type) == KEY_RSA) {
3a2fd7
+			if (kex_rsa_sigalg != NULL)
3a2fd7
+				sigalg = kex_rsa_sigalg;
3a2fd7
+			else if (ssh->kex->flags & KEX_RSA_SHA2_512_SUPPORTED)
3a2fd7
+				sigalg = "rsa-sha2-512";
3a2fd7
+			else if (ssh->kex->flags & KEX_RSA_SHA2_256_SUPPORTED)
3a2fd7
+				sigalg = "rsa-sha2-256";
3a2fd7
+		}
3a2fd7
+		debug3("%s: sign %s key (index %d) using sigalg %s", __func__,
3a2fd7
+			sshkey_type(key), ndx, sigalg == NULL ? "default" : sigalg);
3a2fd7
 		if ((r = sshbuf_put_cstring(sigbuf,
3a2fd7
 		    "hostkeys-prove-00@openssh.com")) != 0 ||
3a2fd7
 		    (r = sshbuf_put_string(sigbuf,
3a2fd7
 		    ssh->kex->session_id, ssh->kex->session_id_len)) != 0 ||
3a2fd7
 		    (r = sshkey_puts(key, sigbuf)) != 0 ||
3a2fd7
 		    (r = ssh->kex->sign(ssh, key_prv, key_pub, &sig, &slen,
3a2fd7
-		    sshbuf_ptr(sigbuf), sshbuf_len(sigbuf),
3a2fd7
-		    use_kexsigtype ? ssh->kex->hostkey_alg : NULL)) != 0 ||
3a2fd7
+		    sshbuf_ptr(sigbuf), sshbuf_len(sigbuf), sigalg)) != 0 ||
3a2fd7
 		    (r = sshbuf_put_string(resp, sig, slen)) != 0) {
3a2fd7
 			error("%s: couldn't prepare signature: %s",
3a2fd7
 			    __func__, ssh_err(r));
3a2fd7
diff --color -ru a/sshkey.c b/sshkey.c
3a2fd7
--- a/sshkey.c	2022-06-23 10:25:29.532922728 +0200
3a2fd7
+++ b/sshkey.c	2022-06-23 10:26:12.914762158 +0200
3a2fd7
@@ -82,7 +82,6 @@
3a2fd7
     struct sshbuf *buf, enum sshkey_serialize_rep);
3a2fd7
 static int sshkey_from_blob_internal(struct sshbuf *buf,
3a2fd7
     struct sshkey **keyp, int allow_cert);
3a2fd7
-static int get_sigtype(const u_char *sig, size_t siglen, char **sigtypep);
3a2fd7
 
3a2fd7
 /* Supported key types */
3a2fd7
 struct keytype {
3a2fd7
@@ -2092,7 +2091,8 @@
3a2fd7
 	if ((ret = sshkey_verify(key->cert->signature_key, sig, slen,
3a2fd7
 	    sshbuf_ptr(key->cert->certblob), signed_len, NULL, 0)) != 0)
3a2fd7
 		goto out;
3a2fd7
-	if ((ret = get_sigtype(sig, slen, &key->cert->signature_type)) != 0)
3a2fd7
+	if ((ret = sshkey_get_sigtype(sig, slen,
3a2fd7
+	    &key->cert->signature_type)) != 0)
3a2fd7
 		goto out;
3a2fd7
 
3a2fd7
 	/* Success */
3a2fd7
@@ -2394,8 +2394,8 @@
3a2fd7
 	return r;
3a2fd7
 }
3a2fd7
 
3a2fd7
-static int
3a2fd7
-get_sigtype(const u_char *sig, size_t siglen, char **sigtypep)
3a2fd7
+int
3a2fd7
+sshkey_get_sigtype(const u_char *sig, size_t siglen, char **sigtypep)
3a2fd7
 {
3a2fd7
 	int r;
3a2fd7
 	struct sshbuf *b = NULL;
3a2fd7
@@ -2477,7 +2477,7 @@
3a2fd7
 		return 0;
3a2fd7
 	if ((expected_alg = sshkey_sigalg_by_name(requested_alg)) == NULL)
3a2fd7
 		return SSH_ERR_INVALID_ARGUMENT;
3a2fd7
-	if ((r = get_sigtype(sig, siglen, &sigtype)) != 0)
3a2fd7
+	if ((r = sshkey_get_sigtype(sig, siglen, &sigtype)) != 0)
3a2fd7
 		return r;
3a2fd7
 	r = strcmp(expected_alg, sigtype) == 0;
3a2fd7
 	free(sigtype);
3a2fd7
@@ -2739,7 +2739,7 @@
3a2fd7
 	    sshbuf_len(cert), alg, 0, signer_ctx)) != 0)
3a2fd7
 		goto out;
3a2fd7
 	/* Check and update signature_type against what was actually used */
3a2fd7
-	if ((ret = get_sigtype(sig_blob, sig_len, &sigtype)) != 0)
3a2fd7
+	if ((ret = sshkey_get_sigtype(sig_blob, sig_len, &sigtype)) != 0)
3a2fd7
 		goto out;
3a2fd7
 	if (alg != NULL && strcmp(alg, sigtype) != 0) {
3a2fd7
 		ret = SSH_ERR_SIGN_ALG_UNSUPPORTED;
3a2fd7
diff --color -ru a/sshkey.h b/sshkey.h
3a2fd7
--- a/sshkey.h	2022-06-23 10:25:29.521922515 +0200
3a2fd7
+++ b/sshkey.h	2022-06-23 10:26:12.907762022 +0200
3a2fd7
@@ -211,6 +211,7 @@
3a2fd7
     const u_char *, size_t, const char *, u_int);
3a2fd7
 int	 sshkey_check_sigtype(const u_char *, size_t, const char *);
3a2fd7
 const char *sshkey_sigalg_by_name(const char *);
3a2fd7
+int      sshkey_get_sigtype(const u_char *, size_t, char **);
3a2fd7
 
3a2fd7
 /* for debug */
3a2fd7
 void	sshkey_dump_ec_point(const EC_GROUP *, const EC_POINT *);