totp_scene_generate_token.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. static const char* STEAM_ALGO_ALPHABET = "23456789BCDFGHJKMNPQRTVWXY";
  24. static const uint8_t PROGRESS_BAR_MARGIN = 3;
  25. static const uint8_t PROGRESS_BAR_HEIGHT = 4;
  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_y = SCREEN_HEIGHT_CENTER - (modeNine_15ptFontInfo.height >> 1);
  152. for(uint8_t i = 0; i < code_length; i++) {
  153. char ch = scene_state->last_code[i];
  154. uint8_t char_index = ch - modeNine_15ptFontInfo.startChar;
  155. canvas_draw_xbm(
  156. canvas,
  157. offset_x,
  158. offset_y,
  159. char_width,
  160. modeNine_15ptFontInfo.height,
  161. &modeNine_15ptFontInfo.data[modeNine_15ptFontInfo.charInfo[char_index].offset]);
  162. offset_x += char_width + modeNine_15ptFontInfo.spacePixels;
  163. }
  164. }
  165. void totp_scene_generate_token_init(const PluginState* plugin_state) {
  166. UNUSED(plugin_state);
  167. }
  168. void totp_scene_generate_token_activate(
  169. PluginState* plugin_state,
  170. const GenerateTokenSceneContext* context) {
  171. if(!plugin_state->token_list_loaded) {
  172. TokenLoadingResult token_load_result = totp_config_file_load_tokens(plugin_state);
  173. if(token_load_result != TokenLoadingResultSuccess) {
  174. DialogMessage* message = dialog_message_alloc();
  175. dialog_message_set_buttons(message, NULL, "Okay", NULL);
  176. if(token_load_result == TokenLoadingResultWarning) {
  177. dialog_message_set_text(
  178. message,
  179. "Unable to load some tokens\nPlease review conf file",
  180. SCREEN_WIDTH_CENTER,
  181. SCREEN_HEIGHT_CENTER,
  182. AlignCenter,
  183. AlignCenter);
  184. } else if(token_load_result == TokenLoadingResultError) {
  185. dialog_message_set_text(
  186. message,
  187. "Unable to load tokens\nPlease review conf file",
  188. SCREEN_WIDTH_CENTER,
  189. SCREEN_HEIGHT_CENTER,
  190. AlignCenter,
  191. AlignCenter);
  192. }
  193. dialog_message_show(plugin_state->dialogs_app, message);
  194. dialog_message_free(message);
  195. }
  196. }
  197. SceneState* scene_state = malloc(sizeof(SceneState));
  198. furi_check(scene_state != NULL);
  199. if(context == NULL || context->current_token_index > plugin_state->tokens_count) {
  200. scene_state->current_token_index = 0;
  201. } else {
  202. scene_state->current_token_index = context->current_token_index;
  203. }
  204. scene_state->need_token_update = true;
  205. plugin_state->current_scene_state = scene_state;
  206. FURI_LOG_D(LOGGING_TAG, "Timezone set to: %f", (double)plugin_state->timezone_offset);
  207. update_totp_params(plugin_state);
  208. scene_state->last_code_update_sync = furi_mutex_alloc(FuriMutexTypeNormal);
  209. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  210. scene_state->usb_type_code_worker_context = totp_usb_type_code_worker_start(
  211. &scene_state->last_code[0],
  212. TOTP_TOKEN_DIGITS_MAX_COUNT + 1,
  213. scene_state->last_code_update_sync);
  214. }
  215. #ifdef TOTP_BADBT_TYPE_ENABLED
  216. if(plugin_state->automation_method & AutomationMethodBadBt) {
  217. if(plugin_state->bt_type_code_worker_context == NULL) {
  218. plugin_state->bt_type_code_worker_context = totp_bt_type_code_worker_init();
  219. }
  220. totp_bt_type_code_worker_start(
  221. plugin_state->bt_type_code_worker_context,
  222. &scene_state->last_code[0],
  223. TOTP_TOKEN_DIGITS_MAX_COUNT + 1,
  224. scene_state->last_code_update_sync);
  225. }
  226. #endif
  227. }
  228. void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_state) {
  229. if(plugin_state->tokens_count == 0) {
  230. canvas_draw_str_aligned(
  231. canvas,
  232. SCREEN_WIDTH_CENTER,
  233. SCREEN_HEIGHT_CENTER - 10,
  234. AlignCenter,
  235. AlignCenter,
  236. "Token list is empty");
  237. canvas_draw_str_aligned(
  238. canvas,
  239. SCREEN_WIDTH_CENTER,
  240. SCREEN_HEIGHT_CENTER + 10,
  241. AlignCenter,
  242. AlignCenter,
  243. "Press OK button to add");
  244. return;
  245. }
  246. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  247. FuriHalRtcDateTime curr_dt;
  248. furi_hal_rtc_get_datetime(&curr_dt);
  249. uint32_t curr_ts = furi_hal_rtc_datetime_to_timestamp(&curr_dt);
  250. bool is_new_token_time = curr_ts % scene_state->current_token->duration == 0;
  251. if(is_new_token_time && scene_state->last_token_gen_time != curr_ts) {
  252. scene_state->need_token_update = true;
  253. }
  254. if(scene_state->need_token_update) {
  255. scene_state->need_token_update = false;
  256. scene_state->last_token_gen_time = curr_ts;
  257. const TokenInfo* tokenInfo = scene_state->current_token;
  258. if(tokenInfo->token != NULL && tokenInfo->token_length > 0) {
  259. furi_mutex_acquire(scene_state->last_code_update_sync, FuriWaitForever);
  260. size_t key_length;
  261. uint8_t* key = totp_crypto_decrypt(
  262. tokenInfo->token, tokenInfo->token_length, &plugin_state->iv[0], &key_length);
  263. int_token_to_str(
  264. totp_at(
  265. get_totp_algo_impl(tokenInfo->algo),
  266. key,
  267. key_length,
  268. curr_ts,
  269. plugin_state->timezone_offset,
  270. tokenInfo->duration),
  271. scene_state->last_code,
  272. tokenInfo->digits,
  273. tokenInfo->algo);
  274. memset_s(key, key_length, 0, key_length);
  275. free(key);
  276. } else {
  277. furi_mutex_acquire(scene_state->last_code_update_sync, FuriWaitForever);
  278. int_token_to_str(0, scene_state->last_code, tokenInfo->digits, tokenInfo->algo);
  279. }
  280. furi_mutex_release(scene_state->last_code_update_sync);
  281. if(is_new_token_time) {
  282. notification_message(
  283. plugin_state->notification_app,
  284. get_notification_sequence_new_token(plugin_state, scene_state));
  285. }
  286. }
  287. canvas_set_font(canvas, FontPrimary);
  288. uint16_t token_name_width = canvas_string_width(canvas, scene_state->current_token->name);
  289. if(SCREEN_WIDTH - token_name_width > 18) {
  290. canvas_draw_str_aligned(
  291. canvas,
  292. SCREEN_WIDTH_CENTER,
  293. SCREEN_HEIGHT_CENTER - 20,
  294. AlignCenter,
  295. AlignCenter,
  296. scene_state->current_token->name);
  297. } else {
  298. canvas_draw_str_aligned(
  299. canvas,
  300. 9,
  301. SCREEN_HEIGHT_CENTER - 20,
  302. AlignLeft,
  303. AlignCenter,
  304. scene_state->current_token->name);
  305. canvas_set_color(canvas, ColorWhite);
  306. canvas_draw_box(canvas, 0, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  307. canvas_draw_box(canvas, SCREEN_WIDTH - 10, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  308. canvas_set_color(canvas, ColorBlack);
  309. }
  310. draw_totp_code(canvas, scene_state);
  311. const uint8_t TOKEN_LIFETIME = scene_state->current_token->duration;
  312. float percentDone = (float)(TOKEN_LIFETIME - curr_ts % TOKEN_LIFETIME) / (float)TOKEN_LIFETIME;
  313. uint8_t barWidth = (uint8_t)((float)(SCREEN_WIDTH - (PROGRESS_BAR_MARGIN << 1)) * percentDone);
  314. uint8_t barX =
  315. ((SCREEN_WIDTH - (PROGRESS_BAR_MARGIN << 1) - barWidth) >> 1) + PROGRESS_BAR_MARGIN;
  316. canvas_draw_box(
  317. canvas,
  318. barX,
  319. SCREEN_HEIGHT - PROGRESS_BAR_MARGIN - PROGRESS_BAR_HEIGHT,
  320. barWidth,
  321. PROGRESS_BAR_HEIGHT);
  322. if(plugin_state->tokens_count > 1) {
  323. canvas_draw_icon(canvas, 0, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_left_8x9);
  324. canvas_draw_icon(
  325. canvas, SCREEN_WIDTH - 9, SCREEN_HEIGHT_CENTER - 24, &I_totp_arrow_right_8x9);
  326. }
  327. #ifdef TOTP_AUTOMATION_ICONS_ENABLED
  328. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  329. canvas_draw_icon(
  330. canvas,
  331. #ifdef TOTP_BADBT_TYPE_ENABLED
  332. SCREEN_WIDTH_CENTER -
  333. (plugin_state->automation_method & AutomationMethodBadBt ? 33 : 15),
  334. #else
  335. SCREEN_WIDTH_CENTER - 15,
  336. #endif
  337. SCREEN_HEIGHT_CENTER + 12,
  338. &I_hid_usb_31x9);
  339. }
  340. #ifdef TOTP_BADBT_TYPE_ENABLED
  341. if(plugin_state->automation_method & AutomationMethodBadBt &&
  342. plugin_state->bt_type_code_worker_context != NULL &&
  343. plugin_state->bt_type_code_worker_context->is_advertising) {
  344. canvas_draw_icon(
  345. canvas,
  346. SCREEN_WIDTH_CENTER +
  347. (plugin_state->automation_method & AutomationMethodBadUsb ? 2 : -15),
  348. SCREEN_HEIGHT_CENTER + 12,
  349. &I_hid_ble_31x9);
  350. }
  351. #endif
  352. #endif
  353. }
  354. bool totp_scene_generate_token_handle_event(
  355. const PluginEvent* const event,
  356. PluginState* plugin_state) {
  357. if(event->type != EventTypeKey) {
  358. return true;
  359. }
  360. if(event->input.type == InputTypeLong && event->input.key == InputKeyBack) {
  361. return false;
  362. }
  363. SceneState* scene_state;
  364. if(event->input.type == InputTypeLong) {
  365. if(event->input.key == InputKeyDown &&
  366. plugin_state->automation_method & AutomationMethodBadUsb) {
  367. scene_state = (SceneState*)plugin_state->current_scene_state;
  368. totp_usb_type_code_worker_notify(
  369. scene_state->usb_type_code_worker_context,
  370. TotpUsbTypeCodeWorkerEventType,
  371. scene_state->current_token->automation_features);
  372. notification_message(
  373. plugin_state->notification_app,
  374. get_notification_sequence_automation(plugin_state, scene_state));
  375. return true;
  376. }
  377. #ifdef TOTP_BADBT_TYPE_ENABLED
  378. else if(
  379. event->input.key == InputKeyUp &&
  380. plugin_state->automation_method & AutomationMethodBadBt) {
  381. scene_state = (SceneState*)plugin_state->current_scene_state;
  382. totp_bt_type_code_worker_notify(
  383. plugin_state->bt_type_code_worker_context,
  384. TotpBtTypeCodeWorkerEventType,
  385. scene_state->current_token->automation_features);
  386. notification_message(
  387. plugin_state->notification_app,
  388. get_notification_sequence_automation(plugin_state, scene_state));
  389. return true;
  390. }
  391. #endif
  392. }
  393. if(event->input.type != InputTypePress && event->input.type != InputTypeRepeat) {
  394. return true;
  395. }
  396. scene_state = (SceneState*)plugin_state->current_scene_state;
  397. switch(event->input.key) {
  398. case InputKeyUp:
  399. break;
  400. case InputKeyDown:
  401. break;
  402. case InputKeyRight:
  403. totp_roll_value_uint16_t(
  404. &scene_state->current_token_index,
  405. 1,
  406. 0,
  407. plugin_state->tokens_count - 1,
  408. RollOverflowBehaviorRoll);
  409. update_totp_params(plugin_state);
  410. break;
  411. case InputKeyLeft:
  412. totp_roll_value_uint16_t(
  413. &scene_state->current_token_index,
  414. -1,
  415. 0,
  416. plugin_state->tokens_count - 1,
  417. RollOverflowBehaviorRoll);
  418. update_totp_params(plugin_state);
  419. break;
  420. case InputKeyOk:
  421. if(plugin_state->tokens_count == 0) {
  422. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, NULL);
  423. } else {
  424. TokenMenuSceneContext ctx = {.current_token_index = scene_state->current_token_index};
  425. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, &ctx);
  426. }
  427. break;
  428. case InputKeyBack:
  429. break;
  430. default:
  431. break;
  432. }
  433. return true;
  434. }
  435. void totp_scene_generate_token_deactivate(PluginState* plugin_state) {
  436. if(plugin_state->current_scene_state == NULL) return;
  437. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  438. if(plugin_state->automation_method & AutomationMethodBadUsb) {
  439. totp_usb_type_code_worker_stop(scene_state->usb_type_code_worker_context);
  440. }
  441. #ifdef TOTP_BADBT_TYPE_ENABLED
  442. if(plugin_state->automation_method & AutomationMethodBadBt) {
  443. totp_bt_type_code_worker_stop(plugin_state->bt_type_code_worker_context);
  444. }
  445. #endif
  446. if(scene_state->notification_sequence_new_token != NULL) {
  447. free(scene_state->notification_sequence_new_token);
  448. }
  449. if(scene_state->notification_sequence_badusb != NULL) {
  450. free(scene_state->notification_sequence_badusb);
  451. }
  452. furi_mutex_free(scene_state->last_code_update_sync);
  453. free(scene_state);
  454. plugin_state->current_scene_state = NULL;
  455. }
  456. void totp_scene_generate_token_free(const PluginState* plugin_state) {
  457. UNUSED(plugin_state);
  458. }