【问题标题】:Two-way SSL certificate exchange on Azure VMAzure VM 上的双向 SSL 证书交换
【发布时间】:2023-04-06 06:07:02
【问题描述】:

我们有一个 Web 应用程序,它必须使用双向 SSL 证书身份验证与 HTTP 服务进行通信。以下是相关代码

        X509Certificate certificate = GetCertFromStore(StoreName.My, StoreLocation.LocalMachine, certName);
        if (certificate != null)
        {
            Log.Debug($"Adding client cert {certificate.Subject}");

            WebRequestHandler handler = new WebRequestHandler();

            handler.ClientCertificates.Add(certificate);
            handler.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
            handler.ClientCertificateOptions = ClientCertificateOption.Manual;
            handler.UseProxy = false;

            using (var client = new HttpClient(handler))
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var postObj = new
                {
                    // somestuff
                };
                var postBody = JsonConvert.SerializeObject(postObj);

                var httpContent = new StringContent(postBody, Encoding.UTF8, "application/json");

                HttpResponseMessage httpResponse = client.PostAsync(queryUrl, httpContent).Result;
                if (httpResponse.IsSuccessStatusCode)
                {
                    // codey code
                }
                else
                {
                    Log.Error($"some error stuff.");
                }
            }
        }
        else
        {
            Log.Error($"Unable to find the certificate with name: {certName}");
        }

目前我们正在使用我们提供给第三方服务提供商并从他们那里安装相关服务器证书的自签名证书。

在本地开发机器上一切正常 - 连接已建立,服务调用和响应正确完成。

但是,当我们在 Azure VM(我们的测试环境)上进行部署时,证书交换会失败。查看 Wireshark 跟踪,我们可以看到服务器证书被接受,但它显示发送了 0 个客户端证书。我们知道在存储中找到了客户端证书(由于日志记录),我们可以从 Wireshark 跟踪中看到客户端证书名称确实列在服务器证书请求的“可分辨名称”中。

无法弄清楚如何,即使代码将我们预期的客户端证书添加到 WebRequestHandler 它在握手期间消失了。

【问题讨论】:

  • 该 Azure VM 个人存储中是否存在自签名证书?
  • 嗨。是的,它已安装在 VM 个人商店中,我们的代码在 GetCertFromStore(...) 函数中加载它。
  • 有没有具体的错误信息?
  • 不,只显示发送了 0 个客户端证书。

标签: c# azure ssl virtual-machine


【解决方案1】:

我们解决了这个问题。

首先我认为证书是在没有私钥的情况下安装的。在我们的函数 GetCertFromStore 中,它正在查找证书,但我认为因为它没有私钥它没有发送它。 (它找到证书的事实让我们感到困惑 - 我们假设如果它找到了,那么一切都很好。)

为解决此问题,我们将其从商店中删除,然后使用 .pfx 文件重新安装,确保同时导入私钥。

还有一个权限问题,我们在 StackOverflow here 上找到了答案(注意,不是标记为“答案”的答案,而是关于权限的答案)。为了使 Web 应用程序能够加载和使用带有私钥的证书,应用程序池身份(或模拟用户)必须对已安装证书的密钥文件(通常位于 C:\ProgramData\Microsoft\Crypto\RSA 中)具有读取权限\MachineKeys)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 2013-07-20
    • 2020-10-20
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多