@@ -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 \0 x2\x03 \x80 \xa1 \xc2 \xe3 ' , 128 ,
4498+ b'\x80 \xa1 \xc2 \xe3 \x00 \x01 \0 x2\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" )
44574516class TestPostHandshakeAuth (unittest .TestCase ):
0 commit comments