これは、この質問のフォローアップです。TwistedTLSConnectionを介してデータが送信されなかった場合のSSLハンドシェイクの失敗
クライアントが接続されるとすぐに接続を閉じる単純なSSLサーバーを実装しました。
私はopensslでテストしていますが、このハンドシェイクの失敗が発生しました。
$ openssl s_client -connect localhost:12345
CONNECTED(00000003) 2329:error:140790E5:SSL routines:SSL23_WRITE
:ssl handshake failure:s23_lib.c:188:
問題はTLS.Connection.loseConnection
、進行中のハンドシェイクが完了するのを待たずに、クライアントを切断することです。
に添付されたコールバックOpenSSL.SSL.Connection.do_handshake
は素晴らしかったでしょう...しかし残念ながら、これができるかどうか...またはそれを行う方法がわかりません。
TLSハンドシェイクが行われたことをテストする方法についてのヒントは大歓迎です。どうもありがとう!
これがコードです
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)
今のところ、私はこれを本当に汚く解決し、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