totp_scene_generate_token.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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: return TOTP_ALGO_SHA1;
  68. case SHA256: return TOTP_ALGO_SHA256;
  69. case SHA512: return TOTP_ALGO_SHA512;
  70. }
  71. return NULL;
  72. }
  73. void update_totp_params(PluginState* const plugin_state) {
  74. SceneState* scene_state = (SceneState *)plugin_state->current_scene_state;
  75. if (scene_state->current_token_index < plugin_state->tokens_count) {
  76. TokenInfo* tokenInfo = (TokenInfo *)(list_element_at(plugin_state->tokens_list, scene_state->current_token_index)->data);
  77. scene_state->need_token_update = true;
  78. scene_state->last_code_name = tokenInfo->name;
  79. }
  80. }
  81. void totp_scene_generate_token_init(PluginState* plugin_state) {
  82. UNUSED(plugin_state);
  83. }
  84. void totp_scene_generate_token_activate(PluginState* plugin_state, const GenerateTokenSceneContext* context) {
  85. if (!plugin_state->token_list_loaded) {
  86. totp_config_file_load_tokens(plugin_state);
  87. }
  88. SceneState* scene_state = malloc(sizeof(SceneState));
  89. if (context == NULL) {
  90. scene_state->current_token_index = 0;
  91. } else {
  92. scene_state->current_token_index = context->current_token_index;
  93. }
  94. scene_state->need_token_update = true;
  95. plugin_state->current_scene_state = scene_state;
  96. FURI_LOG_D(LOGGING_TAG, "Timezone set to: %f", (double)plugin_state->timezone_offset);
  97. update_totp_params(plugin_state);
  98. }
  99. void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_state) {
  100. if (plugin_state->tokens_count == 0) {
  101. canvas_draw_str_aligned(canvas, SCREEN_WIDTH_CENTER, SCREEN_HEIGHT_CENTER - 10, AlignCenter, AlignCenter, "Token list is empty");
  102. canvas_draw_str_aligned(canvas, SCREEN_WIDTH_CENTER, SCREEN_HEIGHT_CENTER + 10, AlignCenter, AlignCenter, "Press OK button to add");
  103. return;
  104. }
  105. SceneState* scene_state = (SceneState *)plugin_state->current_scene_state;
  106. FuriHalRtcDateTime curr_dt;
  107. furi_hal_rtc_get_datetime(&curr_dt);
  108. uint32_t curr_ts = furi_hal_rtc_datetime_to_timestamp(&curr_dt);
  109. bool is_new_token_time = curr_ts % TOKEN_LIFETIME == 0;
  110. if (is_new_token_time && scene_state->last_token_gen_time != curr_ts) {
  111. scene_state->need_token_update = true;
  112. }
  113. if (scene_state->need_token_update) {
  114. scene_state->need_token_update = false;
  115. scene_state->last_token_gen_time = curr_ts;
  116. TokenInfo* tokenInfo = (TokenInfo*)(list_element_at(plugin_state->tokens_list, scene_state->current_token_index)->data);
  117. uint8_t key_length;
  118. uint8_t* key = totp_crypto_decrypt(tokenInfo->token, tokenInfo->token_length, &plugin_state->iv[0], &key_length);
  119. i_token_to_str(totp_at(get_totp_algo_impl(tokenInfo->algo), token_info_get_digits_count(tokenInfo), key, key_length, curr_ts, plugin_state->timezone_offset, TOKEN_LIFETIME), scene_state->last_code, tokenInfo->digits);
  120. memset(key, 0, key_length);
  121. free(key);
  122. if (is_new_token_time) {
  123. notification_message(plugin_state->notification, &sequence_short_vibro_and_sound);
  124. }
  125. }
  126. canvas_set_font(canvas, FontPrimary);
  127. uint16_t token_name_width = canvas_string_width(canvas, scene_state->last_code_name);
  128. if (SCREEN_WIDTH - token_name_width > 18) {
  129. canvas_draw_str_aligned(canvas, SCREEN_WIDTH_CENTER, SCREEN_HEIGHT_CENTER - 20, AlignCenter, AlignCenter, scene_state->last_code_name);
  130. } else {
  131. canvas_draw_str_aligned(canvas, 9, SCREEN_HEIGHT_CENTER - 20, AlignLeft, AlignCenter, scene_state->last_code_name);
  132. canvas_set_color(canvas, ColorWhite);
  133. canvas_draw_box(canvas, 0, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  134. canvas_draw_box(canvas, SCREEN_WIDTH - 10, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  135. canvas_set_color(canvas, ColorBlack);
  136. }
  137. canvas_set_font(canvas, FontBigNumbers);
  138. canvas_draw_str_aligned(canvas, SCREEN_WIDTH_CENTER, SCREEN_HEIGHT_CENTER, AlignCenter, AlignCenter, scene_state->last_code);
  139. const uint8_t BAR_MARGIN = 3;
  140. const uint8_t BAR_HEIGHT = 4;
  141. float percentDone = (float)(TOKEN_LIFETIME - curr_ts % TOKEN_LIFETIME) / (float)TOKEN_LIFETIME;
  142. uint8_t barWidth = (uint8_t)((float)(SCREEN_WIDTH - (BAR_MARGIN << 1)) * percentDone);
  143. uint8_t barX = ((SCREEN_WIDTH - (BAR_MARGIN << 1) - barWidth) >> 1) + BAR_MARGIN;
  144. canvas_draw_box(
  145. canvas,
  146. barX,
  147. SCREEN_HEIGHT - BAR_MARGIN - BAR_HEIGHT,
  148. barWidth,
  149. BAR_HEIGHT);
  150. if (plugin_state->tokens_count > 1) {
  151. canvas_draw_xbm(canvas, 0, SCREEN_HEIGHT_CENTER - 24, ICON_ARROW_LEFT_8x9_WIDTH, ICON_ARROW_LEFT_8x9_HEIGHT, &ICON_ARROW_LEFT_8x9[0]);
  152. 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]);
  153. }
  154. }
  155. bool totp_scene_generate_token_handle_event(PluginEvent* const event, PluginState* plugin_state) {
  156. if(event->type == EventTypeKey) {
  157. if (event->input.type == InputTypeLong && event->input.key == InputKeyBack) {
  158. return false;
  159. } else if(event->input.type == InputTypePress) {
  160. SceneState* scene_state = (SceneState *)plugin_state->current_scene_state;
  161. switch(event->input.key) {
  162. case InputKeyUp:
  163. break;
  164. case InputKeyDown:
  165. break;
  166. case InputKeyRight:
  167. if (scene_state->current_token_index < plugin_state->tokens_count - 1) {
  168. scene_state->current_token_index++;
  169. } else {
  170. scene_state->current_token_index = 0;
  171. }
  172. update_totp_params(plugin_state);
  173. break;
  174. case InputKeyLeft:
  175. if (scene_state->current_token_index > 0) {
  176. scene_state->current_token_index--;
  177. } else {
  178. scene_state->current_token_index = plugin_state->tokens_count - 1;
  179. }
  180. update_totp_params(plugin_state);
  181. break;
  182. case InputKeyOk:
  183. if (plugin_state->tokens_count == 0) {
  184. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, NULL);
  185. } else {
  186. TokenMenuSceneContext ctx = { .current_token_index = scene_state->current_token_index };
  187. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, &ctx);
  188. }
  189. break;
  190. case InputKeyBack:
  191. break;
  192. }
  193. }
  194. }
  195. return true;
  196. }
  197. void totp_scene_generate_token_deactivate(PluginState* plugin_state) {
  198. if (plugin_state->current_scene_state == NULL) return;
  199. SceneState* scene_state = (SceneState *)plugin_state->current_scene_state;
  200. free(scene_state->last_code);
  201. free(scene_state);
  202. plugin_state->current_scene_state = NULL;
  203. }
  204. void totp_scene_generate_token_free(PluginState* plugin_state) {
  205. UNUSED(plugin_state);
  206. }