export.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <lib/toolbox/args.h>
  2. #include <dialogs/dialogs.h>
  3. #include "../../../ui/constants.h"
  4. #include <flipper_application/flipper_application.h>
  5. #include "../../../lib/polyfills/memset_s.h"
  6. #include "../../../services/config/config.h"
  7. #include "../../../services/crypto/crypto_facade.h"
  8. #include "../../../ui/scene_director.h"
  9. #include "../../cli_helpers.h"
  10. #include "../../cli_plugin_interface.h"
  11. #include "../../cli_shared_methods.h"
  12. static const char* BASE32_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
  13. static void print_as_base32(const uint8_t* data, size_t length) {
  14. int buffer = data[0];
  15. size_t next = 1;
  16. int bitsLeft = 8;
  17. while(bitsLeft > 0 || next < length) {
  18. if(bitsLeft < 5) {
  19. if(next < length) {
  20. buffer <<= 8;
  21. buffer |= data[next++] & 0xFF;
  22. bitsLeft += 8;
  23. } else {
  24. int pad = 5 - bitsLeft;
  25. buffer <<= pad;
  26. bitsLeft += pad; //-V1026
  27. }
  28. }
  29. int index = 0x1F & (buffer >> (bitsLeft - 5));
  30. bitsLeft -= 5;
  31. putchar(BASE32_ALPHABET[index]);
  32. }
  33. }
  34. static void print_uri_component(const char* data, size_t length) {
  35. const char* c_ptr = data;
  36. const char* last_ptr = data + length;
  37. while(c_ptr < last_ptr) {
  38. const char c = *c_ptr;
  39. if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
  40. c == '-' || c == '_') {
  41. putchar(c);
  42. } else {
  43. printf("%%%02x", c);
  44. }
  45. c_ptr++;
  46. }
  47. }
  48. static void handle(PluginState* plugin_state, FuriString* args, PipeSide* pipe) {
  49. UNUSED(args);
  50. UNUSED(plugin_state);
  51. if(!totp_cli_ensure_authenticated(plugin_state, pipe)) {
  52. return;
  53. }
  54. TOTP_CLI_PRINTF_WARNING("WARNING!\r\n");
  55. TOTP_CLI_PRINTF_WARNING(
  56. "ALL THE INFORMATION (INCL. UNENCRYPTED SECRET) ABOUT ALL THE TOKENS WILL BE EXPORTED AND PRINTED TO THE CONSOLE.\r\n");
  57. TOTP_CLI_PRINTF_WARNING("Confirm this action on your Flipper\r\n");
  58. DialogMessage* message = dialog_message_alloc();
  59. dialog_message_set_buttons(message, "No", NULL, "Yes");
  60. dialog_message_set_text(
  61. message,
  62. "Would you like to\nEXPORT TOKENS AND\nUNENCRYPTED SECRETS?",
  63. SCREEN_WIDTH_CENTER,
  64. SCREEN_HEIGHT_CENTER - 8,
  65. AlignCenter,
  66. AlignCenter);
  67. DialogMessageButton dialog_result = dialog_message_show(plugin_state->dialogs_app, message);
  68. dialog_message_free(message);
  69. if(dialog_result != DialogMessageButtonRight) {
  70. TOTP_CLI_PRINTF_INFO("User has not confirmed\r\n");
  71. return;
  72. }
  73. if(!totp_cli_ensure_authenticated(plugin_state, pipe)) {
  74. return;
  75. }
  76. TokenInfoIteratorContext* iterator_context =
  77. totp_config_get_token_iterator_context(plugin_state);
  78. size_t total_count = totp_token_info_iterator_get_total_count(iterator_context);
  79. TOTP_CLI_LOCK_UI(plugin_state);
  80. size_t original_index = totp_token_info_iterator_get_current_token_index(iterator_context);
  81. TOTP_CLI_NL();
  82. TOTP_CLI_PRINTF("# --- EXPORT LIST BEGIN ---\r\n");
  83. for(size_t i = 0; i < total_count; i++) {
  84. totp_token_info_iterator_go_to(iterator_context, i);
  85. const TokenInfo* token_info = totp_token_info_iterator_get_current_token(iterator_context);
  86. TOTP_CLI_PRINTF("otpauth://%s/", token_info_get_type_as_cstr(token_info));
  87. print_uri_component(
  88. furi_string_get_cstr(token_info->name), furi_string_size(token_info->name));
  89. TOTP_CLI_PRINTF("?secret=");
  90. size_t key_length;
  91. uint8_t* key = totp_crypto_decrypt(
  92. token_info->token,
  93. token_info->token_length,
  94. &plugin_state->crypto_settings,
  95. &key_length);
  96. print_as_base32(key, key_length);
  97. memset_s(key, key_length, 0, key_length);
  98. free(key);
  99. TOTP_CLI_PRINTF("&algorithm=%s", token_info_get_algo_as_cstr(token_info));
  100. TOTP_CLI_PRINTF("&digits=%" PRIu8, token_info->digits);
  101. if(token_info->type == TokenTypeHOTP) {
  102. TOTP_CLI_PRINTF("&counter=%" PRIu64, token_info->counter);
  103. } else {
  104. TOTP_CLI_PRINTF("&period=%" PRIu8, token_info->duration);
  105. }
  106. TOTP_CLI_NL();
  107. }
  108. TOTP_CLI_PRINTF("# --- EXPORT LIST END ---\r\n\r\n");
  109. totp_token_info_iterator_go_to(iterator_context, original_index);
  110. TOTP_CLI_UNLOCK_UI(plugin_state);
  111. }
  112. static const CliPlugin plugin = {.name = "TOTP CLI Plugin: Export", .handle = &handle};
  113. static const FlipperAppPluginDescriptor plugin_descriptor = {
  114. .appid = PLUGIN_APP_ID,
  115. .ep_api_version = PLUGIN_API_VERSION,
  116. .entry_point = &plugin,
  117. };
  118. const FlipperAppPluginDescriptor* totp_cli_export_plugin_ep() {
  119. return &plugin_descriptor;
  120. }