Bug 56145 - SslStream.AuthenticateAsServer throws System.IO.IOException randomly
Summary: SslStream.AuthenticateAsServer throws System.IO.IOException randomly
Status: NEEDINFO
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Security ()
Version: 5.0 (2017-02)
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Martin Baulig
URL:
Depends on:
Blocks:
 
Reported: 2017-05-09 14:35 UTC by Narinder
Modified: 2018-01-17 17:38 UTC (History)
4 users (show)

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


Attachments
Program to demonstrate a bug (10.42 KB, application/x-zip-compressed)
2017-09-06 15:19 UTC, Narinder
Details


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 for Bug 56145 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
NEEDINFO

Description Narinder 2017-05-09 14:35:32 UTC
Hi 
I am using Debia 8.5

Mono JIT compiler version 5.0.0 (explicit/1d0445b Mon  8 May 12:32:21 BST 2017)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
	TLS:           __thread
	SIGSEGV:       altstack
	Notifications: epoll
	Architecture:  amd64
	Disabled:      none
	Misc:          softdebug 
	LLVM:          supported, not enabled.
	GC:            sgen (concurrent by default)


I have the following piece of code :

                            ServerCertificate = new X509Certificate2(pfx);
                            stream.AuthenticateAsServer(ServerCertificate, false, tlsProtocol, true);
                            theSafeStream = stream;

where pfx is a byte array 

The code sometimes works and the intermittently throws an exception :


System.IO.IOException: The authentication or decryption has failed. ---> System.IO.IOException: Unable to read data from the transport connection: Operation on non-blocking socket would block. ---> System.Net.Sockets.SocketException: Operation on non-blocking socket would block
  at System.Net.Sockets.Socket.Receive (System.Byte[] buffer, System.Int32 offset, System.Int32 size, System.Net.Sockets.SocketFlags socketFlags) [0x00017] in /tmp/mono/mcs/class/referencesource/System/net/System/Net/Sockets/Socket.cs:1773 
  at System.Net.Sockets.NetworkStream.Read (System.Byte[] buffer, System.Int32 offset, System.Int32 size) [0x0009b] in /tmp/mono/mcs/class/referencesource/System/net/System/Net/Sockets/NetworkStream.cs:513 
   --- End of inner exception stack trace ---
  at System.Net.Sockets.NetworkStream.Read (System.Byte[] buffer, System.Int32 offset, System.Int32 size) [0x000c3] in /tmp/mono/mcs/class/referencesource/System/net/System/Net/Sockets/NetworkStream.cs:525 
  at Mono.Security.Protocol.Tls.RecordProtocol.ReceiveRecord (System.IO.Stream record) [0x0002c] in /tmp/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs:449 
  at Mono.Security.Protocol.Tls.SslServerStream.EndNegotiateHandshake (System.IAsyncResult asyncResult) [0x00081] in /tmp/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslServerStream.cs:252 
  at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (System.IAsyncResult asyncResult) [0x0000c] in /tmp/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslStreamBase.cs:101 
   --- End of inner exception stack trace ---

--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:151 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357 
 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:151 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357
Comment 1 Narinder 2017-05-09 14:41:43 UTC
.. I should add that for the above the program contains multiple appdomains, although the coed that raises the exception is always executed in the main appdomain. When the program is run with only one appdomain no exception is raised.
Comment 2 Narinder 2017-05-12 09:42:53 UTC
HI,
I have just tested with mono-5.0.0.100:

Mono JIT compiler version 5.0.0 (explicit/9667aa6 Fri 12 May 10:13:13 BST 2017)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
	TLS:           __thread
	SIGSEGV:       altstack
	Notifications: epoll
	Architecture:  amd64
	Disabled:      none
	Misc:          softdebug 
	LLVM:          supported, not enabled.
	GC:            sgen (concurrent by default)


I now have the following :

Mono.Btls.MonoBtlsException : Syscall
  at Mono.Btls.MonoBtlsContext.ProcessHandshake () [0x00038] in <00076989899b4137978c8b91a9734411>:0 
  at Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake (Mono.Net.Security.AsyncProtocolRequest asyncRequest, Mono.Net.Security.AsyncOperationStatus status) [0x0002a] in <00076989899b4137978c8b91a9734411>:0 
  at Mono.Net.Security.AsyncProtocolRequest.ProcessOperation (Mono.Net.Security.AsyncOperationStatus status) [0x0006b] in <00076989899b4137978c8b91a9734411>:0 
  at Mono.Net.Security.AsyncProtocolRequest.ProcessOperation () [0x0000d] in <00076989899b4137978c8b91a9734411>:0 
  at Mono.Net.Security.AsyncProtocolRequest.StartOperation () [0x0003c] in <00076989899b4137978c8b91a9734411>:0 
  at Mono.Net.Security.AsyncProtocolRequest.StartOperation (Mono.Net.Security.AsyncOperation operation) [0x00024] in <00076989899b4137978c8b91a9734411>:0 
  at Mono.Net.Security.MobileAuthenticatedStream.ProcessAuthentication (System.Net.LazyAsyncResult lazyResult) [0x00057] in <00076989899b4137978c8b91a9734411>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <f80db2b9b10a4c21bc15cd0bca16dccd>:0 
  at Mono.Net.Security.MobileAuthenticatedStream.ProcessAuthentication (System.Net.LazyAsyncResult lazyResult) [0x00078] in <00076989899b4137978c8b91a9734411>:0 
  at Mono.Net.Security.MobileAuthenticatedStream.AuthenticateAsClient (System.String targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Authentication.SslProtocols enabledSslProtocols, System.Boolean checkCertificateRevocation) [0x0000c] in <00076989899b4137978c8b91a9734411>:0 
  at System.Net.Security.SslStream.AuthenticateAsClient (System.String targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Authentication.SslProtocols enabledSslProtocols, System.Boolean checkCertificateRevocation) [0x00006] in <00076989899b4137978c8b91a9734411>:0 
  at CITRD.Network.ConnectedTLSServer..ctor (System.Net.Sockets.TcpClient tcpClient) [0x0003d] in <21fb22cca22c408885ba263c80ddf816>:0 
  at CITRD.Network.ConnectedTLSServer..ctor (System.String hostName, System.Int32 port) [0x00008] in <21fb22cca22c408885ba263c80ddf816>:0 
  at CITRD.GNP.TLSNetworkExecute.TCPConnect (System.String host, System.Int32 port) [0x0002c] in <21fb22cca22c408885ba263c80ddf816>:0 
  at CITRD.GNP.PDLTLSWireNetworkExecute.TLSPDLWireCall (BNPP.CIT.GPrimeWrap.PdlDictionary theDict, System.String theActive, System.String comment, System.String host, System.Int32 port, CITRD.GNP.IWiredPdlFactory theFactory, System.Threading.CancellationToken cancel) [0x00001] in <21fb22cca22c408885ba263c80ddf816>:0 
  at CITRD.UnitTest.GNPTLS.UnitTest_GNP+TLSNetwork.test_GNP_Service_Call_core (System.Func`3[T1,T2,TResult] serviceStarter, System.String extraCmdLines, CITRD.GNP.IWiredPdlFactory theWireFactory) [0x00265] in <223d7797ecab4046b253ed5b1d2c4ba1>:0 
  at CITRD.UnitTest.GNPTLS.UnitTest_GNP+TLSNetwork.test_TLSGNP_service_call_singledomain_gnp_pdl_factory (System.String factoryName) [0x00022] in <223d7797ecab4046b253ed5b1d2c4ba1>:0 
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <f80db2b9b10a4c21bc15cd0bca16dccd>:0 



I would like to add that the code works correctly on Windows .NET 4.6.1. The code is compiled with VS 2017 (same binaries used for Windows AND Debian/mono)
Comment 3 Narinder 2017-05-12 09:47:12 UTC
... hopefully a more helpful stacktrace :

System.Security.Cryptography.CryptographicException: Unable to decode certificate. ---> Mono.Btls.MonoBtlsException: Failed to read certificate from data.
  at Mono.Btls.MonoBtlsX509.LoadFromData (System.Byte[] buffer, Mono.Btls.MonoBtlsX509Format format) [0x0003f] in /tmp/mono/mcs/class/System/Mono.Btls/MonoBtlsX509.cs:171 
  at Mono.Btls.X509CertificateImplBtls.Import (System.Byte[] data) [0x0001c] in /tmp/mono/mcs/class/System/Mono.Btls/X509CertificateImplBtls.cs:394 
  at Mono.Btls.X509CertificateImplBtls.Import (System.Byte[] data, System.String password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags) [0x00009] in /tmp/mono/mcs/class/System/Mono.Btls/X509CertificateImplBtls.cs:359 
   --- End of inner exception stack trace ---
  at CITRD.Network.DomainControllerResource`2[Tstreamreader,TTaskbuilder].ReadAndBuildJob () [0x003f6] in <5b6f1546a8814ef0a3ec854a35fb10fd>:0 
  at CITRD.Network.DomainControllerResource`2+<Sync>d__12[Tstreamreader,TTaskbuilder].MoveNext () [0x0000f] in <5b6f1546a8814ef0a3ec854a35fb10fd>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:151 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357 
  at CITRD.Utils.AsyncHybrid`1+<InternalStart>d__0[T].MoveNext () [0x000ba] in <5b6f1546a8814ef0a3ec854a35fb10fd>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:151 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in /tmp/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:357 
  at CITRD.Network.MultiDomControllerWorker`2+<ProcessInner>d__13[Tstreamreader,TTaskbuilder].MoveNext () [0x001c6] in <5b6f1546a8814ef0a3ec854a35fb10fd>:0
Comment 4 Narinder 2017-05-12 10:36:23 UTC
I see the same error in the following :

Mono JIT compiler version 5.3.0 (master/52d82cb Fri 12 May 11:19:29 BST 2017)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
	TLS:           __thread
	SIGSEGV:       altstack
	Notifications: epoll
	Architecture:  amd64
	Disabled:      none
	Misc:          softdebug 
	LLVM:          supported, not enabled.
	GC:            sgen (concurrent by default)
Comment 5 Miguel de Icaza [MSFT] 2017-05-16 15:29:02 UTC
Please provide a self-contained test case.
Comment 6 Jerod Venema 2017-07-25 20:35:01 UTC
We're seeing the same thing here, intermittently, when using https on an httplistener.

Mono.Btls.MonoBtlsException: Syscall
  at Mono.Btls.MonoBtlsContext.ProcessHandshake () [0x00038] in <2b0d86369d72459
baed0cee98a8e578a>:0
  at Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake (Mono.Net.Secu
rity.AsyncProtocolRequest asyncRequest, Mono.Net.Security.AsyncOperationStatus s
tatus) [0x0002a] in <2b0d86369d72459baed0cee98a8e578a>:0
  at Mono.Net.Security.AsyncProtocolRequest.ProcessOperation (Mono.Net.Security.
AsyncOperationStatus status) [0x0006b] in <2b0d86369d72459baed0cee98a8e578a>:0
  at Mono.Net.Security.AsyncProtocolRequest.ProcessOperation () [0x0000d] in <2b
0d86369d72459baed0cee98a8e578a>:0
  at Mono.Net.Security.AsyncProtocolRequest.StartOperation () [0x0003c] in <2b0d
86369d72459baed0cee98a8e578a>:0
  at Mono.Net.Security.AsyncProtocolRequest.StartOperation (Mono.Net.Security.As
yncOperation operation) [0x00024] in <2b0d86369d72459baed0cee98a8e578a>:0
  at Mono.Net.Security.MobileAuthenticatedStream.ProcessAuthentication (System.N
et.LazyAsyncResult lazyResult) [0x00057] in <2b0d86369d72459baed0cee98a8e578a>:0

--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] i
n <a07d6bf484a54da2861691df910339b1>:0
  at Mono.Net.Security.MobileAuthenticatedStream.ProcessAuthentication
Comment 7 Jerod Venema 2017-07-25 20:36:06 UTC
FWIW, I don't recall encountering this in the 4.8 series.
Comment 8 Narinder 2017-09-06 15:19:38 UTC
Created attachment 24604 [details]
Program to demonstrate a bug

Hi,
Please find attached a bit of code to reproduce the/a problem.


If the solution is built on VS2017 it will run fine. It launches a trivial server on port 12345 (on windows). You can put https://127.0.0.1:12345 into your browser and see that there are no errors (but no meaningful output either).

If the same binary is launched with Mono on Debian (8.5) we have exceptions.


The solution contains 2 projects :

1. X509CertificateBuilder : This builds a certificate and dumps it in a file called my_pfx which is then required by the other solution. hence needs to be run first. It has a dependency on the nuget package Mono.Security 3.2.3 ( I don't know any other way to build the certificate)

2. MonoTLSBug : The main program can run it with and without a command line parameter (the value of which is irrelevant).
 

Thanks
N
Comment 9 Marek Safar 2017-09-06 16:11:31 UTC
Setting for triaging
Comment 10 Narinder 2018-01-17 17:38:40 UTC
Hi,
Is this likely to be fixed in the upcoming 5.8 ?

Thanks
N