← Back to DAGDA | Support | Source: https://github.com/python/cpython/commit/eca7a150317f512d13f73c1b0feeda1e5760b7f6
Skip to content

Commit eca7a15

Browse files
committed
bpo-37952: SSL: add support for export_keying_material
Add support for the RFC5705 SSL_export_keying_material function to the Python SSL module.
1 parent 2e9ea7b commit eca7a15

6 files changed

Lines changed: 217 additions & 1 deletion

File tree

‎Doc/library/ssl.rst‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,39 @@ SSL sockets also have the following additional methods and attributes:
13281328

13291329
.. versionadded:: 3.3
13301330

1331+
.. method:: SSLSocket.export_keying_material(label, material_len, context=None)
1332+
1333+
Returns a bytes object with keying material as defined by
1334+
:rfc:`5705` and :rfc:`8446` or None if no keying material is
1335+
available, i.e. before the SSL handshake or after the SSL
1336+
connection is closed.
1337+
1338+
The appliction specific *label* should contain a value from the the
1339+
IANA Exporter Label Registry
1340+
(https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#exporter-labels).
1341+
Labels beginning with "EXPERIMENTAL" can be used without
1342+
registration. *material_len* specifies how many bytes of keying
1343+
material should be returned. The optional appliction specific
1344+
*context* can be used to get multiple distinct keying materials
1345+
from the same application specific label; None means that no
1346+
context will be used.
1347+
1348+
:exc:`ValueError` will be raised if an unsupported channel binding
1349+
type is requested.
1350+
1351+
In TLSv1.2 passing no context (None) will return different keying
1352+
material than a zero length context. In TLSv1.3 not context will
1353+
return the same keying material as a zero length context.
1354+
1355+
If label or context is a string only ASCII is allowed. Convert the
1356+
string to a bytes object using an explicit encoding if you want to
1357+
use non-ASCII data.
1358+
1359+
This method is a wrapper around the SSL_export_keying_material
1360+
function; refer to the OpenSSLfor documentation for more details.
1361+
1362+
.. versionadded:: 3.11
1363+
13311364
.. method:: SSLSocket.get_channel_binding(cb_type="tls-unique")
13321365

13331366
Get channel binding data for current connection, as a bytes object. Returns
@@ -2764,6 +2797,9 @@ of TLS/SSL. Some new TLS 1.3 features are not yet available.
27642797
:rfc:`RFC 5246: The Transport Layer Security (TLS) Protocol Version 1.2 <5246>`
27652798
T. Dierks et. al.
27662799

2800+
:rfc:`RFC 5705: Keying Material Exporters for Transport Layer Security (TLS) <5705>`
2801+
IETF, E. Rescorla
2802+
27672803
:rfc:`RFC 6066: Transport Layer Security (TLS) Extensions <6066>`
27682804
D. Eastlake
27692805

@@ -2773,5 +2809,8 @@ of TLS/SSL. Some new TLS 1.3 features are not yet available.
27732809
:rfc:`RFC 7525: Recommendations for Secure Use of Transport Layer Security (TLS) and Datagram Transport Layer Security (DTLS) <7525>`
27742810
IETF
27752811

2812+
:rfc:`RFC 8446: The Transport Layer Security (TLS) Protocol Version 1.3 <8446>`
2813+
IETF, E. Rescorla
2814+
27762815
`Mozilla's Server Side TLS recommendations <https://wiki.mozilla.org/Security/Server_Side_TLS>`_
27772816
Mozilla

‎Lib/ssl.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,32 @@ def verify_client_post_handshake(self):
13321332
else:
13331333
raise ValueError("No SSL wrapper around " + str(self))
13341334

1335+
def export_keying_material(self, label, material_len, context=None):
1336+
"""Export keying material from current connection. Return a bytes
1337+
object with keying material or None if no keying material is
1338+
available, i.e. before the SSL handshake or after the SSL
1339+
connection is closed
1340+
1341+
The application specific `label` should be a bytes object or
1342+
ASCII string. `material_len` specifies how many byts of
1343+
keying material to return. The optional application specific
1344+
"context" should be a bytes object, ASCII string or None for
1345+
no conxtext.
1346+
1347+
"""
1348+
1349+
if isinstance(label, str):
1350+
label = label.encode('ASCII')
1351+
1352+
if isinstance(context, str):
1353+
context = context.encode('ASCII')
1354+
1355+
self._checkClosed()
1356+
self._check_connected()
1357+
if self._sslobj is None:
1358+
return None
1359+
return self._sslobj.export_keying_material(label, material_len, context)
1360+
13351361
def _real_close(self):
13361362
self._sslobj = None
13371363
super()._real_close()

‎Lib/test/test_ssl.py‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4452,6 +4452,65 @@ def test_session_handling(self):
44524452
self.assertEqual(str(e.exception),
44534453
'Session refers to a different SSLContext.')
44544454

4455+
def export_keying_material_test(self, tls_version):
4456+
client_context, server_context, hostname = testing_context()
4457+
client_context.minimum_version = tls_version
4458+
client_context.maximum_version = tls_version
4459+
server_context.minimum_version = tls_version
4460+
server_context.maximum_version = tls_version
4461+
4462+
with ThreadedEchoServer(context=server_context,
4463+
chatty=False) as server:
4464+
with client_context.wrap_socket(socket.socket(),
4465+
do_handshake_on_connect=False,
4466+
server_hostname=hostname) as s:
4467+
# can not be used before the connection is open
4468+
with self.assertRaises(OSError) as cm:
4469+
s.export_keying_material('foo', 1)
4470+
self.assertEqual(cm.exception.errno, errno.ENOTCONN)
4471+
s.connect((HOST, server.port))
4472+
# should return None before the handshake is finished
4473+
t = s.export_keying_material('foo', 1)
4474+
self.assertEqual(t, None)
4475+
s.do_handshake()
4476+
# material_len must be positive
4477+
with self.assertRaises(ValueError) as cm:
4478+
s.export_keying_material('foo', 0)
4479+
with self.assertRaises(ValueError) as cm:
4480+
s.export_keying_material('foo', -1)
4481+
with self.assertRaises(ValueError) as cm:
4482+
s.export_keying_material('foo', -13)
4483+
# Strings containing non-ASCII labels are not allowed
4484+
with self.assertRaises(UnicodeEncodeError) as cm:
4485+
s.export_keying_material('\u0394', 1)
4486+
with self.assertRaises(UnicodeEncodeError) as cm:
4487+
s.export_keying_material('foo', 1, '\u0394')
4488+
for args in [
4489+
( 'foo', 32 ),
4490+
( 'foo', 32, None ),
4491+
( 'foo', 32, '' ),
4492+
( 'foo', 32, 'bar' ),
4493+
( b'foo', 32, b'bar' ),
4494+
( b'foo', 32, b'bar' ),
4495+
( b'foo', 1, b'bar' ),
4496+
( b'foo', 128, b'bar' ),
4497+
( b'\x00\x01\0x2\x03\x80\xa1\xc2\xe3', 128,
4498+
b'\x80\xa1\xc2\xe3\x00\x01\0x2\x03' ),
4499+
]:
4500+
t = s.export_keying_material(*args)
4501+
self.assertEqual(len(t), args[1])
4502+
s.close()
4503+
# should return None after the socket has been closed
4504+
t = s.export_keying_material('foo', 1)
4505+
self.assertEqual(t, None)
4506+
4507+
@requires_tls_version('TLSv1_2')
4508+
def test_export_keying_material_tlsv1_2(self):
4509+
self.export_keying_material_test(ssl.TLSVersion.TLSv1_2)
4510+
4511+
@requires_tls_version('TLSv1_3')
4512+
def test_export_keying_material_tlsv1_3(self):
4513+
self.export_keying_material_test(ssl.TLSVersion.TLSv1_3)
44554514

44564515
@unittest.skipUnless(has_tls_version('TLSv1_3'), "Test needs TLS 1.3")
44574516
class TestPostHandshakeAuth(unittest.TestCase):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added support for the RFC5705 :func:`ssl.SSLSocket.export_keying_material`

‎Modules/_ssl.c‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2755,6 +2755,53 @@ _ssl__SSLSocket_verify_client_post_handshake_impl(PySSLSocket *self)
27552755
#endif
27562756
}
27572757

2758+
/*[clinic input]
2759+
_ssl._SSLSocket.export_keying_material
2760+
label: Py_buffer(accept={buffer, str})
2761+
material_len: int
2762+
context: Py_buffer(accept={buffer, str, NoneType})
2763+
[clinic start generated code]*/
2764+
2765+
static PyObject *
2766+
_ssl__SSLSocket_export_keying_material_impl(PySSLSocket *self,
2767+
Py_buffer *label,
2768+
int material_len,
2769+
Py_buffer *context)
2770+
/*[clinic end generated code: output=17b975255ccb2984 input=57daff6c33809e2e]*/
2771+
{
2772+
PyObject *out = NULL;
2773+
unsigned char *material;
2774+
2775+
if (material_len < 1) {
2776+
PyErr_SetString(PyExc_ValueError, "material_len must be positive");
2777+
return NULL;
2778+
}
2779+
2780+
if (!SSL_is_init_finished(self->ssl)) {
2781+
/* handshake not finished */
2782+
Py_RETURN_NONE;
2783+
}
2784+
2785+
out = PyBytes_FromStringAndSize(NULL, material_len);
2786+
if (out == NULL)
2787+
goto error;
2788+
2789+
material = (unsigned char *)PyBytes_AS_STRING(out);
2790+
2791+
if (!SSL_export_keying_material(self->ssl,
2792+
material, material_len,
2793+
label->buf, label->len,
2794+
context->buf, context->len,
2795+
context->buf != NULL)) {
2796+
Py_CLEAR(out);
2797+
_setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__);
2798+
goto error;
2799+
}
2800+
2801+
error:
2802+
return out;
2803+
}
2804+
27582805
static SSL_SESSION*
27592806
_ssl_session_dup(SSL_SESSION *session) {
27602807
SSL_SESSION *newsession = NULL;
@@ -2921,6 +2968,7 @@ static PyMethodDef PySSLMethods[] = {
29212968
_SSL__SSLSOCKET_VERIFY_CLIENT_POST_HANDSHAKE_METHODDEF
29222969
_SSL__SSLSOCKET_GET_UNVERIFIED_CHAIN_METHODDEF
29232970
_SSL__SSLSOCKET_GET_VERIFIED_CHAIN_METHODDEF
2971+
_SSL__SSLSOCKET_EXPORT_KEYING_MATERIAL_METHODDEF
29242972
{NULL, NULL}
29252973
};
29262974

‎Modules/clinic/_ssl.c.h‎

Lines changed: 44 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)