Bootstrap FreeKB - Python (Scripting) - Return SSL certificate data using pyOpenSSL
Python (Scripting) - Return SSL certificate data using pyOpenSSL

Updated:   |  Python (Scripting) articles

The pyOpenSSL module can be used to do something with an SSL certificate such as return the SSL certificate data. The pip install command can be used to install the pyOpenSSL package.

pip install pyOpenSSL

 

And here is a boilerplate example of how to return the date a certificate was issued and when it will expire.

#!/usr/bin/python
import OpenSSL

my_certificate = "/path/to/example.pem"

x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, my_certificate)

print(f"x509.get_notBefore() = {x509.get_notBefore()}")
print(f"x509.get_notAfter() = {x509.get_notAfter()}")

 

Which should return something like this.

x509.get_notBefore() = b'20240112204307Z'
x509.get_notAfter() = b'20260312204308Z'

 

Notice in this example that the stdout begins with b (bytes). decode() can be used to convert the stdout from bytes to something like utf-8.

#!/usr/bin/python
import OpenSSL

my_certificate = "/path/to/example.pem"

x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, my_certificate)
date_the_certificate_was_issued = x509.get_notBefore().decode('utf-8')

print(f"date_the_certificate_was_issued = {date_the_certificate_was_issued}")

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 12dc06 in the box below so that we can be sure you are a human.