totp_scene_generate_token.c 15 KB

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