Implement HMAC signatures of fsm_update broadcast messages

This commit is contained in:
Kristóf Tóth 2018-07-16 14:31:52 +02:00
parent c658894c12
commit d5feba7076
2 changed files with 10 additions and 3 deletions

View File

@ -2,6 +2,7 @@
# All Rights Reserved. See LICENSE file for details.
from tfw import EventHandlerBase
from tfw.crypto import KeyManager, sign_message
from tfw.config.logs import logging
LOG = logging.getLogger(__name__)
@ -12,6 +13,7 @@ class FSMManagingEventHandler(EventHandlerBase):
super().__init__(key)
self.fsm = fsm_type()
self._fsm_updater = FSMUpdater(self.fsm)
self.auth_key = KeyManager().auth_key
self.command_handlers = {
'trigger': self.handle_trigger,
@ -22,7 +24,10 @@ class FSMManagingEventHandler(EventHandlerBase):
try:
data = message['data']
message['data'] = self.command_handlers[data['command']](data)
self.server_connector.broadcast(self._fsm_updater.generate_fsm_update())
fsm_update_message = self._fsm_updater.generate_fsm_update()
sign_message(self.auth_key, message)
sign_message(self.auth_key, fsm_update_message)
self.server_connector.broadcast(fsm_update_message)
return message
except KeyError:
LOG.error('IGNORING MESSAGE: Invalid message received: %s', message)

View File

@ -4,7 +4,7 @@
from abc import ABC, abstractmethod
from tfw.networking.event_handlers import ServerConnector
from tfw.crypto import message_checksum
from tfw.crypto import message_checksum, KeyManager, verify_message
from tfw.config.logs import logging
LOG = logging.getLogger(__name__)
@ -116,10 +116,12 @@ class FSMAwareEventHandler(EventHandlerBase, ABC):
super().__init__(key)
self.subscribe('fsm_update')
self.fsm_state = None
self._auth_key = KeyManager().auth_key
def dispatch_handling(self, message):
if message['key'] == 'fsm_update':
self._handle_fsm_update(message)
if verify_message(self._auth_key, message):
self._handle_fsm_update(message)
return None
return super().dispatch_handling(message)