0

我想编写一个监控 CRL(证书撤销列表)到期日期的程序。因此,我想从 CRL 文件中读取以下属性:1) 生效日期 2) 下一次更新 3) 下一次 CRL 发布

我怎样才能完成我的任务?我只设法找到 X509Certificate2、X509Chain、x509RevocationMode 等的类型。

4

4 回答 4

3

您可以使用 X509Certificate2 类来获取所需的信息。

示例:处理一份认证文件

X509Certificate2 x509 = new X509Certificate2();
byte[] rawData = ReadFile(fname);
x509.Import(rawData);
var validDate= x509 . NotBefore;    
var expireDate = x509.NotAfter;


//Reads a file.
internal static byte[] ReadFile (string fileName)
{
    FileStream f = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    int size = (int)f.Length;
    byte[] data = new byte[size];
    size = f.Read(data, 0, size);
    f.Close();
    return data;
}

参考:

https://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509certificate2(v=vs.110).aspx

编辑:

您可以使用 BouncyCastle.Crypto 库来处理 CRL。下载库并引用 BouncyCastle.Crypto.dll 或安装 nuget 包:

Install-Package BouncyCastle


  //reference library BouncyCastle.Crypto
  //http://www.bouncycastle.org/csharp/
  //Load CRL file and access its properties
    public void  GetCrlInfo(string fileName, Org.BouncyCastle.Math.BigInteger serialNumber, Org.BouncyCastle.X509.X509Certificate cert)
    {
        try
        {
            byte[] buf = ReadFile(fileName);
            X509CrlParser xx = new X509CrlParser();
            X509Crl ss = xx.ReadCrl(buf);
            var nextupdate = ss.NextUpdate;
            var isRevoked = ss.IsRevoked(cert);
            Console.WriteLine("{0} {1}",nextupdate,isRevoked);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
于 2016-07-19T21:19:12.200 回答
1

虽然问题得到了回答,但我要补充一点,还有另一个很好的开放项目可以扩展原生 .NET Framework 以处理 .NET 中缺少的加密对象:https ://github.com/Crypt32/pkix.net

关于 CRL,我X509CRL2以与内置类似的方式开发了一个类X509Certificate2X509CRL2 Class。用法很简单:

// reference System.Security.Cryptography.X509Certificates namespace
var crl = new X509CRL2(@"C:\temp\crlfile.crl");
// Effective date:
var effective = crl.ThisUpdate;
// next update:
var nextupdate = crl.NextUpdate;
// next publish:
var nextPublishExtension = crl.Extensions["1.3.6.1.4.1.311.21.4"];
if (nextPublishExtension != null) { nextPublishExtension.Format(1); }

我支持多种格式的 CRL 文件,包括纯二进制、Base64 甚至十六进制。

通过使用此类,您不仅可以读取 CRL 属性,还可以生成版本 2 CRL。

注意:pkix.net 库依赖于我的另一个开放项目https://github.com/Crypt32/Asn1DerParser.NET,它用于解析 ASN 结构。

于 2016-07-20T05:31:32.527 回答
0

除了 M.Hassan 的帖子;

使用 BouncyCastle.X509 您必须将 System.Security... X509Certificate2 转换为 BouncyCastle 证书,初始代码和编辑之间缺少的功能可能是:

using System.Security.Cryptography.X509Certificates;

public static Org.BouncyCastle.X509.X509Certificate Convert(X509Certificate2 certificate) 
{
    var certificateParser = new Org.BouncyCastle.X509.X509CertificateParser();
    var rawData = certificate.GetRawCertData();
    var bouncyCertificate = certificateParser.ReadCertificate(rawData);
    return bouncyCertificate;
}
于 2019-10-30T11:11:58.603 回答
0

我们可以使用 CertEnroll win32 API。代码可以

CX509CertificateRevocationList crl = new CX509CertificateRevocationList();
crl.InitializeDecode(File.ReadAllText(crlFile), EncodingType.XCN_CRYPT_STRING_BASE64_ANY);

将以下内容添加到 csproj 以包含 certEnrol

<ItemGroup>
<COMReference Include="CERTENROLLLib">
  <WrapperTool>tlbimp</WrapperTool>
  <VersionMinor>0</VersionMinor>
  <VersionMajor>1</VersionMajor>
  <Guid>728ab348-217d-11da-b2a4-000e7bbb2b09</Guid>
  <Lcid>0</Lcid>
  <Isolated>false</Isolated>
  <EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
于 2021-06-03T04:44:58.337 回答