1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-04-10 04:16:16 +00:00

chore(core): improve fallback

This commit is contained in:
M1nd3r 2025-03-20 15:36:13 +01:00
parent 2e72d083a2
commit 2d54ced810
3 changed files with 18 additions and 2 deletions

View File

@ -48,6 +48,10 @@ class ThpDeviceLockedError(ThpError):
pass
class ThpUnallocatedChannelError(ThpError):
pass
class ThpUnallocatedSessionError(ThpError):
def __init__(self, session_id: int) -> None:

View File

@ -215,7 +215,12 @@ class Channel:
self.fallback_decrypt = False
self.expected_payload_length = 0
self.bytes_read = 0
print("FAILED TO FALLBACK")
if __debug__ and utils.ALLOW_DEBUG_MESSAGES:
from ubinascii import hexlify
log.debug(
__name__, "FAILED TO FALLBACK: %s", hexlify(packet).decode()
)
return
to_read_len = min(len(packet) - INIT_HEADER_LENGTH, payload_length)

View File

@ -35,6 +35,7 @@ from . import (
ThpError,
ThpErrorType,
ThpInvalidDataError,
ThpUnallocatedChannelError,
ThpUnallocatedSessionError,
)
from . import alternating_bit_protocol as ABP
@ -129,6 +130,9 @@ async def handle_received_message(
except ThpUnallocatedSessionError as e:
error_message = Failure(code=FailureType.ThpUnallocatedSession)
await ctx.write(error_message, e.session_id)
except ThpUnallocatedChannelError:
await ctx.write_error(ThpErrorType.UNALLOCATED_CHANNEL)
ctx.clear()
except ThpDecryptionError:
await ctx.write_error(ThpErrorType.DECRYPTION_FAILED)
ctx.clear()
@ -271,8 +275,11 @@ async def _handle_state_TH2(ctx: Channel, message_length: int, ctrl_byte: int) -
log.debug(__name__, "handle_state_TH2")
if not control_byte.is_handshake_comp_req(ctrl_byte):
raise ThpError("Message received is not a handshake completion request!")
if ctx.handshake is None:
raise Exception("Handshake object is not prepared. Retry handshake.")
raise ThpUnallocatedChannelError(
"Handshake object is not prepared. Create new channel."
)
if not config.is_unlocked():
raise ThpDeviceLockedError