如果你知道哪些证书可以是根证书和中间证书的证书来检查,你可以加载根和中间的公共密钥ChainPolicy.ExtraStore
X509Chain
对象集合
我的任务是编写一个Windows Forms应用程序来安装证书,只要它是依赖于我国政府已知的“国家根证书”发布的。是允许颁发证书来验证与国家Web服务的连接的有限数量的CA,所以我拥有了一个有限的一组证书可能在链中,并且可能在目标机器上丢失。我收集了CA和政府根证书的所有公钥,并将其存储在应用的子目录“cert”中:
在Visual Studio中,我将该目录cert添加到解决方案中,资源。这让我枚举了我的c#库代码中的“可信”证书集合,即使未安装颁发者证书,也可以构建一个链来检查证书。我做了一个包装类X509Chain为了这个目的:
private class X509TestChain : X509Chain, IDisposable
{
public X509TestChain(X509Certificate2 oCert)
: base(false)
{
try
{
ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority;
if (!Build(oCert) || (ChainElements.Count <= 1))
{
Trace.WriteLine("X509Chain.Build failed with installed certificates.");
Assembly asmExe = System.Reflection.Assembly.GetEntryAssembly();
if (asmExe != null)
{
string[] asResources = asmExe.GetManifestResourceNames();
foreach (string sResource in asResources)
{
if (sResource.IndexOf(".cert.") >= 0)
{
try
{
using (Stream str = asmExe.GetManifestResourceStream(sResource))
using (BinaryReader br = new BinaryReader(str))
{
byte[] abResCert = new byte[str.Length];
br.Read(abResCert, 0, abResCert.Length);
X509Certificate2 oResCert = new X509Certificate2(abResCert);
Trace.WriteLine("Adding extra certificate: " + oResCert.Subject);
ChainPolicy.ExtraStore.Add(oResCert);
}
}
catch (Exception ex)
{
Trace.Write(ex);
}
}
}
}
if (Build(oCert) && (ChainElements.Count > 1))
Trace.WriteLine("X509Chain.Build succeeded with extra certificates.");
else
Trace.WriteLine("X509Chain.Build still fails with extra certificates.");
}
}
catch (Exception ex)
{
Trace.Write(ex);
}
}
public void Dispose()
{
try
{
Trace.WriteLine(string.Format("Dispose: remove {0} extra certificates.", ChainPolicy.ExtraStore.Count));
ChainPolicy.ExtraStore.Clear();
}
catch (Exception ex)
{
Trace.Write(ex);
}
}
}
在调用函数,我现在就可以成功地检查是否有未知的证书由国家根证书导出:
bool bChainOK = false;
using (X509TestChain oChain = new X509TestChain(oCert))
{
if ((oChain.ChainElements.Count > 0)
&& IsPKIOverheidRootCert(oChain.ChainElements[oChain.ChainElements.Count - 1].Certificate))
bChainOK = true;
if (!bChainOK)
{
TraceChain(oChain);
sMessage = "Root certificate not present or not PKI Overheid (Staat der Nederlanden)";
return false;
}
}
return true;
要完成图片:检查根证书(通常是安装的,因为它包含在Windows Update中,但理论上也可能丢失),我将友好名称和指纹与公布的值进行比较:
private static bool IsPKIOverheidRootCert(X509Certificate2 oCert)
{
if (oCert != null)
{
string sFriendlyName = oCert.FriendlyName;
if ((sFriendlyName.IndexOf("Staat der Nederlanden") >= 0)
&& (sFriendlyName.IndexOf(" Root CA") >= 0))
{
switch (oCert.Thumbprint)
{
case "101DFA3FD50BCBBB9BB5600C1955A41AF4733A04": // Staat der Nederlanden Root CA - G1
case "59AF82799186C7B47507CBCF035746EB04DDB716": // Staat der Nederlanden Root CA - G2
case "76E27EC14FDB82C1C0A675B505BE3D29B4EDDBBB": // Staat der Nederlanden EV Root CA
return true;
}
}
}
return false;
}
我不确定这个检查是否安全,但在我的情况下,Windows Forms应用程序的操作员非常确定可以访问要安装的有效证书。该软件的目标是过滤证书列表,以帮助他在计算机的机器存储区中只安装正确的证书(该软件还安装中间证书和根证书的公钥,以确保证书的运行时行为Web服务客户端是正确的)。
我编辑了这个问题,它是一个.NET 2.0 Winforms应用程序。 – RainerM 2011-05-23 14:54:23