Skip to content

Instantly share code, notes, and snippets.

@valtoni
Created August 11, 2018 16:28
Show Gist options
  • Save valtoni/4246117a7219fde975d5f7e45ae4ee75 to your computer and use it in GitHub Desktop.
Save valtoni/4246117a7219fde975d5f7e45ae4ee75 to your computer and use it in GitHub Desktop.
Complete path to create a certification and PKCS12 format file to be used in tomcat or another server
#!/bin/bash
NAME_KEY=key
FILE=${NAME_KEY}
PRV_KEY_HEX=${FILE}.hex64.key
PRV_KEY=${FILE}.pem
CERT_REQUEST=${FILE}.csr
CERTIFICATE=${FILE}.crt
PKCS12_FILE=${FILE}.pkcs12
# Generate private key
openssl genrsa -out $PRV_KEY 2048
# Encode pem in base64
openssl base64 -in $PVT_KEY -out $PRV_KEY_HEX
# Generate certificate request
openssl req -new -sha256 -key ${PRV_KEY} -out ${CERT_REQUEST}
# Emit your certificate (Recomended issuer: https://www.gogetssl.com/comodo-ssl/comodo-positivessl/)
echo "Certificate request: "
cat ${CERT_REQUEST}
read -n1 -r -p "Emit your certificate and put at ${CERTIFICATE}..." key
# Create temporary certificate and key file
cat $PRV_KEY_HEX $CERTIFICATE > cert_and_key.txt
# Create a default key holder PKCS12
echo "(Input password is important for java keychain, but is not necessary for another use)
openssl pkcs12 -export -in cert_and_key.txt -out ${PKCS12_FILE} -name ${NAME_KEY} -noiter -nomaciter
# Destroy temp file
rm cert_and_key.txt
# (Informative) Decode base64 coded format
# openssl base64 -d -in $PVT_KEY_HEX -out $PRV_KEY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment