Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.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/9/ssl/3.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-验证由适当的根用户签名的客户端证书_C#_Ssl_Certificate - Fatal编程技术网

C# C-验证由适当的根用户签名的客户端证书

C# C-验证由适当的根用户签名的客户端证书,c#,ssl,certificate,C#,Ssl,Certificate,我知道关于这场争论有很多问题,但我已经陷入其中好几天了,所以我在这里。 我有根证书和客户端证书。我需要在C web API项目中复制命令openssl verify-CAfile ca.pem client.pem的功能 这就是我现在所知道的,希望这是真的: 验证方法实际验证证书是否由授权机构签名。它就像一个格式控件。哪个城市签署了证书并不重要。 X509链条是最好的选择。将您的ca证书添加到额外的存储中,因为我不打算将证书安装到Windows中。然后通过客户端证书构建。让魔法发生吧!不幸的是,

我知道关于这场争论有很多问题,但我已经陷入其中好几天了,所以我在这里。 我有根证书和客户端证书。我需要在C web API项目中复制命令openssl verify-CAfile ca.pem client.pem的功能

这就是我现在所知道的,希望这是真的:

验证方法实际验证证书是否由授权机构签名。它就像一个格式控件。哪个城市签署了证书并不重要。 X509链条是最好的选择。将您的ca证书添加到额外的存储中,因为我不打算将证书安装到Windows中。然后通过客户端证书构建。让魔法发生吧!不幸的是,我的配置有一些问题。 让我举个例子更清楚地说明一下

private bool VerifyCertificate(X509Certificate2 client)
{
    X509Chain chain = new X509Chain();
    var stringCert = WebConfigurationManager.AppSettings["CACertificate"];
    var byteCert = Encoding.ASCII.GetBytes(stringCert);
    var authority = new X509Certificate2(byteCert);

    chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
    chain.ChainPolicy.VerificationFlags = X509VerificationFlags.IgnoreWrongUsage;

    chain.ChainPolicy.ExtraStore.Add(authority);

    // Do the preliminary validation.
    if (!chain.Build(client))
        return false;

    return true;
}
在本例中,程序返回false。生成未通过。我确定问题出在ChainPolicy属性上,所以我尝试了不同的配置

chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.ExcludeRoot;
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.NoFlag;
chain.ChainPolicy.VerificationTime = DateTime.Now;
chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 0, 0);
但这一个不会验证任何东西,事实上,使用我的ca证书,方法返回true,使用另一个ca证书,我没有使用它来签署我的客户端证书,方法也返回true

我搜索了一个C的OpenSSL包装器,我发现了它,但不幸的是它是基于旧库的,并且repo不再有效。此外,如果可能的话,我将只使用.net框架来实现我的目标

伙计们,快速回顾一下。我想检查只有由我的ca证书确认的证书才能通过验证,所有其他证书都必须停止

提前感谢您的帮助

ExtraStore没有限制,它提供了额外的证书来帮助完成链。它不提供信任数据

为了确定证书是否由您想要的CA颁发,您需要执行以下操作:

private static readonly X509Certificate2 s_trustedRoot = ObtainTheRoot();
private static readonly byte[] s_normalizedRoot = s_trustedRoot.RawData;

private bool VerifyCertificate(X509Certificate2 candidate)
{
    X509Chain chain = new X509Chain();
    // set all the things you need to set to make it build

    if (!chain.Build(candidate))
        return false;

    // Check that the root certificate was the expected one.
    X509ChainElementCollection elements = chain.ChainElements;
    return elements[elements.Count - 1].Certificate.RawData.SequenceEqual(s_normalizedRoot);
}
我将cert和它的规范化字节形式提升为statics,假设它们在进程启动后不会改变。如果证书可以动态更改,则应相应调整。

找到问题所在。 对我来说,这是正确的回答

 private bool VerifyCertificate(X509Certificate2 client)
    {
        X509Chain chain = new X509Chain();
        var stringCert = WebConfigurationManager.AppSettings["CACertificate"];
        var byteCert = Encoding.ASCII.GetBytes(stringCert);
        var authority = new X509Certificate2(byteCert);

        chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
        chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority;

        chain.ChainPolicy.ExtraStore.Add(authority);

        // Do the preliminary validation.
        if (!chain.Build(client))
            return false;

        // This piece makes sure it actually matches your known root
        var valid = chain.ChainElements
            .Cast<X509ChainElement>()
            .Any(x => x.Certificate.Thumbprint == authority.Thumbprint);

        if (!valid)
            return false;

        return true;
    }
现在,调试应用程序我有一些注意事项:

.Build方法应该做什么? 我的意思是,使用ca签名的证书或使用自签名证书,该方法始终返回true,但如果使用受信任的证书,它将在链中添加证书。ChainElement因此不会添加任何内容


我需要理解这件事,但我做的所有测试都表明该方法有效

嗨,你可能是对的,但答案没有得到很好的回答。问题是,我如何让它只构建一个由适当ca签名的证书?生成方法不只是对证书属性的“结构性”检查吗?@BlackShawarna build说,生成的链没有过期,根目录受信任,等等,除了使用chain.chainPolyxy.ValidationFlags抑制的任何检查。具体的CA检查是后面的一部分,您可以将链式元素的字节与您愿意接受的答案进行比较。最后一个用于root,第二个用于directissuer,etcWhat-doesextrastore不受限制,它提供额外的证书来帮助完成链。它不提供信任数据。你是说这里?我有点困惑,它没有提供信任数据。@mslot外部存储中提供的证书并不意味着链必然会说“链是受信任的”,外部存储可以提供许多未被使用的证书,并且链可以在根本不使用外部存储的情况下成功。只是。。。额外:你找到答案了吗?不幸的是,没有上面的代码,它是否按预期工作?这是使其与Cloudflare协同工作的唯一方法: