totp_app.c 10 KB

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