better security
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Torma Kristóf 2021-04-25 00:11:00 +02:00
parent 4854a71b32
commit 8eea92a714
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047

View File

@ -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)