totp_scene_generate_token.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #include <gui/gui.h>
  2. #include <notification/notification.h>
  3. #include <notification/notification_messages.h>
  4. #include <totp_icons.h>
  5. #include "totp_scene_generate_token.h"
  6. #include "../../../types/token_info.h"
  7. #include "../../../types/common.h"
  8. #include "../../constants.h"
  9. #include "../../../services/totp/totp.h"
  10. #include "../../../services/config/config.h"
  11. #include "../../../services/crypto/crypto.h"
  12. #include "../../../services/convert/convert.h"
  13. #include "../../../lib/polyfills/memset_s.h"
  14. #include "../../../lib/roll_value/roll_value.h"
  15. #include "../../scene_director.h"
  16. #include "../token_menu/totp_scene_token_menu.h"
  17. #include "../../../features_config.h"
  18. #include "../../../workers/usb_type_code/usb_type_code.h"
  19. #ifdef TOTP_BADBT_TYPE_ENABLED
  20. #include "../../../workers/bt_type_code/bt_type_code.h"
  21. #endif
  22. static const uint8_t PROGRESS_BAR_MARGIN = 3;
  23. static const uint8_t PROGRESS_BAR_HEIGHT = 4;
  24. typedef struct {
  25. uint16_t current_token_index;
  26. char last_code[TOTP_TOKEN_DIGITS_MAX_COUNT + 1];
  27. bool need_token_update;
  28. TokenInfo* current_token;
  29. uint32_t last_token_gen_time;
  30. TotpUsbTypeCodeWorkerContext* usb_type_code_worker_context;
  31. NotificationMessage const** notification_sequence_new_token;
  32. NotificationMessage const** notification_sequence_badusb;
  33. FuriMutex* last_code_update_sync;
  34. } SceneState;
  35. static const NotificationSequence*
  36. get_notification_sequence_new_token(const PluginState* plugin_state, SceneState* scene_state) {
  37. if(scene_state->notification_sequence_new_token == NULL) {
  38. uint8_t i = 0;
  39. uint8_t length = 4;
  40. if(plugin_state->notification_method & NotificationMethodVibro) {
  41. length += 2;
  42. }
  43. if(plugin_state->notification_method & NotificationMethodSound) {
  44. length += 2;
  45. }
  46. scene_state->notification_sequence_new_token = malloc(sizeof(void*) * length);
  47. furi_check(scene_state->notification_sequence_new_token != NULL);
  48. scene_state->notification_sequence_new_token[i++] = &message_display_backlight_on;
  49. scene_state->notification_sequence_new_token[i++] = &message_green_255;
  50. if(plugin_state->notification_method & NotificationMethodVibro) {
  51. scene_state->notification_sequence_new_token[i++] = &message_vibro_on;
  52. }
  53. if(plugin_state->notification_method & NotificationMethodSound) {
  54. scene_state->notification_sequence_new_token[i++] = &message_note_c5;
  55. }
  56. scene_state->notification_sequence_new_token[i++] = &message_delay_50;
  57. if(plugin_state->notification_method & NotificationMethodVibro) {
  58. scene_state->notification_sequence_new_token[i++] = &message_vibro_off;
  59. }
  60. if(plugin_state->notification_method & NotificationMethodSound) {
  61. scene_state->notification_sequence_new_token[i++] = &message_sound_off;
  62. }
  63. scene_state->notification_sequence_new_token[i++] = NULL;
  64. }
  65. return (NotificationSequence*)scene_state->notification_sequence_new_token;
  66. }
  67. static const NotificationSequence*
  68. get_notification_sequence_automation(const PluginState* plugin_state, SceneState* scene_state) {
  69. if(scene_state->notification_sequence_badusb == NULL) {
  70. uint8_t i = 0;
  71. uint8_t length = 3;
  72. if(plugin_state->notification_method & NotificationMethodVibro) {
  73. length += 2;
  74. }
  75. if(plugin_state->notification_method & NotificationMethodSound) {
  76. length += 6;
  77. }
  78. scene_state->notification_sequence_badusb = malloc(sizeof(void*) * length);
  79. furi_check(scene_state->notification_sequence_badusb != NULL);
  80. scene_state->notification_sequence_badusb[i++] = &message_blue_255;
  81. if(plugin_state->notification_method & NotificationMethodVibro) {
  82. scene_state->notification_sequence_badusb[i++] = &message_vibro_on;
  83. }
  84. if(plugin_state->notification_method & NotificationMethodSound) {
  85. scene_state->notification_sequence_badusb[i++] = &message_note_d5; //-V525
  86. scene_state->notification_sequence_badusb[i++] = &message_delay_50;
  87. scene_state->notification_sequence_badusb[i++] = &message_note_e4;
  88. scene_state->notification_sequence_badusb[i++] = &message_delay_50;
  89. scene_state->notification_sequence_badusb[i++] = &message_note_f3;
  90. }
  91. scene_state->notification_sequence_badusb[i++] = &message_delay_50;
  92. if(plugin_state->notification_method & NotificationMethodVibro) {
  93. scene_state->notification_sequence_badusb[i++] = &message_vibro_off;
  94. }
  95. if(plugin_state->notification_method & NotificationMethodSound) {
  96. scene_state->notification_sequence_badusb[i++] = &message_sound_off;
  97. }
  98. scene_state->notification_sequence_badusb[i++] = NULL;
  99. }
  100. return (NotificationSequence*)scene_state->notification_sequence_badusb;
  101. }
  102. static void int_token_to_str(uint32_t i_token_code, char* str, TokenDigitsCount len) {
  103. if(i_token_code == OTP_ERROR) {
  104. memset(&str[0], '-', len);
  105. } else {
  106. for(int i = len - 1; i >= 0; i--) {
  107. str[i] = CONVERT_DIGIT_TO_CHAR(i_token_code % 10);
  108. i_token_code = i_token_code / 10;
  109. }
  110. }
  111. str[len] = '\0';
  112. }
  113. static TOTP_ALGO get_totp_algo_impl(TokenHashAlgo algo) {
  114. switch(algo) {
  115. case SHA1:
  116. return TOTP_ALGO_SHA1;
  117. case SHA256:
  118. return TOTP_ALGO_SHA256;
  119. case SHA512:
  120. return TOTP_ALGO_SHA512;
  121. default:
  122. break;
  123. }
  124. return NULL;
  125. }
  126. static void update_totp_params(PluginState* const plugin_state) {
  127. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  128. if(scene_state->current_token_index < plugin_state->tokens_count) {
  129. TokenInfo* tokenInfo =
  130. list_element_at(plugin_state->tokens_list, scene_state->current_token_index)->data;
  131. scene_state->need_token_update = true;
  132. scene_state->current_token = tokenInfo;
  133. }
  134. }
  135. void totp_scene_generate_token_init(const PluginState* plugin_state) {
  136. UNUSED(plugin_state);
  137. }
  138. void totp_scene_generate_token_activate(
  139. PluginState* plugin_state,
  140. const GenerateTokenSceneContext* context) {
  141. if(!plugin_state->token_list_loaded) {
  142. TokenLoadingResult token_load_result = totp_config_file_load_tokens(plugin_state);
  143. if(token_load_result != TokenLoadingResultSuccess) {
  144. DialogMessage* message = dialog_message_alloc();
  145. dialog_message_set_buttons(message, NULL, "Okay", NULL);
  146. if(token_load_result == TokenLoadingResultWarning) {
  147. dialog_message_set_text(
  148. message,
  149. "Unable to load some tokens\nPlease review conf file",
  150. SCREEN_WIDTH_CENTER,
  151. SCREEN_HEIGHT_CENTER,
  152. AlignCenter,
  153. AlignCenter);
  154. } else if(token_load_result == TokenLoadingResultError) {
  155. dialog_message_set_text(
  156. message,
  157. "Unable to load tokens\nPlease review conf file",
  158. SCREEN_WIDTH_CENTER,
  159. SCREEN_HEIGHT_CENTER,
  160. AlignCenter,
  161. AlignCenter);
  162. }
  163. dialog_message_show(plugin_state->dialogs_app, message);
  164. dialog_message_free(message);
  165. }
  166. }
  167. SceneState* scene_state = malloc(sizeof(SceneState));
  168. furi_check(scene_state != NULL);
  169. if(context == NULL || context->current_token_index > plugin_state->tokens_count) {
  170. scene_state->current_token_index = 0;
  171. } else {
  172. scene_state->current_token_index = context->current_token_index;
  173. }
  174. scene_state->need_token_update = true;
  175. plugin_state->current_scene_state = scene_state;
  176. FURI_LOG_D(LOGGING_TAG, "Timezone set to: %f", (double)plugin_state->timezone_offset);
  177. update_totp_params(plugin_state);
  178. scene_state->last_code_update_sync = furi_mutex_alloc(FuriMutexTypeNormal);
  179. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  180. scene_state->usb_type_code_worker_context = totp_usb_type_code_worker_start(
  181. &scene_state->last_code[0],
  182. TOTP_TOKEN_DIGITS_MAX_COUNT + 1,
  183. scene_state->last_code_update_sync);
  184. }
  185. #ifdef TOTP_BADBT_TYPE_ENABLED
  186. if(plugin_state->automation_method & AutomationMethodBadBt) {
  187. if(plugin_state->bt_type_code_worker_context == NULL) {
  188. plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init();
  189. }
  190. totp_bt_type_code_worker_start(
  191. plugin_state->bt_type_code_worker_context,
  192. &scene_state->last_code[0],
  193. TOTP_TOKEN_DIGITS_MAX_COUNT + 1,
  194. scene_state->last_code_update_sync);
  195. }
  196. #endif
  197. }
  198. void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_state) {
  199. if(plugin_state->tokens_count == 0) {
  200. canvas_draw_str_aligned(
  201. canvas,
  202. SCREEN_WIDTH_CENTER,
  203. SCREEN_HEIGHT_CENTER - 10,
  204. AlignCenter,
  205. AlignCenter,
  206. "Token list is empty");
  207. canvas_draw_str_aligned(
  208. canvas,
  209. SCREEN_WIDTH_CENTER,
  210. SCREEN_HEIGHT_CENTER + 10,
  211. AlignCenter,
  212. AlignCenter,
  213. "Press OK button to add");
  214. return;
  215. }
  216. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  217. FuriHalRtcDateTime curr_dt;
  218. furi_hal_rtc_get_datetime(&curr_dt);
  219. uint32_t curr_ts = furi_hal_rtc_datetime_to_timestamp(&curr_dt);
  220. bool is_new_token_time = curr_ts % scene_state->current_token->duration == 0;
  221. if(is_new_token_time && scene_state->last_token_gen_time != curr_ts) {
  222. scene_state->need_token_update = true;
  223. }
  224. if(scene_state->need_token_update) {
  225. scene_state->need_token_update = false;
  226. scene_state->last_token_gen_time = curr_ts;
  227. const TokenInfo* tokenInfo = scene_state->current_token;
  228. if(tokenInfo->token != NULL && tokenInfo->token_length > 0) {
  229. furi_mutex_acquire(scene_state->last_code_update_sync, FuriWaitForever);
  230. size_t key_length;
  231. uint8_t* key = totp_crypto_decrypt(
  232. tokenInfo->token, tokenInfo->token_length, &plugin_state->iv[0], &key_length);
  233. int_token_to_str(
  234. totp_at(
  235. get_totp_algo_impl(tokenInfo->algo),
  236. tokenInfo->digits,
  237. key,
  238. key_length,
  239. curr_ts,
  240. plugin_state->timezone_offset,
  241. tokenInfo->duration),
  242. scene_state->last_code,
  243. tokenInfo->digits);
  244. memset_s(key, key_length, 0, key_length);
  245. free(key);
  246. } else {
  247. furi_mutex_acquire(scene_state->last_code_update_sync, FuriWaitForever);
  248. int_token_to_str(0, scene_state->last_code, tokenInfo->digits);
  249. }
  250. furi_mutex_release(scene_state->last_code_update_sync);
  251. if(is_new_token_time) {
  252. notification_message(
  253. plugin_state->notification_app,
  254. get_notification_sequence_new_token(plugin_state, scene_state));
  255. }
  256. }
  257. canvas_set_font(canvas, FontPrimary);
  258. uint16_t token_name_width = canvas_string_width(canvas, scene_state->current_token->name);
  259. if(SCREEN_WIDTH - token_name_width > 18) {
  260. canvas_draw_str_aligned(
  261. canvas,
  262. SCREEN_WIDTH_CENTER,
  263. SCREEN_HEIGHT_CENTER - 20,
  264. AlignCenter,
  265. AlignCenter,
  266. scene_state->current_token->name);
  267. } else {
  268. canvas_draw_str_aligned(
  269. canvas,
  270. 9,
  271. SCREEN_HEIGHT_CENTER - 20,
  272. AlignLeft,
  273. AlignCenter,
  274. scene_state->current_token->name);
  275. canvas_set_color(canvas, ColorWhite);
  276. canvas_draw_box(canvas, 0, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  277. canvas_draw_box(canvas, SCREEN_WIDTH - 10, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  278. canvas_set_color(canvas, ColorBlack);
  279. }
  280. canvas_set_font(canvas, FontBigNumbers);
  281. canvas_draw_str_aligned(
  282. canvas,
  283. SCREEN_WIDTH_CENTER,
  284. SCREEN_HEIGHT_CENTER,
  285. AlignCenter,
  286. AlignCenter,
  287. scene_state->last_code);
  288. const uint8_t TOKEN_LIFETIME = scene_state->current_token->duration;
  289. float percentDone = (float)(TOKEN_LIFETIME - curr_ts % TOKEN_LIFETIME) / (float)TOKEN_LIFETIME;
  290. uint8_t barWidth = (uint8_t)((float)(SCREEN_WIDTH - (PROGRESS_BAR_MARGIN << 1)) * percentDone);
  291. uint8_t barX =
  292. ((SCREEN_WIDTH - (PROGRESS_BAR_MARGIN << 1) - barWidth) >> 1) + PROGRESS_BAR_MARGIN;
  293. canvas_draw_box(
  294. canvas,
  295. barX,
  296. SCREEN_HEIGHT - PROGRESS_BAR_MARGIN - PROGRESS_BAR_HEIGHT,
  297. barWidth,
  298. PROGRESS_BAR_HEIGHT);
  299. if(plugin_state->tokens_count > 1) {
  300. canvas_draw_icon(canvas, 0, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_left_8x9);
  301. canvas_draw_icon(
  302. canvas, SCREEN_WIDTH - 9, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_right_8x9);
  303. }
  304. #if defined(TOTP_BADBT_TYPE_ENABLED) && defined(TOTP_BADBT_TYPE_ICON_ENABLED)
  305. if(plugin_state->automation_method & AutomationMethodBadBt &&
  306. plugin_state->bt_type_code_worker_context != NULL &&
  307. plugin_state->bt_type_code_worker_context->is_advertising) {
  308. canvas_draw_icon(
  309. canvas, SCREEN_WIDTH_CENTER - 5, SCREEN_HEIGHT_CENTER + 13, &I_hid_ble_10x7);
  310. }
  311. #endif
  312. }
  313. bool totp_scene_generate_token_handle_event(
  314. const PluginEvent* const event,
  315. PluginState* plugin_state) {
  316. if(event->type != EventTypeKey) {
  317. return true;
  318. }
  319. if(event->input.type == InputTypeLong && event->input.key == InputKeyBack) {
  320. return false;
  321. }
  322. SceneState* scene_state;
  323. if(event->input.type == InputTypeLong) {
  324. if(event->input.key == InputKeyDown && plugin_state->automation_method & AutomationMethodBadUsb) {
  325. scene_state = (SceneState*)plugin_state->current_scene_state;
  326. totp_usb_type_code_worker_notify(
  327. scene_state->usb_type_code_worker_context, TotpUsbTypeCodeWorkerEventType);
  328. notification_message(
  329. plugin_state->notification_app,
  330. get_notification_sequence_automation(plugin_state, scene_state));
  331. return true;
  332. }
  333. #ifdef TOTP_BADBT_TYPE_ENABLED
  334. else if(event->input.key == InputKeyUp && plugin_state->automation_method & AutomationMethodBadBt) {
  335. scene_state = (SceneState*)plugin_state->current_scene_state;
  336. totp_bt_type_code_worker_notify(
  337. plugin_state->bt_type_code_worker_context, TotpBtTypeCodeWorkerEventType);
  338. notification_message(
  339. plugin_state->notification_app,
  340. get_notification_sequence_automation(plugin_state, scene_state));
  341. return true;
  342. }
  343. #endif
  344. }
  345. if(event->input.type != InputTypePress && event->input.type != InputTypeRepeat) {
  346. return true;
  347. }
  348. scene_state = (SceneState*)plugin_state->current_scene_state;
  349. switch(event->input.key) {
  350. case InputKeyUp:
  351. break;
  352. case InputKeyDown:
  353. break;
  354. case InputKeyRight:
  355. totp_roll_value_uint16_t(
  356. &scene_state->current_token_index,
  357. 1,
  358. 0,
  359. plugin_state->tokens_count - 1,
  360. RollOverflowBehaviorRoll);
  361. update_totp_params(plugin_state);
  362. break;
  363. case InputKeyLeft:
  364. totp_roll_value_uint16_t(
  365. &scene_state->current_token_index,
  366. -1,
  367. 0,
  368. plugin_state->tokens_count - 1,
  369. RollOverflowBehaviorRoll);
  370. update_totp_params(plugin_state);
  371. break;
  372. case InputKeyOk:
  373. if(plugin_state->tokens_count == 0) {
  374. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, NULL);
  375. } else {
  376. TokenMenuSceneContext ctx = {.current_token_index = scene_state->current_token_index};
  377. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, &ctx);
  378. }
  379. break;
  380. case InputKeyBack:
  381. break;
  382. default:
  383. break;
  384. }
  385. return true;
  386. }
  387. void totp_scene_generate_token_deactivate(PluginState* plugin_state) {
  388. if(plugin_state->current_scene_state == NULL) return;
  389. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  390. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  391. totp_usb_type_code_worker_stop(scene_state->usb_type_code_worker_context);
  392. }
  393. #ifdef TOTP_BADBT_TYPE_ENABLED
  394. if(plugin_state->automation_method & AutomationMethodBadBt) {
  395. totp_bt_type_code_worker_stop(plugin_state->bt_type_code_worker_context);
  396. }
  397. #endif
  398. if(scene_state->notification_sequence_new_token != NULL) {
  399. free(scene_state->notification_sequence_new_token);
  400. }
  401. if(scene_state->notification_sequence_badusb != NULL) {
  402. free(scene_state->notification_sequence_badusb);
  403. }
  404. furi_mutex_free(scene_state->last_code_update_sync);
  405. free(scene_state);
  406. plugin_state->current_scene_state = NULL;
  407. }
  408. void totp_scene_generate_token_free(const PluginState* plugin_state) {
  409. UNUSED(plugin_state);
  410. }