.net 8.0 HttpClient getting error: “Autentication failed vecause the remote party sent a TLS alert: HandshakeFailure”

I need to send https requests from .net console app running on Windows Server 2008 (nope, can’t update 🙁 ). Requests made by the app failed with the error:

Autentication failed vecause the remote party sent a TLS alert: HandshakeFailure

So I did some research and succeeded with calling curl from the app. I’d like to avoid using curl if possible, so I decided to try downloading Firefox CA certificate store and adding all it’s certificates to http client:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var caCerts = LoadCACertificates(pemFilePath);
using var httpClientHandler = new HttpClientHandler();
httpClientHandler.ClientCertificates.AddRange(caCerts);
using var httpClient = new HttpClient(httpClientHandler);
...
private static X509Certificate2Collection LoadCACertificates(string pemFilePath)
{
var certificates = new X509Certificate2Collection();
try
{
string pemContent = File.ReadAllText(pemFilePath);
// Extract each certificate in the PEM file
var matches = Regex.Matches(pemContent, "-----BEGIN CERTIFICATE-----(.*?)-----END CERTIFICATE-----", RegexOptions.Singleline);
foreach (Match match in matches)
{
string base64Cert = match.Groups[1].Value.Replace("r", "").Replace("n", "");
byte[] certBytes = Convert.FromBase64String(base64Cert);
var certificate = new X509Certificate2(certBytes);
certificates.Add(certificate);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error loading CA certificates: {ex.Message}");
throw;
}
return certificates;
}
...
</code>
<code>var caCerts = LoadCACertificates(pemFilePath); using var httpClientHandler = new HttpClientHandler(); httpClientHandler.ClientCertificates.AddRange(caCerts); using var httpClient = new HttpClient(httpClientHandler); ... private static X509Certificate2Collection LoadCACertificates(string pemFilePath) { var certificates = new X509Certificate2Collection(); try { string pemContent = File.ReadAllText(pemFilePath); // Extract each certificate in the PEM file var matches = Regex.Matches(pemContent, "-----BEGIN CERTIFICATE-----(.*?)-----END CERTIFICATE-----", RegexOptions.Singleline); foreach (Match match in matches) { string base64Cert = match.Groups[1].Value.Replace("r", "").Replace("n", ""); byte[] certBytes = Convert.FromBase64String(base64Cert); var certificate = new X509Certificate2(certBytes); certificates.Add(certificate); } } catch (Exception ex) { Console.WriteLine($"Error loading CA certificates: {ex.Message}"); throw; } return certificates; } ... </code>
var caCerts = LoadCACertificates(pemFilePath);

using var httpClientHandler = new HttpClientHandler();
httpClientHandler.ClientCertificates.AddRange(caCerts);

using var httpClient = new HttpClient(httpClientHandler);

...

private static X509Certificate2Collection LoadCACertificates(string pemFilePath)
{
    var certificates = new X509Certificate2Collection();

    try
    {
        string pemContent = File.ReadAllText(pemFilePath);

        // Extract each certificate in the PEM file
        var matches = Regex.Matches(pemContent, "-----BEGIN CERTIFICATE-----(.*?)-----END CERTIFICATE-----", RegexOptions.Singleline);

        foreach (Match match in matches)
        {
            string base64Cert = match.Groups[1].Value.Replace("r", "").Replace("n", "");
            byte[] certBytes = Convert.FromBase64String(base64Cert);
            var certificate = new X509Certificate2(certBytes);
            certificates.Add(certificate);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error loading CA certificates: {ex.Message}");
        throw;
    }

    return certificates;
}
...

But without any success, I’m still getting the same error.

Please, has anyone any ideas, why the code doesn’t work?

6

  1. While one possible cause of handshake_failure is not using a client cert AND KEY when the server demands client auth, ONLY a certificate WITH a private key can be a client cert;
    you do not and never will have the private key for any CA trusted by Mozilla (or anybody else sensible). Did you specify a client cert AND KEY to curl?
    If not and it worked without, then the server doesn’t require client auth; curl never defaults client cert, only a truststore of CA certs to verify the server cert — and that varies depending on how it is built.

  2. Another possibility is protocol version. Assuming you don’t mean R2, S2008 supports TLS versions above 1.0
    only in SP2 with a patch AND registry changes.
    SSL3 was totally broken in 2014 and has long been prohibited by everybody. TLS 1.0 is now considered obsolete and marginal, though not definitely broken,
    and many servers won’t accept it; almost as many won’t accept 1.1 either. Among other things if a website wants to accept payment cards
    the industry standard (PCI DSS) effectively prohibits everything below 1.2, and so do lots of governments, regulators, and other authorities, including IETF.

    You may be able to test this with curl, depending on which library it is built to use and possibly which version. (Use --tls-max and --tls-min; the --tlsv{number} options only set a minimum not an exact version.)
    If you can’t test on the machine with the problem, use a different curl build, or openssl s_client -tls{1,1_1,1_2,1_3} (which is always exact), from another machine to the same server.

  3. Yet another possibility is Server Name Indication aka SNI.
    Although defined earlier, in 2008 SNI was rarely used, and I’m pretty sure Vista didn’t support it,
    so there’s at least a chance S2008 doesn’t. My search results are mostly about SNI support in IIS,
    which was a big issue for a while, but client-side you don’t care about that, only about Schannel.
    SNI became common during the 2010s, and today most public servers require it,
    and often give handshake_failure if it is not provided. Any nonancient curl sends SNI.

    This can be tested with openssl s_client by specifying or omitting -noservername
    (except OpenSSL below 1.1.1, which you aren’t likely to find today, but if you do I will update).

  4. A final but I think unlikely possibility is ciphersuites. For a while this was a problem on XP/S2003, which didn’t implement AES,
    the only cipher from that period still accepted by many authorities and servers.
    Vista, and I assume S2008, did implement AES, but conceivably your system has it disabled for some reason.
    curl honors Windows cipher settings when built to use Schannel, but not when built for other libraries like OpenSSL. (You can see how it was built with curl -V uppercase-vee.)

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật