totp_scene_generate_token.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. list_element_at(plugin_state->tokens_list, scene_state->current_token_index)->data;
  81. scene_state->need_token_update = true;
  82. scene_state->last_code_name = tokenInfo->name;
  83. }
  84. }
  85. void totp_scene_generate_token_init(PluginState* plugin_state) {
  86. UNUSED(plugin_state);
  87. }
  88. void totp_scene_generate_token_activate(
  89. PluginState* plugin_state,
  90. const GenerateTokenSceneContext* context) {
  91. if(!plugin_state->token_list_loaded) {
  92. TokenLoadingResult token_load_result = totp_config_file_load_tokens(plugin_state);
  93. if(token_load_result != TokenLoadingResultSuccess) {
  94. DialogMessage* message = dialog_message_alloc();
  95. dialog_message_set_buttons(message, NULL, "Okay", NULL);
  96. if(token_load_result == TokenLoadingResultWarning) {
  97. dialog_message_set_text(
  98. message,
  99. "Unable to load some tokens\nPlease review conf file",
  100. SCREEN_WIDTH_CENTER,
  101. SCREEN_HEIGHT_CENTER,
  102. AlignCenter,
  103. AlignCenter);
  104. } else if(token_load_result == TokenLoadingResultError) {
  105. dialog_message_set_text(
  106. message,
  107. "Unable to load tokens\nPlease review conf file",
  108. SCREEN_WIDTH_CENTER,
  109. SCREEN_HEIGHT_CENTER,
  110. AlignCenter,
  111. AlignCenter);
  112. }
  113. dialog_message_show(plugin_state->dialogs, message);
  114. dialog_message_free(message);
  115. }
  116. }
  117. SceneState* scene_state = malloc(sizeof(SceneState));
  118. if(context == NULL || context->current_token_index > plugin_state->tokens_count) {
  119. scene_state->current_token_index = 0;
  120. } else {
  121. scene_state->current_token_index = context->current_token_index;
  122. }
  123. scene_state->need_token_update = true;
  124. plugin_state->current_scene_state = scene_state;
  125. FURI_LOG_D(LOGGING_TAG, "Timezone set to: %f", (double)plugin_state->timezone_offset);
  126. update_totp_params(plugin_state);
  127. }
  128. void totp_scene_generate_token_render(Canvas* const canvas, PluginState* plugin_state) {
  129. if(plugin_state->tokens_count == 0) {
  130. canvas_draw_str_aligned(
  131. canvas,
  132. SCREEN_WIDTH_CENTER,
  133. SCREEN_HEIGHT_CENTER - 10,
  134. AlignCenter,
  135. AlignCenter,
  136. "Token list is empty");
  137. canvas_draw_str_aligned(
  138. canvas,
  139. SCREEN_WIDTH_CENTER,
  140. SCREEN_HEIGHT_CENTER + 10,
  141. AlignCenter,
  142. AlignCenter,
  143. "Press OK button to add");
  144. return;
  145. }
  146. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  147. FuriHalRtcDateTime curr_dt;
  148. furi_hal_rtc_get_datetime(&curr_dt);
  149. uint32_t curr_ts = furi_hal_rtc_datetime_to_timestamp(&curr_dt);
  150. bool is_new_token_time = curr_ts % TOKEN_LIFETIME == 0;
  151. if(is_new_token_time && scene_state->last_token_gen_time != curr_ts) {
  152. scene_state->need_token_update = true;
  153. }
  154. if(scene_state->need_token_update) {
  155. scene_state->need_token_update = false;
  156. scene_state->last_token_gen_time = curr_ts;
  157. TokenInfo* tokenInfo =
  158. (TokenInfo*)(list_element_at(
  159. plugin_state->tokens_list, scene_state->current_token_index)
  160. ->data);
  161. if(tokenInfo->token != NULL && tokenInfo->token_length > 0) {
  162. uint8_t key_length;
  163. uint8_t* key = totp_crypto_decrypt(
  164. tokenInfo->token, tokenInfo->token_length, &plugin_state->iv[0], &key_length);
  165. i_token_to_str(
  166. totp_at(
  167. get_totp_algo_impl(tokenInfo->algo),
  168. token_info_get_digits_count(tokenInfo),
  169. key,
  170. key_length,
  171. curr_ts,
  172. plugin_state->timezone_offset,
  173. TOKEN_LIFETIME),
  174. scene_state->last_code,
  175. tokenInfo->digits);
  176. memset(key, 0, key_length);
  177. free(key);
  178. } else {
  179. i_token_to_str(0, scene_state->last_code, tokenInfo->digits);
  180. }
  181. if(is_new_token_time) {
  182. notification_message(plugin_state->notification, &sequence_short_vibro_and_sound);
  183. }
  184. }
  185. canvas_set_font(canvas, FontPrimary);
  186. uint16_t token_name_width = canvas_string_width(canvas, scene_state->last_code_name);
  187. if(SCREEN_WIDTH - token_name_width > 18) {
  188. canvas_draw_str_aligned(
  189. canvas,
  190. SCREEN_WIDTH_CENTER,
  191. SCREEN_HEIGHT_CENTER - 20,
  192. AlignCenter,
  193. AlignCenter,
  194. scene_state->last_code_name);
  195. } else {
  196. canvas_draw_str_aligned(
  197. canvas,
  198. 9,
  199. SCREEN_HEIGHT_CENTER - 20,
  200. AlignLeft,
  201. AlignCenter,
  202. scene_state->last_code_name);
  203. canvas_set_color(canvas, ColorWhite);
  204. canvas_draw_box(canvas, 0, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  205. canvas_draw_box(canvas, SCREEN_WIDTH - 10, SCREEN_HEIGHT_CENTER - 24, 9, 9);
  206. canvas_set_color(canvas, ColorBlack);
  207. }
  208. canvas_set_font(canvas, FontBigNumbers);
  209. canvas_draw_str_aligned(
  210. canvas,
  211. SCREEN_WIDTH_CENTER,
  212. SCREEN_HEIGHT_CENTER,
  213. AlignCenter,
  214. AlignCenter,
  215. scene_state->last_code);
  216. const uint8_t BAR_MARGIN = 3;
  217. const uint8_t BAR_HEIGHT = 4;
  218. float percentDone = (float)(TOKEN_LIFETIME - curr_ts % TOKEN_LIFETIME) / (float)TOKEN_LIFETIME;
  219. uint8_t barWidth = (uint8_t)((float)(SCREEN_WIDTH - (BAR_MARGIN << 1)) * percentDone);
  220. uint8_t barX = ((SCREEN_WIDTH - (BAR_MARGIN << 1) - barWidth) >> 1) + BAR_MARGIN;
  221. canvas_draw_box(canvas, barX, SCREEN_HEIGHT - BAR_MARGIN - BAR_HEIGHT, barWidth, BAR_HEIGHT);
  222. if(plugin_state->tokens_count > 1) {
  223. canvas_draw_xbm(
  224. canvas,
  225. 0,
  226. SCREEN_HEIGHT_CENTER - 24,
  227. ICON_ARROW_LEFT_8x9_WIDTH,
  228. ICON_ARROW_LEFT_8x9_HEIGHT,
  229. &ICON_ARROW_LEFT_8x9[0]);
  230. canvas_draw_xbm(
  231. canvas,
  232. SCREEN_WIDTH - 9,
  233. SCREEN_HEIGHT_CENTER - 24,
  234. ICON_ARROW_RIGHT_8x9_WIDTH,
  235. ICON_ARROW_RIGHT_8x9_HEIGHT,
  236. &ICON_ARROW_RIGHT_8x9[0]);
  237. }
  238. }
  239. bool totp_scene_generate_token_handle_event(PluginEvent* const event, PluginState* plugin_state) {
  240. if(event->type == EventTypeKey) {
  241. if(event->input.type == InputTypeLong && event->input.key == InputKeyBack) {
  242. return false;
  243. } else if(event->input.type == InputTypePress) {
  244. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  245. switch(event->input.key) {
  246. case InputKeyUp:
  247. break;
  248. case InputKeyDown:
  249. break;
  250. case InputKeyRight:
  251. if(scene_state->current_token_index < plugin_state->tokens_count - 1) {
  252. scene_state->current_token_index++;
  253. } else {
  254. scene_state->current_token_index = 0;
  255. }
  256. update_totp_params(plugin_state);
  257. break;
  258. case InputKeyLeft:
  259. if(scene_state->current_token_index > 0) {
  260. scene_state->current_token_index--;
  261. } else {
  262. scene_state->current_token_index = plugin_state->tokens_count - 1;
  263. }
  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 = {
  271. .current_token_index = scene_state->current_token_index};
  272. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, &ctx);
  273. }
  274. break;
  275. case InputKeyBack:
  276. break;
  277. }
  278. }
  279. }
  280. return true;
  281. }
  282. void totp_scene_generate_token_deactivate(PluginState* plugin_state) {
  283. if(plugin_state->current_scene_state == NULL) return;
  284. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  285. free(scene_state->last_code);
  286. free(scene_state);
  287. plugin_state->current_scene_state = NULL;
  288. }
  289. void totp_scene_generate_token_free(PluginState* plugin_state) {
  290. UNUSED(plugin_state);
  291. }