totp_scene_generate_token.c 19 KB

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