-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathforeman-certificate-check
More file actions
executable file
·318 lines (282 loc) · 9.54 KB
/
Copy pathforeman-certificate-check
File metadata and controls
executable file
·318 lines (282 loc) · 9.54 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env bash
# Define colors uses for status output
RED=$(tput setaf 1 2> /dev/null)
GREEN=$(tput setaf 2 2> /dev/null)
RESET=$(tput sgr0 2> /dev/null)
CABUNDLE_MAX_ISSUERS=32
function usage () {
cat <<HELP >&2
Verifies, that custom SSL certificate files are usable as part of the installation.
When passing filenames use absolute paths.
usage: $0 -c CERT_FILE -k KEY_FILE -b CA_BUNDLE_FILE
HELP
}
while getopts "c:k:b:" opt; do
case $opt in
c)
CERT_FILE="$(readlink -f $OPTARG)"
;;
k)
KEY_FILE="$(readlink -f $OPTARG)"
;;
b)
CA_BUNDLE_FILE="$(readlink -f $OPTARG)"
;;
h)
usage
exit 0
;;
?)
usage
exit 1
;;
esac
done
EXIT_CODE=0
if [ -z "$CERT_FILE" -o -z "$KEY_FILE" -o -z "$CA_BUNDLE_FILE" ]; then
echo 'One of the required parameters is missing.' >&2
usage
exit 1
fi
function error () {
echo -e "\n${RED}[FAIL]${RESET}\n"
CURRENT_EXIT_CODE=$1
EXIT_CODE=$((EXIT_CODE|CURRENT_EXIT_CODE))
echo -e $2 >&2
}
function success () {
echo -e "\n${GREEN}[OK]${RESET}\n"
}
function check-files-exist() {
if [[ ! -f "$CA_BUNDLE_FILE" ]] || [[ ! -f "$CERT_FILE" ]] || [[ ! -f "$KEY_FILE" ]] ; then
echo "One of '${CERT_FILE}', '${KEY_FILE}' or '${CA_BUNDLE_FILE}' not found" >&2
exit 1
fi
}
function check-server-cert-encoding () {
printf 'Checking server certificate encoding: '
openssl x509 -inform pem -in $CERT_FILE -noout -text &> /dev/null
if [[ $? == "0" ]]; then
success
else
openssl x509 -inform der -in $CERT_FILE -noout -text &> /dev/null
if [[ $? == "0" ]]; then
error 8 "The server certificate is in DER encoding, which is incompatible.\n\n"
printf "Run the following command to convert the certificate to PEM encoding,\n"
printf "then test it again.\n"
printf "# openssl x509 -in %s -outform pem -out %s.pem\n\n" $(basename $CERT_FILE) $(basename $CERT_FILE)
printf "When you run $(basename $0) again, use file\n"
printf "%s.pem for the server certificate.\n\n" $(basename $CERT_FILE)
else
error 9 "The encoding of the server certificate is unknown."
fi
fi
}
function check-expiration () {
CERT_EXP=$(openssl x509 -noout -enddate -in $CERT_FILE | sed -e 's/notAfter=//' | awk '{$NF="";}1')
CA_EXP=$(openssl x509 -noout -enddate -in $CA_BUNDLE_FILE | sed -e 's/notAfter=//' | awk '{$NF="";}1')
DATE_TODAY=$(date -u +%Y%m%d%H%M%S)
CERT_DATE=$(date -d"${CERT_EXP}" +%Y%m%d%H%M%S)
CA_DATE=$(date -d"${CA_EXP}" +%Y%m%d%H%M%S)
printf "Checking expiration of certificate: "
if [[ $DATE_TODAY -gt $CERT_DATE ]]; then
error 6 "The certificate \"$CERT_FILE\" has already expired on: $CERT_EXP"
else
success
fi
printf "Checking expiration of CA bundle: "
if [[ $DATE_TODAY -gt $CA_DATE ]]; then
error 7 "The CA bundle \"$CA_BUNDLE_FILE\" has already expired on: $CA_EXP"
else
success
fi
}
function check-cert-ca-flag () {
printf "Checking if server certificate has CA:TRUE flag "
openssl x509 -in $CERT_FILE -noout -text | grep -q CA:TRUE
if [[ $? -ne 0 ]]; then
success
else
error 7 "The server certificate is marked as a CA and can not be used."
fi
}
function check-passphrase () {
printf "Checking for private key passphrase: "
CHECK=$(cat $KEY_FILE | grep ENCRYPTED)
if [[ $? == "0" ]]; then
error 2 "The $KEY_FILE contains a passphrase, remove the key's passphrase by doing:
\nmv $KEY_FILE $KEY_FILE.old
\nopenssl pkey -in $KEY_FILE.old -out $KEY_FILE"
exit 2
else
success
fi
}
function check-priv-key () {
printf "Checking to see if the private key matches the certificate: "
CERT_PUBKEY_SUM=$(openssl x509 -noout -pubkey -in $CERT_FILE | openssl sha512)
KEY_PUBKEY_SUM=$(openssl pkey -pubout -in $KEY_FILE | openssl sha512)
if [[ "$CERT_PUBKEY_SUM" != "$KEY_PUBKEY_SUM" ]]; then
error 2 "The $KEY_FILE does not match the $CERT_FILE"
else
success
fi
}
function check-ca-bundle () {
printf "Checking CA bundle against the certificate file: "
ERROR_PATTERN="error [0-9]+ at"
CHECK=$(openssl verify -no-CApath -no-CAstore -CAfile $CA_BUNDLE_FILE -purpose sslserver -verbose $CERT_FILE 2>&1)
CHECK_STATUS=$?
if [[ $CHECK_STATUS != "0" || $CHECK =~ $ERROR_PATTERN ]]; then
error 4 "The $CA_BUNDLE_FILE does not verify the $CERT_FILE"
echo -e "${CHECK/OK/}\n"
else
success
fi
}
function check-ca-bundle-size () {
printf "Checking CA bundle size: "
CHECK=$(grep -c "^--*BEGIN" $CA_BUNDLE_FILE)
printf $CHECK
if [[ $CHECK -lt $CABUNDLE_MAX_ISSUERS ]]; then
success
else
CERRTISSUER=$(openssl x509 -noout -in $CERT_FILE -issuer 2>&1)
error 10 "The CA bundle counts $CHECK issuers. Please trim your CA bundle and include only the certs relevant to your cert file"
echo $CERTISSUER
echo
fi
}
function check-ca-bundle-trust-rules () {
printf "Checking if CA bundle has trust rules: "
CHECK=$(grep 'BEGIN TRUSTED CERTIFICATE' $CA_BUNDLE_FILE| wc -l)
printf $CHECK
if [[ $CHECK -lt 1 ]]; then
success
else
error 10 "The CA bundle contains $CHECK certificate(s) with trust rules. This will create problems for older systems. Please, recreate the bundle using certificates without trust rules."
echo
fi
}
function check-cert-san () {
printf "Checking Subject Alt Name on certificate "
DNS_LIST=$(openssl x509 -noout -text -in $CERT_FILE | grep DNS:)
if [[ $? == "0" ]]; then
success
printf "Checking if any Subject Alt Name on certificate matches the Subject CN"
SUBJECT_CN=$(openssl x509 -in $CERT_FILE -noout -subject -nameopt multiline|grep commonName|cut -d '=' -f 2|tr -d ' ')
for DNS in ${DNS_LIST}
do
DNS_VALUE="$( echo ${DNS//DNS:/} | tr -d ',')"
if [ $SUBJECT_CN == $DNS_VALUE ]
then
success
return
fi
done
error 11 "The $CERT_FILE does not have a Subject Alt Name matching the Subject CN"
else
error 11 "The $CERT_FILE does not contain a Subject Alt Name. Common Name is deprecated, use Subject Alt Name instead. See: https://tools.ietf.org/html/rfc2818#section-3.1"
fi
}
function check-cert-usage-key-encipherment () {
printf "Checking Key Usage extension on certificate for Key Encipherment "
CHECK=$(openssl x509 -noout -text -in $CERT_FILE | grep -A1 'X509v3 Key Usage:' | grep 'Key Encipherment')
if [[ $? == "0" ]]; then
success
else
error 4 "The $CERT_FILE does not allow for the 'Key Encipherment' key usage."
fi
}
function check-shortname () {
printf "Checking for use of shortname as CN"
SUBJECT_CN=$(openssl x509 -in $CERT_FILE -noout -subject -nameopt multiline|grep commonName|cut -d '=' -f 2|tr -d ' ')
if [[ $SUBJECT_CN != *"."* ]]; then
error 1 "The $(basename $CERT_FILE) is using a shortname for Common Name (CN) and cannot be used.\n"
fi
DNS_LIST=$(openssl x509 -noout -text -in $CERT_FILE | grep DNS:)
if [[ $? == "0" ]]; then
for DNS in ${DNS_LIST}
do
DNS_VALUE="$( echo ${DNS//DNS:/} | tr -d ',')"
if [[ $DNS_VALUE == *"."* ]]; then
success
return
fi
done
error 1 "The $(basename $CERT_FILE) is using only shortnames for Subject Alt Name and cannot be used.\n"
fi
}
function check-ca-signing-algorithm () {
printf "Checking CA signing algorithm for sha1: "
CHECK=$(openssl crl2pkcs7 -nocrl -certfile "$CA_BUNDLE_FILE" | openssl pkcs7 -print | grep algorithm | grep -q 'sha1WithRSAEncryption')
if [[ $? == "0" ]]; then
error 4 "The file '$CA_BUNDLE_FILE' contains a certificate signed with sha1 and will break installation. Update the server CA certificate and its chain with one signed by sha256 or stronger."
else
success
fi
}
function check-pem-content () {
local FILE=$1
local TYPE=$2
printf "Checking ${TYPE} for non-PEM content: "
if awk '
BEGIN {
in_cert = 0
bad = 0
}
/^-----BEGIN (TRUSTED )?CERTIFICATE-----$/ {
if (in_cert) {
bad = 1
}
in_cert = 1
next
}
/^-----END (TRUSTED )?CERTIFICATE-----$/ {
if (!in_cert) {
bad = 1
}
in_cert = 0
next
}
/^[[:space:]]*$/ {
next
}
{
if (!in_cert) {
bad = 1
}
}
END {
if (in_cert) {
bad = 1
}
exit bad
}
' "$FILE"
then
success
else
error 12 "The ${TYPE} '$FILE' contains content outside PEM CERTIFICATE blocks. Only PEM CERTIFICATE blocks are allowed."
fi
}
check-files-exist
check-pem-content "$CERT_FILE" "certificate file"
check-pem-content "$CA_BUNDLE_FILE" "CA bundle"
check-server-cert-encoding
check-expiration
check-cert-ca-flag
check-passphrase
check-priv-key
check-ca-bundle
check-ca-bundle-size
check-ca-bundle-trust-rules
check-cert-san
check-cert-usage-key-encipherment
check-shortname
check-ca-signing-algorithm
if [[ $EXIT_CODE == "0" ]]; then
echo -e "${GREEN}Validation succeeded${RESET}\n"
else
exit $EXIT_CODE
fi