Skip to content

Commit b1ca528

Browse files
committed
PYTHON-1230 - Use PROTOCOL_TLS_CLIENT when available
1 parent 320f21b commit b1ca528

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

pymongo/ssl_support.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,18 @@ def get_ssl_context(*args):
9191
# Note PROTOCOL_SSLv23 is about the most misleading name imaginable.
9292
# This configures the server and client to negotiate the
9393
# highest protocol version they both support. A very good thing.
94-
ctx = SSLContext(ssl.PROTOCOL_SSLv23)
94+
# PROTOCOL_TLS_CLIENT was added in CPython 3.6, deprecating
95+
# PROTOCOL_SSLv23.
96+
ctx = SSLContext(
97+
getattr(ssl, "PROTOCOL_TLS_CLIENT", ssl.PROTOCOL_SSLv23))
98+
# SSLContext.check_hostname was added in CPython 2.7.9 and 3.4.
99+
# PROTOCOL_TLS_CLIENT enables it by default. Using it
100+
# requires passing server_hostname to wrap_socket, which we already
101+
# do for SNI support. To support older versions of Python we have to
102+
# call match_hostname directly, so we disable check_hostname explicitly
103+
# to avoid calling match_hostname twice.
104+
if hasattr(ctx, "check_hostname"):
105+
ctx.check_hostname = False
95106
if hasattr(ctx, "options"):
96107
# Explicitly disable SSLv2, SSLv3 and TLS compression. Note that
97108
# up to date versions of MongoDB 2.4 and above already disable

0 commit comments

Comments
 (0)