private async Task <Stream> CreateSslStream(Stream unencryptedStream) { var sslStream = new FixedSslStream(unencryptedStream, false); await sslStream.AuthenticateAsServerAsync(AuthTlsCommandHandler.ServerCertificate); return(sslStream); }
private async Task<FtpResponse> ElevateToTls(CancellationToken cancellationToken) { await Connection.WriteAsync(new FtpResponse(234, "Enabling TLS Connection"), cancellationToken); await Connection.SocketStream.FlushAsync(cancellationToken); try { var sslStream = new FixedSslStream(Connection.OriginalStream, true); Connection.SocketStream = sslStream; await sslStream.AuthenticateAsServerAsync(ServerCertificate); return null; } catch (Exception ex) { Connection.Log?.Warn(ex, "SSL stream authentication failed: {0}", ex.Message); return new FtpResponse(421, "TLS authentication failed"); } }
private async Task <FtpResponse> ElevateToTls(CancellationToken cancellationToken) { await Connection.WriteAsync(new FtpResponse(234, "Enabling TLS Connection"), cancellationToken); await Connection.SocketStream.FlushAsync(cancellationToken); try { var sslStream = new FixedSslStream(Connection.OriginalStream, true); Connection.SocketStream = sslStream; await sslStream.AuthenticateAsServerAsync(ServerCertificate); return(null); } catch (Exception ex) { Connection.Log?.Warn(ex, "SSL stream authentication failed: {0}", ex.Message); return(new FtpResponse(421, "TLS authentication failed")); } }
private async Task<Stream> CreateSslStream(Stream unencryptedStream) { var sslStream = new FixedSslStream(unencryptedStream, false); await sslStream.AuthenticateAsServerAsync(AuthTlsCommandHandler.ServerCertificate); return sslStream; }
private static void Main() { // Load server certificate var cert = new X509Certificate2("test.pfx"); AuthTlsCommandHandler.ServerCertificate = cert; // Only allow anonymous login var membershipProvider = new AnonymousMembershipProvider(new NoValidation()); // Use the .NET file system var fsProvider = new DotNetFileSystemProvider(Path.Combine(Path.GetTempPath(), "TestFtpServer")); // Use all commands from the FtpServer assembly and the one(s) from the AuthTls assembly var commandFactory = new AssemblyFtpCommandHandlerFactory(typeof(FtpServer).Assembly, typeof(AuthTlsCommandHandler).Assembly); // Initialize the FTP server using (var ftpServer = new FtpServer(fsProvider, membershipProvider, "127.0.0.1", Port, commandFactory) { DefaultEncoding = Encoding.ASCII, LogManager = new FtpLogManager(), }) { #if USE_FTPS_IMPLICIT // Use an implicit SSL connection (without the AUTHTLS command) ftpServer.ConfigureConnection += (s, e) => { var sslStream = new FixedSslStream(e.Connection.OriginalStream); sslStream.AuthenticateAsServer(cert); e.Connection.SocketStream = sslStream; }; #endif // Create the default logger var log = ftpServer.LogManager?.CreateLog(typeof(Program)); try { // Start the FTP server ftpServer.Start(); Console.WriteLine("Press ENTER/RETURN to close the test application."); Console.ReadLine(); // Stop the FTP server ftpServer.Stop(); } catch (Exception ex) { log?.Error(ex, "Error during main FTP server loop"); } } }