فهرست منبع

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

Willy-JL 1 سال پیش
والد
کامیت
b1ec79bbb1

+ 2 - 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.110",
+    fap_version="5.120",
     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",
@@ -15,6 +15,7 @@ App(
     fap_icon_assets="images",
     fap_icon="totp_10px.png",
     fap_file_assets="assets",
+    fap_libs=["ble_profile"],
     fap_private_libs=[
         Lib(
             name="base32",

+ 2 - 1
totp/totp_app.c

@@ -166,7 +166,8 @@ static bool totp_plugin_state_init(PluginState* const plugin_state) {
 
 #ifdef TOTP_BADBT_AUTOMATION_ENABLED
     if(plugin_state->automation_method & AutomationMethodBadBt) {
-        plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init();
+        plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init(
+            *((uint16_t*)plugin_state->crypto_settings.crypto_verify_data));
     } else {
         plugin_state->bt_type_code_worker_context = NULL;
     }

+ 2 - 1
totp/ui/scenes/generate_token/totp_scene_generate_token.c

@@ -203,7 +203,8 @@ void totp_scene_generate_token_activate(PluginState* plugin_state) {
 
     if(plugin_state->automation_method & AutomationMethodBadBt) {
         if(plugin_state->bt_type_code_worker_context == NULL) {
-            plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init();
+            plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init(
+                *((uint16_t*)plugin_state->crypto_settings.crypto_verify_data));
         }
         totp_bt_type_code_worker_start(
             plugin_state->bt_type_code_worker_context,

+ 1 - 1
totp/version.h

@@ -1,5 +1,5 @@
 #pragma once
 
 #define TOTP_APP_VERSION_MAJOR (5)
-#define TOTP_APP_VERSION_MINOR (11)
+#define TOTP_APP_VERSION_MINOR (12)
 #define TOTP_APP_VERSION_PATCH (0)

+ 20 - 64
totp/workers/bt_type_code/bt_type_code.c

@@ -1,6 +1,6 @@
 #include "bt_type_code.h"
 #include <furi_hal_bt.h>
-#include <furi_hal_bt_hid.h>
+#include <extra_profiles/hid_profile.h>
 #include <furi_hal_version.h>
 #include <furi/core/thread.h>
 #include <furi/core/mutex.h>
@@ -14,11 +14,6 @@
 #include "../../config/app/config.h"
 #include "../../services/config/constants.h"
 
-#if TOTP_TARGET_FIRMWARE == TOTP_FIRMWARE_XTREME_UL
-#define TOTP_BT_WORKER_BT_ADV_NAME_MAX_LEN FURI_HAL_BT_ADV_NAME_LENGTH
-#define TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN GAP_MAC_ADDR_SIZE
-#endif
-
 #define HID_BT_KEYS_STORAGE_PATH CONFIG_FILE_DIRECTORY_PATH "/.bt_hid.keys"
 
 struct TotpBtTypeCodeWorkerContext {
@@ -28,12 +23,9 @@ struct TotpBtTypeCodeWorkerContext {
     FuriThread* thread;
     FuriMutex* code_buffer_sync;
     Bt* bt;
+    FuriHalBleProfileBase* ble_hid_profile;
     bool is_advertising;
     bool is_connected;
-#if TOTP_TARGET_FIRMWARE == TOTP_FIRMWARE_XTREME_UL
-    char previous_bt_name[TOTP_BT_WORKER_BT_ADV_NAME_MAX_LEN];
-    uint8_t previous_bt_mac[TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN];
-#endif
     AutomationKeyboardLayout keyboard_layout;
     uint16_t initial_delay;
 };
@@ -42,25 +34,15 @@ static inline bool totp_type_code_worker_stop_requested() {
     return furi_thread_flags_get() & TotpBtTypeCodeWorkerEventStop;
 }
 
-#if TOTP_TARGET_FIRMWARE == TOTP_FIRMWARE_XTREME_UL
-static void totp_type_code_worker_bt_set_app_mac(uint8_t* mac) {
-    uint8_t max_i;
-    size_t uid_size = furi_hal_version_uid_size();
-    if(uid_size < TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN) {
-        max_i = uid_size;
-    } else {
-        max_i = TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN;
-    }
-
-    const uint8_t* uid = (const uint8_t*)UID64_BASE; //-V566
-    memcpy(mac, uid, max_i);
-    for(uint8_t i = max_i; i < TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN; i++) {
-        mac[i] = 0;
-    }
+static bool hid_key_press(uint16_t button, void* context) {
+    FuriHalBleProfileBase* profile = context;
+    return ble_profile_hid_kb_press(profile, button);
+}
 
-    mac[0] = 0b10;
+static bool hid_key_release(uint16_t button, void* context) {
+    FuriHalBleProfileBase* profile = context;
+    return ble_profile_hid_kb_release(profile, button);
 }
-#endif
 
 static void totp_type_code_worker_type_code(TotpBtTypeCodeWorkerContext* context) {
     uint8_t i = 0;
@@ -72,13 +54,14 @@ static void totp_type_code_worker_type_code(TotpBtTypeCodeWorkerContext* context
     if(context->is_connected &&
        furi_mutex_acquire(context->code_buffer_sync, 500) == FuriStatusOk) {
         totp_type_code_worker_execute_automation(
-            &furi_hal_bt_hid_kb_press,
-            &furi_hal_bt_hid_kb_release,
+            &hid_key_press,
+            &hid_key_release,
             context->code_buffer,
             context->code_buffer_size,
             context->flags,
             context->keyboard_layout,
-            context->initial_delay);
+            context->initial_delay,
+            context->ble_hid_profile);
         furi_mutex_release(context->code_buffer_sync);
     }
 }
@@ -158,7 +141,7 @@ void totp_bt_type_code_worker_notify(
     furi_thread_flags_set(furi_thread_get_id(context->thread), event);
 }
 
-TotpBtTypeCodeWorkerContext* totp_bt_type_code_worker_init() {
+TotpBtTypeCodeWorkerContext* totp_bt_type_code_worker_init(uint16_t mac_xor) {
     TotpBtTypeCodeWorkerContext* context = malloc(sizeof(TotpBtTypeCodeWorkerContext));
     furi_check(context != NULL);
 
@@ -166,37 +149,15 @@ TotpBtTypeCodeWorkerContext* totp_bt_type_code_worker_init() {
     context->is_advertising = false;
     context->is_connected = false;
     bt_disconnect(context->bt);
-    furi_hal_bt_reinit();
     furi_delay_ms(200);
     bt_keys_storage_set_storage_path(context->bt, HID_BT_KEYS_STORAGE_PATH);
 
-#if TOTP_TARGET_FIRMWARE == TOTP_FIRMWARE_XTREME_UL
-    memcpy(
-        &context->previous_bt_name[0],
-        furi_hal_bt_get_profile_adv_name(FuriHalBtProfileHidKeyboard),
-        TOTP_BT_WORKER_BT_ADV_NAME_MAX_LEN);
-    memcpy(
-        &context->previous_bt_mac[0],
-        furi_hal_bt_get_profile_mac_addr(FuriHalBtProfileHidKeyboard),
-        TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN);
-    char new_name[TOTP_BT_WORKER_BT_ADV_NAME_MAX_LEN];
-    snprintf(new_name, sizeof(new_name), "%s TOTP Auth", furi_hal_version_get_name_ptr());
-    uint8_t new_bt_mac[TOTP_BT_WORKER_BT_MAC_ADDRESS_LEN];
-    totp_type_code_worker_bt_set_app_mac(new_bt_mac);
-    furi_hal_bt_set_profile_adv_name(FuriHalBtProfileHidKeyboard, new_name);
-    furi_hal_bt_set_profile_mac_addr(FuriHalBtProfileHidKeyboard, new_bt_mac);
-#endif
-
-    if(!bt_set_profile(context->bt, BtProfileHidKeyboard)) {
-        FURI_LOG_E(LOGGING_TAG, "Failed to switch BT to keyboard HID profile");
-    }
+    BleProfileHidParams ble_params = {.device_name_prefix = "TOTP", .mac_xor = mac_xor};
+    context->ble_hid_profile = bt_profile_start(context->bt, ble_profile_hid, &ble_params);
+    furi_check(context->ble_hid_profile);
 
     furi_hal_bt_start_advertising();
 
-#if TOTP_TARGET_FIRMWARE == TOTP_FIRMWARE_XTREME_UL
-    bt_enable_peer_key_update(context->bt);
-#endif
-
     context->is_advertising = true;
     bt_set_status_changed_callback(context->bt, connection_status_changed_callback, context);
 
@@ -219,15 +180,10 @@ void totp_bt_type_code_worker_free(TotpBtTypeCodeWorkerContext* context) {
     bt_disconnect(context->bt);
     furi_delay_ms(200);
     bt_keys_storage_set_default_path(context->bt);
-
-#if TOTP_TARGET_FIRMWARE == TOTP_FIRMWARE_XTREME_UL
-    furi_hal_bt_set_profile_adv_name(FuriHalBtProfileHidKeyboard, context->previous_bt_name);
-    furi_hal_bt_set_profile_mac_addr(FuriHalBtProfileHidKeyboard, context->previous_bt_mac);
-#endif
-
-    if(!bt_set_profile(context->bt, BtProfileSerial)) {
-        FURI_LOG_E(LOGGING_TAG, "Failed to switch BT to Serial profile");
+    if(!bt_profile_restore_default(context->bt)) {
+        FURI_LOG_E(LOGGING_TAG, "Failed to restore to default BT profile");
     }
+
     furi_record_close(RECORD_BT);
     context->bt = NULL;
 

+ 2 - 1
totp/workers/bt_type_code/bt_type_code.h

@@ -36,9 +36,10 @@ enum TotpBtTypeCodeWorkerEvents {
 
 /**
  * @brief Initializes bluetooth token input automation worker
+ * @param mac_xor value to be used to XOR BT MAC address to make it unique
  * @return worker context
  */
-TotpBtTypeCodeWorkerContext* totp_bt_type_code_worker_init();
+TotpBtTypeCodeWorkerContext* totp_bt_type_code_worker_init(uint16_t mac_xor);
 
 /**
  * @brief Disposes bluetooth token input automation worker and releases all the allocated resources

+ 11 - 7
totp/workers/type_code_common.c

@@ -25,10 +25,11 @@ static void totp_type_code_worker_press_key(
     uint16_t key,
     TOTP_AUTOMATION_KEY_HANDLER key_press_fn,
     TOTP_AUTOMATION_KEY_HANDLER key_release_fn,
-    TokenAutomationFeature features) {
-    (*key_press_fn)(key);
+    TokenAutomationFeature features,
+    void* context) {
+    (*key_press_fn)(key, context);
     furi_delay_ms(get_keypress_delay(features));
-    (*key_release_fn)(key);
+    (*key_release_fn)(key, context);
 }
 
 void totp_type_code_worker_execute_automation(
@@ -38,7 +39,8 @@ void totp_type_code_worker_execute_automation(
     uint8_t code_buffer_size,
     TokenAutomationFeature features,
     AutomationKeyboardLayout keyboard_layout,
-    uint16_t initial_delay) {
+    uint16_t initial_delay,
+    void* context) {
     uint16_t keyboard_layout_dict[TOTP_KB_LAYOUT_DATA_LENGTH];
     if(!totp_kb_layout_provider_get_layout_data(keyboard_layout, &keyboard_layout_dict[0])) {
         return;
@@ -59,7 +61,8 @@ void totp_type_code_worker_execute_automation(
         if(char_index >= TOTP_KB_LAYOUT_DATA_LENGTH) break;
 
         uint16_t hid_kb_key = keyboard_layout_dict[char_index];
-        totp_type_code_worker_press_key(hid_kb_key, key_press_fn, key_release_fn, features);
+        totp_type_code_worker_press_key(
+            hid_kb_key, key_press_fn, key_release_fn, features, context);
         furi_delay_ms(keystroke_delay);
         i++;
     }
@@ -67,11 +70,12 @@ void totp_type_code_worker_execute_automation(
     if(features & TokenAutomationFeatureEnterAtTheEnd) {
         furi_delay_ms(keystroke_delay);
         totp_type_code_worker_press_key(
-            HID_KEYBOARD_RETURN, key_press_fn, key_release_fn, features);
+            HID_KEYBOARD_RETURN, key_press_fn, key_release_fn, features, context);
     }
 
     if(features & TokenAutomationFeatureTabAtTheEnd) {
         furi_delay_ms(keystroke_delay);
-        totp_type_code_worker_press_key(HID_KEYBOARD_TAB, key_press_fn, key_release_fn, features);
+        totp_type_code_worker_press_key(
+            HID_KEYBOARD_TAB, key_press_fn, key_release_fn, features, context);
     }
 }

+ 4 - 2
totp/workers/type_code_common.h

@@ -3,7 +3,7 @@
 #include "../types/token_info.h"
 #include "../types/automation_kb_layout.h"
 
-typedef bool (*TOTP_AUTOMATION_KEY_HANDLER)(uint16_t key);
+typedef bool (*TOTP_AUTOMATION_KEY_HANDLER)(uint16_t key, void* context);
 
 /**
  * @brief Executes token input automation using given key press\release handlers
@@ -14,6 +14,7 @@ typedef bool (*TOTP_AUTOMATION_KEY_HANDLER)(uint16_t key);
  * @param features automation features
  * @param keyboard_layout keyboard layout to be used
  * @param initial_delay initial delay before starting automation
+ * @param context context to be passed to key press\release handlers
  */
 void totp_type_code_worker_execute_automation(
     TOTP_AUTOMATION_KEY_HANDLER key_press_fn,
@@ -22,4 +23,5 @@ void totp_type_code_worker_execute_automation(
     uint8_t code_buffer_size,
     TokenAutomationFeature features,
     AutomationKeyboardLayout keyboard_layout,
-    uint16_t initial_delay);
+    uint16_t initial_delay,
+    void* context);

+ 14 - 3
totp/workers/usb_type_code/usb_type_code.c

@@ -30,6 +30,16 @@ static inline bool totp_type_code_worker_stop_requested() {
     return furi_thread_flags_get() & TotpUsbTypeCodeWorkerEventStop;
 }
 
+static bool hid_key_press(uint16_t button, void* context) {
+    UNUSED(context);
+    return furi_hal_hid_kb_press(button);
+}
+
+static bool hid_key_release(uint16_t button, void* context) {
+    UNUSED(context);
+    return furi_hal_hid_kb_release(button);
+}
+
 static void totp_type_code_worker_type_code(TotpUsbTypeCodeWorkerContext* context) {
     context->usb_mode_prev = furi_hal_usb_get_config();
     furi_hal_usb_unlock();
@@ -43,13 +53,14 @@ static void totp_type_code_worker_type_code(TotpUsbTypeCodeWorkerContext* contex
     if(furi_hal_hid_is_connected() &&
        furi_mutex_acquire(context->code_buffer_sync, 500) == FuriStatusOk) {
         totp_type_code_worker_execute_automation(
-            &furi_hal_hid_kb_press,
-            &furi_hal_hid_kb_release,
+            &hid_key_press,
+            &hid_key_release,
             context->code_buffer,
             context->code_buffer_size,
             context->flags,
             context->keyboard_layout,
-            context->initial_delay);
+            context->initial_delay,
+            NULL);
         furi_mutex_release(context->code_buffer_sync);
 
         furi_delay_ms(100);