2626 socket , AF_INET , SOCK_STREAM , SOL_SOCKET , SO_TYPE , error as socket_error
2727)
2828
29- from wolfssl .__about__ import * # pylint: disable=wildcard-import
29+ # pylint: disable=wildcard-import
30+ from wolfssl .__about__ import * # noqa: F401, F403
31+ # pylint: enable=wildcard-import
3032
3133try :
3234 from wolfssl ._ffi import ffi as _ffi
3638
3739from wolfssl .utils import t2b
3840
39- from wolfssl .exceptions import (
41+ from wolfssl .exceptions import ( # noqa: F401
4042 CertificateError , SSLError , SSLEOFError , SSLSyscallError ,
4143 SSLWantReadError , SSLWantWriteError , SSLZeroReturnError
4244)
4345
44- from wolfssl ._methods import (
46+ from wolfssl ._methods import ( # noqa: F401
4547 PROTOCOL_SSLv23 , PROTOCOL_SSLv3 , PROTOCOL_TLSv1 ,
4648 PROTOCOL_TLSv1_1 , PROTOCOL_TLSv1_2 , PROTOCOL_TLS ,
4749 WolfSSLMethod as _WolfSSLMethod
5860
5961_PY3 = sys .version_info [0 ] == 3
6062
63+
6164class SSLContext (object ):
6265 """
6366 An SSLContext holds various SSL-related configuration options and
@@ -83,12 +86,10 @@ def __init__(self, protocol, server_side=False):
8386 # verify_mode initialization needs a valid native_object.
8487 self .verify_mode = CERT_NONE
8588
86-
8789 def __del__ (self ):
8890 if getattr (self , 'native_object' , _ffi .NULL ) != _ffi .NULL :
8991 _lib .wolfSSL_CTX_free (self .native_object )
9092
91-
9293 @property
9394 def verify_mode (self ):
9495 """
@@ -98,7 +99,6 @@ def verify_mode(self):
9899 """
99100 return self ._verify_mode
100101
101-
102102 @verify_mode .setter
103103 def verify_mode (self , value ):
104104 if value not in _VERIFY_MODE_LIST :
@@ -111,7 +111,6 @@ def verify_mode(self, value):
111111 self ._verify_mode ,
112112 _ffi .NULL )
113113
114-
115114 def wrap_socket (self , sock , server_side = False ,
116115 do_handshake_on_connect = True ,
117116 suppress_ragged_eofs = True ):
@@ -129,20 +128,19 @@ def wrap_socket(self, sock, server_side=False,
129128 suppress_ragged_eofs = suppress_ragged_eofs ,
130129 _context = self )
131130
132-
133131 def set_ciphers (self , ciphers ):
134132 """
135133 Set the available ciphers for sockets created with this context. It
136134 should be a string in the wolfSSL cipher list format. If no cipher can
137- be selected (because compile-time options or other configuration forbids
138- use of all the specified ciphers), an SSLError will be raised.
135+ be selected (because compile-time options or other configuration
136+ forbids use of all the specified ciphers), an SSLError will be raised.
139137 """
140- ret = _lib .wolfSSL_CTX_set_cipher_list (self .native_object , t2b (ciphers ))
138+ ret = _lib .wolfSSL_CTX_set_cipher_list (self .native_object ,
139+ t2b (ciphers ))
141140
142141 if ret != _SSL_SUCCESS :
143142 raise SSLError ("Unnable to set cipher list" )
144143
145-
146144 def load_cert_chain (self , certfile , keyfile = None , password = None ):
147145 """
148146 Load a private key and the corresponding certificate. The certfile
@@ -164,16 +162,16 @@ def load_cert_chain(self, certfile, keyfile=None, password=None):
164162 ret = _lib .wolfSSL_CTX_use_certificate_chain_file (
165163 self .native_object , t2b (certfile ))
166164 if ret != _SSL_SUCCESS :
167- raise SSLError ("Unnable to load certificate chain. Err %d" % ret )
165+ raise SSLError (
166+ "Unnable to load certificate chain. E(%d)" % ret )
168167 else :
169168 raise TypeError ("certfile should be a valid filesystem path" )
170169
171170 if keyfile is not None :
172171 ret = _lib .wolfSSL_CTX_use_PrivateKey_file (
173172 self .native_object , t2b (keyfile ), _SSL_FILETYPE_PEM )
174173 if ret != _SSL_SUCCESS :
175- raise SSLError ("Unnable to load private key. Err %d" % ret )
176-
174+ raise SSLError ("Unnable to load private key. E(%d)" % ret )
177175
178176 def load_verify_locations (self , cafile = None , capath = None , cadata = None ):
179177 """
@@ -198,14 +196,15 @@ def load_verify_locations(self, cafile=None, capath=None, cadata=None):
198196 t2b (capath ) if capath else _ffi .NULL )
199197
200198 if ret != _SSL_SUCCESS :
201- raise SSLError ("Unnable to load verify locations. Err %d " % ret )
199+ raise SSLError ("Unnable to load verify locations. E(%d) " % ret )
202200
203201 if cadata is not None :
204202 ret = _lib .wolfSSL_CTX_load_verify_buffer (
205- self .native_object , t2b (cadata ), len (cadata ), _SSL_FILETYPE_PEM )
203+ self .native_object , t2b (cadata ),
204+ len (cadata ), _SSL_FILETYPE_PEM )
206205
207206 if ret != _SSL_SUCCESS :
208- raise SSLError ("Unnable to load verify locations. Err %d " % ret )
207+ raise SSLError ("Unnable to load verify locations. E(%d) " % ret )
209208
210209
211210class SSLSocket (socket ):
@@ -313,35 +312,30 @@ def __init__(self, sock=None, keyfile=None, certfile=None,
313312 try :
314313 if do_handshake_on_connect :
315314 self .do_handshake ()
316- except :
315+ except SSLError :
317316 self ._release_native_object ()
318317 self .close ()
319318 raise
320319
321-
322320 def __del__ (self ):
323321 self ._release_native_object ()
324322
325-
326323 def _release_native_object (self ):
327324 if getattr (self , 'native_object' , _ffi .NULL ) != _ffi .NULL :
328325 _lib .wolfSSL_CTX_free (self .native_object )
329326 self .native_object = _ffi .NULL
330327
331-
332328 @property
333329 def context (self ):
334330 """
335331 Returns the context used by this object.
336332 """
337333 return self ._context
338334
339-
340335 def dup (self ):
341336 raise NotImplementedError ("Can't dup() %s instances" %
342337 self .__class__ .__name__ )
343338
344-
345339 def _check_closed (self , call = None ):
346340 if self .native_object == _ffi .NULL :
347341 raise ValueError ("%s on closed or unwrapped secure channel" % call )
@@ -354,7 +348,6 @@ def _check_connected(self):
354348 # EAGAIN.
355349 self .getpeername ()
356350
357-
358351 def write (self , data ):
359352 """
360353 Write DATA to the underlying secure channel.
@@ -367,15 +360,13 @@ def write(self, data):
367360
368361 return _lib .wolfSSL_write (self .native_object , data , len (data ))
369362
370-
371363 def send (self , data , flags = 0 ):
372364 if flags != 0 :
373365 raise NotImplementedError ("non-zero flags not allowed in calls to "
374366 "send() on %s" % self .__class__ )
375367
376368 return self .write (data )
377369
378-
379370 def sendall (self , data , flags = 0 ):
380371 if flags != 0 :
381372 raise NotImplementedError ("non-zero flags not allowed in calls to "
@@ -389,25 +380,21 @@ def sendall(self, data, flags=0):
389380
390381 return sent
391382
392-
393383 def sendto (self , data , flags_or_addr , addr = None ):
394- # Ensure programs don't send unencrypted data trying to use this method
384+ # Ensures not to send unencrypted data trying to use this method
395385 raise NotImplementedError ("sendto not allowed on instances "
396386 "of %s" % self .__class__ )
397387
398-
399388 def sendmsg (self , * args , ** kwargs ):
400- # Ensure programs don't send unencrypted data trying to use this method
389+ # Ensures not to send unencrypted data trying to use this method
401390 raise NotImplementedError ("sendmsg not allowed on instances "
402391 "of %s" % self .__class__ )
403392
404-
405393 def sendfile (self , file , offset = 0 , count = None ):
406- # Ensure programs don't send unencrypted files trying to use this method
394+ # Ensures not to send unencrypted files trying to use this method
407395 raise NotImplementedError ("sendfile not allowed on instances "
408396 "of %s" % self .__class__ )
409397
410-
411398 def read (self , length = 1024 , buffer = None ):
412399 """
413400 Read up to LENGTH bytes and return them.
@@ -432,49 +419,41 @@ def read(self, length=1024, buffer=None):
432419
433420 return _ffi .buffer (data , length )[:] if length > 0 else b''
434421
435-
436422 def recv (self , length = 1024 , flags = 0 ):
437423 if flags != 0 :
438424 raise NotImplementedError ("non-zero flags not allowed in calls to "
439425 "recv() on %s" % self .__class__ )
440426
441427 return self .read (self , length )
442428
443-
444429 def recv_into (self , buffer , nbytes = None , flags = 0 ):
445430 raise NotImplementedError ("recv_into not allowed on instances "
446431 "of %s" % self .__class__ )
447432
448-
449433 def recvfrom (self , length = 1024 , flags = 0 ):
450- # Ensure programs don't receive encrypted data trying to use this method
434+ # Ensures not to receive encrypted data trying to use this method
451435 raise NotImplementedError ("recvfrom not allowed on instances "
452436 "of %s" % self .__class__ )
453437
454-
455438 def recvfrom_into (self , buffer , nbytes = None , flags = 0 ):
456- # Ensure programs don't receive encrypted data trying to use this method
439+ # Ensures not to receive encrypted data trying to use this method
457440 raise NotImplementedError ("recvfrom_into not allowed on instances "
458441 "of %s" % self .__class__ )
459442
460-
461443 def recvmsg (self , * args , ** kwargs ):
462444 raise NotImplementedError ("recvmsg not allowed on instances of %s" %
463445 self .__class__ )
464446
465-
466447 def recvmsg_into (self , * args , ** kwargs ):
467448 raise NotImplementedError ("recvmsg_into not allowed on instances of "
468449 "%s" % self .__class__ )
469450
470-
471451 def shutdown (self , how ):
472452 if self .native_object != _ffi .NULL :
473453 _lib .wolfSSL_shutdown (self .native_object )
474454 self ._release_native_object ()
475455 socket .shutdown (self , how )
476456
477-
478457 def unwrap (self ):
479458 """
480459 Unwraps the underlying OS socket from the SSL/TLS connection.
@@ -492,7 +471,6 @@ def unwrap(self):
492471
493472 return sock
494473
495-
496474 def do_handshake (self , block = False ):
497475 """
498476 Perform a TLS/SSL handshake.
@@ -504,7 +482,6 @@ def do_handshake(self, block=False):
504482 if ret != _SSL_SUCCESS :
505483 raise SSLError ("do_handshake failed with error %d" % ret )
506484
507-
508485 def _real_connect (self , addr , connect_ex ):
509486 if self .server_side :
510487 raise ValueError ("can't connect in server-side mode" )
@@ -527,23 +504,20 @@ def _real_connect(self, addr, connect_ex):
527504
528505 return err
529506
530-
531507 def connect (self , addr ):
532508 """
533509 Connects to remote ADDR, and then wraps the connection in a secure
534510 channel.
535511 """
536512 self ._real_connect (addr , False )
537513
538-
539514 def connect_ex (self , addr ):
540515 """
541516 Connects to remote ADDR, and then wraps the connection in a secure
542517 channel.
543518 """
544519 return self ._real_connect (addr , True )
545520
546-
547521 def accept (self ):
548522 """
549523 Accepts a new connection from a remote client, and returns a tuple
@@ -580,16 +554,17 @@ def wrap_socket(sock, keyfile=None, certfile=None, server_side=False,
580554 The parameter server_side is a boolean which identifies whether server-side
581555 or client-side behavior is desired from this socket.
582556
583- The parameter cert_reqs specifies whether a certificate is required from the
584- other side of the connection, and whether it will be validated if provided.
557+ The parameter cert_reqs specifies whether a certificate is required from
558+ the other side of the connection, and whether it will be validated if
559+ provided.
585560 It must be one of the three values:
586561
587562 * CERT_NONE (certificates ignored)
588563 * CERT_OPTIONAL (not required, but validated if provided)
589564 * CERT_REQUIRED (required and validated)
590565
591- If the value of this parameter is not CERT_NONE, then the ca_certs parameter
592- must point to a file of CA certificates.
566+ If the value of this parameter is not CERT_NONE, then the ca_certs
567+ parameter must point to a file of CA certificates.
593568
594569 The ca_certs file contains a set of concatenated “certification authority”
595570 certificates, which are used to validate certificates passed from the other
@@ -619,18 +594,18 @@ def wrap_socket(sock, keyfile=None, certfile=None, server_side=False,
619594 +------------------+-------+-----+-------+---------+---------+
620595
621596 Note:
622- Which connections succeed will vary depending on the versions of the ssl
623- providers on both sides of the communication.
597+ Which connections succeed will vary depending on the versions of the
598+ ssl providers on both sides of the communication.
624599
625600 The ciphers parameter sets the available ciphers for this SSL object. It
626601 should be a string in the wolfSSL cipher list format.
627602
628603 The parameter do_handshake_on_connect specifies whether to do the SSL
629604 handshake automatically after doing a socket.connect(), or whether the
630605 application program will call it explicitly, by invoking the
631- SSLSocket.do_handshake() method. Calling SSLSocket.do_handshake() explicitly
632- gives the program control over the blocking behavior of the socket I/O
633- involved in the handshake.
606+ SSLSocket.do_handshake() method. Calling SSLSocket.do_handshake()
607+ explicitly gives the program control over the blocking behavior of the
608+ socket I/O involved in the handshake.
634609
635610 The parameter suppress_ragged_eofs is not supported yet.
636611 """
0 commit comments