Trying to extract Certificate information in Python

1.8k Views Asked by At

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?

2

There are 2 best solutions below

4
wuerfelfreak On BEST ANSWER

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'
1
tAd On

Python SSL lib don't deal with proxies.