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:
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.
Ok, so I figured out what code is actually causing the exception. The code is this:
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.
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.
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.
@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