-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
729 lines (590 loc) · 27.1 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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
'''
import socket
import threading
HOST = '127.0.0.1'
PORT = 9090
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
server.listen()
clients = []
nicknames = []
def broadcast(message):
for client in clients:
client.send(message)
def handle(client):
while True:
try:
message = client.recv(1024)
print(f"{nicknames[clients.index(client)]} says {message}")
broadcast(message)
except:
index = clients.index(client)
clients.remove(client)
client.close()
nickname = nicknames[index]
nicknames.remove(nickname)
break
def receive():
while True:
client, address = server.accept()
print(f"Connected with {str(address)}!")
client.send("NICK".encode('utf-8'))
nickname = client.recv(1024)
nicknames.append(nickname)
clients.append(client)
print(f"Nickname of the client is {nickname}")
broadcast(f"{nickname} connected to the server!\n".encode('utf-8'))
client.send("Connected to the server".encode('utf-8'))
thread = threading.Thread(target=handle, args=(client,))
thread.start()
print("Server is running!")
receive()
'''
# `TODO : switch hashlib to argon2 properly
# // TODO : start initial encryption between server-client in handling login/register inputs
# // TODO : start initial encryption between server-client in handling register
# // TODO : storing user keys into db on register
# // TODO : retrieve user keys from db on login
# // TODO : maybe except handling on register dupe users?
# // TODO : except handling on exit (init ecdh-decrypt)
# // TODO : add dig sign on init_ecdh for user_key_public?
# TODO : start calc pqxdh sk - send all keys for client in clients in session?, then update?
# TODO : look into resetting spk as defined in paper ; replenish from client
# // TODO : reset and replenish opk as needed
# // TODO : change server pki to mem-only ; prevent deserial attk
# // TODO : look into resetting pq_ct, ep_key after fetch
# // TODO : clean up encryption method in separate funct
### These libs are used for basic funct - network and threading
from base64 import b64encode, b64decode
import json
import time
import socket
import threading
### These libs are used for username, password and salt handling and storing
import sqlite3
import hashlib
import secrets
### These libs are used for conv. symm / asymm op.
from Crypto.PublicKey import ECC
from Crypto.Protocol.DH import key_agreement
from Crypto.Hash import SHAKE128, SHA512
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad, pad
from Crypto.Signature import eddsa
### These libs are used for Post-Quantum op.
from kyber import Kyber1024
from pqc.sign import dilithium5
HOST = '127.0.0.1'
PORT = 9090
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
server.listen()
clients = []
nicknames = []
# Create SQLite database and connect
conn = sqlite3.connect('chat_users.db')
cursor = conn.cursor()
# Create table to store usernames, hashed passwords, and salts
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT,
password TEXT,
salt TEXT
);
''')
conn.commit()
# Create table to store all user keys and sigs
cursor.execute('''
CREATE TABLE IF NOT EXISTS keys (
user_id INTEGER PRIMARY KEY,
id_key_public TEXT, id_key_private TEXT,
pqid_pkey TEXT, pqid_skey TEXT,
spk_key_public TEXT, spk_key_private TEXT,
sig_spk TEXT,
pqspk_pkey TEXT, pqspk_skey TEXT,
sig_pqspk TEXT,
opk_key_public TEXT, opk_key_private TEXT,
pqopk_pkey TEXT, pqopk_skey TEXT,
sig_pqopk TEXT,
ep_key_public TEXT,
pq_ct TEXT
);
''')
conn.commit()
# * This function is to display server's message banner
def banner():
print('''
$$$$$$$\ $$$$$$\ $$$$$$\ $$$$$$$$\ $$$$$$$\ $$$$$$\
$$ __$$\ $$ __$$\ $$ __$$\ $$ _____|$$ __$$\ $$ __$$\
$$ | $$ |$$ / $$ |$$ / \__|$$ | $$ | $$ | $$ / \__| $$$$$$\ $$$$$$\ $$\ $$\ $$$$$$\ $$$$$$\
$$$$$$$\ |$$$$$$$$ |\$$$$$$\ $$$$$\ $$ | $$ | \$$$$$$\ $$ __$$\ $$ __$$\\$$\ $$ |$$ __$$\ $$ __$$\
$$ __$$\ $$ __$$ | \____$$\ $$ __| $$ | $$ | \____$$\ $$$$$$$$ |$$ | \__|\$$\$$ / $$$$$$$$ |$$ | \__|
$$ | $$ |$$ | $$ |$$\ $$ |$$ | $$ | $$ | $$\ $$ |$$ ____|$$ | \$$$ / $$ ____|$$ |
$$$$$$$ |$$ | $$ |\$$$$$$ |$$$$$$$$\ $$$$$$$ | \$$$$$$ |\$$$$$$$\ $$ | \$ / \$$$$$$$\ $$ |
\_______/ \__| \__| \______/ \________|\_______/ \______/ \_______|\__| \_/ \_______|\__|
''')
# * This function is to generate an ECC ed25519 keypair for server
# ? server keypairs are stored locally in .pem files
# ! server keypairs are not encrypted with passphrase - may be vuln to deserial attack.
# ? server keypairs are re-generated each runtime
def server_keygen():
# export server keypair
print("\n\nVERSION vx.x - Unauthorized Access is Prohibited.")
print("\nserver_keygen() >>>")
# Gen server-side keypair
server_key = ECC.generate(curve='ed25519')
# set keypair to session
return server_key
# * This function is to generate an EdDSA signature of server_key_public
# ? The signature is passed to client to be verified upon first-moment comm.
# ? before init_ecdh()
def server_keysign():
signer = eddsa.new(server_key_private, 'rfc8032')
server_key_sig = signer.sign(SHA512.new(server_key_public.export_key(format='DER')))
server_key_sig = b64encode(server_key_sig).decode('utf-8')
return server_key_sig
# * This function is to generate a Kyber-1024 keypair for server
# ! This function is not used anymore as of test v1.3 Alpha
# ? server keypairs are stored in memory
# ? server keypairs are re-generated each runtime
# // def server_PQ_keygen():
# // server_pq_key_public, server_pq_key_private = Kyber1024.keygen()
# // return server_pq_key_public, server_pq_key_private
# * This function is to get server private key from .pem file
# ! This function is not used anymore as of test v1.7
# // def read_server_private_key():
# // with open("server_key_private.pem", "rt") as f:
# // data = f.read()
# // server_key_private = ECC.import_key(data)
# // return server_key_private
# * This function is to get server public key from .pem file
# // def read_server_public_key():
# // with open("server_key_public.pem", "rt") as f:
# // data = f.read()
# // server_key_public = ECC.import_key(data)
# // return server_key_public
# * This function is for Key Derivation Function for ECDH op.
def kdf(x):
return SHAKE128.new(x).read(32)
# * This function is to perform initial ECDH KEP on server-client
# ? This function will return a shared session key
def init_ecdh(client):
# read server keypair for op.
#server_key = server_keygen()
#server_key_public = server_key.public_key()
#server_key_private = server_key
# import user_key_public from user
user_key_public = ECC.import_key(client.recv(1024).decode('utf-8'))
user_key_sig = client.recv(1024).decode('utf-8')
user_key_sig = b64decode(user_key_sig)
# export server_key_public as PEM send to user
client.send(str(server_key_public.export_key(format='PEM')).encode('utf-8'))
client.send(server_key_sig.encode('utf-8'))
# verify user_key_public with sig
verifier = eddsa.new(user_key_public, 'rfc8032')
try:
verifier.verify(SHA512.new(user_key_public.export_key(format='DER')), user_key_sig)
except:
print("User key cannot be verified!")
else:
# perform ecdh on server-client
session_key = key_agreement(static_priv=server_key_private,
static_pub=user_key_public,
kdf=kdf)
return session_key
# * This function is to perform initial encryption of server response
# ? This function will return encrypted server response
def init_encrypt(client, session_key, data):
cipher = AES.new(session_key, AES.MODE_CBC)
data = cipher.encrypt(pad(data.encode('utf-8'), AES.block_size))
json_k = ['iv', 'data']
json_v = [b64encode(x).decode('utf-8') for x in (cipher.iv, data)]
json_ct = json.dumps(dict(zip(json_k, json_v)))
client.send(json_ct.encode('utf-8'))
# * This function is to perform initial decryption of user data
# ? This function will return decrypted user data
def init_decrypt(client, session_key):
try:
# receive ct of login data from user and iv used during enc
login_data = client.recv(20480).decode('utf-8')
b64 = json.loads(login_data)
json_k = [ 'iv', 'data' ]
json_v = {k:b64decode(b64[k]) for k in json_k}
# init AES and attempt to decrypt ct using session_key
# decrypted ct will be unpad to return to pt
cipher = AES.new(session_key, AES.MODE_CBC, json_v['iv'])
login_data = unpad(cipher.decrypt(json_v['data']), AES.block_size)
return login_data
except Exception as e:
pass
# * This function is to store user keys into database
# ? No return for this function
def store_keys(client, username):
cursor.execute('SELECT id from users WHERE username = ?;', (username,))
user_id = cursor.fetchone()
user_id = user_id[0]
id_key_public = client.recv(1024).decode('utf-8')
id_key_private = client.recv(1024).decode('utf-8')
pqid_pkey = client.recv(30720).decode('utf-8')
pqid_skey = client.recv(30720).decode('utf-8')
spk_key_public = client.recv(1024).decode('utf-8')
spk_key_private = client.recv(1024).decode('utf-8')
sig_spk = client.recv(20480).decode('utf-8')
pqspk_pkey = client.recv(30720).decode('utf-8')
pqspk_skey = client.recv(30720).decode('utf-8')
sig_pqspk = client.recv(20480).decode('utf-8')
opk_key_public = client.recv(1024).decode('utf-8')
opk_key_private = client.recv(1024).decode('utf-8')
pqopk_pkey = client.recv(30720).decode('utf-8')
pqopk_skey = client.recv(30720).decode('utf-8')
sig_pqopk = client.recv(20480).decode('utf-8')
# Insert new keys into the database
# ? may need a better way of key mgmt...
cursor.execute('''INSERT INTO keys (
user_id,
id_key_public, id_key_private,
pqid_pkey, pqid_skey,
spk_key_public, spk_key_private,
sig_spk,
pqspk_pkey, pqspk_skey,
sig_pqspk,
opk_key_public, opk_key_private,
pqopk_pkey, pqopk_skey,
sig_pqopk) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);''', (
user_id,
id_key_public, id_key_private,
pqid_pkey, pqid_skey,
spk_key_public, spk_key_private,
sig_spk,
pqspk_pkey, pqspk_skey,
sig_pqspk,
opk_key_public, opk_key_private,
pqopk_pkey, pqopk_skey,
sig_pqopk))
conn.commit()
# * This function is to retrieve user keys from database after successful login
# ? This function will send all keys belonging to client
# ! Still need to revise method of sending pt "NULL" over network - cannot send NoneType
def resp_init_pqxdh(client, user_id):
cursor.execute('SELECT * FROM keys WHERE user_id = ?;', (user_id,))
keys = cursor.fetchone()
client.send(keys[1].encode('utf-8'))
time.sleep(0.05)
client.send(keys[2].encode('utf-8'))
time.sleep(0.05)
client.send(keys[3].encode('utf-8'))
time.sleep(0.05)
client.send(keys[4].encode('utf-8'))
time.sleep(0.05)
client.send(keys[5].encode('utf-8'))
time.sleep(0.05)
client.send(keys[6].encode('utf-8'))
time.sleep(0.05)
client.send(keys[7].encode('utf-8'))
time.sleep(0.05)
client.send(keys[8].encode('utf-8'))
time.sleep(0.05)
client.send(keys[9].encode('utf-8'))
time.sleep(0.05)
client.send(keys[10].encode('utf-8'))
time.sleep(0.05)
# Check if OPKs are not null
# ? if not null, send keys like usual
if (keys[11] is not None and
keys[12] is not None and
keys[13] is not None and
keys[14] is not None and
keys[15] is not None):
client.send(keys[11].encode('utf-8'))
time.sleep(0.05)
client.send(keys[12].encode('utf-8'))
time.sleep(0.05)
client.send(keys[13].encode('utf-8'))
time.sleep(0.05)
client.send(keys[14].encode('utf-8'))
time.sleep(0.05)
client.send(keys[15].encode('utf-8'))
# ? else append "NULL" as keys then send
# ! need to revise method later..
else:
client.send("NULL".encode('utf-8'))
time.sleep(0.05)
client.send("NULL".encode('utf-8'))
time.sleep(0.05)
client.send("NULL".encode('utf-8'))
time.sleep(0.05)
client.send("NULL".encode('utf-8'))
time.sleep(0.05)
client.send("NULL".encode('utf-8'))
# * This function is to retrieve new gen keys from users after
# * OPKs deletion from one session
def resp_update_pqxdh(client, session_key, user_id):
askUpdate = init_decrypt(client, session_key)
askUpdate = askUpdate.decode()
# if OPKs in db is null, update db with keys
if askUpdate == "UPD":
opk_key_public = client.recv(1024).decode('utf-8')
opk_key_private = client.recv(1024).decode('utf-8')
pqopk_pkey = client.recv(30720).decode('utf-8')
pqopk_skey = client.recv(30720).decode('utf-8')
sig_pqopk = client.recv(20480).decode('utf-8')
cursor.execute('''UPDATE keys
SET opk_key_public = ?, opk_key_private = ?,
pqopk_pkey = ?, pqopk_skey = ?,
sig_pqopk = ?
WHERE user_id = ?''', (opk_key_public, opk_key_private, pqopk_pkey, pqopk_skey, sig_pqopk, user_id, ))
conn.commit()
# * This function is to retrieve all pubkeys, sigs of users in server session,
# ! Current implementation may be vulnerable to OPK exhaustion attack - default to SPK
# ? This function send all keys and sigs of corresponding clients to client
# ? If corresp. user has OPK, then perform ENC/DEC with PQOPK
# ? Else, corresp. user don't have OPK ; perform ENC/DEC with PQSPK
def resp_calc_pqxdh(client, session_key, nicknames, username):
askUser = init_decrypt(client, session_key)
askUser = askUser.decode()
# Look for corresp. user in db
cursor.execute('SELECT id FROM users WHERE username = ?;', (askUser, ))
user_id = cursor.fetchone()
# if user exists, proceed to either ENC or DEC
if user_id:
user_id = user_id[0]
# if corresp. user is not logged in, perform ENC
if askUser not in nicknames:
init_encrypt(client, session_key, "ENC")
cursor.execute('''SELECT id_key_public, pqid_pkey, spk_key_public, sig_spk,
pqspk_pkey, sig_pqspk, opk_key_public, pqopk_pkey, sig_pqopk
FROM keys WHERE user_id = ?;''', (user_id, ))
keys = cursor.fetchone()
client.send(keys[0].encode('utf-8'))
time.sleep(0.05)
client.send(keys[1].encode('utf-8'))
time.sleep(0.05)
client.send(keys[2].encode('utf-8'))
time.sleep(0.05)
client.send(keys[3].encode('utf-8'))
time.sleep(0.05)
client.send(keys[4].encode('utf-8'))
time.sleep(0.05)
client.send(keys[5].encode('utf-8'))
time.sleep(0.05)
# Check if OPKs are not null
# ? if not null, send keys like usual
if (keys[6] is not None and
keys[7] is not None and
keys[8] is not None):
client.send(keys[6].encode('utf-8'))
time.sleep(0.05)
client.send(keys[7].encode('utf-8'))
time.sleep(0.05)
client.send(keys[8].encode('utf-8'))
# ? else append "NULL" as keys then send
# ! need to revise method later..
else:
client.send("NULL".encode('utf-8'))
time.sleep(0.05)
client.send("NULL".encode('utf-8'))
time.sleep(0.05)
client.send("NULL".encode('utf-8'))
ct_a = client.recv(30720).decode('utf-8')
ep_key_public = client.recv(1024).decode('utf-8')
cursor.execute('''SELECT id FROM users WHERE username = ?''', (username, ))
user_id = cursor.fetchone()
user_id = user_id[0]
# publish ENC's product to db so corresp. can fetch and DEC
cursor.execute('UPDATE keys SET ep_key_public = ?, pq_ct = ? WHERE user_id = ?;', (ep_key_public, ct_a, user_id, ))
conn.commit()
return True
# else if corresp. user is logged in, perform DEC
else:
init_encrypt(client, session_key, "DEC")
cursor.execute('''SELECT id_key_public, pqid_pkey, spk_key_public, sig_spk,
pqspk_pkey, sig_pqspk, opk_key_public, pqopk_pkey, sig_pqopk, ep_key_public, pq_ct
FROM keys WHERE user_id = ?''', (user_id, ))
keys = cursor.fetchone()
client.send(keys[0].encode('utf-8'))
time.sleep(0.05)
client.send(keys[1].encode('utf-8'))
time.sleep(0.05)
client.send(keys[2].encode('utf-8'))
time.sleep(0.05)
client.send(keys[3].encode('utf-8'))
time.sleep(0.05)
client.send(keys[4].encode('utf-8'))
time.sleep(0.05)
client.send(keys[5].encode('utf-8'))
time.sleep(0.05)
# Check if OPKs are not null
# ? if not null, send keys like usual
if (keys[6] is not None and
keys[7] is not None and
keys[8] is not None):
client.send(keys[6].encode('utf-8'))
time.sleep(0.05)
client.send(keys[7].encode('utf-8'))
time.sleep(0.05)
client.send(keys[8].encode('utf-8'))
# ? else append "NULL" as keys then send
# ! need to revise method later..
else:
client.send("NULL".encode('utf-8'))
time.sleep(0.05)
client.send("NULL".encode('utf-8'))
time.sleep(0.05)
client.send("NULL".encode('utf-8'))
time.sleep(0.05)
client.send(keys[9].encode('utf-8'))
time.sleep(0.05)
client.send(keys[10].encode('utf-8'))
cursor.execute('''UPDATE keys
SET opk_key_public = ?, opk_key_private = ?,
pqopk_pkey = ?, pqopk_skey = ?,
sig_pqopk = ?,
ep_key_public = ?, pq_ct = ? WHERE user_id = ?;''', (None, None, None, None, None, None, None, user_id, ))
conn.commit()
return True
# if corresp. user not exist in db, return no user
else:
init_encrypt(client, session_key, "NO USER")
return False
# * This function is to generate a random 16-byte salt for pass hash
def generate_salt():
return secrets.token_hex(16)
# * This function is to generate a SHA-256 hash from password input and hash
def hash_password(password, salt):
return hashlib.sha256((password + salt).encode('utf-8')).hexdigest()
# * This function is to send message to all clients conn to server
def broadcast(message):
for client in clients:
client.send(message)
# * This function is to try invoke broadcast funct.
# ! if a client exits, server will close conn. with client
def handle(client):
while True:
try:
message = client.recv(30720)
#print(message)
# ? remove client from server list on exit
# // ! might need to revise method since user can malice type "LOG_OUT" lole
if message.decode('utf-8') == "LOG_OUT":
raise ConnectionError
else:
broadcast(message)
except ConnectionError:
index = clients.index(client)
nickname = nicknames[index]
print(f"{nickname} has logged out or exited the session!")
broadcast(f"{nickname} has logged out or exited the session!".encode('utf-8'))
clients.remove(client)
nicknames.remove(nickname)
client.close()
break
# * This function is to accept incoming conn from client
# * and handle login or register of client
# ? if success login, handle funct will be invoked
# ! else of fail login or register, server will return err msg
def receive():
while True:
client, address = server.accept()
print(f"Connected with {str(address)}!")
# Ask for login or register
try:
login_or_register = client.recv(1024).decode('utf-8')
except: # ? catch except if client exit on login/register
pass
else:
# login option
if login_or_register.lower() == 'login':
try:
# initial KEP and decryption on login data
session_key = init_ecdh(client)
login_data = init_decrypt(client, session_key)
login_data = login_data.decode('utf-8').split()
username = login_data[0]
password = login_data[1]
except IndexError:
pass
except ValueError: # ? for except handle ecdh
pass
else:
# ! prevent same acc login twice
if username in nicknames:
init_encrypt(client, session_key, "LOGIN_DUPE")
# else account has not log in yet, proceed login
else:
# Retrieve salt for the user
cursor.execute('SELECT salt FROM users WHERE username = ?;', (username,))
salt_data = cursor.fetchone()
if salt_data:
salt = salt_data[0]
hashed_password = hash_password(password, salt)
# Verify login credentials
cursor.execute('SELECT id, username, password FROM users WHERE username = ? AND password = ?;', (username, hashed_password))
user_data = cursor.fetchone()
if user_data:
init_encrypt(client, session_key, "LOGIN_SUCCESS")
# ? Send all user keys from db to client - init_pqxdh()
resp_init_pqxdh(client, user_data[0])
# ? Recv new gen. opk from user - update_pqxdh()
resp_update_pqxdh(client, session_key, user_data[0])
# Set the username as the nickname
nicknames.append(username)
clients.append(client)
# ? Send corresponder keys from db to client - askUserMsg() -> calc_pqxdh()
# ! input valid here - loop if NO USER
try:
askUser_bool = resp_calc_pqxdh(client, session_key, nicknames, username)
if not askUser_bool:
while not askUser_bool:
askUser_bool = resp_calc_pqxdh(client, session_key, nicknames, username)
except:
pass
else:
print(f"Nickname of the client is {username}")
broadcast(f"{username} connected to the server!\n".encode('utf-8'))
client.send("Connected to the server".encode('utf-8'))
thread = threading.Thread(target=handle, args=(client,))
thread.start()
else:
init_encrypt(client, session_key, "LOGIN_FAILED")
else:
init_encrypt(client, session_key, "LOGIN_FAILED")
# register option
elif login_or_register.lower() == 'register':
try:
session_key = init_ecdh(client)
register_data = init_decrypt(client, session_key)
register_data = register_data.decode('utf-8').split()
username = register_data[0]
password = register_data[1]
except IndexError:
pass
except ValueError: # ? for except handle ecdh
pass
else:
# ! check if there are attempt on insert duplicate username
cursor.execute('SELECT id FROM users WHERE username = ?;', (username, ))
# ? if username exist, abort register
if len(cursor.fetchall()) == 1:
init_encrypt(client, session_key, "REGISTER_FAIL")
# ? else cont register with username
else:
# Generate salt
salt = generate_salt()
# Hash password
hashed_password = hash_password(password, salt)
# Insert new user into the database
cursor.execute('INSERT INTO users (username, password, salt) VALUES (?, ?, ?);', (username, hashed_password, salt))
conn.commit()
init_encrypt(client, session_key, "REGISTER_SUCCESS")
# ? Insert keys from client to db
store_keys(client, username)
else:
client.send("INVALID_OPTION".encode('utf-8'))
banner()
server_key = server_keygen()
server_key_public = server_key.public_key()
server_key_private = server_key
server_key_sig = server_keysign()
print("\nServer is running!")
receive()