Skip to content

Commit 304c223

Browse files
committed
gh-136306: Address additional review comments
1 parent 05c75a5 commit 304c223

2 files changed

Lines changed: 15 additions & 17 deletions

File tree

Doc/library/ssl.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,8 @@ SSL sockets also have the following additional methods and attributes:
12891289
Return the group used for doing key agreement on this connection. If no
12901290
connection has been established, returns ``None``.
12911291

1292+
.. versionadded:: next
1293+
12921294
.. method:: SSLSocket.compression()
12931295

12941296
Return the compression algorithm being used as a string, or ``None``
@@ -1653,10 +1655,10 @@ to speed up repeated connections from the same clients.
16531655
values. For example::
16541656

16551657
>>> ctx = ssl.create_default_context()
1656-
>>> ctx.minimum_version=ssl.TLSVersion.TLSv1_3
1657-
>>> ctx.maximum_version=ssl.TLSVersion.TLSv1_3
1658-
>>> ctx.get_groups()
1659-
['secp256r1', 'secp384r1', 'secp521r1', 'x25519', 'x448', 'brainpoolP256r1tls13', 'brainpoolP384r1tls13', 'brainpoolP512r1tls13', 'ffdhe2048', 'ffdhe3072', 'ffdhe4096', 'ffdhe6144', 'ffdhe8192', 'MLKEM512', 'MLKEM768', 'MLKEM1024', 'SecP256r1MLKEM768', 'X25519MLKEM768', 'SecP384r1MLKEM1024']
1658+
>>> ctx.minimum_version = ssl.TLSVersion.TLSv1_3
1659+
>>> ctx.maximum_version = ssl.TLSVersion.TLSv1_3
1660+
>>> ctx.get_groups() # doctest: +SKIP
1661+
['secp256r1', 'secp384r1', 'secp521r1', 'x25519', 'x448', ...]
16601662

16611663
By default, this method returns only the preferred IANA names for the
16621664
available groups. However, if the ``include_aliases`` parameter is set to
@@ -1700,7 +1702,7 @@ to speed up repeated connections from the same clients.
17001702
When connected, the :meth:`SSLSocket.group` method of SSL sockets will
17011703
return the group used for key agreement on that connection.
17021704

1703-
.. versionadded:: 3.15
1705+
.. versionadded:: next
17041706

17051707
.. method:: SSLContext.set_alpn_protocols(protocols)
17061708

Modules/_ssl.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,12 +2157,10 @@ _ssl__SSLSocket_group_impl(PySSLSocket *self)
21572157
if (self->ssl == NULL) {
21582158
Py_RETURN_NONE;
21592159
}
2160-
21612160
group_name = SSL_get0_group_name(self->ssl);
21622161
if (group_name == NULL) {
21632162
Py_RETURN_NONE;
21642163
}
2165-
21662164
return PyUnicode_DecodeFSDefault(group_name);
21672165
#else
21682166
PyErr_SetString(PyExc_NotImplementedError,
@@ -3467,18 +3465,13 @@ _ssl__SSLContext_get_groups_impl(PySSLContext *self, int include_aliases)
34673465
size_t i, num;
34683466
PyObject *item, *result = NULL;
34693467

3468+
// This "groups" object is dynamically allocated, but the strings inside
3469+
// it are internal constants which shouldn't ever be modified or freed.
34703470
if ((groups = sk_OPENSSL_CSTRING_new_null()) == NULL) {
34713471
_setSSLError(get_state_ctx(self), "Can't allocate stack", 0, __FILE__, __LINE__);
3472-
goto error;
3472+
goto error;
34733473
}
34743474

3475-
/*
3476-
* Note: The "groups" stack is dynamically allocated, but the strings
3477-
* returned in the stack are references to internal constants which
3478-
* should NOT be modified or freed. They should also be plain ASCII,
3479-
* so there should be no decoding issue when converting to Unicode.
3480-
*/
3481-
34823475
if (!SSL_CTX_get0_implemented_groups(self->ctx, include_aliases, groups)) {
34833476
_setSSLError(get_state_ctx(self), "Can't get groups", 0, __FILE__, __LINE__);
34843477
goto error;
@@ -3492,11 +3485,14 @@ _ssl__SSLContext_get_groups_impl(PySSLContext *self, int include_aliases)
34923485
}
34933486

34943487
for (i = 0; i < num; ++i) {
3488+
// There's no allocation here, so group won't ever be NULL.
34953489
group = sk_OPENSSL_CSTRING_value(groups, i);
3496-
assert(group != NULL);
3490+
assert(group != NULL);
34973491

3492+
// Group names are plain ASCII, so there's no chance of a decoding
3493+
// error here. However, an allocation failure could occur when
3494+
// constructing the Unicode version of the names.
34983495
item = PyUnicode_DecodeFSDefault(group);
3499-
35003496
if (item == NULL) {
35013497
_setSSLError(get_state_ctx(self), "Can't allocate group name", 0, __FILE__, __LINE__);
35023498
goto error;

0 commit comments

Comments
 (0)