Skip to content

Commit b9a860f

Browse files
authored
[2.7] bpo-29136: Add TLS 1.3 cipher suites and OP_NO_TLSv1_3 (GH-1363) (#3446)
* bpo-29136: Add TLS 1.3 support TLS 1.3 introduces a new, distinct set of cipher suites. The TLS 1.3 cipher suites don't overlap with cipher suites from TLS 1.2 and earlier. Since Python sets its own set of permitted ciphers, TLS 1.3 handshake will fail as soon as OpenSSL 1.1.1 is released. Let's enable the common AES-GCM and ChaCha20 suites. Additionally the flag OP_NO_TLSv1_3 is added. It defaults to 0 (no op) with OpenSSL prior to 1.1.1. This allows applications to opt-out from TLS 1.3 now. Signed-off-by: Christian Heimes <[email protected]>. (cherry picked from commit cb5b68a)
1 parent 7dcea4c commit b9a860f

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

Doc/library/ssl.rst

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,17 @@ instead.
192192
------------------------ --------- --------- ---------- --------- ----------- -----------
193193
*SSLv2* yes no yes no no no
194194
*SSLv3* no yes yes no no no
195-
*SSLv23* no yes yes yes yes yes
195+
*SSLv23* [1]_ no yes yes yes yes yes
196196
*TLSv1* no no yes yes no no
197197
*TLSv1.1* no no yes no yes no
198198
*TLSv1.2* no no yes no no yes
199199
======================== ========= ========= ========== ========= =========== ===========
200200

201+
.. rubric:: Footnotes
202+
.. [1] TLS 1.3 protocol will be available with :data:`PROTOCOL_SSLv23` in
203+
OpenSSL >= 1.1.1. There is no dedicated PROTOCOL constant for just
204+
TLS 1.3.
205+
201206
.. note::
202207

203208
Which connections succeed will vary depending on the version of
@@ -286,6 +291,11 @@ purposes.
286291

287292
3DES was dropped from the default cipher string.
288293

294+
.. versionchanged:: 2.7.15
295+
296+
TLS 1.3 cipher suites TLS_AES_128_GCM_SHA256, TLS_AES_256_GCM_SHA384,
297+
and TLS_CHACHA20_POLY1305_SHA256 were added to the default cipher string.
298+
289299
.. function:: _https_verify_certificates(enable=True)
290300

291301
Specifies whether or not server certificates are verified when creating
@@ -701,6 +711,16 @@ Constants
701711

702712
.. versionadded:: 2.7.9
703713

714+
.. data:: OP_NO_TLSv1_3
715+
716+
Prevents a TLSv1.3 connection. This option is only applicable in conjunction
717+
with :const:`PROTOCOL_TLS`. It prevents the peers from choosing TLSv1.3 as
718+
the protocol version. TLS 1.3 is available with OpenSSL 1.1.1 or later.
719+
When Python has been compiled against an older version of OpenSSL, the
720+
flag defaults to *0*.
721+
722+
.. versionadded:: 2.7.15
723+
704724
.. data:: OP_CIPHER_SERVER_PREFERENCE
705725

706726
Use the server's cipher ordering preference, rather than the client's.
@@ -765,6 +785,12 @@ Constants
765785

766786
.. versionadded:: 2.7.9
767787

788+
.. data:: HAS_TLSv1_3
789+
790+
Whether the OpenSSL library has built-in support for the TLS 1.3 protocol.
791+
792+
.. versionadded:: 2.7.15
793+
768794
.. data:: CHANNEL_BINDING_TYPES
769795

770796
List of supported TLS channel binding types. Strings in this list

Lib/ssl.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def _import_symbols(prefix):
123123
_import_symbols('PROTOCOL_')
124124
_import_symbols('VERIFY_')
125125

126-
from _ssl import HAS_SNI, HAS_ECDH, HAS_NPN, HAS_ALPN
126+
from _ssl import HAS_SNI, HAS_ECDH, HAS_NPN, HAS_ALPN, HAS_TLSv1_3
127127

128128
from _ssl import _OPENSSL_API_VERSION
129129

@@ -157,6 +157,7 @@ def _import_symbols(prefix):
157157
# (OpenSSL's default setting is 'DEFAULT:!aNULL:!eNULL')
158158
# Enable a better set of ciphers by default
159159
# This list has been explicitly chosen to:
160+
# * TLS 1.3 ChaCha20 and AES-GCM cipher suites
160161
# * Prefer cipher suites that offer perfect forward secrecy (DHE/ECDHE)
161162
# * Prefer ECDHE over DHE for better performance
162163
# * Prefer AEAD over CBC for better performance and security
@@ -168,13 +169,16 @@ def _import_symbols(prefix):
168169
# * Disable NULL authentication, NULL encryption, 3DES and MD5 MACs
169170
# for security reasons
170171
_DEFAULT_CIPHERS = (
172+
'TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:'
173+
'TLS13-AES-128-GCM-SHA256:'
171174
'ECDH+AESGCM:ECDH+CHACHA20:DH+AESGCM:DH+CHACHA20:ECDH+AES256:DH+AES256:'
172175
'ECDH+AES128:DH+AES:ECDH+HIGH:DH+HIGH:RSA+AESGCM:RSA+AES:RSA+HIGH:'
173176
'!aNULL:!eNULL:!MD5:!3DES'
174177
)
175178

176179
# Restricted and more secure ciphers for the server side
177180
# This list has been explicitly chosen to:
181+
# * TLS 1.3 ChaCha20 and AES-GCM cipher suites
178182
# * Prefer cipher suites that offer perfect forward secrecy (DHE/ECDHE)
179183
# * Prefer ECDHE over DHE for better performance
180184
# * Prefer AEAD over CBC for better performance and security
@@ -185,6 +189,8 @@ def _import_symbols(prefix):
185189
# * Disable NULL authentication, NULL encryption, MD5 MACs, DSS, RC4, and
186190
# 3DES for security reasons
187191
_RESTRICTED_SERVER_CIPHERS = (
192+
'TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:'
193+
'TLS13-AES-128-GCM-SHA256:'
188194
'ECDH+AESGCM:ECDH+CHACHA20:DH+AESGCM:DH+CHACHA20:ECDH+AES256:DH+AES256:'
189195
'ECDH+AES128:DH+AES:ECDH+HIGH:DH+HIGH:RSA+AESGCM:RSA+AES:RSA+HIGH:'
190196
'!aNULL:!eNULL:!MD5:!DSS:!RC4:!3DES'

Lib/test/test_ssl.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ def test_constants(self):
168168
ssl.OP_NO_COMPRESSION
169169
self.assertIn(ssl.HAS_SNI, {True, False})
170170
self.assertIn(ssl.HAS_ECDH, {True, False})
171+
ssl.OP_NO_SSLv2
172+
ssl.OP_NO_SSLv3
173+
ssl.OP_NO_TLSv1
174+
ssl.OP_NO_TLSv1_3
175+
if ssl.OPENSSL_VERSION_INFO >= (1, 0, 1):
176+
ssl.OP_NO_TLSv1_1
177+
ssl.OP_NO_TLSv1_2
171178

172179
def test_random(self):
173180
v = ssl.RAND_status()
@@ -2784,6 +2791,24 @@ def test_version_basic(self):
27842791
self.assertEqual(s.version(), 'TLSv1')
27852792
self.assertIs(s.version(), None)
27862793

2794+
@unittest.skipUnless(ssl.HAS_TLSv1_3,
2795+
"test requires TLSv1.3 enabled OpenSSL")
2796+
def test_tls1_3(self):
2797+
context = ssl.SSLContext(ssl.PROTOCOL_TLS)
2798+
context.load_cert_chain(CERTFILE)
2799+
# disable all but TLS 1.3
2800+
context.options |= (
2801+
ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1 | ssl.OP_NO_TLSv1_2
2802+
)
2803+
with ThreadedEchoServer(context=context) as server:
2804+
with context.wrap_socket(socket.socket()) as s:
2805+
s.connect((HOST, server.port))
2806+
self.assertIn(s.cipher()[0], [
2807+
'TLS13-AES-256-GCM-SHA384',
2808+
'TLS13-CHACHA20-POLY1305-SHA256',
2809+
'TLS13-AES-128-GCM-SHA256',
2810+
])
2811+
27872812
@unittest.skipUnless(ssl.HAS_ECDH, "test requires ECDH-enabled OpenSSL")
27882813
def test_default_ecdh_curve(self):
27892814
# Issue #21015: elliptic curve-based Diffie Hellman key exchange
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add TLS 1.3 cipher suites and OP_NO_TLSv1_3.

Modules/_ssl.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4281,6 +4281,11 @@ init_ssl(void)
42814281
#if HAVE_TLSv1_2
42824282
PyModule_AddIntConstant(m, "OP_NO_TLSv1_1", SSL_OP_NO_TLSv1_1);
42834283
PyModule_AddIntConstant(m, "OP_NO_TLSv1_2", SSL_OP_NO_TLSv1_2);
4284+
#endif
4285+
#ifdef SSL_OP_NO_TLSv1_3
4286+
PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", SSL_OP_NO_TLSv1_3);
4287+
#else
4288+
PyModule_AddIntConstant(m, "OP_NO_TLSv1_3", 0);
42844289
#endif
42854290
PyModule_AddIntConstant(m, "OP_CIPHER_SERVER_PREFERENCE",
42864291
SSL_OP_CIPHER_SERVER_PREFERENCE);
@@ -4333,6 +4338,14 @@ init_ssl(void)
43334338
Py_INCREF(r);
43344339
PyModule_AddObject(m, "HAS_ALPN", r);
43354340

4341+
#if defined(TLS1_3_VERSION) && !defined(OPENSSL_NO_TLS1_3)
4342+
r = Py_True;
4343+
#else
4344+
r = Py_False;
4345+
#endif
4346+
Py_INCREF(r);
4347+
PyModule_AddObject(m, "HAS_TLSv1_3", r);
4348+
43364349
/* Mappings for error codes */
43374350
err_codes_to_names = PyDict_New();
43384351
err_names_to_codes = PyDict_New();

0 commit comments

Comments
 (0)