totp_scene_generate_token.c 11 KB

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