Last active
October 8, 2020 16:22
-
-
Save dragonman225/b73e22f30b7676c2e86f86ff349b540d to your computer and use it in GitHub Desktop.
Create a CA certificate and issue client certificates with it.
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 | |
# Parameters | |
# ==================================================================================== | |
# Color Alias | |
NO_COLOR='\033[0m' | |
RED_BG='\033[7;49;31m' | |
RED='\033[1;49;31m' | |
GREEN_BG='\033[7;49;32m' | |
GREEN='\033[1;49;32m' | |
ORANGE_BG='\033[7;49;33m' | |
ORANGE='\033[1;49;33m' | |
BLUE_BG='\033[7;49;34m' | |
BLUE='\033[1;49;34m' | |
PURPLE_BG='\033[7;49;35m' | |
PURPLE='\033[1;49;35m' | |
CYAN_BG='\033[7;49;36m' | |
CYAN='\033[1;49;36m' | |
WHITE_BG='\033[7;49;37m' | |
WHITE='\033[1;49;37m' | |
LIGHT_ORANGE_BG='\033[7;49;93m' | |
LIGHT_ORANGE='\033[1;49;93m' | |
# Functions | |
# ==================================================================================== | |
function welcome { | |
echo -e "${GREEN}Welcome to Certificate Issuer${NO_COLOR}" | |
} | |
function issue_ca { | |
echo -e "${WHITE}Let's issue new CA certificate.${NO_COLOR}" | |
read -p "Common Name (CN) : " cname | |
read -p "Organization (O) : " org | |
openssl req -x509 -newkey rsa:4096 -keyout $1/ca_key.pem -out $1/ca_cert.pem -nodes -days 365 -subj "/CN=${cname}/O=${org}" | |
echo -e "${WHITE}CA certificate saved to $1.${NO_COLOR}" | |
} | |
function issue_client { | |
echo -e "${WHITE}Let's issue new client certificate.${NO_COLOR}" | |
read -p "Common Name (CN) : " cname | |
openssl req -newkey rsa:4096 -keyout $1/${cname}_key.pem -out $1/${cname}_csr.pem -nodes -days 365 -subj "/CN=${cname}" | |
openssl x509 -req -in $1/${cname}_csr.pem -CA $1/ca_cert.pem -CAkey $1/ca_key.pem -out $1/${cname}_cert.pem -set_serial 01 -days 365 | |
openssl pkcs12 -export -clcerts -in $1/${cname}_cert.pem -inkey $1/${cname}_key.pem -out $1/${cname}.p12 | |
echo -e "${WHITE}Client certificate saved to $1.${NO_COLOR}" | |
} | |
function view_ca { | |
openssl x509 -in $1/ca_cert.pem -noout -text | |
} | |
function view_client { | |
local cert_dir=$1 | |
read -p "Enter the client's Common Name (CN) : " cname | |
local cert_file=$1/${cname}_cert.pem | |
if [ -f ${cert_file} ]; then | |
openssl x509 -in ${cert_file} -noout -text | |
else | |
echo -e "${RED}Client not found.${NO_COLOR}" | |
fi | |
} | |
# Main | |
# ==================================================================================== | |
welcome | |
read -p "Select a certificate directory : " cert_dir | |
if [ ! -d "${cert_dir}" ]; then | |
mkdir -p ${cert_dir} | |
fi | |
if [ -f "${cert_dir}/ca_key.pem" ] && [ -f "${cert_dir}/ca_cert.pem" ]; then | |
echo -e "${WHITE}CA certificate found.${NO_COLOR}" | |
read -p "View CA (0) or issue client (1) or view client (2) ? " opt | |
if [ "${opt}" == 0 ]; then | |
view_ca ${cert_dir} | |
elif [ "${opt}" == 1 ]; then | |
issue_client ${cert_dir} | |
elif [ "${opt}" == 2 ]; then | |
view_client ${cert_dir} | |
else | |
echo -e "${RED}Unknown option.${NO_COLOR}" | |
fi | |
else | |
echo -e "${WHITE}No CA certificate found.${NO_COLOR}" | |
issue_ca ${cert_dir} | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment