【问题标题】:Private key is not loading upon loading from the Certificate Store从证书存储加载时未加载私钥
【发布时间】:2019-06-27 10:07:14
【问题描述】:

RavenDB 4 的身份验证需要通过我通过 Let's Encrypt 创建的证书进行。我的私钥证书 (.pfx) 存储在 Azure Key Vault 中。因为只能上传受密码保护的证书,所以我通过密码使其安全。

通过使用 证书指纹 加载证书时,它总是显示它没有私钥,因此我的 RavenDB 实例的身份验证过程失败。通过 Windows 证书存储在本地和在 Azure 中进行。

我已经尝试在没有密码的情况下使用私钥证书,然后我就能够收到私钥了。但是,这不是解决方案,因为我无法将其上传到 Azure。

var certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
certStore.Open(OpenFlags.ReadOnly);
X509Certificate2 targetClientCertificate;
var certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, ravenDbSettings["CertificateThumbPrint"], false);
targetClientCertificate = new X509Certificate2(certCollection[0].GetRawCertData(), ravenDbSettings["CertificatePassword"], X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable); ;

我也通过使用我在某处找到的导出进行了尝试

var targetTwo = new X509Certificate2(certCollection[0].Export(X509ContentType.Pfx, ravenDbSettings["CertificatePassword"]));

然后我得到了

Internal.Cryptography.CryptoThrowHelper.WindowsCryptographicException: '密钥在指定状态下无效'

我的目标是加载证书的私钥,从而能够在 RavenDB 进行身份验证。

【问题讨论】:

  • 你试过targetClientCertificate = certCollection[0];吗?

标签: c# .net cryptography x509certificate certificate-store


【解决方案1】:

您必须使用存储在certCollection 变量中的现有证书。最后一行(填写targetClientCertificate 变量的位置)不是必需的,如果私钥不可导出,则该行将不起作用。如果代码需要X509Certificate2 类的现有实例,则它存储在certCollection 中,并且将具有关联的私钥链接。

【讨论】:

  • 不知道这个“安全”。花了一些时间来完全测试这一点。但这有效。谢谢!
【解决方案2】:

这可能与您创建证书的方式有关。 试试这个:

下载 openssl: http://gnuwin32.sourceforge.net/packages/openssl.htm

生成证书和私钥: openssl req -new -newkey rsa:1024 -days 365 -nodes -x509 -keyout YourName.key -out YourName.cert

生成 pfx 文件: openssl pkcs12 -export -inkey YourName.key -in YourName.cert -out YourName.pfx

导入 YourName.pfx 到商店

【讨论】:

  • OP 询问现有证书,他没有询问新证书的生成。
猜你喜欢
  • 2022-12-18
  • 2015-07-19
  • 2017-09-02
  • 2017-12-26
  • 1970-01-01
  • 2013-01-16
  • 2012-12-25
  • 2020-10-23
  • 1970-01-01
相关资源
最近更新 更多