【发布时间】:2021-05-15 08:21:28
【问题描述】:
我一直在使用以下 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")
}
我有什么遗漏的吗?
【问题讨论】:
标签: c# .net powershell certificate x509certificate