totp_scene_generate_token.c 15 KB

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