/
server.py
102 lines (71 loc) · 2.75 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
import socket
from OpenSSL import crypto
import config2 as config
import sys
import certificate
from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
from Crypto.Cipher import AES, PKCS1_OAEP
HEADERSIZE = 10
def handle_message(s):
full_msg = b''
new_msg = True
while True:
msg = s.recv(16)
if msg:
if new_msg:
print("new msg len:", msg[:HEADERSIZE])
msglen = int(msg[:HEADERSIZE])
new_msg = False
print(f"full message length: {msglen}")
full_msg += msg
print(len(full_msg))
if len(full_msg) - HEADERSIZE == msglen:
print("full msg recvd")
# print(full_msg[HEADERSIZE:])
new_msg = True
return full_msg[HEADERSIZE:]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 1242))
s.listen(5)
with open(config.cert_path) as file:
cert = crypto.load_certificate(crypto.FILETYPE_PEM, file.read())
with open(config.privkey_path) as file:
privkey = crypto.load_privatekey(crypto.FILETYPE_PEM, file.read())
def send(socket, msg: bytes):
msg = bytes(f"{len(msg):<{HEADERSIZE}}", "utf-8") + msg
socket.send(msg)
# now our endpoint knows about the OTHER endpoint.
clientsocket, address = s.accept()
print(f"Connection from {address} has been established.")
msg = crypto.dump_certificate(crypto.FILETYPE_PEM, cert)
msg = bytes(f"{len(msg):<{HEADERSIZE}}", "utf-8") + msg
clientsocket.send(msg)
client_cert = handle_message(clientsocket).decode()
client_cert = crypto.load_certificate(crypto.FILETYPE_PEM, client_cert)
if not certificate.verify_cert(client_cert):
print("Client certificate is false, ending connection")
clientsocket.close()
sys.exit(1)
print("Client certificate is alright")
client_pubkey = client_cert.get_pubkey()
client_pubkey = RSA.import_key(crypto.dump_publickey(crypto.FILETYPE_PEM, client_pubkey))
session_key = get_random_bytes(16)
# Encrypt the session key with the public RSA key
cipher_rsa = PKCS1_OAEP.new(client_pubkey)
enc_session_key = cipher_rsa.encrypt(session_key)
send(clientsocket, enc_session_key)
print("sending crypted aes key")
cipher = AES.new(session_key, AES.MODE_EAX)
while True:
data = input("Enter message: ")
if data:
ciphertext, tag = cipher.encrypt_and_digest(data.encode())
send(clientsocket, cipher.nonce + tag + ciphertext)
cipher = AES.new(session_key, AES.MODE_EAX)
msg = handle_message(clientsocket)
if msg:
nonce, tag, ciphertext = msg[0:16], msg[16:32], msg[32:]
decipher = AES.new(session_key, AES.MODE_EAX, nonce)
data = decipher.decrypt_and_verify(ciphertext, tag)
print(data.decode())