Last active
September 12, 2019 03:19
-
-
Save Stewie410/eb6d99f35709728458c67c073331a6d2 to your computer and use it in GitHub Desktop.
An overly complicated response to u/RANDY_MAYONNSAISE
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/env bash | |
| # | |
| # mkCert.sh | |
| # Author: u/stewie410 | |
| # Date: 2019-09-11 | |
| # | |
| # Generates SSL Certificates auto-magically | |
| # Based on u/RANDY_MAYONNAISE's post: | |
| # https://www.reddit.com/r/bash/comments/d2y10e/working_with_arguments_in_bash/ | |
| # ##--------------------## | |
| # #| Traps |# | |
| # ##--------------------## | |
| trap usv EXIT # Ensure usv() is called on exit signal | |
| # ##------------------------## | |
| # #| Functions |# | |
| # ##------------------------## | |
| # Clean up | |
| usv() { | |
| unset _path_dir_root _path_dir_logs _path_dir_key _path_dir_req _path_dir_crt _path_dir_pfx | |
| unset _path_file_log _path_file_key _path_file_req _path_file_crt _path_file_pfx | |
| unset _ossl_arg_domain _ossl_arg_password _ossl_arg_length _ossl_arg_type _ossl_arg_days _ossl_arg_country _ossl_arg_state _ossl_arg_city _ossl_arg_company _ossl_arg_org _ossl_arg_cname _ossl_arg_email _ossl_arg_mscsp | |
| unset _opt_force _opt_key _opt_req _opt_pfx | |
| unset OPTS | |
| } | |
| # Check options | |
| checkOpts() { | |
| # Check for null variables | |
| if [-z "${_path_dir_root}" ]; then printErr "null" "_path_dir_root"; return 1; fi | |
| if [-z "${_path_dir_logs}" ]; then printErr "null" "_path_dir_logs"; return 1; fi | |
| if [-z "${_path_dir_key}" ]; then printErr "null" "_path_dir_key"; return 1; fi | |
| if [-z "${_path_dir_req}" ]; then printErr "null" "_path_dir_req"; return 1; fi | |
| if [-z "${_path_dir_crt}" ]; then printErr "null" "_path_dir_crt"; return 1; fi | |
| if [-z "${_path_dir_pfx}" ]; then printErr "null" "_path_dir_pfx"; return 1; fi | |
| if [-z "${_path_file_log}" ]; then printErr "null" "_path_file_log"; return 1; fi | |
| if [-z "${_path_file_key}" ]; then printErr "null" "_path_file_key"; return 1; fi | |
| if [-z "${_path_file_req}" ]; then printErr "null" "_path_file_req"; return 1; fi | |
| if [-z "${_path_file_crt}" ]; then printErr "null" "_path_file_crt"; return 1; fi | |
| if [-z "${_path_file_pfx}" ]; then printErr "null" "_path_file_pfx"; return 1; fi | |
| if [-z "${_ossl_arg_domain}" ]; then printErr "null" "_ossl_arg_domain"; return 1; fi | |
| if [-z "${_ossl_arg_password}" ]; then printErr "null" "_ossl_arg_password"; return 1; fi | |
| if [-z "${_ossl_arg_length}" ]; then printErr "null" "_ossl_arg_length"; return 1; fi | |
| if [-z "${_ossl_arg_type}" ]; then printErr "null" "_ossl_arg_type"; return 1; fi | |
| if [-z "${_ossl_arg_days}" ]; then printErr "null" "_ossl_arg_days"; return 1; fi | |
| if [-z "${_ossl_arg_country}" ]; then printErr "null" "_ossl_arg_country"; return 1; fi | |
| if [-z "${_ossl_arg_state}" ]; then printErr "null" "_ossl_arg_state"; return 1; fi | |
| if [-z "${_ossl_arg_city}" ]; then printErr "null" "_ossl_arg_city"; return 1; fi | |
| if [-z "${_ossl_arg_company}" ]; then printErr "null" "_ossl_arg_company"; return 1; fi | |
| if [-z "${_ossl_arg_org}" ]; then printErr "null" "_ossl_arg_org"; return 1; fi | |
| if [-z "${_ossl_arg_cname}" ]; then printErr "null" "_ossl_arg_cname"; return 1; fi | |
| if [-z "${_ossl_arg_email}" ]; then printErr "null" "_ossl_arg_email"; return 1; fi | |
| if [-z "${_ossl_arg_mscsp}" ]; then printErr "null" "_ossl_arg_mscsp"; return 1; fi | |
| if [-z "${_opt_force}" ]; then printErr "null" "_opt_force"; return 1; fi | |
| if [-z "${_opt_key}" ]; then printErr "null" "_opt_key"; return 1; fi | |
| if [-z "${_opt_req}" ]; then printErr "null" "_opt_req"; return 1; fi | |
| if [-z "${_opt_pfx}" ]; then printErr "null" "_opt_pfx"; return 1; fi | |
| # Normalize Options | |
| if ! [ "${_opt_force}" -ge 0 -a "${_opt_force}" -le 1 ]; then printErr "val" "_opt_force"; _opt_force="0"; fi | |
| if ! [ "${_opt_key}" -ge 0 -a "${_opt_key}" -le 1 ]; then printErr "val" "_opt_key"; _opt_key="0"; fi | |
| if ! [ "${_opt_req}" -ge 0 -a "${_opt_req}" -le 1 ]; then printErr "val" "_opt_req"; _opt_req="0"; fi | |
| if ! [ "${_opt_pfx}" -ge 0 -a "${_opt_pfx}" -le 1 ]; then printErr "val" "_opt_pfx"; _opt_pfx="0"; fi | |
| # Check OpenSSL Options (that are relevant to check) | |
| if ! echo "${_ossl_arg_length}" | grep -qiE "^[0-9]+$"; then printErr "ossl" "Length contains non-numeric values:\t${_ossl_arg_length}"; return 1; fi | |
| if ! echo "${_ossl_arg_days}" | grep -qiE "^[0-9]+$"; then printErr "ossl" "Days contains non-numeric values:\t${_ossl_arg_days}"; return 1; fi | |
| if [ "${#_ossl_arg_country}" -ne 2 ]; then printErr "ossl" "Country Code must be exactly two characters:\t${_ossl_arg_country}"; return 1; fi | |
| if ! echo "${_ossl_arg_email}" | grep -qiE "^[a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{3}"; then printErr "ossl" "Email is not valid format:\t${_ossl_arg_email}"; return 1; fi | |
| # Check for existence of directories | |
| if [ -d "${_path_dir_root}" ]; then | |
| if [ "${_opt_force}" -eq 1 ]; then mkdir -p "${_path_dir_root}"; else | |
| read -p "Make Directory '${_path_dir_roo}'? (y/N): " mdc | |
| if [ -n "${mdc}" ] && [[ "${mdc,,}" == "y" ]]; then mkdir -p "${_path_dir_root}"; else | |
| printErr "dir" "${_path_dir_root}" | |
| unset mdc | |
| return 1 | |
| fi | |
| unset mdc | |
| fi | |
| fi | |
| if [ -d "${_path_dir_logs}" ] && [[ "${_path_file_log,,}" != "/dev/null" ]]; then | |
| if [ "${_opt_force}" -eq 1 ]; then mkdir -p "${_path_dir_logs}"; else | |
| read -p "Make Directory '${_path_dir_roo}'? (y/N): " mdc | |
| if [ -n "${mdc}" ] && [[ "${mdc,,}" == "y" ]]; then mkdir -p "${_path_dir_logs}"; else | |
| printErr "dir" "${_path_dir_logs}" | |
| unset mdc | |
| return 1 | |
| fi | |
| unset mdc | |
| fi | |
| fi | |
| if [ -d "${_path_dir_key}" ]; then | |
| if [ "${_opt_force}" -eq 1 ]; then mkdir -p "${_path_dir_key}"; else | |
| read -p "Make Directory '${_path_dir_key}'? (y/N): " mdc | |
| if [ -n "${mdc}" ] && [[ "${mdc,,}" == "y" ]]; then mkdir -p "${_path_dir_key}"; else | |
| printErr "dir" "${_path_dir_key}" | |
| unset mdc | |
| return 1 | |
| fi | |
| unset mdc | |
| fi | |
| fi | |
| if [ -d "${_path_dir_req}" ]; then | |
| if [ "${_opt_force}" -eq 1 ]; then mkdir -p "${_path_dir_req}"; else | |
| read -p "Make Directory '${_path_dir_req}'? (y/N): " mdc | |
| if [ -n "${mdc}" ] && [[ "${mdc,,}" == "y" ]]; then mkdir -p "${_path_dir_req}"; else | |
| printErr "dir" "${_path_dir_req}" | |
| unset mdc | |
| return 1 | |
| fi | |
| unset mdc | |
| fi | |
| fi | |
| if [ -d "${_path_dir_crt}" ]; then | |
| if [ "${_opt_force}" -eq 1 ]; then mkdir -p "${_path_dir_crt}"; else | |
| read -p "Make Directory '${_path_dir_crt}'? (y/N): " mdc | |
| if [ -n "${mdc}" ] && [[ "${mdc,,}" == "y" ]]; then mkdir -p "${_path_dir_crt}"; else | |
| printErr "dir" "${_path_dir_crt}" | |
| unset mdc | |
| return 1 | |
| fi | |
| unset mdc | |
| fi | |
| fi | |
| if [ -d "${_path_dir_pfx}" ]; then | |
| if [ "${_opt_force}" -eq 1 ]; then mkdir -p "${_path_dir_pfx}"; else | |
| read -p "Make Directory '${_path_dir_pfx}'? (y/N): " mdc | |
| if [ -n "${mdc}" ] && [[ "${mdc,,}" == "y" ]]; then mkdir -p "${_path_dir_pfx}"; else | |
| printErr "dir" "${_path_dir_pfx}" | |
| unset mdc | |
| return 1 | |
| fi | |
| unset mdc | |
| fi | |
| fi | |
| # Check for existence of files | |
| if [ ! -f "${_path_file_log}" ] && [[ "${_path_file_log,,}" != "/dev/null" ]]; then | |
| if [ "${_opt_force}" -eq 1 ]; then touch "${_path_file_log}"; else | |
| read -p "Create File '${_path_file_log}'? (y/N): " mfc | |
| if [ -n "${mfc}" ] && [[ "${mfc,,}" == "y" ]]; then touch "${_path_file_log}"; else | |
| printErr "file" "${_path_file_log}" | |
| unset mfc | |
| return 1 | |
| fi | |
| unset mfc | |
| fi | |
| fi | |
| if [ "${_opt_key}" -eq 0 ]; then | |
| if [ "${_opt_req}" -eq 1 ] || [ "${_opt_pfx}" -eq 1 ]; then | |
| if [ ! -f "${_path_file_key}" ]; then printErr "file" "${_path_file_key}"; return 1; fi | |
| fi | |
| else | |
| if [ -f "${_path_file_key}" ]; then | |
| if [ "${_opt_force}" -eq 1 ]; then rm -f "${_path_file_key}"; else | |
| read -p "Overwrite File '${_path_file_key}'? (y/N): " ofc | |
| if [ -n "${ofc}" ] && [[ "${ofc,,}" ==" y" ]]; then rm -f "${_path_file_key}"; else | |
| printErr "file" "${_path_file_key}" | |
| unset ofc | |
| return 1 | |
| fi | |
| unset ofc | |
| fi | |
| fi | |
| fi | |
| if [ "${_opt_req}" -eq 1 ] && [ -f "${_path_file_req}" ]; then | |
| if [ "${_opt_force}" -eq 1 ]; then rm -f "${_path_file_req}"; else | |
| read -p "Overwrite File '${_path_file_req}'? (y/N): " ofc | |
| if [ -n "${ofc}" ] && [[ "${ofc,,}" ==" y" ]]; then rm -f "${_path_file_req}"; else | |
| printErr "file" "${_path_file_req}" | |
| unset ofc | |
| return 1 | |
| fi | |
| unset ofc | |
| fi | |
| fi | |
| if [ "${_opt_pfx}" -eq 1 ]; then | |
| if [ ! -f "${_path_file_crt}" -eq 1 ]; then printErr "file" "${_path_file_crt}"; return 1; fi | |
| if [ -f "${_path_file_pfx}" -eq 1 ]; then | |
| if [ "${_opt_force}" -eq 1 ]; then rm -f "${_path_file_pfx}"; else | |
| read -p "Overwrite File '${_path_file_pfx}'? (y/N): " ofc | |
| if [ -n "${ofc}" ] && [[ "${ofc,,}" ==" y" ]]; then rm -f "${_path_file_pfx}"; else | |
| printErr "file" "${_path_file_pfx}" | |
| unset ofc | |
| return 1 | |
| fi | |
| unset ofc | |
| fi | |
| fi | |
| fi | |
| # Return Success | |
| return 0 | |
| } | |
| # Print Errors -- Args: $!: Type; $2: Message Part | |
| printErr() { | |
| if [ -z "${2}" ]; then return 1; fi # Break function if both args are not passed | |
| local _msg="ERROR:\t" # Message Prefix | |
| case "${1,,}" in | |
| null ) # Null Variable | |
| if echo "${2}" | grep -qiE "domain|password"; then # If $2 is a *REQUIRED* Argument | |
| _msg+="A required argument was not defined:" # Add notification that a required arg is missing | |
| else # Otherwise | |
| _msg+="Variable is undefined:" # Add notification for a standard undefined var | |
| fi # Finally | |
| _msg+="\t${2}" # Add $2 to message | |
| ;; | |
| val ) _msg+="Invalid Option Value:\t${2}\n\t\tResetting to Default";; # Invalid Option Value x<0||x>1 | |
| ossl ) _msg+="OpenSSL Argument Error:\t${2}";; # OpenSSL Error | |
| dir ) _msg+="Directory cannot be found or created:\t${2}";; # Directory error | |
| file ) _msg+="File cannot be found, created or overwritten:\t${2}";; # File Error | |
| * ) _msg+="An unexpected error occurred:\t${2}";; # All other errors | |
| esac | |
| printf '%b\n' "${_msg}" # Print Message | |
| unset _msg # Clear Memory | |
| } | |
| # Show Help | |
| show_help() { | |
| printf '%b\n' "mkCert.sh [-k|-r|-x] [-d|--domain] DOMAIN [-p|--password] PASSWORD\n" "Generate SSL Certificates automagically\n" "Options:" | |
| printf '\t%s\t\t\t%s\n' "-h, --help" "Show this help message" | |
| printf '\t%s\t\t\t%s\n' "-f, --force" "Skip confirmation checks (WARNING: Possibility for data loss! Proceed with Caution!)" | |
| printf '\t%s\t\t\t%s\n' "-l, --log" "Enable logging to file" | |
| printf '\t%s\t\t\t%s\n' "-k, --gen-key" "Generate Keyfile" | |
| printf '\t%s\t\t\t%s\n' "-r, --gen-req" "Generate Certificate Request" | |
| printf '\t%s\t\t\t%s\n' "-x, --gen-pfx" "Generate PFX" | |
| printf '\t%s\t<str>\t\t%s\n' "-d, --domain" "Specify the domain to associate with the generated files (REQUIRED FOR OPERATION)" | |
| printf '\t%s\t<str>\t\t%s\n' "-p, --password" "Specify the Password/Passphrase to use for the encryption on generated files (REQUIRED FOR OPERATION)" | |
| printf '\t%s\t<int>\t\t%s\n' "--length" "Specify the length of the key pair in bits (eg: 2048)" | |
| printf '\t%s\t<str>\t\t%s\n' "--type" "Specify the type/algoritm to use in generating the key pair" | |
| printf '\t%s\t<int>\t\t%s\n' "--days" "Specify the number of days for the requested certificate to be valid" | |
| printf '\t%s\t<str>\t\t%s\n' "--country" "Specify the country code for the certificate request (eg: US, UK, etc)" | |
| printf '\t%s\t<str>\t\t%s\n' "--state" "Specifcy the state/locale for the certificate request" | |
| printf '\t%s\t<str>\t\t%s\n' "--company" "Specify the Company/Organization requesting the certificate" | |
| printf '\t%s\t<str>\t\t%s\n' "--ou" "Specify the Oganizational Unit (Department) in which the request is being generated for" | |
| printf '\t%s\t<str>\t\t%s\n' "--cname" "Specify the Common Name of the Domain requesting the certificate" | |
| printf '\t%s\t<str>\t\t%s\n' "--email" "Specify the primary email address for the certificate request" | |
| printf '\t%s\t<str>\t\t%s\n' "--mscsp" "Specify the Microsoft CSP for the PFX" | |
| printf '\t%s\t<path>\t\t%s\n' "--dir-root" "Absolute path to the root of the storage/output directories for the script" | |
| printf '\t%s\t<path>\t\t%s\n' "--dir-key" "Path to the location of all key files (relative to --dir-root)" | |
| printf '\t%s\t<path>\t\t%s\n' "--dir-req" "Path to the location of all certificate requests (realtive to --dir-root)" | |
| printf '\t%s\t<path>\t\t%s\n' "--dir-crt" "path to the location of all certificates (relative to --dir-root)" | |
| printf '\t%s\t<path>\t\t%s\n' "--dir-pfx" "Path to the location of all PFX files (relative to --dir-pfx" | |
| printf '\t%s\t<path>\t\t%s\n' "--file-key" "Path to the keyfile for this run (relative to --dir-key)" | |
| printf '\t%s\t<path>\t\t%s\n' "--file-req" "Path to the certificate request file for this run (relative to --dir-req)" | |
| printf '\t%s\t<path>\t\t%s\n' "--file-crt" "Path to the certificate file for this run (relative to --dir-crt)" | |
| printf '\t%s\t<path>\t\t%s\n' "--file-pfx" "Path to the PFX file for this run (relative to --dir-pfx)" | |
| printf "\n" | |
| } | |
| # Log messages to file (opt: And terminal) -- Args: $1: Message to log; $2: Log to Terminal (def: 0) | |
| log() { | |
| if [ -z "${1}" ]; then return 1; fi # Break function if message not supplied | |
| if [ -n "${2}" ] && [ "${2}" -eq 1 ]; then # If $2 supplied && == 1 | |
| echo -e "${1}" | tee -a "${_path_file_log}" # Log message to terminal && logfile | |
| else # Otherwise | |
| echo -e "${1}" >> "${_path_file_log}" # Log message to file only | |
| fi | |
| } | |
| # Generate Keyfile | |
| genKey() { | |
| local _dom="${_ossl_arg_domain}" # Domain for generation | |
| local _kpw="${_ossl_arg_password}" # Password for generation | |
| local _len="${_ossl_arg_length}" # Length of key pair (bit) | |
| local _dst="${_path_file_key}" # Destination file | |
| local _log="${_path_file_log}" # Alias for logfile | |
| local _arg="genrsa" # Arguments for keyfile generation | |
| # Update Arguments | |
| if [[ "${_ossl_arg_type,,}" == "aes256" ]]; then _arg+=" -aes256"; fi # Add AES256 switch | |
| # Attempt to generate keyfile | |
| if ! openssl "${_arg}" -out "${_dst}" "${_len}" -passout pass:"${_kpw}" 2>&1 >> "${_log}"; then # Generate Key; On Failure | |
| log "ERROR:\tFailed to generate keyfile!" "1" # Log Failure | |
| unset _dom _kpw _len _dst _log _arg # Clear Memory | |
| return 1 # Return Failure | |
| fi | |
| # Clean Up | |
| unset _dom _kpw _len _dst _log _arg # Clear Memory | |
| return 0 # Return Success | |
| } | |
| # Generate Certificate Request | |
| genReq() { | |
| local _dom="${_ossl_arg_domain}" # Domain for generation | |
| local _kpw="${_ossl_arg_password}" # Password for generation | |
| local _day="${_ossl_arg_days}" # Days certificate is valid | |
| local _dst="${_path_file_req}" # Destination file | |
| local _key="${_path_file_key}" # Keyfile | |
| local _log="${_path_file_log}" # Alias for logfile | |
| local _arg="req -new -x509" # Arguments for request generation | |
| local _sub="" # Subject String | |
| # Build Subject | |
| _sub="/C=${_ossl_arg_country}" # Add Country to Subject | |
| _sub+="/ST=${_ossl_arg_state}" # Add State to Subject | |
| _sub+="/L=${_ossl_arg_city}" # Add City to Subject | |
| _sub+="/O=${_ossl_arg_company}" # Add Company to Subject | |
| _sub+="/OU=${_ossl_arg_org}" # Add OU to Subject | |
| _sub+="/CN=${_ossl_arg_cname}" # Add Common Name to Subject | |
| _sub+="/emailAddress=${_ossl_arg_email}" # Add Email Address to Subject | |
| # Attempt to generate request | |
| if ! openssl "${_arg}" -key "${_key}" -days "${_day}" -out "${_dst}" -subj "${_sub}" -passin pass:"${_kpw}" 2>&1 >> "${_log}"; then # Generate Request; On Failure | |
| log "ERROR:\tFailed to generate Request!" "1" # log Failure | |
| unset _dom _kpw _day _dst _key _log _arg _sub # Clear Memory | |
| return 1 # Return Failure | |
| fi | |
| # Clean Up | |
| unset _dom _kpw _day _dst _key _log _arg _sub # Clear Memory | |
| return 0 | |
| } | |
| # Generate Output FIle (PFX) | |
| genPFX() { | |
| local _dom="${_ossl_arg_domain}" # Domain for generation | |
| local _kpw="${_ossl_arg_password}" # Password for generation | |
| local _csp="${_ossl_arg_mscsp}" # CSP for generation | |
| local _dst="${_path_file_pfx}" # Destination file | |
| local _key="${_path_file_key}" # Keyfile | |
| local _crt="${_path_file_crt}" # Certificate File | |
| local _log="${_path_file_log}" # Alias for logfile | |
| local _arg="pkcs12 -export" # Arguments for request generation | |
| # Attempt to generate request | |
| if ! openssl "${_arg}" -in "${_crt}" -inkey "${_key}" -out "${_dst}" -CSP "${_csp}" -passin pass:"${_kpw}" 2>&1 >> "${_log}"; then # Generate PFX; On Failure | |
| log "ERROR:\tFailed to generate PFX!" "1" # Log Failure | |
| unset _dom _kpw _csp _dst _key _crt _log _arg # Clear Memory | |
| return 1 # Return Failure | |
| fi | |
| # Clean Up | |
| unset _dom _kpw _csp _dst _key _crt _log _arg # Clear Memory | |
| return 0 # Return Success | |
| } | |
| # ##------------------------## | |
| # #| Variables |# | |
| # ##------------------------## | |
| # Paths -- Directories | |
| _path_dir_root="${HOME}/MKCerts" # Root of storage location for script | |
| _path_dir_logs="Logs" # Directory containing logfiles | |
| _path_dir_key="RSA" # Directory containing RSA files | |
| _path_dir_req="Requests" # Directory containing Requests | |
| _path_dir_crt="Certificates" # Directory containing Certificates | |
| _path_dir_pfx="PFX" # Directory containing Final Products (.pfx) | |
| # Paths -- Files | |
| _path_file_log="/dev/null" # Logfile | |
| _path_file_key="_Idp.key" # Keyfile | |
| _path_file_req="_Idp.req" # Certificate Request | |
| _path_file_crt="_Idp.crt" # Certificate | |
| _path_file_pfx="_Idp.pfx" # Output PFX File | |
| # OpenSSL Options | |
| _ossl_arg_domain="" # OpenSSL Domain | |
| _ossl_arg_password="" # OpenSSL Password | |
| _ossl_arg_length="2048" # OpenSSL Key Length | |
| _ossl_arg_type="aes256" # OpenSSL Encryption Type | |
| _ossl_arg_days="730" # OpenSSL Days Valid (def: 2yr) | |
| _ossl_arg_country="US" # OpenSSL Country | |
| _ossl_arg_state="State" # OpenSSL State | |
| _ossl_arg_city="City" # OpenSSL City | |
| _ossl_arg_company="Company,Inc." # OpenSSL Organization | |
| _ossl_arg_org="IT" # OpenSSL Organizational Unit (Department) | |
| _ossl_arg_cname="" # OpenSSL Common Name | |
| _ossl_arg_email="[email protected]" # OpenSSL Email Address | |
| _ossl_arg_mscsp="Microsoft Enhanced RSA and AES Cryptographic Provider" # OpenSSL Microsoft CSP Name | |
| # Options | |
| _opt_force="0" # Skip Confirmation Checks | |
| _opt_key="0" # Generate a Key Pair | |
| _opt_req="0" # Generate a Certificate Request | |
| _opt_pfx="0" # Request a PFX | |
| # ##--------------------------------## | |
| # #| Handle Arguments |# | |
| # ##--------------------------------## | |
| OPTS="$(getopt \ | |
| -o hflkrxad:p: \ | |
| --long help,force,log,gen-key,gen-req,gen-pfx,gen-all,domain:,password:,length:,type:,days:,country:,state:,city:,company:,ou:,cname:,email:,mscsp:,dir-root:,dir-key:,dir-req:,dir-crt:,dir-pfx:,file-key:,file-req:,file-crt:,file-pfx: \ | |
| -n 'mkCert_Args' -- "${@}")" | |
| eval set -- "${OPTS}" | |
| while true; do | |
| case "${1}" in | |
| -h | --help) show_help; exit 0; | |
| -f | --force ) _opt_force="1"; shift;; | |
| -l | --log ) _path_file_log="$(date --iso-8601).log"; shift;; | |
| -k | --gen-key ) _opt_key="1"; shift;; | |
| -r | --gen-req ) _opt_req="1"; shift;; | |
| -x | --gen-pfx ) _opt_pfx="1"; shift;; | |
| -a | --gen-all ) _opt_key="1"; _opt_req="1"; _opt_pfx="1"; shift;; | |
| -d | --domain ) _ossl_arg_domain="${2}"; shift 2;; | |
| -p | --password ) _ossl_arg_password="${2}"; shift 2;; | |
| --length ) _ossl_arg_length="${2}"; shift 2;; | |
| --type ) _ossl_arg_type="${2}"; shift 2;; | |
| --days ) _ossl_arg_days="${2}"; shift 2;; | |
| --country ) _ossl_arg_country="${2}"; shift 2;; | |
| --state ) _ossl_arg_state="${2}"; shift 2;; | |
| --city ) _ossl_arg_city="${2}"; shift 2;; | |
| --ou ) _ossl_arg_org="${2}"; shift 2;; | |
| --cname ) _ossl_arg_cname="${2}"; shift 2;; | |
| --mscsp ) _ossl_arg_mscsp="${2}"; shift 2;; | |
| --dir-root ) _path_dir_root="${2}"; shift 2;; | |
| --dir-key ) _path_dir_key="${2}"; shift 2;; | |
| --dir-req ) _path_dir_req="${2}"; shift 2;; | |
| --dir-crt ) _path_dir_crt="${2}"; shift 2;; | |
| --dir-pfx ) _path_dir_pfx="${2}"; shift 2;; | |
| --file-key ) _path_file_key="${2}"; shift 2;; | |
| --file-req ) _path_file_req="${2}"; shift 2;; | |
| --file-crt ) _path_file_crt="${2}"; shift 2;; | |
| --file-pfx ) _path_file_pfx="${2}"; shift 2;; | |
| -- ) shift; break;; | |
| * ) break;; | |
| esac | |
| done | |
| # ##----------------------------## | |
| # #| Pre-Run Tasks |# | |
| # ##----------------------------## | |
| # Update Paths | |
| if [[ "${_path_dir_logs:0:1}" != "/" ]]; then _path_dir_logs="${_path_dir_root}/${_path_dir_logs}"; fi | |
| if [[ "${_path_dir_key:0:1}" != "/" ]]; then _path_dir_key="${_path_dir_root}/${_path_dir_key}"; fi | |
| if [[ "${_path_dir_req:0:1}" != "/" ]]; then _path_dir_req="${_path_dir_root}/${_path_dir_req}"; fi | |
| if [[ "${_path_dir_crt:0:1}" != "/" ]]; then _path_dir_crt="${_path_dir_root}/${_path_dir_crt}"; fi | |
| if [[ "${_path_dir_pfx:0:1}" != "/" ]]; then _path_dir_pfx="${_path_dir_root}/${_path_dir_pfx}"; fi | |
| if [[ "${_path_file_log:0:1}" != "/" ]] && [[ "${_path_file_log,,}" != "/dev/null" ]]; then _path_file_log="${_path_dir_logs}/${_path_file_log}"; fi | |
| if ! echo "${_path_file_key}" | grep -qi "${_ossl_arg_domain}"; then _path_file_key="${_ossl_arg_domain}${_path_file_key}"; fi | |
| if ! echo "${_path_file_req}" | grep -qi "${_ossl_arg_domain}"; then _path_file_req="${_ossl_arg_domain}${_path_file_req}"; fi | |
| if ! echo "${_path_file_crt}" | grep -qi "${_ossl_arg_domain}"; then _path_file_crt="${_ossl_arg_domain}${_path_file_crt}"; fi | |
| if ! echo "${_path_file_pfx}" | grep -qi "${_ossl_arg_domain}"; then _path_file_pfx="${_ossl_arg_domain}${_path_file_pfx}"; fi | |
| if [[ "${_path_file_key:0:1}" != "/" ]]; then _path_file_key="${_path_dir_key}/${_path_file_key}"; fi | |
| if [[ "${_path_file_req:0:1}" != "/" ]]; then _path_file_req="${_path_dir_req}/${_path_file_req}"; fi | |
| if [[ "${_path_file_crt:0:1}" != "/" ]]; then _path_file_crt="${_path_dir_crt}/${_path_file_crt}"; fi | |
| if [[ "${_path_file_pfx:0:1}" != "/" ]]; then _path_file_pfx="${_path_dir_pfx}/${_path_file_pfx}"; fi | |
| # Check Options | |
| if ! checkOpts; then exit 1; fi | |
| # ##----------------## | |
| # #| Run |# | |
| # ##----------------## | |
| if [ "${_opt_key}" ]; then if ! genKey; then exit 1; fi; fi # Generate Keyfile; On Failure exit code 1 | |
| if [ "${_opt_req}" ]; then if ! genReq; then exit 1; fi; fi # Generate Request; On Failure exit code 1 | |
| if [ "${_opt_pfx}" ]; then if ! genPFX; then exit 1; fi; fi # Generate PFX; On Failure exit code 1 | |
| usv # Clear Memory |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment