Search Unity

Resolved Issues with a TLS connection, Google has nearly nothing on my error: UNITYTLS_INVALID_ARGUMENT

Discussion in 'Editor & General Support' started by DustyDev, Feb 22, 2021.

  1. DustyDev

    DustyDev

    Joined:
    Sep 23, 2015
    Posts:
    40
    Greetings friends,

    I'm having issues with a secure connection to a third-party service. Their SDK was written for C#, but not necessarily for Unity, and I'm trying to port it over. The issue happens after I have validated the certificate and established an SSLStream. Here is the error I get:

    Code (CSharp):
    1. Exception: System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> Mono.Security.Interface.TlsException: Failed to read data to TLS context - error code: UNITYTLS_INVALID_ARGUMENT
    Searching for this yields a bunch of stuff related to old versions of Unity not supporting TLS 1.2, and those errors are different than the one above. This page seems to indicate that some argument is null or otherwise invalid, but I don't know what is actually causing this error. I get a callback from their SDK telling me the error happened and the exception, and that's about it. The connection code is running on a separate thread, and it happens after I log that I'm sending them a request and before I log that I got a valid response.

    I am in contact with the developers of the SDK and attempting to resolve the issue, but the reality is that their SDK doesn't officially support Unity (yet), and I'm trying to jam a square peg in a round hole. Has anyone ever seen this error before, and if so, what might be some possible causes aside from what I already know?

    For what it's worth, I can connect to their servers using an insecure connection, but this is not viable for the production version of the application.

    Cheers,
    - Dusty
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Did you try hit this endpoint and see if your TLS is airtight?

    https://www.sslshopper.com/ssl-checker.html

    Last time I had a weird Android network error, turns out the website itself had some obtuse certificate signing trust error.
     
  3. DustyDev

    DustyDev

    Joined:
    Sep 23, 2015
    Posts:
    40
    Good thought. I just checked it, and it looks like we are all good on that end.
     
    Kurt-Dekker likes this.
  4. DustyDev

    DustyDev

    Joined:
    Sep 23, 2015
    Posts:
    40
    Ok, so I figured out what code is actually causing the exception. The code is this:
    Code (CSharp):
    1. await Input.ReadAsync(Array.Empty<byte>(), 0, 0);
    Where Input is the Stream coming in. This optimization is basically saying to wait to read from the stream until there is data on it. It seems Unity doesn't appreciate this for some reason. I can "solve" the problem by removing this optimization and just always reading from the stream at some performance cost. My intention is to file a bug report on this, as that's a pretty common optimization and it really should work.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    What portion of the above interacts with Unity? There are very strict rules as far as "await" and the main thread as far as Unity goes, but unless
    Input
    above is the Unity Input Manager (I assume it's actually a stream?), that shouldn't matter.
     
  6. DustyDev

    DustyDev

    Joined:
    Sep 23, 2015
    Posts:
    40
    The above code doesn't really interact with Unity. This code is in the third party SDK and is running on it's own thread. I believe the exception came from the Unity fork of the mono runtime. When information comes back on the Stream, I store it if I need it to then be used on the main thread later.
     
  7. andreasreich

    andreasreich

    Unity Technologies

    Joined:
    Sep 24, 2017
    Posts:
    55
    @DustyDev Once you opened the bug (or if already), can you give me the ticket number directly? Sounds like something goes wrong in the Mono TLS backend integration dealing with empty reads. Been fixing something else there recently, so memory is still fresh :)
     
  8. DustyDev

    DustyDev

    Joined:
    Sep 23, 2015
    Posts:
    40
    Case 1316968
     
    andreasreich likes this.
  9. Morphus74

    Morphus74

    Joined:
    Jun 12, 2018
    Posts:
    174
    As this bug ever been fixed?

    I also have a 3rd party component (Telepathy V1.8) that cause an issue if I replace the networkstream by a sslstream