/
check_ssl_cert
executable file
·3222 lines (2605 loc) · 110 KB
/
check_ssl_cert
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
#
# check_ssl_cert
#
# Checks an X.509 certificate:
# - checks if the server is running and delivers a valid certificate
# - checks if the CA matches a given pattern
# - checks the validity
#
# See the INSTALL file for installation instructions
#
# Copyright (c) 2007-2012 ETH Zurich.
# Copyright (c) 2007-2020 Matteo Corti <matteo@corti.li>
#
# This module is free software; you can redistribute it and/or modify it
# under the terms of GNU general public license (gpl) version 3.
# See the LICENSE file for details.
################################################################################
# Constants
VERSION=1.113.0
SHORTNAME="SSL_CERT"
VALID_ATTRIBUTES=",startdate,enddate,subject,issuer,serial,modulus,serial,hash,email,ocsp_uri,fingerprint,"
SIGNALS="HUP INT QUIT TERM ABRT"
# return value for the creation of temporary files
TEMPFILE=""
################################################################################
# Variables
WARNING_MSG=""
CRITICAL_MSG=""
ALL_MSG=""
################################################################################
# Functions
################################################################################
# Prints usage information
# Params
# $1 error message (optional)
usage() {
if [ -n "$1" ] ; then
echo "Error: $1" 1>&2
fi
#### The following line is 80 characters long (helps to fit the help text in a standard terminal)
######--------------------------------------------------------------------------------
echo
echo "Usage: check_ssl_cert -H host [OPTIONS]"
echo
echo "Arguments:"
echo " -H,--host host server"
echo
echo "Options:"
echo " -A,--noauth ignore authority warnings (expiration only)"
echo " --altnames matches the pattern specified in -n with"
echo " alternate names too"
echo " -C,--clientcert path use client certificate to authenticate"
echo " --clientpass phrase set passphrase for client certificate."
echo " -c,--critical days minimum number of days a certificate has to"
echo " be valid to issue a critical status"
echo " --curl-bin path path of the curl binary to be used"
echo " --curl-user-agent string user agent that curl shall use to obtain the"
echo " issuer cert"
echo " --dane verify that valid DANE records exist (since OpenSSL 1.1.0)"
echo " --dane 211 verify that a valid DANE-TA(2) SPKI(1) SHA2-256(1) TLSA record exists"
echo " --dane 301 verify that a valid DANE-EE(3) Cert(0) SHA2-256(1) TLSA record exists"
echo " --dane 302 verify that a valid DANE-EE(3) Cert(0) SHA2-512(2) TLSA record exists"
echo " --dane 311 verify that a valid DANE-EE(3) SPKI(1) SHA2-256(1) TLSA record exists"
echo " -d,--debug produces debugging output"
echo " --dig-bin path path of the dig binary to be used"
echo " --ecdsa signature algorithm selection: force ECDSA certificate"
echo " -e,--email address pattern to match the email address contained"
echo " in the certificate"
echo " -f,--file file local file path (works with -H localhost only)"
echo " with -f you can not only pass a x509"
echo " certificate file but also a certificate"
echo " revocation list (CRL) to check the validity"
echo " period"
echo " --file-bin path path of the file binary to be used"
echo " --fingerprint SHA1 pattern to match the SHA1-Fingerprint"
echo " --force-perl-date force the usage of Perl for date computations"
echo " --format FORMAT format output template on success, for example"
echo " \"%SHORTNAME% OK %CN% from '%CA_ISSUER_MATCHED%'\""
echo " -h,--help,-? this help message"
echo " --http-use-get use GET instead of HEAD (default) for the HTTP"
echo " related checks"
echo " --ignore-exp ignore expiration date"
echo " --ignore-ocsp do not check revocation with OCSP"
echo " --ignore-ocsp-timeout ignore OCSP result when timeout occurs while checking"
echo " --ignore-sig-alg do not check if the certificate was signed with SHA1"
echo " or MD5"
echo " --ignore-ssl-labs-cache Forces a new check by SSL Labs (see -L)"
echo " --inetproto protocol Force IP version 4 or 6"
echo " -i,--issuer issuer pattern to match the issuer of the certificate"
echo " --issuer-cert-cache dir directory where to store issuer certificates cache"
echo " -K,--clientkey path use client certificate key to authenticate"
echo " -L,--check-ssl-labs grade SSL Labs assessment"
echo " (please check https://www.ssllabs.com/about/terms.html)"
echo " --check-ssl-labs-warn grade SSL-Labs grade on which to warn"
echo " --long-output list append the specified comma separated (no spaces) list"
echo " of attributes to the plugin output on additional lines"
echo " Valid attributes are:"
echo " enddate, startdate, subject, issuer, modulus,"
echo " serial, hash, email, ocsp_uri and fingerprint."
echo " 'all' will include all the available attributes."
echo " -n,--cn name pattern to match the CN of the certificate (can be"
echo " specified multiple times)"
echo " --nmap-bin path path of the nmap binary to be used"
echo " --no_ssl2 disable SSL version 2"
echo " --no_ssl3 disable SSL version 3"
echo " --no_tls1 disable TLS version 1"
echo " --no_tls1_1 disable TLS version 1.1"
echo " --no_tls1_2 disable TLS version 1.2"
echo " --no_tls1_3 disable TLS version 1.3"
echo " --not-valid-longer-than days critical if the certificate validity is longer than the specified period"
echo " -N,--host-cn match CN with the host name"
echo " --ocsp-critical hours minimum number of hours an OCSP response has to be valid to"
echo " issue a critical status"
echo " --ocsp-warning hours minimum number of hours an OCSP response has to be valid to"
echo " issue a warning status"
echo " -o,--org org pattern to match the organization of the certificate"
echo " --openssl path path of the openssl binary to be used"
echo " -p,--port port TCP port"
echo " -P,--protocol protocol use the specific protocol"
echo " {ftp|ftps|http|https|h2|imap|imaps|irc|ircs|ldap|ldaps|pop3|pop3s|postgres|sieve|smtp|smtps|xmpp|xmpp-server}"
echo " https: default"
echo " h2: forces HTTP/2"
echo " ftp,imap,irc,ldap,pop3,postgres,sieve,smtp: switch to TLS using StartTLS"
echo " --require-no-ssl2 critical if SSL version 2 is offered"
echo " --require-no-ssl3 critical if SSL version 3 is offered"
echo " --require-no-tls1 critical if TLS 1 is offered"
echo " --require-no-tls1_1 critical if TLS 1.1 is offered"
echo " -s,--selfsigned allows self-signed certificates"
echo " --serial serialnum pattern to match the serial number"
echo " --sni name sets the TLS SNI (Server Name Indication) extension"
echo " in the ClientHello message to 'name'"
echo " --ssl2 forces SSL version 2"
echo " --ssl3 forces SSL version 3"
echo " --require-ocsp-stapling require OCSP stapling"
echo " --require-san require the presence of a Subject Alternative Name"
echo " extension"
echo " -r,--rootcert path root certificate or directory to be used for"
echo " certificate validation"
echo " --rootcert-dir path root directory to be used for certificate validation"
echo " --rootcert-file path root certificate to be used for certificate validation"
echo " --rsa signature algorithm selection: force RSA certificate"
echo " --temp dir directory where to store the temporary files"
echo " --terse terse output"
echo " -t,--timeout seconds timeout after the specified time"
echo " (defaults to 15 seconds)"
echo " --tls1 force TLS version 1"
echo " --tls1_1 force TLS version 1.1"
echo " --tls1_2 force TLS version 1.2"
echo " --tls1_3 force TLS version 1.3"
echo " -v,--verbose verbose output"
echo " -V,--version version"
echo " -w,--warning days minimum number of days a certificate has to be valid"
echo " to issue a warning status"
echo " --xmpphost name specifies the host for the 'to' attribute of the stream element"
echo " -4 force IPv4"
echo " -6 force IPv6"
echo
echo "Deprecated options:"
echo " --days days minimum number of days a certificate has to be valid"
echo " (see --critical and --warning)"
echo " --ocsp check revocation via OCSP"
echo " -S,--ssl version force SSL version (2,3)"
echo " (see: --ssl2 or --ssl3)"
echo
echo "Report bugs to https://github.com/matteocorti/check_ssl_cert/issues"
echo
exit 3
}
################################################################################
# trap passing the signal name
# see https://stackoverflow.com/questions/2175647/is-it-possible-to-detect-which-trap-signal-in-bash/2175751#2175751
trap_with_arg() {
func="$1" ; shift
for sig ; do
# shellcheck disable=SC2064
trap "${func} ${sig}" "${sig}"
done
}
################################################################################
# Cleanup temporary files
remove_temporary_files() {
if [ -n "${DEBUG}" ] ; then
echo "[DBG] cleaning up temporary files"
# shellcheck disable=SC2086
echo ${TEMPORARY_FILES} | tr '\ ' '\n' | sed 's/^/[DBG] /'
fi
# shellcheck disable=SC2086
if [ -n "${TEMPORARY_FILES}" ]; then
rm -f ${TEMPORARY_FILES}
fi
}
################################################################################
# Cleanup when exiting
cleanup() {
SIGNAL=$1
if [ -n "${DEBUG}" ] ; then
echo "[DBG] signal caught ${SIGNAL}"
fi
remove_temporary_files
# shellcheck disable=SC2086
trap - ${SIGNALS}
exit
}
create_temporary_file() {
# create a temporary file
TEMPFILE="$( mktemp -t "${0##*/}XXXXXX" 2> /dev/null )"
if [ -z "${TEMPFILE}" ] || [ ! -w "${TEMPFILE}" ] ; then
unknown 'temporary file creation failure.'
fi
if [ -n "${DEBUG}" ] ; then
echo "[DBG] temporary file ${TEMPFILE} created"
fi
# add the file to the list of temporary files
TEMPORARY_FILES="${TEMPORARY_FILES} ${TEMPFILE}"
}
################################################################################
# Compute the number of hours until a given date
# Params
# $1 date
# Sets HOURS_UNTIL
hours_until() {
DATE=$1
OLDLANG="${LANG}"
LANG=en_US
if [ -n "${DEBUG}" ] ; then
echo "[DBG] Date computations: ${DATETYPE}"
echo "[DBG] Computing number of hours until '${DATE}'"
fi
case "${DATETYPE}" in
"BSD")
HOURS_UNTIL=$(( ( $(${DATEBIN} -jf "%b %d %T %Y %Z" "${DATE}" +%s) - $(${DATEBIN} +%s) ) / 3600 ))
;;
"GNU")
HOURS_UNTIL=$(( ( $(${DATEBIN} -d "${DATE}" +%s) - $(${DATEBIN} +%s) ) / 3600 ))
;;
"PERL")
# Warning: some shell script formatting tools will indent the EOF! (should be at position 0)
if ! HOURS_UNTIL=$(perl - "${DATE}" <<-"EOF"
use strict;
use warnings;
use Date::Parse;
my $cert_date = str2time( $ARGV[0] );
my $hours = int (( $cert_date - time ) / 3600 + 0.5);
print "$hours\n";
EOF
) ; then
# something went wrong with the embedded Perl code: check the indentation of EOF
unknown "Error computing the certificate validity with Perl"
fi
;;
*)
unknown "Internal error: unknown date type"
esac
LANG="${OLDLANG}"
if [ -n "${DEBUG}" ] ; then
echo "[DBG] Hours until ${DATE}: ${HOURS_UNTIL}"
fi
}
################################################################################
# prepends critical messages to list of all messages
# Params
# $1 error message
prepend_critical_message() {
if [ -n "${DEBUG}" ] ; then
echo "[DBG] CRITICAL >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo "[DBG] prepend_critical_message: new message = $1"
echo "[DBG] prepend_critical_message: HOST = ${HOST}"
echo "[DBG] prepend_critical_message: CN = ${CN}"
echo "[DBG] prepend_critical_message: SNI = ${SNI}"
echo "[DBG] prepend_critical_message: FILE = ${FILE}"
echo "[DBG] prepend_critical_message: SHORTNAME = ${SHORTNAME}"
echo "[DBG] prepend_critical_message: MSG = ${MSG}"
echo "[DBG] prepend_critical_message: CRITICAL_MSG = ${CRITICAL_MSG}"
echo "[DBG] prepend_critical_message: ALL_MSG 1 = ${ALL_MSG}"
fi
if [ -n "${CN}" ] ; then
tmp=" ${CN}"
else
if [ -n "${HOST}" ] ; then
if [ -n "${SNI}" ] ; then
tmp=" ${SNI}"
elif [ -n "${FILE}" ] ; then
tmp=" ${FILE}"
else
tmp=" ${HOST}"
fi
fi
fi
MSG="${SHORTNAME} CRITICAL${tmp}: ${1}${PERFORMANCE_DATA}${LONG_OUTPUT}"
if [ "${CRITICAL_MSG}" = "" ]; then
CRITICAL_MSG="${MSG}"
fi
ALL_MSG="\n ${MSG}${ALL_MSG}"
if [ -n "${DEBUG}" ] ; then
echo "[DBG] prepend_critical_message: MSG 2 = ${MSG}"
echo "[DBG] prepend_critical_message: CRITICAL_MSG 2 = ${CRITICAL_MSG}"
echo "[DBG] prepend_critical_message: ALL_MSG 2 = ${ALL_MSG}"
echo "[DBG] CRITICAL <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
fi
}
################################################################################
# Exits with a critical message
# Params
# $1 error message
critical() {
remove_temporary_files
if [ -n "${DEBUG}" ] ; then
echo '[DBG] exiting with CRITICAL'
echo "[DBG] ALL_MSG = ${ALL_MSG}"
fi
NUMBER_OF_ERRORS=$( printf '%b' "${ALL_MSG}" | wc -l )
if [ -n "${DEBUG}" ] ; then
echo "[DBG] number of errors = ${NUMBER_OF_ERRORS}"
fi
if [ "${NUMBER_OF_ERRORS}" -ge 2 ] ; then
printf '%s\nError(s):%b\n' "$1" "${ALL_MSG}"
else
printf '%s\n' "$1"
fi
exit 2
}
################################################################################
# append all warning messages to list of all messages
# Params
# $1 warning message
append_warning_message() {
if [ -n "${DEBUG}" ] ; then
echo "[DBG] WARNING >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo "[DBG] append_warning_message: HOST = ${HOST}"
echo "[DBG] append_warning_message: CN = ${CN}"
echo "[DBG] append_warning_message: SNI = ${SNI}"
echo "[DBG] append_warning_message: FILE = ${FILE}"
echo "[DBG] append_warning_message: SHORTNAME = ${SHORTNAME}"
echo "[DBG] prepend_warning_message: MSG = ${MSG}"
echo "[DBG] prepend_warning_message: WARNING_MSG = ${WARNING_MSG}"
echo "[DBG] prepend_warning_message: ALL_MSG 1 = ${ALL_MSG}"
fi
if [ -n "${CN}" ] ; then
tmp=" ${CN}"
else
if [ -n "${HOST}" ] ; then
if [ -n "${SNI}" ] ; then
tmp=" ${SNI}"
elif [ -n "${FILE}" ] ; then
tmp=" ${FILE}"
else
tmp=" ${HOST}"
fi
fi
fi
MSG="${SHORTNAME} WARN${tmp}: ${1}${PERFORMANCE_DATA}${LONG_OUTPUT}"
if [ "${WARNING_MSG}" = "" ]; then
WARNING_MSG="${MSG}"
fi
ALL_MSG="${ALL_MSG}\n ${MSG}"
if [ -n "${DEBUG}" ] ; then
echo "[DBG] prepend_warning_message: MSG 2 = ${MSG}"
echo "[DBG] prepend_warning_message: WARNING_MSG 2 = ${WARNING_MSG}"
echo "[DBG] prepend_warning_message: ALL_MSG 2 = ${ALL_MSG}"
echo "[DBG] WARNING <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
fi
}
################################################################################
# Exits with a warning message
# Param
# $1 warning message
warning() {
remove_temporary_files
NUMBER_OF_ERRORS=$( printf '%b' "${ALL_MSG}" | wc -l )
if [ "${NUMBER_OF_ERRORS}" -ge 2 ] ; then
printf '%s\nError(s):%b\n' "$1" "${ALL_MSG}"
else
printf '%s\n' "$1"
fi
exit 1
}
################################################################################
# Exits with an 'unknown' status
# Param
# $1 message
unknown() {
if [ -n "${HOST}" ] ; then
if [ -n "${SNI}" ] ; then
tmp=" ${SNI}"
elif [ -n "${FILE}" ] ; then
tmp=" ${FILE}"
else
tmp=" ${HOST}"
fi
fi
remove_temporary_files
printf '%s UNKNOWN%s: %s\n' "${SHORTNAME}" "${tmp}" "$1"
exit 3
}
################################################################################
# To set a variable with an HEREDOC in a POSIX compliant way
# see: https://unix.stackexchange.com/questions/340718/how-do-i-bring-heredoc-text-into-a-shell-script-variable
# Usage:
# set_variable variablename<<'HEREDOC'
# ...
# HEREDOC
set_variable() {
# shellcheck disable=SC2016
eval "$1"'=$(cat)'
}
################################################################################
# Executes command with a timeout
# Params:
# $1 timeout in seconds
# $2 command
# Returns 1 if timed out 0 otherwise
exec_with_timeout() {
time=$1
# start the command in a subshell to avoid problem with pipes
# (spawn accepts one command)
command="/bin/sh -c \"$2\""
if [ -n "${DEBUG}" ] ; then
printf '[DBG] executing with timeout (%ss): %s\n' "${time}" "${2}"
fi
if [ -n "${TIMEOUT_BIN}" ] ; then
if [ -n "${DEBUG}" ] ; then
printf "[DBG] %s %s %s\n" "${TIMEOUT_BIN}" "${time}" "${command}"
fi
eval "${TIMEOUT_BIN} ${time} ${command}" > /dev/null 2>&1
RET=$?
# return codes
# https://www.gnu.org/software/coreutils/manual/coreutils.html#timeout-invocation
if [ "${RET}" -eq 137 ] ; then
prepend_critical_message "SIGKILL received"
elif [ "${RET}" -eq 124 ] ; then
prepend_critical_message "Timeout after ${time} seconds"
elif [ "${RET}" -eq 125 ] ; then
prepend_critical_message "execution of ${command} failed"
elif [ "${RET}" -eq 126 ] ; then
prepend_critical_message "${command} is found but cannot be invoked"
elif [ "${RET}" -eq 127 ] ; then
prepend_critical_message "${command} cannot be found"
fi
return "${RET}"
elif [ -n "${EXPECT}" ] ; then
# just to tell shellcheck that the variable is assigned
# (in fact the value is assigned with the function set_value)
EXPECT_SCRIPT=''
TIMEOUT_ERROR_CODE=42
set_variable EXPECT_SCRIPT << EOT
set echo \"-noecho\"
set timeout ${time}
# spawn the process
spawn -noecho sh -c { ${command} }
expect {
timeout { exit ${TIMEOUT_ERROR_CODE} }
eof
}
# Get the return value
# https://stackoverflow.com/questions/23614039/how-to-get-the-exit-code-of-spawned-process-in-expect-shell-script
foreach { pid spawnid os_error_flag value } [wait] break
# return the command return value
exit \$value
EOT
if [ -n "${DEBUG}" ] ; then
echo '[DBG] Executing expect script'
printf '%s' "${EXPECT_SCRIPT}" | sed 's/^/[DBG]\ \ \ \ \ /'
fi
echo "${EXPECT_SCRIPT}" | expect
RET=$?
if [ -n "${DEBUG}" ] ; then
echo "[DBG] expect returned ${RET}"
fi
if [ "${RET}" -eq "${TIMEOUT_ERROR_CODE}" ] ; then
prepend_critical_message "Timeout after ${time} seconds"
critical "${SHORTNAME} CRITICAL: Timeout after ${time} seconds"
fi
return "${RET}"
else
eval "${command}"
return $?
fi
}
################################################################################
# Checks if a given program is available and executable
# Params
# $1 program name
# Returns 1 if the program exists and is executable
check_required_prog() {
PROG=$(command -v "$1" 2> /dev/null)
if [ -z "${PROG}" ] ; then
unknown "cannot find program: $1"
fi
if [ ! -x "${PROG}" ] ; then
unknown "${PROG} is not executable"
fi
}
################################################################################
# Converts SSL Labs grades to a numeric value
# (see https://www.ssllabs.com/downloads/SSL_Server_Rating_Guide.pdf)
# Params
# $1 program name
# Sets NUMERIC_SSL_LAB_GRADE
convert_ssl_lab_grade() {
GRADE="$1"
unset NUMERIC_SSL_LAB_GRADE
case "${GRADE}" in
'A+'|'a+')
# Value not in documentation
NUMERIC_SSL_LAB_GRADE=85
shift
;;
A|a)
NUMERIC_SSL_LAB_GRADE=80
shift
;;
'A-'|'a-')
# Value not in documentation
NUMERIC_SSL_LAB_GRADE=75
shift
;;
B|b)
NUMERIC_SSL_LAB_GRADE=65
shift
;;
C|c)
NUMERIC_SSL_LAB_GRADE=50
shift
;;
D|d)
NUMERIC_SSL_LAB_GRADE=35
shift
;;
E|e)
NUMERIC_SSL_LAB_GRADE=20
shift
;;
F|f)
NUMERIC_SSL_LAB_GRADE=0
shift
;;
T|t)
# No trust: value not in documentation
NUMERIC_SSL_LAB_GRADE=0
shift
;;
M|m)
# Certificate name mismatch: value not in documentation
NUMERIC_SSL_LAB_GRADE=0
shift
;;
*)
unknown "Cannot convert SSL Lab grade ${GRADE}"
;;
esac
}
################################################################################
# Tries to fetch the certificate
fetch_certificate() {
RET=0
ALPN=''
# IPv6 addresses need brackets in a URI
if [ "${HOST}" != "${HOST#*[0-9].[0-9]}" ]; then
if [ -n "${DEBUG}" ] ; then
echo "[DBG] ${HOST} is an IPv4 address"
fi
elif [ "${HOST}" != "${HOST#*:[0-9a-fA-F]}" ]; then
if [ -n "${DEBUG}" ] ; then
echo "[DBG] ${HOST} is an IPv6 address"
fi
if [ -z "${HOST##*[*}" ] ; then
if [ -n "${DEBUG}" ] ; then
echo "[DBG] ${HOST} is already specified with brakcets"
fi
else
if [ -n "${DEBUG}" ] ; then
echo "[DBG] adding brackets to ${HOST}"
fi
HOST="[${HOST}]"
fi
else
if [ -n "${DEBUG}" ] ; then
echo "[DBG] ${HOST} is not an IP address"
fi
fi
if [ -n "${REQUIRE_OCSP_STAPLING}" ] ; then
STATUS='-status'
fi
if [ -n "${DEBUG}" ] ; then
IGN_EOF='-ign_eof'
fi
# Check if a protocol was specified (if not HTTP switch to TLS)
if [ -n "${PROTOCOL}" ] && [ "${PROTOCOL}" != 'http' ] && [ "${PROTOCOL}" != 'https' ] && [ "${PROTOCOL}" != 'h2' ] ; then
case "${PROTOCOL}" in
pop3|ftp)
exec_with_timeout "${TIMEOUT}" "printf 'QUIT\\n' | ${OPENSSL} s_client ${INETPROTO} ${CLIENT} ${CLIENTPASS} -crlf ${IGN_EOF} -starttls ${PROTOCOL} -connect ${HOST}:${PORT} ${SERVERNAME} -verify 6 ${ROOT_CA} ${SSL_VERSION} ${SSL_VERSION_DISABLED} ${SSL_AU} ${STATUS} ${DANE} 2> ${ERROR} 1> ${CERT}"
RET=$?
;;
pop3s|ftps)
exec_with_timeout "${TIMEOUT}" "printf 'QUIT\\n' | ${OPENSSL} s_client ${INETPROTO} ${CLIENT} ${CLIENTPASS} -crlf ${IGN_EOF} -connect ${HOST}:${PORT} ${SERVERNAME} -verify 6 ${ROOT_CA} ${SSL_VERSION} ${SSL_VERSION_DISABLED} ${SSL_AU} ${STATUS} ${DANE} 2> ${ERROR} 1> ${CERT}"
RET=$?
;;
smtp)
exec_with_timeout "${TIMEOUT}" "printf 'QUIT\\n' | ${OPENSSL} s_client ${INETPROTO} ${CLIENT} ${CLIENTPASS} -crlf ${IGN_EOF} -starttls ${PROTOCOL} -connect ${HOST}:${PORT} ${SERVERNAME} -verify 6 ${ROOT_CA} ${SSL_VERSION} ${SSL_VERSION_DISABLED} ${SSL_AU} ${STATUS} ${DANE} ${S_CLIENT_NAME} 2> ${ERROR} 1> ${CERT}"
RET=$?
;;
smtps)
exec_with_timeout "${TIMEOUT}" "printf 'QUIT\\n' | ${OPENSSL} s_client ${INETPROTO} ${CLIENT} ${CLIENTPASS} -crlf ${IGN_EOF} -connect ${HOST}:${PORT} ${SERVERNAME} -verify 6 ${ROOT_CA} ${SSL_VERSION} ${SSL_VERSION_DISABLED} ${SSL_AU} ${STATUS} ${DANE} ${S_CLIENT_NAME} 2> ${ERROR} 1> ${CERT}"
RET=$?
;;
irc|ldap)
exec_with_timeout "${TIMEOUT}" "echo | ${OPENSSL} s_client ${INETPROTO} ${CLIENT} ${CLIENTPASS} -starttls ${PROTOCOL} -connect ${HOST}:${PORT} ${SERVERNAME} -verify 6 ${ROOT_CA} ${SSL_VERSION} ${SSL_VERSION_DISABLED} ${SSL_AU} ${STATUS} ${DANE} 2> ${ERROR} 1> ${CERT}"
RET=$?
;;
ircs|ldaps)
exec_with_timeout "${TIMEOUT}" "echo | ${OPENSSL} s_client ${INETPROTO} ${CLIENT} ${CLIENTPASS} -connect ${HOST}:${PORT} ${SERVERNAME} -verify 6 ${ROOT_CA} ${SSL_VERSION} ${SSL_VERSION_DISABLED} ${SSL_AU} ${STATUS} ${DANE} 2> ${ERROR} 1> ${CERT}"
RET=$?
;;
imap)
exec_with_timeout "${TIMEOUT}" "printf 'A01 LOGOUT\\n' | ${OPENSSL} s_client ${INETPROTO} ${CLIENT} ${CLIENTPASS} -crlf ${IGN_EOF} -starttls ${PROTOCOL} -connect ${HOST}:${PORT} ${SERVERNAME} -verify 6 ${ROOT_CA} ${SSL_VERSION} ${SSL_VERSION_DISABLED} ${SSL_AU} ${STATUS} ${DANE} 2> ${ERROR} 1> ${CERT}"
RET=$?
;;
imaps)
exec_with_timeout "${TIMEOUT}" "printf 'A01 LOGOUT\\n' | ${OPENSSL} s_client ${INETPROTO} ${CLIENT} ${CLIENTPASS} -crlf ${IGN_EOF} -connect ${HOST}:${PORT} ${SERVERNAME} -verify 6 ${ROOT_CA} ${SSL_VERSION} ${SSL_VERSION_DISABLED} ${SSL_AU} ${STATUS} ${DANE} 2> ${ERROR} 1> ${CERT}"
RET=$?
;;
postgres)
exec_with_timeout "${TIMEOUT}" "printf 'X\0\0\0\4' | ${OPENSSL} s_client ${INETPROTO} ${CLIENT} ${CLIENTPASS} -starttls ${PROTOCOL} -connect ${HOST}:${PORT} ${SERVERNAME} -verify 6 ${ROOT_CA} ${SSL_VERSION} ${SSL_VERSION_DISABLED} ${SSL_AU} ${STATUS} ${DANE} 2> ${ERROR} 1> ${CERT}"
RET=$?
;;
sieve)
exec_with_timeout "${TIMEOUT}" "echo 'LOGOUT' | ${OPENSSL} s_client ${INETPROTO} ${CLIENT} ${CLIENTPASS} -starttls ${PROTOCOL} -connect ${HOST}:${PORT} ${SERVERNAME} -verify 6 ${ROOT_CA} ${SSL_VERSION} ${SSL_VERSION_DISABLED} ${SSL_AU} ${STATUS} ${DANE} 2> ${ERROR} 1> ${CERT}"
RET=$?
;;
xmpp|xmpp-server)
exec_with_timeout "${TIMEOUT}" "echo 'Q' | ${OPENSSL} s_client ${INETPROTO} ${CLIENT} ${CLIENTPASS} -starttls ${PROTOCOL} -connect ${HOST}:${XMPPPORT} ${XMPPHOST} -verify 6 ${ROOT_CA} ${SSL_VERSION} ${SSL_VERSION_DISABLED} ${SSL_AU} ${STATUS} ${DANE} 2> ${ERROR} 1> ${CERT}"
RET=$?
;;
*)
unknown "Error: unsupported protocol ${PROTOCOL}"
;;
esac
elif [ -n "${FILE}" ] ; then
if [ "${HOST}" = "localhost" ] ; then
exec_with_timeout "${TIMEOUT}" "/bin/cat '${FILE}' 2> ${ERROR} 1> ${CERT}"
RET=$?
else
unknown "Error: option 'file' works with -H localhost only"
fi
else
if [ "${PROTOCOL}" = 'h2' ] ; then
ALPN='-alpn h2'
fi
exec_with_timeout "${TIMEOUT}" "printf '${HTTP_REQUEST}' | ${OPENSSL} s_client ${INETPROTO} ${CLIENT} ${CLIENTPASS} -crlf ${IGN_EOF} ${ALPN} -connect ${HOST}:${PORT} ${SERVERNAME} -showcerts -verify 6 ${ROOT_CA} ${SSL_VERSION} ${SSL_VERSION_DISABLED} ${SSL_AU} ${STATUS} ${DANE} 2> ${ERROR} 1> ${CERT}"
RET=$?
fi
if [ -n "${DEBUG}" ] ; then
echo "[DBG] Return value of the command = ${RET}"
echo "[DBG] storing a copy of the retrieved certificate in ${TMPDIR}/${HOST}-${PORT}.crt"
cp "${CERT}" "${TMPDIR}/${HOST}-${PORT}.crt"
echo "[DBG] storing a copy of the OpenSSL errors in ${TMPDIR}/${HOST}-${PORT}.error"
cp "${ERROR}" "${TMPDIR}/${HOST}-${PORT}.error"
fi
if [ "${RET}" -ne 0 ] ; then
if [ -n "${DEBUG}" ] ; then
sed 's/^/[DBG] SSL error: /' "${ERROR}"
fi
# s_client could verify the server certificate because the server requires a client certificate
if ascii_grep '^Acceptable client certificate CA names' "${CERT}" ; then
if [ -n "${VERBOSE}" ] ; then
echo "The server requires a client certificate"
fi
elif ascii_grep 'nodename\ nor\ servname\ provided,\ or\ not\ known' "${ERROR}" ; then
ERROR="${HOST} is invalid"
prepend_critical_message "${ERROR}"
critical "${SHORTNAME} CRITICAL: ${ERROR}"
elif ascii_grep 'Connection\ refused' "${ERROR}" ; then
ERROR='Connection refused'
prepend_critical_message "${ERROR}"
critical "${SHORTNAME} CRITICAL: ${ERROR}"
else
# Try to clean up the error message
# Remove the 'verify and depth' lines
# Take the 1st line (seems OK with the use cases I tested)
ERROR_MESSAGE=$(
grep -v '^depth' "${ERROR}" \
| grep -v '^verify' \
| head -n 1
)
prepend_critical_message "SSL error: ${ERROR_MESSAGE}"
fi
else
if ascii_grep usage "${ERROR}" && [ "${PROTOCOL}" = "ldap" ] ; then
unknown "it seems that OpenSSL -starttls does not support yet LDAP"
fi
fi
}
################################################################################
# Adds metric to performance data
# Params
# $1 performance data in nagios plugin format,
# see https://nagios-plugins.org/doc/guidelines.html#AEN200
add_performance_data() {
if [ -z "${PERFORMANCE_DATA}" ]; then
PERFORMANCE_DATA="|${1}"
else
PERFORMANCE_DATA="${PERFORMANCE_DATA} $1"
fi
}
################################################################################
# Prepares sed-style command for variable replacement
# Params
# $1 variable name (e.g. SHORTNAME)
# $2 variable value (e.g. SSL_CERT)
var_for_sed() {
echo "s|%$1%|$( echo "$2" | sed -e 's#|#\\\\|#g' )|g"
}
################################################################################
# Performs a grep removing the NULL characters first
#
# As the POSIX grep does not have the -a option, we remove the NULL characters
# first to avoid the error Binary file matches
#
# Params
# $1 pattern
# $2 file
#
ascii_grep() {
tr -d '\000' < "$2" | grep -q "$1"
}
################################################################################
# Checks if there is an option argument (should not begin with -)
#
# Params
# $1 name of the option (e.g., '-w,--waring') to be used in the error message
# $2 next command line parameter
check_option_argument() {
if [ -z "$2" ] || [ "${2%${2#?}}"x = '-x' ] ; then
unknown "'${1}' requires an argument"
fi
}
################################################################################
# Main
################################################################################
main() {
# Default values
DEBUG=""
OPENSSL=""
FILE_BIN=""
CURL_BIN=""
CURL_USER_AGENT=""
DIG_BIN=""
NMAP_BIN=""
IGNORE_SSL_LABS_CACHE=""
PORT=""
XMPPPORT="5222"
XMPPHOST=""
SNI=""
TIMEOUT="15"
VERBOSE=""
FORCE_PERL_DATE=""
REQUIRE_SAN=""
REQUIRE_OCSP_STAPLING=""
OCSP="1" # enabled by default
OCSP_IGNORE_TIMEOUT=""
FORMAT=""
HTTP_METHOD="HEAD"
RSA=""
ECDSA=""
DANE=""
DISALLOWED_PROTOCOLS=""
# after 2020-09-01 we could set the default to 398 days because of Apple
# https://support.apple.com/en-us/HT211025
NOT_VALID_LONGER_THAN=""
# Set the default temp dir if not set
if [ -z "${TMPDIR}" ] ; then
TMPDIR="/tmp"
fi
################################################################################
# Process command line options
#
# We do not use getopts since it is unable to process long options and it is
# Bash specific.
COMMAND_LINE_ARGUMENTS=$*
while true; do
case "$1" in
########################################
# Options without arguments
-A|--noauth)
NOAUTH=1
shift
;;
--altnames)
ALTNAMES=1
shift
;;
-d|--debug)
DEBUG=1
VERBOSE=1
shift
;;
-h|--help|-\?)
usage
exit 0
;;
--force-perl-date)
FORCE_PERL_DATE=1
shift
;;
--http-use-get)
HTTP_METHOD="GET"
shift
;;
--ignore-exp)
NOEXP=1
shift
;;
--ignore-sig-alg)
NOSIGALG=1
shift
;;
--ignore-ssl-labs-cache)
IGNORE_SSL_LABS_CACHE="&startNew=on"
shift
;;
--no_ssl2)
SSL_VERSION_DISABLED="${SSL_VERSION_DISABLED} -no_ssl2"
shift
;;
--no_ssl3)
SSL_VERSION_DISABLED="${SSL_VERSION_DISABLED} -no_ssl3"
shift
;;
--no_tls1)
SSL_VERSION_DISABLED="${SSL_VERSION_DISABLED} -no_tls1"
shift
;;
--no_tls1_1)
SSL_VERSION_DISABLED="${SSL_VERSION_DISABLED} -no_tls1_1"
shift
;;
--no_tls1_2)
SSL_VERSION_DISABLED="${SSL_VERSION_DISABLED} -no_tls1_2"
shift
;;
--no_tls1_3)
SSL_VERSION_DISABLED="${SSL_VERSION_DISABLED} -no_tls1_3"
shift
;;
-N|--host-cn)
COMMON_NAME="__HOST__"
shift
;;