-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.py
606 lines (522 loc) · 22.8 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# Código baseado em https://docs.python.org/3.6/library/asyncio-stream.html#tcp-echo-client-using-streams
import asyncio
import base64 as b64
import logging
import os
import sys
import json
from datetime import datetime
from logging.handlers import RotatingFileHandler
import protocol
import utils
from cert_validator import CertificateValidator
from cryptography import x509
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import dh, padding
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.x509 import Certificate
import protocol
import two_fa_msg
import utils
from cert_validator import CertificateValidator
from database import Database
from protocol import Packet
from server_client import Client, Message
p = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF
g = 2
conn_cnt = 0
conn_port = 8443
tsa_host = '127.0.0.1'
tsa_conn_port = 8444
max_msg_size = 9999
db = Database("database.json")
class ServerWorker(object):
"""Classe que implementa a funcionalidade do SERVIDOR."""
def __init__(self, cnt, addr=None):
"""Construtor da classe."""
self.id = cnt
self.addr = addr
self.username = None
self.restart = False
self.msg_cnt = 0
self.dhprivate_key: dh.DHPrivateKey = (
dh.DHParameterNumbers(p, g).parameters().generate_private_key()
)
self.rsa_private_key = None
self.cert_name = "MSG_SERVER.crt"
self.cert: Certificate = None
self.ca_cert: Certificate = None
self.verifier: CertificateValidator = CertificateValidator()
self.client_pub_dh_key: dh.DHPublicKey = None
self.client_cert_name: str = None
self.client_cert: Certificate = None
self.aesgcm: AESGCM = None
self.two_fa_method = None
self.phone_number = None
self.two_fa_key = None
try:
self.rsa_private_key = serialization.load_pem_private_key(
open("certs/MSG_SERVER.key", "rb").read(),
password=None,
)
self.cert = x509.load_pem_x509_certificate(
open("certs/MSG_SERVER.crt", "rb").read()
)
self.ca_cert = x509.load_pem_x509_certificate(
open("certs/MSG_CA.crt", "rb").read()
)
except:
logging.info("Error loading the server's certificates.")
exit(1)
async def process(self, msg):
"""Processa uma mensagem (`bytestring`) enviada pelo CLIENTE.
Retorna a mensagem a transmitir como resposta (`None` para
finalizar ligação)"""
self.msg_cnt += 1
if self.aesgcm:
msg = utils.decrypt_message(msg, self.aesgcm)
message = Packet.from_json(msg.decode())
if message.type == "pub_key":
self.client_pub_dh_key = serialization.load_pem_public_key(
b64.b64decode(message.content.encode())
)
# Creates the pair of public keys
dh_pair = utils.join_pair(
self.dhprivate_key.public_key().public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
),
self.client_pub_dh_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
),
)
# Generates the signature of the pair of keys
signature = self.rsa_private_key.sign(
dh_pair,
padding.PSS(
mgf=padding.MGF1(hashes.SHA256()),
salt_length=padding.PSS.MAX_LENGTH,
),
hashes.SHA256(),
)
new_msg: Packet = Packet(
"pub_sign_cert",
{
"pub_key": b64.b64encode(
self.dhprivate_key.public_key().public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
).decode(),
"signature": b64.b64encode(signature).decode(),
"cert": [
b64.b64encode(
self.cert.public_bytes(encoding=serialization.Encoding.PEM)
).decode(),
self.cert_name,
],
},
)
return new_msg.to_json().encode()
elif message.type == "sign_cert":
self.client_cert = x509.load_pem_x509_certificate(
b64.b64decode(message.content["cert"][0].encode())
)
self.client_cert_name = message.content["cert"][1]
# Verifies the certificate
if not self.verifier.validate_certificate(
self.client_cert, self.client_cert_name.split("/")[-1].split(".")[0]
):
print(f"[{self.id}] Received invalid certificate.")
return
# Verifies the signature
if not utils.verify_signature(
b64.b64decode(message.content["signature"].encode()),
self.client_cert.public_key(),
utils.join_pair(
self.client_pub_dh_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
),
self.dhprivate_key.public_key().public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
),
),
):
logging.info(f"Invalid signature from client [{self.id}]".format)
return
shared_key = self.dhprivate_key.exchange(self.client_pub_dh_key)
key = HKDF(
algorithm=hashes.SHA256(),
length=32,
salt=None,
info=b"handshake data",
).derive(shared_key)
self.aesgcm = AESGCM(key)
new_msg = Packet("conn_success", None)
logging.info(f"Connection established successfully with client [{self.id}]")
return new_msg.to_json().encode()
elif message.type == "ask_cert":
client: Client = db.get(message.content)
if client:
new_msg = protocol.create_message("client_cert", client.cert)
logging.info(
f"[{message.content}]'s certificate sent to client [{self.username}]"
)
else:
new_msg = protocol.create_message(
"msg_error", "Invalid message recipient"
)
logging.info(f"Invalid message recipient from client [{self.username}]")
elif message.type == "send":
# Create the Message
if db.get(message.content["to"]):
message_hash = utils.calculate_message_hash(message.content["message"].encode())
timestamp, tsa_signature = await request_timestamp(message_hash)
msg_to_store = Message(
self.username,
message.content["message"],
timestamp,
message.content["key"],
message.content["signature"],
tsa_signature,
)
# Store the Message in the database (in the sender's instance)
db.add_message(message.content["to"], msg_to_store)
new_msg = protocol.create_message(
"msg_sent", "Message sent successfully"
)
logging.info(
f"Message from client [{self.username}] sent successfully to client [{message.content['to']}]"
)
else:
new_msg = protocol.create_message(
"msg_error", "Invalid message recipient"
)
logging.info(f"Invalid message recipient from client [{self.username}]")
elif message.type == "askqueue":
db_result: Client = db.get(self.username)
messages = db_result.get_messages() if db_result else None
if messages:
unread_messages = [
message for message in messages if not message.is_read
]
if len(unread_messages) > 0:
msgs = []
for msg in unread_messages:
sender_cert = db.get(msg.sender).cert
msgs += [
{
"num": msg.num,
"sender": msg.sender,
"message": msg.message,
"timestamp": msg.timestamp,
"key": msg.key,
"signature": msg.signature,
"cert": sender_cert,
"tsa_signature": msg.tsa_signature
}
]
new_msg = protocol.create_message("msgs", msgs)
else:
new_msg = protocol.create_message("msg_error", "Empty queue")
else:
new_msg = protocol.create_message("msg_error", "Empty queue")
logging.info(f"Client [{self.username}] asked for messages in queue")
elif message.type == "getmsg":
user = db.get(self.username)
message = user.get_message(message.content) if user else None
if message:
sender_cert = db.get(message.sender).cert
new_msg = protocol.create_message(
"msg",
{
"sender": message.sender,
"message": message.message,
"timestamp": message.timestamp,
"key": message.key,
"signature": message.signature,
"cert": sender_cert,
"tsa_signature": message.tsa_signature
},
)
if not message.is_read:
logging.info(
f"Received a request for a message from client [{self.username}]"
)
message.read()
db.save_to_file()
else:
logging.info(f"Old message request from [{self.username}]")
else:
new_msg = protocol.create_message(
"msg_error", "Invalid message request"
)
logging.info(f"Invalid message request from [{self.username}]")
elif message.type == "getallmsgs":
user = db.get(self.username)
messages = user.get_messages()
if messages:
msgs = []
for msg in messages:
sender_cert = db.get(msg.sender).cert
msgs += [
{
"num": msg.num,
"sender": msg.sender,
"message": msg.message,
"timestamp": msg.timestamp,
"key": msg.key,
"signature": msg.signature,
"cert": sender_cert,
"tsa_signature": msg.tsa_signature
}
]
new_msg = protocol.create_message("msgs", msgs)
else:
new_msg = protocol.create_message("msg_error", "Empty queue")
logging.info(f"Client [{self.username}] asked for all messages")
elif message.type == "getmsgsfrom":
user = db.get(self.username)
messages = user.get_messages()
if messages:
msgs = []
for msg in messages:
if msg.sender == message.content:
sender_cert = db.get(msg.sender).cert
msgs += [
{
"num": msg.num,
"sender": msg.sender,
"message": msg.message,
"timestamp": msg.timestamp,
"key": msg.key,
"signature": msg.signature,
"cert": sender_cert,
"tsa_signature": msg.tsa_signature
}
]
new_msg = protocol.create_message("msgs", msgs)
else:
new_msg = protocol.create_message("msg_error", "Empty queue")
logging.info(
f"Client [{self.username}] asked for all messages from [{message.content}]"
)
elif message.type == "register_attempt":
# If username already exists
if db.get(message.content["username"]):
# Send an error message
print(db.get(message.content["username"]))
new_msg = protocol.create_message(
"register_error", "Username already exists."
)
logging.info(
f"Client [{self.id}] tried to register with an existing username ({message.content['username']})."
)
else:
new_client = Client()
# Store the new client in the database
hashed_password = message.content["password"]
new_client.hashed_password = hashed_password
db.set(message.content["username"], new_client)
# Set the username of the client
self.username = message.content["username"]
# Send the success message
new_msg = protocol.create_message(
"register_success", f"\nWelcome {self.username}."
)
self.restart = True
logging.info(
f"Client [{self.id}] registered successfully with username '{message.content['username']}'."
)
elif message.type == "register_success":
user: Client = db.get(self.username)
user.cert = message.content
db.set(self.username, user)
new_msg = protocol.create_message("register_done", None)
elif message.type == "login_attempt":
# If the username doesn't exist
if not db.get(message.content["username"]):
# Send an error message
new_msg = protocol.create_message(
"msg_error", "Username doesn't exist."
)
logging.info(
f"Client [{self.id}] tried to log in with a non-existing username ({message.content['username'].encode()})."
)
else:
# Send the login credentials for the client to check the password on his side
new_msg = protocol.create_message(
"login_creds",
{
"username": message.content["username"],
"password": db.get(message.content["username"]).hashed_password,
},
)
elif message.type == "login_success":
logging.info(
f"Client [{self.id}] logged in successfully with username '{message.content['username']}'."
)
self.username = message.content["username"]
user = db.get(self.username)
self.two_fa_method = user.two_fa
self.phone_number = user.phone_number
self.two_fa_key = user.two_fa_key
if user.two_fa == "totp":
new_msg = protocol.create_message(
"login_totp",
None
)
elif user.two_fa == "sms":
code = two_fa_msg.gen_otp_code(user.two_fa_key)
two_fa_msg.send_msg(user.phone_number, f"This is your code: {code}")
new_msg = protocol.create_message(
"2fa_sms",
{
"code": code,
"phone_number": user.phone_number,
"message": f"Welcome back {self.username}.",
},
)
else:
new_msg = protocol.create_message(
"login_success", f"Welcome back {self.username}."
)
elif message.type == "2fa_request":
if message.content["option"] == "totp":
self.two_fa_key = two_fa_msg.gen_opt_key()
new_msg = protocol.create_message(
"2fa_totp",
{
"key": self.two_fa_key
}
)
elif message.content["option"] == "sms":
self.two_fa_key = two_fa_msg.gen_opt_key()
code = two_fa_msg.gen_otp_code(self.two_fa_key)
two_fa_msg.send_msg(
message.content["phone_number"], f"This is your code: {code}"
)
new_msg = protocol.create_message(
"2fa_sms",
{
"code": code,
"phone_number": message.content["phone_number"],
"message": "Two Factor Authentication activated through SMS",
},
)
logging.info(f"2FA SMS request from [{self.username}]")
elif message.type == "2fa_totp_code":
client_code = message.content["code"]
if two_fa_msg.verify_otp_code(self.two_fa_key, client_code):
if self.two_fa_method != "totp":
self.two_fa_method = "totp"
user = db.get(self.username)
user.two_fa = "totp"
user.two_fa_key = self.two_fa_key
db.set(self.username, user)
new_msg = protocol.create_message("2fa_done", {"option": "totp", "message": "Two Factor Authentication activated through TOTP."})
logging.info(f"2FA TOTP activation successfull from [{self.username}]")
else:
logging.info(f"Client [{self.username}] logged in using TOTP.")
new_msg = protocol.create_message("2fa_done", {"option": "totp", "message": f"Welcome back {self.username}."})
else:
new_msg = protocol.create_message("msg_error", "Invalid code.")
logging.info(f"Invalid TOTP code from [{self.username}]")
elif message.type == "2fa_success":
if message.content["option"] == "sms":
if self.two_fa_method != "sms":
self.two_fa_method = "sms"
self.phone_number = message.content["phone_number"]
logging.info(f"2FA SMS activation successfull from [{self.username}]")
user = db.get(self.username)
user.two_fa = "sms"
user.two_fa_key = self.two_fa_key
user.phone_number = self.phone_number
db.set(self.username, user)
else:
logging.info(f"Client [{self.username}] logged in using SMS.")
new_msg = protocol.create_message("2fa_done", {"option": "sms"})
else:
logging.info(f"Invalid request from [{self.id}]")
# Send an error message
new_msg = protocol.create_message("msg_error", "Invalid request")
if new_msg:
new_msg = new_msg.to_json().encode()
if self.aesgcm:
new_msg = utils.encrypt_message(new_msg, self.aesgcm)
return new_msg
async def request_timestamp(data):
reader, writer = await asyncio.open_connection(tsa_host, tsa_conn_port)
try:
writer.write(data)
await writer.drain()
response_data = await reader.read()
response = json.loads(response_data.decode())
return response['timestamp'], response['signature']
finally:
writer.close()
await writer.wait_closed()
#
#
# Funcionalidade Cliente/Servidor
#
# obs: não deverá ser necessário alterar o que se segue
#
async def handle_echo(reader, writer):
global conn_cnt
conn_cnt += 1
addr = writer.get_extra_info("peername")
srvwrk = ServerWorker(conn_cnt, addr)
try:
data = await reader.read(max_msg_size)
while True:
if not data:
continue
if data[:1] == b"\n":
break
data = await srvwrk.process(data)
writer.write(data)
await writer.drain()
data = await reader.read(max_msg_size)
logging.info("Connection from client [{}]".format(srvwrk.id) + " ended")
writer.close()
except ConnectionResetError as e:
logging.info(f"ConnectionResetError occurred: {e}")
writer.close()
except Exception as e:
logging.info(f"Some error occurred: {e.__repr__()}")
writer.close()
def run_server():
loop = asyncio.new_event_loop()
coro = asyncio.start_server(handle_echo, "127.0.0.1", conn_port)
server = loop.run_until_complete(coro)
logging.info("Serving on {}".format(server.sockets[0].getsockname()))
try:
loop.run_forever()
except KeyboardInterrupt:
logging.info("\nServer closed manually.")
exit(0)
except Exception as e:
logging.error(f"\nServer closed due to { e.__repr()}")
exit(1)
# Close the server
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()
logging.info("Server closed")
logging.basicConfig(
filename="server.log",
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
)
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(
logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
)
logging.getLogger().addHandler(console_handler)
run_server()