-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.py
229 lines (182 loc) · 6.78 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
#!/usr/bin/env python3
# encoding: UTF-8
# Swopnil N. Shrestha
# 2020/04/16
from socket import socket, gethostname
from socket import AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR
from typing import Tuple, Dict
from Crypto.Hash import SHA256, HMAC
from Crypto.Cipher import AES, DES, Blowfish
from diffiehellman.diffiehellman import DiffieHellman
HOST = gethostname()
PORT = 4600
SUPPORTED_CIPHERS = {"DES": [56]}
def parse_proposal(msg: str) -> Dict[str, list]:
"""Parse client's proposal
:param msg: message from the client with a proposal (ciphers and key sizes)
:return: the ciphers and keys as a dictionary
"""
msg = msg[16:]
ciphers = {}
last_c = ''
cipher_name = ''
key_size = ''
curr_key_size_list = []
for c in msg:
if c.isalpha():
cipher_name += c
elif c.isalnum():
key_size += c
elif c in ',':
if last_c.isalnum():
curr_key_size_list.append(int(key_size))
key_size = ''
elif c == ']':
curr_key_size_list.append(int(key_size))
key_size = ''
ciphers[cipher_name] = curr_key_size_list
cipher_name = ''
curr_key_size_list = []
last_c = c
return ciphers
def select_cipher(supported: dict, proposed: dict) -> Tuple[str, int]:
"""Select a cipher to use
:param supported: dictionary of ciphers supported by the server
:param proposed: dictionary of ciphers proposed by the client
:return: tuple (cipher, key_size) of the common cipher where key_size is the longest supported by both
:raise: ValueError if there is no (cipher, key_size) combination that both client and server support
"""
common_ciphers = set(supported.keys()).intersection(proposed.keys())
cipher = None
key_size = -1
if common_ciphers != set():
for c in common_ciphers:
current_keysize = max(
# -1 will be the max value if the intersection is empty
set([-1]).union(set(supported.get(c)).intersection(proposed.get(c))))
if current_keysize > key_size:
key_size = current_keysize
cipher = c
if not cipher or key_size == -1:
raise ValueError(
'Could not agree on a cipher')
return (cipher, key_size)
def generate_cipher_response(cipher: str, key_size: int) -> str:
"""Generate a response message
:param cipher: chosen cipher
:param key_size: chosen key size
:return: (cipher, key_size) selection as a string
"""
return "ChosenCipher:{},{}".format(cipher, key_size)
def parse_dhm_request(msg: str) -> int:
"""Parse client's DHM key exchange request
:param msg: client's DHMKE initial message
:return: number in the client's message
"""
return int(msg.split(':')[1])
def get_key_and_iv(
shared_key: str, cipher_name: str, key_size: int
) -> Tuple[object, bytes, bytes]:
"""Get key and IV from the generated shared secret key
:param shared_key: shared key as computed by `diffiehellman`
:param cipher_name: negotiated cipher's name
:param key_size: negotiated key size
:return: (cipher, key, IV) tuple
cipher_name must be mapped to a Crypto.Cipher object
`key` is the *first* `key_size` bytes of the `shared_key`
DES key must be padded to 64 bits with 0
Length `ivlen` of IV depends on a cipher
`iv` is the *last* `ivlen` bytes of the shared key
Both key and IV must be returned as bytes
"""
cipher_map = {
"DES": DES, "AES": AES, "Blowfish": Blowfish
}
ivlen = {
"DES": 8, "AES": 16, "Blowfish": 8
}
cipher = cipher_map.get(cipher_name)
key = shared_key[:key_size//8]
if cipher_name == "DES":
key += '\0'
key = key.encode()
iv = shared_key[-1 * ivlen.get(cipher_name):].encode()
return cipher, key, iv
def generate_dhm_response(public_key: int) -> str:
"""Generate DHM key exchange response
:param public_key: public portion of the DHMKE
:return: string according to the specification
"""
return 'DHMKE:{}'.format(public_key)
def read_message(msg_cipher: bytes, crypto: object) -> Tuple[str, str]:
"""Read the incoming encrypted message
:param msg_cipher: encrypted message from the socket
:crypto: chosen cipher, must be initialized in the `main`
:return: (plaintext, hmac) tuple
"""
ciph_in = msg_cipher[:-64]
hmac = msg_cipher[-64:].decode('utf-8')
plaintext = crypto.decrypt(ciph_in).decode('utf-8')
plaintext = plaintext.strip('\0')
return plaintext, hmac
def validate_hmac(msg_cipher: bytes, hmac_in: str, hashing: object) -> bool:
"""Validate HMAC
:param msg_cipher: encrypted message from the socket
:param hmac_in: HMAC received from the client
:param hashing: hashing object, must be initialized in the `main`
:raise: ValueError is HMAC is invalid
"""
ciphertext = msg_cipher[:-64]
hashing.update(ciphertext)
hashvalue = hashing.hexdigest()
if hashvalue == hmac_in:
return True
else:
raise ValueError('Bad HMAC')
def main():
"""Main loop
See vpn.md for details
"""
# Create the socket
server_sckt = socket(AF_INET, SOCK_STREAM)
server_sckt.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
server_sckt.bind((HOST, PORT))
server_sckt.listen()
print(f"Listening on {HOST}:{PORT}")
conn, client = server_sckt.accept()
print(f"New client: {client[0]}:{client[1]}")
# Negotiating the cipher
print("Negotiating the cipher")
msg_in = conn.recv(4096).decode('utf-8')
proposed = parse_proposal(msg_in)
cipher_name, key_size = select_cipher(SUPPORTED_CIPHERS, proposed)
print(f"We are going to use {cipher_name}{key_size}")
msg_out = generate_cipher_response(cipher_name, key_size)
conn.send(msg_out.encode())
# Negotiating the key
print("Negotiating the key")
dh = DiffieHellman()
dh.generate_public_key()
msg_in = conn.recv(4096).decode('utf-8')
client_public_key = parse_dhm_request(msg_in)
dh.generate_shared_secret(client_public_key)
msg_out = generate_dhm_response(dh.public_key)
conn.send(msg_out.encode())
cipher, key, iv = get_key_and_iv(dh.shared_key, cipher_name, key_size)
print("The key has been established")
print("Initializing cryptosystem")
crypto = cipher.new(key, cipher.MODE_CBC, iv)
hashing = HMAC.new(key, digestmod=SHA256)
print("All systems ready")
while True:
msg_in = conn.recv(4096)
if len(msg_in) < 1:
conn.close()
break
msg, hmac = read_message(msg_in, crypto)
validate_hmac(msg_in, hmac, hashing)
print(f"Received: {msg}")
msg_out = f"Server says: {msg[::-1]}"
conn.send(msg_out.encode())
if __name__ == "__main__":
main()