Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 证书链究竟是如何构建的?_C#_X509certificate_X509chain - Fatal编程技术网

C# 证书链究竟是如何构建的?

C# 证书链究竟是如何构建的?,c#,x509certificate,x509chain,C#,X509certificate,X509chain,正如标题所述,我想知道证书链是如何内部构建的 我有一个自签名根证书(CA)和一个传入的用户证书,该证书应该是由我的根签名的,这是我必须测试的。测试X509Certificate2可以使用X509Certificate2.Verify()方法完成,该方法使用带有一些标准设置的X509Chain。为了完全控制流程,我直接使用X509Chain。由于我的根未安装在计算机上(不在受信任的根证书存储中),我正在将其添加到ChainPolicy的外部存储中 整个魔法发生在X509Chain.Build()方

正如标题所述,我想知道证书链是如何内部构建的

我有一个自签名根证书(CA)和一个传入的用户证书,该证书应该是由我的根签名的,这是我必须测试的。测试X509Certificate2可以使用X509Certificate2.Verify()方法完成,该方法使用带有一些标准设置的X509Chain。为了完全控制流程,我直接使用X509Chain。由于我的根未安装在计算机上(不在受信任的根证书存储中),我正在将其添加到ChainPolicy的外部存储中

整个魔法发生在X509Chain.Build()方法中,在这里我通过了“待测试”用户证书。链得到构建,从传递的证书开始,以自签名根结束。但具体如何?如何测试证书是否由一个或另一个根用户签名?它是否在内部使用数字签名

我试图调试整个程序,但最终导致DLL导入,该导入使用Libraries.Crypt32中的CertGetCertificateChain方法。那是C++的东西,超过了我的知识领域。< /P> 迄今为止的简化代码:

using (var chain = new X509Chain
            {
                ChainPolicy =
                {
                    RevocationMode = X509RevocationMode.NoCheck,
                    RevocationFlag = X509RevocationFlag.ExcludeRoot,
                    VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority,
                    VerificationTime = DateTime.Now,
                    UrlRetrievalTimeout = new TimeSpan(0, 0, 0)
                }
            })
            {
                chain.ChainPolicy.ExtraStore.Add(authority);

                if (!chain.Build(signedCertificate))
                {
                    //Errorhandling
                }

                //Root certificate should always be last in the X509ChainElements collection
                var isSignedByCorrectRootCert =
                    chain.ChainElements
                        .Cast<X509ChainElement>()
                        .Last()
                        .Certificate.Thumbprint == authority.Thumbprint;

                if (!isSignedByCorrectRootCert)
                {
                    //Errorhandling
                }
            }
使用(变量链=新X509Chain
{
连锁策略=
{
RevocationMode=X509RevocationMode.NoCheck,
RevocationFlag=X509RevocationFlag.ExcludeRoot,
VerificationFlags=X509VerificationFlags.AllowUnknownCertificationAuthority,
VerificationTime=日期时间。现在,
UrlRetrievalTimeout=新的时间跨度(0,0,0)
}
})
{
chain.ChainPolicy.ExtraStore.Add(权限);
如果(!chain.Build(signedCertificate))
{
//错误处理
}
//根证书应始终是X509ChainElements集合中的最后一个
var isSignedByCorrectRootCert=
链式元素
.Cast()
.Last()
.Certificate.Thumbprint==权限.Thumbprint;
如果(!isSignedByCorrectRootCert)
{
//错误处理
}
}
它是否在内部使用数字签名

没错

我试图调试整个程序,但最终导致DLL导入,该导入使用Libraries.Crypt32中的CertGetCertificateChain方法

是的,.NET
X509Chain
类只是CryptoAPI本机(C++)函数上的普通包装器。我想说.NET中95%以上的加密内容都是CryptoAPI函数的包装


几年前,我写了一篇博文,解释了如何在Microsoft Windows中执行链构建:。这篇文章解释了链引擎如何构建链,并在将其发送到验证例程之前在链中绑定证书。链验证是一个复杂得多的过程。验证逻辑如

所述。请参阅和的一系列相关文章his@canton7谢谢你的有趣文章谢谢。这当然有帮助。