1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-04-29 13:39:05 +00:00

chore(core): improve fallback

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

View File

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

View File

@ -214,7 +214,12 @@ class Channel:
self.fallback_decrypt = False self.fallback_decrypt = False
self.expected_payload_length = 0 self.expected_payload_length = 0
self.bytes_read = 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 return
to_read_len = min(len(packet) - INIT_HEADER_LENGTH, payload_length) to_read_len = min(len(packet) - INIT_HEADER_LENGTH, payload_length)

View File

@ -35,6 +35,7 @@ from . import (
ThpError, ThpError,
ThpErrorType, ThpErrorType,
ThpInvalidDataError, ThpInvalidDataError,
ThpUnallocatedChannelError,
ThpUnallocatedSessionError, ThpUnallocatedSessionError,
) )
from . import alternating_bit_protocol as ABP from . import alternating_bit_protocol as ABP
@ -129,6 +130,9 @@ async def handle_received_message(
except ThpUnallocatedSessionError as e: except ThpUnallocatedSessionError as e:
error_message = Failure(code=FailureType.ThpUnallocatedSession) error_message = Failure(code=FailureType.ThpUnallocatedSession)
await ctx.write(error_message, e.session_id) await ctx.write(error_message, e.session_id)
except ThpUnallocatedChannelError:
await ctx.write_error(ThpErrorType.UNALLOCATED_CHANNEL)
ctx.clear()
except ThpDecryptionError: except ThpDecryptionError:
await ctx.write_error(ThpErrorType.DECRYPTION_FAILED) await ctx.write_error(ThpErrorType.DECRYPTION_FAILED)
ctx.clear() 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") log.debug(__name__, "handle_state_TH2")
if not control_byte.is_handshake_comp_req(ctrl_byte): if not control_byte.is_handshake_comp_req(ctrl_byte):
raise ThpError("Message received is not a handshake completion request!") raise ThpError("Message received is not a handshake completion request!")
if ctx.handshake is None: 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(): if not config.is_unlocked():
raise ThpDeviceLockedError raise ThpDeviceLockedError