Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

"Failed to read data to TLS context - error code: UNITYTLS_INTERNAL_ERROR" in company network

Discussion in 'Editor & General Support' started by Manuel_Prinz, Feb 5, 2020.

  1. Manuel_Prinz

    Manuel_Prinz

    Joined:
    Nov 13, 2019
    Posts:
    7
    Hi,

    I'm working for a big company with a very strong firewall. In particular the firewall decodes all encoded traffic, analyzes it and sends it re-encoded. Windows trusts the certificate of the "encryption-breaker" so all webpages work fine in all webbrowsers and other c# projects.
    However in Unity even in an empty project when I send an HTTP-request like this:
    Code (CSharp):
    1. string html = string.Empty;
    2. string url = @"Any URL";
    3.  
    4. WebProxy proxy = new WebProxy
    5. {
    6.         Address = new Uri($"URL of my company Proxy"),
    7. };
    8.  
    9. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    10. request.AutomaticDecompression = DecompressionMethods.GZip;
    11. request.Proxy = proxy;
    12. request.UseDefaultCredentials = true;
    13.  
    14. using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    15. using (Stream stream = response.GetResponseStream())
    16. using (StreamReader reader = new StreamReader(stream))
    17. {
    18.         html = reader.ReadToEnd();
    19. }
    20. Debug.Log(html);
    I get a "TlsException: Failed to read data to TLS context - error code: UNITYTLS_INTERNAL_ERROR".

    Full stack trace:
    https://pastebin.com/RvPx13T9

    This might be hard for you to reproduce because of our custom firewall but here as much additional info as I know:
    • Occures on all unity versions I tested it with (2019.2.0f1, 2019.3.0f6)
    • Same URL always works on any browser but always fails in Unity
    • Occures in empty projects
    • Works in other C# projects with exactly the same code
    • Works outside of the company network with almost the same code (without setting the proxy)
    • Occures with all libraries I tried for HTTP-requests like HttpWebRequest, UnityWebRequest, WebRequest and HttpClient
    • Adding a ServerCertificateValidationCallback that accepts all certificates does not resolve the issue

    In a different thread @Tautvydas-Zilys wrote: "Correct. Unity will do what Internet Explorer does under the hood when you visit the website."
    There has to be a difference because in Internet Explorer it works fine.

    I did not find anyone else on the internet getting this error message and the only search result was the github with the mono code.
    Does anyone have an idea how I can get HTTP-Requests to work?

    Edit: The resulting application will be supplied to hundreds or thousands of clients inside our network so just ignoring the issue or developing in a different network is not an option
     
    oli-2019 likes this.
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,640
    Looks bad.
    Could you report a bug for this and paste a case number here?
     
  3. Manuel_Prinz

    Manuel_Prinz

    Joined:
    Nov 13, 2019
    Posts:
    7
    Case 1217164
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,640
    Thanks, we'll look into it.
     
  5. andreasreich

    andreasreich

    Unity Technologies

    Joined:
    Sep 24, 2017
    Posts:
    55
    @Manuel_Prinz would it be possible for you to have a quick check with a Unity 2020 (alpha) version within your network if it still happens there? We changed the way certificates are verified on Windows to be actually more like Edge/Internet Explorer, it does look like it will be backported to 2019.3 but it would be nice to confirm if this is the same issue or something else. Thank you!
     
  6. Manuel_Prinz

    Manuel_Prinz

    Joined:
    Nov 13, 2019
    Posts:
    7
    Last edited: Feb 5, 2020
  7. Manuel_Prinz

    Manuel_Prinz

    Joined:
    Nov 13, 2019
    Posts:
    7
    This is still a big issue for us. @andreasreich Do you think there is a workaround to bypass unity tls code and use the default c# one or somthing similar. I assume there won't be a fix in a future unity version soon but we have to make this work.
    One possibility would be to pipe the traffic through a native c# app. However is extremely ugly and might even be a security risk.
    Do you have any other ideas?
     
  8. andreasreich

    andreasreich

    Unity Technologies

    Joined:
    Sep 24, 2017
    Posts:
    55
    @manuel-prinz sorry that there has been no update for so long. We couldn't repro the issue so far. I'll work on a tracing option for our tls backend so we can get more information for this and in the future.
    Another thing I just thought of you could try to gather more information meanwhile is to try to do webrequests with Windows 10's preinstalled curl from command line: I.e. run curl <your webrequest url> and see how it responds
    As for workarounds I sadly don't have any good ideas right now.
    Have you tried UnityWebRequest on the Unity alpha version that gave you a different error? It means that your usecase is definitely affected by this fix (which I think is a good thing of sorts. That particular fix was already backported to previous Unity releases)
     
  9. Manuel_Prinz

    Manuel_Prinz

    Joined:
    Nov 13, 2019
    Posts:
    7
    @andreasreich I tested curl and it worked fine getting and showing the response.

    However I tested many things again today and noticed that at least today it only worked outside of unity when I didn't set the proxy in the HttpWebRequest. If I set the Proxy I get a similar result to the one in unity.
    https://pastebin.com/50kMk00W
    It seems that this is not a Problem with unity after all. Outside of unity it works without Setting the Proxy which surprises me because in a Microsoft api also working with http requests internally I had to set it.

    Despite all of this my Problem is still not fixed because in unity without Setting the Proxy I get a timeout on all websites (even non https ones). https://pastebin.com/YHGDX4i2
    After looking into this with wireshark I noticed quite a difference with the network packages between unity and non-unity:
    In non-unity I clearly see the HTTP-GET request in the packages.
    In Unity I only see the DNS lookups on some unity Server

    I have not a single HTTP request in wireshark for the whole ~24 seconds until the timeout.

    curl works on the website, non-unity c# works without proxy with the Website but unity c# times out without proxy on the same website.
    Could this be a Firewall issue? I did not Change any Settings neither for unity nor the non-unity Project.
     
    Last edited: Feb 26, 2020
  10. Manuel_Prinz

    Manuel_Prinz

    Joined:
    Nov 13, 2019
    Posts:
    7
    Instead of the HTTP package I see when I send an HTTP request via non-unity c# I just noticed a TCP package to the desired Server when sending it in a unity project. The Server never responds with any package. Is the TCP package instead of HTTP intended behavour? I expected an HTTP package to show up in wireshark wether I send an HTTP request in a unity project or in a c# Project.
     
  11. andreasreich

    andreasreich

    Unity Technologies

    Joined:
    Sep 24, 2017
    Posts:
    55
    > Is the TCP package instead of HTTP intended behavour?

    Not too familiar with that area but I'd say it's more an issue with how Wireshark displays it since HTTP is based on TCP packages. Maybe @AurimasC can give more guidance (he's on vacation right now).
    Worth noting that HTTP requests in Unity go different paths depending on the api and platform. For Windows doing HTTP via UnityWebRequest we go through a bundled curl. If you use any of the .Net apis, we go through mono's implementation. The one thing we streamlined across is the TLS backend which is shared for all of these (which is MbedTLS based in your case)