Skip to content

Commit 182450c

Browse files
committed
Add ability to TLS 1.3 cipher suites on SSL Context
1 parent 39c2fbc commit 182450c

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
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_ciphersuites`` that allows the allowed TLS 1.3 ciphers.
619

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

src/OpenSSL/SSL.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,9 @@ def set_cipher_list(self, cipher_list: bytes) -> None:
14681468
See the OpenSSL manual for more information (e.g.
14691469
:manpage:`ciphers(1)`).
14701470
1471+
Note this API does not change the cipher suites used in TLS 1.3
1472+
Use `set_ciphersuites` for that.
1473+
14711474
:param bytes cipher_list: An OpenSSL cipher string.
14721475
:return: None
14731476
"""
@@ -1523,6 +1526,31 @@ def set_ciphersuites_list(self, ciphersuites_list: bytes) -> None:
15231526
_lib.SSL_CTX_set_ciphersuites(self._context, cipher_list) == 1
15241527
)
15251528

1529+
@_require_not_used
1530+
def set_ciphersuites(self, ciphersuites: bytes) -> None:
1531+
"""
1532+
Set the list of TLS 1.3 ciphers to be used in this context.
1533+
OpenSSL maintains a separate list of TLS 1.3+ ciphers to
1534+
ciphers for TLS 1.2 and lowers.
1535+
1536+
See the OpenSSL manual for more information (e.g.
1537+
:manpage:`ciphers(1)`).
1538+
1539+
:param bytes ciphersuites: An OpenSSL cipher string containing
1540+
TLS 1.3+ ciphersuites.
1541+
:return: None
1542+
1543+
.. versionadded:: 25.2.0
1544+
"""
1545+
ciphersuites = _text_to_bytes_and_warn("ciphersuites", ciphersuites)
1546+
1547+
if not isinstance(ciphersuites, bytes):
1548+
raise TypeError("ciphersuites must be a byte string.")
1549+
1550+
_openssl_assert(
1551+
_lib.SSL_CTX_set_ciphersuites(self._context, ciphersuites) == 1
1552+
)
1553+
15261554
@_require_not_used
15271555
def set_client_ca_list(
15281556
self, certificate_authorities: Sequence[X509Name]

tests/test_ssl.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,20 +497,23 @@ class TestContext:
497497

498498
@pytest.mark.parametrize(
499499
"cipher_string",
500-
[b"hello world:AES128-SHA", "hello world:AES128-SHA"],
500+
[b"TLS_AES_128_GCM_SHA256", "TLS_AES_128_GCM_SHA256"],
501501
)
502-
def test_set_cipher_list(
502+
def test_set_ciphersuites(
503503
self, context: Context, cipher_string: bytes
504504
) -> None:
505505
"""
506-
`Context.set_cipher_list` accepts both byte and unicode strings
506+
`Context.set_ciphersuites` accepts both byte and unicode strings
507507
for naming the ciphers which connections created with the context
508508
object will be able to choose from.
509509
"""
510-
context.set_cipher_list(cipher_string)
510+
context.set_ciphersuites(cipher_string)
511511
conn = Connection(context, None)
512512

513-
assert "AES128-SHA" in conn.get_cipher_list()
513+
# OpenSSL has different APIs for *setting* TLS <=1.2 and >= 1.3
514+
# but only one API for retrieving them
515+
assert "TLS_AES_128_GCM_SHA256" in conn.get_cipher_list()
516+
assert "TLS_AES_256_GCM_SHA384" not in conn.get_cipher_list()
514517

515518
def test_set_cipher_list_wrong_type(self, context: Context) -> None:
516519
"""

0 commit comments

Comments
 (0)