Powershell 自签名证书私钥不可导出

Powershell 自签名证书私钥不可导出

使用 Powershell,我尝试创建一个带有可导出私钥的自签名 SSL 证书。我已阅读并遵循了各种教程,但最终结果始终是没有导出任何私钥。我使用 Powershell 是因为服务器运行的是 Windows Server 2016 Core 版本。我尝试过从远程计算机使用证书 MMC 控制台,但远程执行此操作时似乎并非所有功能都可用。无论如何,我遵循的各种教程都谈到了导出私钥的能力,但我尚未找到使此功能工作的具体代码示例。以下是我一直在尝试的代码:

$todaydt = Get-Date
$5years = $todaydt.AddYears(5)
$selfSignedRootCA = New-SelfSignedCertificate -DnsName SelfSignedRootCA -notafter $5years -CertStoreLocation Cert:\LocalMachine\My\ -KeyExportPolicy Exportable -KeyUsage CertSign,CRLSign,DigitalSignature -KeySpec KeyExchange -KeyLength 2048 -KeyUsageProperty All -KeyAlgorithm 'RSA' -HashAlgorithm 'SHA256' -Provider 'Microsoft Enhanced RSA and AES Cryptographic Provider'
New-SelfSignedCertificate -CertStoreLocation cert:\LocalMachine\My -DnsName "myserver.test" -notafter $5years -Signer $selfSignedRootCA -KeyUsage KeyEncipherment,DigitalSignature
$CertPassword = ConvertTo-SecureString -String "MyPassword" -Force -AsPlainText
Export-PfxCertificate -Cert cert:\LocalMachine\My\REDACTED-THUMBPRINT -FilePath C:\test.pfx -Password $CertPassword

我的研究表明,最常见的原因是用于创建证书的证书模板不允许导出私钥,因此按照这些建议,我复制了现有的证书模板并确保将其配置为允许导出密钥。但是,我不知道如何/在哪里在上述任何命令中指定新创建的模板。而且我对这些 cmdlet 的文档的审查没有显示任何用于执行此操作的参数。真的被困在这个问题上,所以非常感谢任何建议。

答案1

这对我有用:

$selfSignedRootCA = New-SelfSignedCertificate -DnsName SelfSignedRootCA -notafter (Get-Date).AddMonths(6) -CertStoreLocation Cert:\LocalMachine\My\ -KeyExportPolicy Exportable -KeyUsage CertSign,CRLSign,DigitalSignature -KeySpec KeyExchange -KeyLength 2048 -KeyUsageProperty All -KeyAlgorithm 'RSA' -HashAlgorithm 'SHA256' -Provider 'Microsoft Enhanced RSA and AES Cryptographic Provider'
$CertPassword = ConvertTo-SecureString -String "MyPassword" -Force -AsPlainText
$selfSignedRootCA | Export-PfxCertificate -FilePath C:\test.pfx -Password $CertPassword

相关内容