0

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?

ouflak
  • 2,458
  • 10
  • 44
  • 49

2 Answers2

1

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'
wuerfelfreak
  • 2,363
  • 1
  • 14
  • 29
  • Okay so if I attempt ` ssl.get_server_certificate(('www.google.com', 443)) ` I get a timeout error ` TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond ` I assume its because of my proxy but I do not know how to work around it. – Virtual Penman Aug 09 '19 at 17:42
  • I have got the ssl.get_server_certificate working but it tells me now that I have a value error `ValueError: too many values to unpack (expected 2)` – Virtual Penman Aug 09 '19 at 18:25
  • Its working for me so far. (python 2.7.15) Maybe this might help. [ValueError: too many values to unpack (expected 2)](https://stackoverflow.com/questions/24150952) – wuerfelfreak Aug 10 '19 at 21:02
  • Your answer is 100 percent correct, I just ran it on my Mac at home with no issues. Its the dang proxy at work that is causing the problems. – Virtual Penman Aug 11 '19 at 21:11
-1

Python SSL lib don't deal with proxies.

tAd
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 11 '22 at 15:12