From 8eea92a714e8befb8d4c35efd8fc99939bfd83ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Sun, 25 Apr 2021 00:11:00 +0200 Subject: [PATCH] better security --- client/netwrapper.py | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/client/netwrapper.py b/client/netwrapper.py index f15f49d..35d916a 100644 --- a/client/netwrapper.py +++ b/client/netwrapper.py @@ -37,12 +37,14 @@ class NetWrapper: 'message': b64encode(cipher.encrypt(randommsg.encode('UTF-8')))}).encode( 'UTF-8') self.network.send_msg(self.serverAddr, identMsg) - status, msg = self.network.receive_msg(blocking=True) - if not status: - raise Exception('Network error during connection.') + returnJson = {'source': '', 'type': ''} + while not (returnJson['source'] == self.serverAddr and returnJson['type'] == 'IDY'): + status, msg = self.network.receive_msg(blocking=True) + if not status: + raise Exception('Network error during connection.') + returnJson = json.loads(msg.decode('UTF-8')) myrsakey = RSA.import_key(self.privateKey) mycipher = PKCS1_OAEP.new(myrsakey) - returnJson = json.loads(msg.decode('UTF-8')) retmsg = mycipher.decrypt(b64decode(returnJson['message'])).decode('UTF-8') return retmsg == randommsg @@ -53,10 +55,12 @@ class NetWrapper: mypubkey = b64encode(cipher.encrypt(str(dh.gen_public_key()).encode('UTF-8'))) jsonmsg = json.dumps({'type': 'DH', 'source': self.network.own_addr, 'message': mypubkey}).encode('UTF-8') self.network.send_msg(self.serverAddr, jsonmsg) - status, msg = self.network.receive_msg(blocking=True) - if not status: - raise Exception('Network error during connection.') - decodedmsg = json.loads(msg.decode('UTF-8')) + decodedmsg = {'source': '', 'type': ''} + while not (decodedmsg['source'] == self.serverAddr and decodedmsg['type'] == 'DH'): + status, msg = self.network.receive_msg(blocking=True) + if not status: + raise Exception('Network error during connection.') + decodedmsg = json.loads(msg.decode('UTF-8')) myrsakey = RSA.import_key(self.privateKey) mycipher = PKCS1_OAEP.new(myrsakey) serverpubkey = int(mycipher.decrypt(b64decode(decodedmsg['message'])).decode('UTF-8')) @@ -70,12 +74,13 @@ class NetWrapper: ct = b64encode(ciphertext).decode('UTF-8') sendjson = json.dumps({'type': 'AUT', 'source': self.network.own_addr, 'nonce': nonce, 'message': ct}).encode( 'UTF-8') - self.network.send_msg(self.serverAddr, sendjson) - status, msg = self.network.receive_msg(blocking=True) - if not status: - raise Exception('Network error during connection.') + b64 = {'source': '', 'type': ''} + while not (b64['source'] == self.serverAddr and b64['type'] == 'AUT'): + status, msg = self.network.receive_msg(blocking=True) + if not status: + raise Exception('Network error during connection.') + b64 = json.loads(msg.decode('UTF-8')) try: - b64 = json.loads(msg) retnonce = b64decode(b64['nonce']) retciphertext = b64decode(b64['message']) retcipher = ChaCha20.new(self.cipherkey, nonce=retnonce) @@ -104,11 +109,13 @@ class NetWrapper: self.network.send_msg(self.serverAddr, sendjson) def recieveMessage(self) -> bytes: - status, msg = self.network.receive_msg(blocking=True) - if not status: - raise Exception('Network error during connection.') + b64 = {'source': '', 'type': ''} + while not (b64['source'] == self.serverAddr and b64['type'] == 'AUT'): + status, msg = self.network.receive_msg(blocking=True) + if not status: + raise Exception('Network error during connection.') + b64 = json.loads(msg.decode('UTF-8')) try: - b64 = json.loads(msg) retnonce = b64decode(b64['nonce']) retciphertext = b64decode(b64['message']) retcipher = ChaCha20.new(self.cipherkey, nonce=retnonce)