-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
gh-137197: Add SSLContext.set_ciphersuites to set TLS 1.3 ciphers #137198
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
Changes from 5 commits
5a1cc22
09534fd
6783fe6
d3375f1
48e5164
11b760e
eaae575
e09017f
caf6675
fe8791d
4566478
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1684,19 +1684,30 @@ to speed up repeated connections from the same clients. | |
|
||
.. method:: SSLContext.set_ciphers(ciphers) | ||
|
||
Set the available ciphers for sockets created with this context. | ||
It should be a string in the `OpenSSL cipher list format | ||
Set the allowed ciphers for sockets created with this context when | ||
connecting using TLS 1.2 and earlier. The *ciphers* argument should | ||
be a string in the `OpenSSL cipher list format | ||
<https://docs.openssl.org/master/man1/ciphers/>`_. | ||
To set allowed TLS 1.3 ciphers, use :meth:`SSLContext.set_ciphersuites`. | ||
If no cipher can be selected (because compile-time options or other | ||
configuration forbids use of all the specified ciphers), an | ||
:class:`SSLError` will be raised. | ||
|
||
.. note:: | ||
when connected, the :meth:`SSLSocket.cipher` method of SSL sockets will | ||
give the currently selected cipher. | ||
return details about the negotiated cipher. | ||
|
||
TLS 1.3 cipher suites cannot be disabled with | ||
:meth:`~SSLContext.set_ciphers`. | ||
.. method:: SSLContext.set_ciphersuites(ciphersuites) | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Set the allowed ciphers for sockets created with this context when | ||
connecting using TLS 1.3. The *ciphersuites* argument should be a | ||
colon-separate string of TLS 1.3 cipher names. If no cipher can be | ||
selected (because compile-time options or other configuration forbids | ||
use of all the specified ciphers), an :class:`SSLError` will be raised. | ||
|
||
.. note:: | ||
when connected, the :meth:`SSLSocket.cipher` method of SSL sockets will | ||
return details about the negotiated cipher. | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. method:: SSLContext.set_groups(groups) | ||
|
||
|
@@ -2844,10 +2855,15 @@ TLS 1.3 | |
The TLS 1.3 protocol behaves slightly differently than previous version | ||
of TLS/SSL. Some new TLS 1.3 features are not yet available. | ||
|
||
- TLS 1.3 uses a disjunct set of cipher suites. All AES-GCM and | ||
ChaCha20 cipher suites are enabled by default. The method | ||
:meth:`SSLContext.set_ciphers` cannot enable or disable any TLS 1.3 | ||
ciphers yet, but :meth:`SSLContext.get_ciphers` returns them. | ||
- TLS 1.3 uses a disjunct set of cipher suites. All AES-GCM and ChaCha20 | ||
cipher suites are enabled by default. To restrict which TLS1.3 ciphers | ||
picnixz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
are allowed, the method :meth:`SSLContext.set_ciphersuites` should be | ||
|
||
called instead of :meth:`SSLContext.set_ciphers`, which only affects | ||
ciphers in older TLS versions. The method :meth:`SSLContext.get_ciphers` | ||
returns information about ciphers for both TLS 1.3 and earlier versions | ||
and the method :meth:`SSLSocket.cipher` returns information about the | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
negotiated cipher for both TLS 1.3 and earlier versions once a connection | ||
is established. | ||
- Session tickets are no longer sent as part of the initial handshake and | ||
are handled differently. :attr:`SSLSocket.session` and :class:`SSLSession` | ||
are not compatible with TLS 1.3. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
:mod:`ssl` can now set TLS 1.3 cipher suites. | ||
picnixz marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3595,12 +3595,27 @@ _ssl__SSLContext_set_ciphers_impl(PySSLContext *self, const char *cipherlist) | |
{ | ||
int ret = SSL_CTX_set_cipher_list(self->ctx, cipherlist); | ||
if (ret == 0) { | ||
/* Clearing the error queue is necessary on some OpenSSL versions, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't remove this without understanding which OpenSSL versions it was referring to and whether they are still in use by CPython builds (1.1.x-ish API'd AWS-LC at a minimum, otherwise OpenSSL 3.0+). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The clearing of the error queue is still happening here. I just took advantage of an existing helper function static PyObject *
_setSSLError (_sslmodulestate *state, const char *errstr, int errcode, const char *filename, int lineno)
{
if (errstr == NULL)
errcode = ERR_peek_last_error();
else
errcode = 0;
fill_and_set_sslerror(state, NULL, state->PySSLErrorObject, errcode, errstr, lineno, errcode);
ERR_clear_error();
return NULL;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep it that way and change it in a follow-up PR instead. Actually, the |
||
otherwise the error will be reported again when another SSL call | ||
is done. */ | ||
ERR_clear_error(); | ||
PyErr_SetString(get_state_ctx(self)->PySSLErrorObject, | ||
"No cipher can be selected."); | ||
_setSSLError(get_state_ctx(self), "No cipher can be selected.", 0, __FILE__, __LINE__); | ||
return NULL; | ||
} | ||
Py_RETURN_NONE; | ||
} | ||
|
||
/*[clinic input] | ||
@critical_section | ||
_ssl._SSLContext.set_ciphersuites | ||
ciphersuites: str | ||
/ | ||
[clinic start generated code]*/ | ||
|
||
static PyObject * | ||
_ssl__SSLContext_set_ciphersuites_impl(PySSLContext *self, | ||
const char *ciphersuites) | ||
/*[clinic end generated code: output=9915bec58e54d76d input=2afcc3693392be41]*/ | ||
{ | ||
int ret = SSL_CTX_set_ciphersuites(self->ctx, ciphersuites); | ||
if (ret == 0) { | ||
_setSSLError(get_state_ctx(self), "No cipher suite can be selected.", 0, __FILE__, __LINE__); | ||
return NULL; | ||
} | ||
Py_RETURN_NONE; | ||
|
@@ -5583,6 +5598,7 @@ static struct PyMethodDef context_methods[] = { | |
_SSL__SSLCONTEXT__WRAP_SOCKET_METHODDEF | ||
_SSL__SSLCONTEXT__WRAP_BIO_METHODDEF | ||
_SSL__SSLCONTEXT_SET_CIPHERS_METHODDEF | ||
_SSL__SSLCONTEXT_SET_CIPHERSUITES_METHODDEF | ||
_SSL__SSLCONTEXT_SET_GROUPS_METHODDEF | ||
_SSL__SSLCONTEXT__SET_ALPN_PROTOCOLS_METHODDEF | ||
_SSL__SSLCONTEXT_LOAD_CERT_CHAIN_METHODDEF | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.