Skip to content

Instantly share code, notes, and snippets.

@bockor
Last active March 1, 2020 15:36
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 bockor/0856e1245e3d77689e497aef3eeea514 to your computer and use it in GitHub Desktop.
Save bockor/0856e1245e3d77689e497aef3eeea514 to your computer and use it in GitHub Desktop.
get certificate issuer info in python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 26 09:43:37 2020
@author: bockor
https://stackoverflow.com/questions/30862099/how-can-i-get-certificate-issuer-information-in-python
https://gist.github.com/ryansb/d8c333eb4a74168474c4
https://kite.com/python/docs/ssl.SSLSocket.getpeercert
"""
import ssl, socket
hostname = 'www.example.com'
ctx = ssl.create_default_context()
s = ctx.wrap_socket(socket.socket(), server_hostname=hostname)
s.connect((hostname, 443))
cert = s.getpeercert()
subject = dict(x[0] for x in cert['subject'])
issued_to = subject['commonName']
issuer = dict(x[0] for x in cert['issuer'])
issued_by = issuer['commonName']
expires_at = cert['notAfter']
print("[+] Certificate issued to: {}".format(issued_to))
print("[+] Certificate issued by: {}".format(issued_by))
print("[+] Cerificate expiry date: {}".format(expires_at))
'''
Output
[+] Certificate issued to: www.example.org
[+] Certificate issued by: DigiCert SHA2 Secure Server CA
[+] Cerificate expiry date: Dec 2 12:00:00 2020 GMT
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment