#!/bin/bash # # make-csr is a shell script designed to automatically generate a # certificate signing request (CSR) from an Apache or a Nginx vhost # # Author: Victor Laborie # Licence: AGPLv3 # set -u show_version() { cat <, Victor Laborie , Jérémy Lecour , Benoit Série and others. make-csr comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the GNU Affero General Public License v3.0 for details. END } show_help() { cat <&2 echo "${PROGNAME}: $1" fi } error() { >&2 echo "${PROGNAME}: $1" [ "$1" = "invalid argument(s)" ] && >&2 show_help exit 1 } default_key_size() { grep default_bits "${SSL_CONFIG_FILE}" | cut -d'=' -f2 | xargs } sed_selfsigned_cert_path_for_apache() { local apache_ssl_vhost_path="$1" mkdir -p $(dirname "${apache_ssl_vhost_path}") if [ ! -f "${apache_ssl_vhost_path}" ]; then cat > "${apache_ssl_vhost_path}" < "${nginx_ssl_vhost_path}" < /dev/null else "${OPENSSL_BIN}" x509 -req -sha256 -days 365 -in "${csr}" -signkey "${key}" -out "${crt}" 2> /dev/null fi [ -r "${crt}" ] || error "Something went wrong, ${crt} has not been generated" } openssl_key(){ local key="$1" local key_dir=$(dirname "${key}") local size="$2" [ -w "${key_dir}" ] || error "Directory ${key_dir} is not writable" "${OPENSSL_BIN}" genrsa -out "${key}" "${size}" 2> /dev/null [ -r "${key}" ] || error "Something went wrong, ${key} has not been generated" } openssl_csr() { local csr="$1" local csr_dir=$(dirname "${csr}") local key="$2" local cfg="$3" [ -w "${csr_dir}" ] || error "Directory ${csr_dir} is not writable" if $(grep -q "DNS:" "${cfg}"); then # CSR with SAN "${OPENSSL_BIN}" req -new -sha256 -key "${key}" -reqexts SAN -config "${cfg}" -out "${csr}" else # Single domain CSR "${OPENSSL_BIN}" req -new -sha256 -key "${key}" -config "${cfg}" -out "${csr}" fi [ -r "${csr}" ] || error "Something went wrong, ${csr} has not been generated" } make_key() { local key="$1" local size="$2" openssl_key "${key}" "${size}" debug "Private key stored at ${key}" chown root: "${key}" chmod 600 "${key}" } make_csr() { local domains=$@ local nb=$# local config_file="/tmp/make-csr-${VHOST}.conf" local san="" mkdir -p -m 0755 "${CSR_DIR}" || error "Unable to mkdir ${CSR_DIR}" if [ "${nb}" -eq 1 ]; then cat "${SSL_CONFIG_FILE}" - > "${config_file}" < "${config_file}" </dev/null && sed_selfsigned_cert_path_for_apache "/etc/apache2/ssl/${VHOST}.conf" command -v nginx >/dev/null && sed_selfsigned_cert_path_for_nginx "/etc/nginx/ssl/${VHOST}.conf" exit 0 } readonly PROGNAME=$(basename "$0") readonly PROGDIR=$(realpath -m $(dirname "$0")) readonly ARGS=$@ readonly VERBOSE=${VERBOSE:-"0"} readonly QUIET=${QUIET:-"0"} readonly VERSION="20.12" # Read configuration file, if it exists [ -r /etc/default/evoacme ] && . /etc/default/evoacme # Default value for main variables readonly CSR_DIR=${CSR_DIR:-'/etc/ssl/requests'} readonly SSL_CONFIG_FILE=${SSL_CONFIG_FILE:-"/etc/letsencrypt/openssl.cnf"} readonly SELF_SIGNED_DIR=${SELF_SIGNED_DIR:-'/etc/ssl/self-signed'} readonly SSL_KEY_DIR=${SSL_KEY_DIR:-'/etc/ssl/private'} readonly SSL_KEY_SIZE=${SSL_KEY_SIZE:-$(default_key_size)} main ${ARGS}