Getting certificate chains from websites
import socket
from OpenSSL import SSL, crypto
from cryptography import x509
from cryptography.hazmat.backends import default_backend

def get_certificate_chain(hostname, port=443):
    context = SSL.Context(SSL.TLS_CLIENT_METHOD)
    context.set_verify(SSL.VERIFY_NONE, lambda *args: True)

    conn = SSL.Connection(context, socket.socket(socket.AF_INET, socket.SOCK_STREAM))
    conn.set_tlsext_host_name(hostname.encode())
    conn.connect((hostname, port))
    conn.do_handshake()

    cert_chain = conn.get_peer_cert_chain()
    conn.close()

    for idx, cert in enumerate(cert_chain):
        pem_cert = crypto.dump_certificate(crypto.FILETYPE_PEM, cert).decode()

        print(f"\n--- Certificado {idx + 1} ---")
        print(pem_cert)

        x509_cert = x509.load_pem_x509_certificate(pem_cert.encode(), default_backend())
        subject = x509_cert.subject.rfc4514_string()
        issuer = x509_cert.issuer.rfc4514_string()

        print(f"Assunto: {subject}")
        print(f"Emissor: {issuer}")


get_certificate_chain('www.jaimedcsilva.com')

 




11 April 2025 | Last Updated: 22 Nov. 2025 | jaimedcsilva

Related
  • Printing CMD with colors
  • Calculating & Displaying crypto currency with CoinGecko
  • Creatin an .exe file to run a Python script
  • Getting certificate chains from websites
  • Getting the complete trustchain of a website
  • Installing pip in embedded Python - Full Guide
  • Cython - Hiding Python Code
  • How to Send Emails with Python’s smtplib
  • Generating QR Codes with Python

  • Buy Me a Coffee