Morvader
Morvader

Reputation: 2313

FTPS Server using .NET SslStream

I'm developing a FTP server in C#, I just finished implementing FTPS explicit mode functionality using SslStream class and everything goes almost ok.

I'm having problems using fileZilla > 3.0.11 as client. I google arround, and it seems that sslstream implementation does not close the connection properly. (not sending close_notify alert). Using WinScp, SmartFTP and lftp everithing works fine.

Any ideas or any other SSL library?

Or maybe some way to hardcode the close_notify alert and send it?

Concrete code example would be great!

Creating sslStream:

_sslStream = new SslStream(socket.GetStream());      
var _cert = new X509Certificate2(certPath,pass);    
_sslStream.AuthenticateAsServer(_cert);

Closing connections:

_sslStream.Close();
socket.Close();
_sslStream = null;
socket = null;

FileZilla 3.6.0.2 Error log:

Response:   150 Opening data connection for LIST
Trace:  CFtpControlSocket::TransferParseResponse()
Trace:    code = 1
Trace:    state = 4
Trace:  CFtpControlSocket::SendNextCommand()
Trace:  CFtpControlSocket::TransferSend()
Trace:    state = 5
Trace:  CTlsSocket::OnRead()
Trace:  CTlsSocket::ContinueHandshake()
Trace:  TLS Handshake successful
Trace:  TLS Session resumed
Trace:  Cipher: AES-128-CBC, MAC: SHA1
Trace:  CTransferSocket::OnConnect
Trace:  CTransferSocket::OnReceive(), m_transferMode=0
Trace:  CTlsSocket::Failure(-110, 0)
Error:  GnuTLS error -110 in gnutls_record_recv: The TLS connection was non-properly terminated.
Error:  Could not read from transfer socket: ECONNABORTED - Connection aborted
Trace:  CTransferSocket::TransferEnd(3)
Trace:  CFtpControlSocket::TransferEnd()
Trace:  CTlsSocket::OnRead()
Trace:  CFtpControlSocket::OnReceive()
Response:   226 LIST successful.

Upvotes: 6

Views: 3228

Answers (4)

Neco
Neco

Reputation: 549

Please have a look at a workaround I posted here. It would be great if all together we could make this workaround better.

Upvotes: 3

Rots
Rots

Reputation: 5586

How about

_sslStream.Dispose();

I'm wondering if the Dispose method handles the close_notify.

Upvotes: 0

CSharpie
CSharpie

Reputation: 9477

What happens if you call Shutdown on the Socket before closing it?

socket.Shutdown(SocketShutdown.Both);

Upvotes: 0

Nickolay Olshevsky
Nickolay Olshevsky

Reputation: 14160

I think, you have a real reason to re-invent the wheel. Already there are libraries which implements FTPS server in C#/.NET, like SecureBlackbox (however, it is commercial).

Upvotes: 5

Related Questions