簡體   English   中英

使用 C# 從 CRL 文件中提取屬性

[英]Extract properties from a CRL file using C#

我想編寫一個程序來監視 CRL(證書吊銷列表)到期日期。 因此,我想從 CRL 文件中讀取以下屬性:1) 生效日期 2) 下一次更新 3) 下一次 CRL 發布

我怎樣才能完成我的任務? 我只找到了 X509Certificate2、X509Chain、x509RevocationMode 等的類型。

您可以使用 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);
        }
    }

雖然問題得到了回答,但我要補充的是,還有另一個很好的開放項目,它擴展了原生 .NET Framework 以使用 .NET 中缺少的加密對象: https : //github.com/Crypt32/pkix.net

關於 CRL,我以類似於內置X509Certificate2方式開發了一個X509CRL2類: X509CRL2 類 用法非常簡單:

// 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 結構。

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

我們可以使用 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM