1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-06-02 06:08:46 +00:00

fixup! chore(core): adapt trezorlib transports to session based [no changelog]

This commit is contained in:
Martin Milata 2025-03-14 21:55:28 +01:00 committed by M1nd3r
parent 35f8a36e66
commit f48f9aabc4

View File

@ -5,7 +5,6 @@ import os
import typing as t import typing as t
from binascii import hexlify from binascii import hexlify
import click
from noise.connection import Keypair, NoiseConnection from noise.connection import Keypair, NoiseConnection
from ... import exceptions, messages, protobuf from ... import exceptions, messages, protobuf
@ -175,12 +174,7 @@ class ProtocolV2Channel(Channel):
raise exceptions.DeviceLockedException() raise exceptions.DeviceLockedException()
if not header.is_handshake_init_response(): if not header.is_handshake_init_response():
LOG.debug("Received message is not a valid handshake init response message") LOG.error("Received message is not a valid handshake init response message")
click.echo(
"Received message is not a valid handshake init response message",
err=True,
)
self._noise.read_message(payload) self._noise.read_message(payload)
return payload return payload
@ -217,10 +211,7 @@ class ProtocolV2Channel(Channel):
# Read handshake completion response, ignore payload as we do not care about the state # Read handshake completion response, ignore payload as we do not care about the state
header, data = self._read_until_valid_crc_check() header, data = self._read_until_valid_crc_check()
if not header.is_handshake_comp_response(): if not header.is_handshake_comp_response():
click.echo( LOG.error("Received message is not a valid handshake completion response")
"Received message is not a valid handshake completion response",
err=True,
)
trezor_state = self._noise.decrypt(bytes(data)) trezor_state = self._noise.decrypt(bytes(data))
# TODO handle trezor_state # TODO handle trezor_state
print("trezor state:", trezor_state) print("trezor state:", trezor_state)
@ -249,7 +240,7 @@ class ProtocolV2Channel(Channel):
def _read_ack(self): def _read_ack(self):
header, payload = self._read_until_valid_crc_check() header, payload = self._read_until_valid_crc_check()
if not header.is_ack() or len(payload) > 0: if not header.is_ack() or len(payload) > 0:
click.echo("Received message is not a valid ACK", err=True) LOG.error("Received message is not a valid ACK")
def _send_ack_0(self): def _send_ack_0(self):
LOG.debug("sending ack 0") LOG.debug("sending ack 0")
@ -297,11 +288,10 @@ class ProtocolV2Channel(Channel):
err = _get_error_from_int(raw_payload[0]) err = _get_error_from_int(raw_payload[0])
raise Exception("Received ThpError: " + err) raise Exception("Received ThpError: " + err)
if not header.is_encrypted_transport(): if not header.is_encrypted_transport():
click.echo( LOG.error(
"Trying to decrypt not encrypted message! (" "Trying to decrypt not encrypted message! ("
+ hexlify(header.to_bytes_init() + raw_payload).decode() + hexlify(header.to_bytes_init() + raw_payload).decode()
+ ")", + ")"
err=True,
) )
if not control_byte.is_ack(header.ctrl_byte): if not control_byte.is_ack(header.ctrl_byte):
@ -334,10 +324,9 @@ class ProtocolV2Channel(Channel):
while not is_valid: while not is_valid:
is_valid = checksum.is_valid(chksum, header.to_bytes_init() + payload) is_valid = checksum.is_valid(chksum, header.to_bytes_init() + payload)
if not is_valid: if not is_valid:
click.echo( LOG.error(
"Received a message with an invalid checksum:" "Received a message with an invalid checksum:"
+ hexlify(header.to_bytes_init() + payload + chksum).decode(), + hexlify(header.to_bytes_init() + payload + chksum).decode()
err=True,
) )
header, payload, chksum = thp_io.read(self.transport) header, payload, chksum = thp_io.read(self.transport)
@ -347,17 +336,13 @@ class ProtocolV2Channel(Channel):
self, header: MessageHeader, payload: bytes, original_nonce: bytes self, header: MessageHeader, payload: bytes, original_nonce: bytes
) -> bool: ) -> bool:
if not header.is_channel_allocation_response(): if not header.is_channel_allocation_response():
click.echo( LOG.error("Received message is not a channel allocation response")
"Received message is not a channel allocation response", err=True
)
return False return False
if len(payload) < 10: if len(payload) < 10:
click.echo("Invalid channel allocation response payload", err=True) LOG.error("Invalid channel allocation response payload")
return False return False
if payload[:8] != original_nonce: if payload[:8] != original_nonce:
click.echo( LOG.error("Invalid channel allocation response payload (nonce mismatch)")
"Invalid channel allocation response payload (nonce mismatch)", err=True
)
return False return False
return True return True