-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_ssl_certificate_validity.py
57 lines (47 loc) · 2.16 KB
/
check_ssl_certificate_validity.py
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
###########################################################################################################
# SSL/TLS Certificate Check
# 20240707
###########################################################################################################
import ssl
import socket
import datetime
def check_ssl_certificate_validity(website, outfile, logger):
"""
Args:
website (str): The website being checked.
outfile (file object): The file to write output to.
logger (function pointer): Function to print debug information.
"""
logger(f"=== check_ssl_certificate_validity")
outfile.write("\n===========Certificate validity Check\n")
try:
# Establish a secure connection to the website and retrieve its SSL certificate information
cert = ssl.get_server_certificate((website, 443))
# Verify the certificate
context = ssl.create_default_context()
context.check_hostname = True
context.verify_mode = ssl.CERT_REQUIRED
# Create a socket and wrap it with an SSL context
with socket.create_connection((website, 443)) as sock:
with context.wrap_socket(sock, server_hostname=website) as ssock:
# Get the certificate information
cert_info = ssock.getpeercert()
# Get the expiration date of the certificate
cert_expiration = datetime.datetime.strptime(cert_info['notAfter'], '%b %d %H:%M:%S %Y %Z')
# Get the issuer information of the certificate
cert_ca = cert_info['issuer']
current_time = datetime.datetime.now(datetime.timezone.utc)
days_left = (cert_expiration - current_time).days
outfile.write(f"certificate expiration: {cert_expiration}\n")
outfile.write(f"time of check (utc) : {current_time}\n")
outfile.write(f"certificate days left : {days_left}\n")
outfile.write(f"certificate issuer : {cert_ca}\n")
if days_left > 29:
outfile.write("OK\n")
else:
outfile.write("NOK\n")
return days_left
except ssl.SSLError as e:
print(f"SSL Error: {e}")
# If the certificate is invalid, return False
return 0