Skip to content

Allow the client to initiate the key exchange #972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Renci.SshNet/ConnectionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@ public class ConnectionInfo : IConnectionInfoInternal
/// </value>
public int MaxSessions { get; set; }

/// <summary>
/// For servers that wait for the Key Exchange message, send the SSH_MSG_KEXINIT explicitly on connect()
/// </summary>
/// <value>
/// True to initiate the key exchange on connect(). Default is false.
/// </value>
public bool InitiateKeyExchange { get; set; }

/// <summary>
/// Occurs when authentication banner is sent by the server.
/// </summary>
Expand Down Expand Up @@ -341,6 +349,7 @@ public ConnectionInfo(string host, int port, string username, ProxyTypes proxyTy
ChannelCloseTimeout = DefaultChannelCloseTimeout;
RetryAttempts = 10;
MaxSessions = 10;
InitiateKeyExchange = false;
Encoding = Encoding.UTF8;

KeyExchangeAlgorithms = new Dictionary<string, Type>
Expand Down
5 changes: 5 additions & 0 deletions src/Renci.SshNet/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,11 @@ public void Connect()
// ToDo: Make message pump async, to not consume a thread for every session
_ = ThreadAbstraction.ExecuteThreadLongRunning(MessageListener);

if (ConnectionInfo.InitiateKeyExchange)
{
SendMessage(ClientInitMessage);
}
Comment on lines +653 to +656
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So to summarise my thoughts here, we can

  1. make this unconditional
  2. add _keyExchangeInProgress = true before the SendMessage(ClientInitMessage); call
  3. move it above line 651 (_ = ThreadAbstraction.ExecuteThreadLongRunning(MessageListener);). This will ensure we have sent the client init before we continue the key exchange
  4. Add a parameter bool sendClientInitMessage to IKeyExchange.Start which guards SendMessage(session.ClientInitMessage); in KeyExchange.Start
  5. Add a private bool _initialKeyExchangeCompleted; (or similar) member to Session, which is used to decide the sendClientInitMessage parameter in Session.OnKeyExchangeInitReceived.

As for testing, if we make it unconditional then we are covered already 🙂


// Wait for key exchange to be completed
WaitOnHandle(_keyExchangeCompletedWaitHandle);

Expand Down