Skip to content

Instantly share code, notes, and snippets.

@alastairmccormack
Created October 19, 2014 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alastairmccormack/7881e28bd605fc355b2a to your computer and use it in GitHub Desktop.
Save alastairmccormack/7881e28bd605fc355b2a to your computer and use it in GitHub Desktop.
Checks the expiry of a cert from the a PKCS12 file and alerts using tunable warning period
from OpenSSL.crypto import *
import time
import datetime
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-f", "--file", dest="filename",
help="PKCS12/pfx file", metavar="FILE")
parser.add_option("-p", "--password",
dest="password",
help="password of PKCS12/pfx file")
(options, args) = parser.parse_args()
year = datetime.timedelta(days=365)
month = datetime.timedelta(weeks=4)
day = datetime.timedelta(days=1)
warn_period = month * 1
# open it, using password. Supply/read your own from stdin.
p12_file = open(options.filename, "rb")
p12 = load_pkcs12(p12_file.read(), options.password)
cert = p12.get_certificate()
not_after_date_string = cert.get_notAfter()
not_after_date = datetime.datetime.strptime(not_after_date_string, "%Y%m%d%H%M%SZ")
now = datetime.datetime.now()
difference = not_after_date - now
if difference <= warn_period:
print "Oh heck!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment