mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-26 05:54:46 -07:00
Update and fix TOTP
fix - added this in every button event switch:
default:
break;
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
#include "commands/timezone/timezone.h"
|
||||
#include "commands/help/help.h"
|
||||
|
||||
static void totp_cli_print_unknown_command(FuriString* unknown_command) {
|
||||
static void totp_cli_print_unknown_command(const FuriString* unknown_command) {
|
||||
TOTP_CLI_PRINTF(
|
||||
"Command \"%s\" is unknown. Use \"" TOTP_CLI_COMMAND_HELP
|
||||
"\" command to get list of available commands.",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "cli_helpers.h"
|
||||
#include <cli/cli.h>
|
||||
|
||||
bool totp_cli_ensure_authenticated(PluginState* plugin_state, Cli* cli) {
|
||||
bool totp_cli_ensure_authenticated(const PluginState* plugin_state, Cli* cli) {
|
||||
if(plugin_state->current_scene == TotpSceneAuthentication) {
|
||||
TOTP_CLI_PRINTF("Pleases enter PIN on your flipper device\r\n");
|
||||
|
||||
@@ -11,7 +11,6 @@ bool totp_cli_ensure_authenticated(PluginState* plugin_state, Cli* cli) {
|
||||
}
|
||||
|
||||
TOTP_CLI_DELETE_LAST_LINE();
|
||||
fflush(stdout);
|
||||
|
||||
if(plugin_state->current_scene == TotpSceneAuthentication) {
|
||||
return false;
|
||||
|
||||
@@ -13,16 +13,28 @@
|
||||
#define DOCOPT_OPTIONS "[options]"
|
||||
#define DOCOPT_DEFAULT(val) "[default: " val "]"
|
||||
|
||||
#define TOTP_CLI_PRINTF(format, ...) \
|
||||
_Pragma(STRINGIFY(GCC diagnostic push)); \
|
||||
_Pragma(STRINGIFY(GCC diagnostic ignored "-Wdouble-promotion")); \
|
||||
printf(format, ##__VA_ARGS__); \
|
||||
_Pragma(STRINGIFY(GCC diagnostic pop));
|
||||
#define TOTP_CLI_PRINTF(format, ...) \
|
||||
do { \
|
||||
_Pragma(STRINGIFY(GCC diagnostic push)) \
|
||||
_Pragma(STRINGIFY(GCC diagnostic ignored "-Wdouble-promotion")) \
|
||||
printf(format, ##__VA_ARGS__); \
|
||||
_Pragma(STRINGIFY(GCC diagnostic pop)) \
|
||||
} while(false)
|
||||
|
||||
#define TOTP_CLI_DELETE_LAST_LINE() \
|
||||
TOTP_CLI_PRINTF("\033[A\33[2K\r"); \
|
||||
fflush(stdout)
|
||||
|
||||
#define TOTP_CLI_DELETE_CURRENT_LINE() \
|
||||
TOTP_CLI_PRINTF("\33[2K\r"); \
|
||||
fflush(stdout)
|
||||
|
||||
#define TOTP_CLI_DELETE_LAST_CHAR() \
|
||||
TOTP_CLI_PRINTF("\b \b"); \
|
||||
fflush(stdout)
|
||||
|
||||
#define TOTP_CLI_DELETE_LAST_LINE() TOTP_CLI_PRINTF("\033[A\33[2K\r")
|
||||
#define TOTP_CLI_DELETE_CURRENT_LINE() TOTP_CLI_PRINTF("\33[2K\r")
|
||||
#define TOTP_CLI_PRINT_INVALID_ARGUMENTS() \
|
||||
TOTP_CLI_PRINTF( \
|
||||
"Invalid command arguments. use \"help\" command to get list of available commands")
|
||||
|
||||
bool totp_cli_ensure_authenticated(PluginState* plugin_state, Cli* cli);
|
||||
bool totp_cli_ensure_authenticated(const PluginState* plugin_state, Cli* cli);
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
#define TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX "-d"
|
||||
#define TOTP_CLI_COMMAND_ADD_ARG_UNSECURE_PREFIX "-u"
|
||||
|
||||
static bool token_info_set_digits_from_str(TokenInfo* token_info, FuriString* str) {
|
||||
static bool token_info_set_digits_from_str(TokenInfo* token_info, const FuriString* str) {
|
||||
switch(furi_string_get_char(str, 0)) {
|
||||
case '6':
|
||||
token_info->digits = TOTP_6_DIGITS;
|
||||
@@ -27,7 +27,7 @@ static bool token_info_set_digits_from_str(TokenInfo* token_info, FuriString* st
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool token_info_set_algo_from_str(TokenInfo* token_info, FuriString* str) {
|
||||
static bool token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str) {
|
||||
if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME) == 0) {
|
||||
token_info->algo = SHA1;
|
||||
return true;
|
||||
@@ -79,10 +79,53 @@ void totp_cli_command_add_docopt_options() {
|
||||
TOTP_CLI_COMMAND_ADD_ARG_UNSECURE_PREFIX) " Show console user input as-is without masking\r\n");
|
||||
}
|
||||
|
||||
static void furi_string_secure_free(FuriString* str) {
|
||||
for(long i = furi_string_size(str) - 1; i >= 0; i--) {
|
||||
furi_string_set_char(str, i, '\0');
|
||||
}
|
||||
|
||||
furi_string_free(str);
|
||||
}
|
||||
|
||||
static bool totp_cli_read_secret(Cli* cli, FuriString* out_str, bool mask_user_input) {
|
||||
uint8_t c;
|
||||
while(cli_read(cli, &c, 1) == 1) {
|
||||
if(c == CliSymbolAsciiEsc) {
|
||||
// Some keys generating escape-sequences
|
||||
// We need to ignore them as we case about alpha-numerics only
|
||||
uint8_t c2;
|
||||
cli_read_timeout(cli, &c2, 1, 0);
|
||||
cli_read_timeout(cli, &c2, 1, 0);
|
||||
} else if(c == CliSymbolAsciiETX) {
|
||||
TOTP_CLI_DELETE_CURRENT_LINE();
|
||||
TOTP_CLI_PRINTF("Cancelled by user\r\n");
|
||||
return false;
|
||||
} else if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
|
||||
if(mask_user_input) {
|
||||
putc('*', stdout);
|
||||
} else {
|
||||
putc(c, stdout);
|
||||
}
|
||||
fflush(stdout);
|
||||
furi_string_push_back(out_str, c);
|
||||
} else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
|
||||
size_t out_str_size = furi_string_size(out_str);
|
||||
if(out_str_size > 0) {
|
||||
TOTP_CLI_DELETE_LAST_CHAR();
|
||||
furi_string_left(out_str, out_str_size - 1);
|
||||
}
|
||||
} else if(c == CliSymbolAsciiCR) {
|
||||
cli_nl();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
TOTP_CLI_DELETE_LAST_LINE();
|
||||
return true;
|
||||
}
|
||||
|
||||
void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
|
||||
FuriString* temp_str = furi_string_alloc();
|
||||
const char* temp_cstr;
|
||||
|
||||
TokenInfo* token_info = token_info_alloc();
|
||||
|
||||
// Reading token name
|
||||
@@ -93,9 +136,9 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
|
||||
return;
|
||||
}
|
||||
|
||||
temp_cstr = furi_string_get_cstr(temp_str);
|
||||
token_info->name = malloc(strlen(temp_cstr) + 1);
|
||||
strcpy(token_info->name, temp_cstr);
|
||||
size_t temp_cstr_len = furi_string_size(temp_str);
|
||||
token_info->name = malloc(temp_cstr_len + 1);
|
||||
strlcpy(token_info->name, furi_string_get_cstr(temp_str), temp_cstr_len + 1);
|
||||
|
||||
// Read optional arguments
|
||||
bool mask_user_input = true;
|
||||
@@ -142,59 +185,25 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
|
||||
// Reading token secret
|
||||
furi_string_reset(temp_str);
|
||||
TOTP_CLI_PRINTF("Enter token secret and confirm with [ENTER]\r\n");
|
||||
|
||||
uint8_t c;
|
||||
while(cli_read(cli, &c, 1) == 1) {
|
||||
if(c == CliSymbolAsciiEsc) {
|
||||
uint8_t c2;
|
||||
cli_read_timeout(cli, &c2, 1, 0);
|
||||
cli_read_timeout(cli, &c2, 1, 0);
|
||||
} else if(c == CliSymbolAsciiETX) {
|
||||
TOTP_CLI_DELETE_CURRENT_LINE();
|
||||
TOTP_CLI_PRINTF("Cancelled by user");
|
||||
furi_string_free(temp_str);
|
||||
token_info_free(token_info);
|
||||
return;
|
||||
} else if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
|
||||
if(mask_user_input) {
|
||||
putc('*', stdout);
|
||||
} else {
|
||||
putc(c, stdout);
|
||||
}
|
||||
fflush(stdout);
|
||||
furi_string_push_back(temp_str, c);
|
||||
} else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
|
||||
size_t temp_str_size = furi_string_size(temp_str);
|
||||
if(temp_str_size > 0) {
|
||||
TOTP_CLI_PRINTF("\b \b");
|
||||
fflush(stdout);
|
||||
furi_string_left(temp_str, temp_str_size - 1);
|
||||
}
|
||||
} else if(c == CliSymbolAsciiCR) {
|
||||
cli_nl();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
temp_cstr = furi_string_get_cstr(temp_str);
|
||||
|
||||
TOTP_CLI_DELETE_LAST_LINE();
|
||||
|
||||
if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
|
||||
furi_string_free(temp_str);
|
||||
if(!totp_cli_read_secret(cli, temp_str, mask_user_input) ||
|
||||
!totp_cli_ensure_authenticated(plugin_state, cli)) {
|
||||
furi_string_secure_free(temp_str);
|
||||
token_info_free(token_info);
|
||||
return;
|
||||
}
|
||||
|
||||
if(!token_info_set_secret(token_info, temp_cstr, strlen(temp_cstr), plugin_state->iv)) {
|
||||
if(!token_info_set_secret(
|
||||
token_info,
|
||||
furi_string_get_cstr(temp_str),
|
||||
furi_string_size(temp_str),
|
||||
plugin_state->iv)) {
|
||||
TOTP_CLI_PRINTF("Token secret seems to be invalid and can not be parsed\r\n");
|
||||
furi_string_free(temp_str);
|
||||
furi_string_secure_free(temp_str);
|
||||
token_info_free(token_info);
|
||||
return;
|
||||
}
|
||||
|
||||
furi_string_reset(temp_str);
|
||||
furi_string_free(temp_str);
|
||||
furi_string_secure_free(temp_str);
|
||||
|
||||
bool load_generate_token_scene = false;
|
||||
if(plugin_state->current_scene == TotpSceneGenerateToken) {
|
||||
@@ -202,11 +211,7 @@ void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cl
|
||||
load_generate_token_scene = true;
|
||||
}
|
||||
|
||||
if(plugin_state->tokens_list == NULL) {
|
||||
plugin_state->tokens_list = list_init_head(token_info);
|
||||
} else {
|
||||
list_add(plugin_state->tokens_list, token_info);
|
||||
}
|
||||
TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, token_info);
|
||||
plugin_state->tokens_count++;
|
||||
totp_config_file_save_new_token(token_info);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user