【问题标题】:Certificate Authentication fails with SSL error in .Net only证书身份验证失败,仅在 .Net 中出现 SSL 错误
【发布时间】:2016-11-03 17:04:13
【问题描述】:

我正在尝试使用相互证书身份验证与外部网站通信,但收到“无法为具有权限的 SSL/TLS 建立安全通道”异常。我已将其范围缩小到以下 sn-p(已删除敏感内容):

void Example() 
{
                string KeyIdentifier = "<MyKeyId>";

                X509Store store = new X509Store("My", StoreLocation.LocalMachine);
                store.Open(OpenFlags.ReadOnly);

                X509Certificate2Collection certificates = store.Certificates.Find(X509FindType.FindByThumbprint, KeyIdentifier, true);
                X509Certificate2 certificate = certificates[0];

                HttpWebRequest r = (HttpWebRequest)HttpWebRequest.Create("https://www.example.org");
                r.ClientCertificates.Add( certificate);

                r.Accept = "text/xml";
                r.Method = WebRequestMethods.Http.Post;

                string result = null;
                using (HttpWebResponse resp = (HttpWebResponse)r.GetResponse())
                {
                    StreamReader reader = new StreamReader(resp.GetResponseStream());
                    result = reader.ReadToEnd();
                }
}

我已经正确设置了证书等,并且此代码成功找到了它们,使用 certificate.HasPrivateKey=true,但失败了。 SoapUI 愉快地与 TLS 连接。当我访问该网站时,Chrome 不会报告任何证书错误。但是,上面的代码总是抛出异常。我已经尝试了所有 3 个 TLS 版本,但均未成功。使用 ServicePointManager 跳过服务器证书验证没有区别。

奇怪的是,如果我指定 SSL3

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

然后该过程成功完成,但我自然不想使用 SSL3。该应用是在 Server 2012 上运行的 .Net 4.6。

【问题讨论】:

  • 最早支持的.NET版本是4.5.2,也加入了TLS 1.2支持
  • 我已将我的测试应用升级到 .Net 4.6,但仍然遇到上述问题。 TLS 继续抛出异常,而 SSL3 继续正常工作。
  • 使用日志记录跟踪事务:ferozedaud.blogspot.com/2009/08/tracing-with-systemnet.html。日志文件应向您显示失败时发生的情况。

标签: c# ssl httpwebrequest client-certificates


【解决方案1】:

经过几个月的反复挖掘,我最终尝试使用 SSLStream 建立连接,得到一个不同的异常,并找到了这个答案:A call to SSPI failed, see inner exception - The Local Security Authority cannot be contacted

建议您添加以下注册表项: [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\KeyExchangeAlgorithms\Diffie-Hellman] "ClientMinKeyBitLength"=dword:00000200

这会将 Diffie-Hellman 的最小密钥长度切换为 512 位。这个最小值在 Windows 更新中被提高到 1024 位,以尝试缓解 logjam 攻击,因此请小心设置此值,因为它是系统范围的,并且可能会打开针对您系统的攻击向量。

【讨论】:

    猜你喜欢
    • 2015-01-29
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 2015-06-05
    • 2015-11-19
    • 1970-01-01
    • 2012-05-02
    相关资源
    最近更新 更多