I would like to handle my TLS handshakes manually with socket and ssl libraries in Python. Here is my code where I try to show what I am trying to achieve.
In my code provided, I want to allow TLS handshakes to reach "handleSocket" function. If there are any other SSL based libraries that can allow to manually handle TLS handshakes I would like to use them. I also need their documentation to be specific on this to avoid it becoming too trial and error.
BUFFER_SIZE = 1024import socketimport threadingimport ssldef wrapSSLSocket(socket): return ssl.wrap_socket( socket, server_side=True, certfile="credentials/serverCert.pem", keyfile="credentials/serverKey.pem", do_handshake_on_connect=False, ssl_version=ssl.PROTOCOL_TLSv1_2 )#IMAGINE I RECEIVE A TLS HANDSHAKE (CLIENT HELLO)def handleSocket(sock: socket.socket): try: # RECEIVE SOCKET sock = wrapSSLSocket(sock) # SSL SWALLOWS MY TLS HANDSHAKE FOR ME while True: data = sock.recv(BUFFER_SIZE).decode() with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as connection: # HANDLE SOCKET break finally: sock.close()def HTTPS(): try: server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind(("0.0.0.0", 443)) server_socket.listen(10) while True: client_socket, _ = server_socket.accept() threading.Thread(target=handleSocket, args=(client_socket,)).start() except Exception: print("Couldn't start HTTPS server.") finally: server_socket.close()if __name__ == "__main__": HTTPS()
I would like to at least receive the TLS handshake to manually handle them in "handleSocket" function. Instead of wrap_socket handling the socket from me entirely.
Any help on ways or example of how to handle TLS handshakes manually with "ssl" or maybe another SSL based library would be great. Thank you.