Преглед изворни кода

Partially revert changes from #643.
Fixes #653.

drieseng пре 5 година
родитељ
комит
1a803b50ee

+ 0 - 15
src/Renci.SshNet/Common/Extensions.cs

@@ -72,21 +72,6 @@ namespace Renci.SshNet.Common
             return new BigInteger(reversed.Reverse());
         }
 
-        /// <summary>
-        /// Initializes a new instance of the <see cref="BigInteger"/> structure using the SSH BigNum2 Format
-        /// </summary>
-        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;
-        }
-
         /// <summary>
         /// Initializes a new instance of the <see cref="BigInteger"/> structure using the SSH BigNum2 Format
         /// </summary>

+ 17 - 9
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<byte>(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
         /// <returns></returns>
         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; }
 
             /// <summary>
@@ -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; }
 
             /// <summary>

+ 4 - 20
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
         /// </value>
         protected abstract int HashSize { get; }
 
-        /// <summary>
-        /// Hashes the specified data bytes.
-        /// </summary>
-        /// <param name="hashData">The hash data.</param>
-        /// <returns>
-        /// Hashed bytes
-        /// </returns>
-        protected override byte[] Hash(byte[] hashData)
-        {
-            using (var sha256 = CryptoAbstraction.CreateSHA256())
-            {
-                return sha256.ComputeHash(hashData, 0, hashData.Length);
-            }
-        }
-
         /// <summary>
         /// Calculates key exchange hash value.
         /// </summary>
@@ -68,7 +52,7 @@ namespace Renci.SshNet.Security
         /// </returns>
         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());
         }
 
         /// <summary>
@@ -118,5 +102,5 @@ namespace Renci.SshNet.Security
             _serverPayload = message.GetBytes();
             _clientPayload = Session.ClientInitMessage.GetBytes();
         }
-    }
+   }
 }

+ 17 - 1
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;
         }
 
+        /// <summary>
+        /// Hashes the specified data bytes.
+        /// </summary>
+        /// <param name="hashData">The hash data.</param>
+        /// <returns>
+        /// Hashed bytes
+        /// </returns>
+        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<KeyExchangeEcdhReplyMessage> 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();
         }
     }
 }

+ 1 - 1
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();
         }
     }
 }

+ 17 - 1
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; }
         }
+
+        /// <summary>
+        /// Hashes the specified data bytes.
+        /// </summary>
+        /// <param name="hashData">The hash data.</param>
+        /// <returns>
+        /// Hashed bytes
+        /// </returns>
+        protected override byte[] Hash(byte[] hashData)
+        {
+            using (var sha256 = CryptoAbstraction.CreateSHA256())
+            {
+                return sha256.ComputeHash(hashData, 0, hashData.Length);
+            }
+        }
     }
 }

+ 0 - 1
src/Renci.SshNet/Security/KeyExchangeHash.cs

@@ -1,6 +1,5 @@
 using Renci.SshNet.Common;
 using System;
-using System.Linq;
 
 namespace Renci.SshNet.Security
 {

+ 1 - 1
src/Renci.SshNet/Security/KeyHostAlgorithm.cs

@@ -82,7 +82,7 @@ namespace Renci.SshNet.Security
         private class SshKeyData : SshData
         {
             private byte[] _name;
-            private IList<byte[]> _keys;
+            private List<byte[]> _keys;
 
             public BigInteger[] Keys
             {