Skip to content

Commit 3fc7a38

Browse files
Wojciech NowakWN
Wojciech Nowak
authored and
WN
committed
SSL_CTX_set_ciphersuites for tlsv3 context
1 parent caa1ab3 commit 3fc7a38

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/OpenSSL/SSL.py

+20
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,26 @@ def set_tmp_ecdh(self, curve: _EllipticCurve) -> None:
13431343
"""
13441344
_lib.SSL_CTX_set_tmp_ecdh(self._context, curve._to_EC_KEY())
13451345

1346+
def set_ciphersuites(self, cipher_list: bytes) -> None:
1347+
"""
1348+
Set the list of ciphers to be used to configure the available TLSv1.3
1349+
ciphersuites for this context.
1350+
1351+
See the OpenSSL manual for more information (e.g.
1352+
:manpage:`ciphers(1)`).
1353+
1354+
:param bytes cipher_list: An OpenSSL cipher string.
1355+
:return: None
1356+
"""
1357+
cipher_list = _text_to_bytes_and_warn("cipher_list", cipher_list)
1358+
1359+
if not isinstance(cipher_list, bytes):
1360+
raise TypeError("cipher_list must be a byte string.")
1361+
1362+
_openssl_assert(
1363+
_lib.SSL_CTX_set_ciphersuites(self._context, cipher_list) == 1
1364+
)
1365+
13461366
def set_cipher_list(self, cipher_list: bytes) -> None:
13471367
"""
13481368
Set the list of ciphers to be used in this context.

tests/test_ssl.py

+26
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,24 @@ class TestContext:
486486
Unit tests for `OpenSSL.SSL.Context`.
487487
"""
488488

489+
@pytest.mark.parametrize(
490+
"cipher_string",
491+
[
492+
b"hello world:TLS_AES_128_GCM_SHA256",
493+
"hello world:TLS_AES_128_GCM_SHA256",
494+
],
495+
)
496+
def test_set_ciphersuites(self, context, cipher_string):
497+
"""
498+
`Context.set_ciphersuites` accepts both byte and unicode strings
499+
for naming the ciphers which connections created with the context
500+
object will be able to choose from.
501+
"""
502+
context.set_ciphersuites(cipher_string)
503+
conn = Connection(context, None)
504+
505+
assert "TLS_AES_128_GCM_SHA256" in conn.get_cipher_list()
506+
489507
@pytest.mark.parametrize(
490508
"cipher_string",
491509
[b"hello world:AES128-SHA", "hello world:AES128-SHA"],
@@ -509,6 +527,14 @@ def test_set_cipher_list_wrong_type(self, context):
509527
with pytest.raises(TypeError):
510528
context.set_cipher_list(object())
511529

530+
def test_set_ciphersuites_wrong_type(self, context):
531+
"""
532+
`Context.set_ciphersuites` raises `TypeError` when passed a non-string
533+
argument.
534+
"""
535+
with pytest.raises(TypeError):
536+
context.set_ciphersuites(object())
537+
512538
@pytest.mark.flaky(reruns=2)
513539
def test_set_cipher_list_no_cipher_match(self, context):
514540
"""

0 commit comments

Comments
 (0)