Skip to content

Instantly share code, notes, and snippets.

@t0chas
Created July 5, 2017 20:22
Show Gist options
  • Save t0chas/0f3c6ccf04df0799bee15a57ec718d90 to your computer and use it in GitHub Desktop.
Save t0chas/0f3c6ccf04df0799bee15a57ec718d90 to your computer and use it in GitHub Desktop.
Export APNS Certificate as PEM
#!/bin/sh
CERTIFICATE_FROM_APPLE="$1"
CERTIFICATE_KEY="$2"
PASSWORD="$3"
if ! [ -e "$1" ] ; then
echo "CERTIFICATE_FROM_APPLE is missing."
echo "usage:\n\t $0 CERTIFICATE_FROM_APPLE.cer CERTIFICATE_KEY.p12 PASSWORD"
exit 1;
fi
if ! [ -e "$2" ] ; then
echo "CERTIFICATE_KEY is missing."
echo "usage:\n\t $0 CERTIFICATE_FROM_APPLE.cer CERTIFICATE_KEY.p12 PASSWORD"
exit 1;
fi
if ! [ -n "$3" ] ; then
echo "PASSWORD is missing."
echo "usage:\n\t $0 CERTIFICATE_FROM_APPLE.cer CERTIFICATE_KEY.p12 PASSWORD"
exit 1;
fi
echo "Exporting certificate: '$CERTIFICATE_FROM_APPLE' with key: '$CERTIFICATE_KEY'"
CERTIFICATE_FROM_APPLE_NAME="${CERTIFICATE_FROM_APPLE%%.*}"
CERTIFICATE_KEY_NAME="${CERTIFICATE_KEY%%.*}"
PEM_CERTIFICATE="${CERTIFICATE_FROM_APPLE_NAME}_cert.pem"
PEM_CERTIFICATE_KEY="${CERTIFICATE_KEY_NAME}_key.pem"
PEM_CERTIFICATE_WITH_KEY="${CERTIFICATE_FROM_APPLE_NAME}_certWithKey.pem"
echo "PEM Certificate: '$PEM_CERTIFICATE'"
if ! openssl x509 -in $CERTIFICATE_FROM_APPLE -inform der -out $PEM_CERTIFICATE ; then
rm $PEM_CERTIFICATE
exit 1;
fi
echo "PEM Certificate key: '$PEM_CERTIFICATE_KEY'"
if ! openssl pkcs12 -nocerts -in $CERTIFICATE_KEY -out $PEM_CERTIFICATE_KEY -passin pass:$PASSWORD -passout pass:$PASSWORD ; then
rm $PEM_CERTIFICATE_KEY
exit 1;
fi
echo "Merge PEM files: '$PEM_CERTIFICATE_WITH_KEY'"
cat $PEM_CERTIFICATE $PEM_CERTIFICATE_KEY > $PEM_CERTIFICATE_WITH_KEY
#rm $PEM_CERTIFICATE
#rm $PEM_CERTIFICATE_KEY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment