Skip to content

Commit

Permalink
[FL-1958] U2F prototype (flipperdevices#879)
Browse files Browse the repository at this point in the history
* U2F implementation prototype
* U2F data encryption and store, user confirmation request
* remove debug prints
* fix notification bug in chrome
* split u2f_alloc into u2f_init and u2f_alloc
* typo fix, furi-hal-trng -> furi-hal-random
* rand/srand redefinition
* SubGhz: a little bit of Dante.
* u2f_data naming fix

Co-authored-by: Aleksandr Kutuzov <[email protected]>
  • Loading branch information
nminaylov and skotopes authored Dec 22, 2021
1 parent 9b62b55 commit 9e62f08
Show file tree
Hide file tree
Showing 51 changed files with 10,985 additions and 123 deletions.
6 changes: 6 additions & 0 deletions applications/applications.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern int32_t desktop_srv(void* p);
extern int32_t accessor_app(void* p);
extern int32_t archive_app(void* p);
extern int32_t bad_usb_app(void* p);
extern int32_t u2f_app(void* p);
extern int32_t uart_echo_app(void* p);
extern int32_t blink_test_app(void* p);
extern int32_t bt_debug_app(void* p);
Expand Down Expand Up @@ -154,6 +155,11 @@ const FlipperApplication FLIPPER_APPS[] = {
#ifdef APP_BAD_USB
{.app = bad_usb_app, .name = "Bad USB", .stack_size = 2048, .icon = &A_BadUsb_14},
#endif

#ifdef APP_U2F
{.app = u2f_app, .name = "U2F", .stack_size = 2048, .icon = &A_U2F_14},
#endif

};

const size_t FLIPPER_APPS_COUNT = sizeof(FLIPPER_APPS) / sizeof(FlipperApplication);
Expand Down
9 changes: 8 additions & 1 deletion applications/applications.mk
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ APP_DISPLAY_TEST = 1
APP_BLE_HID = 1
APP_USB_MOUSE = 1
APP_BAD_USB = 1
APP_U2F = 1
APP_UART_ECHO = 1
endif

Expand Down Expand Up @@ -165,7 +166,13 @@ APP_BAD_USB ?= 0
ifeq ($(APP_BAD_USB), 1)
CFLAGS += -DAPP_BAD_USB
SRV_GUI = 1
endif
endif

APP_U2F ?= 0
ifeq ($(APP_U2F), 1)
CFLAGS += -DAPP_U2F
SRV_GUI = 1
endif

APP_BLE_HID ?=0
ifeq ($(APP_BLE_HID), 1)
Expand Down
4 changes: 2 additions & 2 deletions applications/debug_tools/usb_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void usb_test_submenu_callback(void* context, uint32_t index) {
} else if(index == UsbTestSubmenuIndexHid) {
furi_hal_usb_set_config(&usb_hid);
} else if(index == UsbTestSubmenuIndexHidU2F) {
//furi_hal_usb_set_config(UsbModeU2F);
furi_hal_usb_set_config(&usb_hid_u2f);
}
}

Expand Down Expand Up @@ -67,7 +67,7 @@ UsbTestApp* usb_test_app_alloc() {
submenu_add_item(
app->submenu, "HID KB+Mouse", UsbTestSubmenuIndexHid, usb_test_submenu_callback, app);
submenu_add_item(
app->submenu, "TODO: HID U2F", UsbTestSubmenuIndexHidU2F, usb_test_submenu_callback, app);
app->submenu, "HID U2F", UsbTestSubmenuIndexHidU2F, usb_test_submenu_callback, app);
view_set_previous_callback(submenu_get_view(app->submenu), usb_test_exit);
view_dispatcher_add_view(app->view_dispatcher, 0, submenu_get_view(app->submenu));

Expand Down
7 changes: 2 additions & 5 deletions applications/subghz/subghz.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/*
* Give up hope, everyone who enters here!!!
* Оставь надежду, всяк сюда входящий!!!
*/
/* Abandon hope, all ye who enter here. */

#include "subghz_i.h"
#include <lib/toolbox/path.h>
Expand Down Expand Up @@ -329,4 +326,4 @@ int32_t subghz_app(void* p) {
subghz_free(subghz);

return 0;
}
}
30 changes: 30 additions & 0 deletions applications/u2f/scenes/u2f_scene.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "u2f_scene.h"

// Generate scene on_enter handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
void (*const u2f_scene_on_enter_handlers[])(void*) = {
#include "u2f_scene_config.h"
};
#undef ADD_SCENE

// Generate scene on_event handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event,
bool (*const u2f_scene_on_event_handlers[])(void* context, SceneManagerEvent event) = {
#include "u2f_scene_config.h"
};
#undef ADD_SCENE

// Generate scene on_exit handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit,
void (*const u2f_scene_on_exit_handlers[])(void* context) = {
#include "u2f_scene_config.h"
};
#undef ADD_SCENE

// Initialize scene handlers configuration structure
const SceneManagerHandlers u2f_scene_handlers = {
.on_enter_handlers = u2f_scene_on_enter_handlers,
.on_event_handlers = u2f_scene_on_event_handlers,
.on_exit_handlers = u2f_scene_on_exit_handlers,
.scene_num = U2fSceneNum,
};
29 changes: 29 additions & 0 deletions applications/u2f/scenes/u2f_scene.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <gui/scene_manager.h>

// Generate scene id and total number
#define ADD_SCENE(prefix, name, id) U2fScene##id,
typedef enum {
#include "u2f_scene_config.h"
U2fSceneNum,
} U2fScene;
#undef ADD_SCENE

extern const SceneManagerHandlers u2f_scene_handlers;

// Generate scene on_enter handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
#include "u2f_scene_config.h"
#undef ADD_SCENE

// Generate scene on_event handlers declaration
#define ADD_SCENE(prefix, name, id) \
bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event);
#include "u2f_scene_config.h"
#undef ADD_SCENE

// Generate scene on_exit handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context);
#include "u2f_scene_config.h"
#undef ADD_SCENE
1 change: 1 addition & 0 deletions applications/u2f/scenes/u2f_scene_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ADD_SCENE(u2f, main, Main)
92 changes: 92 additions & 0 deletions applications/u2f/scenes/u2f_scene_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "../u2f_app_i.h"
#include "../views/u2f_view.h"
#include "furi-hal.h"
#include "../u2f.h"

#define U2F_EVENT_TIMEOUT 500

static void u2f_scene_main_ok_callback(InputType type, void* context) {
furi_assert(context);
U2fApp* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, U2fCustomEventConfirm);
}

static void u2f_scene_main_event_callback(U2fNotifyEvent evt, void* context) {
furi_assert(context);
U2fApp* app = context;
if(evt == U2fNotifyRegister)
view_dispatcher_send_custom_event(app->view_dispatcher, U2fCustomEventRegister);
else if(evt == U2fNotifyAuth)
view_dispatcher_send_custom_event(app->view_dispatcher, U2fCustomEventAuth);
else if(evt == U2fNotifyWink)
view_dispatcher_send_custom_event(app->view_dispatcher, U2fCustomEventWink);
}

static void u2f_scene_main_timer_callback(void* context) {
furi_assert(context);
U2fApp* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, U2fCustomEventTimeout);
}

bool u2f_scene_main_on_event(void* context, SceneManagerEvent event) {
furi_assert(context);
U2fApp* app = context;
bool consumed = false;

if(event.type == SceneManagerEventTypeCustom) {
if((event.event == U2fCustomEventRegister) || (event.event == U2fCustomEventAuth)) {
osTimerStart(app->timer, U2F_EVENT_TIMEOUT);
if(app->event_cur == U2fCustomEventNone) {
app->event_cur = event.event;
if(event.event == U2fCustomEventRegister)
u2f_view_set_state(app->u2f_view, U2fMsgRegister);
else if(event.event == U2fCustomEventAuth)
u2f_view_set_state(app->u2f_view, U2fMsgAuth);
notification_message(app->notifications, &sequence_success);
}
notification_message(app->notifications, &sequence_blink_blue_10);
} else if(event.event == U2fCustomEventWink) {
notification_message(app->notifications, &sequence_blink_green_10);
} else if(event.event == U2fCustomEventTimeout) {
app->event_cur = U2fCustomEventNone;
u2f_view_set_state(app->u2f_view, U2fMsgNone);
} else if(event.event == U2fCustomEventConfirm) {
if(app->event_cur != U2fCustomEventNone) {
u2f_confirm_user_present(app->u2f_instance);
}
}

consumed = true;
} else if(event.type == SceneManagerEventTypeTick) {
}
return consumed;
}

void u2f_scene_main_on_enter(void* context) {
U2fApp* app = context;

app->timer = osTimerNew(u2f_scene_main_timer_callback, osTimerOnce, app, NULL);

app->u2f_instance = u2f_alloc();
app->u2f_ready = u2f_init(app->u2f_instance);
if(app->u2f_ready == true) {
u2f_set_event_callback(app->u2f_instance, u2f_scene_main_event_callback, app);
app->u2f_hid = u2f_hid_start(app->u2f_instance);
u2f_view_set_ok_callback(app->u2f_view, u2f_scene_main_ok_callback, app);
} else {
u2f_free(app->u2f_instance);
u2f_view_set_state(app->u2f_view, U2fMsgError);
}

view_dispatcher_switch_to_view(app->view_dispatcher, U2fAppViewMain);
}

void u2f_scene_main_on_exit(void* context) {
U2fApp* app = context;
osTimerStop(app->timer);
osTimerDelete(app->timer);
if(app->u2f_ready == true) {
u2f_hid_stop(app->u2f_hid);
u2f_free(app->u2f_instance);
}
}
Loading

0 comments on commit 9e62f08

Please sign in to comment.