【问题标题】:Checking certificate by x509Crl.IsRevoked() method in BouncyCastle library in C#?在 C# 的 BouncyCastle 库中通过 x509Crl.IsRevoked() 方法检查证书?
【发布时间】:2020-09-02 16:49:27
【问题描述】:

我正在尝试使用其吊销列表(crl 文件)检查证书。在 BouncyCustle 库中有一个方法x509Crl.IsRevoked(),应该用于此目的。关键是它获取x509Certificate 对象作为参数,但我不明白如何创建这个x509Certificate 对象。 我使用DotNetUtilities.FromX509Certificate()System.Security.Cryptography.X509Certificates.x509Certificate2 对象转换为Org.BouncyCastle.X509.X509Certificate 对象,但我遇到了问题——方法IsRevoked() 总是返回true——对于我测试的所有crl。

问题:如何直接从二进制创建Org.BouncyCastle.X509.X509Certificate对象而不从System.Security.Cryptography.X509Certificates.x509Certificate2转换?

我用它的 crl 文件检查证书的代码:

static public void RevocationChecker(string certPath, string crlPath)
    {
        X509Certificate2 cert = new X509Certificate2();
        cert.Import(File.ReadAllBytes(certPath));
        Org.BouncyCastle.X509.X509Certificate bouncyCert = DotNetUtilities.FromX509Certificate(cert);

        X509CrlParser crlParser = new X509CrlParser();
        X509Crl crl = crlParser.ReadCrl(File.ReadAllBytes(crlPath));

        bool rezult = crl.IsRevoked(bouncyCert);
        Console.WriteLine(rezult);
    }

【问题讨论】:

    标签: c# bouncycastle x509certificate x509certificate2 certificate-revocation


    【解决方案1】:

    试一试:

    System.Security.Cryptography.X509Certificates.X509Certificate cert = new System.Security
    .Cryptography.X509Certificates.X509Certificate(File.ReadAllBytes(certPath));`    
    
    Org.BouncyCastle.X509.X509Certificate bouncyCert = new Org.BouncyCastle.X509
    .X509CertificateParser().ReadCertificate(cert.GetRawCertData());
    

    【讨论】:

    • 不幸的是它不起作用 - VS 显示错误 CS0122“类由于其保护级别而无法访问”,因为 Org.BouncyCastle.X509.X509Certificate() 构造函数是“受保护的”
    • @АлександрИльин 更新了答案以进行澄清。
    • 我测试了您的代码 - 它可以工作,但 IsRevoced() 方法也返回“true”。它不应该返回“true”——我将 crl 文件转换为 txt 文件,但发现与我的证书序列号不匹配。我不明白为什么该方法返回“true”...
    • 非常抱歉 - 我在错误的文件上测试了您的代码!有用!非常感谢您的帮助!
    • @АлександрИльин 很高兴为您提供帮助!如果您发现它有用,请考虑接受答案。 :)
    猜你喜欢
    • 1970-01-01
    • 2012-05-30
    • 2011-10-13
    • 2013-01-14
    • 2012-06-22
    • 2014-05-19
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多