Created
February 4, 2019 17:30
-
-
Save alaniwi/1c4ae4e5862281f30236b21401e2c0b2 to your computer and use it in GitHub Desktop.
X509 certificate tester
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from cryptography import x509 | |
from cryptography.hazmat.backends import default_backend | |
import datetime | |
def cert_is_valid(cert_file, min_lifetime=0): | |
""" | |
Returns boolean - True if the certificate is in date. | |
Optional argument min_lifetime is the number of seconds | |
which must remain. | |
""" | |
try: | |
with open(cert_file) as f: | |
crt_data = f.read() | |
except IOError: | |
return False | |
try: | |
cert = x509.load_pem_x509_certificate(crt_data, default_backend()) | |
except ValueError: | |
return False | |
now = datetime.datetime.now() | |
return (cert.not_valid_before <= now | |
and cert.not_valid_after > now + datetime.timedelta(0, min_lifetime)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires
pip install cryptography