Question

Ceci est un suivi de cette question: Échecs de poignée de main SSL Lorsqu'aucune donnée n'a été envoyée sur TlSconnection tordue

J'ai implémenté un serveur SSL simple qui ferme la connexion dès que le client est connecté.

Je le teste avec OpenSSL et j'ai eu cette défaillance de poignée de main:

$ openssl s_client -connect localhost:12345                             
CONNECTED(00000003) 2329:error:140790E5:SSL routines:SSL23_WRITE
:ssl handshake failure:s23_lib.c:188: 

Le problème est que TLS.Connection.loseConnection N'attend pas que la poignée de main en cours soit terminée et déconnecte simplement le client.

Un rappel attaché à OpenSSL.SSL.Connection.do_handshake aurait été génial ... mais malheureusement, je ne sais pas si cela peut être fait ... ou comment le faire.

Tout indice dans la façon dont je pouvais tester qu'une poignée de main TLS a été faite est très appréciée. Merci beaucoup!

Voici le code

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)

Pour l'instant, je résolve cela vraiment sale et pas 100% valide.

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

Pas de solution correcte

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top