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