TLS handshake fix

- TLS handshake in python is apparently always asynchronous, so it needs
  proper handling of SSLWantReadError and SSLWantWriteError
- also adds a timeout and a proper shutdown if handshake fails
This commit is contained in:
Peter Šurda 2017-01-07 23:42:07 +01:00
parent 541979a159
commit 4f543e14c1
Signed by: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87

View File

@ -294,14 +294,18 @@ class receiveDataThread(threading.Thread):
while True:
try:
self.sslSock.do_handshake()
logger.debug("TLS handshake success")
break
except ssl.SSLError as e:
if e.errno == 2:
select.select([self.sslSock], [self.sslSock], [])
else:
break
except ssl.SSLWantReadError:
logger.debug("Waiting for SSL socket handhake read")
select.select([self.sslSock], [], [], 10)
except ssl.SSLWantWriteError:
logger.debug("Waiting for SSL socket handhake write")
select.select([], [self.sslSock], [], 10)
except:
break
logger.debug("SSL socket handhake failed, shutting down connection")
self.sendDataThreadQueue.put((0, 'shutdown','tls handshake fail'))
return
# Command the corresponding sendDataThread to set its own connectionIsOrWasFullyEstablished variable to True also
self.sendDataThreadQueue.put((0, 'connectionIsOrWasFullyEstablished', (self.services, self.sslSock)))