-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.py
256 lines (182 loc) · 7.79 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import pickle
from utils import User,CIPHER_KEY_SIZE,KDF
from socket import AF_INET, socket, SOCK_STREAM
from threading import Thread
import sys
import os
import time
import utils
from Crypto.Cipher import AES
import tkinter
authenticated=False
def send(msg,client,raw=False,enc=False):
if(not raw):
msg = msg.encode('utf-8')
if enc:
aes=AES.new(user.senderKey,AES.MODE_EAX)
nonce=aes.nonce
encryptedMsg,tag=aes.encrypt_and_digest(msg)
msg=pickle.dumps((nonce,encryptedMsg,tag))
message_header = f"{len(msg):<{HEADER_LENGTH}}".encode('utf-8')
client_socket.send(message_header + msg)
def receive(client,raw=False):
try:
message_header = client.recv(HEADER_LENGTH)
if not len(message_header):
print("Message Header Not found")
message_length = int(message_header.decode('utf-8').strip())
if(raw):
return client.recv(message_length)
else:
return client.recv(message_length).decode('utf-8')
except:
print("Message Header not in proper format ",message_header)
def checkCMD(cmd,msg):
if(msg[0:len(cmd.lower())]==cmd.lower()):
return True
return False
def keyExchange(user,pubKeys):
user.computeSharedKeys(pubKeys)
user.encryptSenderKeys()
def display(msg):
if MODE=='GUI':
msgs=list(msg.split('\n'))
for i in msgs:
msg_list.insert(tkinter.END, i)
elif MODE=='CLI':
print(msg)
def receiveThread():
global authenticated,user
while True:
msge=receive(client_socket,raw=True)
try:
msg=msge.decode('utf8')
except:
msg=""
if (checkCMD('!login success',msg) or checkCMD('!signup success',msg)):
action="login" if checkCMD('!login success',msg) else "signup"
infodump=receive(client_socket,raw=True)
info=pickle.loads(infodump)
if(action=="login"):
with open("./data/"+info["id"],'rb') as userfile:
user=pickle.load(userfile)
else:
user=User(info['id'],info['p'],info['g'])
with open("./data/"+user.id,'wb+') as userfile:
pickle.dump(user,userfile)
send(str(user.publicKey),client_socket)
elif checkCMD("!new user",msg):
send("!new user",client_socket)
display("Adding user, please wait...")
while(True):
try:
publicKeys=pickle.loads(receive(client_socket,raw=True))
if(user.id not in publicKeys):
publicKeys[user.id]=user.publicKey
keyExchange(user,publicKeys)
send(pickle.dumps(user.encryptedSenderKeys),client_socket,raw=True)
tmp=receive(client_socket,raw=True)
otherSenderKeys=pickle.loads(tmp)
user.decryptSenderKeys(otherSenderKeys)
with open("./data/"+user.id,'wb+') as userfile:
pickle.dump(user,userfile)
authenticated=True
break
except:
display("Waiting for server, please wait...")
time.sleep(0.5)
elif checkCMD('!quit',msg):
user.encryptSenderKeys()
with open("./data/"+user.id,'wb+') as userfile:
pickle.dump(user,userfile)
display("Terminating connection")
client_socket.close()
os._exit(1)
elif checkCMD('!broadcast',msg):
display(msg[len("!broadcast"):])
else:
if(authenticated):
fromUser=msge[:USERNAME_LENGTH].decode('utf8').strip()
nonceRec,msgRec,tagRec=pickle.loads(msge[USERNAME_LENGTH:])
if fromUser==user.id:
decipher = AES.new(user.senderKey, AES.MODE_EAX,nonce=nonceRec)
else:
decipher = AES.new(user.decryptedSenderKeys[fromUser], AES.MODE_EAX,nonce=nonceRec)
decMsgRec=decipher.decrypt(msgRec)
try:
decipher.verify(tagRec)
except ValueError:
display("MAC verification failed. Message may have been tampered with or ratcheting got out of sync")
if(fromUser!=user.id):
display(fromUser+": "+decMsgRec.decode('utf8'))
user.decryptedSenderKeys[fromUser]=KDF(user.decryptedSenderKeys[fromUser],RATCHETING_STEPS)
else:
display(fromUser+": "+decMsgRec.decode('utf8'))
user.senderKey=KDF(user.senderKey,RATCHETING_STEPS)
else:
display(msg)
def sendThread():
"""Handles sending of messages."""
while True:
msg = input("> ")
print ("\033[A \033[A")
if(authenticated and not checkCMD(msg,"!quit")):
send(msg,client_socket,enc=True)
else:
send(msg,client_socket)
def sendGUI(event=None): # event is passed by binders.
"""Handles sending of messages in GUI mode"""
msg = my_msg.get()
my_msg.set("") # Clears input field.
if(authenticated and not checkCMD(msg,"!quit")):
send(msg,client_socket,enc=True)
elif(checkCMD(msg,"!quit")):
send(msg,client_socket)
client_socket.close()
top.quit()
else:
send(msg,client_socket)
def on_closing(event=None):
"""This function is to be called when the window is closed."""
send("!quit",client_socket)
client_socket.close()
MODE='GUI' #CLI mode or GUI
HOST = '127.0.0.1'
PORT = 33001
if not os.path.exists('data'):
os.makedirs('data')
BUFSIZ = 4096
ADDR = (HOST, PORT)
HEADER_LENGTH=10
USERNAME_LENGTH=50
RATCHETING_STEPS=5
if (MODE=='GUI'):
top = tkinter.Tk()
top.title("Hermes")
messages_frame = tkinter.Frame(top,borderwidth=5)
my_msg = tkinter.StringVar() # For the messages to be sent.
my_msg.set("")
scrollbar = tkinter.Scrollbar(messages_frame)
msg_list = tkinter.Listbox(messages_frame, height=50, width=80, yscrollcommand=scrollbar.set)
scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
msg_list.pack(side=tkinter.LEFT, fill=tkinter.BOTH)
msg_list.pack()
messages_frame.pack()
entry_field = tkinter.Entry(top,width='50', textvariable=my_msg)
entry_field.bind("<Return>", sendGUI)
entry_field.pack()
send_button = tkinter.Button(top, text="Send", command=sendGUI)
send_button.pack()
top.protocol("WM_DELETE_WINDOW", on_closing)
client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.connect(ADDR)
receive_thread = Thread(target=receiveThread)
receive_thread.start()
tkinter.mainloop()
elif (MODE=='CLI'):
client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.connect(ADDR)
receive_thread = Thread(target=receiveThread)
receive_thread.start()
swnd_thread = Thread(target=sendThread)
swnd_thread.start()