Skip to content

Commit 07b4148

Browse files
miss-islingtontiran
authored andcommitted
bpo-32008: Prefer client or TLSv1_2 in examples (GH-5797) (GH-16026)
Prefer client or TLSv1_2 in examples Signed-off-by: Christian Heimes <christian@python.org> (cherry picked from commit 894d0f7) Co-authored-by: Christian Heimes <christian@python.org>
1 parent 197ac1a commit 07b4148

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

Doc/library/ssl.rst

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,13 +1873,15 @@ to speed up repeated connections from the same clients.
18731873
:meth:`~SSLContext.wrap_socket` in order to match the hostname. Enabling
18741874
hostname checking automatically sets :attr:`~SSLContext.verify_mode` from
18751875
:data:`CERT_NONE` to :data:`CERT_REQUIRED`. It cannot be set back to
1876-
:data:`CERT_NONE` as long as hostname checking is enabled.
1876+
:data:`CERT_NONE` as long as hostname checking is enabled. The
1877+
:data:`PROTOCOL_TLS_CLIENT` protocol enables hostname checking by default.
1878+
With other protocols, hostname checking must be enabled explicitly.
18771879

18781880
Example::
18791881

18801882
import socket, ssl
18811883

1882-
context = ssl.SSLContext()
1884+
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
18831885
context.verify_mode = ssl.CERT_REQUIRED
18841886
context.check_hostname = True
18851887
context.load_default_certs()
@@ -2181,19 +2183,23 @@ If you prefer to tune security settings yourself, you might create
21812183
a context from scratch (but beware that you might not get the settings
21822184
right)::
21832185

2184-
>>> context = ssl.SSLContext()
2185-
>>> context.verify_mode = ssl.CERT_REQUIRED
2186-
>>> context.check_hostname = True
2186+
>>> context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
21872187
>>> context.load_verify_locations("/etc/ssl/certs/ca-bundle.crt")
21882188

21892189
(this snippet assumes your operating system places a bundle of all CA
21902190
certificates in ``/etc/ssl/certs/ca-bundle.crt``; if not, you'll get an
21912191
error and have to adjust the location)
21922192

2193+
The :data:`PROTOCOL_TLS_CLIENT` protocol configures the context for cert
2194+
validation and hostname verification. :attr:`~SSLContext.verify_mode` is
2195+
set to :data:`CERT_REQUIRED` and :attr:`~SSLContext.check_hostname` is set
2196+
to ``True``. All other protocols create SSL contexts with insecure defaults.
2197+
21932198
When you use the context to connect to a server, :const:`CERT_REQUIRED`
2194-
validates the server certificate: it ensures that the server certificate
2195-
was signed with one of the CA certificates, and checks the signature for
2196-
correctness::
2199+
and :attr:`~SSLContext.check_hostname` validate the server certificate: it
2200+
ensures that the server certificate was signed with one of the CA
2201+
certificates, checks the signature for correctness, and verifies other
2202+
properties like validity and identity of the hostname::
21972203

21982204
>>> conn = context.wrap_socket(socket.socket(socket.AF_INET),
21992205
... server_hostname="www.python.org")

0 commit comments

Comments
 (0)