-
Notifications
You must be signed in to change notification settings - Fork 1
/
check-certificates.sh
executable file
·250 lines (243 loc) · 7.96 KB
/
check-certificates.sh
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
#! /usr/bin/env zsh
#
# array's to store certificate info by key and DN
#
initializeArrays() {
declare -g -A fileByKey=()
declare -g -A fileByDn=()
declare -g -A subjectDnByKey=()
declare -g -A subjectDnByDn=()
declare -g -A issuerDnByKey=()
declare -g -A issuerDnByDn=()
declare -g -A issuerKeyByKey=()
declare -g -A issuerKeyByDn=()
declare -g -A notBeforeByKey=()
declare -g -A notBeforeByDn=()
declare -g -A notAfterByKey=()
declare -g -A notAfterByDn=()
}
#
# load all information from given certificate and add to environment
#
getCertificateDetails() {
unset subjectKey authorityKey issuer subject notBefore notAfter
tmpfile=`mktemp`
openssl x509 -noout -text -in "$1" |
awk '
/X509v3 Subject Key Identifier:/ {
getline;
printf "subjectKey=\"%s\"\n", $1
}
/X509v3 Authority Key Identifier:/ {
getline;
sub("keyid:", "");
printf "authorityKey=\"%s\"\n", $1
}
/Issuer:/{
sub("^ *Issuer: ", "");
printf "issuer=\"%s\"\n", $0
}
/Subject:/{
sub("^ *Subject: ", "");
printf "subject=\"%s\"\n", $0
}
/Not Before:/{
sub("^ *Not Before: ","");
printf "notBefore=\"%s\"\n", $0
}
/Not After :/{
sub("^ *Not After : ","");
printf "notAfter=\"%s\"\n", $0
}
' >$tmpfile
. $tmpfile
rm $tmpfile
}
#
# load all .chain certificates from given directory
#
loadChainCertificates() {
if test ! -d "$1"
then
echo "$1" is not a directory
exit 1
fi
find "$1" -maxdepth 1 -name '*.chain' -print |
while read file
do
#
# skip if certificate is not a file (also works for broken symlinks)
#
if ! test -f "$file"
then
continue
fi
getCertificateDetails "$file"
if test -z "$subjectKey"
then
fileByDn["$subject"]="$file"
subjectDnByDn["$subject"]="$subject"
issuerDnByDn["$subject"]="$issuer"
notBeforeByDn["$subject"]="$notBefore"
notAfterByDn["$subject"]="$notAfter"
else
fileByKey["$subjectKey"]="$file"
fileByDn["$subject"]="$file"
subjectDnByKey["$subjectKey"]="$subject"
subjectDnByDn["$subject"]="$subject"
issuerDnByKey["$subjectKey"]="$issuer"
issuerDnByDn["$subject"]="$issuer"
issuerKeyByKey["$subjectKey"]="$authorityKey"
issuerKeyByDn["$subject"]="$authorityKey"
notBeforeByKey["$subjectKey"]="$notBefore"
notBeforeByDn["$subject"]="$notBefore"
notAfterByKey["$subjectKey"]="$notAfter"
notAfterByDn["$subject"]="$notAfter"
fi
done
}
#
# test if 'not After' date is past now
#
certificateExpired () {
cert=`date -j -f '%b %e %H:%M:%S %Y %Z' '+%s' "$1"`
now=`date '+%s'`
[ $now -ge $cert ]
}
#
# for all .crt in given directory verify that there is a complate set of .chain
# certificates and that all are not expired
#
verifyCertificateChains() {
if test ! -d "$1"
then
echo "$1" is not a directory
exit 1
fi
totalNumberOfCertificates=0
totalNumberOfCompleteChains=0
find "$1" -maxdepth 1 -name '*.crt' -print |
while read file
do
#
# skip if certificate is not a file (also works for broken symlinks)
#
if ! test -f "$file"
then
continue
fi
#
# get and print certificate info
#
indent=""
((totalNumberOfCertificates++))
getCertificateDetails "$file"
print "$subject"
test -n "$subjectKey" && print "$subjectKey"
print "$file"
if certificateExpired "$notAfter"
then
print -P "%F{red}${indent}not valid after $notAfter%F{none}"
continue
else
print "${indent}$notAfter"
fi
((totalNumberOfCompleteChains++))
#
# get and print chain info
#
while true; do
indent+=" "
if test -z "$subjectDnByKey["$authorityKey"]" -a -z "$subjectDnByDn["$issuer"]"
then
print -P "%F{red}${indent}cannot find: $issuer ($authorityKey)%F{none}"
((totalNumberOfCompleteChains--))
break
fi
if test -z "$authorityKey"
then
print -P "%F{green}${indent}$issuer%F{none}"
print -P "%F{green}${indent}$fileByDn["$issuer"]%F{none}"
if certificateExpired "$notAfterByDn["$issuer"]"
then
print -P "%F{red}${indent}not valid after $notAfterByDn["$issuer"]%F{none}"
((totalNumberOfCompleteChains--))
break
else
print -P "%F{green}${indent}$notAfterByDn["$issuer"]%F{none}"
fi
subjectKey=$authorityKey
subject="$issuer"
authorityKey=$issuerKeyByKey["$authorityKey"]
issuer=$issuerDnByDn["$issuer"]
test "$issuer" != "$subject" || break
else
print -P "%F{green}${indent}$subjectDnByKey["$authorityKey"]%F{none}"
print -P "%F{green}${indent}$authorityKey%F{none}"
print -P "%F{green}${indent}$fileByKey["$authorityKey"]%F{none}"
if certificateExpired "$notAfterByKey["$authorityKey"]"
then
print -P "%F{red}${indent}not valid after $notAfterByKey["$authorityKey"]%F{none}"
((totalNumberOfCompleteChains--))
break
else
print -P "%F{green}${indent}$notAfterByKey["$authorityKey"]%F{none}"
fi
subject=$subjectDnByKey["$authorityKey"]
issuer=$issuerDnByKey["$authorityKey"]
subjectKey=$authorityKey
authorityKey=$issuerKeyByKey["$authorityKey"]
if test -z "$authorityKey"
then
test "$issuer" != "$subject" || break
else
test "$authorityKey" != "$subjectKey" || break
fi
fi
done
done
((totalNumberOfCertificates==totalNumberOfCompleteChains)) && print -P -n "%F{green}" || print -P -n "%F{red}"
print -P "$1: $totalNumberOfCompleteChains/$totalNumberOfCertificates complete chains%F{none}"
}
#
# Main
#
# parse comand line arguments
#
checkDirectory=""
while getopts ":d:" opt
do
case ${opt} in
d )
checkDirectory="${OPTARG}"
;;
":" )
echo "option -${OPTARG} requires an argument"
exit 1
;;
\? )
echo "Usage: `basename $0` [-d <certificate directory>]"
exit 0
;;
esac
done
shift $((OPTIND -1))
#
# if there is no specific directory to check then try to find and verify all
# directories with trusted certificates
#
if [ -n "$checkDirectory" ]
then
initializeArrays
loadChainCertificates "$checkDirectory"
verifyCertificateChains "$checkDirectory"
else
#set -e
for dir in config/*/certificates/trust
do
#set +e
initializeArrays
loadChainCertificates "$dir"
verifyCertificateChains "$dir"
done
fi