2014-04-29 12:26:51 +00:00
|
|
|
/*
|
2019-06-17 18:27:55 +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-03-29 16:10:31 +00:00
|
|
|
#include "buttons.h"
|
2019-01-25 10:58:23 +00:00
|
|
|
#include "config.h"
|
2019-03-29 16:10:31 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "fsm.h"
|
|
|
|
#include "gettext.h"
|
|
|
|
#include "layout2.h"
|
2018-03-15 18:00:54 +00:00
|
|
|
#include "memory.h"
|
2019-03-29 16:10:31 +00:00
|
|
|
#include "memzero.h"
|
2014-04-29 12:26:51 +00:00
|
|
|
#include "messages.h"
|
2019-03-29 16:10:31 +00:00
|
|
|
#include "messages.pb.h"
|
2014-04-29 12:26:51 +00:00
|
|
|
#include "oled.h"
|
|
|
|
#include "pinmatrix.h"
|
2019-03-29 16:10:31 +00:00
|
|
|
#include "usb.h"
|
2014-04-29 12:26:51 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
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;
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
bool protectButton(ButtonRequestType type, bool confirm_only) {
|
2019-09-30 15:34:38 +00:00
|
|
|
ButtonRequest resp = {0};
|
2019-03-29 16:10:31 +00:00
|
|
|
bool result = false;
|
|
|
|
bool acked = false;
|
2014-06-06 00:45:15 +00:00
|
|
|
#if DEBUG_LINK
|
2019-03-29 16:10:31 +00:00
|
|
|
bool debug_decided = false;
|
2014-06-06 00:45:15 +00:00
|
|
|
#endif
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
memzero(&resp, sizeof(ButtonRequest));
|
|
|
|
resp.has_code = true;
|
|
|
|
resp.code = type;
|
|
|
|
usbTiny(1);
|
|
|
|
buttonUpdate(); // Clear button state
|
|
|
|
msg_write(MessageType_MessageType_ButtonRequest, &resp);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
usbPoll();
|
|
|
|
|
|
|
|
// check for ButtonAck
|
|
|
|
if (msg_tiny_id == MessageType_MessageType_ButtonAck) {
|
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
acked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// button acked - check buttons
|
|
|
|
if (acked) {
|
|
|
|
usbSleep(5);
|
|
|
|
buttonUpdate();
|
|
|
|
if (button.YesUp) {
|
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!confirm_only && button.NoUp) {
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for Cancel / Initialize
|
|
|
|
protectAbortedByCancel = (msg_tiny_id == MessageType_MessageType_Cancel);
|
|
|
|
protectAbortedByInitialize =
|
|
|
|
(msg_tiny_id == MessageType_MessageType_Initialize);
|
|
|
|
if (protectAbortedByCancel || protectAbortedByInitialize) {
|
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
|
|
|
|
#if DEBUG_LINK
|
2019-03-29 16:10:31 +00:00
|
|
|
// check DebugLink
|
|
|
|
if (msg_tiny_id == MessageType_MessageType_DebugLinkDecision) {
|
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
DebugLinkDecision *dld = (DebugLinkDecision *)msg_tiny;
|
|
|
|
result = dld->yes_no;
|
|
|
|
debug_decided = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (acked && debug_decided) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg_tiny_id == MessageType_MessageType_DebugLinkGetState) {
|
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
fsm_msgDebugLinkGetState((DebugLinkGetState *)msg_tiny);
|
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
#endif
|
2019-03-29 16:10:31 +00:00
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
usbTiny(0);
|
2014-04-29 12:26:51 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
return result;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
const char *requestPin(PinMatrixRequestType type, const char *text) {
|
2019-09-30 15:34:38 +00:00
|
|
|
PinMatrixRequest resp = {0};
|
2019-03-29 16:10:31 +00:00
|
|
|
memzero(&resp, sizeof(PinMatrixRequest));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
// check for Cancel / Initialize
|
|
|
|
protectAbortedByCancel = (msg_tiny_id == MessageType_MessageType_Cancel);
|
|
|
|
protectAbortedByInitialize =
|
|
|
|
(msg_tiny_id == MessageType_MessageType_Initialize);
|
|
|
|
if (protectAbortedByCancel || protectAbortedByInitialize) {
|
|
|
|
pinmatrix_done(0);
|
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
usbTiny(0);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
#if DEBUG_LINK
|
2019-03-29 16:10:31 +00:00
|
|
|
if (msg_tiny_id == MessageType_MessageType_DebugLinkGetState) {
|
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
fsm_msgDebugLinkGetState((DebugLinkGetState *)msg_tiny);
|
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
#endif
|
2019-03-29 16:10:31 +00:00
|
|
|
}
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
secbool protectPinUiCallback(uint32_t wait, uint32_t progress,
|
|
|
|
const char *message) {
|
|
|
|
// Convert wait to secstr string.
|
|
|
|
char secstrbuf[] = _("________0 seconds");
|
|
|
|
char *secstr = secstrbuf + 9;
|
|
|
|
uint32_t secs = wait;
|
|
|
|
do {
|
|
|
|
secstr--;
|
|
|
|
*secstr = (secs % 10) + '0';
|
|
|
|
secs /= 10;
|
|
|
|
} while (secs > 0 && secstr >= secstrbuf);
|
|
|
|
if (wait == 1) {
|
|
|
|
// Change "seconds" to "second".
|
|
|
|
secstrbuf[16] = 0;
|
|
|
|
}
|
|
|
|
oledClear();
|
|
|
|
oledDrawStringCenter(OLED_WIDTH / 2, 0 * 9, message, FONT_STANDARD);
|
|
|
|
oledDrawStringCenter(OLED_WIDTH / 2, 2 * 9, _("Please wait"), FONT_STANDARD);
|
|
|
|
oledDrawStringCenter(OLED_WIDTH / 2, 3 * 9, secstr, FONT_STANDARD);
|
|
|
|
oledDrawStringCenter(OLED_WIDTH / 2, 4 * 9, _("to continue ..."),
|
|
|
|
FONT_STANDARD);
|
|
|
|
// progressbar
|
|
|
|
oledFrame(0, OLED_HEIGHT - 8, OLED_WIDTH - 1, OLED_HEIGHT - 1);
|
|
|
|
oledBox(1, OLED_HEIGHT - 7, OLED_WIDTH - 2, OLED_HEIGHT - 2, 0);
|
|
|
|
progress = progress * (OLED_WIDTH - 4) / 1000;
|
|
|
|
if (progress > OLED_WIDTH - 4) {
|
|
|
|
progress = OLED_WIDTH - 4;
|
|
|
|
}
|
|
|
|
oledBox(2, OLED_HEIGHT - 6, 1 + progress, OLED_HEIGHT - 3, 1);
|
|
|
|
oledRefresh();
|
|
|
|
// Check for Cancel / Initialize.
|
|
|
|
protectAbortedByCancel = (msg_tiny_id == MessageType_MessageType_Cancel);
|
|
|
|
protectAbortedByInitialize =
|
|
|
|
(msg_tiny_id == MessageType_MessageType_Initialize);
|
|
|
|
if (protectAbortedByCancel || protectAbortedByInitialize) {
|
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
usbTiny(0);
|
|
|
|
fsm_sendFailure(FailureType_Failure_PinCancelled, NULL);
|
|
|
|
return sectrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return secfalse;
|
2017-07-31 11:02:44 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
bool protectPin(bool use_cached) {
|
|
|
|
if (use_cached && session_isUnlocked()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = config_unlock(pin);
|
|
|
|
if (!ret) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_PinInvalid, NULL);
|
|
|
|
}
|
|
|
|
return ret;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
bool protectChangePin(bool removal) {
|
|
|
|
static CONFIDENTIAL char old_pin[MAX_PIN_LEN + 1] = "";
|
|
|
|
static CONFIDENTIAL char new_pin[MAX_PIN_LEN + 1] = "";
|
|
|
|
const char *pin = NULL;
|
|
|
|
|
|
|
|
if (config_hasPin()) {
|
|
|
|
pin = requestPin(PinMatrixRequestType_PinMatrixRequestType_Current,
|
|
|
|
_("Please enter current PIN:"));
|
|
|
|
if (pin == NULL) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_PinCancelled, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If removing, defer the check to config_changePin().
|
|
|
|
if (!removal) {
|
|
|
|
usbTiny(1);
|
|
|
|
bool ret = config_unlock(pin);
|
|
|
|
usbTiny(0);
|
|
|
|
if (ret == false) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_PinInvalid, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
strlcpy(old_pin, pin, sizeof(old_pin));
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = config_changePin(old_pin, new_pin);
|
|
|
|
memzero(old_pin, sizeof(old_pin));
|
|
|
|
memzero(new_pin, sizeof(new_pin));
|
2019-10-23 17:20:08 +00:00
|
|
|
if (ret == false) {
|
|
|
|
if (removal) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_PinInvalid, NULL);
|
|
|
|
} else {
|
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError,
|
|
|
|
_("The new PIN must be different from your wipe code."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool protectChangeWipeCode(bool removal) {
|
|
|
|
static CONFIDENTIAL char pin[MAX_PIN_LEN + 1] = "";
|
|
|
|
static CONFIDENTIAL char wipe_code[MAX_PIN_LEN + 1] = "";
|
|
|
|
const char *input = NULL;
|
|
|
|
|
|
|
|
if (config_hasPin()) {
|
|
|
|
input = requestPin(PinMatrixRequestType_PinMatrixRequestType_Current,
|
|
|
|
_("Please enter your PIN:"));
|
|
|
|
if (input == NULL) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_PinCancelled, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If removing, defer the check to config_changeWipeCode().
|
|
|
|
if (!removal) {
|
|
|
|
usbTiny(1);
|
|
|
|
bool ret = config_unlock(input);
|
|
|
|
usbTiny(0);
|
|
|
|
if (ret == false) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_PinInvalid, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
strlcpy(pin, input, sizeof(pin));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!removal) {
|
|
|
|
input = requestPin(PinMatrixRequestType_PinMatrixRequestType_WipeCodeFirst,
|
|
|
|
_("Enter new wipe code:"));
|
|
|
|
if (input == NULL) {
|
|
|
|
memzero(pin, sizeof(pin));
|
|
|
|
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (strncmp(pin, input, sizeof(pin)) == 0) {
|
|
|
|
memzero(pin, sizeof(pin));
|
|
|
|
fsm_sendFailure(FailureType_Failure_ProcessError,
|
|
|
|
_("The wipe code must be different from your PIN."));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
strlcpy(wipe_code, input, sizeof(wipe_code));
|
|
|
|
|
|
|
|
input = requestPin(PinMatrixRequestType_PinMatrixRequestType_WipeCodeSecond,
|
|
|
|
_("Re-enter new wipe code:"));
|
|
|
|
if (input == NULL) {
|
|
|
|
memzero(pin, sizeof(pin));
|
|
|
|
memzero(wipe_code, sizeof(wipe_code));
|
|
|
|
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp(wipe_code, input, sizeof(wipe_code)) != 0) {
|
|
|
|
memzero(pin, sizeof(pin));
|
|
|
|
memzero(wipe_code, sizeof(wipe_code));
|
|
|
|
fsm_sendFailure(FailureType_Failure_WipeCodeMismatch, NULL);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = config_changeWipeCode(pin, wipe_code);
|
|
|
|
memzero(pin, sizeof(pin));
|
|
|
|
memzero(wipe_code, sizeof(wipe_code));
|
2019-03-29 16:10:31 +00:00
|
|
|
if (ret == false) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_PinInvalid, NULL);
|
|
|
|
}
|
|
|
|
return ret;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 14:00:07 +00:00
|
|
|
bool protectPassphrase(char *passphrase) {
|
2019-03-29 16:10:31 +00:00
|
|
|
bool passphrase_protection = false;
|
|
|
|
config_getPassphraseProtection(&passphrase_protection);
|
2020-01-20 14:00:07 +00:00
|
|
|
if (!passphrase_protection) {
|
|
|
|
passphrase[0] = '\0';
|
2019-03-29 16:10:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-30 15:34:38 +00:00
|
|
|
PassphraseRequest resp = {0};
|
2019-03-29 16:10:31 +00:00
|
|
|
memzero(&resp, sizeof(PassphraseRequest));
|
|
|
|
usbTiny(1);
|
|
|
|
msg_write(MessageType_MessageType_PassphraseRequest, &resp);
|
|
|
|
|
|
|
|
layoutDialogSwipe(&bmp_icon_info, NULL, NULL, NULL, _("Please enter your"),
|
|
|
|
_("passphrase using"), _("the computer's"), _("keyboard."),
|
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
bool result;
|
|
|
|
for (;;) {
|
|
|
|
usbPoll();
|
|
|
|
if (msg_tiny_id == MessageType_MessageType_PassphraseAck) {
|
|
|
|
msg_tiny_id = 0xFFFF;
|
|
|
|
PassphraseAck *ppa = (PassphraseAck *)msg_tiny;
|
2020-01-19 10:08:53 +00:00
|
|
|
if (ppa->has_on_device && ppa->on_device == true) {
|
|
|
|
fsm_sendFailure(
|
|
|
|
FailureType_Failure_DataError,
|
|
|
|
_("This firmware is incapable of passphrase entry on the device."));
|
2020-01-20 10:22:08 +00:00
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!ppa->has_passphrase) {
|
|
|
|
fsm_sendFailure(FailureType_Failure_DataError,
|
|
|
|
_("No passphrase provided. Use empty string to set an "
|
|
|
|
"empty passphrase."));
|
|
|
|
result = false;
|
|
|
|
break;
|
2020-01-19 10:08:53 +00:00
|
|
|
}
|
2020-01-20 14:00:07 +00:00
|
|
|
// TODO: ask - why ppa->passphrase.size is not working? because of tiny?
|
|
|
|
strlcpy(passphrase, ppa->passphrase, sizeof(ppa->passphrase));
|
2019-03-29 16:10:31 +00:00
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// check for Cancel / Initialize
|
|
|
|
protectAbortedByCancel = (msg_tiny_id == MessageType_MessageType_Cancel);
|
|
|
|
protectAbortedByInitialize =
|
|
|
|
(msg_tiny_id == MessageType_MessageType_Initialize);
|
|
|
|
if (protectAbortedByCancel || protectAbortedByInitialize) {
|
2020-01-28 14:18:00 +00:00
|
|
|
fsm_sendFailure(FailureType_Failure_ActionCancelled, NULL);
|
2019-03-29 16:10:31 +00:00
|
|
|
result = false;
|
2020-01-28 14:18:00 +00:00
|
|
|
msg_tiny_id = 0xFFFF;
|
2019-03-29 16:10:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
usbTiny(0);
|
|
|
|
layoutHome();
|
|
|
|
return result;
|
2014-04-29 12:26:51 +00:00
|
|
|
}
|