【发布时间】:2018-10-12 19:39:45
【问题描述】:
我有一个 .crt 和 .key 文件,我正在使用 OpenSSL 创建一个 .pfx 文件。我正在尝试使用 PowerShell 将 .pfx 文件导入 Cert:\LocalMachine\My,然后我会将该证书用于 OpenVPN。使用以下代码,我在导入时没有收到任何错误:
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$cert.import("$env:TEMP\$site.pfx", $certPassword, "PersistKeySet")
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store("My", "LocalMachine")
$store.open("MaxAllowed")
$store.add($cert)
$store.close()
我可以在 MMC 中看到证书,但 OpenVPN 的日志文件显示:
错误:C5066064:microsoft cryptoapi:CryptAcquireCertificatePrivateKey:Keyset 不存在
我已经尝试将 $certPassword 作为字符串和安全字符串。当我通过 GUI 导入证书(从 $certPassword 的内容中复制密码)时,OpenVPN 正常启动。
我也试过这段代码,但看到了同样的行为:
Import-PfxCertificate -Password ($certPassword | ConvertTo-SecureString -AsPlainText -Force) -CertStoreLocation Cert:\LocalMachine\My -FilePath $env:temp\$site.pfx
最后,我正在运行提升的 PowerShell 会话。
我做错了什么?谢谢。
【问题讨论】:
-
OpenVPN在什么账户下运行?通过 PowerShell 导入 PFX 后,该帐户是否有权访问私钥?
-
OpenVPN 作为本地系统运行,所以我假设它可以访问私钥。据我了解,由于我使用的是 LocalMachine 根存储,因此私钥应位于 C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys 中。我在那里看到一个文件,我认为它是正确的,因为它的“修改”属性上有正确的时间戳。
标签: powershell openssl ssl-certificate x509certificate2