From 4dbabb52e332c50b6e1bb5be2f372c7643eeaa83 Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Wed, 8 Mar 2023 23:13:38 +0100 Subject: [PATCH] feat(core): allow some messages only over specific interfaces --- .../extmod/modtrezorio/modtrezorio-ble.h | 12 +++++++++++ core/src/apps/workflow_handlers.py | 20 +++++++++++-------- core/src/bluetooth.py | 3 +++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/core/embed/extmod/modtrezorio/modtrezorio-ble.h b/core/embed/extmod/modtrezorio/modtrezorio-ble.h index 67765336a6..4f6e1142f5 100644 --- a/core/embed/extmod/modtrezorio/modtrezorio-ble.h +++ b/core/embed/extmod/modtrezorio/modtrezorio-ble.h @@ -79,6 +79,16 @@ STATIC mp_obj_t mod_trezorio_BLE_write(mp_obj_t self, mp_obj_t msg) { STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorio_BLE_write_obj, mod_trezorio_BLE_write); +/// def iface_type(self) -> int: +/// """ +/// Internal / External distinction +/// """ +STATIC mp_obj_t mod_trezorio_BLE_iface_type(mp_obj_t self) { + return MP_OBJ_NEW_SMALL_INT(ble_last_internal ? 1 : 0); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorio_BLE_iface_type_obj, + mod_trezorio_BLE_iface_type); + STATIC const mp_rom_map_elem_t mod_trezorio_BLE_globals_table[] = { {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ble)}, {MP_ROM_QSTR(MP_QSTR_update_init), @@ -86,6 +96,8 @@ STATIC const mp_rom_map_elem_t mod_trezorio_BLE_globals_table[] = { {MP_ROM_QSTR(MP_QSTR_update_chunk), MP_ROM_PTR(&mod_trezorio_BLE_update_chunk_obj)}, {MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mod_trezorio_BLE_write_obj)}, + {MP_ROM_QSTR(MP_QSTR_iface_type), + MP_ROM_PTR(&mod_trezorio_BLE_iface_type_obj)}, }; STATIC MP_DEFINE_CONST_DICT(mod_trezorio_BLE_globals, mod_trezorio_BLE_globals_table); diff --git a/core/src/apps/workflow_handlers.py b/core/src/apps/workflow_handlers.py index c3cb301e9a..381698925a 100644 --- a/core/src/apps/workflow_handlers.py +++ b/core/src/apps/workflow_handlers.py @@ -13,7 +13,7 @@ def register(wire_type: int, handler: Handler[Msg]) -> None: workflow_handlers[wire_type] = handler -def _find_message_handler_module(msg_type: int) -> str: +def _find_message_handler_module(msg_type: int, iface: WireInterface) -> str: """Statically find the appropriate workflow handler. For now, new messages must be registered by hand in the if-elif manner below. @@ -56,12 +56,16 @@ def _find_message_handler_module(msg_type: int) -> str: return "apps.management.sd_protect" # BLE - if msg_type == MessageType.UploadBLEFirmwareInit: - return "apps.management.ble.upload_ble_firmware_init" - if msg_type == MessageType.PairingRequest: - return "apps.management.ble.pairing_request" - if msg_type == MessageType.RepairRequest: - return "apps.management.ble.repair_request" + if iface.iface_num() != 16: + # cannot update over BLE + if msg_type == MessageType.UploadBLEFirmwareInit: + return "apps.management.ble.upload_ble_firmware_init" + + if iface.iface_num() == 16 and iface.iface_type() == 1: + if msg_type == MessageType.PairingRequest: + return "apps.management.ble.pairing_request" + if msg_type == MessageType.RepairRequest: + return "apps.management.ble.repair_request" # bitcoin if msg_type == MessageType.AuthorizeCoinJoin: @@ -200,7 +204,7 @@ def find_registered_handler(iface: WireInterface, msg_type: int) -> Handler | No return workflow_handlers[msg_type] try: - modname = _find_message_handler_module(msg_type) + modname = _find_message_handler_module(msg_type, iface) handler_name = modname[modname.rfind(".") + 1 :] module = __import__(modname, None, None, (handler_name,), 0) return getattr(module, handler_name) diff --git a/core/src/bluetooth.py b/core/src/bluetooth.py index 75d7709a23..77ada089b0 100644 --- a/core/src/bluetooth.py +++ b/core/src/bluetooth.py @@ -8,6 +8,9 @@ class BleInterface: def iface_num(self) -> int: return 16 + def iface_type(self): + return ble.iface_type(self) + def write(self, msg: bytes) -> int: return ble.write(self, msg)