Created
June 25, 2015 15:47
-
-
Save shoaibi/a99f0ea6cdc52a97a6e0 to your computer and use it in GitHub Desktop.
Generate ssl key, csr or self signed crt with one command
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 | |
| # | |
| # Just a clumsy little cert helper I use oftenly | |
| # | |
| # | |
| E_BADARGS=65 | |
| if [ $# -lt 3 -o $# -gt 11 ]; then | |
| echo "Usage: $0 mode[=key|csr|crt] name keyLength[=4096] commonName organizationUnit organization locality state countryTwoLetterCode emailAddress days[=1900]" | |
| exit $E_BADARGS | |
| fi | |
| mode="${1}" | |
| name="${2}" | |
| keyLength="${3}" | |
| commonName="${4}" | |
| organizationUnit="${5}" | |
| organization="${6}" | |
| locality="${7}" | |
| state="${8}" | |
| countryTwoLetterCode="${9}" | |
| emailAddress="${10}" | |
| days="${11}" | |
| if [ "x$mode" = "x" ]; then | |
| mode="key"; | |
| fi | |
| if [ "x$keyLength" = "x" ]; then | |
| keyLength=4096 | |
| fi | |
| echo | |
| echo "Running in $mode mode" | |
| echo "Generating key"; | |
| openssl genrsa -des3 -out $name.key $keyLength | |
| echo "Generating passwordless variant of key" | |
| \cp -v $name.{key,key.original} | |
| openssl rsa -in $name.key.original -out $name.key | |
| echo | |
| if [ "$mode" = "csr" -o "$mode" = "crt" ]; then | |
| if [ "x$countryTwoLetterCode" = "x" -o "x$state" = "x" -o "x$locality" = "x" -o "x$organization" = "x" -o "x$organizationUnit" = "x" -o "x$commonName" = "x" -o "x$emailAddress" = "x" ]; then | |
| echo "CSR or CRT mode requires countryTwoLetterCode, state, locality, organization, organizationUnit, commonName and emailAddress to be set."; | |
| \rm -v $name.key* | |
| exit $E_BADARGS | |
| fi | |
| echo "Generating CSR" | |
| subject="/C=$countryTwoLetterCode/ST=$state/L=$locality/O=$organization/OU=$organizationUnit/CN=$commonName/emailAddress=$emailAddress" | |
| openssl req -new -key $name.key -out $name.csr -subj "$subject" | |
| if [ "$mode" = "crt" ]; then | |
| if [ "x$days" = "x" ]; then | |
| days=1900 | |
| fi | |
| echo "Generating CRT" | |
| openssl x509 -req -days $days -in $name.csr -signkey $name.key -out $name.crt | |
| fi | |
| fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment