Come verificare che la stretta di mano TLS fosse finalizzata in Twisted
Domanda
Questo è un follow -up di questa domanda: Errori di stretta di mano SSL Quando non sono stati inviati dati su TLSConnection TRISTED
Ho implementato un semplice server SSL che chiude la connessione non appena il client è connesso.
Lo sto testando con OpenSSL e ho ottenuto questo fallimento della stretta di mano:
$ openssl s_client -connect localhost:12345
CONNECTED(00000003) 2329:error:140790E5:SSL routines:SSL23_WRITE
:ssl handshake failure:s23_lib.c:188:
Il problema è che TLS.Connection.loseConnection
Non attende che la stretta di mano in corso venga eseguita e disconnette semplicemente il cliente.
Un callback allegato a OpenSSL.SSL.Connection.do_handshake
Sarebbe stato fantastico ... ma sfortunatamente non so se questo può essere fatto ... o come farlo.
Qualsiasi suggerimento su come ho potuto testare che è stata fatta una stretta di mano TLS è molto apprezzato. Grazie molto!
Ecco il codice
class ApplicationProtocol(Protocol):
'''Protocol that closes the connection when connection is made.'''
def connectionMade(self):
self.transport.loseConnection()
# Here is a barebone TLS Server
serverFactory = ServerFactory()
serverFactory.protocol = ApplicationProtocol
server_cert_path = 'server.pem'
serverContextFactory = DefaultOpenSSLContextFactory(
privateKeyFileName = server_cert_path,
certificateFileName = server_cert_path,
sslmethod=SSL.SSLv23_METHOD)
tlsFactory = TLSMemoryBIOFactory(serverContextFactory, False, serverFactory)
reactor.listenTCP(12345, tlsFactory)
#reactor.listenSSL(12345, serverFactory, serverContextFactory)
Per ora lo risolvo davvero sporco e non valido al 100%.
def tls_lose_connection(self):
"""
Monkey patching for TLSMemoryBIOProtocol to wait for handshake to end,
before closing the connection.
Send a TLS close alert and close the underlying connection.
"""
def close_connection():
self.disconnecting = True
if not self._writeBlockedOnRead:
self._tlsConnection.shutdown()
self._flushSendBIO()
self.transport.loseConnection()
# If we don't know if the handshake was done, we wait for a bit
# and the close the connection.
# This is done to avoid closing the connection in the middle of a
# handshake.
if not self._handshakeDone:
reactor.callLater(0.5, close_connection)
else:
close_connection()
TLSMemoryBIOProtocol.loseConnection = tls_lose_connection
Nessuna soluzione corretta