Skip to content

Instantly share code, notes, and snippets.

@1oglop1
Created October 5, 2019 08:02
Show Gist options
  • Save 1oglop1/77cec5e4cb18844688abac8d268569b5 to your computer and use it in GitHub Desktop.
Save 1oglop1/77cec5e4cb18844688abac8d268569b5 to your computer and use it in GitHub Desktop.
Get fingerprints of certificates in certificate chain (python)
"""
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