#!/bin/bash
# info: list tulio ssl certificate
# options: [FORMAT]
#
# example: v-list-sys-tulio-ssl
#
# This function of obtaining tulio ssl files.

#----------------------------------------------------------#
#                Variables & Functions                     #
#----------------------------------------------------------#

# Argument definition
format=${1-shell}

# Includes
# shellcheck source=/etc/tuliocp/tulio.conf
source /etc/tuliocp/tulio.conf
# shellcheck source=/usr/local/tulio/func/main.sh
source $TULIO/func/main.sh
# load config file
source_conf "$TULIO/conf/tulio.conf"

# Perform verification if read-only mode is enabled
check_tulio_demo_mode

# JSON list function
json_list() {
	json_escape() {
		echo -n "$1" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g'
	}
	echo '{'
	echo -e "\t\"TULIO\": {"
	echo "        \"CRT\": \"$crt\","
	echo "        \"KEY\": \"$key\","
	echo "        \"CA\": \"$ca\","
	echo "        \"SUBJECT\": \"$(json_escape "$subj")\","
	echo "        \"ALIASES\": \"$(json_escape "$alt_dns")\","
	echo "        \"NOT_BEFORE\": \"$(json_escape "$before")\","
	echo "        \"NOT_AFTER\": \"$(json_escape "$after")\","
	echo "        \"SIGNATURE\": \"$(json_escape "$signature")\","
	echo "        \"PUB_KEY\": \"$(json_escape "$pub_key")\","
	echo "        \"ISSUER\": \"$(json_escape "$issuer")\""
	echo -e "\t}\n}"
}

# SHELL list function
shell_list() {
	if [ -n "$crt" ]; then
		echo -e "$crt"
	fi
	if [ -n "$key" ]; then
		echo -e "\n$key"
	fi
	if [ -n "$crt" ]; then
		echo
		echo
		echo "SUBJECT:        $subj"
		if [ -n "$alt_dns" ]; then
			echo "ALIASES:        ${alt_dns//,/ }"
		fi
		echo "VALID FROM:     $before"
		echo "VALID TIL:      $after"
		echo "SIGNATURE:      $signature"
		echo "PUB_KEY:        $pub_key"
		echo "ISSUER:         $issuer"
	fi
}

# PLAIN list function
plain_list() {
	if [ -n "$crt" ]; then
		echo -e "$crt"
	fi
	if [ -n "$key" ]; then
		echo -e "\n$key"
	fi
	if [ -n "$ca" ]; then
		echo -e "\n$ca"
	fi
	if [ -n "$crt" ]; then
		echo "$subj"
		echo "${alt_dns//,/ }"
		echo "$before"
		echo "$after"
		echo "$signature"
		echo "$pub_key"
		echo "$issuer"
	fi

}

# CSV list function
csv_list() {
	echo -n "CRT,KEY,CA,SUBJECT,ALIASES,NOT_BEFORE,NOT_AFTER,SIGNATURE,"
	echo "PUB_KEY,ISSUER"
	echo -n "\"$crt\",\"$key\",\"$ca\",\"$subj\",\"${alt_dns//,/ }\","
	echo "\"$before\",\"$after\",\"$signature\",\"$pub_key\",\"$issuer\""
}

#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

# Parsing SSL certificate
crt=$(cat $TULIO/ssl/certificate.crt | sed ':a;N;$!ba;s/\n/\\n/g')
key=$(cat $TULIO/ssl/certificate.key | sed ':a;N;$!ba;s/\n/\\n/g')
if [ -e "$TULIO/ssl/certificate.ca" ]; then
	ca=$(cat $TULIO/ssl/certificate.ca | sed ':a;N;$!ba;s/\n/\\n/g')
fi

# Parsing SSL certificate details without CA
info=$(openssl x509 -text -in $TULIO/ssl/certificate.crt)
subj=$(openssl x509 -noout -nameopt multiline -subject -in $TULIO/ssl/certificate.crt | sed -n 's/^ *commonName *= //p')
before=$(echo "$info" | grep Before: | sed -e "s/.*Before: //")
after=$(echo "$info" | grep "After :" | sed -e "s/.*After : //")
signature=$(echo "$info" | grep "Algorithm:" | head -n1)
signature=$(echo "$signature" | sed -e "s/.*Algorithm: //")
pub_key=$(echo "$info" | grep Public-Key: | cut -f2 -d \( | tr -d \))
issuer=$(echo "$info" | grep Issuer: | sed -e "s/.*Issuer: //")
alt_dns=$(echo "$info" | grep DNS | sed -e 's/DNS:/\n/g' | tr -d ',')
alt_dns=$(echo "$alt_dns" | tr -d ' ' | sed -e "/^$/d")
alt_dns=$(echo "$alt_dns" | sed -e ':a;N;$!ba;s/\n/,/g')

# Listing data
case $format in
	json) json_list ;;
	plain) plain_list ;;
	csv) csv_list ;;
	shell) shell_list ;;
esac

#----------------------------------------------------------#
#                       Tulio                             #
#----------------------------------------------------------#

exit
