Skip to content

Instantly share code, notes, and snippets.

@zach-klippenstein
Created March 2, 2011 23:55
Show Gist options
  • Save zach-klippenstein/852038 to your computer and use it in GitHub Desktop.
Save zach-klippenstein/852038 to your computer and use it in GitHub Desktop.
Script for quickly generating self-signed, passphrase-less SSL keys/certificates for web servers.
#!/bin/bash
BITS=1024
DAYS=365
function promptForName()
{
echo -n "Enter the SSL certificate/key name: " >&2
read name
echo $name
}
function genPrivKey()
{
privKeyFile="$1"; shift
privKeyTempFile="$privKeyFile.org"
tempPassphrase="$(strings </dev/urandom | head -n 1)"
echo -e "Generating private key: $privKeyFile..."
echo -e "\\tusing passphrase '$tempPassphrase'"
openssl genrsa -des3 -passout "pass:$tempPassphrase" -out "$privKeyTempFile" $BITS
openssl rsa -passin "pass:$tempPassphrase" -in "$privKeyTempFile" -passout "pass:$tempPassphrase" -out "$privKeyFile"
rm -f "$privKeyTempFile"
}
function genCSR()
{
csrFile="$1"; shift
privKeyFile="$1"; shift
echo -e "Creating Certificate Signing Request: $csrFile..."
openssl req -new -key "$privKeyFile" -out "$csrFile"
}
function genCert()
{
certFile="$1"; shift
privKeyFile="$1"; shift
csrFile="$1"; shift
echo -e "Signing the private key to create certificate: $certFile..."
openssl x509 -req -days $DAYS -in "$csrFile" -signkey "$privKeyFile" -out "$certFile"
}
function clean()
{
echo "Removing '$@'..." >&2
rm -f "$@"
}
name="$(promptForName)"
privKeyFile="$name.key"
csrFile="$name.csr"
certFile="$name.crt"
echo "Creating SSL files with name '$name'..."
(
genPrivKey "$privKeyFile" &&
genCSR "$csrFile" "$privKeyFile" &&
genCert "$certFile" "$privKeyFile" "$csrFile"
) ||
(
echo "Error generating SSL files, cleaning..."
clean "$privKeyFile" "$csrFile" "$certFile"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment