From 1a803b50ee33ff7c484144a57693f4fc7ddacf68 Mon Sep 17 00:00:00 2001 From: drieseng Date: Fri, 24 Apr 2020 21:09:12 +0200 Subject: [PATCH] Partially revert changes from #643. Fixes #653. --- src/Renci.SshNet/Common/Extensions.cs | 15 ----------- src/Renci.SshNet/Security/KeyExchange.cs | 26 ++++++++++++------- src/Renci.SshNet/Security/KeyExchangeEC.cs | 24 +++-------------- .../Security/KeyExchangeECCurve25519.cs | 18 ++++++++++++- src/Renci.SshNet/Security/KeyExchangeECDH.cs | 2 +- .../Security/KeyExchangeECDH256.cs | 18 ++++++++++++- src/Renci.SshNet/Security/KeyExchangeHash.cs | 1 - src/Renci.SshNet/Security/KeyHostAlgorithm.cs | 2 +- 8 files changed, 57 insertions(+), 49 deletions(-) diff --git a/src/Renci.SshNet/Common/Extensions.cs b/src/Renci.SshNet/Common/Extensions.cs index 547bdacd..17d72d7f 100644 --- a/src/Renci.SshNet/Common/Extensions.cs +++ b/src/Renci.SshNet/Common/Extensions.cs @@ -72,21 +72,6 @@ namespace Renci.SshNet.Common return new BigInteger(reversed.Reverse()); } - /// - /// Initializes a new instance of the structure using the SSH BigNum2 Format - /// - public static byte[] ToBigNum2(this byte[] data) - { - if ((data[0] & (1 << 7)) != 0) - { - var buf = new byte[data.Length + 1]; - Buffer.BlockCopy(data, 0, buf, 1, data.Length); - data = buf; - } - - return data; - } - /// /// Initializes a new instance of the structure using the SSH BigNum2 Format /// diff --git a/src/Renci.SshNet/Security/KeyExchange.cs b/src/Renci.SshNet/Security/KeyExchange.cs index 57ea6c50..a13c272a 100644 --- a/src/Renci.SshNet/Security/KeyExchange.cs +++ b/src/Renci.SshNet/Security/KeyExchange.cs @@ -345,14 +345,17 @@ namespace Renci.SshNet.Security private byte[] GenerateSessionKey(byte[] sharedKey, byte[] exchangeHash, byte[] key, int size) { var result = new List(key); + while (size > result.Count) { - result.AddRange(Hash(new _SessionKeyAdjustment - { - SharedKey = sharedKey, - ExchangeHash = exchangeHash, - Key = key, - }.GetBytes())); + var sessionKeyAdjustment = new SessionKeyAdjustment + { + SharedKey = sharedKey, + ExchangeHash = exchangeHash, + Key = key, + }; + + result.AddRange(Hash(sessionKeyAdjustment.GetBytes())); } return result.ToArray(); @@ -368,7 +371,7 @@ namespace Renci.SshNet.Security /// private static byte[] GenerateSessionKey(byte[] sharedKey, byte[] exchangeHash, char p, byte[] sessionId) { - var sessionKeyGeneration = new _SessionKeyGeneration + var sessionKeyGeneration = new SessionKeyGeneration { SharedKey = sharedKey, ExchangeHash = exchangeHash, @@ -378,11 +381,14 @@ namespace Renci.SshNet.Security return sessionKeyGeneration.GetBytes(); } - private class _SessionKeyGeneration : SshData + private class SessionKeyGeneration : SshData { public byte[] SharedKey { get; set; } + public byte[] ExchangeHash { get; set; } + public char Char { get; set; } + public byte[] SessionId { get; set; } /// @@ -419,10 +425,12 @@ namespace Renci.SshNet.Security } } - private class _SessionKeyAdjustment : SshData + private class SessionKeyAdjustment : SshData { public byte[] SharedKey { get; set; } + public byte[] ExchangeHash { get; set; } + public byte[] Key { get; set; } /// diff --git a/src/Renci.SshNet/Security/KeyExchangeEC.cs b/src/Renci.SshNet/Security/KeyExchangeEC.cs index 22bd4e77..79cdf5be 100644 --- a/src/Renci.SshNet/Security/KeyExchangeEC.cs +++ b/src/Renci.SshNet/Security/KeyExchangeEC.cs @@ -1,7 +1,6 @@ using System.Text; using Renci.SshNet.Messages.Transport; using Renci.SshNet.Common; -using Renci.SshNet.Abstractions; namespace Renci.SshNet.Security { @@ -45,21 +44,6 @@ namespace Renci.SshNet.Security /// protected abstract int HashSize { get; } - /// - /// Hashes the specified data bytes. - /// - /// The hash data. - /// - /// Hashed bytes - /// - protected override byte[] Hash(byte[] hashData) - { - using (var sha256 = CryptoAbstraction.CreateSHA256()) - { - return sha256.ComputeHash(hashData, 0, hashData.Length); - } - } - /// /// Calculates key exchange hash value. /// @@ -68,7 +52,7 @@ namespace Renci.SshNet.Security /// protected override byte[] CalculateHash() { - var keyExchangeHashData = new KeyExchangeHashData + var hashData = new KeyExchangeHashData { ClientVersion = Session.ClientVersion, ServerVersion = Session.ServerVersion, @@ -77,10 +61,10 @@ namespace Renci.SshNet.Security HostKey = _hostKey, ClientExchangeValue = _clientExchangeValue, ServerExchangeValue = _serverExchangeValue, - SharedKey = SharedKey + SharedKey = SharedKey, }; - return Hash(keyExchangeHashData.GetBytes()); + return Hash(hashData.GetBytes()); } /// @@ -118,5 +102,5 @@ namespace Renci.SshNet.Security _serverPayload = message.GetBytes(); _clientPayload = Session.ClientInitMessage.GetBytes(); } - } + } } \ No newline at end of file diff --git a/src/Renci.SshNet/Security/KeyExchangeECCurve25519.cs b/src/Renci.SshNet/Security/KeyExchangeECCurve25519.cs index 7d91abfe..9de7af8f 100644 --- a/src/Renci.SshNet/Security/KeyExchangeECCurve25519.cs +++ b/src/Renci.SshNet/Security/KeyExchangeECCurve25519.cs @@ -1,4 +1,5 @@ using System; +using Renci.SshNet.Abstractions; using Renci.SshNet.Common; using Renci.SshNet.Messages.Transport; using Renci.SshNet.Security.Chaos.NaCl; @@ -65,6 +66,21 @@ namespace Renci.SshNet.Security Session.KeyExchangeEcdhReplyMessageReceived -= Session_KeyExchangeEcdhReplyMessageReceived; } + /// + /// Hashes the specified data bytes. + /// + /// The hash data. + /// + /// Hashed bytes + /// + protected override byte[] Hash(byte[] hashData) + { + using (var sha256 = CryptoAbstraction.CreateSHA256()) + { + return sha256.ComputeHash(hashData, 0, hashData.Length); + } + } + private void Session_KeyExchangeEcdhReplyMessageReceived(object sender, MessageEventArgs e) { var message = e.Message; @@ -92,7 +108,7 @@ namespace Renci.SshNet.Security var sharedKey = new byte[MontgomeryCurve25519.PublicKeySizeInBytes]; MontgomeryOperations.scalarmult(sharedKey, 0, _privateKey, 0, serverExchangeValue, 0); - SharedKey = sharedKey.ToBigNum2(); + SharedKey = sharedKey.ToBigInteger2().ToByteArray().Reverse(); } } } diff --git a/src/Renci.SshNet/Security/KeyExchangeECDH.cs b/src/Renci.SshNet/Security/KeyExchangeECDH.cs index a32efd86..f87a5c7e 100644 --- a/src/Renci.SshNet/Security/KeyExchangeECDH.cs +++ b/src/Renci.SshNet/Security/KeyExchangeECDH.cs @@ -100,7 +100,7 @@ namespace Renci.SshNet.Security var publicKey = new ECPublicKeyParameters("ECDH", q, DomainParameters); var k1 = KeyAgreement.CalculateAgreement(publicKey); - SharedKey = k1.ToByteArray().ToBigNum2(); + SharedKey = k1.ToByteArray().ToBigInteger2().ToByteArray().Reverse(); } } } diff --git a/src/Renci.SshNet/Security/KeyExchangeECDH256.cs b/src/Renci.SshNet/Security/KeyExchangeECDH256.cs index 84a8d12b..8f14d9bd 100644 --- a/src/Renci.SshNet/Security/KeyExchangeECDH256.cs +++ b/src/Renci.SshNet/Security/KeyExchangeECDH256.cs @@ -1,4 +1,5 @@ -using Renci.SshNet.Security.Org.BouncyCastle.Asn1.Sec; +using Renci.SshNet.Abstractions; +using Renci.SshNet.Security.Org.BouncyCastle.Asn1.Sec; using Renci.SshNet.Security.Org.BouncyCastle.Asn1.X9; namespace Renci.SshNet.Security @@ -34,5 +35,20 @@ namespace Renci.SshNet.Security { get { return 256; } } + + /// + /// Hashes the specified data bytes. + /// + /// The hash data. + /// + /// Hashed bytes + /// + protected override byte[] Hash(byte[] hashData) + { + using (var sha256 = CryptoAbstraction.CreateSHA256()) + { + return sha256.ComputeHash(hashData, 0, hashData.Length); + } + } } } \ No newline at end of file diff --git a/src/Renci.SshNet/Security/KeyExchangeHash.cs b/src/Renci.SshNet/Security/KeyExchangeHash.cs index c5b1003d..e33cb14c 100644 --- a/src/Renci.SshNet/Security/KeyExchangeHash.cs +++ b/src/Renci.SshNet/Security/KeyExchangeHash.cs @@ -1,6 +1,5 @@ using Renci.SshNet.Common; using System; -using System.Linq; namespace Renci.SshNet.Security { diff --git a/src/Renci.SshNet/Security/KeyHostAlgorithm.cs b/src/Renci.SshNet/Security/KeyHostAlgorithm.cs index e9899214..ca94fa70 100644 --- a/src/Renci.SshNet/Security/KeyHostAlgorithm.cs +++ b/src/Renci.SshNet/Security/KeyHostAlgorithm.cs @@ -82,7 +82,7 @@ namespace Renci.SshNet.Security private class SshKeyData : SshData { private byte[] _name; - private IList _keys; + private List _keys; public BigInteger[] Keys {