mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-13 08:38:35 -07:00
@@ -7,19 +7,35 @@
|
||||
#include "../../../services/config/config.h"
|
||||
#include "../../cli_helpers.h"
|
||||
#include <memset_s.h>
|
||||
#include "../../../services/crypto/crypto.h"
|
||||
#include "../../../services/crypto/crypto_facade.h"
|
||||
#include "../../../ui/scene_director.h"
|
||||
|
||||
#define TOTP_CLI_COMMAND_PIN_COMMAND_SET "set"
|
||||
#define TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE "remove"
|
||||
#define TOTP_CLI_COMMAND_PIN_ARG_NEW_CRYPTO_KEY_SLOT_PREFIX "-c"
|
||||
#define TOTP_CLI_COMMAND_PIN_ARG_NEW_CRYPTO_KEY_SLOT "slot"
|
||||
|
||||
void totp_cli_command_pin_docopt_commands() {
|
||||
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_PIN " Set\\change\\remove PIN\r\n");
|
||||
}
|
||||
|
||||
void totp_cli_command_pin_docopt_usage() {
|
||||
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_NAME " " TOTP_CLI_COMMAND_PIN " " DOCOPT_REQUIRED(
|
||||
TOTP_CLI_COMMAND_PIN_COMMAND_SET " | " TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE) "\r\n");
|
||||
TOTP_CLI_PRINTF(
|
||||
" " TOTP_CLI_COMMAND_NAME " " TOTP_CLI_COMMAND_PIN
|
||||
" " DOCOPT_REQUIRED(TOTP_CLI_COMMAND_PIN_COMMAND_SET " | " TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE) " " DOCOPT_OPTIONAL(
|
||||
DOCOPT_OPTION(
|
||||
TOTP_CLI_COMMAND_PIN_ARG_NEW_CRYPTO_KEY_SLOT_PREFIX,
|
||||
DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_PIN_ARG_NEW_CRYPTO_KEY_SLOT))) "\r\n");
|
||||
}
|
||||
|
||||
void totp_cli_command_pin_docopt_options() {
|
||||
TOTP_CLI_PRINTF(
|
||||
" " DOCOPT_OPTION(
|
||||
TOTP_CLI_COMMAND_PIN_ARG_NEW_CRYPTO_KEY_SLOT_PREFIX,
|
||||
DOCOPT_ARGUMENT(
|
||||
TOTP_CLI_COMMAND_PIN_ARG_NEW_CRYPTO_KEY_SLOT)) " New crypto key slot. Must be between %d and %d\r\n",
|
||||
ACCEPTABLE_CRYPTO_KEY_SLOT_START,
|
||||
ACCEPTABLE_CRYPTO_KEY_SLOT_END);
|
||||
}
|
||||
|
||||
static inline uint8_t totp_cli_key_to_pin_code(uint8_t key) {
|
||||
@@ -89,35 +105,49 @@ void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cl
|
||||
|
||||
bool do_change = false;
|
||||
bool do_remove = false;
|
||||
UNUSED(do_remove);
|
||||
if(args_read_string_and_trim(args, temp_str)) {
|
||||
uint8_t crypto_key_slot = plugin_state->crypto_settings.crypto_key_slot;
|
||||
|
||||
bool arguments_parsed = true;
|
||||
while(args_read_string_and_trim(args, temp_str)) {
|
||||
if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_PIN_COMMAND_SET) == 0) {
|
||||
do_change = true;
|
||||
} else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE) == 0) {
|
||||
do_remove = true;
|
||||
} else if(
|
||||
furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_PIN_ARG_NEW_CRYPTO_KEY_SLOT_PREFIX) ==
|
||||
0) {
|
||||
if(!args_read_uint8_and_trim(args, &crypto_key_slot) ||
|
||||
!totp_crypto_check_key_slot(crypto_key_slot)) {
|
||||
TOTP_CLI_PRINTF_ERROR("Slot \"%" PRIu8 "\" can not be used\r\n", crypto_key_slot);
|
||||
arguments_parsed = false;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
totp_cli_print_invalid_arguments();
|
||||
arguments_parsed = false;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
totp_cli_print_invalid_arguments();
|
||||
}
|
||||
|
||||
if((do_change || do_remove) && totp_cli_ensure_authenticated(plugin_state, cli)) {
|
||||
if(!(do_change || do_remove) || (do_change && do_remove)) {
|
||||
totp_cli_print_invalid_arguments();
|
||||
arguments_parsed = false;
|
||||
}
|
||||
|
||||
if(arguments_parsed && totp_cli_ensure_authenticated(plugin_state, cli)) {
|
||||
TOTP_CLI_LOCK_UI(plugin_state);
|
||||
do {
|
||||
uint8_t old_iv[TOTP_IV_SIZE];
|
||||
memcpy(&old_iv[0], &plugin_state->iv[0], TOTP_IV_SIZE);
|
||||
uint8_t new_pin[TOTP_IV_SIZE];
|
||||
memset(&new_pin[0], 0, TOTP_IV_SIZE);
|
||||
uint8_t new_pin[CRYPTO_IV_LENGTH];
|
||||
memset(&new_pin[0], 0, CRYPTO_IV_LENGTH);
|
||||
uint8_t new_pin_length = 0;
|
||||
if(do_change) {
|
||||
if(!totp_cli_read_pin(cli, &new_pin[0], &new_pin_length)) {
|
||||
memset_s(&new_pin[0], TOTP_IV_SIZE, 0, TOTP_IV_SIZE);
|
||||
memset_s(&new_pin[0], CRYPTO_IV_LENGTH, 0, CRYPTO_IV_LENGTH);
|
||||
break;
|
||||
}
|
||||
} else if(do_remove) {
|
||||
new_pin_length = 0;
|
||||
memset(&new_pin[0], 0, TOTP_IV_SIZE);
|
||||
memset(&new_pin[0], 0, CRYPTO_IV_LENGTH);
|
||||
}
|
||||
|
||||
char* backup_path = totp_config_file_backup(plugin_state);
|
||||
@@ -127,7 +157,7 @@ void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cl
|
||||
"Once you make sure everything is fine and works as expected, please delete this backup file\r\n");
|
||||
free(backup_path);
|
||||
} else {
|
||||
memset_s(&new_pin[0], TOTP_IV_SIZE, 0, TOTP_IV_SIZE);
|
||||
memset_s(&new_pin[0], CRYPTO_IV_LENGTH, 0, CRYPTO_IV_LENGTH);
|
||||
TOTP_CLI_PRINTF_ERROR(
|
||||
"An error has occurred during taking backup of config file\r\n");
|
||||
break;
|
||||
@@ -135,10 +165,10 @@ void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cl
|
||||
|
||||
TOTP_CLI_PRINTF("Encrypting...\r\n");
|
||||
|
||||
bool update_result =
|
||||
totp_config_file_update_encryption(plugin_state, new_pin, new_pin_length);
|
||||
bool update_result = totp_config_file_update_encryption(
|
||||
plugin_state, crypto_key_slot, new_pin, new_pin_length);
|
||||
|
||||
memset_s(&new_pin[0], TOTP_IV_SIZE, 0, TOTP_IV_SIZE);
|
||||
memset_s(&new_pin[0], CRYPTO_IV_LENGTH, 0, CRYPTO_IV_LENGTH);
|
||||
|
||||
totp_cli_delete_last_line();
|
||||
|
||||
|
||||
@@ -7,4 +7,5 @@
|
||||
|
||||
void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cli* cli);
|
||||
void totp_cli_command_pin_docopt_commands();
|
||||
void totp_cli_command_pin_docopt_usage();
|
||||
void totp_cli_command_pin_docopt_usage();
|
||||
void totp_cli_command_pin_docopt_options();
|
||||
Reference in New Issue
Block a user