totp_app.c 10 KB

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