[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] server: expose 'ssh_server_init_kex' API (resend)


Hi, this is a resend of an old patch I originally uploaded
to https://red.libssh.org/issues/159.

There is an old thread on the list here looking for this
functionality, too:

 * https://www.libssh.org/archive/libssh/2016-01/0000005.html

Are there any objections to a function like this?

I'm happy to clean up the patch or to make changes as necessary,
please let me know.


-Jon
From 8ccb2a3d7d5444fc28356ff69cb2a250fbf244e4 Mon Sep 17 00:00:00 2001
From: Jon Simons <jon@xxxxxxxxxxxxx>
Date: Sat, 18 Oct 2014 23:30:33 -0700
Subject: [PATCH] server: expose 'ssh_server_init_kex' API

Expose an API 'ssh_server_init_kex' which allows one to change the set of
key exchange, hostkey, ciphers, MACs, and compression algorithms currently
configured for the ssh_session at hand, after having started the
'ssh_handle_key_exchange' process.

One can use this API from the already-existing 'connect_status_function'
callback to dynamically modify the set of algorithms used after having
received the client banner, but before sending out the initial KEXINIT
message.

For example, one might want to prevent advertising the curve25519 key
exchange algorithm for older OpenSSH clients due to interop bugs.

BUG: https://red.libssh.org/issues/159

Signed-off-by: Jon Simons <jon@xxxxxxxxxxxxx>
---
 include/libssh/server.h | 18 ++++++++++++++++++
 src/server.c            | 27 +++++++++++++++++++++------
 2 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/include/libssh/server.h b/include/libssh/server.h
index c2132de1..aeacda00 100644
--- a/include/libssh/server.h
+++ b/include/libssh/server.h
@@ -188,6 +188,24 @@ LIBSSH_API ssh_gssapi_creds ssh_gssapi_get_creds(ssh_session session);
 LIBSSH_API int ssh_handle_key_exchange(ssh_session session);
 
 /**
+ * @brief Initialize the set of key exchange, hostkey, ciphers, MACs, and
+ *        compression algorithms for the given ssh_session.
+ *
+ * The selection of algorithms and keys used are determined by the
+ * options that are currently set in the given ssh_session structure.
+ * May only be called before the initial key exchange has begun.
+ *
+ * @param session  The session structure to initialize.
+ *
+ * @see ssh_handle_key_exchange
+ * @see ssh_options_set
+ *
+ * @return SSH_OK if initialization succeeds.
+ */
+
+LIBSSH_API int ssh_server_init_kex(ssh_session session);
+
+/**
  * @brief Free a ssh servers bind.
  *
  * @param  ssh_bind_o     The ssh server bind to free.
diff --git a/src/server.c b/src/server.c
index 3c1ee74c..4d36adcc 100644
--- a/src/server.c
+++ b/src/server.c
@@ -75,12 +75,12 @@ static int dh_handshake_server(ssh_session session);
  */
 
 /** @internal
- * This functions sets the Key Exchange protocols to be accepted
- * by the server. They depend on
- * -What the user asked (via options)
- * -What is available (keys)
- * It should then accept the intersection of what the user asked
- * and what is available, and return an error if nothing matches
+ *
+ * @brief initialize the set of key exchange, hostkey, ciphers, MACs, and
+ *        compression algorithms for the given ssh_session
+ *
+ * The selection of algorithms and keys used are determined by the
+ * options that are currently set in the given ssh_session structure.
  */
 
 static int server_set_kex(ssh_session session) {
@@ -149,6 +149,21 @@ static int server_set_kex(ssh_session session) {
   return 0;
 }
 
+int ssh_server_init_kex(ssh_session session) {
+    int i;
+
+    if (session->session_state > SSH_SESSION_STATE_BANNER_RECEIVED) {
+        return SSH_ERROR;
+    }
+
+    /* free any currently-set methods: server_set_kex will allocate new ones */
+    for (i = 0; i < 10 /* SSH_KEX_METHODS */; i++) {
+        SAFE_FREE(session->next_crypto->server_kex.methods[i]);
+    }
+
+    return server_set_kex(session);
+}
+
 /** @internal
  * @brief parse an incoming SSH_MSG_KEXDH_INIT packet and complete
  *        key exchange
-- 
2.13.2


Archive administrator: postmaster@lists.cynapses.org