I’m trying to connect to a REST api host on my company’s development server. This server has a self signed certificate and therefore Unity refuses to accept the connexion.
I tested my GET request uri using postman and it worked well as soon as I disabled the certificate validation so I think it is indeed a certificate problem.
I searched for solution and found that some people use HttpWebRequest with a redefinition of the ServicePointManager’s certificate validation callback.
I tried this :
and (when sending on a datagram socket using a sendto call) no address was supplied.```
Does someone have any clue on what could be going on ? Thanks in advance.
I must add that I do not have any access to the server excluding the API so I cannot setup a valid certificate.
So where is your TrustCertificate function? Also, if you’re just gonna yield return 0 theres no point in using an IEnumerator, it’s not gonna be async.
I only have the .crt Firefox got me when I connected to the API with it and already added it to windows 10 trusted root certificates but it didn’t changed anything.
I also tried to add it to Mono’s certificate store but with no success
This time the code stalls after Debug.Log(“Response CB”) and I have no idea why it does.
The server’s .crt is in Windows’ trust store, in Mono’s AddressBook store and in all my browsers store.
For now we allow HTTP access as a workaround but it won’t be acceptable for long…
After further investigation it seems that the problem is not from my code: I tested it with a simple C# console project in Visual Studio (just changed the Debug.Log with Console.WriteLine) and it worked perfectly fine, I was able to connect to the server.
I also added a Debug.Log(“In the callback”) in the TrustCertificate callback and it seems that it is not called with Unity at all.
With Visual Studio i can see “In the callback” written in the console each time I make a request.
I already have .NET set to 4.6 in the player settings.
Setting the validationcallback on the httprequest didn’t change anything
By adding a try catch block in the GetRequest method I discovered that the code was stalling line 13. because of an exception silently catched by Unity.
printing that error gave me this : SecureChannelFailure (The authentication or decryption has failed)
For what its worth, the below code works for us, but only after changing to 4.6 and forgetting about the service point manager (as posted before). We use the most recent unity version.
Since 2018.2 we have a shared TLS backend for new mono (.Net 4+) and UnityWebRequest. This backend supports TLS 1.2 and verifies against a platform dependent certificate store.
Due to a bug in Mono ServicePointManager.ServerCertificateValidationCallback does not work with their modern TLS bindings which we use to connect to our new backend.
Another way to work around this other than UnityWebRequest is by using the deprecated CertificatePolicy setting like this:
class NoCheckCertificatePolicy : System.Net.ICertificatePolicy
{
public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
{
return true;
}
}
// ...
System.Net.ServicePointManager.CertificatePolicy = new NoCheckCertificatePolicy();
This requires Api Compatibility Level .Net 4.x though, but worked when I tested it a month ago.
Hello. I have the same problem, but i want to use sslstream. After copying above code, that @andreasreich posted, it connecting, but i cant send any message, because i have this error: This operation is only allowed using a successfully authenticated context. I already have:
Certificate = new X509Certificate2(“./server.pfx”);
SslStream.AuthenticateAsServer(Certificate, false, false);
in server and
SslStream.AuthenticateAsClient(“192.168.0.4”);
in client. Can someone help me?
Hi, I tried both ServicePointManager.ServerCertificateValidationCallback & ServicePointManager.CertificatePolicy in Unity2019.4.16f1(.Net 4.x), but they all didn’t work. Is there any other way to make a global setting for certificate validation.
I can confirm the proposed solution by @andreasreich works in Unity 2020.3.15f1 on Windows x64 as well as ARM64 UWP HoloLens 2. However, it does not work in the Linux editor on desktop, and by consequence also not in CI environments using it.
I can’t find any way to allow self-signed certificates using HttpClient on Linux, since the intended approach using ServerCertificateCustomValidationCallback is “not implemented” and the above workaround appears to be Windows-only.
In case anyone has a similar issue as me and arrives here via google search. I was getting the error “Curl error 60: Cert verify failed. Certificate Common Name(CN) does not match with the expected CN. UnityTls error code: 7” because UnityWebRequest wouldnt accept my local dev API server (python/flask) fake cert. Making an override handler that always accepts worked: