totp_app.c 7.1 KB

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