totp_app.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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);
  50. } else {
  51. CryptoSeedIVResult seed_result = totp_crypto_seed_iv(plugin_state, NULL, 0);
  52. if(seed_result & CryptoSeedIVResultFlagSuccess &&
  53. seed_result & CryptoSeedIVResultFlagNewCryptoVerifyData) {
  54. if(!totp_config_file_update_crypto_signatures(plugin_state)) {
  55. totp_dialogs_config_loading_error(plugin_state);
  56. return false;
  57. }
  58. } else if(seed_result == CryptoSeedIVResultFailed) {
  59. totp_dialogs_config_loading_error(plugin_state);
  60. return false;
  61. }
  62. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
  63. }
  64. } else if(plugin_state->pin_set) {
  65. totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication);
  66. } else {
  67. CryptoSeedIVResult seed_result = totp_crypto_seed_iv(plugin_state, NULL, 0);
  68. if(seed_result & CryptoSeedIVResultFlagSuccess &&
  69. seed_result & CryptoSeedIVResultFlagNewCryptoVerifyData) {
  70. if(!totp_config_file_update_crypto_signatures(plugin_state)) {
  71. totp_dialogs_config_loading_error(plugin_state);
  72. return false;
  73. }
  74. } else if(seed_result == CryptoSeedIVResultFailed) {
  75. totp_dialogs_config_loading_error(plugin_state);
  76. return false;
  77. }
  78. if(totp_crypto_verify_key(plugin_state)) {
  79. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
  80. } else {
  81. FURI_LOG_E(
  82. LOGGING_TAG,
  83. "Digital signature verification failed. Looks like conf file was created on another flipper and can't be used on any other");
  84. DialogMessage* message = dialog_message_alloc();
  85. dialog_message_set_buttons(message, "Exit", NULL, NULL);
  86. dialog_message_set_text(
  87. message,
  88. "Digital signature verification failed",
  89. SCREEN_WIDTH_CENTER,
  90. SCREEN_HEIGHT_CENTER,
  91. AlignCenter,
  92. AlignCenter);
  93. dialog_message_show(plugin_state->dialogs_app, message);
  94. dialog_message_free(message);
  95. return false;
  96. }
  97. }
  98. return true;
  99. }
  100. static bool totp_plugin_state_init(PluginState* const plugin_state) {
  101. plugin_state->gui = furi_record_open(RECORD_GUI);
  102. plugin_state->notification_app = furi_record_open(RECORD_NOTIFICATION);
  103. plugin_state->dialogs_app = furi_record_open(RECORD_DIALOGS);
  104. memset(&plugin_state->iv[0], 0, TOTP_IV_SIZE);
  105. if(!totp_config_file_load(plugin_state)) {
  106. totp_dialogs_config_loading_error(plugin_state);
  107. return false;
  108. }
  109. plugin_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  110. #ifdef TOTP_BADBT_TYPE_ENABLED
  111. if(plugin_state->automation_method & AutomationMethodBadBt) {
  112. plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init();
  113. } else {
  114. plugin_state->bt_type_code_worker_context = NULL;
  115. }
  116. #endif
  117. return true;
  118. }
  119. static void totp_plugin_state_free(PluginState* plugin_state) {
  120. furi_record_close(RECORD_GUI);
  121. furi_record_close(RECORD_NOTIFICATION);
  122. furi_record_close(RECORD_DIALOGS);
  123. totp_config_file_close(plugin_state);
  124. if(plugin_state->crypto_verify_data != NULL) {
  125. free(plugin_state->crypto_verify_data);
  126. }
  127. #ifdef TOTP_BADBT_TYPE_ENABLED
  128. if(plugin_state->bt_type_code_worker_context != NULL) {
  129. totp_bt_type_code_worker_free(plugin_state->bt_type_code_worker_context);
  130. plugin_state->bt_type_code_worker_context = NULL;
  131. }
  132. #endif
  133. furi_mutex_free(plugin_state->mutex);
  134. free(plugin_state);
  135. }
  136. int32_t totp_app() {
  137. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  138. PluginState* plugin_state = malloc(sizeof(PluginState));
  139. furi_check(plugin_state != NULL);
  140. if(!totp_plugin_state_init(plugin_state)) {
  141. FURI_LOG_E(LOGGING_TAG, "App state initialization failed\r\n");
  142. totp_plugin_state_free(plugin_state);
  143. return 254;
  144. }
  145. TotpCliContext* cli_context = totp_cli_register_command_handler(plugin_state, event_queue);
  146. if(!totp_activate_initial_scene(plugin_state)) {
  147. FURI_LOG_E(LOGGING_TAG, "An error ocurred during activating initial scene\r\n");
  148. totp_plugin_state_free(plugin_state);
  149. return 253;
  150. }
  151. // Affecting dolphin level
  152. DOLPHIN_DEED(DolphinDeedPluginStart);
  153. // Set system callbacks
  154. ViewPort* view_port = view_port_alloc();
  155. view_port_draw_callback_set(view_port, render_callback, plugin_state);
  156. view_port_input_callback_set(view_port, input_callback, event_queue);
  157. // Open GUI and register view_port
  158. gui_add_view_port(plugin_state->gui, view_port, GuiLayerFullscreen);
  159. PluginEvent event;
  160. bool processing = true;
  161. uint32_t last_user_interaction_time = furi_get_tick();
  162. while(processing) {
  163. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  164. if(furi_mutex_acquire(plugin_state->mutex, FuriWaitForever) == FuriStatusOk) {
  165. if(event_status == FuriStatusOk) {
  166. if(event.type == EventTypeKey) {
  167. last_user_interaction_time = furi_get_tick();
  168. }
  169. if(event.type == EventForceCloseApp) {
  170. processing = false;
  171. } else {
  172. processing = totp_scene_director_handle_event(&event, plugin_state);
  173. }
  174. } else if(
  175. plugin_state->pin_set && plugin_state->current_scene != TotpSceneAuthentication &&
  176. plugin_state->current_scene != TotpSceneStandby &&
  177. furi_get_tick() - last_user_interaction_time > IDLE_TIMEOUT) {
  178. totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication);
  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. }