Use PowerShell on Ubuntu to convert PFX to KEY and CER
A little PowerShell (Core 6.1.0) script I’m using on Ubuntu 18.04.1 to assist with converting PFX to .KEY and .CER files for use with nginx/Apache. Relies on the existence of openssl of course which is why it’s running on Ubuntu.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
param( [parameter(Mandatory = $true)][System.IO.FileInfo] $File, [parameter(Mandatory = $true)][SecureString] $Password ) Function ConvertTo-PlainText( [security.securestring]$secure ) { $marshal = [Runtime.InteropServices.Marshal] $marshal::PtrToStringAuto( $marshal::SecureStringToBSTR($secure) ) } Write-Host $File $ActualPass = ConvertTo-PlainText -secure $Password if ((-not(Test-Path $File)) -or ($File.Extension -ne '.pfx')) { Throw 'Need a PFX file...' } openssl pkcs12 -in $File.FullName -nocerts -out "$(Get-Location)/$($File.BaseName)-encrypted.key" -passin pass:$ActualPass -passout pass:$ActualPass openssl pkcs12 -in $File.FullName -clcerts -nokeys -out "$(Get-Location)/$($File.BaseName).cer" -passin pass:$ActualPass openssl rsa -in "$(Get-Location)/$($File.BaseName)-encrypted.key" -out "$(Get-Location)/$($File.BaseName).key" -passin pass:$ActualPass |
No doubt there are improvements to be made but it’s functional.
– Lewis