forked from raimue/ssl-cert-check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssl-cert-check
executable file
·146 lines (130 loc) · 4.89 KB
/
ssl-cert-check
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
#!/usr/bin/env bash
# vim: set fenc=utf-8:
# Copyright (c) 2013-2014, Rainer Müller <[email protected]>
# Copyright (c) 2015, Daniel Danner <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# https://raim.codingfarm.de/blog/tag/ssl-cert-check/
# https://github.com/raimue/ssl-cert-check
if [ $BASH_VERSINFO -lt 4 ]; then
echo "Error: this script requires bash >= 4.0" >&2
exit 3
fi
# First parameter specifies if certificate expire in the next X days
DAYS=$1
shift
if [[ ! $DAYS =~ ^[0-9]+$ ]]; then
echo "Error: missing parameter <days> or invalid number" >&2
exit 3
fi
if [[ "$1" == --list=* ]]; then
LIST=${1##--list=}
if [[ ! -r "$LIST" ]]; then
echo "Error: list is not a regular file" >&2
fi
readarray -t TARGETS < "$LIST"
else
TARGETS=("$@")
fi
# We need extended globbing
shopt -s extglob
exitcode=0
for cert in "${TARGETS[@]}"; do
enddate=""
# For ease of use, map any absolute path name to a file:// URL
if [[ $cert =~ ^/(.*)$ ]]; then
cert=file://$cert
fi
# Split URI into protocol and target
if [[ $cert =~ ^(.*)://(.*)$ ]]; then
proto=${BASH_REMATCH[1]}
target=${BASH_REMATCH[2]}
else
echo "Error: invalid certificate specification: $cert" >&2
if [ $exitcode -lt 2 ]; then
exitcode=2
fi
continue
fi
port=""
extra=""
case $proto in
file)
enddate=$(openssl x509 -checkend $(( 86400 * $DAYS )) -enddate -in "$target")
;;
!(ssl))
# Handle special protocol definition for STARTTLS
if [[ $proto =~ ^(.*)\+starttls$ ]]; then
proto=${BASH_REMATCH[1]}
extra="-starttls $proto"
fi
# If no port was given, use the default for this protocol
servername=$target
if [[ ! $target =~ :[0-9]+$ ]]; then
target+=:$proto
else
servername=${target%:*}
fi
# (intentional fallthrough)
;&
ssl)
# Retrieve certificate
# Create new temporary file
tempfile=$(mktemp -t ssl-cert-check.XXXXX)
if [ -z "$tempfile" ]; then
echo "Error: unable to create temporary file" >&2
if [ $exitcode -lt 2 ]; then
exitcode=2
fi
continue
fi
# Delete temporary file if shell exits during certificate check
trap "rm -f $tempfile" EXIT
cmd=(openssl s_client -connect "$target" -servername "$servername" $extra)
certificate=$(echo | "${cmd[@]}" 2>$tempfile \
| sed -n -e '/-----BEGIN CERTIFICATE-----/,/-----END CERTIFICATE-----/p' \
-e '/-----END CERTIFICATE-----/q')
if [ "$certificate" == "" ]; then
echo "Error: unable to check $cert" >&2
echo "Error: command was '${cmd[@]}', openssl error messages were:" >&2
sed 's/^/ /' $tempfile >&2
if [ $exitcode -lt 2 ]; then
exitcode=2
fi
continue
else
# Extract notAfter date of validity
enddate=$(echo "$certificate" | openssl x509 -checkend $(( 86400 * $DAYS )) -enddate)
fi
rm -f $tempfile
;;
esac
if [[ $enddate =~ (.*)Certificate\ will\ expire ]]; then
echo "==> Certificate $cert is about to expire soon:"
echo -n " ${BASH_REMATCH[1]}"
if [ $exitcode -lt 1 ]; then
exitcode=1
fi
fi
done
exit $exitcode