totp_scene_generate_token.c 11 KB

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