totp_app.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/gui.h>
  4. #include <input/input.h>
  5. #include <dialogs/dialogs.h>
  6. #include <stdlib.h>
  7. #include <flipper_format/flipper_format.h>
  8. #include <notification/notification.h>
  9. #include <notification/notification_messages.h>
  10. #include <dolphin/dolphin.h>
  11. #include "services/config/config.h"
  12. #include "types/plugin_state.h"
  13. #include "types/token_info.h"
  14. #include "types/plugin_event.h"
  15. #include "types/event_type.h"
  16. #include "types/common.h"
  17. #include "ui/scene_director.h"
  18. #include "ui/constants.h"
  19. #include "ui/common_dialogs.h"
  20. #include "services/crypto/crypto.h"
  21. #include "cli/cli.h"
  22. #define IDLE_TIMEOUT 60000
  23. static void render_callback(Canvas* const canvas, void* ctx) {
  24. furi_assert(ctx);
  25. PluginState* plugin_state = ctx;
  26. if (furi_mutex_acquire(plugin_state->mutex, 25) == FuriStatusOk) {
  27. totp_scene_director_render(canvas, plugin_state);
  28. furi_mutex_release(plugin_state->mutex);
  29. }
  30. }
  31. static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  32. furi_assert(event_queue);
  33. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  34. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  35. }
  36. static bool totp_activate_initial_scene(PluginState* const plugin_state) {
  37. if(plugin_state->crypto_verify_data == NULL) {
  38. DialogMessage* message = dialog_message_alloc();
  39. dialog_message_set_buttons(message, "No", NULL, "Yes");
  40. dialog_message_set_text(
  41. message,
  42. "Would you like to setup PIN?",
  43. SCREEN_WIDTH_CENTER,
  44. SCREEN_HEIGHT_CENTER,
  45. AlignCenter,
  46. AlignCenter);
  47. DialogMessageButton dialog_result =
  48. dialog_message_show(plugin_state->dialogs_app, message);
  49. dialog_message_free(message);
  50. if(dialog_result == DialogMessageButtonRight) {
  51. totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL);
  52. } else {
  53. if(!totp_crypto_seed_iv(plugin_state, NULL, 0)) {
  54. totp_dialogs_config_loading_error(plugin_state);
  55. return false;
  56. }
  57. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  58. }
  59. } else if(plugin_state->pin_set) {
  60. totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL);
  61. } else {
  62. if(!totp_crypto_seed_iv(plugin_state, NULL, 0)) {
  63. totp_dialogs_config_loading_error(plugin_state);
  64. return false;
  65. }
  66. if(totp_crypto_verify_key(plugin_state)) {
  67. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  68. } else {
  69. FURI_LOG_E(
  70. LOGGING_TAG,
  71. "Digital signature verification failed. Looks like conf file was created on another flipper and can't be used on any other");
  72. DialogMessage* message = dialog_message_alloc();
  73. dialog_message_set_buttons(message, "Exit", NULL, NULL);
  74. dialog_message_set_text(
  75. message,
  76. "Digital signature verification failed",
  77. SCREEN_WIDTH_CENTER,
  78. SCREEN_HEIGHT_CENTER,
  79. AlignCenter,
  80. AlignCenter);
  81. dialog_message_show(plugin_state->dialogs_app, message);
  82. dialog_message_free(message);
  83. return false;
  84. }
  85. }
  86. return true;
  87. }
  88. static bool totp_plugin_state_init(PluginState* const plugin_state) {
  89. plugin_state->gui = furi_record_open(RECORD_GUI);
  90. plugin_state->notification_app = furi_record_open(RECORD_NOTIFICATION);
  91. plugin_state->dialogs_app = furi_record_open(RECORD_DIALOGS);
  92. if(totp_config_file_load_base(plugin_state) != TotpConfigFileOpenSuccess) {
  93. totp_dialogs_config_loading_error(plugin_state);
  94. return false;
  95. }
  96. plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  97. if (plugin_state->mutex == NULL) {
  98. FURI_LOG_E(LOGGING_TAG, "Cannot create mutex\r\n");
  99. return false;
  100. }
  101. return true;
  102. }
  103. static void totp_plugin_state_free(PluginState* plugin_state) {
  104. furi_record_close(RECORD_GUI);
  105. furi_record_close(RECORD_NOTIFICATION);
  106. furi_record_close(RECORD_DIALOGS);
  107. ListNode* node = plugin_state->tokens_list;
  108. ListNode* tmp;
  109. while(node != NULL) {
  110. tmp = node->next;
  111. TokenInfo* tokenInfo = node->data;
  112. token_info_free(tokenInfo);
  113. free(node);
  114. node = tmp;
  115. }
  116. if(plugin_state->crypto_verify_data != NULL) {
  117. free(plugin_state->crypto_verify_data);
  118. }
  119. furi_mutex_free(plugin_state->mutex);
  120. free(plugin_state);
  121. }
  122. int32_t totp_app() {
  123. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  124. PluginState* plugin_state = malloc(sizeof(PluginState));
  125. furi_check(plugin_state != NULL);
  126. if(!totp_plugin_state_init(plugin_state)) {
  127. FURI_LOG_E(LOGGING_TAG, "App state initialization failed\r\n");
  128. totp_plugin_state_free(plugin_state);
  129. return 254;
  130. }
  131. TotpCliContext* cli_context = totp_cli_register_command_handler(plugin_state, event_queue);
  132. totp_scene_director_init_scenes(plugin_state);
  133. if(!totp_activate_initial_scene(plugin_state)) {
  134. FURI_LOG_E(LOGGING_TAG, "An error ocurred during activating initial scene\r\n");
  135. totp_plugin_state_free(plugin_state);
  136. return 253;
  137. }
  138. // Affecting dolphin level
  139. DOLPHIN_DEED(DolphinDeedPluginStart);
  140. // Set system callbacks
  141. ViewPort* view_port = view_port_alloc();
  142. view_port_draw_callback_set(view_port, render_callback, plugin_state);
  143. view_port_input_callback_set(view_port, input_callback, event_queue);
  144. // Open GUI and register view_port
  145. gui_add_view_port(plugin_state->gui, view_port, GuiLayerFullscreen);
  146. PluginEvent event;
  147. bool processing = true;
  148. uint32_t last_user_interaction_time = furi_get_tick();
  149. while(processing) {
  150. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  151. if (furi_mutex_acquire(plugin_state->mutex, FuriWaitForever) == FuriStatusOk) {
  152. if(event_status == FuriStatusOk) {
  153. if(event.type == EventTypeKey) {
  154. last_user_interaction_time = furi_get_tick();
  155. }
  156. if(event.type == EventForceCloseApp) {
  157. processing = false;
  158. } else {
  159. processing = totp_scene_director_handle_event(&event, plugin_state);
  160. }
  161. } else if(
  162. plugin_state->pin_set && plugin_state->current_scene != TotpSceneAuthentication &&
  163. furi_get_tick() - last_user_interaction_time > IDLE_TIMEOUT) {
  164. totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL);
  165. }
  166. view_port_update(view_port);
  167. furi_mutex_release(plugin_state->mutex);
  168. }
  169. }
  170. totp_cli_unregister_command_handler(cli_context);
  171. totp_scene_director_deactivate_active_scene(plugin_state);
  172. totp_scene_director_dispose(plugin_state);
  173. view_port_enabled_set(view_port, false);
  174. gui_remove_view_port(plugin_state->gui, view_port);
  175. view_port_free(view_port);
  176. furi_message_queue_free(event_queue);
  177. totp_plugin_state_free(plugin_state);
  178. return 0;
  179. }