Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/library/ssl.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2361,6 +2361,12 @@ to speed up repeated connections from the same clients.
:data:`PROTOCOL_TLS_CLIENT` protocol enables hostname checking by default.
With other protocols, hostname checking must be enabled explicitly.

Hostname checking only applies to client-side connections. A context
with hostname checking enabled cannot be used for server-side
connections: :meth:`~SSLContext.wrap_socket` and
:meth:`~SSLContext.wrap_bio` raise :exc:`ValueError` if *server_side*
is true. Use a separate context for servers.

Example::

import socket, ssl
Expand Down
4 changes: 4 additions & 0 deletions Lib/ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ def _check_sslobject_params(server_side, context=None, server_hostname=None,
if ech_config is not None:
raise ValueError("ech_config can only be specified in "
"client mode")
if context.check_hostname:
raise ValueError("check_hostname cannot be used with "
"server_side=True; use a separate server "
"context or disable check_hostname")
if ech_config is not None and not HAS_ECH:
raise NotImplementedError("ECH is not supported by the OpenSSL library")
if context.check_hostname and not server_hostname:
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2725,6 +2725,22 @@ def test_check_hostname_requires_server_hostname(self):
self.assertFalse(context.check_hostname)
context.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO())

def test_check_hostname_rejects_server_side(self):
# gh-65224: check_hostname only applies to client connections, so
# server mode rejects it with an error that says so.
exp_str = ("check_hostname cannot be used with server_side=True; "
"use a separate server context or disable check_hostname")

_, server_context, _ = testing_context()
server_context.check_hostname = True
with self.assertRaisesRegex(ValueError, exp_str):
server_context.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO(),
server_side=True)

with self.assertRaisesRegex(ValueError, exp_str):
with socket.socket() as sock:
server_context.wrap_socket(sock, server_side=True)

def test_server_side_bad_params(self):
# A server neither sends a hostname nor resumes a client's session,
# so wrap_bio() rejects both in server mode like wrap_socket()
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,7 @@ Paul Kippes
Sergey B Kirpichev
Steve Kirsch
Sebastian Kirsche
Vasiliy Kiryanov
Kamil Kisiel
Akira Kitada
Ron Klatchko
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improve the error raised when a context with
:attr:`ssl.SSLContext.check_hostname` enabled is used with
``server_side=True``.
Loading