totp_scene_generate_token.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. #include "totp_scene_generate_token.h"
  2. #include <gui/gui.h>
  3. #include <notification/notification.h>
  4. #include <notification/notification_messages.h>
  5. #include <totp_icons.h>
  6. #include <roll_value.h>
  7. #include "../../../services/fonts/font_provider.h"
  8. #include "../../canvas_extensions.h"
  9. #include "../../../types/token_info.h"
  10. #include "../../../types/common.h"
  11. #include "../../constants.h"
  12. #include "../../../services/config/config.h"
  13. #include "../../scene_director.h"
  14. #include "../../../config/app/config.h"
  15. #include "../../../workers/generate_totp_code/generate_totp_code.h"
  16. #include "../../../workers/usb_type_code/usb_type_code.h"
  17. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  18. #include "../../../workers/bt_type_code/bt_type_code.h"
  19. #endif
  20. #define PROGRESS_BAR_MARGIN (3)
  21. #define PROGRESS_BAR_HEIGHT (4)
  22. typedef struct {
  23. uint8_t progress_bar_x;
  24. uint8_t progress_bar_width;
  25. uint8_t code_total_length;
  26. uint8_t code_offset_x;
  27. uint8_t code_offset_y;
  28. } UiPrecalculatedDimensions;
  29. typedef struct {
  30. char last_code[TokenDigitsCountMax + 1];
  31. TotpUsbTypeCodeWorkerContext* usb_type_code_worker_context;
  32. NotificationMessage const* notification_sequence_new_token[8];
  33. NotificationMessage const* notification_sequence_automation[11];
  34. FuriMutex* last_code_update_sync;
  35. TotpGenerateCodeWorkerContext* generate_code_worker_context;
  36. UiPrecalculatedDimensions ui_precalculated_dimensions;
  37. FontInfo* active_font;
  38. NotificationApp* notification_app;
  39. } SceneState;
  40. static const NotificationSequence*
  41. get_notification_sequence_new_token(const PluginState* plugin_state, SceneState* scene_state) {
  42. if(scene_state->notification_sequence_new_token[0] == NULL) {
  43. NotificationMessage const** sequence = &scene_state->notification_sequence_new_token[0];
  44. *(sequence++) = &message_display_backlight_on;
  45. *(sequence++) = &message_green_255;
  46. if(plugin_state->notification_method & NotificationMethodVibro) {
  47. *(sequence++) = &message_vibro_on;
  48. }
  49. if(plugin_state->notification_method & NotificationMethodSound) {
  50. *(sequence++) = &message_note_c5;
  51. }
  52. *(sequence++) = &message_delay_50;
  53. if(plugin_state->notification_method & NotificationMethodVibro) {
  54. *(sequence++) = &message_vibro_off;
  55. }
  56. if(plugin_state->notification_method & NotificationMethodSound) {
  57. *(sequence++) = &message_sound_off;
  58. }
  59. *(sequence++) = NULL;
  60. }
  61. return (NotificationSequence*)scene_state->notification_sequence_new_token;
  62. }
  63. static const NotificationSequence*
  64. get_notification_sequence_automation(const PluginState* plugin_state, SceneState* scene_state) {
  65. if(scene_state->notification_sequence_automation[0] == NULL) {
  66. NotificationMessage const** sequence = &scene_state->notification_sequence_automation[0];
  67. *(sequence++) = &message_blue_255;
  68. if(plugin_state->notification_method & NotificationMethodVibro) {
  69. *(sequence++) = &message_vibro_on;
  70. }
  71. if(plugin_state->notification_method & NotificationMethodSound) {
  72. *(sequence++) = &message_note_d5; //-V525
  73. *(sequence++) = &message_delay_50;
  74. *(sequence++) = &message_note_e4;
  75. *(sequence++) = &message_delay_50;
  76. *(sequence++) = &message_note_f3;
  77. }
  78. *(sequence++) = &message_delay_50;
  79. if(plugin_state->notification_method & NotificationMethodVibro) {
  80. *(sequence++) = &message_vibro_off;
  81. }
  82. if(plugin_state->notification_method & NotificationMethodSound) {
  83. *(sequence++) = &message_sound_off;
  84. }
  85. *(sequence++) = NULL;
  86. }
  87. return (NotificationSequence*)scene_state->notification_sequence_automation;
  88. }
  89. static void update_totp_params(PluginState* const plugin_state, size_t token_index) {
  90. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  91. TokenInfoIteratorContext* iterator_context =
  92. totp_config_get_token_iterator_context(plugin_state);
  93. if(totp_token_info_iterator_go_to(iterator_context, token_index)) {
  94. totp_generate_code_worker_notify(
  95. scene_state->generate_code_worker_context, TotpGenerateCodeWorkerEventForceUpdate);
  96. }
  97. }
  98. static void draw_totp_code(Canvas* const canvas, const PluginState* const plugin_state) {
  99. const SceneState* scene_state = plugin_state->current_scene_state;
  100. const TokenInfoIteratorContext* iterator_context =
  101. totp_config_get_token_iterator_context(plugin_state);
  102. uint8_t code_length = totp_token_info_iterator_get_current_token(iterator_context)->digits;
  103. canvas_draw_str_ex(
  104. canvas,
  105. scene_state->ui_precalculated_dimensions.code_offset_x,
  106. scene_state->ui_precalculated_dimensions.code_offset_y,
  107. scene_state->last_code,
  108. code_length,
  109. scene_state->active_font);
  110. }
  111. static void on_new_token_code_generated(bool time_left, void* context) {
  112. PluginState* const plugin_state = context;
  113. const TokenInfoIteratorContext* iterator_context =
  114. totp_config_get_token_iterator_context(plugin_state);
  115. if(totp_token_info_iterator_get_total_count(iterator_context) == 0) {
  116. return;
  117. }
  118. SceneState* scene_state = plugin_state->current_scene_state;
  119. const TokenInfo* current_token = totp_token_info_iterator_get_current_token(iterator_context);
  120. uint8_t char_width = scene_state->active_font->char_info[0].width;
  121. scene_state->ui_precalculated_dimensions.code_total_length =
  122. current_token->digits * (char_width + scene_state->active_font->space_width);
  123. scene_state->ui_precalculated_dimensions.code_offset_x =
  124. (SCREEN_WIDTH - scene_state->ui_precalculated_dimensions.code_total_length) >> 1;
  125. scene_state->ui_precalculated_dimensions.code_offset_y =
  126. SCREEN_HEIGHT_CENTER - (scene_state->active_font->height >> 1);
  127. if(time_left) {
  128. notification_message(
  129. scene_state->notification_app,
  130. get_notification_sequence_new_token(plugin_state, scene_state));
  131. }
  132. totp_scene_director_force_redraw(plugin_state);
  133. }
  134. static void on_code_lifetime_updated_generated(float code_lifetime_percent, void* context) {
  135. PluginState* const plugin_state = context;
  136. SceneState* scene_state = plugin_state->current_scene_state;
  137. scene_state->ui_precalculated_dimensions.progress_bar_width =
  138. (uint8_t)((float)(SCREEN_WIDTH - (PROGRESS_BAR_MARGIN << 1)) * code_lifetime_percent);
  139. scene_state->ui_precalculated_dimensions.progress_bar_x =
  140. ((SCREEN_WIDTH - (PROGRESS_BAR_MARGIN << 1) -
  141. scene_state->ui_precalculated_dimensions.progress_bar_width) >>
  142. 1) +
  143. PROGRESS_BAR_MARGIN;
  144. totp_scene_director_force_redraw(plugin_state);
  145. }
  146. void totp_scene_generate_token_activate(PluginState* plugin_state) {
  147. SceneState* scene_state = malloc(sizeof(SceneState));
  148. furi_check(scene_state != NULL);
  149. plugin_state->current_scene_state = scene_state;
  150. FURI_LOG_D(LOGGING_TAG, "Timezone set to: %f", (double)plugin_state->timezone_offset);
  151. scene_state->last_code_update_sync = furi_mutex_alloc(FuriMutexTypeNormal);
  152. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  153. scene_state->usb_type_code_worker_context = totp_usb_type_code_worker_start(
  154. scene_state->last_code,
  155. TokenDigitsCountMax + 1,
  156. scene_state->last_code_update_sync,
  157. plugin_state->automation_kb_layout,
  158. plugin_state->automation_initial_delay);
  159. }
  160. scene_state->active_font = totp_font_info_alloc();
  161. if(!totp_font_provider_get_font(plugin_state->active_font_index, scene_state->active_font)) {
  162. totp_font_provider_get_font(0, scene_state->active_font);
  163. }
  164. scene_state->notification_app = furi_record_open(RECORD_NOTIFICATION);
  165. scene_state->notification_sequence_automation[0] = NULL;
  166. scene_state->notification_sequence_new_token[0] = NULL;
  167. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  168. if(plugin_state->automation_method & AutomationMethodBadBt) {
  169. if(plugin_state->bt_type_code_worker_context == NULL) {
  170. plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init(
  171. *((uint16_t*)plugin_state->crypto_settings.crypto_verify_data),
  172. plugin_state->bt_type_code_worker_profile_index);
  173. }
  174. totp_bt_type_code_worker_start(
  175. plugin_state->bt_type_code_worker_context,
  176. scene_state->last_code,
  177. TokenDigitsCountMax + 1,
  178. scene_state->last_code_update_sync,
  179. plugin_state->automation_kb_layout,
  180. plugin_state->automation_initial_delay);
  181. }
  182. #endif
  183. const TokenInfoIteratorContext* iterator_context =
  184. totp_config_get_token_iterator_context(plugin_state);
  185. scene_state->generate_code_worker_context = totp_generate_code_worker_start(
  186. scene_state->last_code,
  187. totp_token_info_iterator_get_current_token(iterator_context),
  188. scene_state->last_code_update_sync,
  189. plugin_state->timezone_offset,
  190. &plugin_state->crypto_settings);
  191. totp_generate_code_worker_set_code_generated_handler(
  192. scene_state->generate_code_worker_context, &on_new_token_code_generated, plugin_state);
  193. totp_generate_code_worker_set_lifetime_changed_handler(
  194. scene_state->generate_code_worker_context,
  195. &on_code_lifetime_updated_generated,
  196. plugin_state);
  197. update_totp_params(
  198. plugin_state, totp_token_info_iterator_get_current_token_index(iterator_context));
  199. }
  200. void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_state) {
  201. const TokenInfoIteratorContext* iterator_context =
  202. totp_config_get_token_iterator_context(plugin_state);
  203. if(totp_token_info_iterator_get_total_count(iterator_context) == 0) {
  204. canvas_draw_str_aligned(
  205. canvas,
  206. SCREEN_WIDTH_CENTER,
  207. SCREEN_HEIGHT_CENTER - 10,
  208. AlignCenter,
  209. AlignCenter,
  210. "Token list is empty");
  211. canvas_draw_str_aligned(
  212. canvas,
  213. SCREEN_WIDTH_CENTER,
  214. SCREEN_HEIGHT_CENTER + 10,
  215. AlignCenter,
  216. AlignCenter,
  217. "Press OK button to open menu");
  218. return;
  219. }
  220. const SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  221. canvas_set_font(canvas, FontPrimary);
  222. const TokenInfo* token_info = totp_token_info_iterator_get_current_token(iterator_context);
  223. const char* token_name_cstr = furi_string_get_cstr(token_info->name);
  224. uint16_t token_name_width = canvas_string_width(canvas, token_name_cstr);
  225. if(SCREEN_WIDTH - token_name_width > 18) {
  226. canvas_draw_str_aligned(
  227. canvas,
  228. SCREEN_WIDTH_CENTER,
  229. SCREEN_HEIGHT_CENTER - 20,
  230. AlignCenter,
  231. AlignCenter,
  232. token_name_cstr);
  233. } else {
  234. canvas_draw_str_aligned(
  235. canvas, 9, SCREEN_HEIGHT_CENTER - 20, AlignLeft, AlignCenter, token_name_cstr);
  236. canvas_set_color(canvas, ColorWhite);
  237. canvas_draw_box(canvas, 0, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  238. canvas_draw_box(canvas, SCREEN_WIDTH - 10, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  239. canvas_set_color(canvas, ColorBlack);
  240. }
  241. draw_totp_code(canvas, plugin_state);
  242. if(token_info->type == TokenTypeTOTP) {
  243. canvas_draw_box(
  244. canvas,
  245. scene_state->ui_precalculated_dimensions.progress_bar_x,
  246. SCREEN_HEIGHT - PROGRESS_BAR_MARGIN - PROGRESS_BAR_HEIGHT,
  247. scene_state->ui_precalculated_dimensions.progress_bar_width,
  248. PROGRESS_BAR_HEIGHT);
  249. } else {
  250. char buffer[21];
  251. snprintf(&buffer[0], sizeof(buffer), "%" PRIu64, token_info->counter);
  252. canvas_set_font(canvas, FontSecondary);
  253. canvas_draw_str_aligned(
  254. canvas, SCREEN_WIDTH_CENTER, SCREEN_HEIGHT - 5, AlignCenter, AlignCenter, buffer);
  255. }
  256. if(totp_token_info_iterator_get_total_count(iterator_context) > 1) {
  257. canvas_draw_icon(canvas, 0, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_left_8x9);
  258. canvas_draw_icon(
  259. canvas, SCREEN_WIDTH - 8, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_right_8x9);
  260. }
  261. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  262. canvas_draw_icon(
  263. canvas,
  264. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  265. SCREEN_WIDTH_CENTER -
  266. (plugin_state->automation_method & AutomationMethodBadBt ? 33 : 15),
  267. #else
  268. SCREEN_WIDTH_CENTER - 15,
  269. #endif
  270. SCREEN_HEIGHT_CENTER + 12,
  271. &I_hid_usb_31x9);
  272. }
  273. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  274. if(plugin_state->automation_method & AutomationMethodBadBt &&
  275. plugin_state->bt_type_code_worker_context != NULL &&
  276. totp_bt_type_code_worker_is_advertising(plugin_state->bt_type_code_worker_context)) {
  277. canvas_draw_icon(
  278. canvas,
  279. SCREEN_WIDTH_CENTER +
  280. (plugin_state->automation_method & AutomationMethodBadUsb ? 2 : -15),
  281. SCREEN_HEIGHT_CENTER + 12,
  282. &I_hid_ble_31x9);
  283. }
  284. #endif
  285. }
  286. bool totp_scene_generate_token_handle_event(
  287. const PluginEvent* const event,
  288. PluginState* plugin_state) {
  289. if(event->type != EventTypeKey) {
  290. return true;
  291. }
  292. if(event->input.type == InputTypePress && event->input.key == InputKeyBack) {
  293. return false;
  294. }
  295. SceneState* scene_state;
  296. if(event->input.type == InputTypeLong) {
  297. if(event->input.key == InputKeyDown &&
  298. plugin_state->automation_method & AutomationMethodBadUsb) {
  299. scene_state = (SceneState*)plugin_state->current_scene_state;
  300. const TokenInfoIteratorContext* iterator_context =
  301. totp_config_get_token_iterator_context(plugin_state);
  302. totp_usb_type_code_worker_notify(
  303. scene_state->usb_type_code_worker_context,
  304. TotpUsbTypeCodeWorkerEventType,
  305. totp_token_info_iterator_get_current_token(iterator_context)->automation_features);
  306. notification_message(
  307. scene_state->notification_app,
  308. get_notification_sequence_automation(plugin_state, scene_state));
  309. return true;
  310. }
  311. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  312. else if(
  313. event->input.key == InputKeyUp &&
  314. plugin_state->automation_method & AutomationMethodBadBt) {
  315. scene_state = (SceneState*)plugin_state->current_scene_state;
  316. const TokenInfoIteratorContext* iterator_context =
  317. totp_config_get_token_iterator_context(plugin_state);
  318. totp_bt_type_code_worker_notify(
  319. plugin_state->bt_type_code_worker_context,
  320. TotpBtTypeCodeWorkerEventType,
  321. totp_token_info_iterator_get_current_token(iterator_context)->automation_features);
  322. notification_message(
  323. scene_state->notification_app,
  324. get_notification_sequence_automation(plugin_state, scene_state));
  325. return true;
  326. }
  327. #endif
  328. else if(event->input.key == InputKeyOk) {
  329. TokenInfoIteratorContext* iterator_context =
  330. totp_config_get_token_iterator_context(plugin_state);
  331. const TokenInfo* token_info =
  332. totp_token_info_iterator_get_current_token(iterator_context);
  333. if(token_info->type == TokenTypeHOTP) {
  334. scene_state = (SceneState*)plugin_state->current_scene_state;
  335. totp_token_info_iterator_current_token_inc_counter(iterator_context);
  336. totp_generate_code_worker_notify(
  337. scene_state->generate_code_worker_context,
  338. TotpGenerateCodeWorkerEventForceUpdate);
  339. notification_message(
  340. scene_state->notification_app,
  341. get_notification_sequence_new_token(plugin_state, scene_state));
  342. }
  343. }
  344. } else if(event->input.type == InputTypePress || event->input.type == InputTypeRepeat) {
  345. switch(event->input.key) {
  346. case InputKeyUp:
  347. break;
  348. case InputKeyDown:
  349. break;
  350. case InputKeyRight: {
  351. const TokenInfoIteratorContext* iterator_context =
  352. totp_config_get_token_iterator_context(plugin_state);
  353. size_t current_token_index =
  354. totp_token_info_iterator_get_current_token_index(iterator_context);
  355. totp_roll_value_size_t(
  356. &current_token_index,
  357. 1,
  358. 0,
  359. totp_token_info_iterator_get_total_count(iterator_context) - 1,
  360. RollOverflowBehaviorRoll);
  361. update_totp_params(plugin_state, current_token_index);
  362. break;
  363. }
  364. case InputKeyLeft: {
  365. const TokenInfoIteratorContext* iterator_context =
  366. totp_config_get_token_iterator_context(plugin_state);
  367. size_t current_token_index =
  368. totp_token_info_iterator_get_current_token_index(iterator_context);
  369. totp_roll_value_size_t(
  370. &current_token_index,
  371. -1,
  372. 0,
  373. totp_token_info_iterator_get_total_count(iterator_context) - 1,
  374. RollOverflowBehaviorRoll);
  375. update_totp_params(plugin_state, current_token_index);
  376. break;
  377. }
  378. case InputKeyOk:
  379. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu);
  380. break;
  381. case InputKeyBack:
  382. break;
  383. default:
  384. break;
  385. }
  386. }
  387. return true;
  388. }
  389. void totp_scene_generate_token_deactivate(PluginState* plugin_state) {
  390. if(plugin_state->current_scene_state == NULL) return;
  391. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  392. totp_generate_code_worker_stop(scene_state->generate_code_worker_context);
  393. furi_record_close(RECORD_NOTIFICATION);
  394. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  395. totp_usb_type_code_worker_stop(scene_state->usb_type_code_worker_context);
  396. }
  397. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  398. if(plugin_state->automation_method & AutomationMethodBadBt) {
  399. totp_bt_type_code_worker_stop(plugin_state->bt_type_code_worker_context);
  400. }
  401. #endif
  402. furi_mutex_free(scene_state->last_code_update_sync);
  403. totp_font_info_free(scene_state->active_font);
  404. free(scene_state);
  405. plugin_state->current_scene_state = NULL;
  406. }