2012-11-13 14:09:39 +00:00
|
|
|
import struct
|
2016-06-26 22:14:19 +00:00
|
|
|
import mapping
|
2012-11-13 14:09:39 +00:00
|
|
|
|
2013-01-05 14:42:09 +00:00
|
|
|
class NotImplementedException(Exception):
|
|
|
|
pass
|
|
|
|
|
2014-07-09 22:44:46 +00:00
|
|
|
class ConnectionError(Exception):
|
|
|
|
pass
|
|
|
|
|
2012-11-13 14:09:39 +00:00
|
|
|
class Transport(object):
|
|
|
|
def __init__(self, device, *args, **kwargs):
|
|
|
|
self.device = device
|
2016-06-26 19:29:29 +00:00
|
|
|
self.session_id = 0
|
2013-09-09 13:36:17 +00:00
|
|
|
self.session_depth = 0
|
2012-11-13 14:09:39 +00:00
|
|
|
self._open()
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2013-09-09 13:36:17 +00:00
|
|
|
def session_begin(self):
|
2014-08-26 13:54:06 +00:00
|
|
|
"""
|
|
|
|
Apply a lock to the device in order to preform synchronous multistep "conversations" with the device. For example, before entering the transaction signing workflow, one begins a session. After the transaction is complete, the session may be ended.
|
|
|
|
"""
|
2013-09-09 13:36:17 +00:00
|
|
|
if self.session_depth == 0:
|
|
|
|
self._session_begin()
|
|
|
|
self.session_depth += 1
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2013-09-09 13:36:17 +00:00
|
|
|
def session_end(self):
|
2014-08-26 13:54:06 +00:00
|
|
|
"""
|
|
|
|
End a session. Se session_begin for an in depth description of TREZOR sessions.
|
|
|
|
"""
|
2013-09-09 13:36:17 +00:00
|
|
|
self.session_depth -= 1
|
|
|
|
self.session_depth = max(0, self.session_depth)
|
|
|
|
if self.session_depth == 0:
|
|
|
|
self._session_end()
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2012-11-13 14:09:39 +00:00
|
|
|
def close(self):
|
2014-08-26 13:54:06 +00:00
|
|
|
"""
|
|
|
|
Close the connection to the physical device or file descriptor represented by the Transport.
|
|
|
|
"""
|
2012-11-13 14:09:39 +00:00
|
|
|
self._close()
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2012-11-13 14:09:39 +00:00
|
|
|
def write(self, msg):
|
2014-08-26 13:54:06 +00:00
|
|
|
"""
|
|
|
|
Write mesage to tansport. msg should be a member of a valid `protobuf class <https://developers.google.com/protocol-buffers/docs/pythontutorial>`_ with a SerializeToString() method.
|
|
|
|
"""
|
2016-06-26 19:29:29 +00:00
|
|
|
raise NotImplementedException("Not implemented")
|
2012-11-15 20:08:02 +00:00
|
|
|
|
2012-11-13 14:09:39 +00:00
|
|
|
def read(self):
|
2014-08-26 13:54:06 +00:00
|
|
|
"""
|
|
|
|
If there is data available to be read from the transport, reads the data and tries to parse it as a protobuf message. If the parsing succeeds, return a protobuf object.
|
|
|
|
Otherwise, returns None.
|
|
|
|
"""
|
2016-06-26 19:29:29 +00:00
|
|
|
if not self._ready_to_read():
|
2012-12-09 13:53:09 +00:00
|
|
|
return None
|
2012-12-13 18:48:24 +00:00
|
|
|
|
2012-12-09 13:53:09 +00:00
|
|
|
data = self._read()
|
2016-05-26 15:20:44 +00:00
|
|
|
if data is None:
|
2012-12-09 13:53:09 +00:00
|
|
|
return None
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2012-12-13 18:48:24 +00:00
|
|
|
return self._parse_message(data)
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2012-12-13 18:48:24 +00:00
|
|
|
def read_blocking(self):
|
2014-08-26 13:54:06 +00:00
|
|
|
"""
|
2016-06-26 19:29:29 +00:00
|
|
|
Same as read, except blocks until data is available to be read.
|
2014-08-26 13:54:06 +00:00
|
|
|
"""
|
2012-12-13 18:48:24 +00:00
|
|
|
while True:
|
|
|
|
data = self._read()
|
|
|
|
if data != None:
|
|
|
|
break
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2012-12-13 18:48:24 +00:00
|
|
|
return self._parse_message(data)
|
|
|
|
|
|
|
|
def _parse_message(self, data):
|
2016-06-26 19:29:29 +00:00
|
|
|
(session_id, msg_type, data) = data
|
|
|
|
|
|
|
|
# Raise exception if we get the response with
|
|
|
|
# unexpected session ID
|
|
|
|
self._check_session_id(session_id)
|
|
|
|
|
2014-07-26 14:27:28 +00:00
|
|
|
if msg_type == 'protobuf':
|
|
|
|
return data
|
|
|
|
else:
|
|
|
|
inst = mapping.get_class(msg_type)()
|
2016-06-26 19:29:29 +00:00
|
|
|
inst.ParseFromString(bytes(data))
|
2014-07-26 14:27:28 +00:00
|
|
|
return inst
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2016-06-26 19:29:29 +00:00
|
|
|
def _check_session_id(self, session_id):
|
|
|
|
if self.session_id == 0:
|
|
|
|
# Let the device set the session ID
|
|
|
|
self.session_id = session_id
|
|
|
|
elif session_id != self.session_id:
|
|
|
|
# Session ID has been already set, but it differs from response
|
|
|
|
raise Exception("Session ID mismatch. Have %d, got %d" % (self.session_id, session_id))
|
|
|
|
|
|
|
|
# Functions to be implemented in specific transports:
|
|
|
|
def _open(self):
|
|
|
|
raise NotImplementedException("Not implemented")
|
|
|
|
|
|
|
|
def _close(self):
|
|
|
|
raise NotImplementedException("Not implemented")
|
|
|
|
|
|
|
|
def _write_chunk(self, chunk):
|
|
|
|
raise NotImplementedException("Not implemented")
|
|
|
|
|
|
|
|
def _read_chunk(self):
|
|
|
|
raise NotImplementedException("Not implemented")
|
|
|
|
|
|
|
|
def _ready_to_read(self):
|
|
|
|
"""
|
|
|
|
Returns True if there is data to be read from the transport. Otherwise, False.
|
|
|
|
"""
|
|
|
|
raise NotImplementedException("Not implemented")
|
|
|
|
|
|
|
|
def _session_begin(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _session_end(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class TransportV1(Transport):
|
|
|
|
def write(self, msg):
|
|
|
|
ser = msg.SerializeToString()
|
|
|
|
header = struct.pack(">HL", mapping.get_type(msg), len(ser))
|
|
|
|
data = bytearray(b"##" + header + ser)
|
|
|
|
|
|
|
|
while len(data):
|
|
|
|
# Report ID, data padded to 63 bytes
|
|
|
|
chunk = b'?' + data[:63] + b'\0' * (63 - len(data[:63]))
|
|
|
|
self._write_chunk(chunk)
|
|
|
|
data = data[63:]
|
|
|
|
|
|
|
|
def _read(self):
|
|
|
|
chunk = self._read_chunk()
|
|
|
|
(msg_type, datalen, data) = self.parse_first(chunk)
|
|
|
|
|
|
|
|
while len(data) < datalen:
|
|
|
|
chunk = self._read_chunk()
|
|
|
|
data.extend(self.parse_next(chunk))
|
|
|
|
|
|
|
|
# Strip padding zeros
|
|
|
|
data = data[:datalen]
|
|
|
|
return (0, msg_type, data)
|
|
|
|
|
|
|
|
def parse_first(self, chunk):
|
|
|
|
if chunk[:3] != b"?##":
|
|
|
|
raise Exception("Unexpected magic characters")
|
|
|
|
|
|
|
|
try:
|
|
|
|
headerlen = struct.calcsize(">HL")
|
2016-07-19 19:37:54 +00:00
|
|
|
(msg_type, datalen) = struct.unpack(">HL", bytes(chunk[3:3 + headerlen]))
|
2016-06-26 19:29:29 +00:00
|
|
|
except:
|
2016-06-26 22:14:19 +00:00
|
|
|
raise Exception("Cannot parse header")
|
2016-06-26 19:29:29 +00:00
|
|
|
|
|
|
|
data = chunk[3 + headerlen:]
|
|
|
|
return (msg_type, datalen, data)
|
|
|
|
|
|
|
|
def parse_next(self, chunk):
|
|
|
|
if chunk[0:1] != b"?":
|
|
|
|
raise Exception("Unexpected magic characters")
|
|
|
|
|
|
|
|
return chunk[1:]
|
|
|
|
|
|
|
|
class TransportV2(Transport):
|
|
|
|
def write(self, msg):
|
2016-06-26 22:14:19 +00:00
|
|
|
data = bytearray(msg.SerializeToString())
|
|
|
|
|
|
|
|
header1 = struct.pack(">L", self.session_id)
|
|
|
|
header2 = struct.pack(">LL", mapping.get_type(msg), len(data))
|
|
|
|
|
|
|
|
data = header2 + data
|
|
|
|
|
|
|
|
first = True
|
|
|
|
while len(data):
|
|
|
|
if first:
|
|
|
|
# Magic characters, header1, header2, data padded to 64 bytes
|
|
|
|
datalen = 62 - len(header1)
|
|
|
|
chunk = b'?!' + header1 + data[:datalen] + b'\0' * (datalen - len(data[:datalen]))
|
|
|
|
else:
|
|
|
|
# Magic characters, header1, data padded to 64 bytes
|
|
|
|
datalen = 63 - len(header1)
|
|
|
|
chunk = b'?' + header1 + data[:datalen] + b'\0' * (datalen - len(data[:datalen]))
|
|
|
|
|
|
|
|
self._write_chunk(chunk)
|
|
|
|
data = data[datalen:]
|
|
|
|
first = False
|
2016-06-26 19:29:29 +00:00
|
|
|
|
|
|
|
def _read(self):
|
2016-06-26 22:14:19 +00:00
|
|
|
chunk = self._read_chunk()
|
|
|
|
(session_id, msg_type, datalen, data) = self.parse_first(chunk)
|
|
|
|
|
|
|
|
while len(data) < datalen:
|
|
|
|
chunk = self._read_chunk()
|
2016-07-13 15:41:08 +00:00
|
|
|
(next_session_id, next_data) = self.parse_next(chunk)
|
2016-06-26 22:14:19 +00:00
|
|
|
|
2016-07-13 15:41:08 +00:00
|
|
|
if next_session_id != session_id:
|
2016-06-26 22:14:19 +00:00
|
|
|
raise Exception("Session id mismatch")
|
|
|
|
|
2016-07-13 15:41:08 +00:00
|
|
|
data.extend(next_data)
|
2016-06-26 22:14:19 +00:00
|
|
|
|
|
|
|
# Strip padding zeros
|
|
|
|
data = data[:datalen]
|
|
|
|
return (session_id, msg_type, data)
|
|
|
|
|
|
|
|
def parse_first(self, chunk):
|
|
|
|
if chunk[:2] != b"?!":
|
|
|
|
raise Exception("Unexpected magic characters")
|
|
|
|
|
|
|
|
try:
|
|
|
|
headerlen = struct.calcsize(">LLL")
|
2016-07-19 19:37:54 +00:00
|
|
|
(session_id, msg_type, datalen) = struct.unpack(">LLL", bytes(chunk[2:2 + headerlen]))
|
2016-06-26 22:14:19 +00:00
|
|
|
except:
|
|
|
|
raise Exception("Cannot parse header")
|
|
|
|
|
|
|
|
data = chunk[2 + headerlen:]
|
|
|
|
return (session_id, msg_type, datalen, data)
|
|
|
|
|
|
|
|
def parse_next(self, chunk):
|
|
|
|
if chunk[0:1] != b"?":
|
|
|
|
raise Exception("Unexpected magic characters")
|
|
|
|
|
|
|
|
try:
|
|
|
|
headerlen = struct.calcsize(">L")
|
2016-07-19 19:37:54 +00:00
|
|
|
(session_id,) = struct.unpack(">L", bytes(chunk[1:1 + headerlen]))
|
2016-06-26 22:14:19 +00:00
|
|
|
except:
|
|
|
|
raise Exception("Cannot parse header")
|
|
|
|
|
|
|
|
data = chunk[1 + headerlen:]
|
|
|
|
return (session_id, data)
|
2016-06-26 19:29:29 +00:00
|
|
|
|
2016-06-26 22:14:19 +00:00
|
|
|
'''
|
2016-06-26 19:29:29 +00:00
|
|
|
def read_headers(self, read_f):
|
|
|
|
c = read_f.read(2)
|
|
|
|
if c != b"?!":
|
|
|
|
raise Exception("Unexpected magic characters")
|
|
|
|
|
|
|
|
try:
|
|
|
|
headerlen = struct.calcsize(">HL")
|
|
|
|
(session_id, msg_type, datalen) = struct.unpack(">LLL", read_f.read(headerlen))
|
|
|
|
except:
|
|
|
|
raise Exception("Cannot parse header length")
|
2016-02-10 15:46:58 +00:00
|
|
|
|
2016-06-26 19:29:29 +00:00
|
|
|
return (0, msg_type, datalen)
|
2016-06-26 22:14:19 +00:00
|
|
|
'''
|