Bug 54343 - HttpListener SSL connection reuse throws InvalidOperationException
Summary: HttpListener SSL connection reuse throws InvalidOperationException
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Net.Http ()
Version: 4.8.0 (C9)
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Martin Baulig
Depends on:
Reported: 2017-03-31 15:08 UTC by Martin Frydl
Modified: 2017-05-11 20:41 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Test case (832 bytes, text/x-csharp)
2017-03-31 15:08 UTC, Martin Frydl
patch (768 bytes, patch)
2017-03-31 18:45 UTC, Martin Frydl

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Martin Frydl 2017-03-31 15:08:43 UTC
Created attachment 21111 [details]
Test case

When SSL connection to HttpListener is reused by client for second call, the server throws InvalidOperationException. In asynchronous case this exception cannot be handled by user code since it is thrown only inside Mono code in separate handler thread.

Compile the attached test case, generate some certificate for the server and install it to port 4040 using httpcfg tool. Then run the test and try to connect to it with browser or just 'curl -k https://localhost:4040/'. It will return the response but the server will crash with following exception:

System.InvalidOperationException: Operation is not valid due to the current state of the object.
  at Mono.Net.Security.MobileAuthenticatedStream.ValidateCreateContext (System.Boolean serverMode, System.String targetHost, System.Security.Authentication.SslProtocols enabledProtocols, System.Security.Cryptography.X509Certificates.X509Certificate serverCertificate, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Boolean clientCertRequired) [0x0006d] in /builddir/build/BUILD/mono-4.8.0/mcs/class/System/Mono.Net.Security/MobileAuthenticatedStream.cs:263
  at Mono.Net.Security.MobileAuthenticatedStream.AuthenticateAsServer (System.Security.Cryptography.X509Certificates.X509Certificate serverCertificate, System.Boolean clientCertificateRequired, System.Security.Authentication.SslProtocols enabledSslProtocols, System.Boolean checkCertificateRevocation) [0x00000] in /builddir/build/BUILD/mono-4.8.0/mcs/class/System/Mono.Net.Security/MobileAuthenticatedStream.cs:154
  at Mono.Net.Security.Private.MonoSslStreamWrapper.AuthenticateAsServer (System.Security.Cryptography.X509Certificates.X509Certificate serverCertificate, System.Boolean clientCertificateRequired, System.Security.Authentication.SslProtocols enabledSslProtocols, System.Boolean checkCertificateRevocation) [0x00000] in /builddir/build/BUILD/mono-4.8.0/mcs/class/System/Mono.Net.Security/MonoSslStreamWrapper.cs:100
  at System.Net.HttpConnection.Init () [0x0000b] in /builddir/build/BUILD/mono-4.8.0/mcs/class/System/System.Net/HttpConnection.cs:114
  at System.Net.HttpConnection.Close (System.Boolean force_close) [0x000e3] in /builddir/build/BUILD/mono-4.8.0/mcs/class/System/System.Net/HttpConnection.cs:474
  at System.Net.HttpListenerResponse.Close (System.Boolean force) [0x00007] in /builddir/build/BUILD/mono-4.8.0/mcs/class/System/System.Net/HttpListenerResponse.cs:285
  at System.Net.HttpListenerResponse.Close () [0x0000c] in /builddir/build/BUILD/mono-4.8.0/mcs/class/System/System.Net/HttpListenerResponse.cs:293
  at test.ChunkingError.Process (System.Net.HttpListenerContext context) [0x00035] in <c2e1490814ee475ab31567a7019c8dcb>:0
  at test.ChunkingError.Main (System.String[] args) [0x0002d] in <c2e1490814ee475ab31567a7019c8dcb>:0
Comment 1 Martin Frydl 2017-03-31 18:45:07 UTC
Created attachment 21116 [details]

The problem is in initialization of SSL stream before each request (creating SSL context). In OpenSSL it is initialized only once per connection (when opened) and then stream is just used. The attached patch moves the initialization from HttpConnection.Init() (which is called from constructor on open and also from Close() when reused) to constructor.