Ssl

Powershell自簽名證書私鑰不可導出

  • February 26, 2021

使用 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 文件的審查沒有顯示任何參數。真的堅持這一點,所以任何建議都非常感謝。

這對我有用:

$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

引用自:https://serverfault.com/questions/1055229