Skip to content

Commit d33d690

Browse files
authored
fix: INTERCOM messages RSA (#44)
* fix: INTERCOM messages RSA these were still using the removed pgpy * fix: INTERCOM messages RSA these were still using the removed pgpy
1 parent 5407634 commit d33d690

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

hivemind_bus_client/client.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import pybase64
21
import json
32
import ssl
43
from threading import Event
54
from typing import Union, Optional, Callable
65

7-
import pgpy
6+
import pybase64
7+
from Cryptodome.PublicKey import RSA
88
from ovos_bus_client import Message as MycroftMessage, MessageBusClient as OVOSBusClient
99
from ovos_bus_client.session import Session
10+
from ovos_utils.fakebus import FakeBus
11+
from ovos_utils.log import LOG
1012
from pyee import EventEmitter
1113
from websocket import ABNF
1214
from websocket import WebSocketApp, WebSocketConnectionClosedException
@@ -17,8 +19,7 @@
1719
from hivemind_bus_client.serialization import get_bitstring, decode_bitstring
1820
from hivemind_bus_client.util import serialize_message, \
1921
encrypt_as_json, decrypt_from_json, encrypt_bin, decrypt_bin
20-
from ovos_utils.log import LOG
21-
from ovos_utils.fakebus import FakeBus
22+
from poorman_handshake.asymmetric.utils import encrypt_RSA, load_RSA_key, sign_RSA
2223

2324

2425
class BinaryDataCallbacks:
@@ -492,24 +493,15 @@ def wait_for_payload_response(self, message: Union[MycroftMessage, HiveMessage],
492493
self.emit(message)
493494
return waiter.wait(timeout)
494495

495-
# targeted messages for nodes, assymetric encryption
496+
# targeted messages for nodes, asymmetric encryption
496497
def emit_intercom(self, message: Union[MycroftMessage, HiveMessage],
497-
pubkey: Union[str, pgpy.PGPKey]):
498-
499-
if isinstance(pubkey, str):
500-
pubkey, _ = pgpy.PGPKey.from_blob(pubkey)
501-
assert isinstance(pubkey, pgpy.PGPKey)
502-
503-
txt = message.serialize()
498+
pubkey: Union[str, bytes, RSA.RsaKey]):
504499

505-
text_message = pgpy.PGPMessage.new(txt)
506-
encrypted_message = pubkey.encrypt(text_message)
500+
encrypted_message = encrypt_RSA(pubkey, message.serialize())
507501

508502
# sign message
509-
with open(self.identity.private_key, "r") as f:
510-
private_key = pgpy.PGPKey.from_blob(f.read())
511-
# the bitwise OR operator '|' is used to add a signature to a PGPMessage.
512-
encrypted_message |= private_key.sign(encrypted_message,
513-
intended_recipients=[pubkey])
503+
private_key = load_RSA_key(self.identity.private_key)
504+
signature = sign_RSA(private_key, encrypted_message)
514505

515-
self.emit(HiveMessage(HiveMessageType.INTERCOM, payload={"ciphertext": str(encrypted_message)}))
506+
self.emit(HiveMessage(HiveMessageType.INTERCOM, payload={"ciphertext": pybase64.b64encode(encrypted_message),
507+
"signature": pybase64.b64encode(signature)}))

hivemind_bus_client/protocol.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1+
import pybase64
12
from dataclasses import dataclass
3+
from typing import Optional
24

3-
import pgpy
45
from ovos_bus_client import Message as MycroftMessage
56
from ovos_bus_client import MessageBusClient
67
from ovos_bus_client.message import Message
78
from ovos_bus_client.session import Session, SessionManager
89
from ovos_utils.log import LOG
9-
from poorman_handshake import HandShake, PasswordHandShake
10-
from typing import Optional
1110

1211
from hivemind_bus_client.client import HiveMessageBusClient
1312
from hivemind_bus_client.identity import NodeIdentity
1413
from hivemind_bus_client.message import HiveMessage, HiveMessageType
14+
from poorman_handshake import HandShake, PasswordHandShake
15+
from poorman_handshake.asymmetric.utils import decrypt_RSA, load_RSA_key
1516

1617

1718
@dataclass()
@@ -274,12 +275,17 @@ def handle_intercom(self, message: HiveMessage):
274275
pload = message.payload
275276
if isinstance(pload, dict) and "ciphertext" in pload:
276277
try:
277-
message_from_blob = pgpy.PGPMessage.from_blob(pload["ciphertext"])
278+
ciphertext = pybase64.b64decode(pload["ciphertext"])
279+
signature = pybase64.b64decode(pload["signature"])
280+
281+
# TODO allow verifying, but we need to store known pubkeys before this is possible
282+
# pubkey = ""
283+
# verified: bool = verify_RSA(pubkey, ciphertext, signature)
284+
285+
private_key = load_RSA_key(self.identity.private_key)
278286

279-
with open(self.identity.private_key, "r") as f:
280-
private_key = pgpy.PGPKey.from_blob(f.read())
287+
decrypted = decrypt_RSA(private_key, ciphertext).decode("utf8")
281288

282-
decrypted: str = private_key.decrypt(message_from_blob)
283289
message._payload = HiveMessage.deserialize(decrypted)
284290
except:
285291
if k:

0 commit comments

Comments
 (0)