Skip to content

Commit 2bcc073

Browse files
authored
check pin before u2f command (#111)
* feat:cache se status * feat:return to the upper menu after setting the brightness * feat:u2f command enables backlight * feat:check pin before u2f command
1 parent 44cf86e commit 2bcc073

File tree

8 files changed

+110
-8
lines changed

8 files changed

+110
-8
lines changed

legacy/atca/se_atca.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ typedef struct {
1414
} PINCache;
1515

1616
static bool se_has_pin = false;
17+
static bool se_is_init = false;
1718

1819
extern ATCAPairingInfo *pair_info;
1920

@@ -83,7 +84,7 @@ bool se_getSeedStrength(uint32_t *strength) {
8384

8485
void pin_updateCounter(void) { atca_update_counter(); }
8586

86-
bool se_hasPin(void) {
87+
void se_get_status(void) {
8788
ATCAUserState state = {0};
8889

8990
atca_pair_unlock();
@@ -92,6 +93,17 @@ bool se_hasPin(void) {
9293
if (state.pin_set) {
9394
se_has_pin = true;
9495
} else {
96+
se_has_pin = false;
97+
pin_cacheSave(pair_info->init_pin);
98+
}
99+
100+
if (state.initialized) {
101+
se_is_init = true;
102+
}
103+
}
104+
105+
bool se_hasPin(void) {
106+
if (!se_has_pin) {
95107
pin_cacheSave(pair_info->init_pin);
96108
}
97109
return se_has_pin;
@@ -184,13 +196,14 @@ bool se_changePin(const char *old_pin, const char *new_pin) {
184196
}
185197

186198
bool se_isInitialized(void) {
187-
ATCAUserState state = {0};
199+
// ATCAUserState state = {0};
188200

189-
atca_pair_unlock();
201+
// atca_pair_unlock();
190202

191-
atca_read_slot_data(SLOT_USER_SATATE, (uint8_t *)&state);
203+
// atca_read_slot_data(SLOT_USER_SATATE, (uint8_t *)&state);
192204

193-
return state.initialized;
205+
// return state.initialized;
206+
return se_is_init;
194207
}
195208

196209
bool se_importSeed(uint8_t *seed) {
@@ -206,6 +219,7 @@ bool se_importSeed(uint8_t *seed) {
206219
SLOT_IO_PROTECT_KEY)) {
207220
if (!state.initialized) {
208221
state.initialized = true;
222+
se_is_init = true;
209223
atca_pair_unlock();
210224
if (ATCA_SUCCESS ==
211225
atca_write_enc(SLOT_USER_SATATE, 0, (uint8_t *)&state,
@@ -262,6 +276,8 @@ void se_reset_state(void) {
262276
atca_pair_unlock();
263277
atca_write_enc(SLOT_USER_SATATE, 0, (uint8_t *)&zeros, pair_info->protect_key,
264278
SLOT_IO_PROTECT_KEY);
279+
se_has_pin = false;
280+
se_is_init = false;
265281
}
266282

267283
void se_reset_storage(void) {

legacy/atca/se_atca.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ bool se_setSeedStrength(uint32_t strength);
1414
bool se_getSeedStrength(uint32_t *strength);
1515
bool se_importSeed(uint8_t *seed);
1616
bool se_export_seed(uint8_t *seed);
17+
void se_get_status(void);
1718
bool se_hasPin(void);
1819
bool se_verifyPin(const char *pin);
1920
bool se_changePin(const char *old_pin, const char *new_pin);

legacy/firmware/config.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,8 @@ void config_init(void) {
496496
if (se_is_wiping()) {
497497
config_wipe();
498498
}
499+
500+
se_get_status();
499501
#endif
500502

501503
// imported xprv is not supported anymore so we set initialized to false
@@ -1069,7 +1071,9 @@ bool config_unlock(const char *pin) {
10691071
#endif
10701072
{
10711073
if (!storage_is_unlocked()) {
1074+
register_timer("usbpoll", timer1s / 30, usbPoll);
10721075
storage_unlock(PIN_EMPTY, PIN_EMPTY_LEN, NULL);
1076+
unregister_timer("usbpoll");
10731077
}
10741078

10751079
se_unlocked = sectrue;

legacy/firmware/layout2.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,6 +2608,8 @@ void layoutInputPin(uint8_t pos, const char *text, int index,
26082608
char buf[2] = {0};
26092609
int x = 6;
26102610

2611+
layoutLast = layoutInputPin;
2612+
26112613
for (uint8_t i = 0; i < pos; i++) {
26122614
pin_show[i] = '*';
26132615
}

legacy/firmware/menu_list.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,9 +564,9 @@ static struct menu shutdown_set_menu = {
564564

565565
#if ONEKEY_MINI
566566
static struct menu_item brightness_set_menu_items[] = {
567-
{"High", NULL, true, menu_para_set_brightness, NULL, false},
568-
{"Medium", NULL, true, menu_para_set_brightness, NULL, false},
569-
{"Low", NULL, true, menu_para_set_brightness, NULL, false}};
567+
{"High", NULL, true, menu_para_set_brightness, NULL, true},
568+
{"Medium", NULL, true, menu_para_set_brightness, NULL, true},
569+
{"Low", NULL, true, menu_para_set_brightness, NULL, true}};
570570

571571
static struct menu brightness_set_menu = {
572572
.start = 0,

legacy/firmware/protect.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,8 @@ uint8_t blindsignWaitKey(void) {
766766
return key;
767767
}
768768

769+
extern bool u2f_init_command;
770+
769771
uint8_t protectWaitKey(uint32_t time_out, uint8_t mode) {
770772
uint8_t key = KEY_NULL;
771773

@@ -782,6 +784,13 @@ uint8_t protectWaitKey(uint32_t time_out, uint8_t mode) {
782784
msg_tiny_id = 0xFFFF;
783785
break;
784786
}
787+
if (layoutLast == layoutScreensaver) {
788+
if (u2f_init_command) {
789+
u2f_init_command = false;
790+
break;
791+
}
792+
}
793+
785794
key = keyScan();
786795
if (key != KEY_NULL) {
787796
if (device_sleep_state) device_sleep_state = SLEEP_CANCEL_BY_BUTTON;

legacy/firmware/u2f.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "hmac.h"
3131
#include "layout2.h"
3232
#include "memzero.h"
33+
#include "protect.h"
3334
#if !EMULATOR
3435
#include "mi2c.h"
3536
#endif
@@ -89,6 +90,11 @@ typedef enum {
8990

9091
static U2F_STATE last_req_state = INIT;
9192

93+
static bool input_pin = false;
94+
static bool first_package = true;
95+
static uint32_t package_len = 0, rec_len = 0;
96+
bool u2f_init_command = false;
97+
9298
typedef struct {
9399
uint8_t reserved;
94100
uint8_t appId[U2F_APPID_SIZE];
@@ -132,6 +138,7 @@ U2F_ReadBuffer *reader;
132138
void u2fhid_read(char tiny, const U2FHID_FRAME *f) {
133139
// Always handle init packets directly
134140
if (f->init.cmd == U2FHID_INIT) {
141+
u2f_init_command = true;
135142
u2fhid_init(f);
136143
if (tiny && reader && f->cid == cid) {
137144
// abort current channel
@@ -142,6 +149,24 @@ void u2fhid_read(char tiny, const U2FHID_FRAME *f) {
142149
return;
143150
}
144151

152+
if (layoutLast == layoutInputPin) {
153+
if (first_package) {
154+
first_package = false;
155+
package_len = MSG_LEN(*f);
156+
rec_len = sizeof(f->cont.data);
157+
while (rec_len < package_len) {
158+
usbPoll();
159+
}
160+
send_u2f_error(U2F_SW_CONDITIONS_NOT_SATISFIED);
161+
first_package = true;
162+
return;
163+
} else {
164+
rec_len += sizeof(f->cont.data);
165+
return;
166+
}
167+
return;
168+
}
169+
145170
if (tiny) {
146171
// read continue packet
147172
if (reader == 0 || cid != f->cid) {
@@ -259,6 +284,10 @@ void u2fhid_read_start(const U2FHID_FRAME *f) {
259284
// standard requires to remember button press for 10 seconds.
260285
dialog_timeout = 10 * U2F_TIMEOUT;
261286
}
287+
if (reader == 0) {
288+
layoutHome();
289+
return;
290+
}
262291
}
263292

264293
if (reader->cmd == 0) {
@@ -632,6 +661,20 @@ void u2f_register(const APDU *a) {
632661
return;
633662
}
634663

664+
if (!session_isUnlocked()) {
665+
input_pin = true;
666+
send_u2f_error(U2F_SW_CONDITIONS_NOT_SATISFIED);
667+
}
668+
669+
protectPinOnDevice(true, true);
670+
671+
if (input_pin) {
672+
input_pin = false;
673+
last_req_state = REG;
674+
dialog_timeout = U2F_TIMEOUT;
675+
return;
676+
}
677+
635678
// Validate basic request parameters
636679
debugLog(0, "", "u2f register");
637680
if (APDU_LEN(*a) != sizeof(U2F_REGISTER_REQ)) {
@@ -775,6 +818,20 @@ void u2f_authenticate(const APDU *a) {
775818
return;
776819
}
777820

821+
if (!session_isUnlocked()) {
822+
input_pin = true;
823+
send_u2f_error(U2F_SW_CONDITIONS_NOT_SATISFIED);
824+
}
825+
826+
protectPinOnDevice(true, true);
827+
828+
if (input_pin) {
829+
input_pin = false;
830+
last_req_state = AUTH;
831+
dialog_timeout = U2F_TIMEOUT;
832+
return;
833+
}
834+
778835
if (a->p1 == U2F_AUTH_CHECK_ONLY) {
779836
debugLog(0, "", "u2f authenticate check");
780837
// This is a success for a good keyhandle

legacy/firmware/usb.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
#include "webusb.h"
4545
#include "winusb.h"
4646

47+
#if ONEKEY_MINI
48+
#include <libopencm3/stm32/timer.h>
49+
#endif
50+
4751
#define USB_INTERFACE_INDEX_MAIN 0
4852
#if DEBUG_LINK
4953
#define USB_INTERFACE_INDEX_DEBUG 1
@@ -320,6 +324,11 @@ static void u2f_rx_callback(usbd_device *dev, uint8_t ep) {
320324
if (usbd_ep_read_packet(dev, ENDPOINT_ADDRESS_U2F_OUT, buf, sizeof(buf)) !=
321325
USB_PACKET_SIZE)
322326
return;
327+
timer_sleep_start_reset();
328+
#if ONEKEY_MINI
329+
// open back light
330+
timer_enable_oc_output(TIM3, TIM_OC2);
331+
#endif
323332
u2fhid_read(tiny, (const U2FHID_FRAME *)(void *)buf);
324333
}
325334

@@ -338,6 +347,10 @@ static void main_rx_callback(usbd_device *dev, uint8_t ep) {
338347
host_channel = CHANNEL_SLAVE;
339348
}
340349
timer_sleep_start_reset();
350+
#if ONEKEY_MINI
351+
// open back light
352+
timer_enable_oc_output(TIM3, TIM_OC2);
353+
#endif
341354
debugLog(0, "", "main_rx_callback");
342355
if (!tiny) {
343356
msg_read(buf, sizeof(buf));

0 commit comments

Comments
 (0)