【问题标题】:Verify Remote Server X509Certificate using CA Certificate File使用 CA 证书文件验证远程服务器 X509Certificate
【发布时间】:2011-10-08 07:35:30
【问题描述】:

我已经使用 OpenSSL 生成了一个 CA 和多个证书(由 CA 签名),并且我有一个 .NET/C# 客户端和服务器,它们都使用 SslStream,每个都有自己的证书/密钥,启用相互身份验证并撤销已禁用。

我正在使用RemoteCertificateValidationCallback for SslStream 来验证远程服务器的证书,我希望我可以在程序中加载 CA 的公共证书(作为文件)并使用它来验证远程证书而不是实际在 Windows 证书存储中安装 CA。问题是 X509Chain 不会显示任何其他内容,除非我将 CA 安装到商店中,当我打开其中一个证书的 PEM 版本时,Windows CryptoAPI shell 也不会显示。

我的问题是,当RemoteCertificateValidationCallbackX509Certificate 时,如何仅使用 CA 的公共证书文件不使用 Windows 证书存储或 WCF 验证证书已由我的特定 CA 签名和X509Chain 似乎没有给我任何工作?

【问题讨论】:

  • 我不明白为什么将 CA 证书添加到 Windows 应用商店会导致 X509Chain 在链中显示它,但如果我不这样做,它就不是其中的一部分。是否有人将 CA 证书添加到 RemoteCertificateValidationCallback 中的链中?
  • 我还是找不到答案:(

标签: c# ssl openssl x509certificate sslstream


【解决方案1】:

因为 CA 证书不在根证书存储中,所以您将在 RemoteCertificateValidationCallback() 中有一个 SslPolicyErrors.RemoteCertificateChainErrors 的错误标志;一种可能性是针对您自己的 X509Certificate2Collection 明确验证证书链,因为您没有使用本地存储。

if (sslPolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors)
{
    X509Chain chain0 = new X509Chain();
    chain0.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
    // add all your extra certificate chain
    chain0.ChainPolicy.ExtraStore.Add(new X509Certificate2(PublicResource.my_ca));
    chain0.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority;
    isValid = chain0.Build((X509Certificate2)certificate);
}

您还可以重新使用回调中传递的链,在 ExtraStore 集合中添加您的额外证书,并使用 AllowUnknownCertificateAuthority 标志进行验证需要,因为您将不受信任的证书添加到链中。

您还可以通过在受信任的根存储中以编程方式添加 CA 证书来防止原始错误(当然它会打开一个弹出窗口,因为全局添加新的受信任的 CA 根是一个主要的安全问题):

var store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
X509Certificate2 ca_cert = new X509Certificate2(PublicResource.my_ca);
store.Add(ca_cert);
store.Close();

编辑:对于那些想用你的 CA 清楚地测试链的人:

另一种可能性是使用库BouncyCastle 来构建证书链并验证信任。选项很清楚,错误很容易理解。如果成功,它将构建链,否则返回异常。下面的示例:

// rootCerts : collection of CA
// currentCertificate : the one you want to test
var builderParams = new PkixBuilderParameters(rootCerts, 
                        new X509CertStoreSelector { Certificate = currentCertificate });
// crls : The certificate revocation list
builderParams.IsRevocationEnabled = crls.Count != 0;
// validationDate : probably "now"
builderParams.Date = new DateTimeObject(validationDate);

// The indermediate certs are items necessary to create the certificate chain
builderParams.AddStore(X509StoreFactory.Create("Certificate/Collection", new X509CollectionStoreParameters(intermediateCerts)));
builderParams.AddStore(X509StoreFactory.Create("CRL/Collection", new X509CollectionStoreParameters(crls)));

try
{
    PkixCertPathBuilderResult result = builder.Build(builderParams);
    return result.CertPath.Certificates.Cast<X509Certificate>();
    ...

【讨论】:

  • 另一种可能性是使用库BouncyCastle 来构建证书链并验证信任。选项清晰,错误易于理解。
  • 在此处查看回复stackoverflow.com/a/27310276/1038496 以正确检查您自己的 CA。上面完成的方法将接受每个 CA(不检查有效性)。
【解决方案2】:

当 RemoteCertificateValidationCallback、X509Certificate 和 X509Chain 似乎对我没有任何帮助时,我如何仅使用 CA 的公共证书文件而不使用 Windows 证书存储或 WCF 来验证证书是否已由我的特定 CA 签名?

以下代码将避免 Windows 证书存储并验证链。它与 JB 的代码有点不同,尤其是在标志的使用方面。下面的代码不需要AllowUnknownCertificateAuthority(但它确实使用X509RevocationMode.NoCheck,因为我没有CRL)。

函数的名称无关紧要。下面,VerifyServerCertificate 是与 SslStream 类中的 RemoteCertificateValidationCallback 相同的回调。您也可以将它用于ServicePointManager 中的ServerCertificateValidationCallback

static bool VerifyServerCertificate(object sender, X509Certificate certificate,
    X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    try
    {
        String CA_FILE = "ca-cert.der";
        X509Certificate2 ca = new X509Certificate2(CA_FILE);

        X509Chain chain2 = new X509Chain();
        chain2.ChainPolicy.ExtraStore.Add(ca);

        // Check all properties
        chain2.ChainPolicy.VerificationFlags = X509VerificationFlags.NoFlag;

        // This setup does not have revocation information
        chain2.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;

        // Build the chain
        chain2.Build(new X509Certificate2(certificate));

        // Are there any failures from building the chain?
        if (chain2.ChainStatus.Length == 0)
            return true;

        // If there is a status, verify the status is NoError
        bool result = chain2.ChainStatus[0].Status == X509ChainStatusFlags.NoError;
        Debug.Assert(result == true);

        return result;
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }

    return false;
}

想出了如何默认使用这个链(下面的chain2),这样就不需要回调了。也就是说,将它安装在 ssl 套接字上,连接将“正常工作”。而且我没有弄清楚如何安装它以便将其传递到回调中。也就是说,我必须为回调的每次调用构建链。我认为这些是 .Net 中的架构缺陷,但我可能遗漏了一些明显的东西。

【讨论】:

  • 看起来不错。 chain2 将是测试证书和根证书之间的证书信任列表。现在,如果您对证书执行高级操作,您可能希望使用 BouncyCastle 库。
猜你喜欢
  • 1970-01-01
  • 2020-11-30
  • 1970-01-01
  • 2016-05-30
  • 2010-10-21
  • 2011-07-28
  • 2021-01-29
  • 2021-07-22
  • 2016-12-20
相关资源
最近更新 更多