totp_app.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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(message, "Would you like to setup PIN?", SCREEN_WIDTH_CENTER, SCREEN_HEIGHT_CENTER, AlignCenter, AlignCenter);
  46. DialogMessageButton dialog_result = dialog_message_show(plugin_state->dialogs, message);
  47. dialog_message_free(message);
  48. if (dialog_result == DialogMessageButtonRight) {
  49. totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL);
  50. } else {
  51. totp_crypto_seed_iv(plugin_state, NULL, 0);
  52. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  53. }
  54. } else if (plugin_state->pin_set) {
  55. totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL);
  56. } else {
  57. totp_crypto_seed_iv(plugin_state, NULL, 0);
  58. if (totp_crypto_verify_key(plugin_state)) {
  59. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  60. } else {
  61. FURI_LOG_E(LOGGING_TAG, "Digital signature verification failed. Looks like conf file was created on another flipper and can't be used on any other");
  62. DialogMessage* message = dialog_message_alloc();
  63. dialog_message_set_buttons(message, "Exit", NULL, NULL);
  64. dialog_message_set_text(message, "Digital signature verification failed", SCREEN_WIDTH_CENTER, SCREEN_HEIGHT_CENTER, AlignCenter, AlignCenter);
  65. dialog_message_show(plugin_state->dialogs, message);
  66. dialog_message_free(message);
  67. return false;
  68. }
  69. }
  70. return true;
  71. }
  72. static void totp_plugin_state_free(PluginState* plugin_state) {
  73. totp_cli_unregister_command_handler();
  74. totp_scene_director_deactivate_active_scene(plugin_state);
  75. totp_scene_director_dispose(plugin_state);
  76. furi_record_close(RECORD_GUI);
  77. furi_record_close(RECORD_NOTIFICATION);
  78. furi_record_close(RECORD_DIALOGS);
  79. ListNode* node = plugin_state->tokens_list;
  80. ListNode* tmp;
  81. while (node != NULL) {
  82. tmp = node->next;
  83. TokenInfo* tokenInfo = node->data;
  84. token_info_free(tokenInfo);
  85. free(node);
  86. node = tmp;
  87. }
  88. if (plugin_state->crypto_verify_data != NULL) {
  89. free(plugin_state->crypto_verify_data);
  90. }
  91. free(plugin_state);
  92. }
  93. int32_t totp_app() {
  94. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  95. PluginState* plugin_state = malloc(sizeof(PluginState));
  96. if (!totp_plugin_state_init(plugin_state)) {
  97. FURI_LOG_E(LOGGING_TAG, "App state initialization failed\r\n");
  98. totp_plugin_state_free(plugin_state);
  99. return 254;
  100. }
  101. ValueMutex state_mutex;
  102. if(!init_mutex(&state_mutex, plugin_state, sizeof(PluginState))) {
  103. FURI_LOG_E(LOGGING_TAG, "Cannot create mutex\r\n");
  104. totp_plugin_state_free(plugin_state);
  105. return 255;
  106. }
  107. // Set system callbacks
  108. ViewPort* view_port = view_port_alloc();
  109. view_port_draw_callback_set(view_port, render_callback, &state_mutex);
  110. view_port_input_callback_set(view_port, input_callback, event_queue);
  111. // Open GUI and register view_port
  112. gui_add_view_port(plugin_state->gui, view_port, GuiLayerFullscreen);
  113. PluginEvent event;
  114. bool processing = true;
  115. uint32_t last_user_interaction_time = furi_get_tick();
  116. while(processing) {
  117. if (plugin_state->changing_scene) continue;
  118. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  119. PluginState* plugin_state = acquire_mutex_block(&state_mutex);
  120. if(event_status == FuriStatusOk) {
  121. if (event.type == EventTypeKey) {
  122. last_user_interaction_time = furi_get_tick();
  123. }
  124. processing = totp_scene_director_handle_event(&event, plugin_state);
  125. } else if (plugin_state->pin_set && plugin_state->current_scene != TotpSceneAuthentication && furi_get_tick() - last_user_interaction_time > IDLE_TIMEOUT) {
  126. totp_scene_director_activate_scene(plugin_state, TotpSceneAuthentication, NULL);
  127. }
  128. view_port_update(view_port);
  129. release_mutex(&state_mutex, plugin_state);
  130. }
  131. view_port_enabled_set(view_port, false);
  132. gui_remove_view_port(plugin_state->gui, view_port);
  133. view_port_free(view_port);
  134. furi_message_queue_free(event_queue);
  135. delete_mutex(&state_mutex);
  136. totp_plugin_state_free(plugin_state);
  137. return 0;
  138. }