.NET 6 中的 TLS v1.2 密码套件 / GET 请求超时

len*_*iep 5 .net c# windows amazon-web-services tls1.2

我目前正在尝试连接到至少需要 TLS v1.2 的 AWS REST API。文档统计表明,客户端还必须支持具有完美前向保密 (PFS) 的密码套件,例如 Ephemeral Diffie-Hellman (DHE) 或 Elliptic Curve Ephemeral Diffie-Hellman (ECDHE)。

使用发送GET请求时HttpClient,连接会超时。我已将 TLS 版本明确设置为TLSv1.2

httpClientHandler.SslProtocols = SslProtocols.Tls12;
Run Code Online (Sandbox Code Playgroud)

这有效,我可以在 Wireshark 跟踪中看到使用了正确的 TLS 版本。我还确认不存在防火墙问题或类似问题。

工作示例(CURL)

当使用cURL时,我可以看到响应中的密码套件Sever HelloTLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (0xc030),这也是服务器所要求的。

在此输入图像描述

损坏的示例(带有 HttpClient 的 .NET 6)

在 .NET 6中使用时HttpClient,上面提到的密码套件在 中提供Client Hello,但服务器响应突然使用TLS_RSA_WITH_AES_256_GCM_SHA384

在此输入图像描述

我可以看到 cURL 请求中还有其他扩展名,例如Extension: psk_key_exchange_modes. 对于服务器不接受第一个密码套件的原因有什么解释吗?根据我的理解,第一个提供的密码套件应该是首选,对吗?

有没有办法在 .NET 6 中强制使用某种密码套件?

这是我用来重现该问题的示例:

public async void PollUrl(string url)
{
    HttpResponseMessage msg = new HttpResponseMessage();

    ServicePointManager.Expect100Continue = true;
    ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;

    using HttpClientHandler httpClientHandler = new();

    httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) => true;
    httpClientHandler.SslProtocols = SslProtocols.Tls12;

    using HttpClient client = new(httpClientHandler);

    // This content type is required for the API call
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

    try
    {
        client.Timeout = TimeSpan.FromSeconds(5);
        msg = await client.GetAsync(url);
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }

    string stringValue = await msg.Content.ReadAsStringAsync();
    Console.WriteLine(stringValue);
}
Run Code Online (Sandbox Code Playgroud)

该应用程序在 Server 2016 上运行。

len*_*iep 2

我们终于找到了原因。Windows 没有启用所需的密码套件。我们已经使用 IISCrypto 启用了相应的密码套件,现在一切正常。

看起来可以强制 .NET 使用 TLS 1.2,即使服务器本身并未启用它。