Просмотр исходного кода

u2f: check files before register/login (#980)

Nikolay Minaylov 3 лет назад
Родитель
Сommit
40479e1761

+ 6 - 1
applications/u2f/scenes/u2f_scene_main.c

@@ -28,6 +28,8 @@ static void u2f_scene_main_event_callback(U2fNotifyEvent evt, void* context) {
         view_dispatcher_send_custom_event(app->view_dispatcher, U2fCustomEventConnect);
     else if(evt == U2fNotifyDisconnect)
         view_dispatcher_send_custom_event(app->view_dispatcher, U2fCustomEventDisconnect);
+    else if(evt == U2fNotifyError)
+        view_dispatcher_send_custom_event(app->view_dispatcher, U2fCustomEventDataError);
 }
 
 static void u2f_scene_main_timer_callback(void* context) {
@@ -75,10 +77,13 @@ bool u2f_scene_main_on_event(void* context, SceneManagerEvent event) {
             if(app->event_cur != U2fCustomEventNone) {
                 u2f_confirm_user_present(app->u2f_instance);
             }
+        } else if(event.event == U2fCustomEventDataError) {
+            osTimerStop(app->timer);
+            u2f_view_set_state(app->u2f_view, U2fMsgError);
         }
         consumed = true;
-    } else if(event.type == SceneManagerEventTypeTick) {
     }
+
     return consumed;
 }
 

+ 14 - 0
applications/u2f/u2f.c

@@ -186,6 +186,13 @@ static uint16_t u2f_register(U2fData* U2F, uint8_t* buf) {
     uint8_t hash[32];
     uint8_t signature[64];
 
+    if(u2f_data_check(false) == false) {
+        U2F->ready = false;
+        if(U2F->callback != NULL) U2F->callback(U2fNotifyError, U2F->context);
+        memcpy(&buf[0], state_not_supported, 2);
+        return 2;
+    }
+
     if(U2F->callback != NULL) U2F->callback(U2fNotifyRegister, U2F->context);
     if(U2F->user_present == false) {
         memcpy(&buf[0], state_user_missing, 2);
@@ -250,6 +257,13 @@ static uint16_t u2f_authenticate(U2fData* U2F, uint8_t* buf) {
     uint8_t hash[32];
     uint8_t signature[64];
 
+    if(u2f_data_check(false) == false) {
+        U2F->ready = false;
+        if(U2F->callback != NULL) U2F->callback(U2fNotifyError, U2F->context);
+        memcpy(&buf[0], state_not_supported, 2);
+        return 2;
+    }
+
     if(U2F->callback != NULL) U2F->callback(U2fNotifyAuth, U2F->context);
     if(U2F->user_present == true) {
         flags |= 1;

+ 1 - 0
applications/u2f/u2f.h

@@ -13,6 +13,7 @@ typedef enum {
     U2fNotifyWink,
     U2fNotifyConnect,
     U2fNotifyDisconnect,
+    U2fNotifyError,
 } U2fNotifyEvent;
 
 typedef struct U2fData U2fData;

+ 1 - 1
applications/u2f/u2f_app.c

@@ -48,7 +48,7 @@ U2fApp* u2f_app_alloc() {
     view_dispatcher_add_view(
         app->view_dispatcher, U2fAppViewMain, u2f_view_get_view(app->u2f_view));
 
-    if(u2f_data_check()) {
+    if(u2f_data_check(true)) {
         scene_manager_next_scene(app->scene_manager, U2fSceneMain);
     } else {
         scene_manager_next_scene(app->scene_manager, U2fSceneError);

+ 1 - 0
applications/u2f/u2f_app_i.h

@@ -20,6 +20,7 @@ typedef enum {
 
     U2fCustomEventConnect,
     U2fCustomEventDisconnect,
+    U2fCustomEventDataError,
 
     U2fCustomEventRegister,
     U2fCustomEventAuth,

+ 12 - 4
applications/u2f/u2f_data.c

@@ -38,17 +38,25 @@ typedef struct {
     uint32_t control;
 } __attribute__((packed)) U2fCounterData;
 
-bool u2f_data_check() {
+bool u2f_data_check(bool cert_only) {
     bool state = false;
     Storage* fs_api = furi_record_open("storage");
     File* file = storage_file_alloc(fs_api);
 
-    if(storage_file_open(file, U2F_CERT_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) {
+    do {
+        if(!storage_file_open(file, U2F_CERT_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) break;
         storage_file_close(file);
-        if(storage_file_open(file, U2F_CERT_KEY_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) {
+        if(!storage_file_open(file, U2F_CERT_KEY_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) break;
+        if(cert_only) {
             state = true;
+            break;
         }
-    }
+        storage_file_close(file);
+        if(!storage_file_open(file, U2F_KEY_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) break;
+        storage_file_close(file);
+        if(!storage_file_open(file, U2F_CNT_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) break;
+        state = true;
+    } while(0);
 
     storage_file_close(file);
     storage_file_free(file);

+ 1 - 1
applications/u2f/u2f_data.h

@@ -6,7 +6,7 @@ extern "C" {
 
 #include <furi.h>
 
-bool u2f_data_check();
+bool u2f_data_check(bool cert_only);
 
 bool u2f_data_cert_check();