feat(core): add SilentError and suppres message response on Cancel from Host in pairing phase when waiting for user interaction

[no changelog]
M1nd3r/thp1
M1nd3r 2 months ago
parent 92b1a59b07
commit a465cfeaa8

@ -5,6 +5,7 @@ from trezor import loop, protobuf
from trezor.crypto.hashlib import sha256
from trezor.enums import MessageType, ThpPairingMethod
from trezor.messages import (
Cancel,
ThpCodeEntryChallenge,
ThpCodeEntryCommitment,
ThpCodeEntryCpaceHost,
@ -23,7 +24,7 @@ from trezor.messages import (
ThpQrCodeTag,
ThpStartPairingRequest,
)
from trezor.wire.errors import ActionCancelled, UnexpectedMessage
from trezor.wire.errors import ActionCancelled, SilentError, UnexpectedMessage
from trezor.wire.thp import ChannelState, ThpError, crypto
from trezor.wire.thp.pairing_context import PairingContext
@ -97,7 +98,9 @@ async def handle_pairing_request(
await _prepare_pairing(ctx)
await ctx.write(ThpPairingPreparationsFinished())
ctx.channel_ctx.set_channel_state(ChannelState.TP3)
response = await show_display_data(ctx, _get_possible_pairing_methods(ctx))
response = await show_display_data(
ctx, _get_possible_pairing_methods_and_cancel(ctx)
)
if __debug__:
from trezor.messages import DebugLinkGetState
@ -107,8 +110,12 @@ async def handle_pairing_request(
dl_state = await dispatch_DebugLinkGetState(response)
assert dl_state is not None
await ctx.write(dl_state)
response = await show_display_data(ctx, _get_possible_pairing_methods(ctx))
response = await show_display_data(
ctx, _get_possible_pairing_methods_and_cancel(ctx)
)
if Cancel.is_type_of(response):
ctx.channel_ctx.clear()
raise SilentError("Action was cancelled by the Host")
# TODO disable NFC (if enabled)
response = await _handle_different_pairing_methods(ctx, response)
@ -369,6 +376,12 @@ def _is_method_included(ctx: PairingContext, method: ThpPairingMethod) -> bool:
# Helpers - getters
def _get_possible_pairing_methods_and_cancel(ctx: PairingContext) -> Tuple[int, ...]:
r = _get_possible_pairing_methods(ctx)
mtype = Cancel.MESSAGE_WIRE_TYPE
return r + ((mtype,) if mtype is not None else ())
def _get_possible_pairing_methods(ctx: PairingContext) -> Tuple[int, ...]:
r = tuple(
_get_message_type_for_method(method)

@ -8,6 +8,12 @@ class Error(Exception):
self.message = message
class SilentError(Exception):
def __init__(self, message: str) -> None:
super().__init__()
self.message = message
class UnexpectedMessage(Error):
def __init__(self, message: str) -> None:
super().__init__(FailureType.UnexpectedMessage, message)

@ -7,7 +7,7 @@ from trezor.crypto import random
from trezor.ui.layouts.tt import RustLayout
from trezor.wire import context, message_handler, protocol_common
from trezor.wire.context import UnexpectedMessageException
from trezor.wire.errors import ActionCancelled
from trezor.wire.errors import ActionCancelled, SilentError
from trezor.wire.protocol_common import Context, Message
if TYPE_CHECKING:
@ -233,7 +233,9 @@ async def handle_pairing_request_message(
# We might handle only the few common cases here, like
# Initialize and Cancel.
return exc.msg
except SilentError as exc:
if __debug__:
log.error(__name__, "SilentError: %s", exc.message)
except BaseException as exc:
# Either:
# - the message had a type that has a registered handler, but does not have

Loading…
Cancel
Save