c# - 如何获取和安装根 CA 证书

标签 c# .net ssl ssl-certificate x509certificate

我遇到 SslStream.AuthenticateAsClient 需要“长时间”(~15 秒)的问题。这是一个已知问题,在此 MSDN blog post 中进行了解释.

它给出了两种可能的解决方案

Summing up, this behavior is by design. Options we have are: 1) Install the root CA cert locally so we don’t need to go to the Internet for the list of trusted root CA certs. 2) Disable the Automatic Root Certificates Update feature via GPO so we don’t go to the Internet in any case.

有人告诉我,从安全角度来看,选项 2 不是一个好主意,所以我需要执行选项 1。

问题是我不知道如何获得根 CA 证书。获得它后,我可能会弄清楚如何使用 certutil 来安装它。

我可以在这个函数中中断我的执行

private static bool CertificateValidationCallback(
            object oSender,
            X509Certificate oCertificate,
            X509Chain oChain,
            SslPolicyErrors oSslPolicyErrors)
        {

        }

所以我想我的问题是:

如何获得根 CA 证书? 我需要什么信息才能得到它? 我从哪里获得这些信息?

最佳答案

X509 标准的权限信息访问扩展包含根 CA 证书的位置信息 (URL),但它是一个可选字段。

https://www.rfc-editor.org/rfc/rfc5280#section-4.2.2.1

var cert = new X509Certificate2(certData);
var authInfoExtnsions = from ext in cert.Extensions.Cast<X509Extension>()
                        where ext.Oid.Value == "1.3.6.1.5.5.7.1.1"
                        select ext;
foreach (var authInfoExtnsion in authInfoExtnsions)
{
    Console.WriteLine(Encoding.UTF8.GetString(authInfoExtnsion.RawData));
}

authInfoExtnsion.RawData 是一个复杂的 ASN.1 结构(您可以在 X509 标准中找到详细信息)并且此代码不会为您提供根 CA 证书的 URL。您需要解析并获取 URL。正如我所说,权限信息访问是一个可选扩展,但如果它存在,您会注意到可以在控制台中读取 Root Ca Certficate 的 URL。

关于c# - 如何获取和安装根 CA 证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14403002/

相关文章:

ssl - 如何在 Heroku 上使用 Go 修复 TLS 握手错误?

c# - 如何添加外部原生依赖dll?

c# - 如何从 C# 中的字符串中删除 '\0'?

c# - VS中构建和发布的区别?

c# - 在限制请求数量的同时从 REST 服务异步检索信息

c# - 在没有私钥的情况下解析 PKCS #7 SSL 证书链 (.p7b)?

c# - 这个 "tool"是什么以及如何禁用它?

c# - 将 ASP.NET MVC 区域添加到 ASP.NET Web 窗体现有项目

c# - 为什么我的元素变量在此 foreach 循环中始终为 null?

ios - 使用经过验证的证书“发生 SSL 错误,无法与服务器建立安全连接”