Skip to content

Instantly share code, notes, and snippets.

@jonasbjork
Last active September 15, 2017 12:13
Show Gist options
  • Select an option

  • Save jonasbjork/d34dc29ab5bfea293323f493210087af to your computer and use it in GitHub Desktop.

Select an option

Save jonasbjork/d34dc29ab5bfea293323f493210087af to your computer and use it in GitHub Desktop.
Bash script for converting pfx certificates (from Windows) to key / pem (to be used in Linux)
#!/usr/bin/env bash
#
# pfx2pem
# Jonas Björk <jonas.bjork@gmail.com>
# 2017-09-15, Helsingborg
#
set -e
if [ $# -lt 1 ]; then
echo ""
echo "This script will convert a pfx certificate to key / pem"
echo ""
echo "Syntax:"
echo " ${0} file.pfx"
exit 1
fi
if [ ! -f $1 ] ; then
echo "Error: Could not find certificate file $1"
exit 1
fi
if [ -z $(whereis openssl | awk '{print $2}') ] ; then
echo "Error: Could not find openssl in path"
exit 1
fi
INCERT=${1}
echo "Extracting key to ${INCERT:0:-4}.key"
if [ -f ${INCERT:0:-4}.key ] ; then
echo "Error: ${INCERT:0:-4}.key already exists."
exit 1
fi
openssl pkcs12 -in ${INCERT} -nocerts -out ${INCERT:0:-4}.tmp
openssl rsa -in ${INCERT:0:-4}.tmp -out ${INCERT:0:-4}.key
rm ${INCERT:0:-4}.tmp
echo "Extracting certificate to ${INCERT:0:-4}.pem"
if [ -f ${INCERT:0:-4}.pem ] ; then
echo "Error: ${INCERT:0:-4}.pem already exists."
exit 1
fi
openssl pkcs12 -in ${INCERT} -clcerts -nokeys -out ${INCERT:0:-4}.pem
echo "Done. Have a nice day."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment