2014-04-29 12:26:51 +00:00
|
|
|
/*
|
2017-11-05 16:46:34 +00:00
|
|
|
* This file is part of the TREZOR project, https://trezor.io/
|
2014-04-29 12:26:51 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2014 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "protect.h"
|
2019-01-25 10:58:23 +00:00
|
|
|
#include "config.h"
|
2018-03-15 18:00:54 +00:00
|
|
|
#include "memory.h"
|
2014-04-29 12:26:51 +00:00
|
|
|
#include "messages.h"
|
|
|
|
#include "usb.h"
|
|
|
|
#include "oled.h"
|
|
|
|
#include "buttons.h"
|
|
|
|
#include "pinmatrix.h"
|
|
|
|
#include "fsm.h"
|
|
|
|
#include "layout2.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "debug.h"
|
2017-06-18 20:22:33 +00:00
|
|
|
#include "gettext.h"
|
2018-01-18 14:21:48 +00:00
|
|
|
#include "memzero.h"
|
2018-07-10 14:58:00 +00:00
|
|
|
#include "messages.pb.h"
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2017-07-31 00:26:28 +00:00
|
|
|
#define MAX_WRONG_PINS 15
|
|
|
|
|
2018-06-05 20:38:39 +00:00
|
|
|
bool protectAbortedByCancel = false;
|
2014-05-20 13:36:35 +00:00
|
|
|
bool protectAbortedByInitialize = false;
|
|
|
|
|
2018-08-27 17:06:11 +00:00
|
|
|
bool protectButton(ButtonRequestType type, bool confirm_only)
|
2014-04-29 12:26:51 +00:00
|
|
|
{
|
|
|
|
ButtonRequest resp;
|
2014-06-06 00:45:15 +00:00
|
|
|
bool result = false;
|
2014-04-29 12:26:51 +00:00
|
|
|
bool acked = false;
|
2014-06-06 00:45:15 +00:00
|
|
|
#if DEBUG_LINK
|
|
|
|
bool debug_decided = false;
|
|
|
|
#endif
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2019-01-23 16:00:01 +00:00
|
|
|
memzero(&resp, sizeof(ButtonRequest));
|
2014-04-29 12:26:51 +00:00
|
|
|
resp.has_code = true;
|
|
|
|
resp.code = type;
|
|
|
|
usbTiny(1);
|
2016-05-23 23:54:08 +00:00
|
|
|
buttonUpdate(); // Clear button state
|
2014-04-29 12:26:51 +00:00
|
|
|
msg_write(MessageType_MessageType_ButtonRequest, &resp);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
usbPoll();
|
|
|
|
|
2014-06-06 00:45:15 +00:00
|
|
|
// check for ButtonAck
|
2014-04-29 12:26:51 +00:00
|
|
|
if (msg_tiny_id == MessageType_MessageType_ButtonAck) {
|
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
acked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// button acked - check buttons
|
|
|
|
if (acked) {
|
2016-11-17 19:07:41 +00:00
|
|
|
usbSleep(5);
|
2014-04-29 12:26:51 +00:00
|
|
|
buttonUpdate();
|
|
|
|
if (button.YesUp) {
|
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!confirm_only && button.NoUp) {
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 00:45:15 +00:00
|
|
|
// check for Cancel / Initialize
|
2018-06-06 12:45:50 +00:00
|
|
|
protectAbortedByCancel = (msg_tiny_id == MessageType_MessageType_Cancel);
|
|
|
|
protectAbortedByInitialize = (msg_tiny_id == MessageType_MessageType_Initialize);
|
|
|
|
if (protectAbortedByCancel || protectAbortedByInitialize) {
|
2014-04-29 12:26:51 +00:00
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_LINK
|
2014-06-06 00:45:15 +00:00
|
|
|
// check DebugLink
|
2014-04-29 12:26:51 +00:00
|
|
|
if (msg_tiny_id == MessageType_MessageType_DebugLinkDecision) {
|
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
DebugLinkDecision *dld = (DebugLinkDecision *)msg_tiny;
|
|
|
|
result = dld->yes_no;
|
2014-06-06 00:45:15 +00:00
|
|
|
debug_decided = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (acked && debug_decided) {
|
2014-04-29 12:26:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg_tiny_id == MessageType_MessageType_DebugLinkGetState) {
|
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
fsm_msgDebugLinkGetState((DebugLinkGetState *)msg_tiny);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
usbTiny(0);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-08-27 17:06:11 +00:00
|
|
|
const char *requestPin(PinMatrixRequestType type, const char *text)
|
2014-04-29 12:26:51 +00:00
|
|
|
{
|
|
|
|
PinMatrixRequest resp;
|
2019-01-23 16:00:01 +00:00
|
|
|
memzero(&resp, sizeof(PinMatrixRequest));
|
2014-04-29 12:26:51 +00:00
|
|
|
resp.has_type = true;
|
|
|
|
resp.type = type;
|
|
|
|
usbTiny(1);
|
|
|
|
msg_write(MessageType_MessageType_PinMatrixRequest, &resp);
|
|
|
|
pinmatrix_start(text);
|
|
|
|
for (;;) {
|
|
|
|
usbPoll();
|
|
|
|
if (msg_tiny_id == MessageType_MessageType_PinMatrixAck) {
|
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
PinMatrixAck *pma = (PinMatrixAck *)msg_tiny;
|
|
|
|
pinmatrix_done(pma->pin); // convert via pinmatrix
|
|
|
|
usbTiny(0);
|
|
|
|
return pma->pin;
|
|
|
|
}
|
2018-06-06 12:45:50 +00:00
|
|
|
// check for Cancel / Initialize
|
|
|
|
protectAbortedByCancel = (msg_tiny_id == MessageType_MessageType_Cancel);
|
|
|
|
protectAbortedByInitialize = (msg_tiny_id == MessageType_MessageType_Initialize);
|
|
|
|
if (protectAbortedByCancel || protectAbortedByInitialize) {
|
2014-04-29 12:26:51 +00:00
|
|
|
pinmatrix_done(0);
|
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
usbTiny(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#if DEBUG_LINK
|
|
|
|
if (msg_tiny_id == MessageType_MessageType_DebugLinkGetState) {
|
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
fsm_msgDebugLinkGetState((DebugLinkGetState *)msg_tiny);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 18:01:06 +00:00
|
|
|
secbool protectPinUiCallback(uint32_t wait, uint32_t progress)
|
2019-01-25 10:58:23 +00:00
|
|
|
{
|
|
|
|
(void) progress;
|
|
|
|
|
|
|
|
// Convert wait to secstr string.
|
|
|
|
char secstrbuf[] = _("________0 seconds");
|
|
|
|
char *secstr = secstrbuf + 9;
|
|
|
|
uint32_t secs = wait;
|
2019-02-05 10:57:19 +00:00
|
|
|
do {
|
2019-01-25 10:58:23 +00:00
|
|
|
secstr--;
|
|
|
|
*secstr = (secs % 10) + '0';
|
|
|
|
secs /= 10;
|
2019-02-05 10:57:19 +00:00
|
|
|
} while (secs > 0 && secstr >= secstrbuf);
|
2019-01-25 10:58:23 +00:00
|
|
|
if (wait == 1) {
|
|
|
|
// Change "seconds" to "second".
|
|
|
|
secstrbuf[16] = 0;
|
|
|
|
}
|
2019-02-07 15:27:03 +00:00
|
|
|
layoutDialog(&bmp_icon_info, NULL, NULL, NULL, _("Verifying PIN"), NULL, _("Please wait"), secstr, _("to continue ..."), NULL);
|
2017-07-31 11:02:44 +00:00
|
|
|
|
2019-02-06 18:01:06 +00:00
|
|
|
// Check for Cancel / Initialize.
|
|
|
|
protectAbortedByCancel = (msg_tiny_id == MessageType_MessageType_Cancel);
|
|
|
|
protectAbortedByInitialize = (msg_tiny_id == MessageType_MessageType_Initialize);
|
|
|
|
if (protectAbortedByCancel || protectAbortedByInitialize) {
|
2019-01-25 10:58:23 +00:00
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
usbTiny(0);
|
|
|
|
fsm_sendFailure(FailureType_Failure_PinCancelled, NULL);
|
2019-02-06 18:01:06 +00:00
|
|
|
return sectrue;
|
2019-01-25 10:58:23 +00:00
|
|
|
}
|
2019-02-06 18:01:06 +00:00
|
|
|
|
|
|
|
return secfalse;
|
2017-07-31 11:02:44 +00:00
|
|
|
}
|
|
|
|
|
2014-04-29 12:26:51 +00:00
|
|
|
bool protectPin(bool use_cached)
|
|
|
|
{
|
2019-02-05 19:40:58 +00:00
|
|
|
if (use_cached && session_isPinCached()) {
|
2014-04-29 12:26:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-01-25 10:58:23 +00:00
|
|
|
|
|
|
|
// TODO If maximum number of PIN attempts:
|
|
|
|
// error_shutdown("Too many wrong PIN", "attempts. Storage has", "been wiped.", NULL, "Please unplug", "the device.");
|
|
|
|
|
2019-02-05 19:40:58 +00:00
|
|
|
const char *pin = "";
|
|
|
|
if (config_hasPin()) {
|
|
|
|
pin = requestPin(PinMatrixRequestType_PinMatrixRequestType_Current, _("Please enter current PIN:"));
|
|
|
|
if (!pin) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_PinCancelled, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
2019-01-25 10:58:23 +00:00
|
|
|
|
|
|
|
usbTiny(1);
|
|
|
|
bool ret = config_containsPin(pin);
|
|
|
|
usbTiny(0);
|
|
|
|
if (!ret) {
|
2018-08-27 17:06:11 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_PinInvalid, NULL);
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
2019-01-25 10:58:23 +00:00
|
|
|
return ret;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2019-01-25 10:58:23 +00:00
|
|
|
bool protectChangePin(bool removal)
|
2014-04-29 12:26:51 +00:00
|
|
|
{
|
2019-01-25 10:58:23 +00:00
|
|
|
static CONFIDENTIAL char old_pin[MAX_PIN_LEN + 1] = "";
|
|
|
|
static CONFIDENTIAL char new_pin[MAX_PIN_LEN + 1] = "";
|
|
|
|
const char* pin = NULL;
|
2017-09-04 15:09:34 +00:00
|
|
|
|
2019-01-25 10:58:23 +00:00
|
|
|
if (config_hasPin()) {
|
|
|
|
pin = requestPin(PinMatrixRequestType_PinMatrixRequestType_Current, _("Please enter current PIN:"));
|
|
|
|
if (pin == NULL) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_PinCancelled, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
strlcpy(old_pin, pin, sizeof(old_pin));
|
|
|
|
}
|
2017-09-04 15:09:34 +00:00
|
|
|
|
2019-01-25 10:58:23 +00:00
|
|
|
if (!removal) {
|
|
|
|
pin = requestPin(PinMatrixRequestType_PinMatrixRequestType_NewFirst, _("Please enter new PIN:"));
|
|
|
|
if (pin == NULL) {
|
|
|
|
memzero(old_pin, sizeof(old_pin));
|
|
|
|
fsm_sendFailure(FailureType_Failure_PinCancelled, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
strlcpy(new_pin, pin, sizeof(new_pin));
|
2017-09-04 15:09:34 +00:00
|
|
|
|
2019-01-25 10:58:23 +00:00
|
|
|
pin = requestPin(PinMatrixRequestType_PinMatrixRequestType_NewSecond, _("Please re-enter new PIN:"));
|
|
|
|
if (pin == NULL) {
|
|
|
|
memzero(old_pin, sizeof(old_pin));
|
|
|
|
memzero(new_pin, sizeof(new_pin));
|
|
|
|
fsm_sendFailure(FailureType_Failure_PinCancelled, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-04 15:09:34 +00:00
|
|
|
|
2019-01-25 10:58:23 +00:00
|
|
|
if (strncmp(new_pin, pin, sizeof(new_pin)) != 0) {
|
|
|
|
memzero(old_pin, sizeof(old_pin));
|
|
|
|
memzero(new_pin, sizeof(new_pin));
|
|
|
|
fsm_sendFailure(FailureType_Failure_PinMismatch, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-09-04 15:09:34 +00:00
|
|
|
|
2019-01-25 10:58:23 +00:00
|
|
|
usbTiny(1);
|
|
|
|
bool ret = config_changePin(old_pin, new_pin);
|
|
|
|
usbTiny(0);
|
|
|
|
memzero(old_pin, sizeof(old_pin));
|
|
|
|
memzero(new_pin, sizeof(new_pin));
|
|
|
|
if (ret == false) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_PinInvalid, NULL);
|
|
|
|
}
|
|
|
|
return ret;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool protectPassphrase(void)
|
|
|
|
{
|
2019-02-07 15:06:07 +00:00
|
|
|
bool passphrase_protection = false;
|
|
|
|
config_getPassphraseProtection(&passphrase_protection);
|
|
|
|
if (!passphrase_protection || session_isPassphraseCached()) {
|
2014-04-29 12:26:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
PassphraseRequest resp;
|
2019-01-23 16:00:01 +00:00
|
|
|
memzero(&resp, sizeof(PassphraseRequest));
|
2014-04-29 12:26:51 +00:00
|
|
|
usbTiny(1);
|
|
|
|
msg_write(MessageType_MessageType_PassphraseRequest, &resp);
|
|
|
|
|
2017-06-18 20:22:33 +00:00
|
|
|
layoutDialogSwipe(&bmp_icon_info, NULL, NULL, NULL, _("Please enter your"), _("passphrase using"), _("the computer's"), _("keyboard."), NULL, NULL);
|
2014-04-29 12:26:51 +00:00
|
|
|
|
|
|
|
bool result;
|
|
|
|
for (;;) {
|
|
|
|
usbPoll();
|
2018-02-24 16:26:57 +00:00
|
|
|
// TODO: correctly process PassphraseAck with state field set (mismatch => Failure)
|
2014-04-29 12:26:51 +00:00
|
|
|
if (msg_tiny_id == MessageType_MessageType_PassphraseAck) {
|
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
PassphraseAck *ppa = (PassphraseAck *)msg_tiny;
|
2018-02-27 14:41:02 +00:00
|
|
|
session_cachePassphrase(ppa->has_passphrase ? ppa->passphrase : "");
|
2014-04-29 12:26:51 +00:00
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
}
|
2018-06-06 12:45:50 +00:00
|
|
|
// check for Cancel / Initialize
|
|
|
|
protectAbortedByCancel = (msg_tiny_id == MessageType_MessageType_Cancel);
|
|
|
|
protectAbortedByInitialize = (msg_tiny_id == MessageType_MessageType_Initialize);
|
|
|
|
if (protectAbortedByCancel || protectAbortedByInitialize) {
|
2014-04-29 12:26:51 +00:00
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
usbTiny(0);
|
|
|
|
layoutHome();
|
|
|
|
return result;
|
|
|
|
}
|