totp_scene_generate_token.c 9.3 KB

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