diff --git a/DearFTP/Connection/Commands/ListCommand.cs b/DearFTP/Connection/Commands/ListCommand.cs index a75bc48..f202336 100644 --- a/DearFTP/Connection/Commands/ListCommand.cs +++ b/DearFTP/Connection/Commands/ListCommand.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; +using System.Net.Security; using System.Text; namespace DearFTP.Connection.Commands @@ -31,6 +32,8 @@ namespace DearFTP.Connection.Commands stream.Send(ResponseCode.FileStatusOK, "Listing coming."); + dataConnection.Authenticate(); + string path = null; bool humanReadable = false; diff --git a/DearFTP/Connection/Commands/ListMachineCommand.cs b/DearFTP/Connection/Commands/ListMachineCommand.cs index 0db2413..9100137 100644 --- a/DearFTP/Connection/Commands/ListMachineCommand.cs +++ b/DearFTP/Connection/Commands/ListMachineCommand.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Net.Security; namespace DearFTP.Connection.Commands { @@ -71,6 +72,8 @@ namespace DearFTP.Connection.Commands stream.Send(ResponseCode.FileStatusOK, "Listing coming."); + dataConnection.Authenticate(); + var dataStream = new FtpStream(dataConnection.Stream); foreach (var info in infos) diff --git a/DearFTP/Connection/Commands/RetrieveCommand.cs b/DearFTP/Connection/Commands/RetrieveCommand.cs index 6c730fa..a9f8b98 100644 --- a/DearFTP/Connection/Commands/RetrieveCommand.cs +++ b/DearFTP/Connection/Commands/RetrieveCommand.cs @@ -38,6 +38,8 @@ namespace DearFTP.Connection.Commands stream.Send(ResponseCode.FileStatusOK, "File coming."); + dataConnection.Authenticate(); + if (dataConnection.IsTlsProtected && !dataConnection.IsAvailable) { stream.Send(ResponseCode.DataConnectionOpenError, "Passive mode not activated."); diff --git a/DearFTP/Connection/Commands/StoreCommand.cs b/DearFTP/Connection/Commands/StoreCommand.cs index a105029..5bfb850 100644 --- a/DearFTP/Connection/Commands/StoreCommand.cs +++ b/DearFTP/Connection/Commands/StoreCommand.cs @@ -49,6 +49,8 @@ namespace DearFTP.Connection.Commands stream.Send(ResponseCode.FileStatusOK, "Waiting file."); + dataConnection.Authenticate(); + if (dataConnection.IsTlsProtected && !dataConnection.IsAvailable) { stream.Send(ResponseCode.DataConnectionOpenError, "Passive mode not activated."); diff --git a/DearFTP/Connection/DynamicDataConnection.cs b/DearFTP/Connection/DynamicDataConnection.cs index 3f017d5..17d5396 100644 --- a/DearFTP/Connection/DynamicDataConnection.cs +++ b/DearFTP/Connection/DynamicDataConnection.cs @@ -64,10 +64,7 @@ namespace DearFTP.Connection if (IsTlsProtected) { - var sslStream = new SslStream(_client.GetStream(), false); - sslStream.AuthenticateAsServer(FtpServer.Instance.Configuration.Tls.X509Certificate, false, true); - - Stream = sslStream; + Stream = new SslStream(_client.GetStream(), false); } else { @@ -76,6 +73,14 @@ namespace DearFTP.Connection }); } + public void Authenticate() + { + if (IsTlsProtected) + { + ((SslStream)Stream).AuthenticateAsServer(FtpServer.Instance.Configuration.Tls.X509Certificate, false, true); + } + } + public void ActivateTls() { IsTlsProtected = true; diff --git a/DearFTP/Connection/IDataConnection.cs b/DearFTP/Connection/IDataConnection.cs index 97df4dc..898e555 100644 --- a/DearFTP/Connection/IDataConnection.cs +++ b/DearFTP/Connection/IDataConnection.cs @@ -11,6 +11,7 @@ namespace DearFTP.Connection void Create(); void AcceptClient(); + void Authenticate(); void ActivateTls(); void DesactivateTls(); void Close(); diff --git a/DearFTP/Connection/StaticDataConnection.cs b/DearFTP/Connection/StaticDataConnection.cs index cb04f9f..9445360 100644 --- a/DearFTP/Connection/StaticDataConnection.cs +++ b/DearFTP/Connection/StaticDataConnection.cs @@ -66,10 +66,7 @@ namespace DearFTP.Connection if (IsTlsProtected) { - var sslStream = new SslStream(_client.GetStream(), false); - sslStream.AuthenticateAsServer(FtpServer.Instance.Configuration.Tls.X509Certificate, false, true); - - Stream = sslStream; + Stream = new SslStream(_client.GetStream(), false); } else { @@ -78,6 +75,14 @@ namespace DearFTP.Connection }); } + public void Authenticate() + { + if (IsTlsProtected) + { + ((SslStream)Stream).AuthenticateAsServer(FtpServer.Instance.Configuration.Tls.X509Certificate, false, true); + } + } + public void ActivateTls() { IsTlsProtected = true;