Skip to content

Commit df2480d

Browse files
markrwilliamsalex
authored andcommitted
Raise an Error with "no cipher match" even with TLS 1.3 (#818)
* Raise an Error with "no cipher match" even with TLS 1.3 This makes Twisted's OpenSSLAcceptableCiphers.fromOpenSSLCipherString and seamlessly work with TLS 1.3: https://github.com/twisted/twisted/pull/1100/files/a5df2fb373ac67b0e3032acc9291ae88dfd0b3b1#diff-df501bac724aab523150498f84749b88R1767 * Split TestContext.test_set_cipher_list_wrong_args into two tests.
1 parent ca749b5 commit df2480d

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

src/OpenSSL/SSL.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,13 +1189,22 @@ def set_cipher_list(self, cipher_list):
11891189
# invalid cipher string is passed, but without the following check
11901190
# for the TLS 1.3 specific cipher suites it would never error.
11911191
tmpconn = Connection(self, None)
1192-
_openssl_assert(
1193-
tmpconn.get_cipher_list() != [
1192+
if (
1193+
tmpconn.get_cipher_list() == [
11941194
'TLS_AES_256_GCM_SHA384',
11951195
'TLS_CHACHA20_POLY1305_SHA256',
11961196
'TLS_AES_128_GCM_SHA256'
11971197
]
1198-
)
1198+
):
1199+
raise Error(
1200+
[
1201+
(
1202+
'SSL routines',
1203+
'SSL_CTX_set_cipher_list',
1204+
'no cipher match',
1205+
),
1206+
],
1207+
)
11991208

12001209
def set_client_ca_list(self, certificate_authorities):
12011210
"""

tests/test_ssl.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -410,18 +410,31 @@ def test_set_cipher_list(self, context, cipher_string):
410410

411411
assert "AES128-SHA" in conn.get_cipher_list()
412412

413-
@pytest.mark.parametrize("cipher_list,error", [
414-
(object(), TypeError),
415-
("imaginary-cipher", Error),
416-
])
417-
def test_set_cipher_list_wrong_args(self, context, cipher_list, error):
413+
def test_set_cipher_list_wrong_type(self, context):
418414
"""
419415
`Context.set_cipher_list` raises `TypeError` when passed a non-string
420-
argument and raises `OpenSSL.SSL.Error` when passed an incorrect cipher
421-
list string.
416+
argument.
422417
"""
423-
with pytest.raises(error):
424-
context.set_cipher_list(cipher_list)
418+
with pytest.raises(TypeError):
419+
context.set_cipher_list(object())
420+
421+
def test_set_cipher_list_no_cipher_match(self, context):
422+
"""
423+
`Context.set_cipher_list` raises `OpenSSL.SSL.Error` with a
424+
`"no cipher match"` reason string regardless of the TLS
425+
version.
426+
"""
427+
with pytest.raises(Error) as excinfo:
428+
context.set_cipher_list(b"imaginary-cipher")
429+
assert excinfo.value.args == (
430+
[
431+
(
432+
'SSL routines',
433+
'SSL_CTX_set_cipher_list',
434+
'no cipher match',
435+
),
436+
],
437+
)
425438

426439
def test_load_client_ca(self, context, ca_file):
427440
"""

0 commit comments

Comments
 (0)