2015-06-10 30 views
2

我有證書驗證問題。我有.perm文件女巫是鏈證書文件(裏面有多個BEGIN和END證書)。來自文件的ChainCertificate

我嘗試導入收藏證書,但進口收集後的長度爲1

X509Certificate2Collection collection = new X509Certificate2Collection(); 
collection.Import(certpath); 

我沒有看到任何有趣的選項

X509Chain chain2 = new X509Chain(); 

我得到驗證的回報虛假的,我相信原因是並非所有的證書都已被加載。

下面是我的全部驗證方法

private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
    { 
     try 
     { 
      string certpath = "actual path"; 

      X509Certificate2Collection collection = new X509Certificate2Collection(); 
      collection.Import(certpath); 

      X509Chain chain2 = new X509Chain(); 
      foreach(X509Certificate2 c in collection) 
      { 
       chain2.ChainPolicy.ExtraStore.Add(c); 
      } 

      // Check all properties 
      chain2.ChainPolicy.VerificationFlags = X509VerificationFlags.NoFlag; 

      // This setup does not have revocation information 
      chain2.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; 

      // Build the chain 
      chain2.Build(new X509Certificate2(certificate)); 

      // Are there any failures from building the chain? 
      if (chain2.ChainStatus.Length == 0) 
       return true; 

      // If there is a status, verify the status is NoError 
      bool result = chain2.ChainStatus[0].Status == X509ChainStatusFlags.NoError; 

      return result; 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex); 
     } 
     return false; 
    } 
+1

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

1

X509Certificate2CollectionImport方法不支持包含多個證書(附加了一個又一個)的文件。請參閱此方法的文檔here

有一種格式可能工作 - SerializedStore但文檔沒有多說這件事。我認爲它是SerializedCert的某個國王,它是具有它屬性的證書,所以即使這種格式與你擁有的格式不符。

嘗試分離證書並使用this constructor初始化X509Certificate2Collection