c# - 导入证书时缺少 PrivateKey
问题描述
我一直在使用以下 cdmlet 将 PFX 证书导入 Windows Docker 容器:
Import-PfxCertificate -FilePath $CertificatePath -CertStoreLocation $location -Password $securePassword
直到 2 天前(可能与CVE-2021-1731有关?)之前,这一切都很好,当时一些测试开始失败并出现以下错误:
System.Security.Cryptography.CryptographicException :密钥在指定状态下无效。
运行Get-ChildItem Cert:\ -Recurse | Format-List *
时,我能够发现 Docker 映像的先前版本和最新版本之间的一些差异。
在以前的(工作版本)中:
HasPrivateKey : True
PrivateKey : System.Security.Cryptography.RSACryptoServiceProvider
在最新(失败版本)中:
HasPrivateKey : True
PrivateKey :
我正在尝试在LocalMachine
和CurrentUser
商店中安装证书。基于其他 SO 帖子,并提出了这个作为替代方案Import-PfxCertificate ...
:
$CertificatePath = "..."
$securePassword = "..."
$StoreLocations=@()
$StoreLocations += 'Cert:\LocalMachine\My'
$StoreLocations += 'Cert:\LocalMachine\Root'
$StoreLocations += 'Cert:\CurrentUser\My'
$StoreLocations += 'Cert:\CurrentUser\Root'
foreach($location in $StoreLocations){
$certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$certificate.Import($CertificatePath, $securePassword, "PersistKeySet,MachineKeySet")
$storeLocation = $location.Split("\")[1] # e.g. LocalMachine
$storeName = $location.Split("\")[2] # e.g. My
$store = New-object System.Security.Cryptography.X509Certificates.X509Store($storeName, $storeLocation)
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::"ReadWrite")
$store.Add($certificate)
$store.Close()
}
这似乎适用于LocalMachine
商店,但它失败CurrentUser
(拒绝访问)。
我还尝试根据商店设置不同的值,但没有运气:
if($location -Match "CurrentUser"){
$certificate.Import($CertificatePath, $securePassword, "PersistKeySet,UserKeySet")
}
elseif($location -Match "LocalMachine"){
$certificate.Import($CertificatePath, $securePassword, "PersistKeySet,MachineKeySet")
}
有什么我想念的吗?
解决方案
一切看起来都很好。HasPrivateKey
返回$true
,所以私钥在那里。属性为空的事实PrivateKey
意味着密钥存储在密钥存储提供程序中,而不是传统的 CSP。事实上,X509Certificate2.PrivateKey
属性从 .NET 4.6 开始就已经过时了。
推荐阅读
- excel - Excel VBA - 表的 ShowAllData 失败
- javascript - change color of the text on the indented tree - d3.js
- python - 在 url base64 编码中添加有效负载及其签名,然后对其进行解码
- asp.net-mvc - Linq 无法正常工作,未在 Ajax My Linq Query 中获取任何数据
- c++ - 对 unordered_map 使用 equal_range
- php - 我们如何使用 PHP 中的 GD 库将 html 表格转换为 png?
- c# - 将 HttpClientHandler.AutomaticDecompression 与 WebApplicationFactory.CreateClient() 一起使用
- makefile - 目标中的 Makefile ifndef 变量
- javascript - 如何使用 ExcelJS 读取 xlsx 文件?
- vim - vim 在带有 *.py 文件的目录中崩溃