Skip to content

Commit 78c7d52

Browse files
authored
bpo-37120: Add SSLContext.num_tickets (GH-13719)
Signed-off-by: Christian Heimes <[email protected]>
1 parent 47eb223 commit 78c7d52

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

Doc/library/ssl.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,6 +1959,19 @@ to speed up repeated connections from the same clients.
19591959

19601960
.. versionadded:: 3.7
19611961

1962+
.. attribute:: SSLContext.num_tickets
1963+
1964+
Control the number of TLS 1.3 session tickets of a
1965+
:attr:`TLS_PROTOCOL_SERVER` context. The setting has no impact on TLS
1966+
1.0 to 1.2 connections.
1967+
1968+
.. note::
1969+
1970+
This attribute is not available unless the ssl module is compiled
1971+
with OpenSSL 1.1.1 or newer.
1972+
1973+
.. versionadded:: 3.8
1974+
19621975
.. attribute:: SSLContext.options
19631976

19641977
An integer representing the set of SSL options enabled on this context.

Lib/test/test_ssl.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,24 @@ class MySSLObject(ssl.SSLObject):
16341634
obj = ctx.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO())
16351635
self.assertIsInstance(obj, MySSLObject)
16361636

1637+
@unittest.skipUnless(IS_OPENSSL_1_1_1, "Test requires OpenSSL 1.1.1")
1638+
def test_num_tickest(self):
1639+
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
1640+
self.assertEqual(ctx.num_tickets, 2)
1641+
ctx.num_tickets = 1
1642+
self.assertEqual(ctx.num_tickets, 1)
1643+
ctx.num_tickets = 0
1644+
self.assertEqual(ctx.num_tickets, 0)
1645+
with self.assertRaises(ValueError):
1646+
ctx.num_tickets = -1
1647+
with self.assertRaises(TypeError):
1648+
ctx.num_tickets = None
1649+
1650+
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
1651+
self.assertEqual(ctx.num_tickets, 2)
1652+
with self.assertRaises(ValueError):
1653+
ctx.num_tickets = 1
1654+
16371655

16381656
class SSLErrorTests(unittest.TestCase):
16391657

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add SSLContext.num_tickets to control the number of TLSv1.3 session tickets.

Modules/_ssl.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3617,6 +3617,39 @@ set_maximum_version(PySSLContext *self, PyObject *arg, void *c)
36173617
}
36183618
#endif /* SSL_CTRL_GET_MAX_PROTO_VERSION */
36193619

3620+
#if (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER)
3621+
static PyObject *
3622+
get_num_tickets(PySSLContext *self, void *c)
3623+
{
3624+
return PyLong_FromLong(SSL_CTX_get_num_tickets(self->ctx));
3625+
}
3626+
3627+
static int
3628+
set_num_tickets(PySSLContext *self, PyObject *arg, void *c)
3629+
{
3630+
long num;
3631+
if (!PyArg_Parse(arg, "l", &num))
3632+
return -1;
3633+
if (num < 0) {
3634+
PyErr_SetString(PyExc_ValueError, "value must be non-negative");
3635+
return -1;
3636+
}
3637+
if (self->protocol != PY_SSL_VERSION_TLS_SERVER) {
3638+
PyErr_SetString(PyExc_ValueError,
3639+
"SSLContext is not a server context.");
3640+
return -1;
3641+
}
3642+
if (SSL_CTX_set_num_tickets(self->ctx, num) != 1) {
3643+
PyErr_SetString(PyExc_ValueError, "failed to set num tickets.");
3644+
return -1;
3645+
}
3646+
return 0;
3647+
}
3648+
3649+
PyDoc_STRVAR(PySSLContext_num_tickets_doc,
3650+
"Control the number of TLSv1.3 session tickets");
3651+
#endif /* OpenSSL 1.1.1 */
3652+
36203653
static PyObject *
36213654
get_options(PySSLContext *self, void *c)
36223655
{
@@ -4654,6 +4687,10 @@ static PyGetSetDef context_getsetlist[] = {
46544687
(setter) _PySSLContext_set_msg_callback, NULL},
46554688
{"sni_callback", (getter) get_sni_callback,
46564689
(setter) set_sni_callback, PySSLContext_sni_callback_doc},
4690+
#if (OPENSSL_VERSION_NUMBER >= 0x10101000L) && !defined(LIBRESSL_VERSION_NUMBER)
4691+
{"num_tickets", (getter) get_num_tickets,
4692+
(setter) set_num_tickets, PySSLContext_num_tickets_doc},
4693+
#endif
46574694
{"options", (getter) get_options,
46584695
(setter) set_options, NULL},
46594696
{"post_handshake_auth", (getter) get_post_handshake_auth,

0 commit comments

Comments
 (0)