Просмотр исходного кода

Implemented export (#219) (#220)

* feat: implemented export (#219)

* CLang format changes

---------

Co-authored-by: akopachov <akopachov@users.noreply.github.com>
Alexander Kopachov 2 лет назад
Родитель
Сommit
8c8a960202
7 измененных файлов с 153 добавлено и 4 удалено
  1. 6 1
      app_api_table_i.h
  2. 8 0
      application.fam
  3. 2 0
      assets/cli/cli_help.txt
  4. 4 1
      cli/cli.c
  5. 127 0
      cli/plugins/export/export.c
  6. 4 0
      cli/plugins/export/meta.h
  7. 2 2
      cli/plugins/list/list.c

+ 6 - 1
app_api_table_i.h

@@ -4,6 +4,7 @@
 #include <lib/toolbox/args.h>
 #include <lib/toolbox/args.h>
 #include <memset_s.h>
 #include <memset_s.h>
 #include "services/crypto/crypto_facade.h"
 #include "services/crypto/crypto_facade.h"
+#include "types/crypto_settings.h"
 #include "ui/scene_director.h"
 #include "ui/scene_director.h"
 #include "services/config/config.h"
 #include "services/config/config.h"
 #include "services/kb_layouts/kb_layout_provider.h"
 #include "services/kb_layouts/kb_layout_provider.h"
@@ -74,4 +75,8 @@ static constexpr auto app_api_table = sort(create_array_t<sym_entry>(
     API_METHOD(
     API_METHOD(
         totp_kb_layout_provider_get_layout_name,
         totp_kb_layout_provider_get_layout_name,
         bool,
         bool,
-        (AutomationKeyboardLayout, char*, size_t))));
+        (AutomationKeyboardLayout, char*, size_t)),
+    API_METHOD(
+        totp_crypto_decrypt,
+        uint8_t*,
+        (const uint8_t*, const size_t, const CryptoSettings*, size_t*))));

+ 8 - 0
application.fam

@@ -164,3 +164,11 @@ App(
     requires=["totp"],
     requires=["totp"],
     sources=["cli/plugins/notification/notification.c", "cli/cli_shared_methods.c"],
     sources=["cli/plugins/notification/notification.c", "cli/cli_shared_methods.c"],
 )
 )
+
+App(
+    appid="totp_cli_export_plugin",
+    apptype=FlipperAppType.PLUGIN,
+    entry_point="totp_cli_export_plugin_ep",
+    requires=["totp"],
+    sources=["cli/plugins/export/export.c", "cli/cli_shared_methods.c"],
+)

+ 2 - 0
assets/cli/cli_help.txt

@@ -12,6 +12,7 @@ Usage:
   totp (timezone | tz) [<timezone>]
   totp (timezone | tz) [<timezone>]
   totp reset
   totp reset
   totp automation [-k <layout>] [-w <delay>] [<automation>...]
   totp automation [-k <layout>] [-w <delay>] [<automation>...]
+  totp export
 
 
 Commands:
 Commands:
   help, h, ?       Show command usage help
   help, h, ?       Show command usage help
@@ -27,6 +28,7 @@ Commands:
   timezone, tz     Get or set current timezone
   timezone, tz     Get or set current timezone
   reset            Reset application to default settings
   reset            Reset application to default settings
   automation       Get or set automation settings
   automation       Get or set automation settings
+  export           Exports and prints all the tokens into URI-list format
 
 
 Arguments:
 Arguments:
   name          Token name
   name          Token name

+ 4 - 1
cli/cli.c

@@ -17,6 +17,7 @@
 #include "plugins/automation/meta.h"
 #include "plugins/automation/meta.h"
 #include "plugins/details/meta.h"
 #include "plugins/details/meta.h"
 #include "plugins/version/meta.h"
 #include "plugins/version/meta.h"
+#include "plugins/export/meta.h"
 #include "cli_plugin_interface.h"
 #include "cli_plugin_interface.h"
 #include "../app_api_interface.h"
 #include "../app_api_interface.h"
 
 
@@ -137,6 +138,8 @@ static void totp_cli_handler(Cli* cli, FuriString* args, void* context) {
         external_plugin_name = TOTP_CLI_PLUGIN_DETAILS_FILE_NAME;
         external_plugin_name = TOTP_CLI_PLUGIN_DETAILS_FILE_NAME;
     } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_VERSION) == 0) {
     } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_VERSION) == 0) {
         external_plugin_name = TOTP_CLI_PLUGIN_VERSION_FILE_NAME;
         external_plugin_name = TOTP_CLI_PLUGIN_VERSION_FILE_NAME;
+    } else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_EXPORT) == 0) {
+        external_plugin_name = TOTP_CLI_PLUGIN_EXPORT_FILE_NAME;
     } else {
     } else {
         totp_cli_print_unknown_command(cmd);
         totp_cli_print_unknown_command(cmd);
     }
     }
@@ -172,4 +175,4 @@ void totp_cli_unregister_command_handler(TotpCliContext* context) {
 
 
     furi_record_close(RECORD_CLI);
     furi_record_close(RECORD_CLI);
     free(context);
     free(context);
-}
+}

+ 127 - 0
cli/plugins/export/export.c

@@ -0,0 +1,127 @@
+#include <lib/toolbox/args.h>
+#include <flipper_application/flipper_application.h>
+#include "../../../lib/polyfills/memset_s.h"
+#include "../../../services/config/config.h"
+#include "../../../services/crypto/crypto_facade.h"
+#include "../../../ui/scene_director.h"
+#include "../../cli_helpers.h"
+#include "../../cli_plugin_interface.h"
+#include "../../cli_shared_methods.h"
+
+static const char* BASE32_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
+
+static void print_as_base32(const uint8_t* data, size_t length) {
+    int buffer = data[0];
+    size_t next = 1;
+    int bitsLeft = 8;
+    while(bitsLeft > 0 || next < length) {
+        if(bitsLeft < 5) {
+            if(next < length) {
+                buffer <<= 8;
+                buffer |= data[next++] & 0xFF;
+                bitsLeft += 8;
+            } else {
+                int pad = 5 - bitsLeft;
+                buffer <<= pad;
+                bitsLeft += pad; //-V1026
+            }
+        }
+        int index = 0x1F & (buffer >> (bitsLeft - 5));
+        bitsLeft -= 5;
+        putchar(BASE32_ALPHABET[index]);
+    }
+}
+
+static void print_uri_component(const char* data, size_t length) {
+    const char* c_ptr = data;
+    const char* last_ptr = data + length;
+    while(c_ptr < last_ptr) {
+        const char c = *c_ptr;
+        if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
+           c == '-' || c == '_') {
+            putchar(c);
+        } else {
+            printf("%%%x", c);
+        }
+        c_ptr++;
+    }
+}
+
+static void handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
+    UNUSED(args);
+    UNUSED(plugin_state);
+    if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
+        return;
+    }
+
+    TOTP_CLI_PRINTF_WARNING("WARNING!\r\n");
+    TOTP_CLI_PRINTF_WARNING(
+        "ALL THE INFORMATION (INCL. UNENCRYPTED SECRET) ABOUT ALL THE TOKENS WILL BE EXPORTED AND PRINTED TO THE CONSOLE.\r\n");
+    TOTP_CLI_PRINTF_WARNING("Confirm? [y/n]\r\n");
+    fflush(stdout);
+    char user_pick;
+    do {
+        user_pick = tolower(cli_getc(cli));
+    } while(user_pick != 'y' && user_pick != 'n' && user_pick != CliSymbolAsciiCR &&
+            user_pick != CliSymbolAsciiETX && user_pick != CliSymbolAsciiEsc);
+
+    if(user_pick != 'y' && user_pick != CliSymbolAsciiCR) {
+        TOTP_CLI_PRINTF_INFO("User has not confirmed\r\n");
+        return;
+    }
+
+    TokenInfoIteratorContext* iterator_context =
+        totp_config_get_token_iterator_context(plugin_state);
+    size_t total_count = totp_token_info_iterator_get_total_count(iterator_context);
+
+    TOTP_CLI_LOCK_UI(plugin_state);
+
+    size_t original_index = totp_token_info_iterator_get_current_token_index(iterator_context);
+
+    cli_nl();
+    TOTP_CLI_PRINTF("# --- EXPORT LIST BEGIN ---\r\n");
+
+    for(size_t i = 0; i < total_count; i++) {
+        totp_token_info_iterator_go_to(iterator_context, i);
+        const TokenInfo* token_info = totp_token_info_iterator_get_current_token(iterator_context);
+        TOTP_CLI_PRINTF("otpauth://%s/", token_info_get_type_as_cstr(token_info));
+        print_uri_component(
+            furi_string_get_cstr(token_info->name), furi_string_size(token_info->name));
+        TOTP_CLI_PRINTF("?secret=");
+        size_t key_length;
+        uint8_t* key = totp_crypto_decrypt(
+            token_info->token,
+            token_info->token_length,
+            &plugin_state->crypto_settings,
+            &key_length);
+        print_as_base32(key, key_length);
+        memset_s(key, key_length, 0, key_length);
+        free(key);
+        TOTP_CLI_PRINTF("&algorithm=%s", token_info_get_algo_as_cstr(token_info));
+        TOTP_CLI_PRINTF("&digits=%" PRIu8, token_info->digits);
+        if(token_info->type == TokenTypeHOTP) {
+            TOTP_CLI_PRINTF("&counter=%" PRIu64, token_info->counter);
+        } else {
+            TOTP_CLI_PRINTF("&period=%" PRIu8, token_info->duration);
+        }
+        cli_nl();
+    }
+
+    TOTP_CLI_PRINTF("# --- EXPORT LIST END ---\r\n\r\n");
+
+    totp_token_info_iterator_go_to(iterator_context, original_index);
+
+    TOTP_CLI_UNLOCK_UI(plugin_state);
+}
+
+static const CliPlugin plugin = {.name = "TOTP CLI Plugin: Export", .handle = &handle};
+
+static const FlipperAppPluginDescriptor plugin_descriptor = {
+    .appid = PLUGIN_APP_ID,
+    .ep_api_version = PLUGIN_API_VERSION,
+    .entry_point = &plugin,
+};
+
+const FlipperAppPluginDescriptor* totp_cli_export_plugin_ep() {
+    return &plugin_descriptor;
+}

+ 4 - 0
cli/plugins/export/meta.h

@@ -0,0 +1,4 @@
+#pragma once
+
+#define TOTP_CLI_COMMAND_EXPORT "export"
+#define TOTP_CLI_PLUGIN_EXPORT_FILE_NAME "totp_cli_export_plugin"

+ 2 - 2
cli/plugins/list/list.c

@@ -39,7 +39,7 @@ static void handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
         totp_config_get_token_iterator_context(plugin_state);
         totp_config_get_token_iterator_context(plugin_state);
     size_t total_count = totp_token_info_iterator_get_total_count(iterator_context);
     size_t total_count = totp_token_info_iterator_get_total_count(iterator_context);
     if(total_count <= 0) {
     if(total_count <= 0) {
-        TOTP_CLI_PRINTF("There are no tokens");
+        TOTP_CLI_PRINTF("There are no tokens\r\n");
         return;
         return;
     }
     }
 
 
@@ -79,4 +79,4 @@ static const FlipperAppPluginDescriptor plugin_descriptor = {
 
 
 const FlipperAppPluginDescriptor* totp_cli_list_plugin_ep() {
 const FlipperAppPluginDescriptor* totp_cli_list_plugin_ep() {
     return &plugin_descriptor;
     return &plugin_descriptor;
-}
+}