2019-10-25 15:43:55 +00:00
|
|
|
import storage
|
|
|
|
import storage.device
|
|
|
|
import storage.recovery
|
|
|
|
import storage.sd_salt
|
|
|
|
from storage import cache
|
2020-04-21 13:49:37 +00:00
|
|
|
from trezor import config, sdcard, utils, wire, workflow
|
2019-08-28 10:42:33 +00:00
|
|
|
from trezor.messages import Capability, MessageType
|
2018-02-27 15:35:21 +00:00
|
|
|
from trezor.messages.Features import Features
|
|
|
|
from trezor.messages.Success import Success
|
|
|
|
|
2019-10-25 15:43:55 +00:00
|
|
|
from apps.common import mnemonic
|
2020-04-21 13:49:37 +00:00
|
|
|
from apps.common.request_pin import verify_user_pin
|
2016-05-25 12:27:22 +00:00
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
if False:
|
2020-04-21 13:49:37 +00:00
|
|
|
import protobuf
|
|
|
|
from typing import Optional, NoReturn
|
2019-07-03 13:07:04 +00:00
|
|
|
from trezor.messages.Initialize import Initialize
|
|
|
|
from trezor.messages.GetFeatures import GetFeatures
|
|
|
|
from trezor.messages.Cancel import Cancel
|
2020-04-21 13:49:37 +00:00
|
|
|
from trezor.messages.LockDevice import LockDevice
|
2019-07-03 13:07:04 +00:00
|
|
|
from trezor.messages.Ping import Ping
|
2016-05-25 12:27:22 +00:00
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
|
|
|
|
def get_features() -> Features:
|
2016-10-20 13:12:25 +00:00
|
|
|
f = Features()
|
2018-07-03 14:20:58 +00:00
|
|
|
f.vendor = "trezor.io"
|
2019-12-07 11:11:51 +00:00
|
|
|
f.language = "en-US"
|
2018-08-31 14:52:56 +00:00
|
|
|
f.major_version = utils.VERSION_MAJOR
|
|
|
|
f.minor_version = utils.VERSION_MINOR
|
|
|
|
f.patch_version = utils.VERSION_PATCH
|
2019-10-02 12:40:25 +00:00
|
|
|
f.revision = utils.GITREV.encode()
|
2018-08-31 14:52:56 +00:00
|
|
|
f.model = utils.MODEL
|
2019-10-25 15:43:55 +00:00
|
|
|
f.device_id = storage.device.get_device_id()
|
|
|
|
f.label = storage.device.get_label()
|
2018-05-28 13:20:31 +00:00
|
|
|
f.pin_protection = config.has_pin()
|
2020-06-19 14:04:24 +00:00
|
|
|
f.unlocked = config.is_unlocked()
|
2019-11-08 08:43:32 +00:00
|
|
|
f.passphrase_protection = storage.device.is_passphrase_enabled()
|
2020-04-27 11:15:56 +00:00
|
|
|
|
2019-08-22 18:16:18 +00:00
|
|
|
if utils.BITCOIN_ONLY:
|
2019-08-28 10:42:33 +00:00
|
|
|
f.capabilities = [
|
|
|
|
Capability.Bitcoin,
|
|
|
|
Capability.Crypto,
|
|
|
|
Capability.Shamir,
|
|
|
|
Capability.ShamirGroups,
|
2019-11-21 09:43:57 +00:00
|
|
|
Capability.PassphraseEntry,
|
2019-08-28 07:25:11 +00:00
|
|
|
]
|
2019-08-22 18:16:18 +00:00
|
|
|
else:
|
2019-08-28 10:42:33 +00:00
|
|
|
f.capabilities = [
|
|
|
|
Capability.Bitcoin,
|
|
|
|
Capability.Bitcoin_like,
|
|
|
|
Capability.Binance,
|
|
|
|
Capability.Cardano,
|
|
|
|
Capability.Crypto,
|
|
|
|
Capability.EOS,
|
|
|
|
Capability.Ethereum,
|
|
|
|
Capability.Lisk,
|
|
|
|
Capability.Monero,
|
|
|
|
Capability.NEM,
|
|
|
|
Capability.Ripple,
|
|
|
|
Capability.Stellar,
|
|
|
|
Capability.Tezos,
|
|
|
|
Capability.U2F,
|
|
|
|
Capability.Shamir,
|
|
|
|
Capability.ShamirGroups,
|
2019-11-21 09:43:57 +00:00
|
|
|
Capability.PassphraseEntry,
|
2019-08-22 18:16:18 +00:00
|
|
|
]
|
2020-02-17 14:51:39 +00:00
|
|
|
f.sd_card_present = sdcard.is_present()
|
2020-06-16 07:20:06 +00:00
|
|
|
f.initialized = storage.device.is_initialized()
|
2020-04-27 11:15:56 +00:00
|
|
|
|
|
|
|
# private fields:
|
|
|
|
if config.is_unlocked():
|
|
|
|
|
|
|
|
f.needs_backup = storage.device.needs_backup()
|
|
|
|
f.unfinished_backup = storage.device.unfinished_backup()
|
|
|
|
f.no_backup = storage.device.no_backup()
|
|
|
|
f.flags = storage.device.get_flags()
|
|
|
|
f.recovery_mode = storage.recovery.is_in_progress()
|
|
|
|
f.backup_type = mnemonic.get_type()
|
|
|
|
f.sd_protection = storage.sd_salt.is_enabled()
|
|
|
|
f.wipe_code_protection = config.has_wipe_code()
|
|
|
|
f.passphrase_always_on_device = storage.device.get_passphrase_always_on_device()
|
|
|
|
|
2016-11-15 10:51:28 +00:00
|
|
|
return f
|
2016-04-28 21:43:34 +00:00
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
async def handle_Initialize(ctx: wire.Context, msg: Initialize) -> Features:
|
2020-02-13 09:19:07 +00:00
|
|
|
features = get_features()
|
|
|
|
if msg.session_id:
|
|
|
|
msg.session_id = bytes(msg.session_id)
|
|
|
|
features.session_id = cache.start_session(msg.session_id)
|
|
|
|
return features
|
2018-05-28 13:20:31 +00:00
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
async def handle_GetFeatures(ctx: wire.Context, msg: GetFeatures) -> Features:
|
2018-05-28 13:20:31 +00:00
|
|
|
return get_features()
|
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
async def handle_Cancel(ctx: wire.Context, msg: Cancel) -> NoReturn:
|
2020-04-21 12:18:53 +00:00
|
|
|
raise wire.ActionCancelled
|
2018-06-06 15:23:27 +00:00
|
|
|
|
|
|
|
|
2020-04-21 13:49:37 +00:00
|
|
|
async def handle_LockDevice(ctx: wire.Context, msg: LockDevice) -> Success:
|
|
|
|
lock_device()
|
2020-02-03 15:28:08 +00:00
|
|
|
return Success()
|
2018-02-27 15:35:21 +00:00
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
async def handle_Ping(ctx: wire.Context, msg: Ping) -> Success:
|
2017-09-06 20:53:36 +00:00
|
|
|
if msg.button_protection:
|
|
|
|
from apps.common.confirm import require_confirm
|
|
|
|
from trezor.messages.ButtonRequestType import ProtectCall
|
|
|
|
from trezor.ui.text import Text
|
2018-07-03 14:20:58 +00:00
|
|
|
|
|
|
|
await require_confirm(ctx, Text("Confirm"), ProtectCall)
|
2018-02-28 16:04:09 +00:00
|
|
|
return Success(message=msg.message)
|
2018-02-09 17:07:47 +00:00
|
|
|
|
|
|
|
|
2020-04-27 11:15:56 +00:00
|
|
|
ALLOW_WHILE_LOCKED = (
|
|
|
|
MessageType.Initialize,
|
|
|
|
MessageType.GetFeatures,
|
2020-05-25 13:20:43 +00:00
|
|
|
MessageType.Cancel,
|
2020-04-29 10:05:08 +00:00
|
|
|
MessageType.LockDevice,
|
2020-04-27 11:15:56 +00:00
|
|
|
MessageType.WipeDevice,
|
|
|
|
)
|
2020-04-21 13:49:37 +00:00
|
|
|
|
|
|
|
|
2020-04-22 08:58:24 +00:00
|
|
|
def set_homescreen() -> None:
|
|
|
|
if not config.is_unlocked():
|
|
|
|
from apps.homescreen.lockscreen import lockscreen
|
|
|
|
|
|
|
|
workflow.set_default(lockscreen)
|
|
|
|
|
|
|
|
elif storage.recovery.is_in_progress():
|
|
|
|
from apps.management.recovery_device.homescreen import recovery_homescreen
|
|
|
|
|
|
|
|
workflow.set_default(recovery_homescreen)
|
|
|
|
|
|
|
|
else:
|
|
|
|
from apps.homescreen.homescreen import homescreen
|
|
|
|
|
|
|
|
workflow.set_default(homescreen)
|
|
|
|
|
|
|
|
|
2020-04-21 13:49:37 +00:00
|
|
|
def lock_device() -> None:
|
2020-04-24 10:16:32 +00:00
|
|
|
if config.has_pin():
|
|
|
|
config.lock()
|
|
|
|
wire.find_handler = get_pinlocked_handler
|
2020-05-19 12:56:22 +00:00
|
|
|
set_homescreen()
|
|
|
|
workflow.close_others()
|
2020-04-21 13:49:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def unlock_device(ctx: wire.GenericContext = wire.DUMMY_CONTEXT) -> None:
|
2020-05-28 09:57:04 +00:00
|
|
|
"""Ensure the device is in unlocked state.
|
|
|
|
|
|
|
|
If the storage is locked, attempt to unlock it. Reset the homescreen and the wire
|
|
|
|
handler.
|
|
|
|
"""
|
|
|
|
if not config.is_unlocked():
|
|
|
|
# verify_user_pin will raise if the PIN was invalid
|
|
|
|
await verify_user_pin(ctx)
|
|
|
|
|
2020-04-22 08:58:24 +00:00
|
|
|
set_homescreen()
|
2020-04-21 13:49:37 +00:00
|
|
|
wire.find_handler = wire.find_registered_workflow_handler
|
|
|
|
|
|
|
|
|
|
|
|
def get_pinlocked_handler(
|
|
|
|
iface: wire.WireInterface, msg_type: int
|
|
|
|
) -> Optional[wire.Handler[wire.Msg]]:
|
|
|
|
orig_handler = wire.find_registered_workflow_handler(iface, msg_type)
|
|
|
|
if orig_handler is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if __debug__:
|
|
|
|
import usb
|
|
|
|
|
|
|
|
if iface is usb.iface_debug:
|
|
|
|
return orig_handler
|
|
|
|
|
|
|
|
if msg_type in ALLOW_WHILE_LOCKED:
|
|
|
|
return orig_handler
|
|
|
|
|
|
|
|
async def wrapper(ctx: wire.Context, msg: wire.Msg) -> protobuf.MessageType:
|
|
|
|
# mypy limitation: orig_handler is not recognized as non-None
|
|
|
|
assert orig_handler is not None
|
|
|
|
await unlock_device(ctx)
|
|
|
|
return await orig_handler(ctx, msg)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2019-10-31 09:27:59 +00:00
|
|
|
def boot() -> None:
|
2020-04-21 13:49:37 +00:00
|
|
|
wire.register(MessageType.Initialize, handle_Initialize)
|
|
|
|
wire.register(MessageType.GetFeatures, handle_GetFeatures)
|
|
|
|
wire.register(MessageType.Cancel, handle_Cancel)
|
|
|
|
wire.register(MessageType.LockDevice, handle_LockDevice)
|
|
|
|
wire.register(MessageType.Ping, handle_Ping)
|
2020-05-18 12:59:58 +00:00
|
|
|
|
|
|
|
workflow.idle_timer.set(storage.device.get_autolock_delay_ms(), lock_device)
|