SecurityΒΆ
See the general description of the security: Security
See the implementation of the cryptography: Cryptography.
Methods, that are called to exchange the keys:
Server_side: (Server-side communication)
def exchange_keys(client_communicator: ClientCommunicator):
"""Exchanges a symmetric `communication key` with the client. The `communication key` is encrypted,
with the public key of the client. After this function, all packets are encrypted with this `communication key`"""
# generate communication_key#
cryptographer = client_communicator.communicator.cryptographer
serialized_communication_key = cryptographer.generate_communication_key()
# wait for public key
IDManager(client_communicator.id).append_dummy_functions(2)
public_key_packet = client_communicator.communicator.wait_for_response()
serialized_public_key = public_key_packet.data["public_key"]
cryptographer.public_key_from_serialized_key(serialized_public_key)
encrypted_communication_key = cryptographer.encrypt_with_public_key(serialized_communication_key)
# send communication key
communication_packet = DataPacket(communication_key=encrypted_communication_key)
client_communicator.communicator.send_packet(communication_packet)
cryptographer.communication_key = serialized_communication_key
client_communicator._exchanged_keys = True
Client_side: (Client-side communication)
def exchange_keys(connector: Union['Connector', Type['SingleConnector']]):
"""Exchanges a symmetric `communication key` with the server. The `communication key` is received from the
server. It is decrypted with the private key. After this function, all packets are encrypted with this
`communication key`"""
# generate public key + private key
cryptographer = connector.communicator.cryptographer
cryptographer.generate_key_pair()
serialized_public_key = cryptographer.get_serialized_public_key()
IDManager(connector.get_id()).append_dummy_functions(2)
# send public key
public_key_packet = DataPacket(public_key=serialized_public_key)
connector.communicator.send_packet(public_key_packet)
# wait for communication key
communication_packet = connector.communicator.wait_for_response()
encrypted_communication_key = communication_packet.data["communication_key"]
# decrypt key with private key
communication_key = cryptographer.decrypt_with_private_key(encrypted_communication_key)
# set communication key
cryptographer.communication_key = communication_key
connector._exchanged_keys = True