Explorar el Código

Merge totp from https://github.com/akopachov/flipper-zero_authenticator

Willy-JL hace 8 meses
padre
commit
3909c73c7a

+ 1 - 1
totp/.gitsubtree

@@ -1,2 +1,2 @@
 https://github.com/xMasterX/all-the-plugins dev base_pack/totp 3d5f458db12f2dcb7bc930506383a88ee3c0fdda
-https://github.com/akopachov/flipper-zero_authenticator master totp 6e9b94b319a8dc945b82e8185f3981ce49f3246d
+https://github.com/akopachov/flipper-zero_authenticator master totp 8099f41067f628fce98123f56d2c853e9d250e52

+ 5 - 1
totp/.ofwcatalog/CHANGELOG.md

@@ -1,6 +1,10 @@
 # Changelog
 
-## v 5.17.3 - Apr 11 2025
+## v5.18.0 - Apr 29 2025
+
+* fix: tokens longer than 64 bytes are leading to incorrect codes ([#260](https://github.com/akopachov/flipper-zero_authenticator/issues/260))
+
+## v5.17.3 - Apr 11 2025
 
 * fix: compatibility with SDK version 1.3 f7 ([#256](https://github.com/akopachov/flipper-zero_authenticator/issues/256))
 

+ 1 - 1
totp/application.fam

@@ -7,7 +7,7 @@ App(
     requires=["gui", "cli", "dialogs", "storage", "input", "notification", "bt"],
     stack_size=2 * 1024,
     order=20,
-    fap_version="5.173",
+    fap_version="5.180",
     fap_author="Alexander Kopachov (@akopachov)",
     fap_description="Software-based TOTP/HOTP authenticator for Flipper Zero device",
     fap_weburl="https://github.com/akopachov/flipper-zero_authenticator",

+ 3 - 1
totp/cli/plugins/export/export.c

@@ -106,7 +106,9 @@ static void handle(PluginState* plugin_state, FuriString* args, PipeSide* pipe)
             token_info->token_length,
             &plugin_state->crypto_settings,
             &key_length);
-        print_as_base32(key, key_length);
+        size_t plain_key_length =
+            token_info->token_plain_length > 0 ? token_info->token_plain_length : key_length;
+        print_as_base32(key, plain_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));

+ 2 - 1
totp/services/config/constants.h

@@ -5,11 +5,12 @@
 
 #define CONFIG_FILE_DIRECTORY_PATH EXT_PATH("apps_data/totp")
 #define CONFIG_FILE_HEADER "Flipper TOTP plugin config file"
-#define CONFIG_FILE_ACTUAL_VERSION (13)
+#define CONFIG_FILE_ACTUAL_VERSION (14)
 
 #define TOTP_CONFIG_KEY_TIMEZONE "Timezone"
 #define TOTP_CONFIG_KEY_TOKEN_NAME "TokenName"
 #define TOTP_CONFIG_KEY_TOKEN_SECRET "TokenSecret"
+#define TOTP_CONFIG_KEY_TOKEN_SECRET_LENGTH "TokenSecretLength"
 #define TOTP_CONFIG_KEY_TOKEN_ALGO "TokenAlgo"
 #define TOTP_CONFIG_KEY_TOKEN_DIGITS "TokenDigits"
 #define TOTP_CONFIG_KEY_TOKEN_DURATION "TokenDuration"

+ 11 - 0
totp/services/config/migrations/common_migration.c

@@ -152,6 +152,17 @@ bool totp_config_migrate_to_latest(
                 fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str);
             flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str);
 
+            if(current_version > 13) {
+                flipper_format_read_string(
+                    fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET_LENGTH, temp_str);
+                flipper_format_write_string(
+                    fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET_LENGTH, temp_str);
+            } else {
+                const uint32_t default_secret_length = 0;
+                flipper_format_write_uint32(
+                    fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET_LENGTH, &default_secret_length, 1);
+            }
+
             if(current_version > 1) {
                 flipper_format_read_string(
                     fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str);

+ 17 - 2
totp/services/config/token_info_iterator.c

@@ -178,7 +178,13 @@ static bool
             break;
         }
 
-        uint32_t tmp_uint32 = token_info->algo;
+        uint32_t tmp_uint32 = token_info->token_plain_length;
+        if(!flipper_format_write_uint32(
+               temp_ff, TOTP_CONFIG_KEY_TOKEN_SECRET_LENGTH, &tmp_uint32, 1)) {
+            break;
+        }
+
+        tmp_uint32 = token_info->algo;
         if(!flipper_format_write_uint32(temp_ff, TOTP_CONFIG_KEY_TOKEN_ALGO, &tmp_uint32, 1)) {
             break;
         }
@@ -507,6 +513,7 @@ bool totp_token_info_iterator_go_to(TokenInfoIteratorContext* context, size_t to
         tokenInfo->token_length = 0;
     }
 
+    uint32_t temp_data32;
     if(secret_bytes_count == 1) { // Plain secret key
         FuriString* temp_str = furi_string_alloc();
 
@@ -526,6 +533,7 @@ bool totp_token_info_iterator_go_to(TokenInfoIteratorContext* context, size_t to
             } else {
                 tokenInfo->token = NULL;
                 tokenInfo->token_length = 0;
+                tokenInfo->token_plain_length = 0;
                 FURI_LOG_W(
                     LOGGING_TAG,
                     "Token \"%s\" has invalid secret",
@@ -534,6 +542,7 @@ bool totp_token_info_iterator_go_to(TokenInfoIteratorContext* context, size_t to
         } else {
             tokenInfo->token = NULL;
             tokenInfo->token_length = 0;
+            tokenInfo->token_plain_length = 0;
         }
 
         furi_string_free(temp_str);
@@ -554,9 +563,15 @@ bool totp_token_info_iterator_go_to(TokenInfoIteratorContext* context, size_t to
         } else {
             tokenInfo->token = NULL;
         }
+
+        if(flipper_format_read_uint32(
+               context->config_file, TOTP_CONFIG_KEY_TOKEN_SECRET_LENGTH, &temp_data32, 1)) {
+            tokenInfo->token_plain_length = temp_data32;
+        } else {
+            tokenInfo->token_plain_length = tokenInfo->token_length;
+        }
     }
 
-    uint32_t temp_data32;
     if(!flipper_format_read_uint32(
            context->config_file, TOTP_CONFIG_KEY_TOKEN_ALGO, &temp_data32, 1) ||
        !token_info_set_algo_from_int(tokenInfo, temp_data32)) {

+ 1 - 0
totp/types/token_info.c

@@ -59,6 +59,7 @@ bool token_info_set_secret(
     }
 
     bool result;
+    token_info->token_plain_length = plain_secret_length;
     if(plain_secret_length > 0) {
         if(token_info->token != NULL) {
             free(token_info->token);

+ 5 - 0
totp/types/token_info.h

@@ -180,6 +180,11 @@ typedef struct {
      */
     size_t token_length;
 
+    /**
+     * @brief Plain token secret length 
+     */
+    size_t token_plain_length;
+
     /**
      * @brief User-friendly token name 
      */

+ 2 - 2
totp/version.h

@@ -1,5 +1,5 @@
 #pragma once
 
 #define TOTP_APP_VERSION_MAJOR (5)
-#define TOTP_APP_VERSION_MINOR (17)
-#define TOTP_APP_VERSION_PATCH (3)
+#define TOTP_APP_VERSION_MINOR (18)
+#define TOTP_APP_VERSION_PATCH (0)

+ 4 - 3
totp/workers/generate_totp_code/generate_totp_code.c

@@ -71,19 +71,20 @@ static void generate_totp_code(
         size_t key_length;
         uint8_t* key = totp_crypto_decrypt(
             token_info->token, token_info->token_length, context->crypto_settings, &key_length);
-
         uint64_t otp_code;
+        size_t plain_key_length =
+            token_info->token_plain_length > 0 ? token_info->token_plain_length : key_length;
         if(token_info->type == TokenTypeTOTP) {
             otp_code = totp_at(
                 get_totp_algo_impl(token_info->algo),
                 key,
-                key_length,
+                plain_key_length,
                 current_ts,
                 context->timezone_offset,
                 token_info->duration);
         } else if(token_info->type == TokenTypeHOTP) {
             otp_code = hotp_at(
-                get_totp_algo_impl(token_info->algo), key, key_length, token_info->counter);
+                get_totp_algo_impl(token_info->algo), key, plain_key_length, token_info->counter);
         } else {
             furi_crash("Unknown token type");
         }