【问题标题】:C# - Verify client certificate signed by proper rootC# - 验证由正确的根签名的客户端证书
【发布时间】:2018-10-19 10:47:20
【问题描述】:

我知道关于这个论点有很多问题,但我已经坚持了好几天了,所以我在这里。 我有一个根证书和一个客户端证书。我需要在 C# Web API 项目中复制命令 openssl verify -CAfile ca.pem client.pem 的作用。

这是我目前所知道的(希望这是真的):

  • Verify() 方法实际上验证证书是否由权威机构签署。它就像一个格式控件。哪个机构签署证书并不重要。
  • X509 Chain 是要走的路。将您的 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 properties 所以我尝试了不同的配置

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证书确定的证书才能通过验证,其他的都必须停止。

提前感谢您的帮助

【问题讨论】:

    标签: c# ssl certificate


    【解决方案1】:

    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);
    }
    

    我将证书和规范化的字节形式提升为静态,假设一旦进程开始它们就不会改变。如果证书可以动态更改,那么您应该相应地进行调整。

    【讨论】:

    • 您好,您可能是对的,但没有人回答。关键是,我怎样才能让它建立一个仅由适当的 ca 签名的证书?构建方法不只是对证书属性的“结构”检查吗?
    • @BlackShawarna Build 表示链已构建、未过期、根受信任等(除了使用 chain.ChainPolixy.ValidationFlags 抑制的任何检查)。特定的 CA 检查是之后的部分,您将链元素的字节与您愿意接受的答案进行比较。 (最后一个用于 root,第二个用于直接发行人等)
    • 什么是“ExtraStore 没有限制,它提供额外的证书来帮助完成链。它不提供信任数据。”是这里的意思吗?我对“它不提供信任数据”有点困惑。
    • @mslot ExtraStore 中提供的证书并不意味着链一定会说“该链是可信的”,ExtraStore 可以提供很多没有被使用的证书,并且该链完全不用ExtraStore也能成功。这只是......额外的:)
    【解决方案2】:

    找到问题。 就我而言,这是正确的回应。

     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;
        }
    

    现在,调试应用程序我有一些考虑:

    1. .Build() 方法应该做什么?

    我的意思是,使用由 ca 签名的证书或使用自签名证书,该方法始终返回 true 但如果我使用受信任的证书,它将在 chain.ChainElements 中添加证书,否则不会添加任何内容.

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

    【讨论】:

    • 你找到答案了吗?
    • 很遗憾没有
    • 以上代码,是否按预期工作?这是我可以让它与 Cloudflare 一起工作的唯一方法:)
    猜你喜欢
    • 2019-06-02
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2018-09-21
    • 2013-07-14
    • 2011-05-24
    • 2018-10-15
    • 1970-01-01
    相关资源
    最近更新 更多