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

View File

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