Created
October 5, 2019 08:02
-
-
Save 1oglop1/77cec5e4cb18844688abac8d268569b5 to your computer and use it in GitHub Desktop.
Get fingerprints of certificates in certificate chain (python)
This file contains 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
""" | |
https://stackoverflow.com/questions/19145097/ | |
""" | |
import socket | |
from OpenSSL import SSL | |
import certifi | |
hostname='www.google.com' | |
port=443 | |
context = SSL.Context(method=SSL.TLSv1_METHOD) | |
context.load_verify_locations(cafile=certifi.where()) | |
conn = SSL.Connection(context, socket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)) | |
conn.settimeout(5) | |
conn.connect((hostname, port)) | |
conn.setblocking(1) | |
conn.do_handshake() | |
conn.set_tlsext_host_name(hostname.encode()) | |
for (idx, cert) in enumerate(conn.get_peer_cert_chain()): | |
print(f'{idx} subject: {cert.get_subject()}') | |
print(f' issuer: {cert.get_issuer()})') | |
print(f' fingerprint: {cert.digest("sha1")}') | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment