gpt4 book ai didi

c# - RemoteCertificateValidationCallback Delegate 中的 X509Chain 参数是如何构建的?

转载 作者:太空宇宙 更新时间:2023-11-03 10:40:32 27 4
gpt4 key购买 nike

如下所示,RemoteCertificateValidationCallback 委托(delegate)用于验证远程安全套接字层 (SSL) 证书。 certificate参数是远程服务器返回的终端实体服务器证书。我不确定的是 chain 参数是如何构造的。它是根据远程服务器返回的证书列表构建的(通常是服务器证书和中间 CA 证书),还是转到证书本地存储并尝试为 certificate 参数中返回的终端实体服务器证书构建链?

// The following method is invoked by the RemoteCertificateValidationDelegate. 
public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
...

}

更新

我怀疑它正在使用 X509Chain.Build(server_cert),但想知道到底发生了什么。

这个人看到了类似的东西:c# Retrieving a Certificate from an SSL stream shows different chain results vs other external tools来自 SSL 流的证书显示不同的链结果

最佳答案

它作为 TLS handshake 的一部分从服务器接收:

7.4.2. Server Certificate

When this message will be sent:

The server MUST send a Certificate message whenever the agreed-upon key exchange method uses certificates for authentication(this includes all key exchange methods defined in this documentexcept DH_anon). This message will always immediately follow theServerHello message.

Meaning of this message:

This message conveys the server's certificate chain to the client.

The certificate MUST be appropriate for the negotiated ciphersuite's key exchange algorithm and any negotiated extensions.

Structure of this message:

 opaque ASN.1Cert<1..2^24-1>;

struct {
ASN.1Cert certificate_list<0..2^24-1>;
} Certificate;

certificate_list

This is a sequence (chain) of certificates. The sender'scertificate MUST come first in the list. Each followingcertificate MUST directly certify the one preceding it. Becausecertificate validation requires that root keys be distributedindependently, the self-signed certificate that specifies the rootcertificate authority MAY be omitted from the chain, under theassumption that the remote end must already possess it in order tovalidate it in any case.

嗯,根据X509Chain.BuildChain()它使用 CAPI CertGetCertificateChain ,这意味着取自本地证书存储,由给定证书构建。您可以在 TransportSecurityHelpers.cs 中看到如何调用验证回调.链内置_SecureChannel.VerifyRemoteCertificate :

chain = new X509Chain();
chain.ChainPolicy.RevocationMode = m_CheckCertRevocation? X509RevocationMode.Online : X509RevocationMode.NoCheck;
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.ExcludeRoot;
if (remoteCertificateStore != null)
chain.ChainPolicy.ExtraStore.AddRange(remoteCertificateStore);
if (!chain.Build(remoteCertificateEx) // Build failed on handle or on policy
&& chain.ChainContext == IntPtr.Zero) // Build failed to generate a valid handle
{
throw new CryptographicException(Marshal.GetLastWin32Error());
}

所以看起来 .Net 从 SSL 上下文中获取服务器证书并使用 CAPI 链构建函数构建链。

关于c# - RemoteCertificateValidationCallback Delegate 中的 X509Chain 参数是如何构建的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25504996/

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