@@ -81,7 +81,9 @@ class WolfSSL(object):
8181
8282 @classmethod
8383 def enable_debug (self ):
84- _lib .wolfSSL_Debugging_ON ()
84+ if _lib .wolfSSL_Debugging_ON () != _SSL_SUCCESS :
85+ raise RuntimeError (
86+ "wolfSSL debugging not available" )
8587
8688 @classmethod
8789 def disable_debug (self ):
@@ -356,9 +358,10 @@ def load_verify_locations(self, cafile=None, capath=None, cadata=None):
356358 raise SSLError ("Unable to load verify locations. E(%d)" % ret )
357359
358360 if cadata is not None :
361+ cadata_bytes = t2b (cadata )
359362 ret = _lib .wolfSSL_CTX_load_verify_buffer (
360- self .native_object , t2b ( cadata ) ,
361- len (cadata ), _SSL_FILETYPE_PEM )
363+ self .native_object , cadata_bytes ,
364+ len (cadata_bytes ), _SSL_FILETYPE_PEM )
362365
363366 if ret != _SSL_SUCCESS :
364367 raise SSLError ("Unable to load verify locations. E(%d)" % ret )
@@ -476,8 +479,11 @@ def __init__(self, sock=None, keyfile=None, certfile=None,
476479 ret = _lib .wolfSSL_check_domain_name (self .native_object ,
477480 sni )
478481 if ret != _SSL_SUCCESS :
479- raise SSLError ("Unable to set domain name check for "
480- "hostname verification" )
482+ self ._release_native_object ()
483+ raise SSLError (
484+ "Unable to set domain name "
485+ "check for hostname "
486+ "verification" )
481487
482488 if connected :
483489 try :
@@ -606,13 +612,6 @@ def sendall(self, data, flags=0):
606612
607613 while sent < length :
608614 ret = self .write (data [sent :])
609- if (ret <= 0 ):
610- #expect to receive 0 when peer is reset or closed
611- err = _lib .wolfSSL_get_error (self .native_object , 0 )
612- if err == _SSL_ERROR_WANT_WRITE :
613- raise SSLWantWriteError ()
614- else :
615- raise SSLError ("wolfSSL_write error (%d)" % err )
616615
617616 sent += ret
618617
@@ -683,7 +682,7 @@ def recv_into(self, buffer, nbytes=None, flags=0):
683682 if buffer is None :
684683 raise ValueError ("buffer cannot be None" )
685684
686- if nbytes is None :
685+ if nbytes is None or nbytes == 0 :
687686 nbytes = len (buffer )
688687 else :
689688 nbytes = min (len (buffer ), nbytes )
@@ -724,7 +723,9 @@ def recvmsg_into(self, *args, **kwargs):
724723
725724 def shutdown (self , how ):
726725 if self .native_object != _ffi .NULL :
727- _lib .wolfSSL_shutdown (self .native_object )
726+ ret = _lib .wolfSSL_shutdown (self .native_object )
727+ if ret == 0 :
728+ _lib .wolfSSL_shutdown (self .native_object )
728729 self ._release_native_object ()
729730 if self ._context .protocol < PROTOCOL_DTLSv1 :
730731 self ._sock .shutdown (how )
@@ -823,18 +824,16 @@ def _real_connect(self, addr, connect_ex):
823824 raise ValueError ("attempt to connect already-connected SSLSocket!" )
824825
825826 err = 0
826- ret = _SSL_SUCCESS
827-
827+
828828 if self ._context .protocol >= PROTOCOL_DTLSv1 :
829- self .add_peer (addr )
829+ self .add_peer (addr )
830830 else :
831831 if connect_ex :
832832 err = self ._sock .connect_ex (addr )
833833 else :
834- err = 0
835834 self ._sock .connect (addr )
836835
837- if err == 0 and ret == _SSL_SUCCESS :
836+ if err == 0 :
838837 self ._connected = True
839838 if self .do_handshake_on_connect :
840839 self .do_handshake ()
@@ -912,6 +911,9 @@ def version(self):
912911 # Socket object. These are also exposed through Python's ssl module
913912 # API and are provided here for compatibility.
914913 def close (self ):
914+ if self .native_object != _ffi .NULL :
915+ _lib .wolfSSL_shutdown (self .native_object )
916+ self ._release_native_object ()
915917 self ._sock .close ()
916918
917919 def fileno (self ):
@@ -1041,12 +1043,17 @@ def callback(self):
10411043 def _get_passwd (self , passwd , sz , rw , userdata ):
10421044 try :
10431045 result = self ._passwd_wrapper (sz , rw , userdata )
1044- if not isinstance (result , bytes ):
1045- raise ValueError ("Problem, expected String, not bytes" )
1046- if len (result ) > sz :
1047- raise ValueError ("Problem with password returned being long" )
1048- for i in range (len (result )):
1049- passwd [i ] = result [i :i + 1 ]
1050- return len (result )
1051- except Exception as e :
1052- raise ValueError ("Problem getting password from callback" )
1046+ except Exception :
1047+ raise ValueError (
1048+ "Problem getting password from callback" )
1049+ if not isinstance (result , bytes ):
1050+ raise ValueError (
1051+ "Password callback must return bytes,"
1052+ " not str" )
1053+ if len (result ) > sz :
1054+ raise ValueError (
1055+ "Problem with password returned"
1056+ " being long" )
1057+ for i in range (len (result )):
1058+ passwd [i ] = result [i :i + 1 ]
1059+ return len (result )
0 commit comments