totp_app.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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_facade.h"
  19. #include "cli/cli.h"
  20. struct TotpRenderCallbackContext {
  21. FuriMutex* mutex;
  22. PluginState* plugin_state;
  23. };
  24. static void render_callback(Canvas* const canvas, void* const ctx) {
  25. furi_assert(ctx);
  26. const struct TotpRenderCallbackContext* context = ctx;
  27. if(furi_mutex_acquire(context->mutex, 25) == FuriStatusOk) {
  28. totp_scene_director_render(canvas, context->plugin_state);
  29. furi_mutex_release(context->mutex);
  30. }
  31. }
  32. static void input_callback(InputEvent* const input_event, void* const ctx) {
  33. furi_assert(ctx);
  34. FuriMessageQueue* event_queue = ctx;
  35. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  36. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  37. }
  38. static bool totp_activate_initial_scene(PluginState* const plugin_state) {
  39. if(plugin_state->crypto_verify_data == NULL) {
  40. DialogMessage* message = dialog_message_alloc();
  41. dialog_message_set_buttons(message, "No", NULL, "Yes");
  42. dialog_message_set_text(
  43. message,
  44. "Would you like to setup PIN?",
  45. SCREEN_WIDTH_CENTER,
  46. SCREEN_HEIGHT_CENTER,
  47. AlignCenter,
  48. AlignCenter);
  49. DialogMessageButton dialog_result =
  50. dialog_message_show(plugin_state->dialogs_app, message);
  51. dialog_message_free(message);
  52. if(dialog_result == DialogMessageButtonRight) {
  53. totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication);
  54. } else {
  55. CryptoSeedIVResult seed_result = totp_crypto_seed_iv(plugin_state, NULL, 0);
  56. if(seed_result & CryptoSeedIVResultFlagSuccess &&
  57. seed_result & CryptoSeedIVResultFlagNewCryptoVerifyData) {
  58. if(!totp_config_file_update_crypto_signatures(plugin_state)) {
  59. totp_dialogs_config_loading_error(plugin_state);
  60. return false;
  61. }
  62. } else if(seed_result == CryptoSeedIVResultFailed) {
  63. totp_dialogs_config_loading_error(plugin_state);
  64. return false;
  65. }
  66. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
  67. }
  68. } else if(plugin_state->pin_set) {
  69. totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication);
  70. } else {
  71. CryptoSeedIVResult seed_result = totp_crypto_seed_iv(plugin_state, NULL, 0);
  72. if(seed_result & CryptoSeedIVResultFlagSuccess &&
  73. seed_result & CryptoSeedIVResultFlagNewCryptoVerifyData) {
  74. if(!totp_config_file_update_crypto_signatures(plugin_state)) {
  75. totp_dialogs_config_loading_error(plugin_state);
  76. return false;
  77. }
  78. } else if(seed_result == CryptoSeedIVResultFailed) {
  79. totp_dialogs_config_loading_error(plugin_state);
  80. return false;
  81. }
  82. if(totp_crypto_verify_key(plugin_state)) {
  83. totp_config_file_ensure_latest_encryption(plugin_state, NULL, 0);
  84. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
  85. } else {
  86. FURI_LOG_E(
  87. LOGGING_TAG,
  88. "Digital signature verification failed. Looks like conf file was created on another flipper and can't be used on any other");
  89. DialogMessage* message = dialog_message_alloc();
  90. dialog_message_set_buttons(message, "Exit", NULL, NULL);
  91. dialog_message_set_text(
  92. message,
  93. "Digital signature verification failed",
  94. SCREEN_WIDTH_CENTER,
  95. SCREEN_HEIGHT_CENTER,
  96. AlignCenter,
  97. AlignCenter);
  98. dialog_message_show(plugin_state->dialogs_app, message);
  99. dialog_message_free(message);
  100. return false;
  101. }
  102. }
  103. return true;
  104. }
  105. static bool on_user_idle(void* context) {
  106. PluginState* plugin_state = context;
  107. if(plugin_state->current_scene != TotpSceneAuthentication &&
  108. plugin_state->current_scene != TotpSceneStandby) {
  109. totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication);
  110. totp_scene_director_force_redraw(plugin_state);
  111. return true;
  112. }
  113. return false;
  114. }
  115. static bool totp_plugin_state_init(PluginState* const plugin_state) {
  116. plugin_state->gui = furi_record_open(RECORD_GUI);
  117. plugin_state->dialogs_app = furi_record_open(RECORD_DIALOGS);
  118. memset(&plugin_state->iv[0], 0, CRYPTO_IV_LENGTH);
  119. if(!totp_config_file_load(plugin_state)) {
  120. totp_dialogs_config_loading_error(plugin_state);
  121. return false;
  122. }
  123. plugin_state->event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  124. #ifdef TOTP_BADBT_TYPE_ENABLED
  125. if(plugin_state->automation_method & AutomationMethodBadBt) {
  126. plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init();
  127. } else {
  128. plugin_state->bt_type_code_worker_context = NULL;
  129. }
  130. #endif
  131. if(plugin_state->pin_set) {
  132. plugin_state->idle_timeout_context =
  133. idle_timeout_alloc(TOTP_AUTO_LOCK_IDLE_TIMEOUT_SEC, &on_user_idle, plugin_state);
  134. idle_timeout_start(plugin_state->idle_timeout_context);
  135. } else {
  136. plugin_state->idle_timeout_context = NULL;
  137. }
  138. return true;
  139. }
  140. static void totp_plugin_state_free(PluginState* plugin_state) {
  141. if(plugin_state->idle_timeout_context != NULL) {
  142. idle_timeout_stop(plugin_state->idle_timeout_context);
  143. idle_timeout_free(plugin_state->idle_timeout_context);
  144. }
  145. furi_record_close(RECORD_GUI);
  146. furi_record_close(RECORD_DIALOGS);
  147. totp_config_file_close(plugin_state);
  148. if(plugin_state->crypto_verify_data != NULL) {
  149. free(plugin_state->crypto_verify_data);
  150. }
  151. #ifdef TOTP_BADBT_TYPE_ENABLED
  152. if(plugin_state->bt_type_code_worker_context != NULL) {
  153. totp_bt_type_code_worker_free(plugin_state->bt_type_code_worker_context);
  154. plugin_state->bt_type_code_worker_context = NULL;
  155. }
  156. #endif
  157. furi_message_queue_free(plugin_state->event_queue);
  158. free(plugin_state);
  159. }
  160. int32_t totp_app() {
  161. PluginState* plugin_state = malloc(sizeof(PluginState));
  162. furi_check(plugin_state != NULL);
  163. if(!totp_plugin_state_init(plugin_state)) {
  164. FURI_LOG_E(LOGGING_TAG, "App state initialization failed\r\n");
  165. totp_plugin_state_free(plugin_state);
  166. return 254;
  167. }
  168. TotpCliContext* cli_context = totp_cli_register_command_handler(plugin_state);
  169. if(!totp_activate_initial_scene(plugin_state)) {
  170. FURI_LOG_E(LOGGING_TAG, "An error ocurred during activating initial scene\r\n");
  171. totp_plugin_state_free(plugin_state);
  172. return 253;
  173. }
  174. // Affecting dolphin level
  175. dolphin_deed(DolphinDeedPluginStart);
  176. FuriMutex* main_loop_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  177. struct TotpRenderCallbackContext render_context = {
  178. .plugin_state = plugin_state, .mutex = main_loop_mutex};
  179. // Set system callbacks
  180. ViewPort* view_port = view_port_alloc();
  181. view_port_draw_callback_set(view_port, render_callback, &render_context);
  182. view_port_input_callback_set(view_port, input_callback, plugin_state->event_queue);
  183. // Open GUI and register view_port
  184. gui_add_view_port(plugin_state->gui, view_port, GuiLayerFullscreen);
  185. PluginEvent event;
  186. bool processing = true;
  187. while(processing) {
  188. if(furi_message_queue_get(plugin_state->event_queue, &event, FuriWaitForever) ==
  189. FuriStatusOk) {
  190. if(event.type == EventForceCloseApp) {
  191. processing = false;
  192. } else if(event.type == EventForceRedraw) {
  193. processing = true; //-V1048
  194. } else if(furi_mutex_acquire(main_loop_mutex, FuriWaitForever) == FuriStatusOk) {
  195. if(event.type == EventTypeKey && plugin_state->idle_timeout_context != NULL) {
  196. idle_timeout_report_activity(plugin_state->idle_timeout_context);
  197. }
  198. processing = totp_scene_director_handle_event(&event, plugin_state);
  199. furi_mutex_release(main_loop_mutex);
  200. }
  201. }
  202. view_port_update(view_port);
  203. }
  204. totp_cli_unregister_command_handler(cli_context);
  205. totp_scene_director_deactivate_active_scene(plugin_state);
  206. view_port_enabled_set(view_port, false);
  207. gui_remove_view_port(plugin_state->gui, view_port);
  208. view_port_free(view_port);
  209. furi_mutex_free(main_loop_mutex);
  210. totp_plugin_state_free(plugin_state);
  211. return 0;
  212. }