Skip to content

Commit a157163

Browse files
authored
Add ability to TLS 1.3 cipher suites on SSL Context (#1432)
1 parent f9a68e6 commit a157163

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

CHANGELOG.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ Changelog
33

44
Versions are year-based with a strict backward-compatibility policy.
55
The third digit is only for regressions.
6+
UNRELEASED
7+
----------
8+
9+
Backward-incompatible changes:
10+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
12+
Deprecations:
13+
^^^^^^^^^^^^^
14+
15+
Changes:
16+
^^^^^^^^
17+
18+
- Added ``OpenSSL.SSL.Context.set_tls13_ciphersuites`` that allows the allowed TLS 1.3 ciphers.
619

720
25.1.0 (2025-05-17)
821
-------------------

src/OpenSSL/SSL.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,9 @@ def set_cipher_list(self, cipher_list: bytes) -> None:
14691469
See the OpenSSL manual for more information (e.g.
14701470
:manpage:`ciphers(1)`).
14711471
1472+
Note this API does not change the cipher suites used in TLS 1.3
1473+
Use `set_tls13_ciphersuites` for that.
1474+
14721475
:param bytes cipher_list: An OpenSSL cipher string.
14731476
:return: None
14741477
"""
@@ -1501,6 +1504,29 @@ def set_cipher_list(self, cipher_list: bytes) -> None:
15011504
],
15021505
)
15031506

1507+
@_require_not_used
1508+
def set_tls13_ciphersuites(self, ciphersuites: bytes) -> None:
1509+
"""
1510+
Set the list of TLS 1.3 ciphers to be used in this context.
1511+
OpenSSL maintains a separate list of TLS 1.3+ ciphers to
1512+
ciphers for TLS 1.2 and lowers.
1513+
1514+
See the OpenSSL manual for more information (e.g.
1515+
:manpage:`ciphers(1)`).
1516+
1517+
:param bytes ciphersuites: An OpenSSL cipher string containing
1518+
TLS 1.3+ ciphersuites.
1519+
:return: None
1520+
1521+
.. versionadded:: 25.2.0
1522+
"""
1523+
if not isinstance(ciphersuites, bytes):
1524+
raise TypeError("ciphersuites must be a byte string.")
1525+
1526+
_openssl_assert(
1527+
_lib.SSL_CTX_set_ciphersuites(self._context, ciphersuites) == 1
1528+
)
1529+
15041530
@_require_not_used
15051531
def set_client_ca_list(
15061532
self, certificate_authorities: Sequence[X509Name]

tests/test_ssl.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,20 @@ def test_set_cipher_list(
512512

513513
assert "AES128-SHA" in conn.get_cipher_list()
514514

515+
def test_set_tls13_ciphersuites(self, context: Context) -> None:
516+
"""
517+
`Context.set_tls13_ciphersuites` accepts both byte and unicode strings
518+
for naming the ciphers which connections created with the context
519+
object will be able to choose from.
520+
"""
521+
context.set_tls13_ciphersuites(b"TLS_AES_128_GCM_SHA256")
522+
conn = Connection(context, None)
523+
524+
# OpenSSL has different APIs for *setting* TLS <=1.2 and >= 1.3
525+
# but only one API for retrieving them
526+
assert "TLS_AES_128_GCM_SHA256" in conn.get_cipher_list()
527+
assert "TLS_AES_256_GCM_SHA384" not in conn.get_cipher_list()
528+
515529
def test_set_cipher_list_wrong_type(self, context: Context) -> None:
516530
"""
517531
`Context.set_cipher_list` raises `TypeError` when passed a non-string

0 commit comments

Comments
 (0)