浏览代码

Fix AES-256-CBC SSH private key decryption
Restore silverlight compatibility

olegkap_cp 13 年之前
父节点
当前提交
41bb6920d0

+ 3 - 0
Renci.SshClient/Renci.SshNet.Silverlight/Renci.SshNet.Silverlight.csproj

@@ -519,6 +519,9 @@
     <Compile Include="..\Renci.SshNet\Security\Cryptography\Ciphers\Paddings\PKCS7Padding.cs">
       <Link>Security\Cryptography\Ciphers\Paddings\PKCS7Padding.cs</Link>
     </Compile>
+    <Compile Include="..\Renci.SshNet\Security\Cryptography\Ciphers\Paddings\PKCS5Padding.cs">
+      <Link>Security\Cryptography\Ciphers\Paddings\PKCS5Padding.cs</Link>
+    </Compile>
     <Compile Include="..\Renci.SshNet\Security\Cryptography\Ciphers\RsaCipher.cs">
       <Link>Security\Cryptography\Ciphers\RsaCipher.cs</Link>
     </Compile>

+ 40 - 35
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<SocketAsyncEventArgs>(OnReceive);
-                    this._socket.ReceiveAsync(args);
-
-                    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);
-                    }
-
-                    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);
-                    }
+                SocketAsyncEventArgs args = new SocketAsyncEventArgs();
+                args.SetBuffer(buffer, offset + receivedTotal, length - receivedTotal);
+                args.UserToken = this._socket;
+                args.RemoteEndPoint = this._socket.RemoteEndPoint;
+                args.Completed += new EventHandler<SocketAsyncEventArgs>(OnReceive);
+                this._socket.ReceiveAsync(args);
+
+                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);
+                }
+
+                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);
+                }
             } 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;
                 }
             }
         }

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

+ 5 - 0
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;

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