【问题标题】:how to parse X509 ssl certificate custom extension "certificate policy"?如何解析 X509 ssl 证书自定义扩展“证书策略”?
【发布时间】:2014-10-19 21:29:52
【问题描述】:

我正在尝试解析 X509 证书自定义扩展。 (我创建了一个带有“证书策略”类型的自定义扩展的自签名证书)。我需要解析这个“证书策略”及其值。以编程方式,我可以获得“证书策略”的自定义扩展名,但我无法获得它的值。我使用的代码如下:

    public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        var cert = (X509Certificate2)certificate;
        foreach (X509Extension ext in cert.Extensions)
        {
          //  AsnEncodedData x = new AsnEncodedData(ext.Oid, ext.RawData);
            MessageBox.Show("Name: " + ext.Oid.FriendlyName + "\nValue: " + ext.Oid.Value);

        }

        return true;
    }

此代码仅显示“证书策略”,但我需要下面显示的策略的解析元素:

证书策略元素下的值:

[1]Certificate Policy:
     Policy Identifier=1.2.3.4
[2]Certificate Policy:
     Policy Identifier=1.5.6.7.8
[3]Certificate Policy:
     Policy Identifier=1.3.5.8
     [3,1]Policy Qualifier Info:
          Policy Qualifier Id=CPS
          Qualifier:
               Tarzano
     [3,2]Policy Qualifier Info:
          Policy Qualifier Id=User Notice
          Qualifier:
               Notice Reference:
                    Organization=Tarzano Ltd
                    Notice Number=1, 2, 3, 4
               Notice Text=Buraya mesaj yazilabilir

感谢您的帮助!

【问题讨论】:

    标签: c# ssl x509certificate asn.1 x509certificate2


    【解决方案1】:

    首先,您需要一个 ASN.1 解析器并使用 X.509 ASN.1 模块将扩展值解码为策略集合。我编写了一个托管类,它从我的 PowerShell PKI 模块中的 .NET 库中扩展了现有的 X.509 扩展。您可以从 PSPKI 项目站点获取托管 .dll 或项目源:http://pspki.codeplex.com/ 了解如何解码此扩展(我使用自己的 ASN.1 解析器,因此此处的代码 sn-ps 没有多大意义)如果您想拥有自己的东西(并且不要依赖第 3 方组件)。

    有一个 PKI.Core.dll(也附有来源)。 X.509 扩展类在 System.Security.Cryptography.X509Certificates 命名空间中定义。以及图书馆中此类的文档:http://pkix2.sysadmins.lv/library/html/T_System_Security_Cryptography_X509Certificates_X509CertificatePoliciesExtension.htm 扩展的制作方式与 .NET 本机类似(它们从 X509Extension 类继承),但我的扩展是完全本机的(不要使用 CryptoAPI c++ 函数,如在 .NET 中)。

    【讨论】:

    【解决方案2】:

    .NET 中似乎没有内置支持来解析 x509 扩展的 ASN.1 数据,除了 .Format() 方法之外,如果遇到任何未知对象类型,它会恢复为返回一个十六进制编码的字符串。

    但广泛使用的 BouncyCastle 库 https://www.bouncycastle.org/csharp/(也可通过 NuGet 获得)具有良好的 ASN.1 解析支持。这是一个打印证书扩展中找到的所有 OID 类型对象的示例。它适用于 .NET 无法解析和显示的扩展。 Org.BouncyCastle.Asn1.Utilities.Asn1Dump.DumpAsString() 方法也很有用。

    using System;
    using System.IO;
    using System.Linq;
    using System.Collections.Generic;
    using System.Security.Cryptography.X509Certificates;
    using Org.BouncyCastle.Asn1;
    public class AsnTest {
        public static void Main() {
            var certificate = new X509Certificate2(File.ReadAllBytes("Test.x509"));
            foreach (var ext in certificate.Extensions) {
                // This is as far as we reliably get with native .NET libraries, switch to BouncyCastle for additional parsing
                var o = new Asn1InputStream(ext.RawData).ReadObject();
                var q = new Queue<Asn1Sequence>();
                var i = new List<DerObjectIdentifier>();
                if (o is Asn1Sequence) {
                    q.Enqueue(o as Asn1Sequence);
                } else if (o is DerObjectIdentifier) {
                    i.Add(o as DerObjectIdentifier);
                }
                while (q.Any()) {
                    var s = q.Dequeue();
                    i.AddRange(s.OfType<DerObjectIdentifier>());
                    foreach (var n in s.OfType<Asn1Sequence>())
                    {
                        q.Enqueue(n);
                    }
                }
                if (i.Any()) {
                    Console.WriteLine("Found the follwing OID value(s) in the " + ext.Oid.Value + " extension: " + string.Join(", ", i.Select(j => j.Id)));
                } else {
                    Console.WriteLine("Found no OID values in the " + ext.Oid.Value + " extension.");
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多