forked from redredgroovy/easy-ca
-
Notifications
You must be signed in to change notification settings - Fork 1
/
show-status
executable file
·264 lines (212 loc) · 8.69 KB
/
show-status
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/bin/bash
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# Christian Göttsche <[email protected]>
set -eu
set -o pipefail
umask 0077
BIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# shellcheck disable=SC1090
source "$BIN_DIR/functions"
# shellcheck disable=SC1090
source "$BIN_DIR/defaults.conf"
ROOT_DIR="$(readlink -e "$BIN_DIR/..")"
echo -e "$NOTE Checking directory $ROOT_DIR ..."
if [ -L "$BIN_DIR/functions" ]; then
echo -e "$NOTE Symlinked toolchain (dev mode)"
else
echo -e "$NOTE Standalone toolchain"
fi
# check directory structure
if [ ! -d bin/templates ]; then
echo -e "$ERR No 'bin/templates' directory!";
exit 2
fi
if [ ! -d ca ]; then
echo -e "$ERR No 'ca' directory!";
exit 2
fi
if [ ! -f ca/ca.conf ]; then
echo -e "$ERR No 'ca/ca.conf' file!";
exit 2
fi
if [ ! -f ca/ca.crt ]; then
echo -e "$ERR No 'ca/ca.crt' file!";
exit 2
fi
if [ ! -f ca/ca.csr ]; then
echo -e "$ERR No 'ca/ca.csr' file!";
exit 2
fi
if [ ! -f ca/chain.pem ]; then
echo -e "$ERR No 'ca/chain.pem' file!'";
exit 2
fi
if [ ! -f ca/root.crt ]; then
echo -e "$ERR No 'ca/root.crt' file!";
exit 2
fi
if [ ! -d ca/archive ]; then
echo -e "$ERR No 'ca/archives' directory!";
exit 2
fi
if [ ! -d ca/crl ]; then
echo -e "$ERR No 'ca/crl' directory!";
exit 2
fi
if [ ! -d ca/db ]; then
echo -e "$ERR No 'ca/db' directory!";
exit 2
fi
if [ ! -f ca/db/certificate.db ]; then
echo -e "$ERR No 'ca/db/certificate.db' file!";
exit 2
fi
if [ ! -f ca/db/certificate.db.attr ]; then
echo -e "$ERR No 'ca/db/certificate.db.attr' file!";
exit 2
fi
if [ ! -f ca/db/crl.srl ]; then
echo -e "$ERR No 'ca/db/crl.srl' file!";
exit 2
fi
if [ ! -f ca/db/crt.srl ]; then
echo -e "$ERR No 'ca/db/crt.srl' file!";
exit 2
fi
if [ ! -f ca/private/ca.key ]; then
echo -e "$ERR No 'ca/private/ca.key' file!";
exit 2
fi
if [ ! -d ca/private ]; then
echo -e "$ERR No 'ca/private' directory!";
exit 2
fi
if [ ! -d certs ]; then
echo -e "$ERR No 'certs' directory!";
exit 2
fi
CA_CRT="$ROOT_DIR/ca/ca.crt"
if openssl verify -CAfile "$CA_CRT" "$CA_CRT" >/dev/null 2>&1; then
echo -e "$SUCC RootCA directory (found self-signed CA)"
if [ ! -f "$ROOT_DIR/ca/archive/01.pem" ] || ! cmp "$CA_CRT" "$ROOT_DIR/ca/archive/01.pem" >/dev/null; then
echo -e "$ERR Main certificate does not match first archive entry!"
exit 2
fi
rootCA=1 #signs itself
else
echo -e "$SUCC SignCA directory (found signed CA)"
rootCA=0
fi
echo -e "$NOTE Sign CA details"
echo -e "$NOTE Name $CA_NAME"
echo -e "$NOTE Domain $CA_DOMAIN"
subject=$(openssl x509 -noout -in "$CA_CRT" -subject) # ignore subject=
echo -e "$NOTE Subject ${subject:8}"
if [ ! $rootCA -eq 1 ]; then
issuer=$(openssl x509 -noout -in "$CA_CRT" -issuer) # ignore issuer=
echo -e "$NOTE Issuer ${issuer:7}"
fi
enddate=$(openssl x509 -noout -in "$CA_CRT" -enddate) # ignore notAfter=
echo -e -n "$NOTE Valid until ${enddate:9}"
if ! openssl x509 -noout -in "$CA_CRT" -checkend 15768000 > /dev/null; then # 1/2 year
echo -e -n " - \033[33;1mabout to expire\033[0m"
else
echo
fi
echo -e "$NOTE Bits $(openssl x509 -noout -in "$CA_CRT" -text -certopt no_header,no_aux,no_extensions,no_version,no_serial,no_issuer,no_validity,no_subject,no_signame,no_sigdump | grep 'Public-Key:' | cut -d ':' -f2 | tr -d "()")"
srl=$(cat "$ROOT_DIR/ca/db/crt.srl")
certs=$((16#$srl - rootCA - 1))
echo -e "$NOTE Signed Certificates " $certs
if ! openssl crl -in "$ROOT_DIR/ca/ca.crl" -CAfile "$CA_CRT" >/dev/null 2>&1; then
echo -e "$ERR Certificate revocation list not valid!"
exit 2
fi
if [ "$(openssl crl -in "$ROOT_DIR/ca/ca.crl" -text -noout | grep -cE '^No Revoked Certificates.')" -eq 1 ]; then
revoked_certs=0
else
revoked_certs=$(openssl crl -in "$ROOT_DIR/ca/ca.crl" -text -noout | grep -cE '^\s+Revocation Date:')
fi
echo -e "$NOTE Revoked Certificates $revoked_certs"
revoked_lists=$(($(cat "$ROOT_DIR/ca/db/crl.srl") - 1))
echo -e "$NOTE Revoke Certificates Lists " $revoked_lists
if [ $rootCA -eq 0 ]; then
ROOT_CRT="$ROOT_DIR/ca/root.crt"
echo -e "$NOTE Root CA details"
root_subject=$(openssl x509 -noout -in "$ROOT_CRT" -subject) # ignore subject=
echo -e "$NOTE Subject ${root_subject:8}"
root_enddate=$(openssl x509 -noout -in "$ROOT_CRT" -enddate) # ignore notAfter=
echo -e -n "$NOTE Valid until ${root_enddate:9}"
if ! openssl x509 -noout -in "$ROOT_CRT" -checkend 15768000 > /dev/null; then # 1/2 year
echo -e -n " - \033[33;1mabout to expire\033[0m"
else
echo
fi
echo -e "$NOTE Bits $(openssl x509 -noout -in "$ROOT_CRT" -text -certopt no_header,no_aux,no_extensions,no_version,no_serial,no_issuer,no_validity,no_subject,no_signame,no_sigdump | grep 'Public-Key:' | cut -d ':' -f2 | tr -d "()")"
fi
skip=$rootCA
if [ $certs -gt 0 ]; then
num=1
echo
while read -r line; do
if [ $skip -eq 1 ]; then
skip=0
else
if [[ "$line" == "V"* ]]; then
ca_number=$(echo "$line" | tr -s ' \t' ' ' | cut -d ' ' -f 3)
revoked="no"
else
ca_number=$(echo "$line" | tr -s ' \t' ' ' | cut -d ' ' -f 4)
revoked="yes"
fi
pem_file="$ROOT_DIR/ca/archive/$ca_number.pem"
ca_cname=$(openssl x509 -noout -subject -in "$pem_file" -nameopt multiline | grep -E '^\s+commonName' | cut -d '=' -f 2-)
server_cert=$(openssl x509 -noout -in "$pem_file" -ext extendedKeyUsage | grep -c 'TLS Web Server Authentication') || true
sign_cert=$(openssl x509 -noout -in "$pem_file" -ext keyUsage | grep -c 'Certificate Sign') || true
client_cert=$(openssl x509 -noout -in "$pem_file" -ext extendedKeyUsage | grep -c 'TLS Web Client Authentication') || true
valid_since=$(openssl x509 -noout -in "$pem_file" -startdate) # ignore notBefore=
valid_until=$(openssl x509 -noout -in "$pem_file" -enddate) # ignore notAfter=
bits=$(openssl x509 -noout -in "$pem_file" -text -certopt no_header,no_aux,no_extensions,no_version,no_serial,no_issuer,no_validity,no_subject,no_signame,no_sigdump | grep 'Public-Key:' | cut -d ':' -f2 | tr -d "()")
subjectAltName=$(openssl x509 -noout -in "$pem_file" -ext subjectAltName)
subjectAltName=$(grep -vE '^X509v3 Subject Alternative Name:\s*$' <<< "$subjectAltName" | sed -e 's/^[[:space:]]*//')
if [ "$server_cert" -ge 1 ]; then
type="server"
san=" SAN: $subjectAltName\n"
elif [ "$sign_cert" -ge 1 ]; then
type="signing"
san=""
elif [ "$client_cert" -ge 1 ]; then
type="client"
san=" SAN: $subjectAltName\n"
else
echo -e "$ERR unknown certificate type!!"
exit 2
fi
sha1_hash=$(openssl x509 -noout -fingerprint -sha1 -in "$ROOT_DIR/ca/archive/$ca_number.pem") # ignore SHA1 Fingerprint=
subj=$(openssl x509 -noout -in "$ROOT_DIR/ca/archive/$ca_number.pem" -subject) # ignore subject=
echo -e -n "$NOTE $ca_number - $type -$ca_cname"
if ! openssl x509 -noout -in "$pem_file" -checkend 0 > /dev/null; then
echo -e -n " - \033[33;1mexpired\033[0m"
elif ! openssl x509 -noout -in "$pem_file" -checkend 2592000 > /dev/null; then # 30 days
echo -e -n " - \033[33;1mabout to expire\033[0m"
fi
if [ $revoked = "yes" ]; then
echo -e " - \033[33;1mrevoked\033[0m"
else
echo
fi
echo -e " Subject: ${subj:8}"
echo -e -n "$san"
echo -e " Revoked: $revoked"
echo -e " Valid since: ${valid_since:10}"
echo -e " Valid until: ${valid_until:9}"
echo -e " Bits: $bits"
echo -e " Type: $type"
echo -e " SHA1 hash: ${sha1_hash:17}"
echo -e ""
fi
num=$((num+1))
done < "$ROOT_DIR/ca/db/certificate.db"
fi
echo -e "$SUCC Status finished."