totp_scene_generate_token.c 11 KB

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