Created
June 30, 2022 20:04
-
-
Save colehocking/e2b04b8f460b790262f99b1f7db46f5c to your computer and use it in GitHub Desktop.
Creates PFX and generates PFX password
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Create PFX File given private and public key | |
# usage: ./create_pfx.sh <private_key> <public_key> | |
# ARG Input | |
PRIV_KEY="$1" | |
#echo "${PRIV_KEY}" | |
PUB_KEY="$2" | |
#echo "${PUB_KEY}" | |
#------------------------ | |
# Generate PFX File Password from 'a-zA-Z0-9!@#$%^&' chars and of length PSSWD_LGTH | |
generate_PFX_pass(){ | |
PSSWD_LGTH=12 | |
PFX_PASS="$(head -c 500 /dev/urandom | LC_ALL=C tr -dc 'a-zA-Z0-9!#$%^&'| fold -w ${PSSWD_LGTH} | head -n 1 )" | |
echo -e "PFX PASSWORD: \n ${PFX_PASS} \n" | |
} | |
#------------------------ | |
# Create PFX using openSSL | |
create_PFX(){ | |
openssl pkcs12 -export -out "$(echo "${PUB_KEY}" | rev | cut -d "." -f2- | rev ).pfx" -inkey "${PRIV_KEY}" -in "${PUB_KEY}" | |
} | |
#------------------------ | |
main(){ | |
#private key | |
if [[ -f "${PRIV_KEY}" ]]; then | |
case $PRIV_KEY in | |
*.key) PRIVKEY_FMT="key";; | |
*.pem) PRIVKEY_FMT="pem";; | |
*) echo "valid private key extensions: <file.key>, <file.pem>"; exit 1;; | |
esac | |
else | |
echo "create_pfx.sh: private key file not found." | |
exit 1 | |
fi | |
#public key | |
if [[ -f "${PUB_KEY}" ]]; then | |
case $PUB_KEY in | |
*.pem) | |
if [[ "${PRIVKEY_FMT}" == "pem" ]]; then | |
echo "pub/priv key must have different extensions!" | |
exit 1 | |
fi ;; | |
*.crt) ;; | |
*) echo " valid public key extensions: <file.pem> <file.crt> " | |
esac | |
else | |
echo "create_pfx.sh: public key file not found" | |
fi | |
#echo "${PRIVKEY_FMT}" | |
generate_PFX_pass | |
create_PFX | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment