Exemplo n.º 1
0
def perform_handshake(accepted_socket: ssl.SSLSocket) -> None:
    """
    Tries to perform a TLS handshake, if handshake don't succeed in 10 seconds, it closes the
    connection

    :param accepted_socket: SSLSocket object wrapping an accepted client socket
    :return: None
    """
    accepted_socket.settimeout(src.Constants.HANDSHAKE_TIMEOUT_SECONDS)
    try:
        accepted_socket.do_handshake()
        accepted_socket.settimeout(None)
    except (socket.timeout, ssl.SSLError, OSError):
        accepted_socket.close()
        exit(0)
    def doSSLHandshake(self, sock: ssl.SSLSocket) -> None:
        """
		Performs an SSL handshake.

		Note:
			The [`SSLSocket.do_handshake`][1] method is non-blocking and must be retried until it returns successfully.
			See [here][2] for further explanation.
			[1]: <https://docs.python.org/library/ssl.html#ssl.SSLSocket.do_handshake>
			[2]: <https://docs.python.org/library/ssl.html#ssl-nonblocking>

		Args:
			sock: The socket to perform the handshake on.
		"""
        while True:
            try:
                sock.do_handshake()
                break
            except ssl.SSLWantReadError:
                select.select([sock], [], [])
            except ssl.SSLWantWriteError:
                select.select([], [sock], [])