This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ssl.wrap_socket doesn't handle virtual TLS hosts
Type: Stage:
Components: Documentation, Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: docs@python Nosy List: alex, christian.heimes, docs@python, dstufft, janssen, nagle, pitrou
Priority: normal Keywords:

Created on 2015-04-01 18:32 by nagle, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg239834 - (view) Author: John Nagle (nagle) Date: 2015-04-01 18:32
ssl.wrap_socket() always uses the SSL certificate associated with the raw IP address, rather than using the server_host feature of TLS. Even when wrap_socket is used before calling "connect(port, host)", the "host" parameter isn't used by TLS.

To get proper TLS behavior (which only works in recent Python versions), it's necessary to create an SSLContext, then use

context.wrap_socket(sock, server_hostname="example.com")

This behavior is backwards-compatible (the SSL module didn't talk TLS until very recently) but confusing.  The documentation does not reflect this difference.  There's a lot of old code and online advice which suggests using ssl.wrap_socket().  It works until you hit a virtual host with TLS support. Then you get the wrong server cert and an unexpected "wrong host" SSL error.

Possible fixes:

1. Deprecate ssl.wrap_socket(), and modify the documentation to tell users to always use context.wrap_socket().

2. Add a "server_hostname" parameter to ssl.wrap_socket().  It doesn't accept that parameter; only context.wrap_socket() does.  Modify documentation accordingly.
msg239866 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2015-04-02 00:52
Not sure why you're using wrap_socket() directly. Most of the time you should be using a higher-level library instead (for example a HTTP(S) library).

In any case, the doc already mentions that "Starting from Python 3.2, it can be more flexible to use SSLContext.wrap_socket() instead".

I leave this open in case other people feel positively about it.
msg239887 - (view) Author: John Nagle (nagle) Date: 2015-04-02 08:03
I'm using wrap_socket because I want to read the details of a server's SSL certificate.  

"Starting from Python 3.2, it can be more flexible to use SSLContext.wrap_socket() instead" does not convey that ssl.wrap_socket() will fail to connect to some servers because it will silently check the wrong certificate.
msg275044 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2016-09-08 15:21
ssl.wrap_socket() will be deprecated in 3.6. Please use a context. You can still inspect the server cert with a context. In fact ssl.wrap_socket() uses a context internally.
History
Date User Action Args
2022-04-11 14:58:15adminsetgithub: 68031
2016-09-08 15:22:12giampaolo.rodolasetnosy: - giampaolo.rodola
2016-09-08 15:21:18christian.heimessetstatus: open -> closed
resolution: wont fix
messages: + msg275044

versions: + Python 3.6, - Python 3.4
2015-04-02 08:03:25naglesetmessages: + msg239887
2015-04-02 00:52:16pitrousetnosy: + janssen, pitrou, giampaolo.rodola, christian.heimes, alex, dstufft
messages: + msg239866
2015-04-01 18:32:18naglecreate