-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
247 lines (221 loc) · 8.53 KB
/
server.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
import hashlib
import json
import math
import os
import socket
import sys
import threading
import time
from factorise import *
from helper import *
from ipaddress import ip_address
from pathlib import Path
from random import getrandbits
from random import randrange
from random import sample
from struct import *
# password file dictionary on server
PASSWORD_FILE = {}
def display_table():
print("<===============PASSWORD TABLE==================>")
print(json.dumps(PASSWORD_FILE, sort_keys=True, indent=4))
print("<===============================================>")
def get_ip():
hostname = socket.gethostname()
return socket.gethostbyname(hostname)
# function to check if userID and Password present in the PASSWORD_FILE
def check_creds(id, password):
global PASSWORD_FILE
password += str(PASSWORD_FILE[id]['salt']) + str(PASSWORD_FILE[id]['prime'])
password = hashlib.sha1(password.encode()).hexdigest()
if PASSWORD_FILE[id]['password'] == password:
return 1
return -1
# thread fuction for each client connnected
def threaded(conn, addr):
s_addr = get_ip()
d_addr = addr[0]
# receiving public key tuple (Ya, p,q, alpha) from client
# msg = conn.recv(calcsize(FORMAT))
# msg = unpack_message(msg)
try:
msg = conn.recv(calcsize(FORMAT))
msg = unpack_message(msg)
except:
print("Closing Connection with ==> ", d_addr)
conn.close()
print(
"Received: PUBKEY ==> ",
display(msg, opcode=OP_CODES['PUBKEY']),
" from ==> ",
d_addr,
)
Ya, p, q, alpha = msg['y'], msg['p'], msg['q'], msg['alpha']
# computing Yb from global (p,q,alpha) received from client
Xb = randrange(1, q) # server's private Key
Yb = pow_mod(alpha, Xb, p) # server's public key
# Sending public key (Yb) to client
msg = create_message(s_addr=s_addr, d_addr=d_addr, opcode=OP_CODES['PUBKEY'], y=Yb)
print("Sent: PUBKEY ==> ", {"Yb": Yb}, " to ==> ", d_addr)
conn.sendall(msg)
# computing the shared session key with client
key = pow_mod(Ya, Xb, p)
print("K_BA created!==> ", key, " with ==> ", d_addr)
while True:
try:
msg = conn.recv(calcsize(FORMAT))
msg = unpack_message(msg)
except:
print("Closing Connection with ==> ", d_addr)
break
print("Received:", end=" ")
if msg['opcode'] == OP_CODES['EXITSTATUS']:
print("EXITSTATUS from ==> ", d_addr)
break
elif msg['opcode'] == OP_CODES['SIGNEDMSG']:
print("SIGNEDMSG ==>", end=" ")
ID = decrypt(msg['ID'], key)
chat_msg = decrypt(msg['plaintext'], key)
print({"ID": ID, "Message": chat_msg}, " FROM ==> ", d_addr)
E = msg['e']
S = msg['s']
signature = {'e': E, 's': S}
global_params = {'p': p, 'q': q, "alpha": alpha}
client_public_key = Ya
ver_status = verify_signature(
signature, chat_msg, global_params, client_public_key
)
msg = create_message(
s_addr=s_addr,
d_addr=d_addr,
opcode=OP_CODES['VERSTATUS'],
status=int(ver_status),
)
if ver_status:
print("Message verified!!. Sent VERSTATUS ==> ", d_addr)
else:
print("Message verfication failed!!. Sent VERSTATUS ==> ", d_addr)
conn.sendall(msg)
elif msg['opcode'] == OP_CODES["LOGINCREAT"]:
print("LOGINCREAT ==>", end=" ")
ID = decrypt(msg['ID'], key)
password = decrypt(msg['password'], key)
dummy = decrypt(msg['dummy'], key)
print({"ID": ID, "password": password, "qa": dummy}, " FROM ==> ", d_addr)
salt = getrandbits(13) # getting a random salt
password += str(salt) + str(dummy)
# using sha-1 hash function
password = hashlib.sha1(password.encode()).hexdigest()
# add to password table
if ID in PASSWORD_FILE.keys():
msg = create_message(
s_addr=s_addr,
d_addr=d_addr,
opcode=OP_CODES['LOGINREPLY'],
status=0,
)
print("UserID already exists!!. Sent LOGINREPLY ==> ", d_addr)
conn.sendall(msg)
else:
PASSWORD_FILE[ID] = {
'password': password,
'prime': int(dummy),
'salt': salt,
}
display_table()
msg = create_message(
s_addr=s_addr,
d_addr=d_addr,
opcode=OP_CODES['LOGINREPLY'],
status=1,
)
print("UserID created!!. Sent LOGINREPLY ==> ", d_addr)
conn.sendall(msg)
elif msg['opcode'] == OP_CODES['AUTHREQUEST']:
print("AUTHREQUEST ==>", end=" ")
ID = decrypt(msg['ID'], key)
password = decrypt(msg['password'], key)
print({"ID": ID, "password": password}, " FROM ==> ", d_addr)
# check in table
if ID not in PASSWORD_FILE.keys():
msg = create_message(
s_addr=s_addr, d_addr=d_addr, opcode=OP_CODES['AUTHREPLY'], status=0
)
print("UserID does not exists!!. Sent AUTHREPLY ==> ", d_addr)
conn.sendall(msg)
else:
status = check_creds(ID, password)
msg = create_message(
s_addr=s_addr,
d_addr=d_addr,
opcode=OP_CODES['AUTHREPLY'],
status=status,
)
if status == 1:
print("Request granted!!. Sent AUTHREPLY ==> ", d_addr)
else:
print("Incorrect Password!!. Sent AUTHREPLY ==> ", d_addr)
conn.sendall(msg)
elif msg['opcode'] == OP_CODES['SERVICEREQUEST']:
print("SERVICEREQUEST ==>", end=" ")
ID = decrypt(msg['ID'], key)
file = decrypt(msg['file'], key)
filename = os.path.basename(file)
print({"ID": ID, "file": file}, " FROM ==> ", d_addr)
if not os.path.isfile(file):
msg = create_message(
s_addr=s_addr,
d_addr=d_addr,
opcode=OP_CODES['SERVICEDONE'],
status=-1,
)
print("File does not exists on server!!. Sent SERVICEDONE ==> ", d_addr)
conn.sendall(msg)
else:
filesize = os.path.getsize(file)
f = open(file, "rb")
print("Requested file found!!. Transferring... ==> ", d_addr)
lim = 1024 # 1024 bytes of file at a time to client
cnt = 1
while True:
if filesize < 1024:
lim = -1
c = f.read(lim)
if not c:
# print("DEBUG: REACHED!!!!!", cnt)
break
msg = create_message(
s_addr=s_addr,
d_addr=d_addr,
opcode=OP_CODES['SERVICEDONE'],
file=encrypt(filename, key),
buf=c,
status=0,
plaintext=encrypt("Fragment" + str(cnt), key),
)
conn.sendall(msg)
cnt += 1
thankyou_msg = "Thank you for using our service and have a nice day!"
msg = create_message(
s_addr=s_addr,
d_addr=d_addr,
opcode=OP_CODES['SERVICEDONE'],
file=encrypt(filename, key),
status=1,
plaintext=encrypt(thankyou_msg, key),
)
conn.sendall(msg)
print("File transfer completed!!")
conn.close()
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
print("Server Listening...")
s.listen()
while True:
conn, addr = s.accept()
print('Connected! ==> ', addr)
t = threading.Thread(target=threaded, args=(conn, addr))
t.start()
s.close()