Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain SSL certificate from peer without verification using Python

Tags:

python

ssl

Am in the process of building a quick python script to periodically check my clients websites are working correctly. One of these checks is to ensure their SSL certificates are current, or to provide an alert if their certificate is about to expire.

The ssl packages provides a way to obtain the peer certificate with the SSLSocket.getpeercert() method but this will only return a certificate if the certificate can be validated. If the CA cert has not been obtained the validation does not work.

What I want to do is obtain the peer certificate even if it can not be validated so I am able to get the information required to both obtain the correct CA certificate and do other checks such as checking the domain name matches, expiry date is in the correct range etc. Does anybody know of a way to obtain this information?

pyCurl and pyOpenSSL look like possible candidates but have not been able to find an example or manage to get them to return the certificate.

Cheers

like image 891
Tim Avatar asked Mar 30 '11 00:03

Tim


People also ask

How do I skip SSL verification in Python?

Method 1: Passing verify=False to request methodAlong with the URL also pass the verify=False parameter to the method in order to disable the security checks.

How do I verify a certificate in Python?

Certification holders may now have others easily verify their certification status by using a unique certificate verification code. The code can be found in the top right-hand corner on all digital certificates issued by the Python Institute.


1 Answers

It may be possible to use a shell script to grab the certificates and then use Python to iterate over certificate output files. Something like:

$ openssl s_client -connect host:port -showcerts > certfile

might work. You might also read the documentation on pyOpenSSL's Connection object, which has a get_peer_certificate() method:

http://packages.python.org/pyOpenSSL/openssl-connection.html#l2h-187

I haven't ever used the pyOpenSSL module, but it's probably your best bet for keeping everything in Python.

like image 154
Kurt McKee Avatar answered Oct 13 '22 00:10

Kurt McKee