From d2c4f15af5f79756343df95640fad404ee1af6f8 Mon Sep 17 00:00:00 2001 From: Nikolay Minaylov Date: Wed, 2 Feb 2022 17:27:33 +0300 Subject: [PATCH] [FL-2226] BadUSB, U2F: missing assets error (#979) * badusb, u2f: showing error screen if assets are missing * BadUsb: remove dead code * U2F: remove dead code Co-authored-by: Aleksandr Kutuzov --- applications/bad_usb/bad_usb_app.c | 34 +++++++++++- applications/bad_usb/bad_usb_app_i.h | 3 ++ applications/bad_usb/bad_usb_script.c | 2 - .../bad_usb/scenes/bad_usb_scene_config.h | 1 + .../bad_usb/scenes/bad_usb_scene_error.c | 53 +++++++++++++++++++ applications/u2f/scenes/u2f_scene_config.h | 1 + applications/u2f/scenes/u2f_scene_error.c | 49 +++++++++++++++++ applications/u2f/u2f_app.c | 15 +++++- applications/u2f/u2f_app_i.h | 5 ++ applications/u2f/u2f_data.c | 20 +++++++ applications/u2f/u2f_data.h | 2 + applications/u2f/views/u2f_view.c | 2 +- 12 files changed, 182 insertions(+), 5 deletions(-) create mode 100644 applications/bad_usb/scenes/bad_usb_scene_error.c create mode 100644 applications/u2f/scenes/u2f_scene_error.c diff --git a/applications/bad_usb/bad_usb_app.c b/applications/bad_usb/bad_usb_app.c index 4f94598e..c4a3c930 100644 --- a/applications/bad_usb/bad_usb_app.c +++ b/applications/bad_usb/bad_usb_app.c @@ -1,6 +1,7 @@ #include "bad_usb_app_i.h" #include #include +#include static bool bad_usb_app_custom_event_callback(void* context, uint32_t event) { furi_assert(context); @@ -20,6 +21,24 @@ static void bad_usb_app_tick_event_callback(void* context) { scene_manager_handle_tick_event(app->scene_manager); } +static bool bad_usb_check_assets() { + Storage* fs_api = furi_record_open("storage"); + + File* dir = storage_file_alloc(fs_api); + bool ret = false; + + if(storage_dir_open(dir, BAD_USB_APP_PATH_FOLDER)) { + ret = true; + } + + storage_dir_close(dir); + storage_file_free(dir); + + furi_record_close("storage"); + + return ret; +} + BadUsbApp* bad_usb_app_alloc() { BadUsbApp* app = furi_alloc(sizeof(BadUsbApp)); @@ -41,11 +60,20 @@ BadUsbApp* bad_usb_app_alloc() { view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen); + // Custom Widget + app->widget = widget_alloc(); + view_dispatcher_add_view( + app->view_dispatcher, BadUsbAppViewError, widget_get_view(app->widget)); + app->bad_usb_view = bad_usb_alloc(); view_dispatcher_add_view( app->view_dispatcher, BadUsbAppViewWork, bad_usb_get_view(app->bad_usb_view)); - scene_manager_next_scene(app->scene_manager, BadUsbAppViewFileSelect); + if(bad_usb_check_assets()) { + scene_manager_next_scene(app->scene_manager, BadUsbSceneFileSelect); + } else { + scene_manager_next_scene(app->scene_manager, BadUsbSceneError); + } return app; } @@ -58,6 +86,10 @@ void bad_usb_app_free(BadUsbApp* app) { view_dispatcher_remove_view(app->view_dispatcher, BadUsbAppViewWork); bad_usb_free(app->bad_usb_view); + // Custom Widget + view_dispatcher_remove_view(app->view_dispatcher, BadUsbAppViewError); + widget_free(app->widget); + // View dispatcher view_dispatcher_free(app->view_dispatcher); scene_manager_free(app->scene_manager); diff --git a/applications/bad_usb/bad_usb_app_i.h b/applications/bad_usb/bad_usb_app_i.h index 2806cd70..5772725d 100644 --- a/applications/bad_usb/bad_usb_app_i.h +++ b/applications/bad_usb/bad_usb_app_i.h @@ -11,6 +11,7 @@ #include #include #include +#include #include "views/bad_usb_view.h" #define BAD_USB_APP_PATH_FOLDER "/any/badusb" @@ -23,6 +24,7 @@ struct BadUsbApp { SceneManager* scene_manager; NotificationApp* notifications; DialogsApp* dialogs; + Widget* widget; char file_name[BAD_USB_FILE_NAME_LEN + 1]; BadUsb* bad_usb_view; @@ -30,6 +32,7 @@ struct BadUsbApp { }; typedef enum { + BadUsbAppViewError, BadUsbAppViewFileSelect, BadUsbAppViewWork, } BadUsbAppView; diff --git a/applications/bad_usb/bad_usb_script.c b/applications/bad_usb/bad_usb_script.c index 6cf460df..53710372 100644 --- a/applications/bad_usb/bad_usb_script.c +++ b/applications/bad_usb/bad_usb_script.c @@ -159,8 +159,6 @@ static bool ducky_altchar(const char* charcode) { uint8_t i = 0; bool state = false; - //TODO: numlock - FURI_LOG_I(WORKER_TAG, "char %s", charcode); furi_hal_hid_kb_press(KEY_MOD_LEFT_ALT); diff --git a/applications/bad_usb/scenes/bad_usb_scene_config.h b/applications/bad_usb/scenes/bad_usb_scene_config.h index b32ecb91..0ab8f54f 100644 --- a/applications/bad_usb/scenes/bad_usb_scene_config.h +++ b/applications/bad_usb/scenes/bad_usb_scene_config.h @@ -1,2 +1,3 @@ ADD_SCENE(bad_usb, file_select, FileSelect) ADD_SCENE(bad_usb, work, Work) +ADD_SCENE(bad_usb, error, Error) diff --git a/applications/bad_usb/scenes/bad_usb_scene_error.c b/applications/bad_usb/scenes/bad_usb_scene_error.c new file mode 100644 index 00000000..6419ee37 --- /dev/null +++ b/applications/bad_usb/scenes/bad_usb_scene_error.c @@ -0,0 +1,53 @@ +#include "../bad_usb_app_i.h" + +typedef enum { + SubghzCustomEventErrorBack, +} BadUsbCustomEvent; + +static void + bad_usb_scene_error_event_callback(GuiButtonType result, InputType type, void* context) { + furi_assert(context); + BadUsbApp* app = context; + + if((result == GuiButtonTypeLeft) && (type == InputTypeShort)) { + view_dispatcher_send_custom_event(app->view_dispatcher, SubghzCustomEventErrorBack); + } +} + +void bad_usb_scene_error_on_enter(void* context) { + BadUsbApp* app = context; + + widget_add_icon_element(app->widget, 0, 0, &I_SDQuestion_35x43); + + widget_add_string_multiline_element( + app->widget, + 81, + 4, + AlignCenter, + AlignTop, + FontSecondary, + "No SD card or\napp data found.\nThis app will not\nwork without\nrequired files."); + + widget_add_button_element( + app->widget, GuiButtonTypeLeft, "Back", bad_usb_scene_error_event_callback, app); + + view_dispatcher_switch_to_view(app->view_dispatcher, BadUsbAppViewError); +} + +bool bad_usb_scene_error_on_event(void* context, SceneManagerEvent event) { + BadUsbApp* app = context; + bool consumed = false; + + if(event.type == SceneManagerEventTypeCustom) { + if(event.event == SubghzCustomEventErrorBack) { + view_dispatcher_stop(app->view_dispatcher); + consumed = true; + } + } + return consumed; +} + +void bad_usb_scene_error_on_exit(void* context) { + BadUsbApp* app = context; + widget_clear(app->widget); +} diff --git a/applications/u2f/scenes/u2f_scene_config.h b/applications/u2f/scenes/u2f_scene_config.h index 636589f2..7bacd67b 100644 --- a/applications/u2f/scenes/u2f_scene_config.h +++ b/applications/u2f/scenes/u2f_scene_config.h @@ -1 +1,2 @@ ADD_SCENE(u2f, main, Main) +ADD_SCENE(u2f, error, Error) diff --git a/applications/u2f/scenes/u2f_scene_error.c b/applications/u2f/scenes/u2f_scene_error.c new file mode 100644 index 00000000..d0338568 --- /dev/null +++ b/applications/u2f/scenes/u2f_scene_error.c @@ -0,0 +1,49 @@ +#include "../u2f_app_i.h" + +static void u2f_scene_error_event_callback(GuiButtonType result, InputType type, void* context) { + furi_assert(context); + U2fApp* app = context; + + if((result == GuiButtonTypeLeft) && (type == InputTypeShort)) { + view_dispatcher_send_custom_event(app->view_dispatcher, U2fCustomEventErrorBack); + } +} + +void u2f_scene_error_on_enter(void* context) { + U2fApp* app = context; + + widget_add_icon_element(app->widget, 0, 0, &I_SDQuestion_35x43); + + widget_add_string_multiline_element( + app->widget, + 81, + 4, + AlignCenter, + AlignTop, + FontSecondary, + "No SD card or\napp data found.\nThis app will not\nwork without\nrequired files."); + + widget_add_button_element( + app->widget, GuiButtonTypeLeft, "Back", u2f_scene_error_event_callback, app); + + view_dispatcher_switch_to_view(app->view_dispatcher, U2fAppViewError); +} + +bool u2f_scene_error_on_event(void* context, SceneManagerEvent event) { + U2fApp* app = context; + bool consumed = false; + + if(event.type == SceneManagerEventTypeCustom) { + if(event.event == U2fCustomEventErrorBack) { + view_dispatcher_stop(app->view_dispatcher); + consumed = true; + } + } + + return consumed; +} + +void u2f_scene_error_on_exit(void* context) { + U2fApp* app = context; + widget_clear(app->widget); +} diff --git a/applications/u2f/u2f_app.c b/applications/u2f/u2f_app.c index c512e531..f2e55730 100644 --- a/applications/u2f/u2f_app.c +++ b/applications/u2f/u2f_app.c @@ -1,4 +1,5 @@ #include "u2f_app_i.h" +#include "u2f_data.h" #include #include @@ -39,11 +40,19 @@ U2fApp* u2f_app_alloc() { view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen); + // Custom Widget + app->widget = widget_alloc(); + view_dispatcher_add_view(app->view_dispatcher, U2fAppViewError, widget_get_view(app->widget)); + app->u2f_view = u2f_view_alloc(); view_dispatcher_add_view( app->view_dispatcher, U2fAppViewMain, u2f_view_get_view(app->u2f_view)); - scene_manager_next_scene(app->scene_manager, U2fAppViewMain); + if(u2f_data_check()) { + scene_manager_next_scene(app->scene_manager, U2fSceneMain); + } else { + scene_manager_next_scene(app->scene_manager, U2fSceneError); + } return app; } @@ -55,6 +64,10 @@ void u2f_app_free(U2fApp* app) { view_dispatcher_remove_view(app->view_dispatcher, U2fAppViewMain); u2f_view_free(app->u2f_view); + // Custom Widget + view_dispatcher_remove_view(app->view_dispatcher, U2fAppViewError); + widget_free(app->widget); + // View dispatcher view_dispatcher_free(app->view_dispatcher); scene_manager_free(app->scene_manager); diff --git a/applications/u2f/u2f_app_i.h b/applications/u2f/u2f_app_i.h index 6bb95a03..33092ba3 100644 --- a/applications/u2f/u2f_app_i.h +++ b/applications/u2f/u2f_app_i.h @@ -10,6 +10,7 @@ #include #include #include +#include #include "views/u2f_view.h" #include "u2f_hid.h" #include "u2f.h" @@ -29,9 +30,12 @@ typedef enum { U2fCustomEventConfirm, + U2fCustomEventErrorBack, + } GpioCustomEvent; typedef enum { + U2fAppViewError, U2fAppViewMain, } U2fAppView; @@ -40,6 +44,7 @@ struct U2fApp { ViewDispatcher* view_dispatcher; SceneManager* scene_manager; NotificationApp* notifications; + Widget* widget; osTimerId_t timer; U2fHid* u2f_hid; U2fView* u2f_view; diff --git a/applications/u2f/u2f_data.c b/applications/u2f/u2f_data.c index 6f4cbbdf..1de86164 100644 --- a/applications/u2f/u2f_data.c +++ b/applications/u2f/u2f_data.c @@ -38,6 +38,26 @@ typedef struct { uint32_t control; } __attribute__((packed)) U2fCounterData; +bool u2f_data_check() { + 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)) { + storage_file_close(file); + if(storage_file_open(file, U2F_CERT_KEY_FILE, FSAM_READ, FSOM_OPEN_EXISTING)) { + state = true; + } + } + + storage_file_close(file); + storage_file_free(file); + + furi_record_close("storage"); + + return state; +} + bool u2f_data_cert_check() { bool state = false; Storage* fs_api = furi_record_open("storage"); diff --git a/applications/u2f/u2f_data.h b/applications/u2f/u2f_data.h index e47e26a1..a96704eb 100644 --- a/applications/u2f/u2f_data.h +++ b/applications/u2f/u2f_data.h @@ -6,6 +6,8 @@ extern "C" { #include +bool u2f_data_check(); + bool u2f_data_cert_check(); uint32_t u2f_data_cert_load(uint8_t* cert); diff --git a/applications/u2f/views/u2f_view.c b/applications/u2f/views/u2f_view.c index 20c34e9c..c810d141 100644 --- a/applications/u2f/views/u2f_view.c +++ b/applications/u2f/views/u2f_view.c @@ -39,7 +39,7 @@ static void u2f_view_draw_callback(Canvas* canvas, void* _model) { canvas, 128 / 2, 3, AlignCenter, AlignTop, "Authentication successfull!"); } else if(model->display_msg == U2fMsgError) { canvas_draw_icon(canvas, 22, 15, &I_Error_62x31); - canvas_draw_str_aligned(canvas, 128 / 2, 3, AlignCenter, AlignTop, "Ceritficate missing"); + canvas_draw_str_aligned(canvas, 128 / 2, 3, AlignCenter, AlignTop, "Ceritficate error"); } }