Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Question SSL Stream handsake exception

Discussion in 'Experimental Scripting Previews' started by vodenkaj3, May 13, 2020.

  1. vodenkaj3

    vodenkaj3

    Joined:
    Nov 22, 2018
    Posts:
    5
    Hello,

    I have decided to move my game server to the unity (mainly because of collisions), sadly the SSL Authentication is for some odd reason throwing a bunch of errors. Both server and client are authenticating with Tls12, and it is working perfectly fine when I'm connecting the client to the console server that I've been using before. I'm using a self-signed cert for that, that I have moved to the Trusted root folder. There is the exception that it's throwing:

    Authentication failed: System.AggregateException: One or more errors occurred. ---> System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> Mono.Security.Interface.TlsException: Verification failure during handshake
    at Mono.Unity.UnityTlsContext.ProcessHandshake () [0x00081] in <525dc68fbe6640f483d9939a51075a29>:0
    at Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake (Mono.Net.Security.AsyncOperationStatus status) [0x0003e] in <525dc68fbe6640f483d9939a51075a29>:0
    at (wrapper remoting-invoke-with-check) Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake(Mono.Net.Security.AsyncOperationStatus)
    at Mono.Net.Security.AsyncHandshakeRequest.Run (Mono.Net.Security.AsyncOperationStatus status) [0x00006] in <525dc68fbe6640f483d9939a51075a29>:0
    at Mono.Net.Security.AsyncProtocolRequest+<ProcessOperation>d__24.MoveNext () [0x000ff] in <525dc68fbe6640f483d9939a51075a29>:0
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <fb001e01371b4adca20013e0ac763896>:0
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <fb001e01371b4adca20013e0ac763896>:0
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <fb001e01371b4adca20013e0ac763896>:0
    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <fb001e01371b4adca20013e0ac763896>:0
    at System.Runtime.CompilerServices.ConfiguredTaskAwaitable+ConfiguredTaskAwaiter.GetResult () [0x00000] in <fb001e01371b4adca20013e0ac763896>:0
    at Mono.Net.Security.AsyncProtocolRequest+<StartOperation>d__23.MoveNext () [0x0008b] in <525dc68fbe6640f483d9939a51075a29>:0
    --- End of inner exception stack trace ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <fb001e01371b4adca20013e0ac763896>:0
    at Mono.Net.Security.MobileAuthenticatedStream+<ProcessAuthentication>d__47.MoveNext () [0x00254] in <525dc68fbe6640f483d9939a51075a29>:0
    --- End of inner exception stack trace ---
    at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00011] in <fb001e01371b4adca20013e0ac763896>:0
    at System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) [0x00043] in <fb001e01371b4adca20013e0ac763896>:0
    at System.Threading.Tasks.Task.Wait () [0x00000] in <fb001e01371b4adca20013e0ac763896>:0
    at Mono.Net.Security.MobileAuthenticatedStream.AuthenticateAsServer (System.Security.Cryptography.X509Certificates.X509Certificate serverCertificate, System.Boolean clientCertificateRequired, System.Security.Authentication.SslProtocols enabledSslProtocols, System.Boolean checkCertificateRevocation) [0x00011] in <525dc68fbe6640f483d9939a51075a29>:0
    at System.Net.Security.SslStream.AuthenticateAsServer (System.Security.Cryptography.X509Certificates.X509Certificate serverCertificate, System.Boolean clientCertificateRequired, System.Security.Authentication.SslProtocols enabledSslProtocols, System.Boolean checkCertificateRevocation) [0x00006] in <525dc68fbe6640f483d9939a51075a29>:0
    at Client+SSL.ProcessClient (System.Net.Sockets.TcpClient _client, System.Int32 _id) [0x00058] in D:\Dokumenty\Projects\PolyFights\PolyWorldServer\Assets\Scripts\Client.cs:216
    ---> (Inner Exception #0) System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> Mono.Security.Interface.TlsException: Verification failure during handshake
    at Mono.Unity.UnityTlsContext.ProcessHandshake () [0x00081] in <525dc68fbe6640f483d9939a51075a29>:0
    at Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake (Mono.Net.Security.AsyncOperationStatus status) [0x0003e] in <525dc68fbe6640f483d9939a51075a29>:0
    at (wrapper remoting-invoke-with-check) Mono.Net.Security.MobileAuthenticatedStream.ProcessHandshake(Mono.Net.Security.AsyncOperationStatus)
    at Mono.Net.Security.AsyncHandshakeRequest.Run (Mono.Net.Security.AsyncOperationStatus status) [0x00006] in <525dc68fbe6640f483d9939a51075a29>:0
    at Mono.Net.Security.AsyncProtocolRequest+<ProcessOperation>d__24.MoveNext () [0x000ff] in <525dc68fbe6640f483d9939a51075a29>:0
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <fb001e01371b4adca20013e0ac763896>:0
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <fb001e01371b4adca20013e0ac763896>:0
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <fb001e01371b4adca20013e0ac763896>:0
    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <fb001e01371b4adca20013e0ac763896>:0
    at System.Runtime.CompilerServices.ConfiguredTaskAwaitable+ConfiguredTaskAwaiter.GetResult () [0x00000] in <fb001e01371b4adca20013e0ac763896>:0
    at Mono.Net.Security.AsyncProtocolRequest+<StartOperation>d__23.MoveNext () [0x0008b] in <525dc68fbe6640f483d9939a51075a29>:0
    --- End of inner exception stack trace ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <fb001e01371b4adca20013e0ac763896>:0
    at Mono.Net.Security.MobileAuthenticatedStream+<ProcessAuthentication>d__47.MoveNext () [0x00254] in <525dc68fbe6640f483d9939a51075a29>:0 <---

    UnityEngine.Debug:Log (object)
    Client/SSL:processClient (System.Net.Sockets.TcpClient,int) (at Assets/Scripts/Client.cs:224)
    Client/TCP:Connect (System.Net.Sockets.TcpClient) (at Assets/Scripts/Client.cs:76)
    Server:TCPConnectCallback (System.IAsyncResult) (at Assets/Scripts/Server.cs:61)
    System.Threading._ThreadPoolWaitCallback:performWaitCallback ()
     
  2. vodenkaj3

    vodenkaj3

    Joined:
    Nov 22, 2018
    Posts:
    5
    So I've tried to generate a new certificate with a "New-SelfSignedCertificate", and it's still throwing the same error, also I found out, that when I want to use X509Store, it would return 0 certificates, and when I tried to create a custom store for unity certs, it created one, but I have no idea where is that one stored, it's not in "C:\Windows\System32\Certlog", but Unity can access it without a problem. Just to be sure that my code is right (which should be, because like I said, it's working perfectly fine on my console server), this is the code that I'm using for verification:

    Server:
    Code (CSharp):
    1. public bool ProcessClient(TcpClient _client)
    2.         {
    3.             socket = _client;
    4.  
    5.             stream = new SslStream(_client.GetStream(), false);
    6.             receivedData = new Packet();
    7.             receiveBuffer = new byte[dataBufferSize];
    8.  
    9.             try
    10.             {
    11.                 stream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls12, true);
    12.  
    13.                 stream.BeginRead(receiveBuffer, 0, dataBufferSize, ReceiveCallback, null);
    14.                 return true;
    15.             }
    16.             catch (Exception _ex)
    17.             {
    18.                 Debug.Log($"Authentication failed: {_ex}");
    19.                 stream.Close();
    20.                 socket.Close();
    21.                 return false;
    22.             }
    23.         }
    Client:
    Code (CSharp):
    1. private static bool ValidateServerCertificate(
    2.               object sender,
    3.               X509Certificate certificate,
    4.               X509Chain chain,
    5.               SslPolicyErrors sslPolicyErrors)
    6.         {
    7.             //if (sslPolicyErrors == SslPolicyErrors.None)
    8.             //    return true;
    9.             return true;
    10.  
    11.             Debug.Log($"Certificate error: {sslPolicyErrors}");
    12.  
    13.             return false;
    14.         }
    15.  
    16.         public void VerifyCredentials(TcpClient _client)
    17.         {
    18.             Debug.Log("Client connected, waiting for authentication..");
    19.  
    20.             socket = _client;
    21.             stream = new SslStream(socket.GetStream(), false);
    22.  
    23.             receivedData = new Packet();
    24.             receiveBuffer = new byte[dataBufferSize];
    25.  
    26.             try
    27.             {
    28.                 stream.AuthenticateAsClient(nameOfTheServer);
    29.             }
    30.             catch (Exception _ex)
    31.             {
    32.                 Debug.Log($"SSL stream failed:{_ex}");
    33.  
    34.                 if (_ex.InnerException != null)
    35.                     Debug.Log("Authentication failed.");
    36.                 socket.Close();
    37.             }
    38.  
    39.             ClientSend.AuthenticationVerify();
    40.         }
     
  3. jflejmer

    jflejmer

    Joined:
    Mar 1, 2015
    Posts:
    6
    Have you been able to figure this out? I've also encountered this. Works perfectly fine on console server but errors out on unity server build.