totp_app.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/gui.h>
  4. #include <input/input.h>
  5. #include <dialogs/dialogs.h>
  6. #include <stdlib.h>
  7. #include <flipper_format/flipper_format.h>
  8. #include <notification/notification.h>
  9. #include <notification/notification_messages.h>
  10. #include "services/base32/base32.h"
  11. #include "services/list/list.h"
  12. #include "services/config/config.h"
  13. #include "types/plugin_state.h"
  14. #include "types/token_info.h"
  15. #include "types/plugin_event.h"
  16. #include "types/event_type.h"
  17. #include "types/common.h"
  18. #include "scenes/scene_director.h"
  19. #include "services/ui/constants.h"
  20. #include "services/crypto/crypto.h"
  21. #include "services/cli/cli.h"
  22. #define IDLE_TIMEOUT 60000
  23. static void render_callback(Canvas* const canvas, void* ctx) {
  24. PluginState* plugin_state = acquire_mutex((ValueMutex*)ctx, 25);
  25. if(plugin_state != NULL && !plugin_state->changing_scene) {
  26. totp_scene_director_render(canvas, plugin_state);
  27. }
  28. release_mutex((ValueMutex*)ctx, plugin_state);
  29. }
  30. static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  31. furi_assert(event_queue);
  32. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  33. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  34. }
  35. static bool totp_plugin_state_init(PluginState* const plugin_state) {
  36. plugin_state->gui = furi_record_open(RECORD_GUI);
  37. plugin_state->notification = furi_record_open(RECORD_NOTIFICATION);
  38. plugin_state->dialogs = furi_record_open(RECORD_DIALOGS);
  39. totp_config_file_load_base(plugin_state);
  40. totp_cli_register_command_handler(plugin_state);
  41. totp_scene_director_init_scenes(plugin_state);
  42. if(plugin_state->crypto_verify_data == NULL) {
  43. DialogMessage* message = dialog_message_alloc();
  44. dialog_message_set_buttons(message, "No", NULL, "Yes");
  45. dialog_message_set_text(
  46. message,
  47. "Would you like to setup PIN?",
  48. SCREEN_WIDTH_CENTER,
  49. SCREEN_HEIGHT_CENTER,
  50. AlignCenter,
  51. AlignCenter);
  52. DialogMessageButton dialog_result = dialog_message_show(plugin_state->dialogs, message);
  53. dialog_message_free(message);
  54. if(dialog_result == DialogMessageButtonRight) {
  55. totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL);
  56. } else {
  57. totp_crypto_seed_iv(plugin_state, NULL, 0);
  58. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  59. }
  60. } else if(plugin_state->pin_set) {
  61. totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL);
  62. } else {
  63. totp_crypto_seed_iv(plugin_state, NULL, 0);
  64. if(totp_crypto_verify_key(plugin_state)) {
  65. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  66. } else {
  67. FURI_LOG_E(
  68. LOGGING_TAG,
  69. "Digital signature verification failed. Looks like conf file was created on another flipper and can't be used on any other");
  70. DialogMessage* message = dialog_message_alloc();
  71. dialog_message_set_buttons(message, "Exit", NULL, NULL);
  72. dialog_message_set_text(
  73. message,
  74. "Digital signature verification failed",
  75. SCREEN_WIDTH_CENTER,
  76. SCREEN_HEIGHT_CENTER,
  77. AlignCenter,
  78. AlignCenter);
  79. dialog_message_show(plugin_state->dialogs, message);
  80. dialog_message_free(message);
  81. return false;
  82. }
  83. }
  84. return true;
  85. }
  86. static void totp_plugin_state_free(PluginState* plugin_state) {
  87. totp_cli_unregister_command_handler();
  88. totp_scene_director_deactivate_active_scene(plugin_state);
  89. totp_scene_director_dispose(plugin_state);
  90. furi_record_close(RECORD_GUI);
  91. furi_record_close(RECORD_NOTIFICATION);
  92. furi_record_close(RECORD_DIALOGS);
  93. ListNode* node = plugin_state->tokens_list;
  94. ListNode* tmp;
  95. while(node != NULL) {
  96. tmp = node->next;
  97. TokenInfo* tokenInfo = node->data;
  98. token_info_free(tokenInfo);
  99. free(node);
  100. node = tmp;
  101. }
  102. if(plugin_state->crypto_verify_data != NULL) {
  103. free(plugin_state->crypto_verify_data);
  104. }
  105. free(plugin_state);
  106. }
  107. int32_t totp_app() {
  108. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  109. PluginState* plugin_state = malloc(sizeof(PluginState));
  110. furi_check(plugin_state != NULL);
  111. if(!totp_plugin_state_init(plugin_state)) {
  112. FURI_LOG_E(LOGGING_TAG, "App state initialization failed\r\n");
  113. totp_plugin_state_free(plugin_state);
  114. return 254;
  115. }
  116. ValueMutex state_mutex;
  117. if(!init_mutex(&state_mutex, plugin_state, sizeof(PluginState))) {
  118. FURI_LOG_E(LOGGING_TAG, "Cannot create mutex\r\n");
  119. totp_plugin_state_free(plugin_state);
  120. return 255;
  121. }
  122. // Set system callbacks
  123. ViewPort* view_port = view_port_alloc();
  124. view_port_draw_callback_set(view_port, render_callback, &state_mutex);
  125. view_port_input_callback_set(view_port, input_callback, event_queue);
  126. // Open GUI and register view_port
  127. gui_add_view_port(plugin_state->gui, view_port, GuiLayerFullscreen);
  128. PluginEvent event;
  129. bool processing = true;
  130. uint32_t last_user_interaction_time = furi_get_tick();
  131. while(processing) {
  132. if(plugin_state->changing_scene) continue;
  133. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  134. PluginState* plugin_state_m = acquire_mutex_block(&state_mutex);
  135. if(event_status == FuriStatusOk) {
  136. if(event.type == EventTypeKey) {
  137. last_user_interaction_time = furi_get_tick();
  138. }
  139. processing = totp_scene_director_handle_event(&event, plugin_state_m);
  140. } else if(
  141. plugin_state_m->pin_set && plugin_state_m->current_scene != TotpSceneAuthentication &&
  142. furi_get_tick() - last_user_interaction_time > IDLE_TIMEOUT) {
  143. totp_scene_director_activate_scene(plugin_state_m, TotpSceneAuthentication, NULL);
  144. }
  145. view_port_update(view_port);
  146. release_mutex(&state_mutex, plugin_state_m);
  147. }
  148. view_port_enabled_set(view_port, false);
  149. gui_remove_view_port(plugin_state->gui, view_port);
  150. view_port_free(view_port);
  151. furi_message_queue_free(event_queue);
  152. delete_mutex(&state_mutex);
  153. totp_plugin_state_free(plugin_state);
  154. return 0;
  155. }