2022-10-12 02:01:37 +00:00
|
|
|
#include <gui/gui.h>
|
|
|
|
#include <input/input.h>
|
|
|
|
#include <dialogs/dialogs.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <notification/notification.h>
|
|
|
|
#include <notification/notification_messages.h>
|
2023-02-13 19:06:01 +00:00
|
|
|
#include <dolphin/dolphin.h>
|
2023-03-21 16:03:53 +00:00
|
|
|
#include "features_config.h"
|
2022-10-12 02:01:37 +00:00
|
|
|
#include "services/config/config.h"
|
|
|
|
#include "types/plugin_state.h"
|
|
|
|
#include "types/token_info.h"
|
|
|
|
#include "types/plugin_event.h"
|
|
|
|
#include "types/event_type.h"
|
|
|
|
#include "types/common.h"
|
2022-11-23 22:19:19 +00:00
|
|
|
#include "ui/scene_director.h"
|
|
|
|
#include "ui/constants.h"
|
2022-12-26 20:31:38 +00:00
|
|
|
#include "ui/common_dialogs.h"
|
2022-10-17 18:22:34 +00:00
|
|
|
#include "services/crypto/crypto.h"
|
2022-11-23 22:19:19 +00:00
|
|
|
#include "cli/cli.h"
|
2022-10-12 02:01:37 +00:00
|
|
|
|
2023-04-13 18:20:29 +00:00
|
|
|
#define IDLE_TIMEOUT (60000)
|
2022-10-12 02:01:37 +00:00
|
|
|
|
|
|
|
static void render_callback(Canvas* const canvas, void* ctx) {
|
2023-03-07 21:18:23 +00:00
|
|
|
furi_assert(ctx);
|
|
|
|
PluginState* plugin_state = ctx;
|
2023-03-09 15:40:09 +00:00
|
|
|
if(furi_mutex_acquire(plugin_state->mutex, 25) == FuriStatusOk) {
|
2022-10-12 02:01:37 +00:00
|
|
|
totp_scene_director_render(canvas, plugin_state);
|
2023-03-09 15:40:09 +00:00
|
|
|
furi_mutex_release(plugin_state->mutex);
|
2022-10-12 02:01:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
|
|
|
|
furi_assert(event_queue);
|
|
|
|
|
|
|
|
PluginEvent event = {.type = EventTypeKey, .input = *input_event};
|
|
|
|
furi_message_queue_put(event_queue, &event, FuriWaitForever);
|
|
|
|
}
|
|
|
|
|
2022-12-26 20:31:38 +00:00
|
|
|
static bool totp_activate_initial_scene(PluginState* const plugin_state) {
|
2022-11-02 20:39:54 +00:00
|
|
|
if(plugin_state->crypto_verify_data == NULL) {
|
2022-10-17 18:22:34 +00:00
|
|
|
DialogMessage* message = dialog_message_alloc();
|
|
|
|
dialog_message_set_buttons(message, "No", NULL, "Yes");
|
2022-11-02 20:39:54 +00:00
|
|
|
dialog_message_set_text(
|
|
|
|
message,
|
|
|
|
"Would you like to setup PIN?",
|
|
|
|
SCREEN_WIDTH_CENTER,
|
|
|
|
SCREEN_HEIGHT_CENTER,
|
|
|
|
AlignCenter,
|
|
|
|
AlignCenter);
|
2022-11-23 22:19:19 +00:00
|
|
|
DialogMessageButton dialog_result =
|
|
|
|
dialog_message_show(plugin_state->dialogs_app, message);
|
2022-10-17 18:22:34 +00:00
|
|
|
dialog_message_free(message);
|
2022-11-02 20:39:54 +00:00
|
|
|
if(dialog_result == DialogMessageButtonRight) {
|
2023-04-26 20:50:37 +00:00
|
|
|
totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication);
|
2022-10-17 18:22:34 +00:00
|
|
|
} else {
|
2023-04-26 20:50:37 +00:00
|
|
|
CryptoSeedIVResult seed_result = totp_crypto_seed_iv(plugin_state, NULL, 0);
|
|
|
|
if(seed_result & CryptoSeedIVResultFlagSuccess &&
|
|
|
|
seed_result & CryptoSeedIVResultFlagNewCryptoVerifyData) {
|
|
|
|
if(!totp_config_file_update_crypto_signatures(plugin_state)) {
|
|
|
|
totp_dialogs_config_loading_error(plugin_state);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if(seed_result == CryptoSeedIVResultFailed) {
|
2022-12-26 20:31:38 +00:00
|
|
|
totp_dialogs_config_loading_error(plugin_state);
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-26 20:50:37 +00:00
|
|
|
|
|
|
|
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
|
2022-10-17 18:22:34 +00:00
|
|
|
}
|
2022-11-02 20:39:54 +00:00
|
|
|
} else if(plugin_state->pin_set) {
|
2023-04-26 20:50:37 +00:00
|
|
|
totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication);
|
2022-10-17 18:22:34 +00:00
|
|
|
} else {
|
2023-04-26 20:50:37 +00:00
|
|
|
CryptoSeedIVResult seed_result = totp_crypto_seed_iv(plugin_state, NULL, 0);
|
|
|
|
if(seed_result & CryptoSeedIVResultFlagSuccess &&
|
|
|
|
seed_result & CryptoSeedIVResultFlagNewCryptoVerifyData) {
|
|
|
|
if(!totp_config_file_update_crypto_signatures(plugin_state)) {
|
|
|
|
totp_dialogs_config_loading_error(plugin_state);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if(seed_result == CryptoSeedIVResultFailed) {
|
2022-12-26 20:31:38 +00:00
|
|
|
totp_dialogs_config_loading_error(plugin_state);
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-26 20:50:37 +00:00
|
|
|
|
2022-11-02 20:39:54 +00:00
|
|
|
if(totp_crypto_verify_key(plugin_state)) {
|
2023-04-26 20:50:37 +00:00
|
|
|
totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
|
2022-10-17 18:22:34 +00:00
|
|
|
} else {
|
2022-11-02 20:39:54 +00:00
|
|
|
FURI_LOG_E(
|
|
|
|
LOGGING_TAG,
|
|
|
|
"Digital signature verification failed. Looks like conf file was created on another flipper and can't be used on any other");
|
2022-10-17 18:22:34 +00:00
|
|
|
DialogMessage* message = dialog_message_alloc();
|
|
|
|
dialog_message_set_buttons(message, "Exit", NULL, NULL);
|
2022-11-02 20:39:54 +00:00
|
|
|
dialog_message_set_text(
|
|
|
|
message,
|
|
|
|
"Digital signature verification failed",
|
|
|
|
SCREEN_WIDTH_CENTER,
|
|
|
|
SCREEN_HEIGHT_CENTER,
|
|
|
|
AlignCenter,
|
|
|
|
AlignCenter);
|
2022-11-23 22:19:19 +00:00
|
|
|
dialog_message_show(plugin_state->dialogs_app, message);
|
2022-10-17 18:22:34 +00:00
|
|
|
dialog_message_free(message);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2022-10-12 02:01:37 +00:00
|
|
|
}
|
|
|
|
|
2022-12-26 20:31:38 +00:00
|
|
|
static bool totp_plugin_state_init(PluginState* const plugin_state) {
|
|
|
|
plugin_state->gui = furi_record_open(RECORD_GUI);
|
|
|
|
plugin_state->notification_app = furi_record_open(RECORD_NOTIFICATION);
|
|
|
|
plugin_state->dialogs_app = furi_record_open(RECORD_DIALOGS);
|
2023-04-13 18:20:29 +00:00
|
|
|
memset(&plugin_state->iv[0], 0, TOTP_IV_SIZE);
|
2022-10-28 15:34:35 +00:00
|
|
|
|
2023-04-26 20:50:37 +00:00
|
|
|
if(!totp_config_file_load(plugin_state)) {
|
2022-12-26 20:31:38 +00:00
|
|
|
totp_dialogs_config_loading_error(plugin_state);
|
|
|
|
return false;
|
|
|
|
}
|
2022-10-12 02:01:37 +00:00
|
|
|
|
2023-03-09 15:40:09 +00:00
|
|
|
plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
|
|
|
|
|
2023-03-21 16:03:53 +00:00
|
|
|
#ifdef TOTP_BADBT_TYPE_ENABLED
|
|
|
|
if(plugin_state->automation_method & AutomationMethodBadBt) {
|
|
|
|
plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init();
|
|
|
|
} else {
|
|
|
|
plugin_state->bt_type_code_worker_context = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-12-26 20:31:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-10-12 02:01:37 +00:00
|
|
|
|
2022-12-26 20:31:38 +00:00
|
|
|
static void totp_plugin_state_free(PluginState* plugin_state) {
|
2022-10-12 02:01:37 +00:00
|
|
|
furi_record_close(RECORD_GUI);
|
|
|
|
furi_record_close(RECORD_NOTIFICATION);
|
|
|
|
furi_record_close(RECORD_DIALOGS);
|
|
|
|
|
2023-04-26 20:50:37 +00:00
|
|
|
totp_config_file_close(plugin_state);
|
2022-10-12 02:01:37 +00:00
|
|
|
|
2022-11-02 20:39:54 +00:00
|
|
|
if(plugin_state->crypto_verify_data != NULL) {
|
2022-10-12 02:01:37 +00:00
|
|
|
free(plugin_state->crypto_verify_data);
|
|
|
|
}
|
2023-03-09 15:40:09 +00:00
|
|
|
|
2023-03-21 16:03:53 +00:00
|
|
|
#ifdef TOTP_BADBT_TYPE_ENABLED
|
|
|
|
if(plugin_state->bt_type_code_worker_context != NULL) {
|
|
|
|
totp_bt_type_code_worker_free(plugin_state->bt_type_code_worker_context);
|
|
|
|
plugin_state->bt_type_code_worker_context = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-03-09 15:40:09 +00:00
|
|
|
furi_mutex_free(plugin_state->mutex);
|
2022-10-12 02:01:37 +00:00
|
|
|
free(plugin_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t totp_app() {
|
|
|
|
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
|
|
|
|
PluginState* plugin_state = malloc(sizeof(PluginState));
|
2022-11-10 05:32:21 +00:00
|
|
|
furi_check(plugin_state != NULL);
|
2022-10-12 02:01:37 +00:00
|
|
|
|
2022-11-02 20:39:54 +00:00
|
|
|
if(!totp_plugin_state_init(plugin_state)) {
|
2022-10-17 18:22:34 +00:00
|
|
|
FURI_LOG_E(LOGGING_TAG, "App state initialization failed\r\n");
|
2022-10-28 15:34:35 +00:00
|
|
|
totp_plugin_state_free(plugin_state);
|
2022-10-17 18:22:34 +00:00
|
|
|
return 254;
|
|
|
|
}
|
2022-10-12 02:01:37 +00:00
|
|
|
|
2023-01-06 23:07:26 +00:00
|
|
|
TotpCliContext* cli_context = totp_cli_register_command_handler(plugin_state, event_queue);
|
2023-04-13 18:20:29 +00:00
|
|
|
|
2022-12-26 20:31:38 +00:00
|
|
|
if(!totp_activate_initial_scene(plugin_state)) {
|
|
|
|
FURI_LOG_E(LOGGING_TAG, "An error ocurred during activating initial scene\r\n");
|
|
|
|
totp_plugin_state_free(plugin_state);
|
|
|
|
return 253;
|
|
|
|
}
|
|
|
|
|
2023-02-13 19:06:01 +00:00
|
|
|
// Affecting dolphin level
|
|
|
|
DOLPHIN_DEED(DolphinDeedPluginStart);
|
|
|
|
|
2022-10-12 02:01:37 +00:00
|
|
|
// Set system callbacks
|
|
|
|
ViewPort* view_port = view_port_alloc();
|
2023-03-07 21:18:23 +00:00
|
|
|
view_port_draw_callback_set(view_port, render_callback, plugin_state);
|
2022-10-12 02:01:37 +00:00
|
|
|
view_port_input_callback_set(view_port, input_callback, event_queue);
|
|
|
|
|
|
|
|
// Open GUI and register view_port
|
|
|
|
gui_add_view_port(plugin_state->gui, view_port, GuiLayerFullscreen);
|
|
|
|
|
|
|
|
PluginEvent event;
|
|
|
|
bool processing = true;
|
|
|
|
uint32_t last_user_interaction_time = furi_get_tick();
|
|
|
|
while(processing) {
|
|
|
|
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
|
|
|
|
|
2023-03-09 15:40:09 +00:00
|
|
|
if(furi_mutex_acquire(plugin_state->mutex, FuriWaitForever) == FuriStatusOk) {
|
|
|
|
if(event_status == FuriStatusOk) {
|
|
|
|
if(event.type == EventTypeKey) {
|
|
|
|
last_user_interaction_time = furi_get_tick();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.type == EventForceCloseApp) {
|
|
|
|
processing = false;
|
|
|
|
} else {
|
|
|
|
processing = totp_scene_director_handle_event(&event, plugin_state);
|
|
|
|
}
|
|
|
|
} else if(
|
|
|
|
plugin_state->pin_set && plugin_state->current_scene != TotpSceneAuthentication &&
|
2023-04-26 20:50:37 +00:00
|
|
|
plugin_state->current_scene != TotpSceneStandby &&
|
2023-03-09 15:40:09 +00:00
|
|
|
furi_get_tick() - last_user_interaction_time > IDLE_TIMEOUT) {
|
2023-04-26 20:50:37 +00:00
|
|
|
totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication);
|
2022-10-12 02:01:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 15:40:09 +00:00
|
|
|
view_port_update(view_port);
|
|
|
|
furi_mutex_release(plugin_state->mutex);
|
2022-10-12 02:01:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 23:07:26 +00:00
|
|
|
totp_cli_unregister_command_handler(cli_context);
|
2022-12-26 20:31:38 +00:00
|
|
|
totp_scene_director_deactivate_active_scene(plugin_state);
|
|
|
|
|
2022-10-12 02:01:37 +00:00
|
|
|
view_port_enabled_set(view_port, false);
|
|
|
|
gui_remove_view_port(plugin_state->gui, view_port);
|
|
|
|
view_port_free(view_port);
|
|
|
|
furi_message_queue_free(event_queue);
|
2022-10-28 15:34:35 +00:00
|
|
|
totp_plugin_state_free(plugin_state);
|
2022-10-12 02:01:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|