From 41bb6920d06da09f64a9ae44d3d2016d34585fa8 Mon Sep 17 00:00:00 2001 From: olegkap_cp Date: Thu, 20 Dec 2012 01:41:43 +0000 Subject: [PATCH] Fix AES-256-CBC SSH private key decryption Restore silverlight compatibility --- .../Renci.SshNet.Silverlight.csproj | 3 + .../Session.SilverlightShared.cs | 67 ++++++++++--------- .../Renci.SshNet/PrivateKeyFile.cs | 2 +- Renci.SshClient/Renci.SshNet/Session.NET.cs | 5 ++ Renci.SshClient/Renci.SshNet/Session.cs | 6 +- 5 files changed, 50 insertions(+), 33 deletions(-) diff --git a/Renci.SshClient/Renci.SshNet.Silverlight/Renci.SshNet.Silverlight.csproj b/Renci.SshClient/Renci.SshNet.Silverlight/Renci.SshNet.Silverlight.csproj index 59fe0bc8..2bb0b735 100644 --- a/Renci.SshClient/Renci.SshNet.Silverlight/Renci.SshNet.Silverlight.csproj +++ b/Renci.SshClient/Renci.SshNet.Silverlight/Renci.SshNet.Silverlight.csproj @@ -519,6 +519,9 @@ Security\Cryptography\Ciphers\Paddings\PKCS7Padding.cs + + Security\Cryptography\Ciphers\Paddings\PKCS5Padding.cs + Security\Cryptography\Ciphers\RsaCipher.cs diff --git a/Renci.SshClient/Renci.SshNet.Silverlight/Session.SilverlightShared.cs b/Renci.SshClient/Renci.SshNet.Silverlight/Session.SilverlightShared.cs index 2e1699db..fc2e21e6 100644 --- a/Renci.SshClient/Renci.SshNet.Silverlight/Session.SilverlightShared.cs +++ b/Renci.SshClient/Renci.SshNet.Silverlight/Session.SilverlightShared.cs @@ -26,6 +26,11 @@ namespace Renci.SshNet private bool _isConnected = false; + partial void IsSocketConnected(ref bool isConnected) + { + isConnected = (!this._isDisconnecting && this._socket != null && this._socket.Connected && this._isAuthenticated && this._messageListenerCompleted != null && this._isConnected); + } + partial void SocketConnect(string host, int port) { var ep = new DnsEndPoint(host, port); @@ -94,39 +99,39 @@ namespace Renci.SshNet do { - SocketAsyncEventArgs args = new SocketAsyncEventArgs(); - args.SetBuffer(buffer, offset + receivedTotal, length - receivedTotal); - args.UserToken = this._socket; - args.RemoteEndPoint = this._socket.RemoteEndPoint; - args.Completed += new EventHandler(OnReceive); - this._socket.ReceiveAsync(args); + SocketAsyncEventArgs args = new SocketAsyncEventArgs(); + args.SetBuffer(buffer, offset + receivedTotal, length - receivedTotal); + args.UserToken = this._socket; + args.RemoteEndPoint = this._socket.RemoteEndPoint; + args.Completed += new EventHandler(OnReceive); + this._socket.ReceiveAsync(args); - this._receiveEvent.WaitOne(this.ConnectionInfo.Timeout); + this._receiveEvent.WaitOne(this.ConnectionInfo.Timeout); - if (args.SocketError == SocketError.WouldBlock || - args.SocketError == SocketError.IOPending || - args.SocketError == SocketError.NoBufferSpaceAvailable) - { - // socket buffer is probably empty, wait and try again - Thread.Sleep(30); - continue; - } - else if (args.SocketError != SocketError.Success) - { - throw new SocketException((int)args.SocketError); - } + if (args.SocketError == SocketError.WouldBlock || + args.SocketError == SocketError.IOPending || + args.SocketError == SocketError.NoBufferSpaceAvailable) + { + // socket buffer is probably empty, wait and try again + Thread.Sleep(30); + continue; + } + else if (args.SocketError != SocketError.Success) + { + throw new SocketException((int)args.SocketError); + } - var receivedBytes = args.BytesTransferred; + var receivedBytes = args.BytesTransferred; - if (receivedBytes > 0) - { - receivedTotal += receivedBytes; - continue; - } - else - { - throw new SshConnectionException("An established connection was aborted by the software in your host machine.", DisconnectReason.ConnectionLost); - } + if (receivedBytes > 0) + { + receivedTotal += receivedBytes; + continue; + } + else + { + throw new SshConnectionException("An established connection was aborted by the software in your host machine.", DisconnectReason.ConnectionLost); + } } while (receivedTotal < length); } @@ -174,8 +179,8 @@ namespace Renci.SshNet { foreach (var item in from m in this._messagesMetadata where m.Name == messageName select m) { - item.Enabled = true; - item.Activated = true; + item.Enabled = true; + item.Activated = true; } } } diff --git a/Renci.SshClient/Renci.SshNet/PrivateKeyFile.cs b/Renci.SshClient/Renci.SshNet/PrivateKeyFile.cs index 5ec87454..e3f94696 100644 --- a/Renci.SshClient/Renci.SshNet/PrivateKeyFile.cs +++ b/Renci.SshClient/Renci.SshNet/PrivateKeyFile.cs @@ -151,7 +151,7 @@ namespace Renci.SshNet 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 PKCS5Padding()); }); + cipher = new CipherInfo(256, (key, iv) => { return new AesCipher(key, new CbcCipherMode(iv), new PKCS7Padding()); }); break; default: throw new SshException(string.Format(CultureInfo.CurrentCulture, "Private key cipher \"{0}\" is not supported.", cipherName)); diff --git a/Renci.SshClient/Renci.SshNet/Session.NET.cs b/Renci.SshClient/Renci.SshNet/Session.NET.cs index fb95f21d..e6f415cd 100644 --- a/Renci.SshClient/Renci.SshNet/Session.NET.cs +++ b/Renci.SshClient/Renci.SshNet/Session.NET.cs @@ -22,6 +22,11 @@ namespace Renci.SshNet new TraceSource("SshNet.Logging"); #endif + partial void IsSocketConnected(ref bool isConnected) + { + isConnected = (!this._isDisconnecting && this._socket != null && this._socket.Connected && this._isAuthenticated && this._messageListenerCompleted != null) && !(this._socket.Poll(10, SelectMode.SelectRead)); + } + partial void SocketConnect(string host, int port) { IPAddress addr; diff --git a/Renci.SshClient/Renci.SshNet/Session.cs b/Renci.SshClient/Renci.SshNet/Session.cs index 8beb34a8..a55e06cc 100644 --- a/Renci.SshClient/Renci.SshNet/Session.cs +++ b/Renci.SshClient/Renci.SshNet/Session.cs @@ -183,7 +183,9 @@ namespace Renci.SshNet { get { - return (!this._isDisconnecting && this._socket != null && this._socket.Connected && this._isAuthenticated && this._messageListenerCompleted != null) && !(this._socket.Poll(10, SelectMode.SelectRead)); + var isSocketConnected = false; + IsSocketConnected(ref isSocketConnected); + return isSocketConnected; } } @@ -1552,6 +1554,8 @@ namespace Renci.SshNet partial void ExecuteThread(Action action); + partial void IsSocketConnected(ref bool isConnected); + partial void SocketConnect(string host, int port); partial void SocketDisconnect();