Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

“The authentication or decryption has failed.” Exception when connecting socket in android #118

Open
diogod3 opened this issue Nov 18, 2016 · 2 comments

Comments

@diogod3
Copy link

diogod3 commented Nov 18, 2016

I'm writing an app using C# and PCL to runs in android, ios and windows and in that app I'm using sockets for pcl to create and connect to a TCP socket.

This is the code to create and connect the socket:

  _bufferSize = 1024 * 1000 * 2;
  _client = new TcpSocketClient(_bufferSize);

   await _client.ConnectAsync(_hostName, _port, true); 

This is inside an async task that is called when initializing the app.

In windows it works fine, however in android sometimes it works, sometimes it doesn't and when it doesn't I keep getting this exception "The authentication or decryption has failed."

{System.IO.IOException: The authentication or decryption has failed. ---> 
	System.IO.IOException: Error while sending TLS Alert (Fatal:InternalError): 
		System.NotSupportedException: Cannot write to a BufferedStream while the read buffer is not empty if the underlying stream is not seekable. 
		Ensure that the stream underlying this BufferedStream can seek or avoid interleaving read and write operations on this BufferedStream.  
			at System.IO.BufferedStream.ClearReadBufferBeforeWrite () [0x00032] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/io/bufferedstream.cs:432   
			at System.IO.BufferedStream.BeginWrite (System.Byte[] buffer, System.Int32 offset, System.Int32 count, System.AsyncCallback callback, System.Object state) [0x000ab] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/io/bufferedstream.cs:1022   
			at Mono.Security.Protocol.Tls.SslStreamBase.InternalBeginWrite (Mono.Security.Protocol.Tls.SslStreamBase+InternalAsyncResult asyncResult) [0x00031] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslStreamBase.cs:782  ---> 
			System.NotSupportedException: Cannot write to a BufferedStream while the read buffer is not empty if the underlying stream is not seekable. Ensure that the stream underlying this BufferedStream can seek or avoid interleaving read and write operations on this BufferedStream.  
				at System.IO.BufferedStream.ClearReadBufferBeforeWrite () [0x00032] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/io/bufferedstream.cs:432   
				at System.IO.BufferedStream.BeginWrite (System.Byte[] buffer, System.Int32 offset, System.Int32 count, System.AsyncCallback callback, System.Object state) [0x000ab] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/io/bufferedstream.cs:1022   
				at Mono.Security.Protocol.Tls.RecordProtocol.BeginSendRecord (Mono.Security.Protocol.Tls.ContentType contentType, System.Byte[] recordData, System.AsyncCallback callback, System.Object state) [0x00026] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs:765   
				at Mono.Security.Protocol.Tls.RecordProtocol.SendRecord (Mono.Security.Protocol.Tls.ContentType contentType, System.Byte[] recordData) [0x00000] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs:786   
				at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (Mono.Security.Protocol.Tls.Alert alert) [0x00027] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs:633   
				at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (System.Exception& ex) [0x00021] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs:598    
				--- End of inner exception stack trace ---   
			--- End of inner exception stack trace ---  
			at Mono.Security.Protocol.Tls.SslStreamBase.EndRead (System.IAsyncResult asyncResult) [0x00051] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslStreamBase.cs:883   
			at Mono.Net.Security.Private.LegacySslStream.EndAuthenticateAsClient (System.IAsyncResult asyncResult) [0x00011] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/System/Mono.Net.Security/LegacySslStream.cs:475   
			at Mono.Net.Security.Private.LegacySslStream.AuthenticateAsClient (System.String targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Authentication.SslProtocols enabledSslProtocols, System.Boolean checkCertificateRevocation) [0x00000] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/System/Mono.Net.Security/LegacySslStream.cs:445   
			at Mono.Net.Security.Private.MonoSslStreamImpl.AuthenticateAsClient (System.String targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Authentication.SslProtocols enabledSslProtocols, System.Boolean checkCertificateRevocation) [0x00000] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/System/Mono.Net.Security/MonoSslStreamImpl.cs:77   
			at System.Net.Security.SslStream.AuthenticateAsClient (System.String targetHost, System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates, System.Security.Authentication.SslProtocols enabledSslProtocols, System.Boolean checkCertificateRevocation) [0x00000] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/System/System.Net.Security/SslStream.cs:147   
			at Sockets.Plugin.TcpSocketClient+<ConnectAsync>d__7.MoveNext () [0x0022e] in C:\Users\rdavis\Source\Repos\sockets-for-pcl-for_real\Sockets\Sockets.Implementation.NET\TcpSocketClient.cs:101 
		--- End of stack trace from previous location where exception was thrown ---  
		at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143   
		at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00047] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187   
		at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156   
		at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128   
		at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:113   
		at BetAPI.ESAService.Client+<ConnectSocket>d__73.MoveNext () [0x000a4]  }

I've looked for similar issues but I didn't find anything. Does anyone know what the problem is?

@diogod3 diogod3 changed the title Exception on ConnectAsync “The authentication or decryption has failed.” Exception when connecting socket in android Nov 29, 2016
@rdavisau
Copy link
Owner

rdavisau commented Dec 8, 2016

Hi - sorry about the delayed response. The stacktrace is interesting and I've never encountered this one before. Can you try using a zero buffer size? How frequently does the error occur? Can it occur and/or not occur on the same device? Between or within the same launch instance, etc. ?

@diogod3
Copy link
Author

diogod3 commented Dec 9, 2016

Hi, actually earlier this week I tried using a zero buffer size and it solved my problem. So I guess the problem had something to do with the buffer size I was using.

The error didn't have a constant frequency, sometimes it would occur several times in a row but other times it worked fine several times in a row too. Yes it would occur and not occur on the same device. After the error occur one time I had to reset the app because the socket would be throwing the same error every time I tried to connect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants