Skip to content

Commit

Permalink
update totp
Browse files Browse the repository at this point in the history
  • Loading branch information
xMasterX committed Oct 3, 2023
1 parent dcc5d48 commit 4d791b7
Show file tree
Hide file tree
Showing 30 changed files with 516 additions and 114 deletions.
5 changes: 4 additions & 1 deletion app_api_table_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@ static constexpr auto app_api_table = sort(create_array_t<sym_entry>(
bool,
(TokenInfo*, const char*, size_t, PlainTokenSecretEncoding, const CryptoSettings*)),
API_METHOD(totp_crypto_check_key_slot, bool, (uint8_t)),
API_METHOD(totp_bt_type_code_worker_free, void, (TotpBtTypeCodeWorkerContext*))));
API_METHOD(totp_bt_type_code_worker_free, void, (TotpBtTypeCodeWorkerContext*)),
API_METHOD(token_info_set_token_type_from_str, bool, (TokenInfo*, const FuriString*)),
API_METHOD(token_info_set_token_counter_from_str, bool, (TokenInfo*, const FuriString*)),
API_METHOD(token_info_get_type_as_cstr, const char*, (const TokenInfo*))));
4 changes: 2 additions & 2 deletions application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ App(
requires=["gui", "cli", "dialogs", "storage", "input", "notification", "bt"],
stack_size=2 * 1024,
order=20,
fap_version="5.00",
fap_version="5.50",
fap_author="Alexander Kopachov (@akopachov)",
fap_description="Software-based TOTP authenticator for Flipper Zero device",
fap_description="Software-based TOTP/HOTP authenticator for Flipper Zero device",
fap_weburl="https://github.com/akopachov/flipper-zero_authenticator",
fap_category="Tools",
fap_icon_assets="images",
Expand Down
8 changes: 5 additions & 3 deletions assets/cli/cli_help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Usage:
totp version
totp (list | ls)
totp (lsattr | cat) <index>
totp (add | mk | new) <name> [-a <algo>] [-e <encoding>] [-d <digits>] [-l <duration>] [-u] [-b <feature>]...
totp (update) <index> [-a <algo>] [-e <encoding>] [-n <name>] [-d <digits>] [-l <duration>] [-u] [-s] [-b <feature>]...
totp (add | mk | new) <name> [-t <type>] [-i <counter>] [-a <algo>] [-e <encoding>] [-d <digits>] [-l <duration>] [-u] [-b <feature>]...
totp (update) <index> [-t <type>] [-i <counter>] [-a <algo>] [-e <encoding>] [-n <name>] [-d <digits>] [-l <duration>] [-u] [-s] [-b <feature>]...
totp (delete | rm) <index> [-f]
totp (move | mv) <index> <new_index>
totp pin (set | remove) [-c <slot>]
Expand Down Expand Up @@ -37,10 +37,12 @@ Arguments:
automation Automation method to be set. Must be one of: none, usb, bt

Options:
-t <type> Token type. Must be one of: totp, hotp [default: totp]
-i <counter> Token initial counter. Applicable for HOTP tokens only. Must be positive integer number [default: 0]
-a <algo> Token hashing algorithm. Must be one of: sha1, sha256, sha512, steam [default: sha1]
-d <digits> Number of digits to generate, one of: 5, 6, 8 [default: 6]
-e <encoding> Token secret encoding, one of base32, base64 [default: base32]
-l <duration> Token lifetime duration in seconds, between: 15 and 255 [default: 30]
-l <duration> Token lifetime duration in seconds. Applicable for TOTP tokens only.Must be between: 15 and 255 [default: 30]
-u Show console user input as-is without masking
-b <feature> Token automation features to be enabled. Must be one of: none, enter, tab [default: none]
# none - No features
Expand Down
17 changes: 13 additions & 4 deletions cli/plugins/details/details.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ typedef void (*TOTP_CLI_DETAILS_AUTOMATION_FEATURE_ITEM_FORMATTER)(
typedef void (*TOTP_CLI_DETAILS_CSTR_FORMATTER)(const char* key, const char* value);
typedef void (*TOTP_CLI_DETAILS_UINT8T_FORMATTER)(const char* key, uint8_t value);
typedef void (*TOTP_CLI_DETAILS_SIZET_FORMATTER)(const char* key, size_t value);
typedef void (*TOTP_CLI_DETAILS_UINT64T_FORMATTER)(const char* key, uint64_t value);

typedef struct {
const TOTP_CLI_DETAILS_HEADER_FORMATTER header_formatter;
Expand All @@ -27,6 +28,7 @@ typedef struct {
const TOTP_CLI_DETAILS_CSTR_FORMATTER cstr_formatter;
const TOTP_CLI_DETAILS_UINT8T_FORMATTER uint8t_formatter;
const TOTP_CLI_DETAILS_SIZET_FORMATTER sizet_formatter;
const TOTP_CLI_DETAILS_UINT64T_FORMATTER uint64t_formatter;
} TotpCliDetailsFormatter;

static const TotpCliDetailsFormatter available_formatters[] = {
Expand All @@ -35,14 +37,16 @@ static const TotpCliDetailsFormatter available_formatters[] = {
.automation_feature_item_formatter = &details_output_formatter_print_automation_feature_table,
.cstr_formatter = &details_output_formatter_print_cstr_table,
.uint8t_formatter = &details_output_formatter_print_uint8t_table,
.sizet_formatter = &details_output_formatter_print_sizet_table},
.sizet_formatter = &details_output_formatter_print_sizet_table,
.uint64t_formatter = &details_output_formatter_print_uint64t_table},

{.header_formatter = &details_output_formatter_print_header_tsv,
.footer_formatter = &details_output_formatter_print_footer_tsv,
.automation_feature_item_formatter = &details_output_formatter_print_automation_feature_tsv,
.cstr_formatter = &details_output_formatter_print_cstr_tsv,
.uint8t_formatter = &details_output_formatter_print_uint8t_tsv,
.sizet_formatter = &details_output_formatter_print_sizet_tsv},
.sizet_formatter = &details_output_formatter_print_sizet_tsv,
.uint64t_formatter = &details_output_formatter_print_uint64t_tsv},
};

static void print_automation_features(
Expand Down Expand Up @@ -103,10 +107,15 @@ static void handle(PluginState* plugin_state, FuriString* args, Cli* cli) {

(*formatter->header_formatter)();
(*formatter->sizet_formatter)("Index", token_number);
(*formatter->cstr_formatter)("Type", token_info_get_type_as_cstr(token_info));
(*formatter->cstr_formatter)("Name", furi_string_get_cstr(token_info->name));
(*formatter->cstr_formatter)("Hashing algorithm", token_info_get_algo_as_cstr(token_info));
(*formatter->uint8t_formatter)("Number of digits", token_info->digits);
(*formatter->uint8t_formatter)("Token lifetime", token_info->duration);
if(token_info->type == TokenTypeTOTP) {
(*formatter->uint8t_formatter)("Token lifetime", token_info->duration);
} else if(token_info->type == TokenTypeHOTP) {
(*formatter->uint64t_formatter)("Token counter", token_info->counter);
}
print_automation_features(token_info, formatter);
(*formatter->footer_formatter)();
} else {
Expand All @@ -128,4 +137,4 @@ static const FlipperAppPluginDescriptor plugin_descriptor = {

const FlipperAppPluginDescriptor* totp_cli_details_plugin_ep() {
return &plugin_descriptor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,35 @@
#include "../../../../cli_helpers.h"

void details_output_formatter_print_header_table() {
TOTP_CLI_PRINTF("+----------------------+------------------------------+\r\n");
TOTP_CLI_PRINTF("| %-20s | %-28s |\r\n", "Property", "Value");
TOTP_CLI_PRINTF("+----------------------+------------------------------+\r\n");
TOTP_CLI_PRINTF("+----------------------+-------------------------------+\r\n");
TOTP_CLI_PRINTF("| %-20s | %-29s |\r\n", "Property", "Value");
TOTP_CLI_PRINTF("+----------------------+-------------------------------+\r\n");
}

void details_output_formatter_print_footer_table() {
TOTP_CLI_PRINTF("+----------------------+------------------------------+\r\n");
TOTP_CLI_PRINTF("+----------------------+-------------------------------+\r\n");
}

void details_output_formatter_print_automation_feature_table(
const char* key,
const char* feature,
bool* header_printed) {
TOTP_CLI_PRINTF("| %-20s | %-28.28s |\r\n", *header_printed ? "" : key, feature);
TOTP_CLI_PRINTF("| %-20s | %-29.29s |\r\n", *header_printed ? "" : key, feature);
*header_printed = true;
}

void details_output_formatter_print_cstr_table(const char* key, const char* value) {
TOTP_CLI_PRINTF("| %-20s | %-28.28s |\r\n", key, value);
TOTP_CLI_PRINTF("| %-20s | %-29.29s |\r\n", key, value);
}

void details_output_formatter_print_uint8t_table(const char* key, uint8_t value) {
TOTP_CLI_PRINTF("| %-20s | %-28" PRIu8 " |\r\n", key, value);
TOTP_CLI_PRINTF("| %-20s | %-29" PRIu8 " |\r\n", key, value);
}

void details_output_formatter_print_sizet_table(const char* key, size_t value) {
TOTP_CLI_PRINTF("| %-20s | %-28" PRIu16 " |\r\n", key, value);
TOTP_CLI_PRINTF("| %-20s | %-29" PRIu16 " |\r\n", key, value);
}

void details_output_formatter_print_uint64t_table(const char* key, uint64_t value) {
TOTP_CLI_PRINTF("| %-20s | %-29" PRIu64 " |\r\n", key, value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ void details_output_formatter_print_automation_feature_table(
void details_output_formatter_print_cstr_table(const char* key, const char* value);
void details_output_formatter_print_uint8t_table(const char* key, uint8_t value);
void details_output_formatter_print_sizet_table(const char* key, size_t value);
void details_output_formatter_print_uint64t_table(const char* key, uint64_t value);
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ void details_output_formatter_print_uint8t_tsv(const char* key, uint8_t value) {
void details_output_formatter_print_sizet_tsv(const char* key, size_t value) {
TOTP_CLI_PRINTF("%s\t%" PRIu16 "\r\n", key, value);
}

void details_output_formatter_print_uint64t_tsv(const char* key, uint64_t value) {
TOTP_CLI_PRINTF("%s\t%" PRIu64 "\r\n", key, value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ void details_output_formatter_print_automation_feature_tsv(
void details_output_formatter_print_cstr_tsv(const char* key, const char* value);
void details_output_formatter_print_uint8t_tsv(const char* key, uint8_t value);
void details_output_formatter_print_sizet_tsv(const char* key, size_t value);
void details_output_formatter_print_uint64t_tsv(const char* key, uint64_t value);
14 changes: 7 additions & 7 deletions cli/plugins/list/formatters/table/list_output_formatter_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
#include "../../../../cli_helpers.h"

void list_output_formatter_print_header_table() {
TOTP_CLI_PRINTF("+-----+---------------------------+--------+----+-----+\r\n");
TOTP_CLI_PRINTF("| %-3s | %-25s | %-6s | %-s | %-s |\r\n", "#", "Name", "Algo", "Ln", "Dur");
TOTP_CLI_PRINTF("+-----+---------------------------+--------+----+-----+\r\n");
TOTP_CLI_PRINTF("+-----+---------------------------+--------+----+------+\r\n");
TOTP_CLI_PRINTF("| %-3s | %-25s | %-6s | %-s | %-s |\r\n", "#", "Name", "Algo", "Ln", "Type");
TOTP_CLI_PRINTF("+-----+---------------------------+--------+----+------+\r\n");
}

void list_output_formatter_print_body_item_table(size_t index, const TokenInfo* token_info) {
TOTP_CLI_PRINTF(
"| %-3" PRIu16 " | %-25.25s | %-6s | %-2" PRIu8 " | %-3" PRIu8 " |\r\n",
"| %-3" PRIu16 " | %-25.25s | %-6s | %-2" PRIu8 " | %-4s |\r\n",
index + 1,
furi_string_get_cstr(token_info->name),
token_info_get_algo_as_cstr(token_info),
token_info->digits,
token_info->duration);
token_info_get_type_as_cstr(token_info));
}

void list_output_formatter_print_footer_table() {
TOTP_CLI_PRINTF("+-----+---------------------------+--------+----+-----+\r\n");
}
TOTP_CLI_PRINTF("+-----+---------------------------+--------+----+------+\r\n");
}
8 changes: 4 additions & 4 deletions cli/plugins/list/formatters/tsv/list_output_formatter_tsv.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
#include "../../../../cli_helpers.h"

void list_output_formatter_print_header_tsv() {
TOTP_CLI_PRINTF("%s\t%s\t%s\t%s\t%s\r\n", "#", "Name", "Algo", "Ln", "Dur");
TOTP_CLI_PRINTF("%s\t%s\t%s\t%s\t%s\r\n", "#", "Name", "Algo", "Ln", "Type");
}

void list_output_formatter_print_body_item_tsv(size_t index, const TokenInfo* token_info) {
TOTP_CLI_PRINTF(
"%" PRIu16 "\t%s\t%s\t%" PRIu8 "\t%" PRIu8 "\r\n",
"%" PRIu16 "\t%s\t%s\t%" PRIu8 "\t%s\r\n",
index + 1,
furi_string_get_cstr(token_info->name),
token_info_get_algo_as_cstr(token_info),
token_info->digits,
token_info->duration);
token_info_get_type_as_cstr(token_info));
}

void list_output_formatter_print_footer_tsv() {
}
}
6 changes: 4 additions & 2 deletions cli/plugins/modify/add/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ static TotpIteratorUpdateTokenResult
!totp_cli_try_read_unsecure_flag(temp_str, &parsed, &mask_user_input) &&
!totp_cli_try_read_automation_features(token_info, temp_str, context_t->args, &parsed) &&
!totp_cli_try_read_plain_token_secret_encoding(
temp_str, context_t->args, &parsed, &token_secret_encoding)) {
temp_str, context_t->args, &parsed, &token_secret_encoding) &&
!totp_cli_try_read_token_type(token_info, temp_str, context_t->args, &parsed) &&
!totp_cli_try_read_token_counter(token_info, temp_str, context_t->args, &parsed)) {
totp_cli_printf_unknown_argument(temp_str);
}

Expand Down Expand Up @@ -123,4 +125,4 @@ static const FlipperAppPluginDescriptor plugin_descriptor = {

const FlipperAppPluginDescriptor* totp_cli_add_plugin_ep() {
return &plugin_descriptor;
}
}
48 changes: 47 additions & 1 deletion cli/plugins/modify/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,50 @@ bool totp_cli_try_read_plain_token_secret_encoding(
}

return false;
}
}

bool totp_cli_try_read_token_type(
TokenInfo* token_info,
FuriString* arg,
FuriString* args,
bool* parsed) {
if(furi_string_cmpi_str(arg, TOTP_CLI_COMMAND_ARG_TYPE_PREFIX) == 0) {
if(!args_read_string_and_trim(args, arg)) {
totp_cli_printf_missed_argument_value(TOTP_CLI_COMMAND_ARG_TYPE_PREFIX);
} else if(!token_info_set_token_type_from_str(token_info, arg)) {
TOTP_CLI_PRINTF_ERROR(
"\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ARG_TYPE_PREFIX
"\"\r\n",
furi_string_get_cstr(arg));
} else {
*parsed = true;
}

return true;
}

return false;
}

bool totp_cli_try_read_token_counter(
TokenInfo* token_info,
FuriString* arg,
FuriString* args,
bool* parsed) {
if(furi_string_cmpi_str(arg, TOTP_CLI_COMMAND_ARG_COUNTER_PREFIX) == 0) {
if(!args_read_string_and_trim(args, arg)) {
totp_cli_printf_missed_argument_value(TOTP_CLI_COMMAND_ARG_COUNTER_PREFIX);
} else if(!token_info_set_token_counter_from_str(token_info, arg)) {
TOTP_CLI_PRINTF_ERROR(
"\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ARG_COUNTER_PREFIX
"\"\r\n",
furi_string_get_cstr(arg));
} else {
*parsed = true;
}

return true;
}

return false;
}
39 changes: 31 additions & 8 deletions cli/plugins/modify/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,15 @@
extern "C" {
#endif

#define TOTP_CLI_COMMAND_ARG_NAME "name"
#define TOTP_CLI_COMMAND_ARG_NAME_PREFIX "-n"
#define TOTP_CLI_COMMAND_ARG_ALGO "algo"
#define TOTP_CLI_COMMAND_ARG_ALGO_PREFIX "-a"
#define TOTP_CLI_COMMAND_ARG_DIGITS "digits"
#define TOTP_CLI_COMMAND_ARG_DIGITS_PREFIX "-d"
#define TOTP_CLI_COMMAND_ARG_UNSECURE_PREFIX "-u"
#define TOTP_CLI_COMMAND_ARG_DURATION "duration"
#define TOTP_CLI_COMMAND_ARG_DURATION_PREFIX "-l"
#define TOTP_CLI_COMMAND_ARG_AUTOMATION_FEATURE_PREFIX "-b"
#define TOTP_CLI_COMMAND_ARG_AUTOMATION_FEATURE "feature"
#define TOTP_CLI_COMMAND_ARG_INDEX "index"
#define TOTP_CLI_COMMAND_ARG_SECRET_ENCODING_PREFIX "-e"
#define TOTP_CLI_COMMAND_ARG_SECRET_ENCODING "encoding"
#define TOTP_CLI_COMMAND_ARG_TYPE_PREFIX "-t"
#define TOTP_CLI_COMMAND_ARG_COUNTER_PREFIX "-i"

/**
* @brief Tries to read token hashing algo
Expand Down Expand Up @@ -96,6 +91,34 @@ bool totp_cli_try_read_plain_token_secret_encoding(
bool* parsed,
PlainTokenSecretEncoding* secret_encoding);

/**
* @brief Tries to read token type
* @param token_info token info to set parsed token type to if successfully read and parsed
* @param arg argument to parse
* @param args rest of arguments
* @param parsed will be set to \c true if token type sucecssfully read and parsed; \c false otherwise
* @return \c true if \c arg represents token type argument; \c false otherwise
*/
bool totp_cli_try_read_token_type(
TokenInfo* token_info,
FuriString* arg,
FuriString* args,
bool* parsed);

/**
* @brief Tries to read token counter
* @param token_info token info to set parsed token counter to if successfully read and parsed
* @param arg argument to parse
* @param args rest of arguments
* @param parsed will be set to \c true if token counter sucecssfully read and parsed; \c false otherwise
* @return \c true if \c arg represents token counter argument; \c false otherwise
*/
bool totp_cli_try_read_token_counter(
TokenInfo* token_info,
FuriString* arg,
FuriString* args,
bool* parsed);

#ifdef __cplusplus
}
#endif
#endif
6 changes: 4 additions & 2 deletions cli/plugins/modify/update/update.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ static TotpIteratorUpdateTokenResult
!totp_cli_try_read_change_secret_flag(temp_str, &parsed, &update_token_secret) &&
!totp_cli_try_read_automation_features(token_info, temp_str, context_t->args, &parsed) &&
!totp_cli_try_read_plain_token_secret_encoding(
temp_str, context_t->args, &parsed, &token_secret_encoding)) {
temp_str, context_t->args, &parsed, &token_secret_encoding) &&
!totp_cli_try_read_token_type(token_info, temp_str, context_t->args, &parsed) &&
!totp_cli_try_read_token_counter(token_info, temp_str, context_t->args, &parsed)) {
totp_cli_printf_unknown_argument(temp_str);
}

Expand Down Expand Up @@ -162,4 +164,4 @@ static const FlipperAppPluginDescriptor plugin_descriptor = {

const FlipperAppPluginDescriptor* totp_cli_update_plugin_ep() {
return &plugin_descriptor;
}
}
4 changes: 3 additions & 1 deletion services/config/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#define CONFIG_FILE_DIRECTORY_PATH EXT_PATH("apps_data/totp")
#define CONFIG_FILE_HEADER "Flipper TOTP plugin config file"
#define CONFIG_FILE_ACTUAL_VERSION (9)
#define CONFIG_FILE_ACTUAL_VERSION (10)

#define TOTP_CONFIG_KEY_TIMEZONE "Timezone"
#define TOTP_CONFIG_KEY_TOKEN_NAME "TokenName"
Expand All @@ -13,6 +13,8 @@
#define TOTP_CONFIG_KEY_TOKEN_DIGITS "TokenDigits"
#define TOTP_CONFIG_KEY_TOKEN_DURATION "TokenDuration"
#define TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES "TokenAutomationFeatures"
#define TOTP_CONFIG_KEY_TOKEN_TYPE "TokenType"
#define TOTP_CONFIG_KEY_TOKEN_COUNTER "TokenCounter"
#define TOTP_CONFIG_KEY_CRYPTO_VERIFY "Crypto"
#define TOTP_CONFIG_KEY_SALT "Salt"
#define TOTP_CONFIG_KEY_PINSET "PinIsSet"
Expand Down
Loading

0 comments on commit 4d791b7

Please sign in to comment.