pythonsslpython-requestspyopensslm2crypto

Trying to extract Certificate information in Python


I am very new to python and cannot seem to figure out how to accomplish this task. I want to connect to a website and extract the certificate information such as issuer and expiration dates.

I have looked all over, tried all kinds of steps but because I am new I am getting lost in the socket, wrapper etc.

To make matters worse, I am in a proxy environment and it seems to really complicate things.

Does anyone know how I could connect and extract the information while behind the proxy?


Solution

  • As explained in this Answer:

    You can still the server certificate with the ssl.get_server_certificate() function, but it returns it in PEM format.

    import ssl
    print ssl.get_server_certificate(('server.test.com', 443))

    From here, I would use M2Crypto or OpenSSL to read the cert and get values:

    # M2Crypto
    cert = ssl.get_server_certificate(('www.google.com', 443))
    x509 = M2Crypto.X509.load_cert_string(cert)
    x509.get_subject().as_text()
     # 'C=US, ST=California, L=Mountain View, O=Google Inc, CN=www.google.com'