2017-08-24 12:29:27 +00:00
|
|
|
# This file is part of the TREZOR project.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012-2016 Marek Palatinus <slush@satoshilabs.com>
|
|
|
|
# Copyright (C) 2012-2016 Pavol Rusnak <stick@satoshilabs.com>
|
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import struct
|
2017-12-12 15:40:11 +00:00
|
|
|
from io import BytesIO
|
|
|
|
from . import messages as proto
|
2017-08-24 12:29:27 +00:00
|
|
|
from . import mapping
|
2017-12-12 15:40:11 +00:00
|
|
|
from . import protobuf
|
2017-08-24 12:29:27 +00:00
|
|
|
|
|
|
|
REPLEN = 64
|
|
|
|
|
|
|
|
|
|
|
|
class ProtocolV2(object):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.session = None
|
|
|
|
|
|
|
|
def session_begin(self, transport):
|
|
|
|
chunk = struct.pack('>B', 0x03)
|
2017-09-04 09:44:33 +00:00
|
|
|
chunk = chunk.ljust(REPLEN, b'\x00')
|
2017-08-24 12:29:27 +00:00
|
|
|
transport.write_chunk(chunk)
|
|
|
|
resp = transport.read_chunk()
|
|
|
|
self.session = self.parse_session_open(resp)
|
|
|
|
|
|
|
|
def session_end(self, transport):
|
|
|
|
if not self.session:
|
|
|
|
return
|
|
|
|
chunk = struct.pack('>BL', 0x04, self.session)
|
2017-09-04 09:44:33 +00:00
|
|
|
chunk = chunk.ljust(REPLEN, b'\x00')
|
2017-08-24 12:29:27 +00:00
|
|
|
transport.write_chunk(chunk)
|
|
|
|
resp = transport.read_chunk()
|
2017-09-04 09:44:33 +00:00
|
|
|
(magic, ) = struct.unpack('>B', resp[:1])
|
|
|
|
if magic != 0x04:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise RuntimeError('Expected session close')
|
2017-08-24 12:29:27 +00:00
|
|
|
self.session = None
|
|
|
|
|
|
|
|
def write(self, transport, msg):
|
|
|
|
if not self.session:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise RuntimeError('Missing session for v2 protocol')
|
2017-08-24 12:29:27 +00:00
|
|
|
|
|
|
|
# Serialize whole message
|
2017-12-12 15:40:11 +00:00
|
|
|
data = BytesIO()
|
|
|
|
protobuf.dump_message(data, msg)
|
|
|
|
data = data.getvalue()
|
2017-08-24 12:29:27 +00:00
|
|
|
dataheader = struct.pack('>LL', mapping.get_type(msg), len(data))
|
|
|
|
data = dataheader + data
|
|
|
|
seq = -1
|
|
|
|
|
|
|
|
# Write it out
|
|
|
|
while data:
|
|
|
|
if seq < 0:
|
|
|
|
repheader = struct.pack('>BL', 0x01, self.session)
|
|
|
|
else:
|
|
|
|
repheader = struct.pack('>BLL', 0x02, self.session, seq)
|
|
|
|
datalen = REPLEN - len(repheader)
|
|
|
|
chunk = repheader + data[:datalen]
|
2017-09-04 09:44:33 +00:00
|
|
|
chunk = chunk.ljust(REPLEN, b'\x00')
|
2017-08-24 12:29:27 +00:00
|
|
|
transport.write_chunk(chunk)
|
|
|
|
data = data[datalen:]
|
|
|
|
seq += 1
|
|
|
|
|
|
|
|
def read(self, transport):
|
|
|
|
if not self.session:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise RuntimeError('Missing session for v2 protocol')
|
2017-08-24 12:29:27 +00:00
|
|
|
|
|
|
|
# Read header with first part of message data
|
|
|
|
chunk = transport.read_chunk()
|
|
|
|
msg_type, datalen, data = self.parse_first(chunk)
|
|
|
|
|
|
|
|
# Read the rest of the message
|
|
|
|
while len(data) < datalen:
|
|
|
|
chunk = transport.read_chunk()
|
|
|
|
next_data = self.parse_next(chunk)
|
|
|
|
data.extend(next_data)
|
|
|
|
|
|
|
|
# Strip padding
|
2017-12-12 15:40:11 +00:00
|
|
|
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))
|
2017-08-24 12:29:27 +00:00
|
|
|
return msg
|
|
|
|
|
|
|
|
def parse_first(self, chunk):
|
|
|
|
try:
|
|
|
|
headerlen = struct.calcsize('>BLLL')
|
2017-09-04 09:44:33 +00:00
|
|
|
(magic, session, msg_type, datalen) = struct.unpack('>BLLL', chunk[:headerlen])
|
2017-08-24 12:29:27 +00:00
|
|
|
except:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise RuntimeError('Cannot parse header')
|
2017-08-24 12:29:27 +00:00
|
|
|
if magic != 0x01:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise RuntimeError('Unexpected magic character')
|
2017-08-24 12:29:27 +00:00
|
|
|
if session != self.session:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise RuntimeError('Session id mismatch')
|
2017-08-24 12:29:27 +00:00
|
|
|
return msg_type, datalen, chunk[headerlen:]
|
|
|
|
|
|
|
|
def parse_next(self, chunk):
|
|
|
|
try:
|
|
|
|
headerlen = struct.calcsize('>BLL')
|
2017-09-04 09:44:33 +00:00
|
|
|
(magic, session, sequence) = struct.unpack('>BLL', chunk[:headerlen])
|
2017-08-24 12:29:27 +00:00
|
|
|
except:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise RuntimeError('Cannot parse header')
|
2017-08-24 12:29:27 +00:00
|
|
|
if magic != 0x02:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise RuntimeError('Unexpected magic characters')
|
2017-08-24 12:29:27 +00:00
|
|
|
if session != self.session:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise RuntimeError('Session id mismatch')
|
2017-08-24 12:29:27 +00:00
|
|
|
return chunk[headerlen:]
|
|
|
|
|
|
|
|
def parse_session_open(self, chunk):
|
|
|
|
try:
|
|
|
|
headerlen = struct.calcsize('>BL')
|
2017-09-04 09:44:33 +00:00
|
|
|
(magic, session) = struct.unpack('>BL', chunk[:headerlen])
|
2017-08-24 12:29:27 +00:00
|
|
|
except:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise RuntimeError('Cannot parse header')
|
2017-08-24 12:29:27 +00:00
|
|
|
if magic != 0x03:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise RuntimeError('Unexpected magic character')
|
2017-08-24 12:41:31 +00:00
|
|
|
return session
|