totp_app.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include <gui/gui.h>
  2. #include <input/input.h>
  3. #include <dialogs/dialogs.h>
  4. #include <stdlib.h>
  5. #include <notification/notification.h>
  6. #include <notification/notification_messages.h>
  7. #include <dolphin/dolphin.h>
  8. #include "features_config.h"
  9. #include "services/config/config.h"
  10. #include "types/plugin_state.h"
  11. #include "types/token_info.h"
  12. #include "types/plugin_event.h"
  13. #include "types/event_type.h"
  14. #include "types/common.h"
  15. #include "ui/scene_director.h"
  16. #include "ui/constants.h"
  17. #include "ui/common_dialogs.h"
  18. #include "services/crypto/crypto.h"
  19. #include "cli/cli.h"
  20. #define IDLE_TIMEOUT (60000)
  21. static void render_callback(Canvas* const canvas, void* ctx) {
  22. furi_assert(ctx);
  23. PluginState* plugin_state = ctx;
  24. if(furi_mutex_acquire(plugin_state->mutex, 25) == FuriStatusOk) {
  25. totp_scene_director_render(canvas, plugin_state);
  26. furi_mutex_release(plugin_state->mutex);
  27. }
  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. memset(&plugin_state->iv[0], 0, TOTP_IV_SIZE);
  91. if(totp_config_file_load_base(plugin_state) != TotpConfigFileOpenSuccess) {
  92. totp_dialogs_config_loading_error(plugin_state);
  93. return false;
  94. }
  95. plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  96. #ifdef TOTP_BADBT_TYPE_ENABLED
  97. if(plugin_state->automation_method & AutomationMethodBadBt) {
  98. plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init();
  99. } else {
  100. plugin_state->bt_type_code_worker_context = NULL;
  101. }
  102. #endif
  103. return true;
  104. }
  105. static void totp_plugin_state_free(PluginState* plugin_state) {
  106. furi_record_close(RECORD_GUI);
  107. furi_record_close(RECORD_NOTIFICATION);
  108. furi_record_close(RECORD_DIALOGS);
  109. ListNode* node = plugin_state->tokens_list;
  110. ListNode* tmp;
  111. while(node != NULL) {
  112. tmp = node->next;
  113. TokenInfo* tokenInfo = node->data;
  114. token_info_free(tokenInfo);
  115. free(node);
  116. node = tmp;
  117. }
  118. if(plugin_state->crypto_verify_data != NULL) {
  119. free(plugin_state->crypto_verify_data);
  120. }
  121. #ifdef TOTP_BADBT_TYPE_ENABLED
  122. if(plugin_state->bt_type_code_worker_context != NULL) {
  123. totp_bt_type_code_worker_free(plugin_state->bt_type_code_worker_context);
  124. plugin_state->bt_type_code_worker_context = NULL;
  125. }
  126. #endif
  127. furi_mutex_free(plugin_state->mutex);
  128. free(plugin_state);
  129. }
  130. int32_t totp_app() {
  131. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  132. PluginState* plugin_state = malloc(sizeof(PluginState));
  133. furi_check(plugin_state != NULL);
  134. if(!totp_plugin_state_init(plugin_state)) {
  135. FURI_LOG_E(LOGGING_TAG, "App state initialization failed\r\n");
  136. totp_plugin_state_free(plugin_state);
  137. return 254;
  138. }
  139. TotpCliContext* cli_context = totp_cli_register_command_handler(plugin_state, event_queue);
  140. if(!totp_activate_initial_scene(plugin_state)) {
  141. FURI_LOG_E(LOGGING_TAG, "An error ocurred during activating initial scene\r\n");
  142. totp_plugin_state_free(plugin_state);
  143. return 253;
  144. }
  145. // Affecting dolphin level
  146. DOLPHIN_DEED(DolphinDeedPluginStart);
  147. // Set system callbacks
  148. ViewPort* view_port = view_port_alloc();
  149. view_port_draw_callback_set(view_port, render_callback, plugin_state);
  150. view_port_input_callback_set(view_port, input_callback, event_queue);
  151. // Open GUI and register view_port
  152. gui_add_view_port(plugin_state->gui, view_port, GuiLayerFullscreen);
  153. PluginEvent event;
  154. bool processing = true;
  155. uint32_t last_user_interaction_time = furi_get_tick();
  156. while(processing) {
  157. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  158. if(furi_mutex_acquire(plugin_state->mutex, FuriWaitForever) == FuriStatusOk) {
  159. if(event_status == FuriStatusOk) {
  160. if(event.type == EventTypeKey) {
  161. last_user_interaction_time = furi_get_tick();
  162. }
  163. if(event.type == EventForceCloseApp) {
  164. processing = false;
  165. } else {
  166. processing = totp_scene_director_handle_event(&event, plugin_state);
  167. }
  168. } else if(
  169. plugin_state->pin_set && plugin_state->current_scene != TotpSceneAuthentication &&
  170. furi_get_tick() - last_user_interaction_time > IDLE_TIMEOUT) {
  171. totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL);
  172. }
  173. view_port_update(view_port);
  174. furi_mutex_release(plugin_state->mutex);
  175. }
  176. }
  177. totp_cli_unregister_command_handler(cli_context);
  178. totp_scene_director_deactivate_active_scene(plugin_state);
  179. view_port_enabled_set(view_port, false);
  180. gui_remove_view_port(plugin_state->gui, view_port);
  181. view_port_free(view_port);
  182. furi_message_queue_free(event_queue);
  183. totp_plugin_state_free(plugin_state);
  184. return 0;
  185. }