Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python requests library - get SSL certificate information

I'm looking for a way to get to the information about SSL certificate used by the remote site using requests (CA and CN would suffice).

I can easily get those when I use for example socket + OpenSSL, but in my code I use a special resolver ( Python 'requests' library - define specific DNS? ), so I need to use Requests or urllib2 - so far I examined most of the requests code through inspect module, and I see no way of getting to these values.

I'd appreciate any hints at this point, maybe I'm missing something obvious :)

[edit]:

To be more precise - I'm patching part of the requests lib (from 'socket' up to urllib2), to use a custom name resolver- it comes down to this:

self.my_opener = urllib2.build_opener(MyHTTPHandler,MyHTTPSHandler) (urllib2.OpenerDirector instance)

my_opener.open(url) returns an urllib.addinfourl object - from which I would need to extract certificate info if that's at all posible.

Unfortunately this is not the case for the question after which this one was marked as a duplicate.

like image 673
Taku Avatar asked May 26 '26 21:05

Taku


1 Answers

Requests source code is quite explicit when it comes to validating certificates, see:

https://github.com/kennethreitz/requests/blob/826667a54c608d72d8813c3eddb75b27ab26c988/requests/packages/urllib3/packages/ssl_match_hostname/_implementation.py#L67

Either monkey-patch this code, or track down where it is called from:

https://github.com/kennethreitz/requests/blob/dae4026c18b29b5995d41c1e50741a3d34f1297e/requests/packages/urllib3/connection.py#L189

like image 74
Dima Tisnek Avatar answered May 28 '26 11:05

Dima Tisnek