Last active
March 1, 2020 15:36
-
-
Save bockor/0856e1245e3d77689e497aef3eeea514 to your computer and use it in GitHub Desktop.
get certificate issuer info in python
This file contains hidden or 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
#!/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