totp_scene_generate_token.c 18 KB

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