-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssl-cert-info.sh
236 lines (198 loc) · 7.23 KB
/
ssl-cert-info.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
#!/usr/bin/env bash
## Shell script to check SSL certificate info like expiration date and subject.
## From https://gist.github.com/stevenringo/2fe5000d8091f800aee4bb5ed1e800a6
## Taken from http://giantdorks.org/alain/shell-script-to-check-ssl-certificate-info-like-expiration-date-and-subject/
usage()
{
cat <<EOF
Usage: $(basename "$0") [options]
This shell script is a simple wrapper around the openssl binary. It uses
s_client to get certificate information from remote hosts, or x509 for local
certificate files. It can parse out some of the openssl output or just dump all
of it as text.
Options:
--all-info Print all output, including boring things like Modulus and
Exponent.
--alt Print Subject Alternative Names. These will be typically be
additional hostnames that the certificate is valid for.
--cn Print commonName from Subject. This is typically the host for
which the certificate was issued.
--debug Print additional info that might be helpful when debugging this
script.
--end Print certificate expiration date. For additional functionality
related to certificate expiration, take a look at this script:
"http://prefetch.net/code/ssl-cert-check".
--dates Print start and end dates of when the certificate is valid.
--file Use a local certificate file for input.
--help Print this help message.
--host Fetch the certificate from this remote host.
--name Specify a specific domain name (Virtual Host) along with the
request. This value will be used as the '-servername' in the
s_client command. This is for TLS SNI (Server Name Indication).
--issuer Print the certificate issuer.
--most-info Print almost everything. Skip boring things like Modulus and
Exponent.
--option Pass any openssl option through to openssl to get its raw
output.
--port Use this port when conneting to remote host. If ommitted, port
defaults to 443.
--subject Print the certificate Subject -- typically address and org name.
Examples:
1. Print a list of all hostnames that the certificate used by amazon.com
is valid for.
$(basename "$0") --host amazon.com --alt
DNS:uedata.amazon.com
DNS:amazon.com
DNS:amzn.com
DNS:www.amzn.com
DNS:www.amazon.com
2. Print issuer of certificate used by smtp.gmail.com. Fetch certficate info
over port 465.
$(basename "$0") --host smtp.gmail.com --port 465 --issuer
issuer=
countryName = US
organizationName = Google Inc
commonName = Google Internet Authority G2
3. Print valid dates for the certificate, using a local file as the source of
certificate data. Dates are formatted using the date command and display
time in your local timezone instead of GMT.
$(basename "$0") --file /path/to/file.crt --dates
valid from: 2014-02-04 16:00:00 PST
valid till: 2017-02-04 15:59:59 PST
4. Print certificate serial number. This script doesn't have a special option
to parse out the serial number, so will use the generic --option flag to
pass '-serial' through to openssl.
$(basename "$0") --host gmail.com --option -serial
serial=4BF004B4DDC9C2F8
EOF
}
if ! [ -x "$(type -P openssl)" ]; then
echo "ERROR: script requires openssl"
echo "For Debian and friends, get it with 'apt-get install openssl'"
exit 1
fi
while [ "$1" ]; do
case "$1" in
--file)
shift
crt="$1"
source="local"
;;
--host)
shift
host="$1"
source="remote"
;;
--port)
shift
port="$1"
;;
--name)
shift
servername="-servername $1"
;;
--all-info)
opt="-text"
;;
--alt)
FormatOutput() {
grep -A1 "Subject Alternative Name:" | tail -n1 |
tr -d ' ' | tr ',' '\n'
}
;;
--cn)
opt="-subject -nameopt multiline"
FormatOutput() {
awk '/commonName/ {print$NF}'
}
;;
--dates)
opt="-dates"
FormatOutput() {
dates=$(cat -)
start=$(grep Before <<<"$dates" | cut -d= -f2-)
end=$(grep After <<<"$dates" | cut -d= -f2-)
echo "valid from: $(date -d "$start" '+%F %T %Z')"
echo "valid till: $(date -d "$end" '+%F %T %Z')"
}
;;
--end)
opt="-enddate"
FormatOutput() {
read -r end
end=$(cut -d= -f2- <<<"$end")
date -d "$end" '+%F %T %Z'
}
;;
--issuer)
opt="-issuer -nameopt multiline"
;;
--most-info)
opt="-text -certopt no_header,no_version,no_serial,no_signame,no_pubkey,no_sigdump,no_aux"
;;
--option)
shift
opt="$1"
;;
--subject)
opt="-subject -nameopt multiline"
;;
--help)
usage
exit 0
;;
--debug)
DEBUG="yes"
;;
*)
echo "$(basename "$0"): invalid option $1" >&2
echo "see --help for usage"
exit 1
;;
esac
shift
done
CheckLocalCert() {
openssl x509 -in "$crt" -noout "$opt"
}
CheckRemoteCert() {
# shellcheck disable=SC2086
echo |
openssl s_client $servername -connect "$host:$port" 2>/dev/null |
openssl x509 -noout "$opt"
}
if [ -z "$(type -t FormatOutput)" ]; then
FormatOutput() { cat; }
fi
if [ -z "$opt" ]; then
opt="-text -certopt no_header,no_version,no_serial,no_signame,no_pubkey,no_sigdump,no_aux"
fi
if [ -z "$source" ]; then
echo "ERROR: missing certificate source."
echo "Provide one via '--file' or '--host' arguments."
echo "See '--help' for examples."
exit 1
fi
if [ "$source" == "local" ]; then
[ -n "$DEBUG" ] && echo "DEBUG: certificate source is local file"
if [ -z "$crt" ]; then
echo "ERROR: missing certificate file"
exit 1
fi
[ -n "$DEBUG" ] && echo
CheckLocalCert | FormatOutput
fi
if [ "$source" == "remote" ]; then
[ -n "$DEBUG" ] && echo "DEBUG: certificate source is remote host"
if [ -z "$host" ]; then
echo "ERROR: missing remote host value."
echo "Provide one via '--host' argument"
exit 1
fi
if [ -z "$port" ]; then
[ -n "$DEBUG" ] && echo "DEBUG: defaulting to 443 for port."
port="443"
fi
[ -n "$DEBUG" ] && echo
CheckRemoteCert | FormatOutput
fi