CA.sh: show all output

add some color
This commit is contained in:
Li Chen 2013-11-30 13:56:25 +01:00
parent c90e41b6b7
commit 5de5775310

View file

@ -1,4 +1,4 @@
# TODO key usage
#! /bin/sh
if [[ -z "${TOP_DIR}" ]]; then
TOP_DIR=pki
@ -13,9 +13,15 @@ CAREQ=./careq.csr
CACERT=./cacert.crt
DAYS=365
GREEN="\033[1;32m"
RED="\033[1;31m"
COLOR_RST="\033[0m"
#GREEN="\033[1;32m"
#RED="\033[1;31m"
#COLOR_RST="\033[0m"
GREEN="<font color=green>"
RED="<font color=red>"
COLOR_RST="</font>"
BOLD="<b>"
END_BOLD="</b>"
usage()
{
@ -23,25 +29,37 @@ usage()
exit 1
}
[ $# -lt 1 ] && usage
case $1 in
"-newca" )
# echo -e -n "${GREEN}Create the directories, take care this will delete"
# echo -e "the old directories ${COLOR_RST}"
# sleep 1; echo -n "1 "; sleep 1; echo -n "2 "; sleep 1; echo "3"
clean()
{
if [ "$1" = "ca" ]; then
rm -rf ${TOP_DIR}
mkdir -p ${TOP_DIR}/certs
mkdir -p ${TOP_DIR}/crl
mkdir -p ${TOP_DIR}/newcerts
mkdir -p ${TOP_DIR}/private
mkdir -p ${TOP_DIR}/pkcs
elif [ "$1" = "client" ]; then
rm -rf ${2}.key ${2}.csr ${2}.crt
fi
rm -rf $OUTPUT
}
[ $# -lt 1 ] && usage
OUTPUT=$(mktemp)
case $1 in
"-newca" )
echo -e -n "${GREEN}Create the directories, take care this will delete"
echo -e "the old directories ${COLOR_RST}"
# sleep 1; echo -n "1 "; sleep 1; echo -n "2 "; sleep 1; echo "3"
clean "ca"
touch ${TOP_DIR}/index.txt
ESCAPED=$(echo "${TOP_DIR}" | sed 's/[\/\.]/\\&/g')
# echo -e "${GREEN}Making CA key and csr${COLOR_RST}"
echo -e "${GREEN}Making CA key and csr${COLOR_RST}"
sed -i 's/=.*#COMMONNAME/= FIC2014 CA #COMMONNAME/' $OPENSSL_CONF
sed -i "s/=.*#DIR/= ${ESCAPED} #DIR/" $OPENSSL_CONF
sed -i "s/=.*#CERTTYPE/= objsign #CERTTYPE/" $OPENSSL_CONF
@ -56,17 +74,32 @@ case $1 in
openssl req -batch -new -keyout ${TOP_DIR}/private/${CAKEY} \
-out ${TOP_DIR}/${CAREQ} -passout pass:$pass \
-config $OPENSSL_CONF
-config $OPENSSL_CONF > $OUTPUT 2>&1
if [ $? -ne 0 ]; then
cat $OUTPUT
clean "ca"
exit 4
fi
# This line deleted the passphase for the FIC 2014 automatisation
openssl rsa -passin pass:$pass -in ${TOP_DIR}/private/${CAKEY} \
-out ${TOP_DIR}/private/${CAKEY}
-out ${TOP_DIR}/private/${CAKEY} > $OUTPUT 2>&1
if [ $? -ne 0 ]; then
cat $OUTPUT
clean "ca"
exit 4
fi
# echo -e "${GREEN}Self signes the CA certificate${COLOR_RST}"
echo -e "${GREEN}Self signes the CA certificate${COLOR_RST}"
openssl ca -batch -create_serial -out ${TOP_DIR}/${CACERT} \
-days ${DAYS} -keyfile ${TOP_DIR}/private/${CAKEY} \
-selfsign -extensions v3_ca -config ${OPENSSL_CONF} \
-infiles ${TOP_DIR}/${CAREQ}
-infiles ${TOP_DIR}/${CAREQ} > $OUTPUT 2>&1
if [ $? -ne 0 ]; then
cat $OUTPUT
clean "ca"
exit 4
fi
;;
"-newserver" )
echo -e "${GREEN}Making the Server key and cert${COLOR_RST}"
@ -77,13 +110,18 @@ case $1 in
sed -i 's/=.*#COMMONNAME/= FIC2014 Server #COMMONNAME/' $OPENSSL_CONF
sed -i "s/=.*#CERTTYPE/= server #CERTTYPE/" $OPENSSL_CONF
openssl req -batch -new -keyout server.key -out server.csr \
-days ${DAYS} -config ${OPENSSL_CONF}
-days ${DAYS} -config ${OPENSSL_CONF} > $OUTPUT 2>&1
if [ $? -ne 0 ]; then
cat $OUTPUT
exit 4
fi
echo -e "${GREEN}Signing the Server crt${COLOR_RST}"
openssl ca -policy policy_match -config ${OPENSSL_CONF} \
-out server.crt -infiles server.csr
-out server.crt -infiles server.csr > $OUTPUT 2>&1
if [ $? -ne 0 ]; then
echo -e "${RED}Signing failed for new server${COLOR_RST}"
rm -rf server.key server.crt server.csr
cat $OUTPUT
exit 3
else
rm server.csr # remove ?
@ -95,7 +133,8 @@ case $1 in
echo "Usage: $0 -newclient NAME"
exit 1
fi
# echo -e "${GREEN}Making the client key and csr${COLOR_RST}"
echo "=============================================================="
echo -e "${GREEN}Making the client key and csr of ${BOLD}${2}${END_BOLD}${COLOR_RST}"
if ! [ -f ${TOP_DIR}/private/${CAKEY} ]; then
echo -e "${RED}Can not found the CA's key${COLOR_RST}"
@ -113,27 +152,37 @@ case $1 in
pass=`pwgen -n -B -y 12 1`
openssl req -batch -new -keyout ${2}.key -out ${2}.csr \
-config ${OPENSSL_CONF} -passout pass:$pass -days ${DAYS}
-config ${OPENSSL_CONF} -passout pass:$pass -days ${DAYS} > $OUTPUT 2>&1
if [ $? -ne 0 ]; then
cat $OUTPUT
clean "client" $2
exit 4
fi
# echo -e "${GREEN}Signing the Client crt${COLOR_RST}"
echo -e "${GREEN}Signing the Client crt${COLOR_RST}"
openssl ca -batch -policy policy_match -out ${2}.crt \
-config ${OPENSSL_CONF} -infiles ${2}.csr
-config ${OPENSSL_CONF} -infiles ${2}.csr > $OUTPUT 2>&1
if [ $? -ne 0 ]; then
echo -e "${RED}Signing failed for $2 ${COLOR_RST}"
cat $OUTPUT
clean "client" $2
exit 3
fi
# echo -e "${GREEN}Export the Client files to pkcs12${COLOR_RST}"
echo -e "${GREEN}Export the Client files to pkcs12${COLOR_RST}"
openssl pkcs12 -export -inkey ${2}.key -in ${2}.crt -name ${2} \
-passin pass:$pass -out ${TOP_DIR}/pkcs/${2}.p12 -passout pass:$pass
-passin pass:$pass -out ${TOP_DIR}/pkcs/${2}.p12 \
-passout pass:$pass > $OUTPUT 2>&1
if [ $? -ne 0 ]; then
echo -e "${RED}pkcs12 export failed${COLOR_RST} for $2"
echo -e "${RED}pkcs12 export failed${COLOR_RST} for ${BOLD}$2${END_BOLD}"
cat $OUTPUT
clean "client" $2
exit 4
else
echo -e "Exported pkcs12 file is ${2}.p12"
fi
# TODO handle this file
echo "$2:$pass" >> teams.pass
rm -rf ${2}.key ${2}.csr ${2}.crt
clean "client" $2
;;
* )
usage