Skip to content

Commit e075631

Browse files
[3.11] gh-96931: Fix incorrect results in ssl.SSLSocket.shared_ciphers (GH-96932) (#102918)
gh-96931: Fix incorrect results in ssl.SSLSocket.shared_ciphers (GH-96932) (cherry picked from commit af9c34f) Co-authored-by: Benjamin Fogle <[email protected]>
1 parent fd43fb6 commit e075631

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

Doc/library/ssl.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,7 @@ SSL sockets also have the following additional methods and attributes:
13091309

13101310
.. method:: SSLSocket.shared_ciphers()
13111311

1312-
Return the list of ciphers shared by the client during the handshake. Each
1312+
Return the list of ciphers available in both the client and server. Each
13131313
entry of the returned list is a three-value tuple containing the name of the
13141314
cipher, the version of the SSL protocol that defines its use, and the number
13151315
of secret bits the cipher uses. :meth:`~SSLSocket.shared_ciphers` returns

Lib/test/test_ssl.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2326,13 +2326,13 @@ def test_bio_handshake(self):
23262326
self.assertIs(sslobj._sslobj.owner, sslobj)
23272327
self.assertIsNone(sslobj.cipher())
23282328
self.assertIsNone(sslobj.version())
2329-
self.assertIsNotNone(sslobj.shared_ciphers())
2329+
self.assertIsNone(sslobj.shared_ciphers())
23302330
self.assertRaises(ValueError, sslobj.getpeercert)
23312331
if 'tls-unique' in ssl.CHANNEL_BINDING_TYPES:
23322332
self.assertIsNone(sslobj.get_channel_binding('tls-unique'))
23332333
self.ssl_io_loop(sock, incoming, outgoing, sslobj.do_handshake)
23342334
self.assertTrue(sslobj.cipher())
2335-
self.assertIsNotNone(sslobj.shared_ciphers())
2335+
self.assertIsNone(sslobj.shared_ciphers())
23362336
self.assertIsNotNone(sslobj.version())
23372337
self.assertTrue(sslobj.getpeercert())
23382338
if 'tls-unique' in ssl.CHANNEL_BINDING_TYPES:
@@ -4302,7 +4302,7 @@ def cb_wrong_return_type(ssl_sock, server_name, initial_context):
43024302
def test_shared_ciphers(self):
43034303
client_context, server_context, hostname = testing_context()
43044304
client_context.set_ciphers("AES128:AES256")
4305-
server_context.set_ciphers("AES256")
4305+
server_context.set_ciphers("AES256:eNULL")
43064306
expected_algs = [
43074307
"AES256", "AES-256",
43084308
# TLS 1.3 ciphers are always enabled
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix incorrect results from :meth:`ssl.SSLSocket.shared_ciphers`

Modules/_ssl.c

+28-8
Original file line numberDiff line numberDiff line change
@@ -2000,24 +2000,44 @@ static PyObject *
20002000
_ssl__SSLSocket_shared_ciphers_impl(PySSLSocket *self)
20012001
/*[clinic end generated code: output=3d174ead2e42c4fd input=0bfe149da8fe6306]*/
20022002
{
2003-
STACK_OF(SSL_CIPHER) *ciphers;
2004-
int i;
2003+
STACK_OF(SSL_CIPHER) *server_ciphers;
2004+
STACK_OF(SSL_CIPHER) *client_ciphers;
2005+
int i, len;
20052006
PyObject *res;
2007+
const SSL_CIPHER* cipher;
2008+
2009+
/* Rather than use SSL_get_shared_ciphers, we use an equivalent algorithm because:
2010+
2011+
1) It returns a colon seperated list of strings, in an undefined
2012+
order, that we would have to post process back into tuples.
2013+
2) It will return a truncated string with no indication that it has
2014+
done so, if the buffer is too small.
2015+
*/
20062016

2007-
ciphers = SSL_get_ciphers(self->ssl);
2008-
if (!ciphers)
2017+
server_ciphers = SSL_get_ciphers(self->ssl);
2018+
if (!server_ciphers)
20092019
Py_RETURN_NONE;
2010-
res = PyList_New(sk_SSL_CIPHER_num(ciphers));
2020+
client_ciphers = SSL_get_client_ciphers(self->ssl);
2021+
if (!client_ciphers)
2022+
Py_RETURN_NONE;
2023+
2024+
res = PyList_New(sk_SSL_CIPHER_num(server_ciphers));
20112025
if (!res)
20122026
return NULL;
2013-
for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
2014-
PyObject *tup = cipher_to_tuple(sk_SSL_CIPHER_value(ciphers, i));
2027+
len = 0;
2028+
for (i = 0; i < sk_SSL_CIPHER_num(server_ciphers); i++) {
2029+
cipher = sk_SSL_CIPHER_value(server_ciphers, i);
2030+
if (sk_SSL_CIPHER_find(client_ciphers, cipher) < 0)
2031+
continue;
2032+
2033+
PyObject *tup = cipher_to_tuple(cipher);
20152034
if (!tup) {
20162035
Py_DECREF(res);
20172036
return NULL;
20182037
}
2019-
PyList_SET_ITEM(res, i, tup);
2038+
PyList_SET_ITEM(res, len++, tup);
20202039
}
2040+
Py_SET_SIZE(res, len);
20212041
return res;
20222042
}
20232043

0 commit comments

Comments
 (0)