certgen/certgen.sh

149 lines
4.4 KiB
Bash
Raw Normal View History

2022-12-29 09:34:34 +01:00
#!/bin/bash
########################################
# Author: F. Bischof (frank@meer-web.nl)
2023-06-02 10:03:35 +02:00
# Version: 1.5.2
# Date: 02-06-2023
2022-12-29 09:34:34 +01:00
########################################
ENCRYPTION="sha256"
BITS="rsa:2048"
function create_csr {
2023-05-15 16:29:28 +02:00
echo -en "Domain: "; read DOMAIN
echo -en "Config file (optional): "; read CONFIG
2022-12-29 09:34:34 +01:00
if [ "${CONFIG}" != "" ]
then
openssl req -utf8 -nodes -${ENCRYPTION} -newkey ${BITS} -keyout ${DOMAIN}.key -out ${DOMAIN}.csr -config ${CONFIG_FILE} -extensions 'req_ext'
else
openssl req -utf8 -nodes -${ENCRYPTION} -newkey ${BITS} -keyout ${DOMAIN}.key -out ${DOMAIN}.csr
fi
echo "CSR ${DOMAIN}.csr created!"
}
function create_ssc {
2023-05-15 16:29:28 +02:00
echo -en "Domain: "; read DOMAIN
2022-12-29 09:34:34 +01:00
echo -en "How many years should it be valid: "; read YEARSVALID
YEARSVALID=$(($YEARSVALID * 365))
openssl req -x509 -nodes -days ${YEARSVALID} -newkey ${BITS} -keyout ${DOMAIN}.key -out ${DOMAIN}.crt -${ENCRYPTION}
echo "Self Signed Certificate generated!"
}
function create_pfx {
2023-05-15 16:29:28 +02:00
echo -en "PFX output filename: "; read PFX_FILE
2022-12-29 09:34:34 +01:00
echo -en "Domain certificate path: "; read DOMAIN_CERT
echo -en "Domain key path: "; read DOMAIN_KEY
echo -en "Intermediate certificate path (optional): "; read INT_CERT
if [ "${INT_CERT}" != "" ];
then
echo -en "Root certificate path: "; read ROOT_CERT
2023-05-15 16:29:28 +02:00
cat ${INT_CERT} ${ROOT_CERT} > bundle-ca.crt;
BUNDLE_CERT="bundle-ca.crt"
openssl pkcs12 -export -in ${DOMAIN_CERT} -out ${PFX_FILE}.pfx -inkey ${DOMAIN_KEY} -certfile ${BUNDLE_CERT}
2022-12-29 09:34:34 +01:00
else
2023-05-15 16:29:28 +02:00
openssl pkcs12 -export -in ${DOMAIN_CERT} -out ${PFX_FILE}.pfx -inkey ${DOMAIN_KEY}
2022-12-29 09:34:34 +01:00
fi
2023-05-15 16:29:28 +02:00
echo "File ${PFX_FILE}.pfx created!"
2022-12-29 09:34:34 +01:00
}
function extract_pfx {
echo -en "PFX path: "; read PFX_CERT
2023-06-02 10:03:35 +02:00
FILENAME="`${PFX_CERT} | sed 's/\.pfx//'`"
2023-05-15 16:29:28 +02:00
openssl pkcs12 -in ${PFX_CERT} -nocerts -out ${FILENAME}.key -nodes
openssl pkcs12 -in ${PFX_CERT} -nokeys -out ${FILENAME}.crt
echo "Files ${FILENAME}.key and ${FILENAME}.crt created from PFX"
2022-12-29 09:34:34 +01:00
}
function remove_pw {
2023-05-15 16:29:28 +02:00
echo -en "Key path: "; read DOMAIN_KEY
2022-12-29 09:34:34 +01:00
openssl rsa -in ${DOMAIN_KEY} -out decrypted-${DOMAIN_KEY}
echo "File decrypted-${DOMAIN_KEY} created!"
}
function create_all {
2023-05-15 16:29:28 +02:00
echo -en "Domain: "; read DOMAIN
2022-12-29 09:34:34 +01:00
echo -en "Domain certificate path: "; read DOMAIN_CERT
echo -en "Domain key path (optional): "; read DOMAIN_KEY
echo -en "Intermediate certificate path: "; read INT_CERT
echo -en "Root certificate path: "; read ROOT_CERT
# Create PEM
if [ "${DOMAIN_KEY}" != "" ]
then
touch ${DOMAIN}.pem
cat ${DOMAIN_KEY} > ${DOMAIN}.pem
2023-06-02 10:03:35 +02:00
echo "" >> ${DOMAIN}.pem
2022-12-29 09:34:34 +01:00
cat ${DOMAIN_CERT} >> ${DOMAIN}.pem
2023-06-02 10:03:35 +02:00
echo "" >> ${DOMAIN}.pem
2022-12-29 09:34:34 +01:00
cat ${INT_CERT} >> ${DOMAIN}.pem
2023-06-02 10:03:35 +02:00
echo "" >> ${DOMAIN}.pem
2022-12-29 09:34:34 +01:00
cat ${ROOT_CERT} >> ${DOMAIN}.pem
2023-06-02 10:03:35 +02:00
echo "" >> ${DOMAIN}.pem
sed '/^$/d' -i ${DOMAIN}.pem
2022-12-29 09:34:34 +01:00
echo "${DOMAIN}.pem (key, domain, intermediate, root) created"
fi
# Create BUNDLE-CA
touch bundle-ca.crt
cat ${INT_CERT} >> bundle-ca.crt
2023-06-02 10:03:35 +02:00
echo "" >> bundle-ca.crt
2022-12-29 09:34:34 +01:00
cat ${ROOT_CERT} >> bundle-ca.crt
2023-06-02 10:03:35 +02:00
sed '/^$/d' -i bundle-ca.crt
2022-12-29 09:34:34 +01:00
echo "bundle-ca created (intermediate, root)"
# Create domain bundle
touch bundle.crt
cat ${DOMAIN_CERT} >> bundle.crt
2023-06-02 10:03:35 +02:00
echo "" >> bundle.crt
2022-12-29 09:34:34 +01:00
cat ${INT_CERT} >> bundle.crt
2023-06-02 10:03:35 +02:00
echo "" >> bundle.crt
2022-12-29 09:34:34 +01:00
cat ${ROOT_CERT} >> bundle.crt
2023-06-02 10:03:35 +02:00
echo "" >> bundle.crt
sed '/^$/d' -i bundle.crt
2022-12-29 09:34:34 +01:00
echo "bundle.crt (domain, intermediate, root) created"
}
function decode_csr {
echo -en "CSR path: "; read CSR_PATH
openssl req -in ${CSR_PATH} -noout -text
}
function decode_crt {
echo -en "CRT path: "; read CRT_PATH
openssl x509 -in ${CRT_PATH} -text -noout
}
function match_crtkey {
echo -en "CRT path: "; read CRT_PATH
echo -en "KEY path: "; read KEY_PATH
CRT_HASH="`openssl x509 -in ${CRT_PATH} -pubkey -noout -outform pem | sha256sum`"
KEY_HASH="`openssl pkey -in ${KEY_PATH} -pubout -outform pem | sha256sum`"
if [ "${CRT_HASH}" == "${KEY_HASH}" ]
then
echo -e "\nCertificate and key matching"
else
echo -e "\nCertificate and key are NOT matching!"
fi
}
# Show options menu
echo "1. Create self signed certificate"
echo "2. Create CSR";
echo "3. Create PFX file"
echo "4. Extract PFX to CRT/KEY"
echo "5. Create CA-Bundle and PEM files"
echo "6. Remove password from KEY file"
echo "7. Decode CSR"
echo "8. Decode CRT"
echo "9. Match CRT/KEY"
echo -en "Option: "; read OPTION
2022-12-29 09:34:34 +01:00
case ${OPTION} in
1) create_ssc ;;
2) create_csr ;;
3) create_pfx ;;
4) extract_pfx ;;
5) create_all ;;
6) remove_pw ;;
7) decode_csr ;;
8) decode_crt ;;
9) match_crtkey ;;
*) echo "Invalid option, exiting!"; exit 2;;
esac
exit 0