Debian Codegraph @

src/TestDB/view-certificate

raw symbols SCIP
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 Viewer
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="view-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 [-h|--help] certificate [...]"
45 exit 1
46 }
47
48
49 # ###### Main program #######################################################
50
51 # ====== Handle arguments ===================================================
52 GETOPT="$(PATH=/usr/local/bin:${PATH} which getopt)"
53 options="$(${GETOPT} -o h --long help -a -- "$@")"
54 # shellcheck disable=SC2181
55 if [[ $? -ne 0 ]]; then
56 usage
57 fi
58
59 eval set -- "${options}"
60 while [ $# -gt 0 ] ; do
61 case "$1" in
62 -h | --help)
63 usage
64 # shift
65 ;;
66 --)
67 shift
68 break
69 ;;
70 esac
71 done
72 if [ $# -lt 1 ] ; then
73 usage
74 fi
75
76 # ====== Check availability of tools ========================================
77 OPENSSL="$(which openssl || true)"
78 if [ "${OPENSSL}" == "" ] ; then
79 gettext >&2 "ERROR: OpenSSL is not installed!"
80 echo >&2
81 exit 1
82 fi
83
84 # ====== Check each certificate =============================================
85 while [ $# -gt 0 ] ; do
86
87 # ====== Look for certificate ============================================
88 CERTIFICATE_FILE="$1"
89 shift
90 if [ ! -e "${CERTIFICATE_FILE}" ] ; then
91 eval_gettext >&2 "ERROR: Unable to find certificate file ${CERTIFICATE_FILE}!"
92 echo >&2
93 exit 1
94 fi
95
96 # ====== Display certificate details =====================================
97 echo -e "\e[34m$(eval_gettext "Certificate in \${CERTIFICATE_FILE}:")\e[0m"
98 "${OPENSSL}" x509 -in "${CERTIFICATE_FILE}" -noout -subject -dates -ext subjectAltName,basicConstraints,keyUsage,extendedKeyUsage,subjectKeyIdentifier,authorityKeyIdentifier | (
99 validityFrom="?"
100 validityTo="?"
101 subject="?"
102 declare -A extensions=(
103 ["Subject Alternative Name"]="(none)"
104 ["Key Usage"]="(none)"
105 ["Basic Constraints"]="(none)"
106 ["Extended Key Usage"]="(none)"
107 ["Subject Key Identifier"]="(none)"
108 ["Authority Key Identifier"]="(none)"
109 )
110 while read -r line ; do
111 if [[ "${line}" =~ ^(subject=)(.*)$ ]] ; then
112 # Subject with underlined CN:
113 # shellcheck disable=SC2001
114 subject="$(echo "${BASH_REMATCH[2]}" | sed -e 's/\(CN = \)\([^,]*\)/\x1b[4m\1\2\x1b[24m/')"
115 elif [[ "${line}" =~ ^(notBefore=)(.*)$ ]] ; then
116 validityFrom="${BASH_REMATCH[2]}"
117 elif [[ "${line}" =~ ^(notAfter=)(.*)$ ]] ; then
118 validityTo="${BASH_REMATCH[2]}"
119 elif [[ "${line}" =~ ^([ \t]*)(X509v3 )(.*)(:[[:space:]]*)(.*)$ ]] ; then
120 extension="${BASH_REMATCH[3]}"
121 critical="${BASH_REMATCH[5]}"
122 if [ "${critical}" != "" ] ; then
123 extensions["${extension}"]="\\e[4m${critical}:\\e[24m "
124 else
125 extensions["${extension}"]=""
126 fi
127 elif [ "${extension}" != "" ] && [[ "${line}" =~ ^([[:space:]]*)(.*)$ ]] ; then
128 extensions["${extension}"]+="${BASH_REMATCH[2]}"
129 extension=""
130 fi
131 done
132 label="$(gettext "%-25s")"
133 printf "\e[33m${label} \e[35m%s\e[0m\n" \
134 "$(gettext "Subject:")" "$(echo -e "${subject}")"
135 printf "\e[33m${label} \e[35m%s\e[0m\n" \
136 "$(gettext "Subject Alt Name:")" "${extensions["Subject Alternative Name"]}"
137 printf "\e[33m${label} \e[36m%s — %s\e[0m\n" \
138 "$(gettext "Validity:")" "${validityFrom}" "${validityTo}"
139 for extension in "Basic Constraints" \
140 "Key Usage" "Extended Key Usage" \
141 "Subject Key Identifier" "Authority Key Identifier" ; do
142 printf "\e[33m${label} \e[33m%s\e[0m\n" \
143 "${extension}:" "$(echo -e "${extensions["${extension}"]}")"
144 done
145 )
146 echo -en "\e[0m"
147
148 # ====== Display certificate hierarchy ===================================
149 echo -e "\e[34m$(eval_gettext "Hierarchy in \${CERTIFICATE_FILE}"):\e[0m"
150 "${OPENSSL}" crl2pkcs7 -nocrl -certfile "${CERTIFICATE_FILE}" | \
151 "${OPENSSL}" pkcs7 -print_certs -noout | (
152 entry=0
153 while read -r line ; do
154 if [[ "${line}" =~ ^(subject=)(.*)$ ]] ; then
155 # Subject with underlined CN:
156 # shellcheck disable=SC2001
157 subject="$(echo "${BASH_REMATCH[2]}" | sed -e 's/\(CN = \)\([^,]*\)/\x1b[4m\1\2\x1b[24m/')"
158
159 entry=$((entry + 1))
160 echo -en " \x1b[33m$(eval_gettext "#\${entry}.") "
161 if [ ${entry} -eq 1 ] ; then
162 echo -e "\e[35m${subject}\e[0m"
163 else
164 echo -e "\e[37m${subject}\e[0m"
165 fi
166 fi
167 done
168 )
169
170 done