1313
1414Functions:
1515
16+ get_sigalgs -- return a list of all available TLS signature
17+ algorithms (requires OpenSSL 3.4 or later)
18+
1619 cert_time_to_seconds -- convert time string used for certificate
1720 notBefore and notAfter functions to integer
1821 seconds past the Epoch (the time values
107110 )
108111from _ssl import txt2obj as _txt2obj , nid2obj as _nid2obj
109112from _ssl import RAND_status , RAND_add , RAND_bytes
110- try :
111- from _ssl import RAND_egd
112- except ImportError :
113- # RAND_egd is not supported on some platforms
114- pass
113+ from _ssl import get_sigalgs
115114
116115
117116from _ssl import (
118117 HAS_SNI , HAS_ECDH , HAS_NPN , HAS_ALPN , HAS_SSLv2 , HAS_SSLv3 , HAS_TLSv1 ,
119- HAS_TLSv1_1 , HAS_TLSv1_2 , HAS_TLSv1_3 , HAS_PSK , HAS_PHA
118+ HAS_TLSv1_1 , HAS_TLSv1_2 , HAS_TLSv1_3 , HAS_PSK , HAS_PSK_TLS13 , HAS_PHA
120119)
121120from _ssl import _DEFAULT_CIPHERS , _OPENSSL_API_VERSION
122121
151150 source = _ssl )
152151
153152PROTOCOL_SSLv23 = _SSLMethod .PROTOCOL_SSLv23 = _SSLMethod .PROTOCOL_TLS
154- _PROTOCOL_NAMES = {value : name for name , value in _SSLMethod .__members__ .items ()}
153+ _PROTOCOL_NAMES = frozendict ({
154+ value : name for name , value in _SSLMethod .__members__ .items ()})
155155
156156_SSLv2_IF_EXISTS = getattr (_SSLMethod , 'PROTOCOL_SSLv2' , None )
157157
@@ -373,6 +373,20 @@ def _ipaddress_match(cert_ipaddress, host_ip):
373373 return ip == host_ip
374374
375375
376+ def _check_sslobject_params (server_side , context = None , server_hostname = None , session = None ):
377+ """Raises a ValueError if SSLObject._create() parameters aren't valid.
378+ """
379+ if server_side :
380+ if server_hostname :
381+ raise ValueError ("server_hostname can only be specified "
382+ "in client mode" )
383+ if session is not None :
384+ raise ValueError ("session can only be specified in "
385+ "client mode" )
386+ if context .check_hostname and not server_hostname :
387+ raise ValueError ("check_hostname requires server_hostname" )
388+
389+
376390DefaultVerifyPaths = namedtuple ("DefaultVerifyPaths" ,
377391 "cafile capath openssl_cafile_env openssl_cafile openssl_capath_env "
378392 "openssl_capath" )
@@ -813,6 +827,8 @@ def __init__(self, *args, **kwargs):
813827 @classmethod
814828 def _create (cls , incoming , outgoing , server_side = False ,
815829 server_hostname = None , session = None , context = None ):
830+ _check_sslobject_params (server_side = server_side , context = context ,
831+ server_hostname = server_hostname , session = session )
816832 self = cls .__new__ (cls )
817833 sslobj = context ._wrap_bio (
818834 incoming , outgoing , server_side = server_side ,
@@ -931,6 +947,18 @@ def cipher(self):
931947 ssl_version, secret_bits)``."""
932948 return self ._sslobj .cipher ()
933949
950+ def group (self ):
951+ """Return the currently selected key agreement group name."""
952+ return self ._sslobj .group ()
953+
954+ def client_sigalg (self ):
955+ """Return the selected client authentication signature algorithm."""
956+ return self ._sslobj .client_sigalg ()
957+
958+ def server_sigalg (self ):
959+ """Return the selected server handshake signature algorithm."""
960+ return self ._sslobj .server_sigalg ()
961+
934962 def shared_ciphers (self ):
935963 """Return a list of ciphers shared by the client during the handshake or
936964 None if this is not a valid server connection.
@@ -975,6 +1003,10 @@ def _sslcopydoc(func):
9751003 return func
9761004
9771005
1006+ class _GiveupOnSSLSendfile (Exception ):
1007+ pass
1008+
1009+
9781010class SSLSocket (socket ):
9791011 """This class implements a subtype of socket.socket that wraps
9801012 the underlying OS socket in an SSL context when necessary, and
@@ -993,15 +1025,8 @@ def _create(cls, sock, server_side=False, do_handshake_on_connect=True,
9931025 context = None , session = None ):
9941026 if sock .getsockopt (SOL_SOCKET , SO_TYPE ) != SOCK_STREAM :
9951027 raise NotImplementedError ("only stream sockets are supported" )
996- if server_side :
997- if server_hostname :
998- raise ValueError ("server_hostname can only be specified "
999- "in client mode" )
1000- if session is not None :
1001- raise ValueError ("session can only be specified in "
1002- "client mode" )
1003- if context .check_hostname and not server_hostname :
1004- raise ValueError ("check_hostname requires server_hostname" )
1028+ _check_sslobject_params (server_side = server_side , context = context ,
1029+ server_hostname = server_hostname , session = session )
10051030
10061031 sock_timeout = sock .gettimeout ()
10071032 kwargs = dict (
@@ -1206,6 +1231,30 @@ def cipher(self):
12061231 else :
12071232 return self ._sslobj .cipher ()
12081233
1234+ @_sslcopydoc
1235+ def group (self ):
1236+ self ._checkClosed ()
1237+ if self ._sslobj is None :
1238+ return None
1239+ else :
1240+ return self ._sslobj .group ()
1241+
1242+ @_sslcopydoc
1243+ def client_sigalg (self ):
1244+ self ._checkClosed ()
1245+ if self ._sslobj is None :
1246+ return None
1247+ else :
1248+ return self ._sslobj .client_sigalg ()
1249+
1250+ @_sslcopydoc
1251+ def server_sigalg (self ):
1252+ self ._checkClosed ()
1253+ if self ._sslobj is None :
1254+ return None
1255+ else :
1256+ return self ._sslobj .server_sigalg ()
1257+
12091258 @_sslcopydoc
12101259 def shared_ciphers (self ):
12111260 self ._checkClosed ()
@@ -1266,15 +1315,26 @@ def sendall(self, data, flags=0):
12661315 return super ().sendall (data , flags )
12671316
12681317 def sendfile (self , file , offset = 0 , count = None ):
1269- """Send a file, possibly by using os. sendfile() if this is a
1270- clear-text socket . Return the total number of bytes sent.
1318+ """Send a file, possibly by using an efficient sendfile() call if
1319+ the system supports it . Return the total number of bytes sent.
12711320 """
1272- if self ._sslobj is not None :
1273- return self ._sendfile_use_send (file , offset , count )
1274- else :
1275- # os.sendfile() works with plain sockets only
1321+ if self ._sslobj is None :
12761322 return super ().sendfile (file , offset , count )
12771323
1324+ if not self ._sslobj .uses_ktls_for_send ():
1325+ return self ._sendfile_use_send (file , offset , count )
1326+
1327+ sendfile = getattr (self ._sslobj , "sendfile" , None )
1328+ if sendfile is None :
1329+ return self ._sendfile_use_send (file , offset , count )
1330+
1331+ try :
1332+ return self ._sendfile_zerocopy (
1333+ sendfile , _GiveupOnSSLSendfile , file , offset , count ,
1334+ )
1335+ except _GiveupOnSSLSendfile :
1336+ return self ._sendfile_use_send (file , offset , count )
1337+
12781338 def recv (self , buflen = 1024 , flags = 0 ):
12791339 self ._checkClosed ()
12801340 if self ._sslobj is not None :
@@ -1484,11 +1544,8 @@ def DER_cert_to_PEM_cert(der_cert_bytes):
14841544 """Takes a certificate in binary DER format and returns the
14851545 PEM version of it as a string."""
14861546
1487- f = str (base64 .standard_b64encode (der_cert_bytes ), 'ASCII' , 'strict' )
1488- ss = [PEM_HEADER ]
1489- ss += [f [i :i + 64 ] for i in range (0 , len (f ), 64 )]
1490- ss .append (PEM_FOOTER + '\n ' )
1491- return '\n ' .join (ss )
1547+ f = str (base64 .b64encode (der_cert_bytes , wrapcol = 64 ), 'ASCII' )
1548+ return f'{ PEM_HEADER } \n { f } \n { PEM_FOOTER } \n '
14921549
14931550def PEM_cert_to_DER_cert (pem_cert_string ):
14941551 """Takes a certificate in ASCII PEM format and returns the
0 commit comments