totp_scene_generate_token.c 15 KB

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