Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mg/improved client #22

Merged
merged 5 commits into from
Apr 25, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ venv/

# Custom data
server.log
data/
data/
.vscode/
100 changes: 89 additions & 11 deletions client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from noise.connection import NoiseConnection, Keypair
from sys import argv
import constants
import argparse


class Client:
Expand Down Expand Up @@ -41,22 +42,99 @@ def send_encrypted_msg(self, message: str):
encrypted_message = self.noise.encrypt(bytes(message, encoding="UTF-8"))
self.sock.send(encrypted_message)

def send_messages(self):
print("You can write messages now, [q, quit, exit] to quit:")
while True:
user_input = input("Message: ")
if len(user_input) == 0:
print("Please input valid message, [q, quit, exit] to quit.")
continue
if user_input.lower() in ["q", "quit", "exit"]:
return
self.sock = socket.socket()
self.sock.connect((self.ip, self.port))
self.set_connection_keys()
self.noise_handshake()
self.send_encrypted_msg(user_input)
self.receive_and_decrypt_msg()
self.sock.close()

def receive_and_decrypt_msg(self):
ciphertext = self.sock.recv(constants.CLIENT_PORT)
plaintext = self.noise.decrypt(ciphertext)
print(plaintext)

def run(self, message):
self.sock.connect(("localhost", constants.SERVER_PORT))
self.set_connection_keys()
self.noise_handshake()
self.send_encrypted_msg(message)
self.receive_and_decrypt_msg()
def register(self):
# TODO: read TPMs PCR values and somehow send them to a server
print("Registration complete, your identifier is <TOP_SECRET_PCR_HASH>")
return True

def run(self, ip, port, message):
self.ip = ip
self.port = port
if message: # One time
self.sock.connect((ip, port))
self.set_connection_keys()
self.noise_handshake()
self.send_encrypted_msg(message)
self.receive_and_decrypt_msg()
else: # Multiple messages
self.send_messages()


if __name__ == "__main__":
client = Client()
if len(argv) > 1:
client.run(argv[1])
else:
print("Please write message as argument")
parser = argparse.ArgumentParser(
description="PV204 NoisyTPM - this is a part of team project for PV204. \
Client app can communicate with server using Noise framework \
and authenticate via TPM. Please see \
'https://github.com/Matej4545/PV204-NoisyTPM/' for more info."
)
parser.add_argument(
"-s",
"--server",
dest="server",
metavar="IP",
type=str,
nargs=1,
default="localhost",
help="An IP address or hostname of the server.",
)
parser.add_argument(
"-p",
"--port",
dest="port",
metavar="PORT",
type=int,
nargs=1,
default=5555,
help="A port where the server is listening.",
)
parser.add_argument(
"-m",
"--message",
metavar="MESSAGE",
dest="message",
type=str,
nargs="+",
help="Specify message as argument. For interactive session please omit.",
)
parser.add_argument(
"-r --register",
dest="register",
action="store_true",
default=False,
help="If you are not authenticated or running the app first time, you will need to register.",
)
args = parser.parse_args()

try:
message = "" if args.message is None else "".join(args.message).strip()
server = args.server[0].strip()
port = args.port[0]
client = Client()
if args.register:
client.register()
client.run(server, port, message)
except Exception as e:
print("An error occured! Quitting app.")
print(e)