totp_scene_generate_token.c 12 KB

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