|
17 | 17 | from hivemind_bus_client.message import HiveMessage, HiveMessageType |
18 | 18 | from hivemind_bus_client.serialization import HiveMindBinaryPayloadType |
19 | 19 | from hivemind_bus_client.serialization import get_bitstring, decode_bitstring |
20 | | -from hivemind_bus_client.util import serialize_message, \ |
21 | | - encrypt_as_json, decrypt_from_json, encrypt_bin, decrypt_bin |
| 20 | +from hivemind_bus_client.util import serialize_message |
| 21 | +from hivemind_bus_client.encryption import (encrypt_as_json, decrypt_from_json, encrypt_bin, decrypt_bin, |
| 22 | + SupportedEncodings, SupportedCiphers) |
22 | 23 | from poorman_handshake.asymmetric.utils import encrypt_RSA, load_RSA_key, sign_RSA |
23 | 24 |
|
24 | 25 |
|
@@ -104,6 +105,8 @@ def __init__(self, key: Optional[str] = None, |
104 | 105 | internal_bus: Optional[OVOSBusClient] = None, |
105 | 106 | bin_callbacks: BinaryDataCallbacks = BinaryDataCallbacks()): |
106 | 107 | self.bin_callbacks = bin_callbacks |
| 108 | + self.json_encoding = SupportedEncodings.JSON_HEX # server defaults before it was made configurable |
| 109 | + self.cipher = SupportedCiphers.AES_GCM # server defaults before it was made configurable |
107 | 110 |
|
108 | 111 | self.identity = identity or None |
109 | 112 | self._password = password |
@@ -271,11 +274,12 @@ def on_message(self, *args): |
271 | 274 | if self.crypto_key: |
272 | 275 | # handle binary encryption |
273 | 276 | if isinstance(message, bytes): |
274 | | - message = decrypt_bin(self.crypto_key, message) |
| 277 | + message = decrypt_bin(self.crypto_key, message, cipher=self.cipher) |
275 | 278 | # handle json encryption |
276 | 279 | elif "ciphertext" in message: |
277 | 280 | # LOG.debug(f"got encrypted message: {len(message)}") |
278 | | - message = decrypt_from_json(self.crypto_key, message) |
| 281 | + message = decrypt_from_json(self.crypto_key, message, |
| 282 | + cipher=self.cipher, encoding=self.json_encoding) |
279 | 283 | else: |
280 | 284 | LOG.debug("Message was unencrypted") |
281 | 285 |
|
@@ -367,14 +371,15 @@ def emit(self, message: Union[MycroftMessage, HiveMessage], |
367 | 371 | binary_type=binary_type, |
368 | 372 | hivemeta=message.metadata) |
369 | 373 | if self.crypto_key: |
370 | | - ws_payload = encrypt_bin(self.crypto_key, bitstr.bytes) |
| 374 | + ws_payload = encrypt_bin(self.crypto_key, bitstr.bytes, cipher=self.cipher) |
371 | 375 | else: |
372 | 376 | ws_payload = bitstr.bytes |
373 | 377 | self.client.send(ws_payload, ABNF.OPCODE_BINARY) |
374 | 378 | else: |
375 | 379 | ws_payload = serialize_message(message) |
376 | 380 | if self.crypto_key: |
377 | | - ws_payload = encrypt_as_json(self.crypto_key, ws_payload) |
| 381 | + ws_payload = encrypt_as_json(self.crypto_key, ws_payload, |
| 382 | + cipher=self.cipher, encoding=self.json_encoding) |
378 | 383 | self.client.send(ws_payload) |
379 | 384 |
|
380 | 385 | except WebSocketConnectionClosedException: |
|
0 commit comments