Browse Source

Code Cleanup
Some TODO items
Replace IEnumerable<byte> with byte[] to reduce calling .ToArray() function
Fix ListDirectory ( close open directory handle)

olegkap_cp 14 years ago
parent
commit
34a5cba265

+ 0 - 4
Renci.SshClient/Renci.SshNet/Common/DerData.cs

@@ -225,16 +225,12 @@ namespace Renci.SshNet.Common
                 }
             }
 
-
-
             this._data.Add(OBJECTIDENTIFIER);
             var length = this.GetLength(bytes.Count);
             this.WriteBytes(length);
             this.WriteBytes(bytes);
         }
 
-
-
         /// <summary>
         /// Writes NULL data into internal buffer.
         /// </summary>

+ 2 - 2
Renci.SshClient/Renci.SshNet/Common/SshData.cs

@@ -32,7 +32,7 @@ namespace Renci.SshNet.Common
             }
         }
 
-        private IEnumerable<byte> _loadedData;
+        private byte[] _loadedData;
 
         /// <summary>
         /// Gets the index that represents zero in current data type.
@@ -93,7 +93,7 @@ namespace Renci.SshNet.Common
         /// Loads data bytes into internal buffer.
         /// </summary>
         /// <param name="bytes">The bytes.</param>
-        protected void LoadBytes(IEnumerable<byte> bytes)
+        protected void LoadBytes(byte[] bytes)
         {
             this.ResetReader();
             this._loadedData = bytes;

+ 1 - 2
Renci.SshClient/Renci.SshNet/ForwardedPortLocal.NET40.cs

@@ -85,8 +85,7 @@ namespace Renci.SshNet
                 return;
 
             this._listener.Stop();
-            //  TODO:   Add timeout to WaitOne method
-            this._listenerTaskCompleted.WaitOne();
+            this._listenerTaskCompleted.WaitOne(this.Session.ConnectionInfo.Timeout);
             this._listenerTaskCompleted.Dispose();
             this._listenerTaskCompleted = null;
 

+ 1 - 1
Renci.SshClient/Renci.SshNet/Messages/Transport/KeyExchangeInitMessage.cs

@@ -27,7 +27,7 @@ namespace Renci.SshNet.Messages.Transport
         /// <summary>
         /// Gets session cookie.
         /// </summary>
-        public IEnumerable<byte> Cookie { get; private set; }
+        public byte[] Cookie { get; private set; }
 
         /// <summary>
         /// Gets or sets supported key exchange algorithms.

+ 15 - 18
Renci.SshClient/Renci.SshNet/PrivateKeyFile.cs

@@ -22,9 +22,9 @@ namespace Renci.SshNet
     public class PrivateKeyFile
     {
 #if SILVERLIGHT
-        private static Regex _privateKeyRegex = new Regex(@"^-----BEGIN (?<keyName>\w+) PRIVATE KEY-----\r?\n(Proc-Type: 4,ENCRYPTED\r?\nDEK-Info: (?<cipherName>[A-Z0-9-]+),(?<salt>[A-F0-9]{16})\r?\n\r?\n)?(?<data>([a-zA-Z0-9/+=]{1,64}\r?\n)+)-----END \k<keyName> PRIVATE KEY-----.*", RegexOptions.Multiline);
+        private static Regex _privateKeyRegex = new Regex(@"^-----BEGIN (?<keyName>\w+) PRIVATE KEY-----\r?\n(Proc-Type: 4,ENCRYPTED\r?\nDEK-Info: (?<cipherName>[A-Z0-9-]+),(?<salt>[A-F0-9]+)\r?\n\r?\n)?(?<data>([a-zA-Z0-9/+=]{1,64}\r?\n)+)-----END \k<keyName> PRIVATE KEY-----.*", RegexOptions.Multiline);
 #else
-        private static Regex _privateKeyRegex = new Regex(@"^-----BEGIN (?<keyName>\w+) PRIVATE KEY-----\r?\n(Proc-Type: 4,ENCRYPTED\r?\nDEK-Info: (?<cipherName>[A-Z0-9-]+),(?<salt>[A-F0-9]{16})\r?\n\r?\n)?(?<data>([a-zA-Z0-9/+=]{1,64}\r?\n)+)-----END \k<keyName> PRIVATE KEY-----.*", RegexOptions.Compiled | RegexOptions.Multiline);
+        private static Regex _privateKeyRegex = new Regex(@"^-----BEGIN (?<keyName>\w+) PRIVATE KEY-----\r?\n(Proc-Type: 4,ENCRYPTED\r?\nDEK-Info: (?<cipherName>[A-Z0-9-]+),(?<salt>[A-F0-9]+)\r?\n\r?\n)?(?<data>([a-zA-Z0-9/+=]{1,64}\r?\n)+)-----END \k<keyName> PRIVATE KEY-----.*", RegexOptions.Compiled | RegexOptions.Multiline);
 #endif
 
         /// <summary>
@@ -117,7 +117,7 @@ namespace Renci.SshNet
 
             var binaryData = System.Convert.FromBase64String(data);
 
-            IEnumerable<byte> decryptedData;
+            byte[] decryptedData;
 
             if (!string.IsNullOrEmpty(cipherName) && !string.IsNullOrEmpty(salt))
             {
@@ -138,23 +138,20 @@ namespace Renci.SshNet
                         cipher = new CipherInfo(192, (key, iv) => { return new TripleDesCipher(key, new CfbCipherMode(iv), new PKCS7Padding()); });
                         break;
                     case "DES-CBC":
-                        //  TODO:   Not tested
                         cipher = new CipherInfo(64, (key, iv) => { return new DesCipher(key, new CbcCipherMode(iv), new PKCS7Padding()); });
                         break;
-                    case "AES-128-CBC":
-                        //  TODO:   Not tested
-                        cipher = new CipherInfo(128, (key, iv) => { return new AesCipher(key, new CbcCipherMode(iv), new PKCS7Padding()); });
-                        break;
-                    case "AES-192-CBC":
-                        //  TODO:   Not tested
-                        cipher = new CipherInfo(192, (key, iv) => { return new AesCipher(key, new CbcCipherMode(iv), new PKCS7Padding()); });
-                        break;
-                    case "AES-256-CBC":
-                        //  TODO:   Not tested
-                        cipher = new CipherInfo(256, (key, iv) => { return new AesCipher(key, new CbcCipherMode(iv), new PKCS7Padding()); });
-                        break;
+                        //  TODO:   Implement more private key ciphers
+                    //case "AES-128-CBC":
+                    //    cipher = new CipherInfo(128, (key, iv) => { return new AesCipher(key, new CbcCipherMode(iv), new PKCS7Padding()); });
+                    //    break;
+                    //case "AES-192-CBC":
+                    //    cipher = new CipherInfo(192, (key, iv) => { return new AesCipher(key, new CbcCipherMode(iv), new PKCS7Padding()); });
+                    //    break;
+                    //case "AES-256-CBC":
+                    //    cipher = new CipherInfo(256, (key, iv) => { return new AesCipher(key, new CbcCipherMode(iv), new PKCS7Padding()); });
+                    //    break;
                     default:
-                        throw new SshException(string.Format(CultureInfo.CurrentCulture, "Unknown private key cipher \"{0}\".", cipherName));
+                        throw new SshException(string.Format(CultureInfo.CurrentCulture, "Private key cipher \"{0}\" is not supported.", cipherName));
                 }
 
                 decryptedData = DecryptKey(cipher, binaryData, passPhrase, binarySalt);
@@ -186,7 +183,7 @@ namespace Renci.SshNet
         /// <param name="binarySalt">Decryption binary salt.</param>
         /// <returns></returns>
         /// <exception cref="ArgumentNullException"><paramref name="cipherInfo"/>, <paramref name="cipherData"/>, <paramref name="passPhrase"/> or <paramref name="binarySalt"/> is null.</exception>
-        public static IEnumerable<byte> DecryptKey(CipherInfo cipherInfo, byte[] cipherData, string passPhrase, byte[] binarySalt)
+        public static byte[] DecryptKey(CipherInfo cipherInfo, byte[] cipherData, string passPhrase, byte[] binarySalt)
         {
             if (cipherInfo == null)
                 throw new ArgumentNullException("cipherInfo");

+ 1 - 1
Renci.SshClient/Renci.SshNet/Security/Cryptography/Ciphers/DesCipher.cs

@@ -300,7 +300,7 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
         /// </returns>
 		protected override bool ValidateKeySize(int keySize)
 		{
-			if (keySize == 56)
+			if (keySize == 64)
 				return true;
 			else
 				return false;

+ 7 - 8
Renci.SshClient/Renci.SshNet/Security/KeyExchange.cs

@@ -285,11 +285,10 @@ namespace Renci.SshNet.Security
         /// </summary>
         /// <param name="hashBytes">Data to hash.</param>
         /// <returns>Hashed bytes</returns>
-        protected virtual byte[] Hash(IEnumerable<byte> hashBytes)
+        protected virtual byte[] Hash(byte[] hashData)
         {
             using (var sha1 = new Renci.SshNet.Security.Cryptography.SHA1Hash())
             {
-                var hashData = hashBytes.ToArray();
                 return sha1.ComputeHash(hashData, 0, hashData.Length);
             }
         }
@@ -311,7 +310,7 @@ namespace Renci.SshNet.Security
         /// <param name="key">The key.</param>
         /// <param name="size">The size.</param>
         /// <returns></returns>
-        private byte[] GenerateSessionKey(BigInteger sharedKey, byte[] exchangeHash, IEnumerable<byte> key, int size)
+        private byte[] GenerateSessionKey(BigInteger sharedKey, byte[] exchangeHash, byte[] key, int size)
         {
             var result = new List<byte>(key);
             while (size > result.Count)
@@ -335,7 +334,7 @@ namespace Renci.SshNet.Security
         /// <param name="p">The p.</param>
         /// <param name="sessionId">The session id.</param>
         /// <returns></returns>
-        private IEnumerable<byte> GenerateSessionKey(BigInteger sharedKey, IEnumerable<byte> exchangeHash, char p, IEnumerable<byte> sessionId)
+        private byte[] GenerateSessionKey(BigInteger sharedKey, byte[] exchangeHash, char p, byte[] sessionId)
         {
             return new _SessionKeyGeneration
             {
@@ -349,9 +348,9 @@ namespace Renci.SshNet.Security
         private class _SessionKeyGeneration : SshData
         {
             public BigInteger SharedKey { get; set; }
-            public IEnumerable<byte> ExchangeHash { get; set; }
+            public byte[] ExchangeHash { get; set; }
             public char Char { get; set; }
-            public IEnumerable<byte> SessionId { get; set; }
+            public byte[] SessionId { get; set; }
 
             protected override void LoadData()
             {
@@ -370,8 +369,8 @@ namespace Renci.SshNet.Security
         private class _SessionKeyAdjustment : SshData
         {
             public BigInteger SharedKey { get; set; }
-            public IEnumerable<byte> ExcahngeHash { get; set; }
-            public IEnumerable<byte> Key { get; set; }
+            public byte[] ExcahngeHash { get; set; }
+            public byte[] Key { get; set; }
 
             protected override void LoadData()
             {

+ 1 - 7
Renci.SshClient/Renci.SshNet/Security/KeyExchangeDiffieHellman.cs

@@ -73,15 +73,9 @@ namespace Renci.SshNet.Security
         {
             var exchangeHash = this.CalculateHash();
 
-            //  TODO:   See how to improve this area
-
-            var bytes = this._hostKey;
-
             var length = (uint)(this._hostKey[0] << 24 | this._hostKey[1] << 16 | this._hostKey[2] << 8 | this._hostKey[3]);
 
-            var algorithmName = Renci.SshNet.Common.ASCIIEncoding.Current.GetString(bytes.Skip(4).Take((int)length).ToArray());
-
-            var data = bytes.Skip(4 + algorithmName.Length);
+            var algorithmName = Renci.SshNet.Common.ASCIIEncoding.Current.GetString(this._hostKey, 4, (int)length);
 
             var key = this.Session.ConnectionInfo.HostKeyAlgorithms[algorithmName](this._hostKey);
 

+ 2 - 2
Renci.SshClient/Renci.SshNet/Security/KeyExchangeDiffieHellmanGroupExchangeSha256.cs

@@ -88,11 +88,11 @@ namespace Renci.SshNet.Security
         /// <returns>
         /// Hashed bytes
         /// </returns>
-        protected override byte[] Hash(IEnumerable<byte> hashBytes)
+        protected override byte[] Hash(byte[] hashBytes)
         {
             using (var md = new SHA256Hash())
             {
-                return md.ComputeHash(hashBytes.ToArray());
+                return md.ComputeHash(hashBytes);
             }
         }
 

+ 0 - 55
Renci.SshClient/Renci.SshNet/Sftp/SftpFileStream.cs

@@ -295,65 +295,10 @@ namespace Renci.SshNet.Sftp
 
             if (mode == FileMode.Append)
             {
-                //  TODO:   Validate Size property value exists
                 this._position = this._attributes.Size;
             }
         }
 
-        internal SftpFileStream(SftpSession session, byte[] handle, FileAccess access)
-            : this(session, handle, access, true, 4096, false)
-        {
-            // Nothing to do here.
-        }
-
-        internal SftpFileStream(SftpSession session, byte[] handle, FileAccess access, bool ownsHandle)
-            : this(session, handle, access, ownsHandle, 4096, false)
-        {
-            // Nothing to do here.
-        }
-
-        internal SftpFileStream(SftpSession session, byte[] handle, FileAccess access, bool ownsHandle, int bufferSize)
-            : this(session, handle, access, ownsHandle, bufferSize, false)
-        {
-            // Nothing to do here.
-        }
-
-        internal SftpFileStream(SftpSession session, byte[] handle, FileAccess access, bool ownsHandle, int bufferSize, bool isAsync)
-        {
-            //  TODO:   See if it make sense to have "handle" constructors
-
-            // Validate the parameters.
-            if (bufferSize <= 0)
-            {
-                throw new ArgumentOutOfRangeException("bufferSize");
-            }
-            if (access < FileAccess.Read || access > FileAccess.ReadWrite)
-            {
-                throw new ArgumentOutOfRangeException("access");
-            }
-
-            // Initialize the object state.
-            this._handle = handle;
-            this._access = access;
-            this._ownsHandle = ownsHandle;
-            this._isAsync = isAsync;
-            this._bufferSize = bufferSize;
-            this._buffer = new byte[bufferSize];
-            this._bufferPosn = 0;
-            this._bufferLen = 0;
-            this._bufferOwnedByWrite = false;
-            this._canSeek = true;
-            this._position = 0; //  Assumption is that no other object uses the same file handle
-
-            this._attributes = this._session.RequestFStat(this._handle);
-
-            //if (mode == FileMode.Append)
-            //{
-            //    //  TODO:   Validate Size property value exists
-            //    this._position = (long)this._attributes.Size.Value;
-            //}
-        }
-
         /// <summary>
         /// Releases unmanaged resources and performs other cleanup operations before the
         /// <see cref="SftpFileStream"/> is reclaimed by garbage collection.

+ 0 - 2
Renci.SshClient/Renci.SshNet/Sftp/SftpSession.cs

@@ -252,8 +252,6 @@ namespace Renci.SshNet.Sftp
             if (string.IsNullOrEmpty(partialFullPath))
                 partialFullPath = "/";
 
-            //canonizedPath = this.RequestRealPath(partialFullPath).First().FullName;
-
             realPathFiles = this.RequestRealPath(partialFullPath, true);
 
             if (realPathFiles != null)

+ 2 - 0
Renci.SshClient/Renci.SshNet/SftpClient.cs

@@ -1100,6 +1100,8 @@ namespace Renci.SshNet
 
                 files = this._sftpSession.RequestReadDir(handle);
             }
+            
+            this._sftpSession.RequestClose(handle);
 
             return result;
         }

+ 1 - 2
Renci.SshClient/Renci.SshNet/Shell.cs

@@ -252,8 +252,7 @@ namespace Renci.SshNet
             this._input.Dispose();
             this._input = null;
 
-            //  TODO:   Add timeout to WaitOne method
-            this._dataReaderTaskCompleted.WaitOne();
+            this._dataReaderTaskCompleted.WaitOne(this._session.ConnectionInfo.Timeout);
             this._dataReaderTaskCompleted.Dispose();
             this._dataReaderTaskCompleted = null;