netwrapper client done
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
ad754440dc
commit
c61ea16c02
@ -1,97 +1,117 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
import random
|
||||||
|
import string
|
||||||
|
import json
|
||||||
|
from base64 import b64encode, b64decode
|
||||||
|
import pyDH
|
||||||
|
from Crypto.Cipher import PKCS1_OAEP
|
||||||
|
from Crypto.PublicKey import RSA
|
||||||
|
from Crypto.Cipher import ChaCha20
|
||||||
|
from Crypto.Random import get_random_bytes
|
||||||
|
|
||||||
from netsim import network_interface
|
from netsim import network_interface
|
||||||
|
|
||||||
|
|
||||||
class NetWrapper:
|
class NetWrapper:
|
||||||
SERVER_ADDRESS = 'A'
|
SERVER_ADDRESS = 'A'
|
||||||
|
|
||||||
def __init__(self, publicKey: str, privateKey: str, clientAddress: str, username: str, cipherKey: str = ""):
|
def __init__(self, privateKey: str, clientAddress: str, username: str, serverPubKey: str = "",
|
||||||
|
serverAddr: str = 'A'):
|
||||||
# Create network_interface: network_interface(path, addr) path root is shared with network / addr is own address
|
# Create network_interface: network_interface(path, addr) path root is shared with network / addr is own address
|
||||||
self.network = network_interface('./network/', clientAddress)
|
self.network = network_interface('./', clientAddress)
|
||||||
|
self.serverAddr = serverAddr
|
||||||
self.username = username
|
self.username = username
|
||||||
self.publicKey = publicKey
|
|
||||||
self.privateKey = privateKey
|
self.privateKey = privateKey
|
||||||
self.cipherKey = cipherKey
|
self.serverPubKey = serverPubKey
|
||||||
|
self.cipherkey="".encode('UTF-8')
|
||||||
|
|
||||||
def identifyServer(self) -> bytes:
|
def randomStringGenerator(self, str_size: int = 512,
|
||||||
# Message is coded with Server RSA public key in string byte format
|
allowed_chars: str = string.ascii_letters + string.punctuation) -> str:
|
||||||
# Creat json with format:
|
return ''.join(random.choice(allowed_chars) for x in range(str_size))
|
||||||
# fos = {
|
|
||||||
# 'type':'IDY',
|
|
||||||
# 'source':self.network.own_addr,
|
|
||||||
# 'username': self.username,
|
|
||||||
# 'message': message <- random generalt, hogy biztos ne legyen elore ismert a challange
|
|
||||||
# }
|
|
||||||
# jsonmsg = json.dump(fos)
|
|
||||||
# asd = jsonmsg.encode('UTF-8')
|
|
||||||
# network_interface.send_msg(self.SERVER_ADDRESS,asd)
|
|
||||||
# Listen for response
|
|
||||||
# status, msg = network_interface.receive_msg(blocking=True) -> status is boolean flag, msg is message
|
|
||||||
# truemsg = msg.decode('UTF-8') nezd meg, h ez tenyleg bajttomb-e, testverem
|
|
||||||
# jsonmsg = json.loads(truemsg)
|
|
||||||
# plaintextrcvmsg = jsonmsg['message'] dekodolod rsa-val
|
|
||||||
# if (plaintextrcvmsg == message): akkor goodness else: fail
|
|
||||||
# return status, msg
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
def identifyServer(self) -> bool:
|
||||||
|
randommsg = self.randomStringGenerator()
|
||||||
|
rsakey = RSA.import_key(self.serverPubKey)
|
||||||
|
cipher = PKCS1_OAEP.new(rsakey)
|
||||||
|
identMsg = json.dumps(
|
||||||
|
{'type': 'IDY', 'source': self.network.own_addr, 'username': self.username,
|
||||||
|
'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.')
|
||||||
|
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
|
||||||
|
|
||||||
def createEncryptedChannel(self):
|
def createEncryptedChannel(self):
|
||||||
# pyDH-t hasznalsz
|
dh = pyDH.DiffieHellman()
|
||||||
|
rsakey = RSA.import_key(self.serverPubKey)
|
||||||
# pydh = pyDH() #alapertelmezett cuccok jok
|
cipher = PKCS1_OAEP.new(rsakey)
|
||||||
# mypubkey = pydh.gen_public_key()
|
mypubkey = b64encode(cipher.encrypt(str(dh.gen_public_key()).encode('UTF-8')))
|
||||||
# 'type':'DH',
|
jsonmsg = json.dumps({'type': 'DH', 'source': self.network.own_addr, 'message': mypubkey}).encode('UTF-8')
|
||||||
# 'source':self.network.own_addr
|
self.network.send_msg(self.serverAddr,jsonmsg)
|
||||||
# fos = {,
|
status, msg = self.network.receive_msg(blocking=True)
|
||||||
# 'message': mypubkey <- pls titkosisd a szerver publikus rsa kulcsaval, hogy authentikalt legyen a dh procedura
|
if not status:
|
||||||
# }
|
raise Exception('Network error during connection.')
|
||||||
# jsonmsg = json.dump(fos)
|
decodedmsg = json.loads(msg.decode('UTF-8'))
|
||||||
# asd = jsonmsg.encode('UTF-8')
|
myrsakey = RSA.import_key(self.privateKey)
|
||||||
# self.network.send_msg(asd)
|
mycipher = PKCS1_OAEP.new(myrsakey)
|
||||||
# status, msg = network_interface.receive_msg(blocking=True) -> status is boolean flag, msg is message
|
serverpubkey = int(mycipher.decrypt(b64decode(decodedmsg['message'])).decode('UTF-8'))
|
||||||
# truemsg = msg.decode('UTF-8') nezd meg, h ez tenyleg bajttomb-e, testverem
|
self.cipherkey = dh.gen_shared_key(serverpubkey).encode('UTF-8')
|
||||||
# jsonmsg = json.loads(truemsg)
|
|
||||||
# self.sharedkey = pydh.gen_shared_key(jsonmsg['message']<-dekoldold a sajat publikus rsa kulccsal)
|
|
||||||
# self.chachageci = ChaChaGeci(self.sharedkey) <- a lenyeg, h a kapott osztott kulccsal legyen egy chacha stream ciphered
|
|
||||||
pass
|
|
||||||
|
|
||||||
def authenticate(self, password: str):
|
def authenticate(self, password: str):
|
||||||
# message = self.chachageci.titkosisd_jol_testverem(f'LIN %self.username% %password%')
|
message = f"LIN {self.username} {password}".encode('UTF-8')
|
||||||
# fos = {
|
cipher = ChaCha20.new(self.cipherkey, get_random_bytes(12))
|
||||||
# 'type':'AUT',
|
ciphertext = cipher.encrypt(message)
|
||||||
# 'source':self.network.own_addr,
|
nonce = b64encode(cipher.nonce).decode('UTF-8')
|
||||||
# 'nonce': str
|
ct = b64encode(ciphertext).decode('UTF-8')
|
||||||
# 'message': message
|
sendjson = json.dumps({'type': 'AUT', 'source': self.network.own_addr, 'nonce': nonce, 'message': ct}).encode('UTF-8')
|
||||||
# }
|
self.network.send_msg(self.serverAddr, sendjson)
|
||||||
# jsonmsg = json.dump(fos)
|
status, msg = self.network.receive_msg(blocking=True)
|
||||||
# asd = jsonmsg.encode('UTF-8')
|
if not status:
|
||||||
# network_interface.send_msg(self.SERVER_ADDRESS,asd)
|
raise Exception('Network error during connection.')
|
||||||
# status, msg = network_interface.receive_msg(blocking=True) -> status is boolean flag, msg is message
|
try:
|
||||||
# truemsg = msg.decode('UTF-8') nezd meg, h ez tenyleg bajttomb-e, testverem
|
b64 = json.loads(msg)
|
||||||
# jsonmsg = json.loads(truemsg)
|
retnonce = b64decode(b64['nonce'])
|
||||||
#
|
retciphertext = b64decode(b64['message'])
|
||||||
pass
|
retcipher = ChaCha20.new(self.cipherkey, nonce=retnonce)
|
||||||
|
plaintext = retcipher.decrypt(retciphertext).decode('UTF-8')
|
||||||
|
if plaintext != "OK":
|
||||||
|
raise Exception('Authentication error')
|
||||||
|
except Exception:
|
||||||
|
print("Incorrect decryption")
|
||||||
|
|
||||||
def connectToServer(self):
|
def connectToServer(self):
|
||||||
#self.identifyServer()
|
identStatus = self.identifyServer()
|
||||||
#self.createEncryptedChannel()
|
if not identStatus:
|
||||||
#self.authenticate()
|
raise Exception('Server identification faliure')
|
||||||
pass
|
self.createEncryptedChannel()
|
||||||
|
print('Please enter your password:')
|
||||||
|
pw = input()
|
||||||
|
self.authenticate(pw)
|
||||||
|
|
||||||
def sendMessage(self, message: bytes):
|
def sendMessage(self, message: bytes):
|
||||||
# message: encoding message with chacha20
|
cipher = ChaCha20.new(self.cipherkey, get_random_bytes(12))
|
||||||
# json = {
|
ciphertext = cipher.encrypt(message)
|
||||||
# 'type':'CMD',
|
nonce = b64encode(cipher.nonce).decode('UTF-8')
|
||||||
# 'source':self.network.own_addr,
|
ct = b64encode(ciphertext).decode('UTF-8')
|
||||||
# 'nonce': str
|
sendjson = json.dumps({'type': 'CMD', 'source': self.network.own_addr, 'nonce': nonce, 'message': ct}).encode('UTF-8')
|
||||||
# 'message': message
|
self.network.send_msg(self.serverAddr,sendjson)
|
||||||
# }
|
|
||||||
# message = json.dump()
|
|
||||||
# message.encode('utf-8')
|
|
||||||
# network_interface.send_msg(self.SERVER_ADDRESS,message)
|
|
||||||
pass
|
|
||||||
|
|
||||||
def recieveMessage(self, message: bytes):
|
def recieveMessage(self, message: bytes) -> bytes:
|
||||||
# status, msg = network_interface.receive_msg(blocking=True) -> status is boolean flag, msg is message
|
status, msg = self.network.receive_msg(blocking=True)
|
||||||
# Decode the message with chacha20
|
if not status:
|
||||||
pass
|
raise Exception('Network error during connection.')
|
||||||
|
try:
|
||||||
|
b64 = json.loads(msg)
|
||||||
|
retnonce = b64decode(b64['nonce'])
|
||||||
|
retciphertext = b64decode(b64['message'])
|
||||||
|
retcipher = ChaCha20.new(self.cipherkey, nonce=retnonce)
|
||||||
|
plaintext = retcipher.decrypt(retciphertext)
|
||||||
|
return plaintext
|
||||||
|
except Exception:
|
||||||
|
print("Incorrect decryption")
|
||||||
|
@ -1 +1,2 @@
|
|||||||
pycryptodome
|
pycryptodome
|
||||||
|
pydh
|
Loading…
Reference in New Issue
Block a user