Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSL_CTX_set_ciphersuites for tlsv1.3 context #1292

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/OpenSSL/SSL.py
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,26 @@ def set_tmp_ecdh(self, curve: _EllipticCurve) -> None:
"""
_lib.SSL_CTX_set_tmp_ecdh(self._context, curve._to_EC_KEY())

def set_ciphersuites(self, cipher_list: bytes) -> None:
"""
Set the list of ciphers to be used to configure the available TLSv1.3
ciphersuites for this context.

See the OpenSSL manual for more information (e.g.
:manpage:`ciphers(1)`).

:param bytes cipher_list: An OpenSSL cipher string.
:return: None
"""
cipher_list = _text_to_bytes_and_warn("cipher_list", cipher_list)

if not isinstance(cipher_list, bytes):
raise TypeError("cipher_list must be a byte string.")

_openssl_assert(
_lib.SSL_CTX_set_ciphersuites(self._context, cipher_list) == 1
)

def set_cipher_list(self, cipher_list: bytes) -> None:
"""
Set the list of ciphers to be used in this context.
Expand Down
26 changes: 26 additions & 0 deletions tests/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,24 @@ class TestContext:
Unit tests for `OpenSSL.SSL.Context`.
"""

@pytest.mark.parametrize(
"cipher_string",
[
b"hello world:TLS_AES_128_GCM_SHA256",
"hello world:TLS_AES_128_GCM_SHA256",
],
)
def test_set_ciphersuites(self, context, cipher_string):
"""
`Context.set_ciphersuites` accepts both byte and unicode strings
for naming the ciphers which connections created with the context
object will be able to choose from.
"""
context.set_ciphersuites(cipher_string)
conn = Connection(context, None)

assert "TLS_AES_128_GCM_SHA256" in conn.get_cipher_list()

@pytest.mark.parametrize(
"cipher_string",
[b"hello world:AES128-SHA", "hello world:AES128-SHA"],
Expand All @@ -509,6 +527,14 @@ def test_set_cipher_list_wrong_type(self, context):
with pytest.raises(TypeError):
context.set_cipher_list(object())

def test_set_ciphersuites_wrong_type(self, context):
"""
`Context.set_ciphersuites` raises `TypeError` when passed a non-string
argument.
"""
with pytest.raises(TypeError):
context.set_ciphersuites(object())

@pytest.mark.flaky(reruns=2)
def test_set_cipher_list_no_cipher_match(self, context):
"""
Expand Down