May-25-2023, 10:39 AM
(This post was last modified: May-25-2023, 10:39 AM by JohnnyCoffee.)
Do I need to send a ( Server Hello ) as a response to the handshake ( Client Hello ) received, but I couldn’t find out why it doesn’t work? If I can help, below is an example:
import socket
import ssl
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to port 443
sock.bind(("", 443))
# Listen for connections
sock.listen(1)
# Accept a connection
conn, addr = sock.accept()
# Create a SSL/TLS context
ctx = ssl.SSLContext()
# Load the server's certificate
ctx.load_cert_chain("server.crt")
# Create a SSL/TLS wrapper around the socket
ssl_sock = ctx.wrap_socket(conn)
# Receive the "ClientHello" message
client_hello = ssl_sock.recv(16384)
# Select the highest version of SSL/TLS common between the client and the server
ssl_version = client_hello[0:2]
# Select a set of encryption algorithms and security parameters supported by both the client and the server
ciphers = ssl.get_ciphers()
# Send the "ServerHello" message to the client
ssl_sock.sendall(b"Server Hello\n" + ssl_version + b"\n" + ciphers + b"\n")
# Receive the "ClientKeyExchange" message from the client
client_key_exchange = ssl_sock.recv(16384)
# Generate a shared secret
shared_secret = ssl.generate_shared_secret(client_key_exchange)
# Encrypt the data stream
ssl_sock.write(shared_secret)
# Receive data from the client
data = ssl_sock.read()
# Decrypt the data
decrypted_data = ssl.decrypt_data(data, shared_secret)
# Print the decrypted data
print(decrypted_data)
# Close the socket
ssl_sock.close()
