2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
2016-11-25 21:53:55 +00:00
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# Copyright (C) 2012-2018 SatoshiLabs and contributors
|
2016-11-25 21:53:55 +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.
|
2016-11-25 21:53:55 +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>.
|
2016-11-25 21:53:55 +00:00
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
import logging
|
2018-01-29 16:46:24 +00:00
|
|
|
import struct
|
2018-08-13 16:21:24 +00:00
|
|
|
from io import BytesIO
|
|
|
|
|
|
|
|
import requests
|
2017-08-24 12:29:27 +00:00
|
|
|
|
2018-03-02 14:44:24 +00:00
|
|
|
from . import Transport, TransportException
|
2018-08-13 16:21:24 +00:00
|
|
|
from .. import mapping, protobuf
|
2014-07-26 14:27:28 +00:00
|
|
|
|
2018-05-24 15:55:41 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
TREZORD_HOST = "http://127.0.0.1:21325"
|
2014-07-26 14:27:28 +00:00
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
class BridgeTransport(Transport):
|
2018-08-13 16:21:24 +00:00
|
|
|
"""
|
2017-08-24 12:29:27 +00:00
|
|
|
BridgeTransport implements transport through TREZOR Bridge (aka trezord).
|
2018-08-13 16:21:24 +00:00
|
|
|
"""
|
2017-04-20 11:16:15 +00:00
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
PATH_PREFIX = "bridge"
|
|
|
|
HEADERS = {"Origin": "https://python.trezor.io"}
|
2018-02-02 18:17:48 +00:00
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
def __init__(self, device):
|
2018-03-02 14:44:24 +00:00
|
|
|
super().__init__()
|
2015-12-21 17:18:06 +00:00
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
self.device = device
|
|
|
|
self.conn = requests.Session()
|
2015-12-21 17:18:06 +00:00
|
|
|
self.session = None
|
2018-10-04 15:05:10 +00:00
|
|
|
self.request = None
|
2015-12-21 17:18:06 +00:00
|
|
|
|
2018-02-06 20:10:30 +00:00
|
|
|
def get_path(self):
|
2018-08-13 16:21:24 +00:00
|
|
|
return "%s:%s" % (self.PATH_PREFIX, self.device["path"])
|
2015-12-21 17:18:06 +00:00
|
|
|
|
2018-10-04 15:05:10 +00:00
|
|
|
@classmethod
|
|
|
|
def _call(cls, action, data=None, uri_suffix=None, session=None):
|
|
|
|
if uri_suffix is not None:
|
|
|
|
uri_suffix = "/" + uri_suffix
|
|
|
|
elif session is not None:
|
|
|
|
uri_suffix = "/{}".format(session)
|
|
|
|
else:
|
|
|
|
uri_suffix = ""
|
|
|
|
|
|
|
|
url = "{}/{}{}".format(TREZORD_HOST, action, uri_suffix)
|
|
|
|
r = requests.post(url, headers=cls.HEADERS, data=data)
|
|
|
|
|
|
|
|
if r.status_code != 200:
|
|
|
|
raise TransportException(
|
|
|
|
"trezord: '{}' action failed with code {}: {}".format(
|
|
|
|
action, r.status_code, r.json().get("error", "(no error message)")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return r
|
|
|
|
|
2018-03-01 09:33:47 +00:00
|
|
|
@classmethod
|
|
|
|
def enumerate(cls):
|
2018-02-02 18:17:48 +00:00
|
|
|
try:
|
2018-10-04 15:05:10 +00:00
|
|
|
r = cls._call("enumerate")
|
2018-02-02 18:17:48 +00:00
|
|
|
return [BridgeTransport(dev) for dev in r.json()]
|
2018-08-10 14:05:14 +00:00
|
|
|
except Exception:
|
2018-02-02 18:17:48 +00:00
|
|
|
return []
|
2017-08-24 12:29:27 +00:00
|
|
|
|
|
|
|
def open(self):
|
2018-10-04 15:05:10 +00:00
|
|
|
r = self._call("acquire", uri_suffix="{}/null".format(self.device["path"]))
|
2018-08-13 16:21:24 +00:00
|
|
|
self.session = r.json()["session"]
|
2014-07-26 14:27:28 +00:00
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
def close(self):
|
|
|
|
if not self.session:
|
|
|
|
return
|
2018-10-04 15:05:10 +00:00
|
|
|
self._call("release", session=self.session)
|
2017-08-24 12:29:27 +00:00
|
|
|
self.session = None
|
|
|
|
|
|
|
|
def write(self, msg):
|
2018-10-04 15:05:10 +00:00
|
|
|
if self.request is not None:
|
|
|
|
raise TransportException("trezord can't perform two writes without a read")
|
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
LOG.debug(
|
2018-10-04 15:05:10 +00:00
|
|
|
"preparing message: {}".format(msg.__class__.__name__),
|
2018-08-13 16:21:24 +00:00
|
|
|
extra={"protobuf": msg},
|
|
|
|
)
|
2018-10-04 15:05:10 +00:00
|
|
|
# encode the message
|
2018-01-29 16:46:24 +00:00
|
|
|
data = BytesIO()
|
|
|
|
protobuf.dump_message(data, msg)
|
|
|
|
ser = data.getvalue()
|
|
|
|
header = struct.pack(">HL", mapping.get_type(msg), len(ser))
|
2018-10-04 15:05:10 +00:00
|
|
|
# store for later
|
|
|
|
self.request = (header + ser).hex()
|
2014-07-26 14:27:28 +00:00
|
|
|
|
2017-08-24 12:29:27 +00:00
|
|
|
def read(self):
|
2018-10-04 15:05:10 +00:00
|
|
|
if self.request is None:
|
|
|
|
raise TransportException("trezord: no request in queue")
|
|
|
|
|
|
|
|
try:
|
|
|
|
LOG.debug("sending prepared message")
|
|
|
|
r = self._call("call", data=self.request, session=self.session)
|
|
|
|
|
|
|
|
data = bytes.fromhex(r.text)
|
|
|
|
headerlen = struct.calcsize(">HL")
|
|
|
|
msg_type, datalen = struct.unpack(">HL", data[:headerlen])
|
|
|
|
data = BytesIO(data[headerlen : headerlen + datalen])
|
|
|
|
msg = protobuf.load_message(data, mapping.get_class(msg_type))
|
|
|
|
LOG.debug(
|
|
|
|
"received message: {}".format(msg.__class__.__name__),
|
|
|
|
extra={"protobuf": msg},
|
|
|
|
)
|
|
|
|
return msg
|
|
|
|
finally:
|
|
|
|
self.request = None
|
2018-05-24 17:14:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
TRANSPORT = BridgeTransport
|