totp_app.c 7.7 KB

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