2016-09-29 10:29:43 +00:00
|
|
|
from micropython import const
|
2016-09-21 12:21:18 +00:00
|
|
|
import ustruct
|
|
|
|
|
2017-07-04 16:09:08 +00:00
|
|
|
from trezor import loop
|
|
|
|
from trezor import utils
|
|
|
|
from trezor.crypto import random
|
|
|
|
|
|
|
|
# TREZOR wire protocol #2:
|
2016-09-21 12:21:18 +00:00
|
|
|
#
|
2017-07-04 16:09:08 +00:00
|
|
|
# # Initial message report
|
|
|
|
# uint8_t marker; // REP_MARKER_INIT
|
|
|
|
# uint32_t session_id; // Big-endian
|
|
|
|
# uint32_t message_type; // Big-endian
|
|
|
|
# uint32_t message_size; // Big-endian
|
|
|
|
# uint8_t data[];
|
2016-09-21 12:21:18 +00:00
|
|
|
#
|
2017-07-04 16:09:08 +00:00
|
|
|
# # Continuation message report
|
|
|
|
# uint8_t marker; // REP_MARKER_CONT
|
|
|
|
# uint32_t session_id; // Big-endian
|
|
|
|
# uint32_t sequence; // Big-endian, 0 for 1st continuation report
|
|
|
|
# uint8_t data[];
|
2016-09-21 12:21:18 +00:00
|
|
|
|
|
|
|
_REP_LEN = const(64)
|
2016-10-20 13:13:08 +00:00
|
|
|
|
2017-07-04 16:09:08 +00:00
|
|
|
_REP_MARKER_INIT = const(0x01)
|
|
|
|
_REP_MARKER_CONT = const(0x02)
|
|
|
|
_REP_MARKER_OPEN = const(0x03)
|
|
|
|
_REP_MARKER_CLOSE = const(0x04)
|
2016-09-21 12:21:18 +00:00
|
|
|
|
2017-08-15 13:09:09 +00:00
|
|
|
_REP = '>BL' # marker, session_id
|
2017-07-04 16:09:08 +00:00
|
|
|
_REP_INIT = '>BLLL' # marker, session_id, message_type, message_size
|
2017-08-15 13:09:09 +00:00
|
|
|
_REP_CONT = '>BLL' # marker, session_id, sequence
|
2017-07-04 16:09:08 +00:00
|
|
|
_REP_INIT_DATA = const(13) # offset of data in init report
|
2017-08-15 13:09:09 +00:00
|
|
|
_REP_CONT_DATA = const(9) # offset of data in cont report
|
2016-09-21 12:21:18 +00:00
|
|
|
|
|
|
|
|
2017-07-04 16:09:08 +00:00
|
|
|
class Reader:
|
|
|
|
'''
|
|
|
|
Decoder for v2 codec over the HID layer. Provides readable async-file-like
|
|
|
|
interface.
|
2016-10-21 13:01:49 +00:00
|
|
|
'''
|
2016-09-21 12:21:18 +00:00
|
|
|
|
2017-07-04 16:09:08 +00:00
|
|
|
def __init__(self, iface, sid):
|
|
|
|
self.iface = iface
|
|
|
|
self.sid = sid
|
|
|
|
self.type = None
|
|
|
|
self.size = None
|
|
|
|
self.data = None
|
|
|
|
self.ofs = 0
|
|
|
|
self.seq = 0
|
|
|
|
|
|
|
|
def __repr__(self):
|
2017-08-15 13:09:09 +00:00
|
|
|
return '<Reader: sid=%x type=%d size=%dB>' % (self.sid, self.type,
|
|
|
|
self.size)
|
2017-07-04 16:09:08 +00:00
|
|
|
|
2017-08-15 13:09:09 +00:00
|
|
|
async def aopen(self):
|
2017-07-04 16:09:08 +00:00
|
|
|
'''
|
|
|
|
Begin the message transmission by waiting for initial V2 message report
|
|
|
|
on this session. `self.type` and `self.size` are initialized and
|
2017-08-15 13:09:09 +00:00
|
|
|
available after `aopen()` returns.
|
2017-07-04 16:09:08 +00:00
|
|
|
'''
|
2017-08-15 13:09:09 +00:00
|
|
|
read = loop.select(self.iface.iface_num() | loop.READ)
|
2017-07-04 16:09:08 +00:00
|
|
|
while True:
|
|
|
|
# wait for initial report
|
|
|
|
report = await read
|
|
|
|
marker, sid, mtype, msize = ustruct.unpack(_REP_INIT, report)
|
|
|
|
if sid == self.sid and marker == _REP_MARKER_INIT:
|
|
|
|
break
|
|
|
|
|
|
|
|
# load received message header
|
|
|
|
self.type = mtype
|
|
|
|
self.size = msize
|
|
|
|
self.data = report[_REP_INIT_DATA:_REP_INIT_DATA + msize]
|
|
|
|
self.ofs = 0
|
|
|
|
self.seq = 0
|
|
|
|
|
2017-08-15 13:09:09 +00:00
|
|
|
async def areadinto(self, buf):
|
2017-07-04 16:09:08 +00:00
|
|
|
'''
|
|
|
|
Read exactly `len(buf)` bytes into `buf`, waiting for additional
|
|
|
|
reports, if needed. Raises `EOFError` if end-of-message is encountered
|
|
|
|
before the full read can be completed.
|
|
|
|
'''
|
|
|
|
if self.size < len(buf):
|
|
|
|
raise EOFError
|
|
|
|
|
2017-08-15 13:09:09 +00:00
|
|
|
read = loop.select(self.iface.iface_num() | loop.READ)
|
2017-07-04 16:09:08 +00:00
|
|
|
nread = 0
|
|
|
|
while nread < len(buf):
|
|
|
|
if self.ofs == len(self.data):
|
|
|
|
# we are at the end of received data
|
|
|
|
# wait for continuation report
|
|
|
|
while True:
|
|
|
|
report = await read
|
|
|
|
marker, sid, seq = ustruct.unpack(_REP_CONT, report)
|
|
|
|
if sid == self.sid and marker == _REP_MARKER_CONT:
|
|
|
|
if seq != self.seq:
|
|
|
|
raise ValueError
|
|
|
|
break
|
|
|
|
self.data = report[_REP_CONT_DATA:_REP_CONT_DATA + self.size]
|
|
|
|
self.seq += 1
|
|
|
|
self.ofs = 0
|
|
|
|
|
|
|
|
# copy as much as possible to target buffer
|
|
|
|
nbytes = utils.memcpy(buf, nread, self.data, self.ofs, len(buf))
|
|
|
|
nread += nbytes
|
|
|
|
self.ofs += nbytes
|
|
|
|
self.size -= nbytes
|
|
|
|
|
|
|
|
return nread
|
|
|
|
|
|
|
|
|
|
|
|
class Writer:
|
|
|
|
'''
|
|
|
|
Encoder for v2 codec over the HID layer. Provides writable async-file-like
|
|
|
|
interface.
|
|
|
|
'''
|
2016-09-21 12:21:18 +00:00
|
|
|
|
2017-08-15 13:09:09 +00:00
|
|
|
def __init__(self, iface, sid):
|
2017-07-04 16:09:08 +00:00
|
|
|
self.iface = iface
|
|
|
|
self.sid = sid
|
2017-08-15 13:09:09 +00:00
|
|
|
self.type = None
|
|
|
|
self.size = None
|
|
|
|
self.data = bytearray(_REP_LEN)
|
|
|
|
self.ofs = 0
|
|
|
|
self.seq = 0
|
|
|
|
|
2017-08-21 11:22:35 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return '<WriterV2: type=%d size=%dB>' % (self.type, self.size)
|
|
|
|
|
2017-08-15 13:09:09 +00:00
|
|
|
def setheader(self, mtype, msize):
|
|
|
|
'''
|
|
|
|
Reset the writer state and load the message header with passed type and
|
|
|
|
total message size.
|
|
|
|
'''
|
2017-07-04 16:09:08 +00:00
|
|
|
self.type = mtype
|
|
|
|
self.size = msize
|
2017-08-15 13:09:09 +00:00
|
|
|
ustruct.pack_into(_REP_INIT, self.data, 0, _REP_MARKER_INIT, self.sid,
|
|
|
|
mtype, msize)
|
2017-07-04 16:09:08 +00:00
|
|
|
self.ofs = _REP_INIT_DATA
|
|
|
|
self.seq = 0
|
|
|
|
|
2017-08-15 13:09:09 +00:00
|
|
|
async def awrite(self, buf):
|
2017-07-04 16:09:08 +00:00
|
|
|
'''
|
|
|
|
Encode and write every byte from `buf`. Does not need to be called in
|
|
|
|
case message has zero length. Raises `EOFError` if the length of `buf`
|
|
|
|
exceeds the remaining message length.
|
|
|
|
'''
|
|
|
|
if self.size < len(buf):
|
|
|
|
raise EOFError
|
|
|
|
|
2017-08-15 13:09:09 +00:00
|
|
|
write = loop.select(self.iface.iface_num() | loop.WRITE)
|
2017-07-04 16:09:08 +00:00
|
|
|
nwritten = 0
|
|
|
|
while nwritten < len(buf):
|
|
|
|
# copy as much as possible to report buffer
|
|
|
|
nbytes = utils.memcpy(self.data, self.ofs, buf, nwritten, len(buf))
|
|
|
|
nwritten += nbytes
|
|
|
|
self.ofs += nbytes
|
|
|
|
self.size -= nbytes
|
|
|
|
|
|
|
|
if self.ofs == _REP_LEN:
|
|
|
|
# we are at the end of the report, flush it, and prepare header
|
|
|
|
await write
|
2017-08-15 13:09:09 +00:00
|
|
|
self.iface.write(self.data)
|
|
|
|
ustruct.pack_into(_REP_CONT, self.data, 0, _REP_MARKER_CONT,
|
|
|
|
self.sid, self.seq)
|
2017-07-04 16:09:08 +00:00
|
|
|
self.ofs = _REP_CONT_DATA
|
|
|
|
self.seq += 1
|
|
|
|
|
|
|
|
return nwritten
|
|
|
|
|
2017-08-15 13:09:09 +00:00
|
|
|
async def aclose(self):
|
2017-07-04 16:09:08 +00:00
|
|
|
'''Flush and close the message transmission.'''
|
|
|
|
if self.ofs != _REP_CONT_DATA:
|
|
|
|
# we didn't write anything or last write() wasn't report-aligned,
|
|
|
|
# pad the final report and flush it
|
|
|
|
while self.ofs < _REP_LEN:
|
|
|
|
self.data[self.ofs] = 0x00
|
|
|
|
self.ofs += 1
|
|
|
|
|
2017-08-15 13:09:09 +00:00
|
|
|
await loop.select(self.iface.iface_num() | loop.WRITE)
|
|
|
|
self.iface.write(self.data)
|
2017-07-04 16:09:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SesssionSupervisor:
|
|
|
|
'''Handles session open/close requests on v2 protocol layer.'''
|
|
|
|
|
|
|
|
def __init__(self, iface, handler):
|
|
|
|
self.iface = iface
|
|
|
|
self.handler = handler
|
|
|
|
self.handling_tasks = {}
|
|
|
|
self.session_report = bytearray(_REP_LEN)
|
|
|
|
|
|
|
|
async def listen(self):
|
|
|
|
'''
|
|
|
|
Listen for open/close requests on configured interface. After open
|
|
|
|
request, session is started and a new task is scheduled to handle it.
|
|
|
|
After close request, the handling task is closed and session terminated.
|
|
|
|
Both requests receive responses confirming the operation.
|
|
|
|
'''
|
2017-08-15 13:09:09 +00:00
|
|
|
read = loop.select(self.iface.iface_num() | loop.READ)
|
|
|
|
write = loop.select(self.iface.iface_num() | loop.WRITE)
|
2017-07-04 16:09:08 +00:00
|
|
|
while True:
|
|
|
|
report = await read
|
|
|
|
repmarker, repsid = ustruct.unpack(_REP, report)
|
|
|
|
# because tasks paused on I/O have a priority over time-scheduled
|
|
|
|
# tasks, we need to `yield` explicitly before sending a response to
|
|
|
|
# open/close request. Otherwise the handler would have no chance to
|
|
|
|
# run and schedule communication.
|
|
|
|
if repmarker == _REP_MARKER_OPEN:
|
|
|
|
newsid = self.newsid()
|
|
|
|
self.open(newsid)
|
|
|
|
yield
|
|
|
|
await write
|
2017-08-14 09:08:47 +00:00
|
|
|
self.writeopen(newsid)
|
2017-07-04 16:09:08 +00:00
|
|
|
elif repmarker == _REP_MARKER_CLOSE:
|
|
|
|
self.close(repsid)
|
|
|
|
yield
|
|
|
|
await write
|
2017-08-14 09:08:47 +00:00
|
|
|
self.writeclose(repsid)
|
2017-07-04 16:09:08 +00:00
|
|
|
|
|
|
|
def open(self, sid):
|
|
|
|
if sid not in self.handling_tasks:
|
|
|
|
task = self.handling_tasks[sid] = self.handler(self.iface, sid)
|
|
|
|
loop.schedule_task(task)
|
|
|
|
|
|
|
|
def close(self, sid):
|
|
|
|
if sid in self.handling_tasks:
|
|
|
|
task = self.handling_tasks.pop(sid)
|
|
|
|
task.close()
|
|
|
|
|
|
|
|
def newsid(self):
|
|
|
|
while True:
|
|
|
|
sid = random.uniform(0xffffffff) + 1
|
|
|
|
if sid not in self.handling_tasks:
|
|
|
|
return sid
|
|
|
|
|
2017-08-14 09:08:47 +00:00
|
|
|
def writeopen(self, sid):
|
2017-07-04 16:09:08 +00:00
|
|
|
ustruct.pack_into(_REP, self.session_report, 0, _REP_MARKER_OPEN, sid)
|
2017-08-15 13:09:09 +00:00
|
|
|
self.iface.write(self.session_report)
|
2017-07-04 16:09:08 +00:00
|
|
|
|
2017-08-14 09:08:47 +00:00
|
|
|
def writeclose(self, sid):
|
2017-07-04 16:09:08 +00:00
|
|
|
ustruct.pack_into(_REP, self.session_report, 0, _REP_MARKER_CLOSE, sid)
|
2017-08-15 13:09:09 +00:00
|
|
|
self.iface.write(self.session_report)
|