-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
937 lines (733 loc) · 28.5 KB
/
main.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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
"""
Main
2021/02/26
Authors:
Adam Christensen
Riley Matthews
Justin Spidell
Evan Podrabsky
Lannin Nakai
"""
# our modules
import cryptography as c
import database as d
# needed for networking
import socket
import threading
import multiprocessing
import sys
import traceback
import pickle
from requests import get
# general
from datetime import datetime
# Every user on the local system will have their own ID.
USER_ID = 0
# Empty string to represent user
USERNAME = ""
# friendcode of the current user
FRIENDCODE = ""
INTERNAL_FRIENDCODE = ""
# [[ContactID, IV, MachineID, ContactName,
# IP_address, SecretKey, PublicKey, Port#],...]
CONTACT_LIST = []
# [[MessageID, IV, Text, Timestamp, Sent],...]
MESSAGE_LIST = []
# the IV created for the user when they made their account.
# Used for db encryption
USER_IV = None
# key used for encrypting and decrypting entries in the database
DB_KEY = None
# Users public key
PUBLIC_KEY = None
# Users private key
PRIVATE_KEY = None
# diffie hellman object. see cryptography.py
DIFFIE_HELLMAN = None
# user's public ipv4 address
PUBLIC_IP = None
# variable to hold the separate thread running the socket server
SERVER_THREAD = None
# networking constants
BUFSIZE = 4096 # TODO enforce this somewhere
FORMAT = 'utf-8' # probably not needed
# this reads from config.txt and retrieves the ip address and port to
# use when opening up the socket server
with open("config.txt") as config:
global SERVER_IP, PORT
server_ip = config.readline().replace(" ", "")
server_ip = server_ip[11:(len(server_ip) - 1)]
port = config.readline().replace(" ", "")
port = port[5:len(port)]
if len(server_ip) < 7 or len(server_ip) > 15:
sys.stderr.write("ERROR: Specified IPV4 address is not valid")
sys.exit()
try:
port = int(port)
if port < 0 or port > 65535:
sys.stderr.write("ERROR: Specified port number is not within the"
"standardized range")
except:
sys.stderr.write("ERROR: Specified port number is not an integer")
sys.exit()
SERVER_IP = server_ip
PORT = port
def initialize_database(db_name: str):
"""
initialize_database
Searches for an existing sql database of the given name
Returns an object of the database if it exists,
creates a new database if it does not exist
:param
db_name: name of the database. type: str
:return
database.database: an sql database object from the database module
"""
try:
db_file = open(db_name, "r")
db_file.close()
return d.database(db_name)
except:
db = d.database(db_name)
db.create_users()
db.create_contacts()
db.create_messages()
return db
# initialize the database global variable
DATABASE = initialize_database("Primary")
class Message:
def __init__(self, user_id, machine_id, flag, message,
signature=None, friendcode=None, tag=None, nonce=None):
# the user's ID on the user's system
self.user_ID = user_id
# the ID of the contact on the contact's system
# (different from contactID)
self.machine_ID = machine_id
# flag to help process the message
self.flag = flag
# the message
self.message = message
# cryptographic signature
self.signature = signature
# friendcode for verifying corresponding
# contact upon receiving a message
self.friendcode = friendcode
# needed for decrypting the message (should not be encrypted)
self.tag = tag
# needed for decrypting the message (should not be encrypted)
self.nonce = nonce
def get_user_ip() -> str:
"""
get_user_ip
Queries an ip bot server and returns the user's
public ipv4 address
:param
None
:return
public_ipv4: user's public ipv4 address. type: str
"""
public_ip = get("http://ipgrab.io").text
return public_ip[:(len(public_ip) - 1)]
def encode_friend(ip_address: str, user_id: int, port: int):
"""
encode_friend
encodes a given ip address, user id, and port number
into one number to be used as the friend code
:param
ip_address: the user's public ipv4 address. type: str
user_id: the number id of the current user. type: int
port: forwarded port number of the user. type: int
:return
friendcode: encoded friendcode. type: str
"""
ip_address = ip_address.split('.')
ip_address = list(map(int, ip_address))
a = 24
friendcode = []
for i in ip_address:
friendcode.append(i << a)
a += 8
friendcode = sum(friendcode)
friendcode += (port << 8)
friendcode += user_id
friendcode = hex(friendcode)
friendcode = friendcode[2:]
return friendcode
def decode_friend(friendcode):
"""
decode_friend
Decodes a friend code into the original ip address,
user id, and port number
:param
friendcode: encoded friendcode to decode. type: str
:return
ip_address: decoded ip address. type: str
user_id: decoded user id. type: int
port: decoded port number. type: int
"""
friendcode = int(friendcode, 16)
ip_address = []
while friendcode != 0:
ip_address.append(friendcode & 255)
friendcode = friendcode >> 8
machine_id = ip_address.pop(0)
port = ip_address.pop(0)
port += ip_address.pop(0) << 8
ip_address = '.'.join(map(str, ip_address))
return machine_id, ip_address, port
def create_account(username: str, password: str):
"""
create_account
Creates an account in the local sql database with the
given username and password
:param
username: The username of the given user. Not case-sensitive type: str
password: the password of the given user. Case-sensetive. type: str
:return
None
"""
print("Creating account...")
if not username or not password:
return -1
elif "'" in username or "'" in password:
return -2
# check if username is unique
elif DATABASE.find_user(username.lower()):
# this may have to be reworked to include the flask functionality
# of displaying a specific page or error message upon receiving
# an invalid/in-use username
print("User already exists")
return -3
# create public and private key
PublicKey, PrivateKey = c.generate_public_private()
# generate hash of user's password
PasswordHash = c.pwd2hash(c.string2bytes(password))
# user's local encryption key, not stored as global
# variable yet, this will be done during login
db_key = c.pwd2key(c.string2bytes(password))
# generate initialization vector for this user
User_IV = c.create_iv()
# get public ip address (must be connected to internet, otherwise blank)
IP_address = get_user_ip()
# encrypt publicKey, privateKey
enc_PublicKey = c.encrypt_db(PublicKey, db_key, User_IV)
enc_PrivateKey = c.encrypt_db(PrivateKey, db_key, User_IV)
enc_PORT = c.encrypt_db(c.string2bytes(str(PORT)), db_key, User_IV)
enc_IP_address = c.encrypt_db(c.string2bytes(IP_address), db_key, User_IV)
# anything which is initially stored as bytes must be encoded
# as a base64 byte string and converted to a python string before
# being added to the database
UserID = DATABASE.new_user(username.lower(),
c.bytes2string(c.bytes2base64(PasswordHash)),
c.bytes2string(c.bytes2base64(User_IV)),
c.bytes2string(c.bytes2base64(enc_IP_address)),
c.bytes2string(c.bytes2base64(enc_PORT)),
c.bytes2string(c.bytes2base64(enc_PublicKey)),
c.bytes2string(c.bytes2base64(enc_PrivateKey)))
print(f"New user {username} created! "
f"UserID: {UserID}")
# global variables will need to be set in login, but this may change
# when we figure out how to make flask and sql play nice
return 0
# contact in CONTACT_LIST
# [ContactID, IV, MachineID, Contactname, IP_address, SecretKey, PublicKey]
def login(username, password):
"""
login
This function checks two strings, username and password,
checks if the credentials match an entry in the user table
of the database, then prepares the environment for this user
to move to the messaging GUI by setting global variables from
the database.
Socket server is started upon login and stopped when
exiting the main messaging page
:param
username: The username of the given user. Not case-sensitive type: str
password: the password of the given user. Case-sensetive. type: str
:return
None
"""
global DB_KEY, USER_ID, PUBLIC_KEY, PRIVATE_KEY, \
USER_IV, CONTACT_LIST, DATABASE, USERNAME, \
FRIENDCODE, SERVER_THREAD, PUBLIC_IP, SERVER_IP, \
PORT, INTERNAL_FRIENDCODE
# no blank username or password accepted
if not username or not password:
return -1
# database doesn't like single apostrophe characters
elif "'" in username or "'" in password:
return -2
# Step 0: find if user with given username exists in the system
user_info = DATABASE.find_user(username.lower())
if user_info:
user_info = user_info[0]
print(f"User {user_info[1]} found!")
PasswordHash = c.base642bytes(c.string2bytes(user_info[2]))
if CONTACT_LIST:
CONTACT_LIST.clear()
if MESSAGE_LIST:
MESSAGE_LIST.clear()
else:
# database will return empty list if user not found
print("User not found")
return -3
# Step 1: Authenticate User
password_hash = c.pwd2hash(c.string2bytes(password))
# Step 2: check if password's hash matches stored hash
if (password_hash == PasswordHash):
# Step 2a (if step 2 is a success): set global variables
print("Password was correct!")
# if the correct password is entered, update globals
DB_KEY = c.pwd2key(c.string2bytes(password))
USER_ID = int(user_info[0])
USERNAME = user_info[1]
USER_IV = c.base642bytes(c.string2bytes(user_info[3]))
PUBLIC_IP = c.bytes2string(c.decrypt_db(
c.base642bytes(c.string2bytes(user_info[4])), DB_KEY, USER_IV))
User_PORT = int(c.bytes2string(c.decrypt_db(
c.base642bytes(c.string2bytes(user_info[5])), DB_KEY, USER_IV)))
FRIENDCODE = encode_friend(PUBLIC_IP, USER_ID, User_PORT)
INTERNAL_FRIENDCODE = encode_friend('127.0.0.1', USER_ID, User_PORT)
PUBLIC_KEY = c.decrypt_db(
c.base642bytes(c.string2bytes(user_info[6])), DB_KEY, USER_IV)
PRIVATE_KEY = c.decrypt_db(
c.base642bytes(c.string2bytes(user_info[7])), DB_KEY, USER_IV)
else:
print("Incorrect password")
# if the wrong password is entered
return -4
# Step 3: Populate Contact List global variable
_populate_contact_list(USER_ID)
# Step 4: start server
SERVER_THREAD = multiprocessing.Process(target=start_server,
args=(USER_ID,
DB_KEY,
USER_IV,
PUBLIC_IP,
SERVER_IP,
PORT,
FRIENDCODE,
PUBLIC_KEY,
PRIVATE_KEY))
return 0
def _clear_contact_list():
"""
_clear_contact_list
Helper function to clear contact list
:param
None
:return
None
"""
if CONTACT_LIST:
CONTACT_LIST.clear()
def _clear_message_list():
"""
_clear_message_list
Helper function to clear message list
:param
None
:return
None
"""
if MESSAGE_LIST:
MESSAGE_LIST.clear()
def _populate_contact_list(UserID: int) -> None:
"""
_populate_contact_list
Helper function for querying the sql database for contacts
to populate the global CONTACT_LIST
:param
UserID: The current user's ID
:return
None
"""
temp_contact_list = DATABASE.find_contacts(UserID)
for contact in temp_contact_list:
new_contact = []
# ContactID (no decrypt)
new_contact.append(int(contact[1]))
# Initialization Vector (no decrypt)
new_contact.append(c.base642bytes(c.string2bytes(contact[2])))
# Machine ID (decrypt)
new_contact.append(int(c.bytes2string(c.decrypt_db(c.base642bytes(
c.string2bytes(contact[3])), DB_KEY, new_contact[1]))))
# Contact name (decrypt)
new_contact.append(c.bytes2string(c.decrypt_db(c.base642bytes(
c.string2bytes(contact[4])), DB_KEY, new_contact[1])))
# IP address (decrypt)
new_contact.append(c.bytes2string(c.decrypt_db(c.base642bytes(
c.string2bytes(contact[5])), DB_KEY, new_contact[1])))
# Secret key
new_contact.append(c.decrypt_db(c.base642bytes(
c.string2bytes(contact[7])), DB_KEY, new_contact[1]))
# Public key (decrypt)
new_contact.append(c.decrypt_db(c.base642bytes(
c.string2bytes(contact[8])), DB_KEY, new_contact[1]))
# Port (decrypt)
new_contact.append(int(c.bytes2string(c.decrypt_db(c.base642bytes(
c.string2bytes(contact[6])), DB_KEY, new_contact[1]))))
CONTACT_LIST.append(new_contact)
return
# [[MessageID, IV, Text, Timestamp, Sent],...]
def _populate_message_list(UserID: int, ContactID) -> None:
"""
_populate_message_list
Helper function for querying the sql database for messages
from a specific User and Contact to populate the global MESSAGE_LIST
:param
UserID: The current user's ID
:return
None
"""
temp_message_list = DATABASE.find_messages(UserID, ContactID)
for message in temp_message_list:
new_message = []
# MessageID (no decrypt)
new_message.append(int(message[2]))
# Initialization Vector (no decrypt)
new_message.append(c.base642bytes(c.string2bytes(message[3])))
# Message Text (decrypt)
new_message.append(c.bytes2string(c.decrypt_db(c.base642bytes(
c.string2bytes(message[4])), DB_KEY, new_message[1])))
# Timestamp (decrypt)
new_message.append(c.bytes2string(c.decrypt_db(c.base642bytes(
c.string2bytes(message[5])), DB_KEY, new_message[1])))
# Sent (decrypt)
# Might be an Int or a string
new_message.append(int(c.bytes2string(c.decrypt_db(c.base642bytes(
c.string2bytes(message[6])), DB_KEY, new_message[1]))))
MESSAGE_LIST.append(new_message)
return
def add_contact(Contactname, friendcode, public_key=None):
"""
Creates a new contact in CONTACT_LIST.
Also adds new contact into database.
:param
Contactname: name of contact. type: str
friendcode: contacts friendcode. type: str
:return
returns bool
"""
# get machineID, port and ip_address from friendcode
machine_id, ip_address, port = decode_friend(friendcode)
# temp contactID
contactID = 0
# create iv
contactIV = c.create_iv()
# create new contact
new_contact = [contactID, contactIV, machine_id,
Contactname, ip_address, "temp sk", public_key, port]
# insert new contact into CONTACT_LIST
CONTACT_LIST.append(new_contact)
# Convert to bytes
machine_id = c.string2bytes(str(machine_id))
Contactname = c.string2bytes(str(Contactname))
ip_address = c.string2bytes(str(ip_address))
port = c.string2bytes(str(port))
public_key = c.string2bytes("temp pk")
sk = c.string2bytes("temp sk")
# encrypt senstitive information
enc_machine_id = c.encrypt_db(machine_id, DB_KEY, contactIV)
enc_Contactname = c.encrypt_db(Contactname, DB_KEY, contactIV)
enc_ip_address = c.encrypt_db(ip_address, DB_KEY, contactIV)
enc_port = c.encrypt_db(port, DB_KEY, contactIV)
# create new contact to be added to database
new_contact_db = (USER_ID,
c.bytes2string(c.bytes2base64(contactIV)),
c.bytes2string(c.bytes2base64(enc_machine_id)),
c.bytes2string(c.bytes2base64(enc_Contactname)),
c.bytes2string(c.bytes2base64(enc_ip_address)),
c.bytes2string(c.bytes2base64(enc_port)),
c.bytes2string(c.bytes2base64(public_key)),
c.bytes2string(c.bytes2base64(sk)))
# add contact to database
cid = DATABASE.new_contact(new_contact_db)[0][0]
# set contactID of new contact to the correct value
CONTACT_LIST[-1][0] = cid
return True
def start_keyexchange(contact):
"""
starts the keyexchange protocols with the given contact
:param
contact: list
:return
nothing
"""
# create the initial message
message = Message(USER_ID, contact[2], 'a1', PUBLIC_KEY,
None, friendcode=FRIENDCODE)
# send the message
send_message(message, contact)
# contact in CONTACT_LIST
# [ContactID, IV, MachineID, Contactname,
# IP_address, SecretKey, PublicKey, Port#]
def receive_message(connection, address):
"""
This function handles incoming messages.
:param
connection: python socket object
address: tuple (ip address of sender, port)
:return
bool
"""
global DIFFIE_HELLMAN
# NETWORK
# get the sent message object
# NOTE: larger messages might require the receiving to be done in a loop
msg = connection.recv(BUFSIZE)
message = pickle.loads(msg)
friend = message.friendcode
useless_machineID, contact_ip, contact_port = decode_friend(friend)
# get machineID from message
machineID = message.user_ID
# close the connection
connection.close()
if CONTACT_LIST:
CONTACT_LIST.clear()
_populate_contact_list(USER_ID)
# find contact in CONTACT_LIST based on ip_address and machineID
index = 0
for i in range(len(CONTACT_LIST)):
if (CONTACT_LIST[i][4] == contact_ip) \
and (CONTACT_LIST[i][2] == machineID) \
and (CONTACT_LIST[i][7]):
# found the right contact
index = i
break
# process flags
if message.flag == 'm':
print("flag: m, text received!")
# create secret key from contact
secret_key = CONTACT_LIST[index][5]
# decrypt message
plaintext = c.decrypt_mssg(message.message, secret_key,
message.tag, message.nonce)
# create message iv
messageIV = c.create_iv()
# create timestamp and string representation
timestamp = datetime.now()
datestr = timestamp.strftime("%m/%d/%Y, %H:%M:%S")
# convert to bytes
datestrb = c.string2bytes(datestr)
sent = c.string2bytes(str(0))
# encrypt sensitive information for the database
ciphertext = c.encrypt_db(plaintext, DB_KEY, messageIV)
enc_timestamp = c.encrypt_db(datestrb, DB_KEY, messageIV)
enc_sent = c.encrypt_db(sent, DB_KEY, messageIV)
# add message to database
MessageID = DATABASE.new_message(USER_ID,
CONTACT_LIST[index][0],
c.bytes2string(
c.bytes2base64(messageIV)),
c.bytes2string(
c.bytes2base64(ciphertext)),
c.bytes2string(
c.bytes2base64(enc_timestamp)),
c.bytes2string(
c.bytes2base64(enc_sent)))
# Add the new message to MESSAGE_LIST and update the GUI if appropriate
new_message = [USER_ID, CONTACT_LIST[index][0], MessageID,
messageIV, plaintext, datestr, 0]
MESSAGE_LIST.append(new_message)
return True
# Below are automatic key exchange protocol messages.
# These messages should not be kept in the database
elif message.flag == 'a1':
# flag a1: step 1 of the public key exchange
public_key = message.message
# add public_key to contact in CONTACT_LIST
CONTACT_LIST[index][6] = public_key
# encrypt the public key before storing in the database
enc_public_key = c.encrypt_db(public_key, DB_KEY,
CONTACT_LIST[index][1])
DATABASE.store_pub_key(c.bytes2string(c.bytes2base64(enc_public_key)),
CONTACT_LIST[index][0])
# NETWORKING: send this message
new_message = Message(USER_ID, machineID, 'a2',
PUBLIC_KEY, friendcode=FRIENDCODE)
send_message(new_message, CONTACT_LIST[index])
return True
elif message.flag == 'a2':
# flag a2: step 2 of the public key exchange
public_key = message.message
# add public_key to contact in CONTACT_LIST
CONTACT_LIST[index][6] = public_key
# encrypt the public key before storing in the database
enc_public_key = c.encrypt_db(public_key, DB_KEY,
CONTACT_LIST[index][1])
DATABASE.store_pub_key(c.bytes2string(c.bytes2base64(enc_public_key)),
CONTACT_LIST[index][0])
# initiate diffie hellman key exchange
DIFFIE_HELLMAN = c.DiffieHellman()
g, p = DIFFIE_HELLMAN.create_gp()
value = DIFFIE_HELLMAN.create_value()
# NETWORKING: send this message
new_message = Message(USER_ID, machineID, 'b1', (g, p, value),
friendcode=FRIENDCODE)
send_message(new_message, CONTACT_LIST[index])
return True
elif message.flag == 'b1':
# flag b1: step 1 of the diffie hellman key exchange
g, p, value = message.message
DIFFIE_HELLMAN = c.DiffieHellman(g, p)
other_value = DIFFIE_HELLMAN.create_value()
key = DIFFIE_HELLMAN.create_key(value)
# add key to contact in CONTACT_LIST - this completes the contact
CONTACT_LIST[index][5] = key
# first encrypt the key
enc_key = c.encrypt_db(key, DB_KEY, CONTACT_LIST[index][1])
# update the database with the key
DATABASE.store_sec_key(c.bytes2string(c.bytes2base64(enc_key)),
CONTACT_LIST[index][0])
# NETWORKING: send this message
new_message = Message(USER_ID, machineID, 'b2', other_value,
friendcode=FRIENDCODE)
send_message(new_message, CONTACT_LIST[index])
return True
elif message.flag == 'b2':
value = message.message
key = DIFFIE_HELLMAN.create_key(value)
# add key to contact in CONTACT_LIST - this completes the contact
CONTACT_LIST[index][5] = key
# first encrypt the key
enc_key = c.encrypt_db(key, DB_KEY, CONTACT_LIST[index][1])
# update the database with the key
DATABASE.store_sec_key(c.bytes2string(c.bytes2base64(enc_key)),
CONTACT_LIST[index][0])
return True
else:
print("Message Flag Error")
return False
def create_message(text, contact):
"""
Creates a message object with the given text addressed to the given contact
param:
text: str
contact: list
return: nothing
"""
# get necessary info
text = c.string2bytes(text)
machineID = contact[2]
secret_key = contact[5]
# create message
ciphertext, tag, nonce = c.encrypt_mssg(text, secret_key)
signature = c.create_signature(text, PRIVATE_KEY)
message = Message(USER_ID, machineID, 'm', ciphertext, signature,
FRIENDCODE, tag, nonce)
return message
# contact in CONTACT_LIST
# [ContactID, IV, MachineID, Contactname,
# IP_address, SecretKey, PublicKey, Port#]
def send_message(message, contact):
"""
Sends a message object to the requested contact.
Also stores sent message in database.
:param
message: message object
contact: list
:return
1 is sucessful, 0 if not
"""
# get necessary info
ip_address = contact[4]
port = contact[7]
# create a socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# try connecting the socket to the given host and port
try:
client.connect((ip_address, port))
except:
print("Connection Error to", (ip_address, port))
return 0
# pickle the message object and send it
pickled_message = pickle.dumps(message)
client.send(pickled_message)
# close the connection
client.close()
# we only want to store messages with the 'm' flag
if message.flag == 'm':
# create secret key from contact
secret_key = contact[5]
# decrypt message
plaintext = c.decrypt_mssg(message.message, secret_key,
message.tag, message.nonce)
# create message iv
messageIV = c.create_iv()
# create timestamp and string representation
timestamp = datetime.now()
datestr = timestamp.strftime("%m/%d/%Y, %H:%M:%S")
# convert to bytes
datestrb = c.string2bytes(datestr)
sent = c.string2bytes(str(1))
# encrypt sensitive information for the database
ciphertext = c.encrypt_db(plaintext, DB_KEY, messageIV)
enc_timestamp = c.encrypt_db(datestrb, DB_KEY, messageIV)
enc_sent = c.encrypt_db(sent, DB_KEY, messageIV)
# add message to database
MessageID = DATABASE.new_message(USER_ID,
contact[0],
c.bytes2string(
c.bytes2base64(messageIV)),
c.bytes2string(
c.bytes2base64(ciphertext)),
c.bytes2string(
c.bytes2base64(enc_timestamp)),
c.bytes2string(
c.bytes2base64(enc_sent)))
# add new message to MESSAGE_LIST
new_message = [USER_ID, contact[0], MessageID,
messageIV, plaintext, datestr, 1]
MESSAGE_LIST.append(new_message)
return 1
def start_server(user_id: int, db_key: bytes, user_iv: bytes,
public_ip: str, server_ip: str, port: int,
friendcode: str, public_key: bytes, private_key: bytes):
"""
The purpose of this function is to
(a) create a server socket
(b) listen for connections
(c) handle incoming messages
"""
# need to update global variables for when this server
# is run in a separate thread from the flask server
global USER_ID, DB_KEY, PUBLIC_KEY, PRIVATE_KEY, \
USER_IV, PUBLIC_IP, SERVER_IP, PORT, FRIENDCODE
USER_ID = user_id
DB_KEY = db_key
USER_IV = user_iv
PUBLIC_IP = public_ip
FRIENDCODE = friendcode
SERVER_IP = server_ip
PORT = port
PUBLIC_KEY = public_key
PRIVATE_KEY = private_key
# create a socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
server.bind((SERVER_IP, PORT))
except:
print("Bind failed. Error : " + str(sys.exc_info()))
sys.exit()
# listen
server.listen()
print('server now listening at: ', SERVER_IP, PORT)
# continuously loop to accept connections
while True:
# accept a connection
connection, address = server.accept()
ip, port = server.getsockname()
print("Connected with " + ip + ":" + str(port))
# start a new thread for connection
try:
thread = threading.Thread(target=receive_message,
args=[connection, address])
thread.start()
except:
print('thread did not start')
traceback.print_exc()
if __name__ == "__main__":
# start the message server
start_server(None, None, None, None, None, None, None, None, None)