Skip to content

Instantly share code, notes, and snippets.

@kylelemons
Created November 17, 2011 02:55
Show Gist options
  • Save kylelemons/1372236 to your computer and use it in GitHub Desktop.
Save kylelemons/1372236 to your computer and use it in GitHub Desktop.
Generate a key/certificate
#!/bin/bash
# Usage: gencert.sh
# Generates SSL certificates
set -e
PREFIX="my_"
function oops() {
echo "Failed."
exit 1
}
trap oops ERR
CERTFILE="${PREFIX}cert.pem"
PRIVKEY="${PREFIX}private.pem"
KEYFILE="${PREFIX}key.pem"
BITS=4096
EXPIRE=$((365*10))
echo Generating the private key...
openssl genrsa -des3 -passout pass:password -out $PRIVKEY $BITS >/dev/null 2>&1
echo Decrypting the key...
openssl rsa -passin pass:password -in $PRIVKEY -out $KEYFILE >/dev/null 2>&1
rm $PRIVKEY
echo Generating the certificate...
openssl req -new -x509 -key $KEYFILE -out $CERTFILE -days $EXPIRE >/dev/null 2>&1 <<EOF
<country>
<state>
<city>
<company>
<division>
<hostname>
<email>
EOF
# Make sure the world can't read it
chmod o-rwx $CERTFILE $KEYFILE
echo "Generated: $CERTFILE | $KEYFILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment