totp_scene_generate_token.c 11 KB

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