Skip to content

Instantly share code, notes, and snippets.

@alaniwi
Created February 4, 2019 17:30
Show Gist options
  • Save alaniwi/1c4ae4e5862281f30236b21401e2c0b2 to your computer and use it in GitHub Desktop.
Save alaniwi/1c4ae4e5862281f30236b21401e2c0b2 to your computer and use it in GitHub Desktop.
X509 certificate tester
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))
@alaniwi
Copy link
Author

alaniwi commented Feb 4, 2019

Requires pip install cryptography

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment