Skip to main content

Misc Scripts

Misc Scripts

Certificate Extraction Scripts

The below scripts are used to extract a certificate and private key from a password protected pfx file. Expect to be prompted for the password.

extract-cert.sh

#! /bin/bash

## Extracts certificate from an exported pfx file
## Use pfx file as the first argument when calling the script
## If the pfx file is password protected, you will be prompted to enter the password
## Example: extract-cert.sh your-pfx-file.pfx

openssl pkcs12 -in $1 -nokeys -out $(basename $1 .pfx)-cert.pem

extract-key.sh

#! /bin/bash

## Extracts private key from an exported pfx file
## Use pfx file as the first argument when calling the script
## If the pfx file is password protected, you will be prompted to enter the password
## Example: extract-key.sh your-pfx-file.pfx

openssl pkcs12 -in $1 -nocerts -out $(basename $1 .pfx)-key.pem -nodes

extract-from-pfx.sh

If you want to run both commands at once, you can do that too.

#! /bin/bash

## Extracts certificate and key from an exported pfx file
## Use pfx file as the first argument when calling the script
## If the pfx file is password protected, you will be prompted to enter the password (once for the cert and again for the key)
## Example: extract-from-pfx.sh your-pfx-file.pfx

openssl pkcs12 -in $1 -nokeys -out $(basename $1 .pfx)-cert.pem
openssl pkcs12 -in $1 -nocerts -out $(basename $1 .pfx)-key.pem -nodes