From 5808a6de20ab47e95b3f38520991c8fa3f82ccb1 Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Sat, 4 Mar 2023 20:50:56 +0100 Subject: [PATCH] feat(core): re-pairing request/dialog --- common/protob/messages-ble.proto | 11 +++ common/protob/messages.proto | 1 + core/embed/ble_firmware/main.c | 87 +++++++++++-------- core/src/all_modules.py | 2 + .../src/apps/management/ble/repair_request.py | 19 ++++ core/src/apps/workflow_handlers.py | 2 + core/src/trezor/enums/MessageType.py | 1 + core/src/trezor/enums/__init__.py | 1 + core/src/trezor/messages.py | 6 ++ core/src/trezor/ui/layouts/common.py | 13 +-- core/src/trezor/ui/layouts/tt_v2/__init__.py | 2 +- python/src/trezorlib/messages.py | 5 ++ 12 files changed, 107 insertions(+), 43 deletions(-) create mode 100644 core/src/apps/management/ble/repair_request.py diff --git a/common/protob/messages-ble.proto b/common/protob/messages-ble.proto index 756d1589f3..2e1bd01fc7 100644 --- a/common/protob/messages-ble.proto +++ b/common/protob/messages-ble.proto @@ -57,3 +57,14 @@ message PairingRequest { message AuthKey { required bytes key = 1; } + + +/** + * Request: initiates repairing request + * @start + * @next Success + * @next Failure + */ +message RepairRequest { +} + diff --git a/common/protob/messages.proto b/common/protob/messages.proto index 3db210d11d..2ba6847cbc 100644 --- a/common/protob/messages.proto +++ b/common/protob/messages.proto @@ -142,6 +142,7 @@ enum MessageType { MessageType_UploadBLEFirmwareChunk = 8002 [(bitcoin_only) = true, (wire_in) = true]; MessageType_PairingRequest = 8003 [(bitcoin_only) = true, (wire_in) = true]; MessageType_AuthKey = 8004 [(bitcoin_only) = true, (wire_out) = true]; + MessageType_RepairRequest = 8005 [(bitcoin_only) = true, (wire_in) = true]; // Bitcoin MessageType_GetPublicKey = 11 [(bitcoin_only) = true, (wire_in) = true]; diff --git a/core/embed/ble_firmware/main.c b/core/embed/ble_firmware/main.c index dfb5052fec..bc02c53f09 100644 --- a/core/embed/ble_firmware/main.c +++ b/core/embed/ble_firmware/main.c @@ -451,6 +451,20 @@ static void on_adv_evt(ble_adv_evt_t ble_adv_evt) { } } +static void int_comm_send(uint8_t *tx_data, uint16_t len) { + uint32_t err_code; + + for (uint32_t i = 0; i < len; i++) { + do { + err_code = app_uart_put(tx_data[i]); + if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY)) { + NRF_LOG_ERROR("Failed sending data to STM. Error 0x%x. ", err_code); + APP_ERROR_CHECK(err_code); + } + } while (err_code == NRF_ERROR_BUSY); + } +} + /**@brief Function for handling BLE events. * * @param[in] p_ble_evt Bluetooth stack event. @@ -497,21 +511,19 @@ static void ble_evt_handler(ble_evt_t const *p_ble_evt, void *p_context) { 0x55, // EOM }; - for (uint32_t i = 0; i < sizeof(tx_data); i++) { - do { - err_code = app_uart_put(tx_data[i]); - if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY)) { - NRF_LOG_ERROR("Failed receiving NUS message. Error 0x%x. ", - err_code); - APP_ERROR_CHECK(err_code); - } - } while (err_code == NRF_ERROR_BUSY); - } + int_comm_send(tx_data, sizeof(tx_data)); uint8_t p_key[6] = {0}; while (!m_uart_rx_data_ready_internal) ; + + uint16_t message_type = (m_uart_rx_data[3] << 8) | m_uart_rx_data[4]; + + if (message_type != 8004) { + break; + } + for (int i = 0; i < 6; i++) { p_key[i] = m_uart_rx_data[i + 11]; } @@ -756,33 +768,6 @@ static void uart_init(void) { } /**@snippet [UART Initialization] */ -/**@brief Function for initializing the Advertising functionality. - */ -// static void advertising_init(void) { -// uint32_t err_code; -// ble_advertising_init_t init; -// -// memset(&init, 0, sizeof(init)); -// -// init.advdata.name_type = BLE_ADVDATA_FULL_NAME; -// init.advdata.include_appearance = false; -// init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE; -// -// init.srdata.uuids_complete.uuid_cnt = -// sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]); -// init.srdata.uuids_complete.p_uuids = m_adv_uuids; -// -// init.config.ble_adv_fast_enabled = true; -// init.config.ble_adv_fast_interval = APP_ADV_INTERVAL; -// init.config.ble_adv_fast_timeout = APP_ADV_DURATION; -// init.evt_handler = on_adv_evt; -// -// err_code = ble_advertising_init(&m_advertising, &init); -// APP_ERROR_CHECK(err_code); -// -// ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG); -//} - static void advertising_init(void) { uint32_t err_code; uint8_t adv_flags; @@ -953,7 +938,35 @@ static void pm_evt_handler(pm_evt_t const *p_evt) { whitelist_set(PM_PEER_ID_LIST_SKIP_NO_ID_ADDR); } break; + case PM_EVT_CONN_SEC_CONFIG_REQ: { + uint8_t tx_data[] = { + 0xA0, // internal message + 0x00, // length - HI + 0x0D, // length - LO + 0x3F, 0x23, 0x23, 0x1F, 0x45, 0x00, 0x00, 0x00, 0x00, + 0x55, // EOM + }; + int_comm_send(tx_data, sizeof(tx_data)); + + while (!m_uart_rx_data_ready_internal) + ; + + uint16_t message_type = (m_uart_rx_data[3] << 8) | m_uart_rx_data[4]; + + m_uart_rx_data_ready_internal = false; + + if (message_type == 2) { + // Allow or reject pairing request from an already bonded peer. + pm_conn_sec_config_t conn_sec_config = {.allow_repairing = true}; + pm_conn_sec_config_reply(p_evt->conn_handle, &conn_sec_config); + } else { + // Allow or reject pairing request from an already bonded peer. + pm_conn_sec_config_t conn_sec_config = {.allow_repairing = false}; + pm_conn_sec_config_reply(p_evt->conn_handle, &conn_sec_config); + } + + } break; default: break; } diff --git a/core/src/all_modules.py b/core/src/all_modules.py index 6433b03afb..0dbff0d773 100644 --- a/core/src/all_modules.py +++ b/core/src/all_modules.py @@ -305,6 +305,8 @@ apps.management.backup_types import apps.management.backup_types apps.management.ble.pairing_request import apps.management.ble.pairing_request +apps.management.ble.repair_request +import apps.management.ble.repair_request apps.management.ble.upload_ble_firmware_init import apps.management.ble.upload_ble_firmware_init apps.management.change_pin diff --git a/core/src/apps/management/ble/repair_request.py b/core/src/apps/management/ble/repair_request.py new file mode 100644 index 0000000000..208b3a9b7d --- /dev/null +++ b/core/src/apps/management/ble/repair_request.py @@ -0,0 +1,19 @@ +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from trezor.wire import GenericContext + from trezor.messages import ( + Success, + RepairRequest, + ) + + +async def repair_request(ctx: GenericContext, _msg: RepairRequest) -> Success: + from trezor.messages import ( + Success, + ) + from trezor.ui.layouts import confirm_action + + await confirm_action(ctx, None, "RE-PAIR DEVICE") + + return Success() diff --git a/core/src/apps/workflow_handlers.py b/core/src/apps/workflow_handlers.py index 5193154c17..c3cb301e9a 100644 --- a/core/src/apps/workflow_handlers.py +++ b/core/src/apps/workflow_handlers.py @@ -60,6 +60,8 @@ def _find_message_handler_module(msg_type: int) -> str: 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" # bitcoin if msg_type == MessageType.AuthorizeCoinJoin: diff --git a/core/src/trezor/enums/MessageType.py b/core/src/trezor/enums/MessageType.py index f83aaf7cc6..dd5c179111 100644 --- a/core/src/trezor/enums/MessageType.py +++ b/core/src/trezor/enums/MessageType.py @@ -55,6 +55,7 @@ UploadBLEFirmwareNextChunk = 8001 UploadBLEFirmwareChunk = 8002 PairingRequest = 8003 AuthKey = 8004 +RepairRequest = 8005 GetPublicKey = 11 PublicKey = 12 SignTx = 15 diff --git a/core/src/trezor/enums/__init__.py b/core/src/trezor/enums/__init__.py index 398b0e17e5..2371f66c3f 100644 --- a/core/src/trezor/enums/__init__.py +++ b/core/src/trezor/enums/__init__.py @@ -77,6 +77,7 @@ if TYPE_CHECKING: UploadBLEFirmwareChunk = 8002 PairingRequest = 8003 AuthKey = 8004 + RepairRequest = 8005 GetPublicKey = 11 PublicKey = 12 SignTx = 15 diff --git a/core/src/trezor/messages.py b/core/src/trezor/messages.py index 26114980c4..a304905c19 100644 --- a/core/src/trezor/messages.py +++ b/core/src/trezor/messages.py @@ -1241,6 +1241,12 @@ if TYPE_CHECKING: def is_type_of(cls, msg: Any) -> TypeGuard["AuthKey"]: return isinstance(msg, cls) + class RepairRequest(protobuf.MessageType): + + @classmethod + def is_type_of(cls, msg: Any) -> TypeGuard["RepairRequest"]: + return isinstance(msg, cls) + class CardanoBlockchainPointerType(protobuf.MessageType): block_index: "int" tx_index: "int" diff --git a/core/src/trezor/ui/layouts/common.py b/core/src/trezor/ui/layouts/common.py index 478134ea78..31be209079 100644 --- a/core/src/trezor/ui/layouts/common.py +++ b/core/src/trezor/ui/layouts/common.py @@ -34,12 +34,15 @@ async def button_request( async def interact( ctx: wire.GenericContext, layout: LayoutType, - br_type: str, + br_type: str | None, br_code: ButtonRequestType = ButtonRequestType.Other, ) -> Any: - if hasattr(layout, "page_count") and layout.page_count() > 1: # type: ignore [Cannot access member "page_count" for type "LayoutType"] - await button_request(ctx, br_type, br_code, pages=layout.page_count()) # type: ignore [Cannot access member "page_count" for type "LayoutType"] - return await ctx.wait(layout) + if br_type is not None: + if hasattr(layout, "page_count") and layout.page_count() > 1: # type: ignore [Cannot access member "page_count" for type "LayoutType"] + await button_request(ctx, br_type, br_code, pages=layout.page_count()) # type: ignore [Cannot access member "page_count" for type "LayoutType"] + return await ctx.wait(layout) + else: + await button_request(ctx, br_type, br_code) + return await ctx.wait(layout) else: - await button_request(ctx, br_type, br_code) return await ctx.wait(layout) diff --git a/core/src/trezor/ui/layouts/tt_v2/__init__.py b/core/src/trezor/ui/layouts/tt_v2/__init__.py index 2b30d73656..90e37625a3 100644 --- a/core/src/trezor/ui/layouts/tt_v2/__init__.py +++ b/core/src/trezor/ui/layouts/tt_v2/__init__.py @@ -207,7 +207,7 @@ async def raise_if_not_confirmed(a: Awaitable[T], exc: Any = ActionCancelled) -> async def confirm_action( ctx: GenericContext, - br_type: str, + br_type: str | None, title: str, action: str | None = None, description: str | None = None, diff --git a/python/src/trezorlib/messages.py b/python/src/trezorlib/messages.py index c0b1b4055f..f992ebeee5 100644 --- a/python/src/trezorlib/messages.py +++ b/python/src/trezorlib/messages.py @@ -85,6 +85,7 @@ class MessageType(IntEnum): UploadBLEFirmwareChunk = 8002 PairingRequest = 8003 AuthKey = 8004 + RepairRequest = 8005 GetPublicKey = 11 PublicKey = 12 SignTx = 15 @@ -2109,6 +2110,10 @@ class AuthKey(protobuf.MessageType): self.key = key +class RepairRequest(protobuf.MessageType): + MESSAGE_WIRE_TYPE = 8005 + + class FirmwareErase(protobuf.MessageType): MESSAGE_WIRE_TYPE = 6 FIELDS = {