Additions to the CPolydorou.Security Powershell Module
This post has been triggered by a project that I'm currently working on that involves nginx and containers. As part of the nginx configuration, I had to create a certificate key pair that was going to be used in order to secure traffic towards nginx.
The challenge I faced was to convert the PFX certificate that was handed to me by the Certificate Authority team to the format nginx understood. Considering that this was a process that I'd followed many times in the past (and also blogged about), I decided to update a Powershell module of mine named CPolydorou.Security in order to make the use of OpenSSL friendlier to the Windows administrator.
The four new functions that are included in the latest version (1.2.0) are:
- Export-ServerCertificateFromPFX
- Export-CertificateChainFromPFX
- Export-PrivateKeyFromPFX
- Decrypt-PrivateKey
 
PS C:\Certificate> Export-ServerCertificateFromPFX -PFXFilePath .\certificate.pfx -PFXPassphrase $pass 
PS C:\Certificate> Get-ChildItem
    Directory: C:\Certificate
Mode    LastWriteTime      Length Name
----    -------------      ------ ----
-a----  24/6/2021   3:29 μμ  6022   certificate.pfx
-a----  29/6/2021   7:16 μμ  2264   certificate.pfx.crt
 
PS C:\Certificate> Export-CertificateChainFromPFX -PFXFilePath .\certificate.pfx -PFXPassphrase $pass -OpenSSLOutput:$true -Verbose 
  VERBOSE: Extracting certificate chain from PFX file: .\certificate.pfx 
VERBOSE: Saving exported certificate at: C:\Certificate\certificate.pfx.chain.crt 
StandardError                                                  ExitCode StandardOutput 
-------------                                                  -------- -------------- 
WARNING: can't open config file: /usr/local/ssl/openssl.cnf...                        0
PS C:\Users\admin\Desktop\Certificate> Get-ChildItem
    Directory: C:\Users\admin\Desktop\Certificate
Mode   LastWriteTime     Length Name
                                   
----   -------------     ------ ----
                                   
-a----         24/6/2021   3:29 μμ           6022   certificate.pfx
-a----         29/6/2021   7:24 μμ           2072   certificate.pfx.chain.crt
-a----         29/6/2021   7:16 μμ           2264   certificate.pfx.crt
  
 
  PS C:\Certificate> Export-PrivateKeyFromPFX -PFXFilePath .\certificate.pfx -PFXPassphrase $pass -KeyPassphrase $key 
PS C:\Certificate> Get-ChildItem
    Directory: C:\Users\admin\Desktop\Certificate
Mode   LastWriteTime     Length Name
                                   
----   -------------     ------ ----
-a----         24/6/2021   3:29 μμ           6022   certificate.pfx
                                 
-a----         29/6/2021   7:24 μμ           2072   certificate.pfx.chain.crt
                                 
-a----         29/6/2021   7:16 μμ           2264   certificate.pfx.crt
-a----         29/6/2021   7:42 μμ           3604   certificate.pfx.key
 
  PS C:\Certificate> Decrypt-PrivateKey -PrivateKeyFilePath .\certificate.pfx.key -PrivateKeyPassphrase $key
PS C:\Certificate> Get-ChildItem
    Directory: C:\Certificate
Mode   LastWriteTime     Length Name
                                   
----   -------------     ------ ----
-a----         24/6/2021   3:29 μμ           6022   certificate.pfx
-a----         29/6/2021   7:24 μμ           2072   certificate.pfx.chain.crt
-a----         29/6/2021   7:16 μμ           2264   certificate.pfx.crt
-a----         29/6/2021   7:42 μμ           3604   certificate.pfx.key
-a----         29/6/2021   7:48 μμ           3247   certificate.pfx.key.plainkey
 
  PS C:\Certificate> Get-Content .\certificate.pfx.crt
Bag Attributes
    1.3.6.1.4.1.311.17.3.121: 00 
    localKeyID: 01 00 00 00 
    1.3.6.1.4.1.311.17.3.71: 43 00 61 00 74 00 65 00 6E 00 61 00 45 00 78 00 70 00 6F 
friendlyName: Test Certificate
subject=/CN=test.com
issuer=/CN=TestRootCA
-----BEGIN CERTIFICATE-----
MIIFbzCCA1egAwIBAgIQGS4atYgz0Z5MByyH2i6yLjANBgkqhkiG9w0BAQ0FADAa
MRgwFgYDVQQDDA9DYXRlbmFQb0NSb290Q0EwHhcNMjEwNjI0MTEzMzA3WhcNMjMw
NjI0MTEzMzA3WjAfMR0wGwYDVQQDDBRxdWlja3NwaW5jYXNpbm9zLmNvbTCCAiIw
DQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBANloKnIY0CuMjaB5VcOEh84fzt0P
HWc3l158qBIvcmueNj+amhXBhWdg7Ak7of7/fQYgXH3hJlAHeL7v2TqMx5JRPKNZ
-----END CERTIFICATE-----
| 1 | Install-Module -Name CPolydorou.Security -AllowClobber -Scope CurrentUser | 
I've also written a blog post on how to convert certificates in the past, that is available here.
 
