2014-08-26 17:33:55 +02:00
#!/bin/sh
#################################################################################
#
# Lynis
# ------------------
#
2016-03-13 16:00:39 +01:00
# Copyright 2007-2013, Michael Boelen
2017-02-09 13:35:40 +01:00
# Copyright 2007-2017, CISOfy
2016-03-13 16:00:39 +01:00
#
# Website : https://cisofy.com
# Blog : http://linux-audit.com
# GitHub : https://github.com/CISOfy/lynis
2014-08-26 17:33:55 +02:00
#
# Lynis comes with ABSOLUTELY NO WARRANTY. This is free software, and you are
# welcome to redistribute it under the terms of the GNU General Public License.
# See LICENSE file for usage of this software.
#
#################################################################################
#
# Cryptography
#
#################################################################################
#
InsertSection "Cryptography"
#
#################################################################################
#
# Test : CRYP-7902
# Description : check for expired SSL certificates
if [ ! -z "${OPENSSLBINARY}" ]; then PREQS_MET="YES"; else PREQS_MET="NO"; fi
2016-07-24 17:22:00 +02:00
Register --test-no CRYP-7902 --preqs-met ${PREQS_MET} --weight L --network NO --category security --description "Check expire date of SSL certificates"
2014-08-26 17:33:55 +02:00
if [ ${SKIPTEST} -eq 0 ]; then
2016-09-08 21:04:02 +02:00
COUNT_TOTAL=0
2014-08-26 17:33:55 +02:00
FOUNDPROBLEM=0
2016-09-08 21:04:02 +02:00
sSSL_PATHS=$(echo ${SSL_CERTIFICATE_PATHS} | ${SEDBINARY} 's/:/ /g')
sSSL_PATHS=$(echo ${sSSL_PATHS} | ${SEDBINARY} 's/^ //' | ${TRBINARY} " " "\n" | ${SORTBINARY} | uniq | ${TRBINARY} "\n" " ")
LogText "Paths to scan: ${sSSL_PATHS}"
2016-04-13 19:49:30 +02:00
2016-09-08 21:04:02 +02:00
for DIR in ${sSSL_PATHS}; do
COUNT_DIR=0
if [ -d ${DIR} ]; then
FileIsReadable ${DIR}
2014-09-09 14:49:37 +02:00
if [ ${CANREAD} -eq 1 ]; then
2016-09-08 21:04:02 +02:00
LogText "Result: found directory ${DIR}"
2014-09-09 14:49:37 +02:00
# Search for CRT files
2017-08-29 14:32:42 +02:00
${FINDBINARY} ${DIR} -type f -print0 | ${EGREPBINARY} -z ".crt$|.pem$|^cert" | ${SORTBINARY} -z | while IFS= read -r -d $'\0' FILE; do
FileIsReadable "${FILE}"
if [ ${CANREAD} -eq 1 ]; then
# Only check the files that are not installed by a package
if ! FileInstalledByPackage "${FILE}"; then
COUNT_DIR=$((COUNT_DIR + 1))
LogText "Test: checking file and determining if it is certificate ${FILE}"
FIND=$(${OPENSSLBINARY} x509 -noout -in "${FILE}" -enddate 2> /dev/null | ${GREPBINARY} "^notAfter")
if [ $? -eq 0 ]; then
# Check certificate where 'end date' has been expired
FIND=$(${OPENSSLBINARY} x509 -noout -checkend 0 -in "${FILE}" -enddate 2> /dev/null)
EXIT_CODE=$?
CERT_CN=$(${OPENSSLBINARY} x509 -noout -subject -in "${FILE}" 2> /dev/null | ${SEDBINARY} -e 's/^subject.*CN=\([a-zA-Z0-9\.\-\*]*\).*$/\1/')
CERT_NOTAFTER=$(${OPENSSLBINARY} x509 -noout -enddate -in "${FILE}" 2> /dev/null | ${AWKBINARY} -F= '{if ($1=="notAfter") { print $2 }}')
Report "certificate[]=${FILE}|${EXIT_CODE}|cn:${CERT_CN};notafter:${CERT_NOTAFTER};|"
if [ ${EXIT_CODE} -eq 0 ]; then
LogText "Result: certificate ${FILE} seems to be correct and still valid"
2017-03-12 16:35:50 +01:00
else
2017-08-29 14:32:42 +02:00
FOUNDPROBLEM=1
LogText "Result: certificate ${FILE} has been expired"
2017-03-12 16:35:50 +01:00
fi
2017-08-29 14:32:42 +02:00
else
LogText "Result: skipping tests for this file (${FILE}) as it is most likely not a certificate (a key file?)"
2016-09-08 21:04:02 +02:00
fi
2014-09-09 14:49:37 +02:00
fi
2017-08-29 14:32:42 +02:00
else
LogText "Result: can not read file ${FILE} (no permission)"
fi
done
if [ -z "$(${FINDBINARY} ${DIR} -type f 2> /dev/null | ${EGREPBINARY} ".crt$|.pem$|^cert")" ]; then
2016-09-08 21:04:02 +02:00
LogText "Result: no certificates found in directory ${DIR}"
fi
else
LogText "Result: can not read path ${DIR} (no permission)"
2014-09-09 14:49:37 +02:00
fi
2017-03-12 16:35:50 +01:00
LogText "Result: found ${COUNT_DIR} certificates in ${DIR}"
2016-09-08 21:04:02 +02:00
else
LogText "Result: SSL path ${DIR} does not exist"
2014-08-26 17:33:55 +02:00
fi
2016-09-08 21:04:02 +02:00
COUNT_TOTAL=$((COUNT_TOTAL + COUNT_DIR))
2014-08-26 17:33:55 +02:00
done
2016-09-08 21:04:02 +02:00
Report "certificates=${COUNT_TOTAL}"
LogText "Result: found a total of ${COUNT_TOTAL} certificates"
2014-08-26 17:33:55 +02:00
if [ ${FOUNDPROBLEM} -eq 0 ]; then
2016-06-18 11:14:01 +02:00
Display --indent 2 --text "- Checking for expired SSL certificates" --result "${STATUS_NONE}" --color GREEN
2016-09-08 21:04:02 +02:00
else
2016-06-18 11:14:01 +02:00
Display --indent 2 --text "- Checking for expired SSL certificates" --result "${STATUS_FOUND}" --color RED
2014-12-03 14:13:29 +01:00
ReportSuggestion ${TEST_NO} "Check available certificates for expiration"
2014-08-26 17:33:55 +02:00
fi
fi
#
#################################################################################
#
2016-04-28 12:31:57 +02:00
WaitForKeyPress
2014-08-26 17:33:55 +02:00
#
#================================================================================
2016-03-13 16:03:46 +01:00
# Lynis - Security Auditing and System Hardening for Linux and UNIX - https://cisofy.com