-
-
Notifications
You must be signed in to change notification settings - Fork 970
Implement OpenSSH strict key exchange extension #1366
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5070ca9
Implement OpenSSH strict key exchange extension
scott-xu 5357fee
The pseudo-algorithm
scott-xu 95bc410
Merge branch 'develop' into strict-kex
scott-xu 15eb67d
Merge branch 'develop' into strict-kex
scott-xu 4f704a7
Merge branch 'develop' into strict-kex
scott-xu afc4bb2
Only send strict kex pseudo algorithm for the first kex.
scott-xu 6350350
Unit tests for strict kex
scott-xu 9097805
More unit tests
scott-xu c5da652
More unit tests
scott-xu d0b0a08
Correct file name
scott-xu 35f0b9c
Update SessionTest_ConnectingBase.cs
scott-xu f0cb434
More unit tests
scott-xu 38b5b9f
Delete SessionTest_Connecting_ServerSendsMaxIgnoreMessagesBeforeKexIn…
scott-xu 5b1421e
Add a comment about throwing exception when inbound sequence number i…
scott-xu 48787e9
Merge branch 'develop' of https://github.com/scott-xu/SSH.NET into st…
scott-xu 9cd2653
Delete SessionTest_Connecting_ServerSendsDebugMessageAfterKexInit_NoS…
scott-xu ebc555e
Fix build
scott-xu c64f058
Merge branch 'develop' into strict-kex
scott-xu a5fdc4f
Update test/Renci.SshNet.Tests/Classes/SessionTest_Connected.cs
Rob-Hague File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -378,6 +378,7 @@ public ConnectionInfo(string host, int port, string username, ProxyTypes proxyTy | |
| { "diffie-hellman-group14-sha256", () => new KeyExchangeDiffieHellmanGroup14Sha256() }, | ||
| { "diffie-hellman-group14-sha1", () => new KeyExchangeDiffieHellmanGroup14Sha1() }, | ||
| { "diffie-hellman-group1-sha1", () => new KeyExchangeDiffieHellmanGroup1Sha1() }, | ||
| { "[email protected]", null }, | ||
| }; | ||
|
|
||
| Encryptions = new Dictionary<string, CipherInfo> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -154,6 +154,17 @@ public class Session : ISession | |
| /// </summary> | ||
| private bool _isDisconnecting; | ||
|
|
||
| /// <summary> | ||
| /// Indicates whether it is the init kex. | ||
| /// </summary> | ||
| private bool _isInitialKex = true; | ||
|
|
||
| /// <summary> | ||
| /// Indicates whether server supports strict key exchange. | ||
| /// <see href="https://github.com/openssh/openssh-portable/blob/master/PROTOCOL"/> 1.10. | ||
| /// </summary> | ||
| private bool _isStrictKex; | ||
|
|
||
| private IKeyExchange _keyExchange; | ||
|
|
||
| private HashAlgorithm _serverMac; | ||
|
|
@@ -288,20 +299,20 @@ public Message ClientInitMessage | |
| get | ||
| { | ||
| _clientInitMessage ??= new KeyExchangeInitMessage | ||
| { | ||
| KeyExchangeAlgorithms = ConnectionInfo.KeyExchangeAlgorithms.Keys.ToArray(), | ||
| ServerHostKeyAlgorithms = ConnectionInfo.HostKeyAlgorithms.Keys.ToArray(), | ||
| EncryptionAlgorithmsClientToServer = ConnectionInfo.Encryptions.Keys.ToArray(), | ||
| EncryptionAlgorithmsServerToClient = ConnectionInfo.Encryptions.Keys.ToArray(), | ||
| MacAlgorithmsClientToServer = ConnectionInfo.HmacAlgorithms.Keys.ToArray(), | ||
| MacAlgorithmsServerToClient = ConnectionInfo.HmacAlgorithms.Keys.ToArray(), | ||
| CompressionAlgorithmsClientToServer = ConnectionInfo.CompressionAlgorithms.Keys.ToArray(), | ||
| CompressionAlgorithmsServerToClient = ConnectionInfo.CompressionAlgorithms.Keys.ToArray(), | ||
| LanguagesClientToServer = new[] { string.Empty }, | ||
| LanguagesServerToClient = new[] { string.Empty }, | ||
| FirstKexPacketFollows = false, | ||
| Reserved = 0 | ||
| }; | ||
| { | ||
| KeyExchangeAlgorithms = ConnectionInfo.KeyExchangeAlgorithms.Keys.ToArray(), | ||
| ServerHostKeyAlgorithms = ConnectionInfo.HostKeyAlgorithms.Keys.ToArray(), | ||
| EncryptionAlgorithmsClientToServer = ConnectionInfo.Encryptions.Keys.ToArray(), | ||
| EncryptionAlgorithmsServerToClient = ConnectionInfo.Encryptions.Keys.ToArray(), | ||
| MacAlgorithmsClientToServer = ConnectionInfo.HmacAlgorithms.Keys.ToArray(), | ||
| MacAlgorithmsServerToClient = ConnectionInfo.HmacAlgorithms.Keys.ToArray(), | ||
| CompressionAlgorithmsClientToServer = ConnectionInfo.CompressionAlgorithms.Keys.ToArray(), | ||
| CompressionAlgorithmsServerToClient = ConnectionInfo.CompressionAlgorithms.Keys.ToArray(), | ||
| LanguagesClientToServer = new[] { string.Empty }, | ||
| LanguagesServerToClient = new[] { string.Empty }, | ||
| FirstKexPacketFollows = false, | ||
| Reserved = 0 | ||
| }; | ||
|
|
||
| return _clientInitMessage; | ||
| } | ||
|
|
@@ -1106,13 +1117,20 @@ internal void SendMessage(Message message) | |
| SendPacket(data, 0, data.Length); | ||
| } | ||
|
|
||
| // increment the packet sequence number only after we're sure the packet has | ||
| // been sent; even though it's only used for the MAC, it needs to be incremented | ||
| // for each package sent. | ||
| // | ||
| // the server will use it to verify the data integrity, and as such the order in | ||
| // which messages are sent must follow the outbound packet sequence number | ||
| _outboundPacketSequence++; | ||
| if (_isStrictKex && message is NewKeysMessage) | ||
| { | ||
| _outboundPacketSequence = 0; | ||
| } | ||
| else | ||
| { | ||
| // increment the packet sequence number only after we're sure the packet has | ||
| // been sent; even though it's only used for the MAC, it needs to be incremented | ||
| // for each package sent. | ||
| // | ||
| // the server will use it to verify the data integrity, and as such the order in | ||
| // which messages are sent must follow the outbound packet sequence number | ||
| _outboundPacketSequence++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1446,6 +1464,18 @@ internal void OnKeyExchangeInitReceived(KeyExchangeInitMessage message) | |
|
|
||
| _keyExchangeCompletedWaitHandle.Reset(); | ||
|
|
||
| if (_isInitialKex && message.KeyExchangeAlgorithms.Contains("[email protected]")) | ||
scott-xu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| _isStrictKex = true; | ||
|
|
||
| DiagnosticAbstraction.Log(string.Format("[{0}] Enabling strict key exchange extension.", ToHex(SessionId))); | ||
|
|
||
| if (_inboundPacketSequence != 1) | ||
| { | ||
| throw new SshConnectionException("KEXINIT was not the first packet during strict key exchange", DisconnectReason.KeyExchangeFailed); | ||
| } | ||
| } | ||
|
|
||
| // Disable messages that are not key exchange related | ||
| _sshMessageFactory.DisableNonKeyExchangeMessages(); | ||
scott-xu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
@@ -1522,6 +1552,13 @@ internal void OnNewKeysReceived(NewKeysMessage message) | |
| // Enable activated messages that are not key exchange related | ||
| _sshMessageFactory.EnableActivatedMessages(); | ||
|
|
||
| _isInitialKex = false; | ||
|
|
||
| if (_isStrictKex) | ||
| { | ||
| _inboundPacketSequence = 0; | ||
| } | ||
|
|
||
| NewKeysReceived?.Invoke(this, new MessageEventArgs<NewKeysMessage>(message)); | ||
|
|
||
| // Signal that key exchange completed | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.