Skip to content

Commit

Permalink
Implemented BadBT automation (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
akopachov committed Mar 21, 2023
1 parent 97c6116 commit 03a7f93
Show file tree
Hide file tree
Showing 27 changed files with 702 additions and 95 deletions.
8 changes: 7 additions & 1 deletion application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ App(
"dialogs",
"storage",
"input",
"notification"
"notification",
#ifdef TOTP_BADBT_TYPE_ENABLED
"bt"
#endif
],
stack_size=2 * 1024,
order=20,
fap_author="Alexander Kopachov (@akopachov)",
fap_description="Software-based TOTP authenticator for Flipper Zero device",
fap_weburl="https://github.com/akopachov/flipper-zero_authenticator",
fap_category="Misc",
fap_icon_assets="images",
fap_icon="totp_10px.png",
Expand Down
3 changes: 3 additions & 0 deletions cli/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "commands/pin/pin.h"
#include "commands/notification/notification.h"
#include "commands/reset/reset.h"
#include "commands/automation/automation.h"

static void totp_cli_print_unknown_command(const FuriString* unknown_command) {
TOTP_CLI_PRINTF_ERROR(
Expand Down Expand Up @@ -57,6 +58,8 @@ static void totp_cli_handler(Cli* cli, FuriString* args, void* context) {
totp_cli_command_pin_handle(plugin_state, args, cli);
} else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_NOTIFICATION) == 0) {
totp_cli_command_notification_handle(plugin_state, args, cli);
} else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_AUTOMATION) == 0) {
totp_cli_command_automation_handle(plugin_state, args, cli);
} else if(furi_string_cmp_str(cmd, TOTP_CLI_COMMAND_RESET) == 0) {
totp_cli_command_reset_handle(cli, cli_context->event_queue);
} else {
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/add/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void totp_cli_command_add_docopt_usage() {
}

void totp_cli_command_add_docopt_arguments() {
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_ADD_ARG_NAME " Token name\r\n");
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_ADD_ARG_NAME " Token name\r\n");
}

void totp_cli_command_add_docopt_options() {
Expand Down
133 changes: 133 additions & 0 deletions cli/commands/automation/automation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include "automation.h"
#include <lib/toolbox/args.h>
#include "../../../services/config/config.h"
#include "../../../ui/scene_director.h"
#include "../../cli_helpers.h"

#define TOTP_CLI_COMMAND_AUTOMATION_ARG_METHOD "automation"
#define TOTP_CLI_COMMAND_AUTOMATION_METHOD_NONE "none"
#define TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB "usb"
#ifdef TOTP_BADBT_TYPE_ENABLED
#define TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT "bt"
#endif

void totp_cli_command_automation_docopt_commands() {
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_AUTOMATION " Get or set automation method\r\n");
}

void totp_cli_command_automation_docopt_usage() {
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_NAME " " TOTP_CLI_COMMAND_AUTOMATION " " DOCOPT_OPTIONAL(
DOCOPT_MULTIPLE(DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_AUTOMATION_ARG_METHOD))) "\r\n");
}

void totp_cli_command_automation_docopt_arguments() {
TOTP_CLI_PRINTF(
" " TOTP_CLI_COMMAND_AUTOMATION_ARG_METHOD
" Automation method to be set. Must be one of [" TOTP_CLI_COMMAND_AUTOMATION_METHOD_NONE
", " TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB
#ifdef TOTP_BADBT_TYPE_ENABLED
", " TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT
#endif
"]\r\n");
}

static void totp_cli_command_automation_print_method(AutomationMethod method, char* color) {
#ifdef TOTP_BADBT_TYPE_ENABLED
bool has_previous_method = false;
#endif
if(method & AutomationMethodBadUsb) {
TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB "\"");
#ifdef TOTP_BADBT_TYPE_ENABLED
has_previous_method = true;
#endif
}

#ifdef TOTP_BADBT_TYPE_ENABLED
if(method & AutomationMethodBadBt) {
if(has_previous_method) {
TOTP_CLI_PRINTF_COLORFUL(color, " and ");
}

TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT "\"");
}
#endif

if(method == AutomationMethodNone) {
TOTP_CLI_PRINTF_COLORFUL(color, "\"" TOTP_CLI_COMMAND_AUTOMATION_METHOD_NONE "\"");
}
}

void totp_cli_command_automation_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
return;
}

FuriString* temp_str = furi_string_alloc();
bool new_method_provided = false;
AutomationMethod new_method = AutomationMethodNone;
bool args_valid = true;
while(args_read_string_and_trim(args, temp_str)) {
if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_AUTOMATION_METHOD_NONE) == 0) {
new_method_provided = true;
new_method = AutomationMethodNone;
} else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_AUTOMATION_METHOD_USB) == 0) {
new_method_provided = true;
new_method |= AutomationMethodBadUsb;
}
#ifdef TOTP_BADBT_TYPE_ENABLED
else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_AUTOMATION_METHOD_BT) == 0) {
new_method_provided = true;
new_method |= AutomationMethodBadBt;
}
#endif
else {
args_valid = false;
break;
}
}

do {
if(!args_valid) {
TOTP_CLI_PRINT_INVALID_ARGUMENTS();
break;
}

if(new_method_provided) {
Scene previous_scene = TotpSceneNone;
if(plugin_state->current_scene == TotpSceneGenerateToken ||
plugin_state->current_scene == TotpSceneAppSettings) {
previous_scene = plugin_state->current_scene;
totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
}

plugin_state->automation_method = new_method;
if(totp_config_file_update_automation_method(new_method) ==
TotpConfigFileUpdateSuccess) {
TOTP_CLI_PRINTF_SUCCESS("Automation method is set to ");
totp_cli_command_automation_print_method(new_method, TOTP_CLI_COLOR_SUCCESS);
cli_nl();
} else {
TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
}

#ifdef TOTP_BADBT_TYPE_ENABLED
if(!(new_method & AutomationMethodBadBt) &&
plugin_state->bt_type_code_worker_context != NULL) {
totp_bt_type_code_worker_free(plugin_state->bt_type_code_worker_context);
plugin_state->bt_type_code_worker_context = NULL;
}
#endif

if(previous_scene != TotpSceneNone) {
totp_scene_director_activate_scene(plugin_state, previous_scene, NULL);
}
} else {
TOTP_CLI_PRINTF_INFO("Current automation method is ");
totp_cli_command_automation_print_method(
plugin_state->automation_method, TOTP_CLI_COLOR_INFO);
cli_nl();
}
} while(false);

furi_string_free(temp_str);
}
11 changes: 11 additions & 0 deletions cli/commands/automation/automation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <cli/cli.h>
#include "../../../types/plugin_state.h"

#define TOTP_CLI_COMMAND_AUTOMATION "automation"

void totp_cli_command_automation_handle(PluginState* plugin_state, FuriString* args, Cli* cli);
void totp_cli_command_automation_docopt_commands();
void totp_cli_command_automation_docopt_usage();
void totp_cli_command_automation_docopt_arguments();
2 changes: 1 addition & 1 deletion cli/commands/delete/delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void totp_cli_command_delete_docopt_usage() {
}

void totp_cli_command_delete_docopt_arguments() {
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_DELETE_ARG_INDEX " Token index in the list\r\n");
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_DELETE_ARG_INDEX " Token index in the list\r\n");
}

void totp_cli_command_delete_docopt_options() {
Expand Down
4 changes: 4 additions & 0 deletions cli/commands/help/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "../pin/pin.h"
#include "../notification/notification.h"
#include "../reset/reset.h"
#include "../automation/automation.h"

void totp_cli_command_help_docopt_commands() {
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_HELP ", " TOTP_CLI_COMMAND_HELP_ALT
Expand All @@ -31,6 +32,7 @@ void totp_cli_command_help_handle() {
totp_cli_command_pin_docopt_usage();
totp_cli_command_notification_docopt_usage();
totp_cli_command_reset_docopt_usage();
totp_cli_command_automation_docopt_usage();
cli_nl();
TOTP_CLI_PRINTF("Commands:\r\n");
totp_cli_command_help_docopt_commands();
Expand All @@ -42,12 +44,14 @@ void totp_cli_command_help_handle() {
totp_cli_command_pin_docopt_commands();
totp_cli_command_notification_docopt_commands();
totp_cli_command_reset_docopt_commands();
totp_cli_command_automation_docopt_commands();
cli_nl();
TOTP_CLI_PRINTF("Arguments:\r\n");
totp_cli_command_add_docopt_arguments();
totp_cli_command_delete_docopt_arguments();
totp_cli_command_timezone_docopt_arguments();
totp_cli_command_notification_docopt_arguments();
totp_cli_command_automation_docopt_arguments();
cli_nl();
TOTP_CLI_PRINTF("Options:\r\n");
totp_cli_command_add_docopt_options();
Expand Down
4 changes: 2 additions & 2 deletions cli/commands/notification/notification.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "../../../ui/scene_director.h"
#include "../../cli_helpers.h"

#define TOTP_CLI_COMMAND_NOTIFICATION_ARG_METHOD "method"
#define TOTP_CLI_COMMAND_NOTIFICATION_ARG_METHOD "notification"
#define TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE "none"
#define TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND "sound"
#define TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "vibro"
Expand All @@ -23,7 +23,7 @@ void totp_cli_command_notification_docopt_usage() {
void totp_cli_command_notification_docopt_arguments() {
TOTP_CLI_PRINTF(
" " TOTP_CLI_COMMAND_NOTIFICATION_ARG_METHOD
" Notification method to be set. Must be one of [" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE
" Notification method to be set. Must be one of [" TOTP_CLI_COMMAND_NOTIFICATION_METHOD_NONE
", " TOTP_CLI_COMMAND_NOTIFICATION_METHOD_SOUND
", " TOTP_CLI_COMMAND_NOTIFICATION_METHOD_VIBRO "]\r\n");
}
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/timezone/timezone.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void totp_cli_command_timezone_docopt_usage() {

void totp_cli_command_timezone_docopt_arguments() {
TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_TIMEZONE_ARG_TIMEZONE
" Timezone offset in hours to be set\r\n");
" Timezone offset in hours to be set\r\n");
}

void totp_cli_command_timezone_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
Expand Down
2 changes: 2 additions & 0 deletions features_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define TOTP_BADBT_TYPE_ENABLED
#define TOTP_BADBT_TYPE_ICON_ENABLED
Binary file added images/hid_ble_10x7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 59 additions & 0 deletions services/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "../list/list.h"
#include "../../types/common.h"
#include "../../types/token_info.h"
#include "../../features_config.h"
#include "migrations/config_migration_v1_to_v2.h"
#include "migrations/config_migration_v2_to_v3.h"

Expand Down Expand Up @@ -136,6 +137,14 @@ static TotpConfigFileOpenResult totp_open_config_file(Storage* storage, FlipperF
flipper_format_write_uint32(
fff_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1);

tmp_uint32 = AutomationMethodBadUsb;
flipper_format_write_comment_cstr(fff_data_file, " ");
flipper_format_write_comment_cstr(
fff_data_file,
"Automation method (0 - None, 1 - BadUSB, 2 - BadBT, 3 - BadUSB and BadBT)");
flipper_format_write_uint32(
fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1);

FuriString* temp_str = furi_string_alloc();

flipper_format_write_comment_cstr(fff_data_file, " ");
Expand Down Expand Up @@ -329,6 +338,33 @@ TotpConfigFileUpdateResult
return update_result;
}

TotpConfigFileUpdateResult
totp_config_file_update_automation_method(AutomationMethod new_automation_method) {
Storage* cfg_storage = totp_open_storage();
FlipperFormat* file;
TotpConfigFileUpdateResult update_result;

if(totp_open_config_file(cfg_storage, &file) == TotpConfigFileOpenSuccess) {
do {
uint32_t tmp_uint32 = new_automation_method;
if(!flipper_format_insert_or_update_uint32(
file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1)) {
update_result = TotpConfigFileUpdateError;
break;
}

update_result = TotpConfigFileUpdateSuccess;
} while(false);

totp_close_config_file(file);
} else {
update_result = TotpConfigFileUpdateError;
}

totp_close_storage();
return update_result;
}

TotpConfigFileUpdateResult totp_config_file_update_user_settings(const PluginState* plugin_state) {
Storage* cfg_storage = totp_open_storage();
FlipperFormat* file;
Expand All @@ -347,6 +383,13 @@ TotpConfigFileUpdateResult totp_config_file_update_user_settings(const PluginSta
break;
}

tmp_uint32 = plugin_state->automation_method;
if(!flipper_format_insert_or_update_uint32(
file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1)) {
update_result = TotpConfigFileUpdateError;
break;
}

update_result = TotpConfigFileUpdateSuccess;
} while(false);

Expand Down Expand Up @@ -409,6 +452,13 @@ TotpConfigFileUpdateResult totp_full_save_config_file(const PluginState* const p
break;
}

tmp_uint32 = plugin_state->automation_method;
if(!flipper_format_write_uint32(
fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1)) {
result = TotpConfigFileUpdateError;
break;
}

bool tokens_written = true;
TOTP_LIST_FOREACH(plugin_state->tokens_list, node, {
const TokenInfo* token_info = node->data;
Expand Down Expand Up @@ -594,6 +644,15 @@ TotpConfigFileOpenResult totp_config_file_load_base(PluginState* const plugin_st
}

plugin_state->notification_method = tmp_uint32;

flipper_format_rewind(fff_data_file);

if(!flipper_format_read_uint32(
fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1)) {
tmp_uint32 = AutomationMethodBadUsb;
}

plugin_state->automation_method = tmp_uint32;
} while(false);

furi_string_free(temp_str);
Expand Down
8 changes: 8 additions & 0 deletions services/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ TotpConfigFileUpdateResult totp_config_file_update_timezone_offset(float new_tim
TotpConfigFileUpdateResult
totp_config_file_update_notification_method(NotificationMethod new_notification_method);

/**
* @brief Updates automation method in an application config file
* @param new_automation_method new automation method to be set
* @return Config file update result
*/
TotpConfigFileUpdateResult
totp_config_file_update_automation_method(AutomationMethod new_automation_method);

/**
* @brief Updates application user settings
* @param plugin_state application state
Expand Down
1 change: 1 addition & 0 deletions services/config/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define TOTP_CONFIG_KEY_BASE_IV "BaseIV"
#define TOTP_CONFIG_KEY_PINSET "PinIsSet"
#define TOTP_CONFIG_KEY_NOTIFICATION_METHOD "NotificationMethod"
#define TOTP_CONFIG_KEY_AUTOMATION_METHOD "AutomationMethod"

#define TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME "sha1"
#define TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME "sha256"
Expand Down
Loading

0 comments on commit 03a7f93

Please sign in to comment.