如何解决X509FindType.FindBySubjectDistinguishedName 没有找到我的证书?
我正在制作一个自签名 X509 证书,如下所示:
public X509Certificate2 GenerateSelfSignedCertificate(Guid customerId)
{
var distinguishedName = GetDistinguishedName(customerId);
var rsa = RSA.Create(KeySize);
var request = new CertificateRequest(distinguishedName,rsa,HashAlgorithm,Padding);
request.CertificateExtensions.Add(new X509BasicConstraintsExtension(false,false,false));
request.CertificateExtensions.Add(new X509KeyUsageExtension(
X509KeyUsageFlags.DigitalSignature | X509KeyUsageFlags.DataEncipherment |
X509KeyUsageFlags.NonRepudiation,false));
var cert = request.CreateSelfSigned(DateTimeOffset.Now,DateTimeOffset.Now.Add(CertificateLifespan));
return cert;
}
然后我使用此方法生成证书并使用以下方法存储它:
public void StoreCertificate(X509Certificate2 certificate)
{
using var store = new X509Store(StoreName.My,StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
store.Add(certificate);
store.Close();
}
接下来,我尝试使用以下方法从证书存储中获取证书:
public X509Certificate2 FetchPrivateCertificate(Guid customerId)
{
var distinguishedName = GetDistinguishedName(customerId);
using var store = new X509Store(StoreName.My,StoreLocation.CurrentUser);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
var certs = store.Certificates.Find(X509FindType.FindBySubjectDistinguishedName,distinguishedName,false);
store.Close();
var privateCert = certs.OfType<X509Certificate2>().FirstOrDefault(x => x.HasPrivateKey);
return privateCert;
}
...使用相同的customerId
,此结果始终为空。甚至无法检查证书是否具有私钥,证书集合始终为空。可分辨名称的生成方式完全相同。证书肯定在证书存储中。
我错过了什么?
更新
根据请求,我添加了专有名称的生成方式:
private string GetDistinguishedName(Guid customerId)
{
var domain = IPGlobalProperties.GetIPGlobalProperties().DomainName;
var domainDistinguishedName = string.Join(",DC=",domain.Split("."));
var distinguishedName = $"DC={Environment.MachineName},DC={domainDistinguishedName},O={customerId}";
return distinguishedName;
}
解决方法
问题(如评论中指出的)是您的 GetDistinguishedName
返回未格式化的 x.500 字符串,这足以构建 X.500 名称,但不足以用于搜索,因为搜索功能使序数和大小写-输入字符串和 *formatted* X.500 字符串的不敏感比较。也就是说,您可以使用这些字符串格式来构建 X.500 名称:
CN=my common name,O=example LLC
CN = my common name,O = example LLC
CN= my common name,O =example LLC
<other variations>
CryptoAPI 使用 X.500 格式化程序将任何有效的 X.500 字符串格式化为外观一致的字符串,例如大写 RDN 属性名称,删除 =
字符周围的空格,在 RDN 分隔符后添加空格等等,该函数可能会重新排序字符串中的 RDN 属性。当按 X.500 名称搜索证书时,您必须将格式化/消毒的 X.500 名称传递给搜索功能。最简单的方法是更新您的 GetDistinguishedName
函数,如下所示:
private string GetDistinguishedName(Guid customerId)
{
var domain = IPGlobalProperties.GetIPGlobalProperties().DomainName;
var domainDistinguishedName = string.Join(",DC=",domain.Split("."));
var distinguishedName = $"DC={Environment.MachineName},DC={domainDistinguishedName},O={customerId}";
return new X500DistinguishedName(distinguishedName).Name;
}
即在返回之前强制格式化字符串。
附言DC={Environment.MachineName}
使用了不正确的 RDN 属性。 DC 代表 domainComponent
。机器名称无论如何都不是域组件。使用 CN
(通用名称)属性来存储机器名称。而 O
代表 Organization
。我怀疑它也被滥用了,因为 customerId
几乎没有组织名称(只是一个假设)。您应该在主题/颁发者字符串中使用正确的 RDN 类型。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。