1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-17 03:48:09 +00:00
trezor-firmware/firmware/protect.c

280 lines
7.4 KiB
C
Raw Normal View History

2014-04-29 12:26:51 +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"
#include "storage.h"
#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"
#include "gettext.h"
#include "memzero.h"
#include "messages.pb.h"
2014-04-29 12:26:51 +00:00
#define MAX_WRONG_PINS 15
bool protectAbortedByCancel = false;
bool protectAbortedByInitialize = false;
bool protectButton(ButtonRequest_ButtonRequestType type, bool confirm_only)
2014-04-29 12:26:51 +00:00
{
ButtonRequest resp;
bool result = false;
2014-04-29 12:26:51 +00:00
bool acked = false;
#if DEBUG_LINK
bool debug_decided = false;
#endif
2014-04-29 12:26:51 +00:00
memset(&resp, 0, sizeof(ButtonRequest));
resp.has_code = true;
resp.code = type;
usbTiny(1);
buttonUpdate(); // Clear button state
2014-04-29 12:26:51 +00:00
msg_write(MessageType_MessageType_ButtonRequest, &resp);
for (;;) {
usbPoll();
// 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) {
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;
}
}
// 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;
}
#if DEBUG_LINK
// 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;
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;
}
const char *requestPin(PinMatrixRequest_PinMatrixRequestType type, const char *text)
2014-04-29 12:26:51 +00:00
{
PinMatrixRequest resp;
memset(&resp, 0, 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) {
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
}
}
static void protectCheckMaxTry(uint32_t wait) {
if (wait < (1 << MAX_WRONG_PINS))
return;
storage_wipe();
layoutDialog(&bmp_icon_error, NULL, NULL, NULL, _("Too many wrong PIN"), _("attempts. Storage has"), _("been wiped."), NULL, _("Please unplug"), _("the device."));
for (;;) {} // loop forever
}
2014-04-29 12:26:51 +00:00
bool protectPin(bool use_cached)
{
if (!storage_hasPin() || (use_cached && session_isPinCached())) {
2014-04-29 12:26:51 +00:00
return true;
}
uint32_t fails = storage_getPinFailsOffset();
2018-03-22 19:12:01 +00:00
uint32_t wait = storage_getPinWait(fails);
protectCheckMaxTry(wait);
2016-05-28 14:24:14 +00:00
usbTiny(1);
while (wait > 0) {
// convert wait to secstr string
char secstrbuf[20];
strlcpy(secstrbuf, _("________0 seconds"), sizeof(secstrbuf));
char *secstr = secstrbuf + 9;
uint32_t secs = wait;
while (secs > 0 && secstr >= secstrbuf) {
secstr--;
*secstr = (secs % 10) + '0';
secs /= 10;
}
if (wait == 1) {
secstrbuf[16] = 0;
2014-04-29 12:26:51 +00:00
}
layoutDialog(&bmp_icon_info, NULL, NULL, NULL, _("Wrong PIN entered"), NULL, _("Please wait"), secstr, _("to continue ..."), NULL);
// wait one second
usbSleep(1000);
2016-05-28 14:24:14 +00:00
if (msg_tiny_id == MessageType_MessageType_Initialize) {
protectAbortedByCancel = false;
2016-05-28 14:24:14 +00:00
protectAbortedByInitialize = true;
msg_tiny_id = 0xFFFF;
usbTiny(0);
fsm_sendFailure(Failure_FailureType_Failure_PinCancelled, NULL);
2016-05-28 14:24:14 +00:00
return false;
}
wait--;
2014-04-29 12:26:51 +00:00
}
2016-05-28 14:24:14 +00:00
usbTiny(0);
2015-01-26 21:03:33 +00:00
const char *pin;
pin = requestPin(PinMatrixRequest_PinMatrixRequestType_PinMatrixRequestType_Current, _("Please enter current PIN:"));
2014-04-29 12:26:51 +00:00
if (!pin) {
fsm_sendFailure(Failure_FailureType_Failure_PinCancelled, NULL);
2014-04-29 12:26:51 +00:00
return false;
}
if (!storage_increasePinFails(fails)) {
fsm_sendFailure(Failure_FailureType_Failure_PinInvalid, NULL);
return false;
}
if (storage_containsPin(pin)) {
session_cachePin();
storage_resetPinFails(fails);
2014-04-29 12:26:51 +00:00
return true;
} else {
2018-03-22 19:12:01 +00:00
protectCheckMaxTry(storage_getPinWait(fails));
fsm_sendFailure(Failure_FailureType_Failure_PinInvalid, NULL);
return false;
2014-04-29 12:26:51 +00:00
}
}
bool protectChangePin(void)
{
static CONFIDENTIAL char pin_compare[17];
const char *pin = requestPin(PinMatrixRequest_PinMatrixRequestType_PinMatrixRequestType_NewFirst, _("Please enter new PIN:"));
2014-04-29 12:26:51 +00:00
if (!pin) {
return false;
}
strlcpy(pin_compare, pin, sizeof(pin_compare));
pin = requestPin(PinMatrixRequest_PinMatrixRequestType_PinMatrixRequestType_NewSecond, _("Please re-enter new PIN:"));
const bool result = pin && (strncmp(pin_compare, pin, sizeof(pin_compare)) == 0);
if (result) {
storage_setPin(pin_compare);
storage_update();
2014-04-29 12:26:51 +00:00
}
memzero(pin_compare, sizeof(pin_compare));
return result;
2014-04-29 12:26:51 +00:00
}
bool protectPassphrase(void)
{
if (!storage_hasPassphraseProtection() || session_isPassphraseCached()) {
2014-04-29 12:26:51 +00:00
return true;
}
PassphraseRequest resp;
memset(&resp, 0, 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);
2014-04-29 12:26:51 +00:00
bool result;
for (;;) {
usbPoll();
// 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;
}
// 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;
}