| 1 | #!/usr/bin/env bash |
| 2 | # ========================================================================== |
| 3 | # ____ _ _____ _ |
| 4 | # / ___| _ _ ___| |_ ___ _ __ ___ |_ _|__ ___ | |___ |
| 5 | # \___ \| | | / __| __/ _ \ '_ ` _ \ _____| |/ _ \ / _ \| / __| |
| 6 | # ___) | |_| \__ \ || __/ | | | | |_____| | (_) | (_) | \__ \ |
| 7 | # |____/ \__, |___/\__\___|_| |_| |_| |_|\___/ \___/|_|___/ |
| 8 | # |___/ |
| 9 | # --- System-Tools --- |
| 10 | # https://www.nntb.no/~dreibh/system-tools/ |
| 11 | # ========================================================================== |
| 12 | # |
| 13 | # X.509 Certificate Checker |
| 14 | # Copyright (C) 2025-2026 by Thomas Dreibholz |
| 15 | # |
| 16 | # This program is free software: you can redistribute it and/or modify |
| 17 | # it under the terms of the GNU General Public License as published by |
| 18 | # the Free Software Foundation, either version 3 of the License, or |
| 19 | # (at your option) any later version. |
| 20 | # |
| 21 | # This program is distributed in the hope that it will be useful, |
| 22 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | # GNU General Public License for more details. |
| 25 | # |
| 26 | # You should have received a copy of the GNU General Public License |
| 27 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | # |
| 29 | # Contact: [email protected] |
| 30 | |
| 31 | # Bash options: |
| 32 | set -eu |
| 33 | |
| 34 | # gettext options: |
| 35 | export TEXTDOMAIN="check-certificate" |
| 36 | # export TEXTDOMAINDIR="${PWD}/locale" # Default: "/usr/share/locale" |
| 37 | |
| 38 | # shellcheck disable=SC1091 |
| 39 | . gettext.sh |
| 40 | |
| 41 | |
| 42 | # ###### Usage ############################################################## |
| 43 | usage () { |
| 44 | echo >&2 "$(gettext "Usage:") $0 ca_certificate_file certificate_file [...] [-C|--crl crl] [-n|--no-view-certificate] [-h|--help]" |
| 45 | exit 1 |
| 46 | } |
| 47 | |
| 48 | |
| 49 | # ###### Main program ####################################################### |
| 50 | |
| 51 | # ====== Handle arguments =================================================== |
| 52 | DIRNAME="$(dirname "$0")" |
| 53 | GETOPT="$(PATH=/usr/local/bin:${PATH} which getopt)" |
| 54 | options="$(${GETOPT} -o C:nh --long crl:,no-view-certificate,help -a -- "$@")" |
| 55 | # shellcheck disable=SC2181 |
| 56 | if [[ $? -ne 0 ]]; then |
| 57 | usage |
| 58 | fi |
| 59 | |
| 60 | CRL="" |
| 61 | VIEW_CERTIFICATE=1 |
| 62 | eval set -- "${options}" |
| 63 | while [ $# -gt 0 ] ; do |
| 64 | case "$1" in |
| 65 | -C | --crl) |
| 66 | CRL="$2" |
| 67 | shift 2 |
| 68 | ;; |
| 69 | -n | --no-view-certificate) |
| 70 | VIEW_CERTIFICATE=0 |
| 71 | shift |
| 72 | ;; |
| 73 | -h | --help) |
| 74 | usage |
| 75 | # shift |
| 76 | ;; |
| 77 | --) |
| 78 | shift |
| 79 | break |
| 80 | ;; |
| 81 | esac |
| 82 | done |
| 83 | if [ $# -lt 1 ] ; then |
| 84 | usage |
| 85 | fi |
| 86 | CA_FILE="$1" |
| 87 | shift |
| 88 | |
| 89 | # ====== Check availability of tools ======================================== |
| 90 | OPENSSL="$(which openssl || true)" |
| 91 | CCERTTOOL="$(which certtool || true)" |
| 92 | CERTUTIL="$(which certutil || true)" |
| 93 | CRLUTIL="$(which crlutil || true)" |
| 94 | |
| 95 | # ====== Look for CA certificate ============================================ |
| 96 | if [ ! -e "${CA_FILE}" ] ; then |
| 97 | eval_gettext >&2 "ERROR: Unable to find CA certificate file \${CA_FILE}!" |
| 98 | echo >&2 |
| 99 | exit 1 |
| 100 | fi |
| 101 | |
| 102 | # ====== Look for CRL ======================================================= |
| 103 | if [ "${CRL}" != "" ] && [ ! -e "${CRL}" ] ; then |
| 104 | eval_gettext >&2 "ERROR: Unable to find CRL file \${CRL}!" |
| 105 | echo >&2 |
| 106 | exit 1 |
| 107 | fi |
| 108 | |
| 109 | |
| 110 | # ====== Check each certificate ============================================= |
| 111 | errors=0 |
| 112 | while [ $# -gt 0 ] ; do |
| 113 | |
| 114 | # ====== Look for certificate ============================================ |
| 115 | CERTIFICATE_FILE="$1" |
| 116 | shift |
| 117 | if [ ! -e "${CERTIFICATE_FILE}" ] ; then |
| 118 | eval_gettext >&2 "ERROR: Unable to find certificate file \${CERTIFICATE_FILE}!" |
| 119 | echo >&2 |
| 120 | exit 1 |
| 121 | fi |
| 122 | |
| 123 | # ====== Display certificate and hierarchy ============================== |
| 124 | if [ ${VIEW_CERTIFICATE} -eq 1 ] ; then |
| 125 | "${DIRNAME}/view-certificate" "${CERTIFICATE_FILE}" |
| 126 | fi |
| 127 | |
| 128 | |
| 129 | # ====== Verify certificate with CA certificate ========================== |
| 130 | echo -e "\e[34m$(eval_gettext "Verifying \${CERTIFICATE_FILE} with CA \${CA_FILE} ...")\e[0m" |
| 131 | if [ "${CA_FILE}" == "${CERTIFICATE_FILE}" ] ; then |
| 132 | gettext >&2 "WARNING: Only checking self-signature!" |
| 133 | echo >&2 |
| 134 | fi |
| 135 | |
| 136 | # ====== OpenSSL ========================================================= |
| 137 | echo -en "\e[33mOpenSSL:\e[0m " |
| 138 | if [ "${OPENSSL}" != "" ] ; then |
| 139 | crlOption="" |
| 140 | if [ "${CRL}" != "" ] ; then |
| 141 | crlOption="-crl_check -CRLfile \"${CRL}\"" |
| 142 | fi |
| 143 | command="\"${OPENSSL}\" verify -no-CApath -no-CAstore -CAfile \"${CA_FILE}\" ${crlOption} -untrusted \"${CERTIFICATE_FILE}\" \"${CERTIFICATE_FILE}\"" |
| 144 | if sh -c "${command}" >/dev/null 2>&1 ; then |
| 145 | echo -e "\e[32;1m$(gettext "✓OKAY")\e[0m" |
| 146 | else |
| 147 | errors=$((errors + 1)) |
| 148 | echo -e "\e[31;1m$(gettext "❌FAILED!")\e[0m" |
| 149 | sh -c "${command}" || true |
| 150 | fi |
| 151 | else |
| 152 | gettext "(OpenSSL is not installed)" |
| 153 | echo |
| 154 | fi |
| 155 | |
| 156 | |
| 157 | # ====== GNU TLS ========================================================= |
| 158 | echo -en "\e[33mGNU TLS:\e[0m " |
| 159 | if [ "${CCERTTOOL}" != "" ] ; then |
| 160 | crlOption="" |
| 161 | if [ "${CRL}" != "" ] ; then |
| 162 | crlOption="--load-crl=\"${CRL}\"" |
| 163 | fi |
| 164 | profiles="low medium high ultra future" |
| 165 | good=0 |
| 166 | for profile in ${profiles} ; do |
| 167 | command="\"${CCERTTOOL}\" --verify --verify-profile=${profile} --load-ca-certificate=\"${CA_FILE}\" ${crlOption} --infile=\"${CERTIFICATE_FILE}\"" && sh -c "${command}" >/dev/null 2>&1 && |
| 168 | good=$((good + 1)) && \ |
| 169 | if [ ${good} -eq 1 ] ; then echo -en "\e[32;1m$(gettext "✓OKAY")\e[0m" ; fi && \ |
| 170 | echo -en " \e[32m${profile}\e[0m" |
| 171 | done |
| 172 | echo |
| 173 | if [ ${good} -eq 0 ] ; then |
| 174 | errors=$((errors + 1)) |
| 175 | echo -e "\e[31;1m$(gettext "❌FAILED!")\e[0m" |
| 176 | sh -c "${command}" || true |
| 177 | fi |
| 178 | else |
| 179 | gettext "(GnuTLS CertTool is not installed)" |
| 180 | echo |
| 181 | fi |
| 182 | |
| 183 | |
| 184 | # ====== NSS ============================================================= |
| 185 | echo -en "\e[33mNSS: \e[0m " |
| 186 | if [ "${CERTUTIL}" != "" ] && [ "${CRLUTIL}" != "" ] ; then |
| 187 | |
| 188 | # NOTE: |
| 189 | # NSS does not support importing a certificate with CAs bundled! It is |
| 190 | # necessary to extract each CA, and then process them individually! |
| 191 | |
| 192 | # ------ Create temporary directory for database ---------------------- |
| 193 | trap 'rm -rf "${db}"' EXIT |
| 194 | db=$(mktemp --tmpdir -d "check-certificate-nss.XXXXXXXXXX") |
| 195 | mkdir -p "${db}/tmp" |
| 196 | |
| 197 | "${CERTUTIL}" --empty-password -N -d "${db}" |
| 198 | if [ "${CA_FILE}" != "${CERTIFICATE_FILE}" ] ; then |
| 199 | "${CERTUTIL}" -A -n "My Root CA" -t "CT,C,C" -e -d "${db}" -a -i "${CA_FILE}" -u L |
| 200 | fi |
| 201 | "${CERTUTIL}" -A -n "My Certificate" -t ",," -e -d "${db}" -a -i "${CERTIFICATE_FILE}" |
| 202 | |
| 203 | # ------ Extract certificate and process each sub-CA individually ----- |
| 204 | "${DIRNAME}/extract-pem" "${CERTIFICATE_FILE}" --skip-first-entry \ |
| 205 | --quiet --output-prefix "${db}/tmp/certificate-" |
| 206 | subCA=0 |
| 207 | find "${db}/tmp" -name "certificate-[0-9]*.crt" | sort -r | \ |
| 208 | while read -r name ; do |
| 209 | subCA=$((subCA + 1)) |
| 210 | "${CERTUTIL}" -A -n "My Sub CA ${subCA}" -t ",," -e -d "${db}" -a -i "${name}" -u L |
| 211 | done |
| 212 | find "${db}/tmp" -name "certificate-[0-9]*.crl" |
| 213 | |
| 214 | # If the Root CA is in the certificate bundle, trust was reset to ",," |
| 215 | # => Set it as trusted again: |
| 216 | if [ "${CA_FILE}" != "${CERTIFICATE_FILE}" ] ; then |
| 217 | "${CERTUTIL}" -A -n "My Root CA" -t "CT,C,C" -e -d "${db}" -a -i "${CA_FILE}" -u L |
| 218 | fi |
| 219 | |
| 220 | # ------ Extract CRL file and process each CRL individually ----------- |
| 221 | if [ "${CRL}" != "" ] ; then |
| 222 | "${DIRNAME}/extract-pem" "${CRL}" --quiet --output-prefix "${db}/tmp/crl-" |
| 223 | |
| 224 | # NSS wants the CRL in DER instead of PEM format, i.e. it needs |
| 225 | # to be converted first. Finally, it can be imported by NSS. |
| 226 | find "${db}/tmp" -name "crl-*.crl" | \ |
| 227 | while read -r crl ; do |
| 228 | "${OPENSSL}" crl -outform der -in "${crl}" -out "${crl}.der" |
| 229 | "${CRLUTIL}" -I -d "${db}" -a -i "${crl}.der" |
| 230 | done |
| 231 | fi |
| 232 | |
| 233 | # "${CERTUTIL}" -L -d "${db}" |
| 234 | # "${CRLUTIL}" -L -d "${db}" |
| 235 | |
| 236 | # ------ Finally, it is possible to verify the validity chain --------- |
| 237 | declare -A types=( |
| 238 | ["server"]="V" |
| 239 | ["client"]="C" |
| 240 | ["CA"]="L" |
| 241 | ["email_signer"]="S" |
| 242 | ["email_recipient"]="R" |
| 243 | ) |
| 244 | good=0 |
| 245 | for type in "${!types[@]}" ; do |
| 246 | setting="${types[$type]}" |
| 247 | command="\"${CERTUTIL}\" -V -e -u \"${setting}\" -n \"My Certificate\" -d \"${db}\"" && sh -c "${command}" >/dev/null 2>&1 && \ |
| 248 | good=$((good + 1)) && \ |
| 249 | if [ ${good} -eq 1 ] ; then echo -en "\e[32;1m$(gettext "✓OKAY")\e[0m" ; fi && \ |
| 250 | echo -en " \e[36m${type}\e[0m" |
| 251 | done |
| 252 | echo |
| 253 | if [ ${good} -eq 0 ] ; then |
| 254 | errors=$((errors + 1)) |
| 255 | echo -e "\e[31;1m$(gettext "$(gettext "❌FAILED!")")\e[0m" |
| 256 | sh -c "${command}" || true |
| 257 | # "${CERTUTIL}" -L -d "${db}" |
| 258 | # "${CRLUTIL}" -L -d "${db}" |
| 259 | fi |
| 260 | else |
| 261 | gettext "(NSS CertUtil is not installed)" |
| 262 | echo |
| 263 | fi |
| 264 | |
| 265 | done |
| 266 | |
| 267 | # ====== Check for errors =================================================== |
| 268 | if [ ${errors} -gt 0 ] ; then |
| 269 | echo -e "\e[37;41;1m***** $(gettext "CERTIFICATE VERIFICATIONS FAILED!") *****\e[0m" |
| 270 | exit 1 |
| 271 | fi |