|
| 1 | +#!/bin/bash |
| 2 | +# info: generate self signed certificate and CSR request |
| 3 | +# options: DOMAIN EMAIL COUNTRY STATE CITY ORG UNIT [FORMAT] |
| 4 | +# |
| 5 | +# The function generates self signed SSL certificate and CSR request |
| 6 | + |
| 7 | + |
| 8 | +#----------------------------------------------------------# |
| 9 | +# Variable&Function # |
| 10 | +#----------------------------------------------------------# |
| 11 | + |
| 12 | +# Argument defenition |
| 13 | +domain=$1 |
| 14 | +domain=$(echo $domain | sed -e 's/\.*$//g' -e 's/^\.*//g') |
| 15 | +domain=$(echo $domain | tr '[:upper:]' '[:lower:]') |
| 16 | +email=$2 |
| 17 | +country=$3 |
| 18 | +state=$4 |
| 19 | +city=$5 |
| 20 | +org=$6 |
| 21 | +org_unit=$7 |
| 22 | +format=${8-shell} |
| 23 | +KEY_SIZE=2048 |
| 24 | +DAYS=365 |
| 25 | + |
| 26 | +# Includes |
| 27 | +source $VESTA/func/main.sh |
| 28 | +source $VESTA/conf/vesta.conf |
| 29 | + |
| 30 | +# Json function |
| 31 | +json_list_ssl() { |
| 32 | + i='1' # iterator |
| 33 | + echo '{' |
| 34 | + echo -e "\t\"$domain\": {" |
| 35 | + echo " \"CRT\": \"$crt\"," |
| 36 | + echo " \"KEY\": \"$key\"," |
| 37 | + echo " \"CSR\": \"$csr\"" |
| 38 | + echo -e "\t}\n}" |
| 39 | +} |
| 40 | + |
| 41 | +# Shell function |
| 42 | +shell_list_ssl() { |
| 43 | + if [ ! -z "$crt" ]; then |
| 44 | + echo -e "$crt" |
| 45 | + fi |
| 46 | + if [ ! -z "$key" ]; then |
| 47 | + echo -e "\n$key" |
| 48 | + fi |
| 49 | + if [ ! -z "$csr" ]; then |
| 50 | + echo -e "\n$csr" |
| 51 | + fi |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +#----------------------------------------------------------# |
| 56 | +# Verifications # |
| 57 | +#----------------------------------------------------------# |
| 58 | + |
| 59 | +check_args '7' "$#" 'DOMAIN EMAIL COUNTRY STATE CITY ORG UNIT [FORMAT]' |
| 60 | +validate_format 'domain' 'email' 'format' |
| 61 | + |
| 62 | + |
| 63 | +#----------------------------------------------------------# |
| 64 | +# Action # |
| 65 | +#----------------------------------------------------------# |
| 66 | + |
| 67 | +# Create temporary work directory |
| 68 | +workdir=$(mktemp -d) |
| 69 | +cd $workdir |
| 70 | + |
| 71 | +# Generate private key |
| 72 | +export PASSPHRASE=gen_password |
| 73 | +openssl genrsa -des3 \ |
| 74 | + -out $domain.key \ |
| 75 | + -passout env:PASSPHRASE $KEY_SIZE 2>/dev/null |
| 76 | + |
| 77 | +# Generate the CSR |
| 78 | +subj="/C=$country/ST=$state/localityName=$city/O=$org" |
| 79 | +subj="$subj/organizationalUnitName=$org_unit/commonName=$domain" |
| 80 | +subj="$subj/emailAddress=$email" |
| 81 | + |
| 82 | +openssl req \ |
| 83 | + -new \ |
| 84 | + -batch \ |
| 85 | + -subj "$subj" \ |
| 86 | + -key $domain.key \ |
| 87 | + -out $domain.csr \ |
| 88 | + -passin env:PASSPHRASE >/dev/null 2>&1 |
| 89 | + |
| 90 | +# Remove passphrase |
| 91 | +cp $domain.key $domain.key.tmp |
| 92 | +openssl rsa \ |
| 93 | + -in $domain.key.tmp \ |
| 94 | + -out $domain.key \ |
| 95 | + -passin env:PASSPHRASE >/dev/null 2>&1 |
| 96 | +rm $domain.key.tmp |
| 97 | + |
| 98 | +# Generate the cert 1 year |
| 99 | +openssl x509 -req \ |
| 100 | + -days $DAYS \ |
| 101 | + -in $domain.csr \ |
| 102 | + -signkey $domain.key \ |
| 103 | + -out $domain.crt >/dev/null 2>&1 |
| 104 | + |
| 105 | +# Listing certificates |
| 106 | +if [ -e "$domain.crt" ]; then |
| 107 | + crt=$(cat $domain.crt | sed ':a;N;$!ba;s/\n/\\n/g' ) |
| 108 | +fi |
| 109 | + |
| 110 | +if [ -e "$domain.key" ]; then |
| 111 | + key=$(cat $domain.key | sed ':a;N;$!ba;s/\n/\\n/g' ) |
| 112 | +fi |
| 113 | + |
| 114 | +if [ -e "$domain.csr" ]; then |
| 115 | + csr=$(cat $domain.csr | sed ':a;N;$!ba;s/\n/\\n/g' ) |
| 116 | +fi |
| 117 | + |
| 118 | +case $format in |
| 119 | + json) json_list_ssl ;; |
| 120 | + plain) nohead=1; shell_list_ssl ;; |
| 121 | + shell) shell_list_ssl ;; |
| 122 | + *) check_args '1' '0' '[FORMAT]' |
| 123 | +esac |
| 124 | + |
| 125 | +# Delete tmp dir |
| 126 | +rm -rf $workdir |
| 127 | + |
| 128 | + |
| 129 | +#----------------------------------------------------------# |
| 130 | +# Vesta # |
| 131 | +#----------------------------------------------------------# |
| 132 | + |
| 133 | +# Logging |
| 134 | +log_event "$OK" "$EVENT" |
| 135 | + |
| 136 | +exit |
0 commit comments