totp_scene_generate_token.c 11 KB

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