totp_scene_generate_token.c 9.8 KB

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