-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
441 lines (385 loc) · 14.9 KB
/
test.py
File metadata and controls
441 lines (385 loc) · 14.9 KB
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
import logging
import sqlite3
from datetime import datetime
import pytz
from utils import (
generate_rsa_key_pair,
handshake_initiate,
handshake_respond,
encrypt_3des_key,
decrypt_3des_key,
sign_auth_info,
verify_auth_info,
generate_3des_key,
create_message_packet,
verify_and_decrypt_message
)
# Cấu hình logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# Đặt múi giờ Việt Nam
vn_timezone = pytz.timezone('Asia/Ho_Chi_Minh')
def get_db_connection():
"""Tạo kết nối đến database"""
conn = sqlite3.connect('instance/chat.db')
conn.row_factory = sqlite3.Row
return conn
def setup_db():
"""Khởi tạo database và các bảng"""
conn = get_db_connection()
cursor = conn.cursor()
# Tạo các bảng
tables = [
'''CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
gmail TEXT UNIQUE,
password_hash TEXT,
rsa_public_key TEXT,
rsa_private_key TEXT,
created_at TIMESTAMP)''',
'''CREATE TABLE IF NOT EXISTS invitations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sender_id INTEGER,
receiver_gmail TEXT,
token TEXT,
status TEXT,
created_at TIMESTAMP)''',
'''CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sender_id INTEGER,
receiver_id INTEGER,
ciphertext TEXT,
iv TEXT,
hash TEXT,
signature TEXT,
status TEXT,
created_at TIMESTAMP)''',
'''CREATE TABLE IF NOT EXISTS contacts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
contact_user_id INTEGER,
created_at TIMESTAMP)''',
'''CREATE TABLE IF NOT EXISTS sessions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
contact_user_id INTEGER,
triple_des_key TEXT,
created_at TIMESTAMP)'''
]
for table in tables:
cursor.execute(table)
conn.commit()
conn.close()
logger.info("Database initialized")
def register_user(name, gmail, password):
"""Đăng ký user mới"""
conn = get_db_connection()
cursor = conn.cursor()
try:
public_key, private_key = generate_rsa_key_pair()
created_at = datetime.now(vn_timezone)
cursor.execute(
"INSERT INTO users (name, gmail, password_hash, rsa_public_key, rsa_private_key, created_at) VALUES (?, ?, ?, ?, ?, ?)",
(name, gmail, password, public_key, private_key, created_at)
)
conn.commit()
user_id = cursor.lastrowid
logger.info(f"User {name} registered successfully (ID: {user_id})")
return user_id, public_key, private_key
except Exception as e:
conn.rollback()
logger.error(f"Registration failed: {str(e)}")
raise
finally:
conn.close()
def login_user(gmail, password):
"""Đăng nhập user"""
conn = get_db_connection()
cursor = conn.cursor()
try:
cursor.execute(
"SELECT id, password_hash, rsa_public_key, rsa_private_key FROM users WHERE gmail = ?",
(gmail,)
)
user = cursor.fetchone()
if user and password == user['password_hash']:
logger.info(f"User {gmail} logged in successfully")
return user['id'], user['rsa_public_key'], user['rsa_private_key']
logger.warning(f"Login failed for {gmail}")
return None, None, None
except Exception as e:
logger.error(f"Login error: {str(e)}")
raise
finally:
conn.close()
def send_invitation(sender_id, receiver_gmail):
"""Gửi lời mời kết bạn"""
conn = get_db_connection()
cursor = conn.cursor()
try:
token = "dummy_token"
created_at = datetime.now(vn_timezone)
cursor.execute(
"INSERT INTO invitations (sender_id, receiver_gmail, token, status, created_at) VALUES (?, ?, ?, ?, ?)",
(sender_id, receiver_gmail, token, 'pending', created_at)
)
conn.commit()
logger.info(f"Invitation sent from {sender_id} to {receiver_gmail}")
return token
except Exception as e:
conn.rollback()
logger.error(f"Failed to send invitation: {str(e)}")
raise
finally:
conn.close()
def confirm_invitation(token):
"""Xác nhận lời mời"""
conn = get_db_connection()
cursor = conn.cursor()
try:
cursor.execute(
"SELECT sender_id, receiver_gmail FROM invitations WHERE token = ? AND status = 'pending'",
(token,)
)
invitation = cursor.fetchone()
if invitation:
cursor.execute(
"SELECT id FROM users WHERE gmail = ?",
(invitation['receiver_gmail'],)
)
receiver = cursor.fetchone()
if receiver:
created_at = datetime.now(vn_timezone)
# Cập nhật trạng thái lời mời
cursor.execute(
"UPDATE invitations SET status = 'accepted' WHERE token = ?",
(token,)
)
# Tạo liên hệ hai chiều
cursor.execute(
"INSERT INTO contacts (user_id, contact_user_id, created_at) VALUES (?, ?, ?)",
(invitation['sender_id'], receiver['id'], created_at)
)
cursor.execute(
"INSERT INTO contacts (user_id, contact_user_id, created_at) VALUES (?, ?, ?)",
(receiver['id'], invitation['sender_id'], created_at)
)
conn.commit()
logger.info(f"Invitation confirmed (Token: {token})")
return True
return False
except Exception as e:
conn.rollback()
logger.error(f"Failed to confirm invitation: {str(e)}")
raise
finally:
conn.close()
def handshake(sender_id, receiver_id):
"""Thực hiện handshake"""
conn = get_db_connection()
cursor = conn.cursor()
try:
# Gửi "Hello!" và nhận "Ready!"
if handshake_initiate() == "Hello!" and handshake_respond() == "Ready!":
created_at = datetime.now(vn_timezone)
# Tạo session hai chiều
cursor.execute(
"INSERT INTO sessions (user_id, contact_user_id, created_at) VALUES (?, ?, ?)",
(sender_id, receiver_id, created_at)
)
cursor.execute(
"INSERT INTO sessions (user_id, contact_user_id, created_at) VALUES (?, ?, ?)",
(receiver_id, sender_id, created_at)
)
conn.commit()
session_id = cursor.lastrowid
logger.info(f"Handshake completed (Session ID: {session_id})")
return session_id
return None
except Exception as e:
conn.rollback()
logger.error(f"Handshake failed: {str(e)}")
raise
finally:
conn.close()
def exchange_key(sender_id, receiver_id, sender_private_key, sender_public_key, receiver_public_key):
"""Trao đổi khóa mã hóa"""
conn = get_db_connection()
cursor = conn.cursor()
try:
# Tạo và ký thông tin xác thực
auth_result = sign_auth_info(sender_id, receiver_id, sender_private_key)
# Xác thực chữ ký
if not verify_auth_info(auth_result['signature'], auth_result['auth_data'], sender_public_key):
raise ValueError("Authentication failed")
# Tạo và mã hóa khóa 3DES
triple_des_key = generate_3des_key()
encrypted_key = encrypt_3des_key(triple_des_key, receiver_public_key)
# Lưu khóa vào session hai chiều
created_at = datetime.now(vn_timezone)
cursor.execute(
"UPDATE sessions SET triple_des_key = ? WHERE user_id = ? AND contact_user_id = ?",
(encrypted_key, sender_id, receiver_id)
)
cursor.execute(
"UPDATE sessions SET triple_des_key = ? WHERE user_id = ? AND contact_user_id = ?",
(encrypted_key, receiver_id, sender_id)
)
conn.commit()
logger.info("Key exchange successful")
return encrypted_key
except Exception as e:
conn.rollback()
logger.error(f"Key exchange failed: {str(e)}")
raise
finally:
conn.close()
def send_message(sender_id, receiver_id, message, sender_private_key, receiver_public_key):
"""Gửi tin nhắn mã hóa"""
conn = get_db_connection()
cursor = conn.cursor()
try:
# Tạo khóa mới cho mỗi tin nhắn
triple_des_key = generate_3des_key()
logger.debug(f"New 3DES key generated: {triple_des_key.hex()}")
# Tạo gói tin nhắn
packet = create_message_packet(message, triple_des_key, sender_private_key)
logger.debug(f"Message packet created (IV: {packet['iv'][:10]}..., Cipher: {packet['cipher'][:10]}...)")
# Mã hóa khóa bằng public key người nhận
encrypted_key = encrypt_3des_key(triple_des_key, receiver_public_key)
# Lưu khóa vào session
created_at = datetime.now(vn_timezone)
cursor.execute(
"UPDATE sessions SET triple_des_key = ? WHERE user_id = ? AND contact_user_id = ?",
(encrypted_key, sender_id, receiver_id)
)
cursor.execute(
"UPDATE sessions SET triple_des_key = ? WHERE user_id = ? AND contact_user_id = ?",
(encrypted_key, receiver_id, sender_id)
)
# Lưu tin nhắn
cursor.execute(
"""INSERT INTO messages
(sender_id, receiver_id, ciphertext, iv, hash, signature, status, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
(sender_id, receiver_id, packet['cipher'], packet['iv'],
packet['hash'], packet['sig'], 'sent', created_at)
)
conn.commit()
message_id = cursor.lastrowid
logger.info(f"Message sent (ID: {message_id})")
return message_id
except Exception as e:
conn.rollback()
logger.error(f"Failed to send message: {str(e)}", exc_info=True)
raise
finally:
conn.close()
def receive_message(receiver_id, sender_id, receiver_private_key, sender_public_key):
"""Nhận và giải mã tin nhắn"""
conn = get_db_connection()
cursor = conn.cursor()
try:
# Lấy tin nhắn chưa đọc
cursor.execute(
"""SELECT iv, ciphertext, hash, signature FROM messages
WHERE receiver_id = ? AND sender_id = ? AND status = 'sent'""",
(receiver_id, sender_id)
)
message = cursor.fetchone()
if not message:
logger.warning("No unread messages found")
return None
# Lấy khóa mã hóa từ session
cursor.execute(
"SELECT triple_des_key FROM sessions WHERE user_id = ? AND contact_user_id = ?",
(receiver_id, sender_id)
)
session = cursor.fetchone()
if not session or not session['triple_des_key']:
raise ValueError("No encryption key found")
# Giải mã khóa 3DES
triple_des_key = decrypt_3des_key(session['triple_des_key'], receiver_private_key)
logger.debug(f"Decrypted 3DES key: {triple_des_key.hex()}")
# Tạo packet
packet = {
'iv': message['iv'],
'cipher': message['ciphertext'],
'hash': message['hash'],
'sig': message['signature']
}
# Giải mã và xác thực
plaintext, status = verify_and_decrypt_message(packet, triple_des_key, sender_public_key)
if status != "ACK":
raise ValueError(f"Verification failed: {status}")
# Cập nhật trạng thái tin nhắn
cursor.execute(
"UPDATE messages SET status = 'delivered' WHERE receiver_id = ? AND sender_id = ? AND ciphertext = ?",
(receiver_id, sender_id, message['ciphertext'])
)
conn.commit()
logger.info("Message received and decrypted successfully")
return plaintext
except ValueError as ve:
conn.rollback()
logger.error(f"Message verification failed: {str(ve)}")
return None
except Exception as e:
conn.rollback()
logger.error(f"Failed to receive message: {str(e)}", exc_info=True)
return None
finally:
conn.close()
def main():
"""Hàm chính để chạy test"""
try:
# 1. Khởi tạo database
setup_db()
# 2. Đăng ký 2 user
print("\n=== Đăng ký user ===")
user1_id, user1_public_key, user1_private_key = register_user("User1", "user1@example.com", "password123")
user2_id, user2_public_key, user2_private_key = register_user("User2", "user2@example.com", "password123")
# 3. Đăng nhập
print("\n=== Đăng nhập ===")
user1_id, user1_public_key, user1_private_key = login_user("user1@example.com", "password123")
user2_id, user2_public_key, user2_private_key = login_user("user2@example.com", "password123")
# 4. Gửi và xác nhận lời mời
print("\n=== Kết bạn ===")
token = send_invitation(user1_id, "user2@example.com")
if not confirm_invitation(token):
raise RuntimeError("Failed to confirm invitation")
# 5. Handshake
print("\n=== Handshake ===")
session_id = handshake(user1_id, user2_id)
if not session_id:
raise RuntimeError("Handshake failed")
# 6. Trao đổi khóa
print("\n=== Trao đổi khóa ===")
encrypted_key = exchange_key(
user1_id, user2_id,
user1_private_key, user1_public_key, user2_public_key
)
if not encrypted_key:
raise RuntimeError("Key exchange failed")
# 7. Gửi tin nhắn
print("\n=== Gửi tin nhắn ===")
message_id = send_message(
user1_id, user2_id,
"Xin chào từ User1!",
user1_private_key, user2_public_key
)
if not message_id:
raise RuntimeError("Failed to send message")
# 8. Nhận tin nhắn
print("\n=== Nhận tin nhắn ===")
received_msg = receive_message(user2_id, user1_id, user2_private_key, user1_public_key)
if received_msg:
print(f"\n=== TEST THÀNH CÔNG ===\nTin nhắn nhận được: '{received_msg}'\n")
else:
print("\n=== TEST THẤT BẠI ===\nKhông nhận được tin nhắn\n")
except Exception as e:
logger.error(f"Test failed: {str(e)}", exc_info=True)
print("\n=== TEST THẤT BẠI ===\n")
if __name__ == "__main__":
main()