简体   繁体   English

检索X509Certificate2对象的颁发者

[英]Retrieving issuer of a X509Certificate2 object

I have a X509Certificate2 object retrieved from X509Store. 我有从X509Store检索的X509Certificate2对象。 I want to get the issuer of this certificate but the only two properties that this object offers are X509Certificate2.Issuer and X509Certificate2.IssuerName where .Issuer is kinda misleading as it returs string that is basically issuer's name. 我想得到这个证书的发行者,但是这个对象提供的唯一两个属性是X509Certificate2.IssuerX509Certificate2.IssuerName ,其中.Issuer有点误导,因为它重写了基本上是发行者名字的字符串。

Both those properties can at most return a Distinguished Name but DNs are not unique, right? 这两个属性最多只返回一个专有名称,但DN不是唯一的,对吧? Therefore I don't want to use X509Certificate2Collection.Find method with X509FindType.FindByIssuerDistinguishedName flag. 因此,我不想将X509Certificate2Collection.Find方法与X509FindType.FindByIssuerDistinguishedName标志一起使用。

How can I get a certificate's issuer and be sure I have the "right one". 我怎样才能获得证书颁发者,并确保我拥有“正确的”证书。 Note: I don't have to use X509Certificate2 object. 注意:我不必使用X509Certificate2对象。 Alternatives are welcome. 欢迎替代方案。

If I understand you correctly, you have a certificate and you want to find the issuer certificate. 如果我理解正确,您有证书,并且您想要找到颁发者证书。 This can be done as follows: 这可以按如下方式完成:

  1. check if the leaf certificate's Subject and Issuer fields are not the same. 检查叶证书的Subject和Issuer字段是否不相同。 Otherwise, the certificate is the issuer (self-signed certificate) 否则,证书是颁发者(自签名证书)

  2. Instatniate X509Chain object and pass leaf certificate to X509Chain.Build method. Institniate X509Chain对象并将叶证书传递给X509Chain.Build方法。 Examine ChainElements property (a collection) and element at index 1 is the issuer. 检查ChainElements属性(集合)和索引1处的元素是发行者。

     using System.Security.Cryptography.X509Certificates; namespace Name { class Class1 { public static X509Certificate2 GetIssuer(X509Certificate2 leafCert) { if (leafCert.Subject == leafCert.Issuer) { return leafCert; } X509Chain chain = new X509Chain(); chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; chain.Build(leafCert); X509Certificate2 issuer = null; if (chain.ChainElements.Count > 1) { issuer = chain.ChainElements[1].Certificate; } chain.Reset(); return issuer; } } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM