如何检查是否一个X509证书有"扩展验证"切换到? [英] How to check if a X509 certificate has "Extended Validation" switched on?
问题描述
我在努力寻找一种可靠的方式从我的C#检查(.NET 4.0)应用程序,如果一个X509Certificate(或X509Certificate2)具有扩展验证(EV)标志设置。有谁知道最好的方法是什么?
I'm struggling to find a reliable way to check from my C# (.Net 4.0) application if an X509Certificate (or X509Certificate2) has the "Extended Validation" (EV) flag set. Does anyone know the best method?
推荐答案
您可以检查 X509证书
包含这些 OID 一张。此外,您可以检查Chromium的源代码所实现的OID列表。你可以在这里找到源。如果你想坚持到Firefox,你可以在这里抢执行一>
You could check if the X509Certificate
contains one of these OIds. Additionally you can check Chromium's Source for a list of implemented OIds. You can find the Source here. If you'd like to stick to Firefox, you can grab the implementation here.
我现在更新了我的源和测试它。我写了一个小方法来验证 X509Certificate2
对维基百科/铬的OID列表。在这种方法中,我使用了维基百科列表中,它可能会更好走铬列表来代替。
I now updated my source and tested it. I've written a small method to validate a X509Certificate2
against the OId-List from Wikipedia/Chromium. In this method I am using the Wikipedia-List, it might be better to take the Chromium-List instead.
每个 CA
有一个或一个以上的ObjectID OID
秒。他们的不的保存您可能已经猜到的延伸,它们被保存为策略扩展中的一个条目。为了得到它的推荐使用的Oid确切的扩展策略扩展
本身,而然后用一个友好的名称。该策略扩展的OID是 2.5.29.32
。
Each CA
has one or more ObjectIds OId
s. They are not saved as an Extension as you might guess, they are saved as an entry within the Policy Extensions. To get the exact Extension it's recommended to use the Oid of the Policy Extension
itself rather then using a Friendly Name. The OId of the Policy Extensions is 2.5.29.32
.
要获得我们可以使用 System.Security.Cryptography.AsnEncodedData code>将其转换为可读
字符串
。字符串本身包含我们需要匹配我们的的String []
来保证,如果它包含一个 EV证书的OID的一个策略$ 。C $ C>
To get the inner content of the Policy Extensions we can use System.Security.Cryptography.AsnEncodedData
to convert it to a readable string
. The string itself contains the policies we need to match against our string[]
to ensure if it contains one of the OIds of an EV Certificate
.
/// <summary>
/// Checks if a X509Certificate2 contains Oids for EV
/// </summary>
/// <param name="certificate"></param>
/// <returns></returns>
private static bool IsCertificateEV(X509Certificate2 certificate)
{
// List of valid EV Oids
// You can find correct values here:
// http://code.google.com/searchframe#OAMlx_jo-ck/src/net/base/ev_root_ca_metadata.cc&exact_package=chromium
// or in Wikipedia
string[] extendedValidationOids =
{
"1.3.6.1.4.1.34697.2.1",
"1.3.6.1.4.1.34697.2.2",
"1.3.6.1.4.1.34697.2.1",
"1.3.6.1.4.1.34697.2.3",
"1.3.6.1.4.1.34697.2.4",
"1.2.40.0.17.1.22",
"2.16.578.1.26.1.3.3",
"1.3.6.1.4.1.17326.10.14.2.1.2",
"1.3.6.1.4.1.17326.10.8.12.1.2",
"1.3.6.1.4.1.6449.1.2.1.5.1",
"2.16.840.1.114412.2.1",
"2.16.528.1.1001.1.1.1.12.6.1.1.1",
"2.16.840.1.114028.10.1.2",
"1.3.6.1.4.1.14370.1.6",
"1.3.6.1.4.1.4146.1.1",
"2.16.840.1.114413.1.7.23.3",
"1.3.6.1.4.1.14777.6.1.1",
"1.3.6.1.4.1.14777.6.1.2",
"1.3.6.1.4.1.22234.2.5.2.3.1",
"1.3.6.1.4.1.782.1.2.1.8.1",
"1.3.6.1.4.1.8024.0.2.100.1.2",
"1.2.392.200091.100.721.1",
"2.16.840.1.114414.1.7.23.3",
"1.3.6.1.4.1.23223.2",
"1.3.6.1.4.1.23223.1.1.1",
"1.3.6.1.5.5.7.1.1",
"2.16.756.1.89.1.2.1.1",
"2.16.840.1.113733.1.7.48.1",
"2.16.840.1.114404.1.1.2.4.1",
"2.16.840.1.113733.1.7.23.6",
"1.3.6.1.4.1.6334.1.100.1",
};
// Logic:
// Locate Certificate Policy Extension
// Convert to AsnEncodedData (String)
// Check if any of the EV Oids exist
return (
from X509Extension ext in certificate.Extensions
where ext.Oid.Value == "2.5.29.32"
select new AsnEncodedData(ext.Oid, ext.RawData).Format(true))
.Any(asnConvertedData => extendedValidationOids.Where(asnConvertedData.Contains).Any()
);
}
如果你需要一些源代码开始:
If you need some source to get started:
static void Main(string[] args)
{
// Create Delegate for analysis of X509Certificate
ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
// Make sample request to EV-Website to get Certificate
var wc = new WebClient();
wc.DownloadString("https://startssl.com"); // EV
wc.DownloadString("https://petrasch.biz"); // Not EV
Console.ReadLine();
}
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
var cert = (X509Certificate2) certificate;
Console.WriteLine("Certificate: " + cert.GetNameInfo(X509NameType.SimpleName, true) + " -> " + IsCertificateEV(cert));
return true;
}
如果有人知道一个更好的方式来实现这一目标,请告诉我们。
If someone knows a better way to achieve this goal, please let us know.
这篇关于如何检查是否一个X509证书有"扩展验证"切换到?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!