gpt4 book ai didi

c# - 什么可能导致 System.Net 错误 :AcquireCredentialsHandle() failed with error 0X8009030D?

转载 作者:太空宇宙 更新时间:2023-11-03 14:33:25 34 4
gpt4 key购买 nike

我有一段简单的 https 连接代码,可以很好地与一个 ssl 主机一起工作,即使根证书颁发机构不受信任也可以连接(这是一个沙盒环境,所以我们不担心这会造成安全漏洞,因为现在),并使用 Nuget 包 OpenSSL.X509Certificate2Provider 从文本文件加载证书和 key :

    public async Task<string> Register()
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;

using (var handler = new HttpClientHandler())
{
var rawcert = File.ReadAllText(@"C:\OpenSSL\bin\certTransport.pem");
var rawkey = File.ReadAllText(@"C:\OpenSSL\bin\privateKeyTransport.key");

var provider = new CertificateFromFileProvider(rawcert, rawkey);
var cert = provider.Certificate;
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.ServerCertificateCustomValidationCallback +=
(HttpRequestMessage req, X509Certificate2 cert2, X509Chain chain, SslPolicyErrors err) =>
{
return true;
};

handler.ClientCertificates.Add(cert);

using (var client = new HttpClient(handler))
{
client.BaseAddress = new Uri("https://{uri}/");
var response = await client.GetStringAsync("register");
return response;
}
}
}

当我将此代码指向另一台主机时,应该以完全相同的方式对其进行保护,但出现以下错误:System.Net.WebException:请求已中止:无法创建 SSL/TLS 安全通道。

深入研究日志,我发现:System.Net 信息:0:[35036] AcquireCredentialsHandle(包 = Microsoft 统一安全协议(protocol)提供程序,意图 = 出站,scc = System.Net.SecureCredential)
System.Net 错误:0:[35036] AcquireCredentialsHandle() 失败,错误为 0X8009030D。

为此搜索堆栈,我发现许多与证书存储权限相关的错误,但在这种情况下,客户端证书并未从存储中加载。

我还发现许多与 SecurityProtocolType 相关的问题。我已验证服务器正在使用 Tls1.2(仅)。

我从 VS 2017 Enterprise 中的单元测试调用代码,在 Win10 x64 上以管理权限运行。代码包含在 .Net 4.7 类库中,而不是托管在 IIS 中。

可能导致此问题的原因是什么?

更新根据@Jeroen Mostert 在下方的有用评论,我尝试了使用 OpenSSL 的基本连接测试。稍微匿名的结果:

OpenSSL> s_client -connect {REMOVED}:443 -CAfile C:\OpenSSL\bin\archive-ii\transport-production-new\certTransport.pem
CONNECTED(000001F0)
depth=2 C = GB, O = OK, CN = {REMOVED} Root CA
verify error:num=19:self signed certificate in certificate chain
---
Certificate chain
0 s:/C=GB/O={REMOVED}/OU={REMOVED}/CN={REMOVED}
i:/C=GB/O={REMOVED}/CN={REMOVED} Issuing CA
1 s:/C=GB/O={REMOVED}/CN={REMOVED} Issuing CA
i:/C=GB/O={REMOVED}/CN={REMOVED} Root CA
2 s:/C=GB/O={REMOVED}/CN={REMOVED} Root CA
i:/C=GB/O={REMOVED}/CN={REMOVED} Root CA
---
Server certificate
-----BEGIN CERTIFICATE-----
{REMOVED}
-----END CERTIFICATE-----
subject=/C=GB/O={REMOVED}/OU={REMOVED}/CN={REMOVED}
issuer=/C=GB/O={REMOVED}/CN={REMOVED} Issuing CA
---
Acceptable client certificate CA names
/C=GB/O={REMOVED}/CN={REMOVED} Issuing CA
/C=GB/O={REMOVED}/CN={REMOVED} Root CA
Client Certificate Types: RSA sign, DSA sign, ECDSA sign
Requested Signature Algorithms: RSA+SHA512:DSA+SHA512:ECDSA+SHA512:RSA+SHA384:DSA+SHA384:ECDSA+SHA384:RSA+SHA256:DSA+SHA256:ECDSA+SHA256:RSA+SHA224:DSA+SHA224:ECDSA+SHA224:RSA+SHA1:DSA+SHA1:ECDSA+SHA1
Shared Requested Signature Algorithms: RSA+SHA512:DSA+SHA512:ECDSA+SHA512:RSA+SHA384:DSA+SHA384:ECDSA+SHA384:RSA+SHA256:DSA+SHA256:ECDSA+SHA256:RSA+SHA224:DSA+SHA224:ECDSA+SHA224:RSA+SHA1:DSA+SHA1:ECDSA+SHA1
Peer signing digest: SHA512
Server Temp Key: ECDH, P-256, 256 bits
---
SSL handshake has read 5155 bytes and written 446 bytes
---
New, TLSv1/SSLv3, Cipher is ECDHE-RSA-AES128-GCM-SHA256
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
Protocol : TLSv1.2
Cipher : ECDHE-RSA-AES128-GCM-SHA256
Session-ID:
Session-ID-ctx:
Master-Key: {REMOVED}
Key-Arg : None
PSK identity: None
PSK identity hint: None
SRP username: None
Start Time: 1527164830
Timeout : 300 (sec)
Verify return code: 19 (self signed certificate in certificate chain)
---

有什么问题吗?

我还查看了 CAPI2 日志(如评论中所建议)。这显示与不受信任的根证书相关的错误,如下所示: enter image description here

这是否意味着我的验证回调不起作用 - 如果是这样,知道为什么会这样吗?

进一步更新:

如果我删除附加证书的代码行,那么错误就会消失 - 我可以连接到 MTLS 测试端点(但是,当然,在那里进行的 MTLS 测试失败)。

这意味着什么?证书本身有问题?

最佳答案

问题最终出在 OpenSSL.X509Certificate2Provider NuGet 包的 CertificateFromFileProvider 方法的输出上。

无论出于何种原因,创建的证书都无法正常工作。使用相同的输入文件并使用 OpenSSL 生成 .pfx导致 X509Certificate2 工作正常。

关于c# - 什么可能导致 System.Net 错误 :AcquireCredentialsHandle() failed with error 0X8009030D?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50508622/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com