C# and dotnet 4.7.1 not adding custom certificate for TLS 1.2 calls

前端 未结 3 829
野性不改
野性不改 2021-01-07 19:09

I have the following C# code, constructing an https call with a custom certificate. When using Tls 1.1, the call works fine. When using Tls 1.2 the call breaks. I using curl

相关标签:
3条回答
  • 2021-01-07 19:45

    Shouldn't you specified the handler's SslProtocols property?

    Try adding this line after hander definition:

    handler.SslProtocols = SslProtocols.Tls12;
    
    0 讨论(0)
  • 2021-01-07 19:50

    I believe this code is masking some type of certificate error by always blindly returning true:

    handler.ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true;
    

    I recommend you have a function to truly analyze the results of arg4. That is your SSL policy errors. Log them and you will get your answer. In my example, I write to the console, but you can write to the trace, or a file. You'll get a number which will be associated a value for the SslPolicyErrors enumeration. Based on the results you might need to check your arg3, which is your chain.

    ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => {
    
    SslPolicyErrors errs = sslPolicyErrors;
    Console.WriteLine("Policy Errors " + sslPolicyErrors.ToString());           
    return true;};
    
    0 讨论(0)
  • 2021-01-07 20:05

    You are right on the root cause of this problem: By default, schannel-based clients offer SHA1, SHA256, SHA384 and SHA512 (on Win10/Server 2016). So TLS 1.2 servers are not supposed to send their MD5 certs to these clients.

    The client (HttpClient) does not list MD5 in the signature_algorithms extension, so the TLS 1.2 handshake fails. The fix is to use a secure server cert.

    0 讨论(0)
提交回复
热议问题