bmh10 / rpms / openssh

Forked from rpms/openssh 2 days ago
Clone
Dmitry Belyavskiy 54fc80
diff --git a/PROTOCOL b/PROTOCOL
Dmitry Belyavskiy 54fc80
index d453c779..ded935eb 100644
Dmitry Belyavskiy 54fc80
--- a/PROTOCOL
Dmitry Belyavskiy 54fc80
+++ b/PROTOCOL
Dmitry Belyavskiy 54fc80
@@ -137,6 +137,32 @@ than as a named global or channel request to allow pings with very
Dmitry Belyavskiy 54fc80
 described at:
Dmitry Belyavskiy 54fc80
 http://git.libssh.org/users/aris/libssh.git/plain/doc/curve25519-sha256@libssh.org.txt?h=curve25519
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
+1.9 transport: strict key exchange extension
Dmitry Belyavskiy 54fc80
+
Dmitry Belyavskiy 54fc80
+OpenSSH supports a number of transport-layer hardening measures under
Dmitry Belyavskiy 54fc80
+a "strict KEX" feature. This feature is signalled similarly to the
Dmitry Belyavskiy 54fc80
+RFC8308 ext-info feature: by including a additional algorithm in the
Dmitry Belyavskiy 54fc80
+initiial SSH2_MSG_KEXINIT kex_algorithms field. The client may append
Dmitry Belyavskiy 54fc80
+"kex-strict-c-v00@openssh.com" to its kex_algorithms and the server
Dmitry Belyavskiy 54fc80
+may append "kex-strict-s-v00@openssh.com". These pseudo-algorithms
Dmitry Belyavskiy 54fc80
+are only valid in the initial SSH2_MSG_KEXINIT and MUST be ignored
Dmitry Belyavskiy 54fc80
+if they are present in subsequent SSH2_MSG_KEXINIT packets.
Dmitry Belyavskiy 54fc80
+
Dmitry Belyavskiy 54fc80
+When an endpoint that supports this extension observes this algorithm
Dmitry Belyavskiy 54fc80
+name in a peer's KEXINIT packet, it MUST make the following changes to
Dmitry Belyavskiy 54fc80
+the the protocol:
Dmitry Belyavskiy 54fc80
+
Dmitry Belyavskiy 54fc80
+a) During initial KEX, terminate the connection if any unexpected or
Dmitry Belyavskiy 54fc80
+   out-of-sequence packet is received. This includes terminating the
Dmitry Belyavskiy 54fc80
+   connection if the first packet received is not SSH2_MSG_KEXINIT.
Dmitry Belyavskiy 54fc80
+   Unexpected packets for the purpose of strict KEX include messages
Dmitry Belyavskiy 54fc80
+   that are otherwise valid at any time during the connection such as
Dmitry Belyavskiy 54fc80
+   SSH2_MSG_DEBUG and SSH2_MSG_IGNORE.
Dmitry Belyavskiy 54fc80
+b) After sending or receiving a SSH2_MSG_NEWKEYS message, reset the
Dmitry Belyavskiy 54fc80
+   packet sequence number to zero. This behaviour persists for the
Dmitry Belyavskiy 54fc80
+   duration of the connection (i.e. not just the first
Dmitry Belyavskiy 54fc80
+   SSH2_MSG_NEWKEYS).
Dmitry Belyavskiy 54fc80
+
Dmitry Belyavskiy 54fc80
 2. Connection protocol changes
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
 2.1. connection: Channel write close extension "eow@openssh.com"
Dmitry Belyavskiy 54fc80
diff --git a/kex.c b/kex.c
Dmitry Belyavskiy 54fc80
index aa5e792d..d478ff6e 100644
Dmitry Belyavskiy 54fc80
--- a/kex.c
Dmitry Belyavskiy 54fc80
+++ b/kex.c
Dmitry Belyavskiy 54fc80
@@ -65,7 +65,7 @@
Dmitry Belyavskiy 54fc80
 #endif
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
 /* prototype */
Dmitry Belyavskiy 54fc80
-static int kex_choose_conf(struct ssh *);
Dmitry Belyavskiy 54fc80
+static int kex_choose_conf(struct ssh *, uint32_t seq);
Dmitry Belyavskiy 54fc80
 static int kex_input_newkeys(int, u_int32_t, struct ssh *);
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
 static const char *proposal_names[PROPOSAL_MAX] = {
Dmitry Belyavskiy 54fc80
@@ -177,6 +177,18 @@ kex_names_valid(const char *names)
Dmitry Belyavskiy 54fc80
 	return 1;
Dmitry Belyavskiy 54fc80
 }
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
+/* returns non-zero if proposal contains any algorithm from algs */
Dmitry Belyavskiy 54fc80
+static int
Dmitry Belyavskiy 54fc80
+has_any_alg(const char *proposal, const char *algs)
Dmitry Belyavskiy 54fc80
+{
Dmitry Belyavskiy 54fc80
+	char *cp;
Dmitry Belyavskiy 54fc80
+
Dmitry Belyavskiy 54fc80
+	if ((cp = match_list(proposal, algs, NULL)) == NULL)
Dmitry Belyavskiy 54fc80
+		return 0;
Dmitry Belyavskiy 54fc80
+	free(cp);
Dmitry Belyavskiy 54fc80
+	return 1;
Dmitry Belyavskiy 54fc80
+}
Dmitry Belyavskiy 54fc80
+
Dmitry Belyavskiy 54fc80
 /*
Dmitry Belyavskiy 54fc80
  * Concatenate algorithm names, avoiding duplicates in the process.
Dmitry Belyavskiy 54fc80
  * Caller must free returned string.
Dmitry Belyavskiy 54fc80
@@ -184,7 +196,7 @@ kex_names_valid(const char *names)
Dmitry Belyavskiy 54fc80
 char *
Dmitry Belyavskiy 54fc80
 kex_names_cat(const char *a, const char *b)
Dmitry Belyavskiy 54fc80
 {
Dmitry Belyavskiy 54fc80
-	char *ret = NULL, *tmp = NULL, *cp, *p, *m;
Dmitry Belyavskiy 54fc80
+	char *ret = NULL, *tmp = NULL, *cp, *p;
Dmitry Belyavskiy 54fc80
 	size_t len;
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
 	if (a == NULL || *a == '\0')
Dmitry Belyavskiy 54fc80
@@ -201,10 +213,8 @@ kex_names_cat(const char *a, const char *b)
Dmitry Belyavskiy 54fc80
 	}
Dmitry Belyavskiy 54fc80
 	strlcpy(ret, a, len);
Dmitry Belyavskiy 54fc80
 	for ((p = strsep(&cp, ",")); p && *p != '\0'; (p = strsep(&cp, ","))) {
Dmitry Belyavskiy 54fc80
-		if ((m = match_list(ret, p, NULL)) != NULL) {
Dmitry Belyavskiy 54fc80
-			free(m);
Dmitry Belyavskiy 54fc80
+		if (has_any_alg(ret, p))
Dmitry Belyavskiy 54fc80
 			continue; /* Algorithm already present */
Dmitry Belyavskiy 54fc80
-		}
Dmitry Belyavskiy 54fc80
 		if (strlcat(ret, ",", len) >= len ||
Dmitry Belyavskiy 54fc80
 		    strlcat(ret, p, len) >= len) {
Dmitry Belyavskiy 54fc80
 			free(tmp);
Dmitry Belyavskiy 54fc80
@@ -466,7 +485,12 @@ kex_protocol_error(int type, u_int32_t seq, struct ssh *ssh)
Dmitry Belyavskiy 54fc80
 {
Dmitry Belyavskiy 54fc80
 	int r;
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
-	error("kex protocol error: type %d seq %u", type, seq);
Dmitry Belyavskiy 54fc80
+	/* If in strict mode, any unexpected message is an error */
Dmitry Belyavskiy 54fc80
+	if ((ssh->kex->flags & KEX_INITIAL) && ssh->kex->kex_strict) {
Dmitry Belyavskiy 54fc80
+		ssh_packet_disconnect(ssh, "strict KEX violation: "
Dmitry Belyavskiy 54fc80
+		    "unexpected packet type %u (seqnr %u)", type, seq);
Dmitry Belyavskiy 54fc80
+	}
Dmitry Belyavskiy 54fc80
+	error_f("type %u seq %u", type, seq);
Dmitry Belyavskiy 54fc80
 	if ((r = sshpkt_start(ssh, SSH2_MSG_UNIMPLEMENTED)) != 0 ||
Dmitry Belyavskiy 54fc80
 	    (r = sshpkt_put_u32(ssh, seq)) != 0 ||
Dmitry Belyavskiy 54fc80
 	    (r = sshpkt_send(ssh)) != 0)
Dmitry Belyavskiy 54fc80
@@ -548,6 +572,11 @@ kex_input_ext_info(int type, u_int32_t seq, struct ssh *ssh)
Dmitry Belyavskiy 54fc80
 	ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &kex_protocol_error);
Dmitry Belyavskiy 54fc80
 	if ((r = sshpkt_get_u32(ssh, &ninfo)) != 0)
Dmitry Belyavskiy 54fc80
 		return r;
Dmitry Belyavskiy 54fc80
+ 	if (ninfo >= 1024) {
Dmitry Belyavskiy 54fc80
+ 		error("SSH2_MSG_EXT_INFO with too many entries, expected "
Dmitry Belyavskiy 54fc80
+ 		    "<=1024, received %u", ninfo);
Dmitry Belyavskiy 54fc80
+		return dispatch_protocol_error(type, seq, ssh);
Dmitry Belyavskiy 54fc80
+ 	}
Dmitry Belyavskiy 54fc80
 	for (i = 0; i < ninfo; i++) {
Dmitry Belyavskiy 54fc80
 		if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0)
Dmitry Belyavskiy 54fc80
 			return r;
Dmitry Belyavskiy 54fc80
@@ -681,7 +705,7 @@ kex_input_kexinit(int type, u_int32_t seq, struct ssh *ssh)
Dmitry Belyavskiy 54fc80
 		error_f("no kex");
Dmitry Belyavskiy 54fc80
 		return SSH_ERR_INTERNAL_ERROR;
Dmitry Belyavskiy 54fc80
 	}
Dmitry Belyavskiy 54fc80
-	ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, NULL);
Dmitry Belyavskiy 54fc80
+	ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_protocol_error);
Dmitry Belyavskiy 54fc80
 	ptr = sshpkt_ptr(ssh, &dlen);
Dmitry Belyavskiy 54fc80
 	if ((r = sshbuf_put(kex->peer, ptr, dlen)) != 0)
Dmitry Belyavskiy 54fc80
 		return r;
Dmitry Belyavskiy 54fc80
@@ -717,7 +741,7 @@ kex_input_kexinit(int type, u_int32_t seq, struct ssh *ssh)
Dmitry Belyavskiy 54fc80
 	if (!(kex->flags & KEX_INIT_SENT))
Dmitry Belyavskiy 54fc80
 		if ((r = kex_send_kexinit(ssh)) != 0)
Dmitry Belyavskiy 54fc80
 			return r;
Dmitry Belyavskiy 54fc80
-	if ((r = kex_choose_conf(ssh)) != 0)
Dmitry Belyavskiy 54fc80
+	if ((r = kex_choose_conf(ssh, seq)) != 0)
Dmitry Belyavskiy 54fc80
 		return r;
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
 	if (kex->kex_type < KEX_MAX && kex->kex[kex->kex_type] != NULL)
Dmitry Belyavskiy 54fc80
@@ -981,20 +1005,14 @@ proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX])
Dmitry Belyavskiy 54fc80
 	return (1);
Dmitry Belyavskiy 54fc80
 }
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
-/* returns non-zero if proposal contains any algorithm from algs */
Dmitry Belyavskiy 54fc80
 static int
Dmitry Belyavskiy 54fc80
-has_any_alg(const char *proposal, const char *algs)
Dmitry Belyavskiy 54fc80
+kexalgs_contains(char **peer, const char *ext)
Dmitry Belyavskiy 54fc80
 {
Dmitry Belyavskiy 54fc80
-	char *cp;
Dmitry Belyavskiy 54fc80
-
Dmitry Belyavskiy 54fc80
-	if ((cp = match_list(proposal, algs, NULL)) == NULL)
Dmitry Belyavskiy 54fc80
-		return 0;
Dmitry Belyavskiy 54fc80
-	free(cp);
Dmitry Belyavskiy 54fc80
-	return 1;
Dmitry Belyavskiy 54fc80
+	return has_any_alg(peer[PROPOSAL_KEX_ALGS], ext);
Dmitry Belyavskiy 54fc80
 }
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
 static int
Dmitry Belyavskiy 54fc80
-kex_choose_conf(struct ssh *ssh)
Dmitry Belyavskiy 54fc80
+kex_choose_conf(struct ssh *ssh, uint32_t seq)
Dmitry Belyavskiy 54fc80
 {
Dmitry Belyavskiy 54fc80
 	struct kex *kex = ssh->kex;
Dmitry Belyavskiy 54fc80
 	struct newkeys *newkeys;
Dmitry Belyavskiy 54fc80
@@ -1019,13 +1037,23 @@ kex_choose_conf(struct ssh *ssh)
Dmitry Belyavskiy 54fc80
 		sprop=peer;
Dmitry Belyavskiy 54fc80
 	}
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
-	/* Check whether client supports ext_info_c */
Dmitry Belyavskiy 54fc80
-	if (kex->server && (kex->flags & KEX_INITIAL)) {
Dmitry Belyavskiy 54fc80
-		char *ext;
Dmitry Belyavskiy 54fc80
-
Dmitry Belyavskiy 54fc80
-		ext = match_list("ext-info-c", peer[PROPOSAL_KEX_ALGS], NULL);
Dmitry Belyavskiy 54fc80
-		kex->ext_info_c = (ext != NULL);
Dmitry Belyavskiy 54fc80
-		free(ext);
Dmitry Belyavskiy 54fc80
+	/* Check whether peer supports ext_info/kex_strict */
Dmitry Belyavskiy 54fc80
+	if ((kex->flags & KEX_INITIAL) != 0) {
Dmitry Belyavskiy 54fc80
+		if (kex->server) {
Dmitry Belyavskiy 54fc80
+			kex->ext_info_c = kexalgs_contains(peer, "ext-info-c");
Dmitry Belyavskiy 54fc80
+			kex->kex_strict = kexalgs_contains(peer,
Dmitry Belyavskiy 54fc80
+			    "kex-strict-c-v00@openssh.com");
Dmitry Belyavskiy 54fc80
+		} else {
Dmitry Belyavskiy 54fc80
+			kex->kex_strict = kexalgs_contains(peer,
Dmitry Belyavskiy 54fc80
+			    "kex-strict-s-v00@openssh.com");
Dmitry Belyavskiy 54fc80
+		}
Dmitry Belyavskiy 54fc80
+		if (kex->kex_strict) {
Dmitry Belyavskiy 54fc80
+			debug3_f("will use strict KEX ordering");
Dmitry Belyavskiy 54fc80
+			if (seq != 0)
Dmitry Belyavskiy 54fc80
+				ssh_packet_disconnect(ssh,
Dmitry Belyavskiy 54fc80
+				    "strict KEX violation: "
Dmitry Belyavskiy 54fc80
+				    "KEXINIT was not the first packet");
Dmitry Belyavskiy 54fc80
+		}
Dmitry Belyavskiy 54fc80
 	}
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
 	/* Check whether client supports rsa-sha2 algorithms */
Dmitry Belyavskiy 54fc80
diff --git a/kex.h b/kex.h
Dmitry Belyavskiy 54fc80
index 5f7ef784..272ebb43 100644
Dmitry Belyavskiy 54fc80
--- a/kex.h
Dmitry Belyavskiy 54fc80
+++ b/kex.h
Dmitry Belyavskiy 54fc80
@@ -149,6 +149,7 @@ struct kex {
Dmitry Belyavskiy 54fc80
 	u_int	kex_type;
Dmitry Belyavskiy 54fc80
 	char	*server_sig_algs;
Dmitry Belyavskiy 54fc80
 	int	ext_info_c;
Dmitry Belyavskiy 54fc80
+	int	kex_strict;
Dmitry Belyavskiy 54fc80
 	struct sshbuf *my;
Dmitry Belyavskiy 54fc80
 	struct sshbuf *peer;
Dmitry Belyavskiy 54fc80
 	struct sshbuf *client_version;
Dmitry Belyavskiy 54fc80
diff --git a/packet.c b/packet.c
Dmitry Belyavskiy 54fc80
index 52017def..beb214f9 100644
Dmitry Belyavskiy 54fc80
--- a/packet.c
Dmitry Belyavskiy 54fc80
+++ b/packet.c
Dmitry Belyavskiy 54fc80
@@ -1207,8 +1207,13 @@ ssh_packet_send2_wrapped(struct ssh *ssh)
Dmitry Belyavskiy 54fc80
 	sshbuf_dump(state->output, stderr);
Dmitry Belyavskiy 54fc80
 #endif
Dmitry Belyavskiy 54fc80
 	/* increment sequence number for outgoing packets */
Dmitry Belyavskiy 54fc80
-	if (++state->p_send.seqnr == 0)
Dmitry Belyavskiy 54fc80
+	if (++state->p_send.seqnr == 0) {
Dmitry Belyavskiy 54fc80
+		if ((ssh->kex->flags & KEX_INITIAL) != 0) {
Dmitry Belyavskiy 54fc80
+			ssh_packet_disconnect(ssh, "outgoing sequence number "
Dmitry Belyavskiy 54fc80
+			    "wrapped during initial key exchange");
Dmitry Belyavskiy 54fc80
+		}
Dmitry Belyavskiy 54fc80
 		logit("outgoing seqnr wraps around");
Dmitry Belyavskiy 54fc80
+	}
Dmitry Belyavskiy 54fc80
 	if (++state->p_send.packets == 0)
Dmitry Belyavskiy 54fc80
 		if (!(ssh->compat & SSH_BUG_NOREKEY))
Dmitry Belyavskiy 54fc80
 			return SSH_ERR_NEED_REKEY;
Dmitry Belyavskiy 54fc80
@@ -1216,6 +1221,11 @@ ssh_packet_send2_wrapped(struct ssh *ssh)
Dmitry Belyavskiy 54fc80
 	state->p_send.bytes += len;
Dmitry Belyavskiy 54fc80
 	sshbuf_reset(state->outgoing_packet);
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
+	if (type == SSH2_MSG_NEWKEYS && ssh->kex->kex_strict) {
Dmitry Belyavskiy 54fc80
+		debug_f("resetting send seqnr %u", state->p_send.seqnr);
Dmitry Belyavskiy 54fc80
+		state->p_send.seqnr = 0;
Dmitry Belyavskiy 54fc80
+	}
Dmitry Belyavskiy 54fc80
+
Dmitry Belyavskiy 54fc80
 	if (type == SSH2_MSG_NEWKEYS)
Dmitry Belyavskiy 54fc80
 		r = ssh_set_newkeys(ssh, MODE_OUT);
Dmitry Belyavskiy 54fc80
 	else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side)
Dmitry Belyavskiy 54fc80
@@ -1344,8 +1354,7 @@ ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Dmitry Belyavskiy 54fc80
 	/* Stay in the loop until we have received a complete packet. */
Dmitry Belyavskiy 54fc80
 	for (;;) {
Dmitry Belyavskiy 54fc80
 		/* Try to read a packet from the buffer. */
Dmitry Belyavskiy 54fc80
-		r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
Dmitry Belyavskiy 54fc80
-		if (r != 0)
Dmitry Belyavskiy 54fc80
+		if ((r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p)) != 0)
Dmitry Belyavskiy 54fc80
 			break;
Dmitry Belyavskiy 54fc80
 		/* If we got a packet, return it. */
Dmitry Belyavskiy 54fc80
 		if (*typep != SSH_MSG_NONE)
Dmitry Belyavskiy 54fc80
@@ -1629,10 +1615,16 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Dmitry Belyavskiy 54fc80
 		if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
Dmitry Belyavskiy 54fc80
 			goto out;
Dmitry Belyavskiy 54fc80
 	}
Dmitry Belyavskiy 54fc80
+
Dmitry Belyavskiy 54fc80
 	if (seqnr_p != NULL)
Dmitry Belyavskiy 54fc80
 		*seqnr_p = state->p_read.seqnr;
Dmitry Belyavskiy 54fc80
-	if (++state->p_read.seqnr == 0)
Dmitry Belyavskiy 54fc80
+	if (++state->p_read.seqnr == 0) {
Dmitry Belyavskiy 54fc80
+		if ((ssh->kex->flags & KEX_INITIAL) != 0) {
Dmitry Belyavskiy 54fc80
+			ssh_packet_disconnect(ssh, "incoming sequence number "
Dmitry Belyavskiy 54fc80
+			    "wrapped during initial key exchange");
Dmitry Belyavskiy 54fc80
+		}
Dmitry Belyavskiy 54fc80
 		logit("incoming seqnr wraps around");
Dmitry Belyavskiy 54fc80
+	}
Dmitry Belyavskiy 54fc80
 	if (++state->p_read.packets == 0)
Dmitry Belyavskiy 54fc80
 		if (!(ssh->compat & SSH_BUG_NOREKEY))
Dmitry Belyavskiy 54fc80
 			return SSH_ERR_NEED_REKEY;
Dmitry Belyavskiy 54fc80
@@ -1698,6 +1690,10 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Dmitry Belyavskiy 54fc80
 #endif
Dmitry Belyavskiy 54fc80
 	/* reset for next packet */
Dmitry Belyavskiy 54fc80
 	state->packlen = 0;
Dmitry Belyavskiy 54fc80
+	if (*typep == SSH2_MSG_NEWKEYS && ssh->kex->kex_strict) {
Dmitry Belyavskiy 54fc80
+		debug_f("resetting read seqnr %u", state->p_read.seqnr);
Dmitry Belyavskiy 54fc80
+		state->p_read.seqnr = 0;
Dmitry Belyavskiy 54fc80
+	}
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
 	if ((r = ssh_packet_check_rekey(ssh)) != 0)
Dmitry Belyavskiy 54fc80
 		return r;
Dmitry Belyavskiy 54fc80
@@ -1720,10 +1716,39 @@ ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Dmitry Belyavskiy 54fc80
 		r = ssh_packet_read_poll2(ssh, typep, seqnr_p);
Dmitry Belyavskiy 54fc80
 		if (r != 0)
Dmitry Belyavskiy 54fc80
 			return r;
Dmitry Belyavskiy 54fc80
-		if (*typep) {
Dmitry Belyavskiy 54fc80
-			state->keep_alive_timeouts = 0;
Dmitry Belyavskiy 54fc80
-			DBG(debug("received packet type %d", *typep));
Dmitry Belyavskiy 54fc80
+		if (*typep == 0) {
Dmitry Belyavskiy 54fc80
+			/* no message ready */
Dmitry Belyavskiy 54fc80
+			return 0;
Dmitry Belyavskiy 54fc80
 		}
Dmitry Belyavskiy 54fc80
+		state->keep_alive_timeouts = 0;
Dmitry Belyavskiy 54fc80
+		DBG(debug("received packet type %d", *typep));
Dmitry Belyavskiy 54fc80
+
Dmitry Belyavskiy 54fc80
+		/* Always process disconnect messages */
Dmitry Belyavskiy 54fc80
+		if (*typep == SSH2_MSG_DISCONNECT) {
Dmitry Belyavskiy 54fc80
+			if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
Dmitry Belyavskiy 54fc80
+			    (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
Dmitry Belyavskiy 54fc80
+				return r;
Dmitry Belyavskiy 54fc80
+			/* Ignore normal client exit notifications */
Dmitry Belyavskiy 54fc80
+			do_log2(ssh->state->server_side &&
Dmitry Belyavskiy 54fc80
+			    reason == SSH2_DISCONNECT_BY_APPLICATION ?
Dmitry Belyavskiy 54fc80
+			    SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
Dmitry Belyavskiy 54fc80
+			    "Received disconnect from %s port %d:"
Dmitry Belyavskiy 54fc80
+			    "%u: %.400s", ssh_remote_ipaddr(ssh),
Dmitry Belyavskiy 54fc80
+			    ssh_remote_port(ssh), reason, msg);
Dmitry Belyavskiy 54fc80
+			free(msg);
Dmitry Belyavskiy 54fc80
+			return SSH_ERR_DISCONNECTED;
Dmitry Belyavskiy 54fc80
+		}
Dmitry Belyavskiy 54fc80
+
Dmitry Belyavskiy 54fc80
+		/*
Dmitry Belyavskiy 54fc80
+		 * Do not implicitly handle any messages here during initial
Dmitry Belyavskiy 54fc80
+		 * KEX when in strict mode. They will be need to be allowed
Dmitry Belyavskiy 54fc80
+		 * explicitly by the KEX dispatch table or they will generate
Dmitry Belyavskiy 54fc80
+		 * protocol errors.
Dmitry Belyavskiy 54fc80
+		 */
Dmitry Belyavskiy 54fc80
+		if (ssh->kex != NULL &&
Dmitry Belyavskiy 54fc80
+		    (ssh->kex->flags & KEX_INITIAL) && ssh->kex->kex_strict)
Dmitry Belyavskiy 54fc80
+			return 0;
Dmitry Belyavskiy 54fc80
+		/* Implicitly handle transport-level messages */
Dmitry Belyavskiy 54fc80
 		switch (*typep) {
Dmitry Belyavskiy 54fc80
 		case SSH2_MSG_IGNORE:
Dmitry Belyavskiy 54fc80
 			debug3("Received SSH2_MSG_IGNORE");
Dmitry Belyavskiy 54fc80
@@ -1738,19 +1763,6 @@ ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
Dmitry Belyavskiy 54fc80
 			debug("Remote: %.900s", msg);
Dmitry Belyavskiy 54fc80
 			free(msg);
Dmitry Belyavskiy 54fc80
 			break;
Dmitry Belyavskiy 54fc80
-		case SSH2_MSG_DISCONNECT:
Dmitry Belyavskiy 54fc80
-			if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
Dmitry Belyavskiy 54fc80
-			    (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
Dmitry Belyavskiy 54fc80
-				return r;
Dmitry Belyavskiy 54fc80
-			/* Ignore normal client exit notifications */
Dmitry Belyavskiy 54fc80
-			do_log2(ssh->state->server_side &&
Dmitry Belyavskiy 54fc80
-			    reason == SSH2_DISCONNECT_BY_APPLICATION ?
Dmitry Belyavskiy 54fc80
-			    SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
Dmitry Belyavskiy 54fc80
-			    "Received disconnect from %s port %d:"
Dmitry Belyavskiy 54fc80
-			    "%u: %.400s", ssh_remote_ipaddr(ssh),
Dmitry Belyavskiy 54fc80
-			    ssh_remote_port(ssh), reason, msg);
Dmitry Belyavskiy 54fc80
-			free(msg);
Dmitry Belyavskiy 54fc80
-			return SSH_ERR_DISCONNECTED;
Dmitry Belyavskiy 54fc80
 		case SSH2_MSG_UNIMPLEMENTED:
Dmitry Belyavskiy 54fc80
 			if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
Dmitry Belyavskiy 54fc80
 				return r;
Dmitry Belyavskiy 54fc80
@@ -2242,6 +2254,7 @@ kex_to_blob(struct sshbuf *m, struct kex *kex)
Dmitry Belyavskiy 54fc80
 	    (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
Dmitry Belyavskiy 54fc80
 	    (r = sshbuf_put_u32(m, kex->hostkey_nid)) != 0 ||
Dmitry Belyavskiy 54fc80
 	    (r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
Dmitry Belyavskiy 54fc80
+	    (r = sshbuf_put_u32(m, kex->kex_strict)) != 0 ||
Dmitry Belyavskiy 54fc80
 	    (r = sshbuf_put_stringb(m, kex->my)) != 0 ||
Dmitry Belyavskiy 54fc80
 	    (r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
Dmitry Belyavskiy 54fc80
 	    (r = sshbuf_put_stringb(m, kex->client_version)) != 0 ||
Dmitry Belyavskiy 54fc80
@@ -2404,6 +2417,7 @@ kex_from_blob(struct sshbuf *m, struct kex **kexp)
Dmitry Belyavskiy 54fc80
 	    (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
Dmitry Belyavskiy 54fc80
 	    (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_nid)) != 0 ||
Dmitry Belyavskiy 54fc80
 	    (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
Dmitry Belyavskiy 54fc80
+	    (r = sshbuf_get_u32(m, &kex->kex_strict)) != 0 ||
Dmitry Belyavskiy 54fc80
 	    (r = sshbuf_get_stringb(m, kex->my)) != 0 ||
Dmitry Belyavskiy 54fc80
 	    (r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
Dmitry Belyavskiy 54fc80
 	    (r = sshbuf_get_stringb(m, kex->client_version)) != 0 ||
Dmitry Belyavskiy 54fc80
@@ -2732,6 +2746,7 @@ sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
Dmitry Belyavskiy 54fc80
 	vsnprintf(buf, sizeof(buf), fmt, args);
Dmitry Belyavskiy 54fc80
 	va_end(args);
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
+	debug2_f("sending SSH2_MSG_DISCONNECT: %s", buf);
Dmitry Belyavskiy 54fc80
 	if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
Dmitry Belyavskiy 54fc80
 	    (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
Dmitry Belyavskiy 54fc80
 	    (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
Dmitry Belyavskiy 54fc80
diff --git a/sshconnect2.c b/sshconnect2.c
Dmitry Belyavskiy 54fc80
index df6caf81..0cccbcc4 100644
Dmitry Belyavskiy 54fc80
--- a/sshconnect2.c
Dmitry Belyavskiy 54fc80
+++ b/sshconnect2.c
Dmitry Belyavskiy 54fc80
@@ -253,7 +253,8 @@ ssh_kex2(struct ssh *ssh, char *host, st
Dmitry Belyavskiy 54fc80
 		fatal_fr(r, "kex_assemble_namelist");
Dmitry Belyavskiy 54fc80
 	free(all_key);
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
-	if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
Dmitry Belyavskiy 54fc80
+	if ((s = kex_names_cat(options.kex_algorithms,
Dmitry Belyavskiy 54fc80
+	    "ext-info-c,kex-strict-c-v00@openssh.com")) == NULL)
Dmitry Belyavskiy 54fc80
 		fatal_f("kex_names_cat");
Dmitry Belyavskiy 54fc80
 	myproposal[PROPOSAL_KEX_ALGS] = prop_kex = compat_kex_proposal(ssh, s);
Dmitry Belyavskiy 54fc80
 	myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Dmitry Belyavskiy 54fc80
@@ -358,7 +358,6 @@ struct cauthmethod {
Dmitry Belyavskiy 54fc80
 };
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
 static int input_userauth_service_accept(int, u_int32_t, struct ssh *);
Dmitry Belyavskiy 54fc80
-static int input_userauth_ext_info(int, u_int32_t, struct ssh *);
Dmitry Belyavskiy 54fc80
 static int input_userauth_success(int, u_int32_t, struct ssh *);
Dmitry Belyavskiy 54fc80
 static int input_userauth_failure(int, u_int32_t, struct ssh *);
Dmitry Belyavskiy 54fc80
 static int input_userauth_banner(int, u_int32_t, struct ssh *);
Dmitry Belyavskiy 54fc80
@@ -472,7 +471,7 @@ ssh_userauth2(struct ssh *ssh, const char *local_user,
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
 	ssh->authctxt = &authctxt;
Dmitry Belyavskiy 54fc80
 	ssh_dispatch_init(ssh, &input_userauth_error);
Dmitry Belyavskiy 54fc80
-	ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
Dmitry Belyavskiy 54fc80
+	ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, kex_input_ext_info);
Dmitry Belyavskiy 54fc80
 	ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
Dmitry Belyavskiy 54fc80
 	ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt.success);	/* loop until success */
Dmitry Belyavskiy 54fc80
 	pubkey_cleanup(ssh);
Dmitry Belyavskiy 54fc80
@@ -531,12 +530,6 @@ input_userauth_service_accept(int type, u_int32_t seq, struct ssh *ssh)
Dmitry Belyavskiy 54fc80
 }
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
 /* ARGSUSED */
Dmitry Belyavskiy 54fc80
-static int
Dmitry Belyavskiy 54fc80
-input_userauth_ext_info(int type, u_int32_t seqnr, struct ssh *ssh)
Dmitry Belyavskiy 54fc80
-{
Dmitry Belyavskiy 54fc80
-	return kex_input_ext_info(type, seqnr, ssh);
Dmitry Belyavskiy 54fc80
-}
Dmitry Belyavskiy 54fc80
-
Dmitry Belyavskiy 54fc80
 void
Dmitry Belyavskiy 54fc80
 userauth(struct ssh *ssh, char *authlist)
Dmitry Belyavskiy 54fc80
 {
Dmitry Belyavskiy 54fc80
@@ -615,6 +608,7 @@ input_userauth_success(int type, u_int32_t seq, struct ssh *ssh)
Dmitry Belyavskiy 54fc80
 	free(authctxt->methoddata);
Dmitry Belyavskiy 54fc80
 	authctxt->methoddata = NULL;
Dmitry Belyavskiy 54fc80
 	authctxt->success = 1;			/* break out */
Dmitry Belyavskiy 54fc80
+	ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, dispatch_protocol_error);
Dmitry Belyavskiy 54fc80
 	return 0;
Dmitry Belyavskiy 54fc80
 }
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
diff -up openssh-8.7p1/sshd.c.kexstrict openssh-8.7p1/sshd.c
Dmitry Belyavskiy 54fc80
--- openssh-8.7p1/sshd.c.kexstrict	2023-11-27 13:19:18.855433602 +0100
Dmitry Belyavskiy 54fc80
+++ openssh-8.7p1/sshd.c	2023-11-27 13:28:10.441325314 +0100
Dmitry Belyavskiy 54fc80
@@ -2531,10 +2531,14 @@ do_ssh2_kex(struct ssh *ssh)
Dmitry Belyavskiy 54fc80
 	struct kex *kex;
Dmitry Belyavskiy 54fc80
 	char *hostkey_types = NULL;
Dmitry Belyavskiy 54fc80
 	char *prop_kex = NULL, *prop_enc = NULL, *prop_hostkey = NULL;
Dmitry Belyavskiy 54fc80
+	char *cp;
Dmitry Belyavskiy 54fc80
 	int r;
Dmitry Belyavskiy 54fc80
 
Dmitry Belyavskiy 54fc80
-	myproposal[PROPOSAL_KEX_ALGS] = prop_kex = compat_kex_proposal(ssh,
Dmitry Belyavskiy 54fc80
-	    options.kex_algorithms);
Dmitry Belyavskiy 54fc80
+	if ((cp = kex_names_cat(options.kex_algorithms, 
Dmitry Belyavskiy 54fc80
+	   "kex-strict-s-v00@openssh.com")) == NULL)
Dmitry Belyavskiy 54fc80
+		fatal_f("kex_names_cat");
Dmitry Belyavskiy 54fc80
+
Dmitry Belyavskiy 54fc80
+	myproposal[PROPOSAL_KEX_ALGS] = prop_kex = compat_kex_proposal(ssh, cp);
Dmitry Belyavskiy 54fc80
 	myproposal[PROPOSAL_ENC_ALGS_CTOS] =
Dmitry Belyavskiy 54fc80
 	    myproposal[PROPOSAL_ENC_ALGS_STOC] = prop_enc =
Dmitry Belyavskiy 54fc80
 	    compat_cipher_proposal(ssh, options.ciphers);
Dmitry Belyavskiy 4c4233
@@ -2586,7 +2586,7 @@ do_ssh2_kex(struct ssh *ssh)
Dmitry Belyavskiy 4c4233
 	if (gss && orig)
Dmitry Belyavskiy 4c4233
 		xasprintf(&newstr, "%s,%s", gss, orig);
Dmitry Belyavskiy 4c4233
 	else if (gss)
Dmitry Belyavskiy 4c4233
-		newstr = gss;
Dmitry Belyavskiy 4c4233
+		xasprintf(&newstr, "%s,%s", gss, "kex-strict-s-v00@openssh.com");
Dmitry Belyavskiy 4c4233
 	else if (orig)
Dmitry Belyavskiy 4c4233
 		newstr = orig;
Dmitry Belyavskiy 4c4233
 
Dmitry Belyavskiy 54fc80
@@ -2650,6 +2654,7 @@ do_ssh2_kex(struct ssh *ssh)
Dmitry Belyavskiy 54fc80
 #endif
Dmitry Belyavskiy 54fc80
 	free(prop_kex);
Dmitry Belyavskiy 54fc80
 	free(prop_enc);
Dmitry Belyavskiy 54fc80
+	free(cp);
Dmitry Belyavskiy 54fc80
 	free(prop_hostkey);
Dmitry Belyavskiy 54fc80
 	debug("KEX done");
Dmitry Belyavskiy 54fc80
 }