.NET's SslStream
class does not send the close_notify
alert before closing the connection.
How can I send the close_notify
alert manually?
.NET's SslStream
class does not send the close_notify
alert before closing the connection.
How can I send the close_notify
alert manually?
Thanks for this question. It pointed me into the right direction, that there is a bug in .Net, which I do not very often think about.
I bumped into this problem during writing of my implementation of FTPS server and Filezilla (or GnuTLS probably) client was complaining "GnuTLS error -110 in gnutls_record_recv: The TLS connection was non-properly terminated". I think it is a quite significant drawback in SslStream implementation.
So I ended up with writing a wrapper which sends this alert before closing the stream:
And the following code is to make it working (this code requires assembly to be 'unsafe'):
Windows API used:
Reflection utilities:
I am not experienced in writing reliable interaction with unmanaged environment, so I hope somebody can have a look and fix issues (and maybe make it 'safe').
For the record, SslStream at least in .NET 2.0 also doesn't appear to response to a close_notify from the other side. This means that calling OpenSSL's SSL_Shutdown() properly, i.e. twice - once to initiate the shutdown and again to wait for the response - will hang on the second call.
It's a bug in .NET's usage of the underlying security API. Note another question by me about being unable to select a specific cypher suite - they really botched up this API wrapper...