使用CA证书文件验证远程服务器的X509证书

7
我使用OpenSSL生成了一个CA和多个证书(由CA签名),我有一个.NET/C#客户端和服务器,两者都使用SslStream并且具有自己的证书/密钥,启用了双向身份验证并禁用了吊销。
我正在使用RemoteCertificateValidationCallback来验证远程服务器的证书。我希望只加载程序中CA的公共证书文件就可以验证远程证书,而无需在Windows证书存储中安装CA。然而,X509Chain不会显示其他内容,除非我将CA安装到存储中,也不会在打开PEM版本的证书时提供Windows CryptoAPI shell.
我的问题是,当RemoteCertificateValidationCallback、X509Certificate和X509Chain似乎没有给我任何工作材料时,我如何使用CA的公共证书文件验证证书是否已由我的特定CA签名,而无需使用Windows证书存储或WCF?

1
我不明白为什么将CA证书添加到Windows Store会导致X509Chain在链中显示它,但如果我不这样做,它就不是其中的一部分。是否有人可以在RemoteCertificateValidationCallback中将CA证书添加到链中? - user985122
我仍然找不到答案 :( - user985122
2个回答

10

因为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”来构建证书链并验证信任。选项清晰,错误易于理解。 - JB.
请查看此处的响应 https://dev59.com/uYXca4cB1Zd3GeqPNcha#27310276 ,以正确检查您自己的 CA。上面所述的方法将接受每个 CA (无效性检查)。 - Zoka

1
如何仅通过使用CA的公共证书文件验证证书是否已由我的特定CA签名,而不使用Windows证书存储或WCF?当RemoteCertificateValidationCallback、X509Certificate和X509Chain似乎没有提供任何可用信息时,以下代码将避免使用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 socket上安装它,连接将“自动运行”。而且,我还没有想出如何安装它,使其传递到回调中。也就是说,我必须为每次回调构建链。我认为这些是.Net中的架构缺陷,但我可能会忽略一些明显的东西。


看起来还不错。chain2将是测试证书和根证书之间的证书信任列表。现在,如果您对证书执行高级操作,可能需要使用BouncyCastle库。 - JB.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接