-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
422 lines (339 loc) · 12.9 KB
/
utils.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
from abc import ABC, abstractmethod
from typing import Optional, Literal
import asyncio
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives import hashes, hmac
from cryptography.hazmat.primitives.ciphers import (
Cipher,
algorithms,
modes,
CipherContext,
)
import fingerprint
import logging
class CryptoUtils(ABC):
"""
Abstract utility class, provides cryptographic functionality for encrypted communications.
The class was made abstract because the attributes required by the methods of the class are not directly populated here.
Attributes
----------
_aes_key_size : Literal[128, 192, 256]
Size of the AES key in bits (default is 256).
_iv : Optional[bytes]
Initialization vector for AES-GCM mode.
_key : Optional[bytes]
AES key used for encryption/decryption.
_cipher : Optional[Cipher]
Cipher object for cryptographic operations.
_encryptor : Optional[CipherContext]
Encryptor context for AES encryption.
_decryptor : Optional[CipherContext]
Decryptor context for AES decryption.
"""
def __init__(self) -> None:
self._aes_key_size: Literal[128, 192, 256] = 256
self._iv: Optional[bytes] = None
self._key: Optional[bytes] = None
self.cipher: Optional[Cipher] = None
self._encryptor: Optional[CipherContext] = None
self._decryptor: Optional[CipherContext] = None
self._hmac_context: Optional[hashes.HashContext] = None
def initialize_cipher(self, key: bytes, iv: bytes) -> None:
"""
Initialize the encryptor and decryptor contexts for future data encryption/decryption
Parameters
----------
key
AES key
iv
Initialization vector
"""
self._hmac_context: hmac.HMAC = hmac.HMAC(self._key, hashes.SHA256())
self._cipher = Cipher(algorithms.AES(self._key), modes.GCM(self._iv))
self._encryptor = self._cipher.encryptor()
self._decryptor = self._cipher.decryptor()
def pack_message(self, data: bytes) -> bytes:
"""
Pack data for transmission by encrypting and signing it.
Parameters
----------
data
Data to be packed.
Returns
-------
bytes
Encrypted data with appended HMAC signature.
"""
ciphertext = self.cipher_operation(data, self._encryptor)
signature = self.sign_with_hmac(ciphertext)
return ciphertext + signature
def unpack_message(self, data: bytes) -> bytes:
signature = data[-32:] # signature (32 bytes) is appended at the end of the ciphertext
hmac_ctx = self._hmac_context.copy()
hmac_ctx.update(data[:-32])
hmac_ctx.verify(signature)
decrypted_data = self.cipher_operation(data[:-32], self._decryptor)
return decrypted_data
def sign_with_hmac(
self, data: bytes
) -> bytes:
"""
Sign `data` using the instance's HMAC context.
To create a HMAC context call initialize_cipher
Parameters
----------
data
Data to sign
Returns
-------
bytes
Signature of the data
"""
hmac_ctx = self._hmac_context.copy()
hmac_ctx.update(data)
signature = hmac_ctx.finalize()
return signature
def cipher_operation(self, data: bytes, cryptor: CipherContext) -> bytes:
"""Encrypts/decrypts data using the provided cryptor (CipherContext) object.
Parameters
----------
data
Data to be encrypted/decrypted
cryptor
The CipherContext representing the encryptor/decryptor
Returns
-------
bytes
Result of the according cryptographic operation
"""
result = cryptor.update(data) + cryptor.finalize()
return result
def calculate_hash(self, data: bytes, algorithm: hashes.HashAlgorithm) -> bytes:
"""
Calculate the hash of the given data using the specified hashing algorithm.
Parameters
----------
data
Data to hash.
algorithm
Hashing algorithm to use.
Returns
-------
bytes
Hash value of the data.
"""
digest = hashes.Hash(algorithm)
digest.update(data)
signature = digest.finalize()
return signature
class BaseAsyncSock(ABC):
"""
Base class for an asynchronous socket.
This abstract base class provides a framework for establishing and managing
an asynchronous socket connection. It includes methods for starting the socket,
sending data, and receiving data. Derived classes should implement the
`start_socket` method to handle the specifics of establishing the connection.
Parameters
----------
host
The hostname or IP address of the socket server.
port
The port number of the socket server.
logger
Logger instance for logging socket events.
Attributes:
-----------
host
The hostname or IP address of the socket server.
port
The port number of the socket server.
logger
Logger instance for logging socket events.
reader
Asynchronous stream reader for the socket.
writer
Asynchronous stream writer for the socket.
"""
def __init__(self, host: str, port: str, logger: logging.Logger) -> None:
self.host = host
self.port = port
self.logger = logger
self.reader: Optional[asyncio.StreamReader] = None
self.writer: Optional[asyncio.StreamWriter] = None
@abstractmethod
async def start_socket(self) -> None: ...
"""Abstract method to start the socket connection. Must be implemented by derived classes."""
async def receive(self, buffer: int) -> bytes:
"""
Asynchronously receives data from the socket.
Parameters
----------
buffer
The maximum number of bytes to read.
Returns
-------
bytes
The data received from the socket.
Logs a warning if no data is received.
"""
data = await self.reader.read(buffer)
if not data:
self.logger.warning("[RECEIVE] No data received from peer")
return data
async def send(self, data: bytes) -> None:
"""
Asynchronously sends data through the socket.
Parameters
----------
data
The data to be sent.
"""
self.writer.write(data)
await self.writer.drain()
class BaseSecureAsynSock(BaseAsyncSock, CryptoUtils):
"""
Abstract class representing the base for an asynchronous socket with cryptographic utilities.
Gathers common cryptographic functionality found in both the server and the client and provides an outline
on how they should operate.
Parameters
----------
host
The hostname or IP address of the socket server.
port
The port number of the socket server.
logger
Logger instance for logging socket events.
Attributes:
-----------
_public_key
Elliptic curve public key
_private_key
Elliptic curve private key
_public_fingerprint
Fingerprint object of the elliptic curve public key
"""
def __init__(self, host: str, port: str, logger: logging.Logger) -> None:
super().__init__(host, port, logger)
CryptoUtils.__init__(self)
self._public_key: Optional[ec.EllipticCurvePublicKey] = None
self._private_key: Optional[ec.EllipticCurvePrivateKey] = None
self._public_fingerprint: Optional[fingerprint.Fingerprint] = None
@property
def public_fingerprint(self) -> Optional[fingerprint.Fingerprint]:
if self._public_key and not self._public_fingerprint:
self._public_fingerprint = fingerprint.Fingerprint(hashes.SHA256())
self._public_fingerprint.key = self._public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
return self._public_fingerprint
return None
@abstractmethod
async def _exchange_iv(self) -> None: ...
"""Abstract method which handles the exchange of the initialization vector between parties"""
def generate_key_pair(self) -> None:
"""Generate an elliptic curve public/private key pair"""
self._private_key = ec.generate_private_key(ec.SECP256R1())
self._public_key = self._private_key.public_key()
self.logger.debug("[*] Generated key pair & serialized public key")
async def establish_secure_channel(self) -> None:
"""Handles the establishment of a secure communication channel."""
self.generate_key_pair()
self.logger.info(
f"[*] Your public key's fingerprint: {self.public_fingerprint.get_bubble_babble()}"
)
await self.get_derived_key()
await self.handle_key_verification()
await self._exchange_iv()
self.initialize_cipher(self._key, self._iv)
self.logger.info("[ESTABLISHED SECURE COMMUNICATION CHANNEL]")
async def handle_key_exchange(self) -> bytes:
"""
Handles the public-key exchange between parties and calculates a shared key
Returns
-------
bytes
The shared key which occured from the key exchange
"""
serialized_public_key = self._public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
_, peer_public_key = await asyncio.gather(
self.send(serialized_public_key),
self.receive(1024),
)
self.logger.info("[KEY EXCHANGE] Exchanged public keys")
self.perform_fingerprint_verification(peer_public_key)
peer_public_key = serialization.load_pem_public_key(peer_public_key)
shared_key = self._private_key.exchange(ec.ECDH(), peer_public_key)
self.logger.info("[KEY EXCHANGE] Shared secret generated")
return shared_key
def perform_fingerprint_verification(self, public_key: bytes) -> None:
"""
Calculates a fingerprint of the public key and shows it to the user for verification
Parameters
----------
public_key : bytes
The public key to verify
"""
fingerprint_ = fingerprint.Fingerprint(hashes.SHA256())
fingerprint_.key = public_key
fingerprint_.verify_fingerprint()
self.logger.info("[FINGERPRINT] Public key fingerprint verified")
async def handle_key_verification(self) -> None:
"""Wrapper of self.verify_key_exchange"""
self.logger.debug("[*] Waiting for other party to verify the hashed key")
if await self.verify_key_exchange():
self.logger.info("[ESTABLISHED SHARED KEY]")
else:
logging.critical(
"[!!CRITICAL!!] AN ADVERSARY IS LIKELY TRYING TO HIJACK YOUR COMMUNICATIONS.\n> PLEASE INVESTIGATE *IMMEDIATELY* <"
)
exit(1)
async def verify_key_exchange(self) -> bool:
"""
Exchanges a hash of the encryption key with the server and compares them for equality
Returns
-------
bool
Whether the local key digest matches the one received from the peer
"""
key_digest = self.calculate_hash(self._key, hashes.SHA256())
_, peer_key_digest = await asyncio.gather(
self.send(key_digest), self.receive(32) # SHA256
)
return key_digest == peer_key_digest
async def get_derived_key(self) -> None:
"""Generates a shared key and derive a 256-bit key used for AES encryption"""
shared_key = await self.handle_key_exchange()
self._key = HKDF(
algorithm=hashes.SHA256(),
length=self._aes_key_size // 8,
info=None,
salt=None,
).derive(shared_key)
async def communication_loop(self) -> None:
"""
By the time this function is called, both parties have established a common AES key.
This function implements the actual messaging between parties.
"""
def get_user_confirmation(prompt: str) -> bool:
"""Prompt the user with a yes/no question using the given prompt
Parameters
----------
prompt : str
Prompt to present the user
Returns
-------
bool
Returns True if users answers "y", otherwise returns False
"""
while True:
response = input(prompt).lower().strip()
if response in {"y", "n"}:
return response == "y"
else:
print("[>w<] Invalid input. Enter 'y' or 'n'.")