Welcome to mirror list, hosted at ThFree Co, Russian Federation.

tests_crypto « include - github.com/CISOfy/lynis.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4188dea94e0d569cab3cf4133a043524150a2851 (plain)
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
#!/bin/sh

#################################################################################
#
#   Lynis
# ------------------
#
# Copyright 2007-2013, Michael Boelen
# Copyright 2007-2019, CISOfy
#
# Website  : https://cisofy.com
# Blog     : http://linux-audit.com
# GitHub   : https://github.com/CISOfy/lynis
#
# 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
    Register --test-no CRYP-7902 --preqs-met ${PREQS_MET} --weight L --network NO --category security --description "Check expire date of SSL certificates"
    if [ ${SKIPTEST} -eq 0 ]; then
        COUNT_EXPIRED=0
        COUNT_TOTAL=0
        FOUNDPROBLEM=0
        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}"

        for DIR in ${sSSL_PATHS}; do
            COUNT_DIR=0
            if [ -d ${DIR} ]; then
                FileIsReadable ${DIR}
                if [ ${CANREAD} -eq 1 ]; then
                    LogText "Result: found directory ${DIR}"
                    # Search for certificate files
                    FILES=$(${FINDBINARY} ${DIR} -type f 2> /dev/null | ${EGREPBINARY} ".crt$|.pem$|^cert" | ${SORTBINARY} | ${SEDBINARY} 's/ /:space:/g')
                    for FILE in ${FILES}; do
                        FILE=$(echo ${FILE} |${SEDBINARY} 's/:space:/ /g')
                        COUNT_DIR=$((COUNT_DIR + 1))
                        FileIsReadable "${FILE}"
                        if [ ${CANREAD} -eq 1 ]; then
                            # Only check the files that are not installed by a package
                            if ! FileInstalledByPackage "${FILE}"; then
                                LogText "Test: test if file is a certificate"
                                OUTPUT=$(${GREPBINARY} -q 'BEGIN CERT' "${FILE}")
                                if [ $? -eq 0 ]; then
                                    LogText "Result: file is a certificate"
                                    LogText "Test: checking certificate details"
                                    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"
                                        else
                                            FOUNDPROBLEM=1
                                            COUNT_EXPIRED=$((COUNT_EXPIRED + 1))
                                            LogText "Result: certificate ${FILE} has been expired"
                                        fi
                                    else
                                        LogText "Result: skipping tests for this file (${FILE}) as it is most likely not a certificate (a key file?)"
                                    fi
                                else
                                    LogText "Result: skipping test for this file (${FILE}) as we could not find 'BEGIN CERT'"
                                fi
                            fi
                        else
                            LogText "Result: can not read file ${FILE} (no permission)"
                        fi
                    done
                    COUNT_TOTAL=$((COUNT_TOTAL + COUNT_DIR))
                    LogText "Result: found ${COUNT_DIR} certificates in ${DIR}"
                else
                    LogText "Result: can not read path ${DIR} (no permission)"
                fi
            else
                LogText "Result: SSL path ${DIR} does not exist"
            fi
        done
        Report "certificates=${COUNT_TOTAL}"
        LogText "Result: found a total of ${COUNT_TOTAL} certificates"

        if [ ${FOUNDPROBLEM} -eq 0 ]; then
            Display --indent 2 --text "- Checking for expired SSL certificates [${COUNT_EXPIRED}/${COUNT_TOTAL}]" --result "${STATUS_NONE}" --color GREEN
        else
            Display --indent 2 --text "- Checking for expired SSL certificates [${COUNT_EXPIRED}/${COUNT_TOTAL}]" --result "${STATUS_FOUND}" --color RED
            ReportSuggestion ${TEST_NO} "Check available certificates for expiration"
        fi
    fi
#
#################################################################################
#

WaitForKeyPress

#
#================================================================================
# Lynis - Security Auditing and System Hardening for Linux and UNIX - https://cisofy.com