2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
2017-08-24 12:29:27 +00:00
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# Copyright (C) 2012-2018 SatoshiLabs and contributors
|
2017-08-24 12:29:27 +00:00
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
2018-06-21 14:28:34 +00:00
|
|
|
# it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
# as published by the Free Software Foundation.
|
2017-08-24 12:29:27 +00:00
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# You should have received a copy of the License along with this library.
|
|
|
|
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
2017-08-24 12:29:27 +00:00
|
|
|
|
2018-04-23 10:58:30 +00:00
|
|
|
import logging
|
2017-08-24 12:29:27 +00:00
|
|
|
import struct
|
2018-08-13 16:21:24 +00:00
|
|
|
from io import BytesIO
|
2018-08-10 13:48:39 +00:00
|
|
|
from typing import Tuple
|
2018-04-23 10:58:30 +00:00
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
from . import mapping, protobuf
|
2018-04-23 10:58:30 +00:00
|
|
|
from .transport import Transport
|
2017-08-24 12:29:27 +00:00
|
|
|
|
|
|
|
REPLEN = 64
|
|
|
|
|
2018-04-23 10:58:30 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
|
2018-04-23 10:58:30 +00:00
|
|
|
class ProtocolV1:
|
|
|
|
def session_begin(self, transport: Transport) -> None:
|
2017-08-24 12:29:27 +00:00
|
|
|
pass
|
|
|
|
|
2018-04-23 10:58:30 +00:00
|
|
|
def session_end(self, transport: Transport) -> None:
|
2017-08-24 12:29:27 +00:00
|
|
|
pass
|
|
|
|
|
2018-04-23 10:58:30 +00:00
|
|
|
def write(self, transport: Transport, msg: protobuf.MessageType) -> None:
|
2018-08-13 16:21:24 +00:00
|
|
|
LOG.debug(
|
|
|
|
"sending message: {}".format(msg.__class__.__name__),
|
|
|
|
extra={"protobuf": msg},
|
|
|
|
)
|
2017-12-12 15:40:11 +00:00
|
|
|
data = BytesIO()
|
|
|
|
protobuf.dump_message(data, msg)
|
|
|
|
ser = data.getvalue()
|
2017-08-24 12:29:27 +00:00
|
|
|
header = struct.pack(">HL", mapping.get_type(msg), len(ser))
|
|
|
|
data = bytearray(b"##" + header + ser)
|
|
|
|
|
|
|
|
while data:
|
|
|
|
# Report ID, data padded to 63 bytes
|
2018-08-13 16:21:24 +00:00
|
|
|
chunk = b"?" + data[: REPLEN - 1]
|
|
|
|
chunk = chunk.ljust(REPLEN, b"\x00")
|
2017-08-24 12:29:27 +00:00
|
|
|
transport.write_chunk(chunk)
|
|
|
|
data = data[63:]
|
|
|
|
|
2018-04-23 10:58:30 +00:00
|
|
|
def read(self, transport: Transport) -> protobuf.MessageType:
|
2017-08-24 12:29:27 +00:00
|
|
|
# Read header with first part of message data
|
|
|
|
chunk = transport.read_chunk()
|
2018-04-23 10:58:30 +00:00
|
|
|
msg_type, datalen, data = self.parse_first(chunk)
|
2017-08-24 12:29:27 +00:00
|
|
|
|
|
|
|
# Read the rest of the message
|
|
|
|
while len(data) < datalen:
|
|
|
|
chunk = transport.read_chunk()
|
|
|
|
data.extend(self.parse_next(chunk))
|
|
|
|
|
2017-12-12 15:40:11 +00:00
|
|
|
# Strip padding
|
|
|
|
data = BytesIO(data[:datalen])
|
2017-08-24 12:29:27 +00:00
|
|
|
|
|
|
|
# Parse to protobuf
|
2017-12-12 15:40:11 +00:00
|
|
|
msg = protobuf.load_message(data, mapping.get_class(msg_type))
|
2018-08-13 16:21:24 +00:00
|
|
|
LOG.debug(
|
|
|
|
"received message: {}".format(msg.__class__.__name__),
|
|
|
|
extra={"protobuf": msg},
|
|
|
|
)
|
2017-08-24 12:29:27 +00:00
|
|
|
return msg
|
|
|
|
|
2018-04-23 10:58:30 +00:00
|
|
|
def parse_first(self, chunk: bytes) -> Tuple[int, int, bytes]:
|
2018-08-13 16:21:24 +00:00
|
|
|
if chunk[:3] != b"?##":
|
|
|
|
raise RuntimeError("Unexpected magic characters")
|
2017-08-24 12:29:27 +00:00
|
|
|
try:
|
2018-08-13 16:21:24 +00:00
|
|
|
headerlen = struct.calcsize(">HL")
|
|
|
|
msg_type, datalen = struct.unpack(">HL", chunk[3 : 3 + headerlen])
|
2018-08-10 14:05:14 +00:00
|
|
|
except Exception:
|
2018-08-13 16:21:24 +00:00
|
|
|
raise RuntimeError("Cannot parse header")
|
2017-08-24 12:29:27 +00:00
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
data = chunk[3 + headerlen :]
|
2018-04-23 10:58:30 +00:00
|
|
|
return msg_type, datalen, data
|
2017-08-24 12:29:27 +00:00
|
|
|
|
2018-04-23 10:58:30 +00:00
|
|
|
def parse_next(self, chunk: bytes) -> bytes:
|
2018-08-13 16:21:24 +00:00
|
|
|
if chunk[:1] != b"?":
|
|
|
|
raise RuntimeError("Unexpected magic characters")
|
2017-08-24 12:29:27 +00:00
|
|
|
return chunk[1:]
|