【发布时间】:2016-09-11 21:06:44
【问题描述】:
目标
好的,所以我在一个目录中有openssl for windows,我正在尝试运行一个脚本,它可以让我获取一个包含我需要的所有内容的 pfx 文件,并将其拆分为我需要的所有文件。这将允许稍后轻松上传到 AWS IAM 证书存储。
问题
我将在底部发布我的内容,唯一的问题是命令由于某种原因没有正确传递给 openssl.exe,但是如果你复制它在“$argu”处创建的输出并针对.exe 手动它完美地工作。我终其一生都无法解决它的问题。
更新
我尝试使用以下两种方法来调用 .exe,但似乎参数字符串未正确转换,并且在参数中添加了一个额外的撇号,导致 openssl 失败。
尝试 1
& $opssl $argu
尝试 2 在我更新的代码下方。 使用选项 1 时,OpenSSL.exe 会引发以下错误。在将字符串传递给 .exe 之前,是否有更好的方法来创建字符串?
错误
openssl:Error: 'pkcs12 -in 'E:\certs\openssl\domain.com.au\domain.com.au-PFX.pfx' -passin pass:(removed) -nokeys -cacerts -out 'domain.com.au-PFX-20160517\domain.com.au-PFX-CA-Cert.pem'' is an invalid command.
脚本
$exedir = split-path -parent $MyInvocation.MyCommand.Definition
cd $exedir
#Powershell to use open SSL to convert a pfx to pem
Write-output "Please enter full path and PFX file"
$cert = read-host
Write-output "Please enter password for you PFX"
$pfxpass = read-host
$date = Get-date -Format "yyyMMdd"
$certDirf = Get-item "$cert" | select basename
$string = [io.path]::GetFileNameWithoutExtension($cert)
$string2 = $string.Substring(0)
$certDir = $string2
$opssl = "$exedir\openssl.exe"
Write-output "$certDir"
$opssltest = If (Test-Path $exedir\openssl.exe){
Write-host "found OpenSSL.exe"
}
Else
{
write-host "couldn't find openSSL.exe"
}
Invoke-Command -scriptblock { $opssltest }
Function Get-Key {
if (!(test-path $certdir-$date)){mkdir $certdir-$date -force}
cd $exedir
$Argu = "pkcs12 -in '$cert' -passin pass:$pfxpass -nocerts -out '$certdir-$date\$certdir-encrypted-key.pem' -nodes"
Write-host "$argu"
Start-Process -FilePath "$opssl" -ArgumentList "pkcs12 -in '$cert' -passin pass:$pfxpass -nocerts -out '$certdir-$date\$certdir-encrypted-key.pem' -nodes"
Write-host "encrypted key written"
sleep 1
$Argu = "rsa -in $certdir-$date\$certdir-encrypted-key.pem -out $certdir-$date\$certdir-key.pem"
Write-host "$argu"
Start-Process -FilePath "$opssl" -ArgumentList "rsa -in '$certdir-$date\$certdir-encrypted-key.pem' -out '$certdir-$date\$certdir-key.pem'"
Write-host "Key Un-encrypted"
Menu
}
Function Get-Cert {
if (!(test-path $certdir-$date)){mkdir $certdir-$date -force}
cd $exedir
$Argu = "pkcs12 -in '$cert' -passin pass:$pfxpass -nokeys -clcerts -out '$certdir-$date\$certdir-Cert.pem'"
Write-host "$Argu"
Start-Process -FilePath "$opssl" -ArgumentList "pkcs12 -in '$cert' -passin pass:$pfxpass -nokeys -clcerts -out '$certdir-$date\$certdir-Cert.pem'"
Write-host "Cert exported"
Menu
}
Function Get-CACert {
if (!(test-path $certdir-$date)){mkdir $certdir-$date -force}
cd $exedir
$Argu = "pkcs12 -in '$cert' -passin pass:$pfxpass -nokeys -cacerts -out '$certdir-$date\$certdir-CA-Cert.pem'"
Write-host "$argu"
$type = "pkcs12"
Start-Process -FilePath "$opssl" -ArgumentList "pkcs12 -in '$cert' -passin pass:$pfxpass -nokeys -cacerts -out '$certdir-$date\$certdir-CA-Cert.pem'"
Write-host "CA-Cert exported"
Menu
}
Function Menu{
[int]$xMenuChoiceA = 0
while ( $xMenuChoiceA -lt 1 -or $xMenuChoiceA -gt 4 ){
Write-host "All exports are in PEM format except for privet keys"
Write-host "1. Export privet key"
Write-host "2. Export Certificate"
Write-host "3. Export Ca Certificaet"
write-host "4. Exit"
[Int]$xMenuChoiceA = read-host "Please enter an option 1 to 4..." }
Switch( $xMenuChoiceA ){
1{Get-Key}
2{Get-Cert}
3{Get-CACert}
4{Exit}
}
}
Menu
【问题讨论】:
-
随着时间的推移,我最终建立了这个,bitbucket.org/LucasSymons/pfxtopempfxtopem/wiki/Home
标签: powershell ssl openssl pem pfx