totp_app.c 8.6 KB

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