totp_scene_authenticate.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "totp_scene_authenticate.h"
  2. #include <dialogs/dialogs.h>
  3. #include <totp_icons.h>
  4. #if __has_include(<assets_icons.h>)
  5. #include <assets_icons.h>
  6. #endif
  7. #include "../../../types/common.h"
  8. #include "../../constants.h"
  9. #include "../../../services/config/config.h"
  10. #include "../../scene_director.h"
  11. #include "../../totp_scenes_enum.h"
  12. #include "../../../services/crypto/crypto_facade.h"
  13. #include "../../../types/user_pin_codes.h"
  14. #define MAX_CODE_LENGTH CRYPTO_IV_LENGTH
  15. static const uint8_t PIN_ASTERISK_RADIUS = 3;
  16. static const uint8_t PIN_ASTERISK_STEP = (PIN_ASTERISK_RADIUS << 1) + 2;
  17. typedef struct {
  18. TotpUserPinCode code_input[MAX_CODE_LENGTH];
  19. uint8_t code_length;
  20. } SceneState;
  21. void totp_scene_authenticate_activate(PluginState* plugin_state) {
  22. SceneState* scene_state = malloc(sizeof(SceneState));
  23. furi_check(scene_state != NULL);
  24. scene_state->code_length = 0;
  25. memset(&scene_state->code_input[0], 0, MAX_CODE_LENGTH);
  26. plugin_state->current_scene_state = scene_state;
  27. memset(&plugin_state->crypto_settings.iv[0], 0, CRYPTO_IV_LENGTH);
  28. }
  29. void totp_scene_authenticate_render(Canvas* const canvas, PluginState* plugin_state) {
  30. const SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  31. int v_shift = 0;
  32. if(scene_state->code_length > 0) {
  33. v_shift = -10;
  34. }
  35. if(plugin_state->crypto_settings.crypto_verify_data == NULL) {
  36. canvas_draw_str_aligned(
  37. canvas,
  38. SCREEN_WIDTH_CENTER,
  39. SCREEN_HEIGHT_CENTER - 10 + v_shift,
  40. AlignCenter,
  41. AlignCenter,
  42. "Use arrow keys");
  43. canvas_draw_str_aligned(
  44. canvas,
  45. SCREEN_WIDTH_CENTER,
  46. SCREEN_HEIGHT_CENTER + 5 + v_shift,
  47. AlignCenter,
  48. AlignCenter,
  49. "to setup new PIN");
  50. } else {
  51. canvas_draw_str_aligned(
  52. canvas,
  53. SCREEN_WIDTH_CENTER,
  54. SCREEN_HEIGHT_CENTER + v_shift,
  55. AlignCenter,
  56. AlignCenter,
  57. "Use arrow keys to enter PIN");
  58. }
  59. if(scene_state->code_length > 0) {
  60. uint8_t left_start_x = ((scene_state->code_length - 1) * PIN_ASTERISK_STEP) >> 1;
  61. for(uint8_t i = 0; i < scene_state->code_length; i++) {
  62. canvas_draw_disc(
  63. canvas,
  64. SCREEN_WIDTH_CENTER - left_start_x + i * PIN_ASTERISK_STEP,
  65. SCREEN_HEIGHT_CENTER + 10,
  66. PIN_ASTERISK_RADIUS);
  67. }
  68. }
  69. }
  70. bool totp_scene_authenticate_handle_event(
  71. const PluginEvent* const event,
  72. PluginState* plugin_state) {
  73. if(event->type != EventTypeKey) {
  74. return true;
  75. }
  76. if(event->input.type == InputTypeShort && event->input.key == InputKeyBack) {
  77. return false;
  78. }
  79. SceneState* scene_state = plugin_state->current_scene_state;
  80. if((event->input.type == InputTypeLong || event->input.type == InputTypeRepeat) &&
  81. event->input.key == InputKeyBack) {
  82. if(scene_state->code_length > 0) {
  83. scene_state->code_input[scene_state->code_length - 1] = 0;
  84. scene_state->code_length--;
  85. }
  86. } else if(event->input.type == InputTypePress) {
  87. switch(event->input.key) {
  88. case InputKeyUp:
  89. if(scene_state->code_length < MAX_CODE_LENGTH) {
  90. scene_state->code_input[scene_state->code_length] = PinCodeArrowUp;
  91. scene_state->code_length++;
  92. }
  93. break;
  94. case InputKeyDown:
  95. if(scene_state->code_length < MAX_CODE_LENGTH) {
  96. scene_state->code_input[scene_state->code_length] = PinCodeArrowDown;
  97. scene_state->code_length++;
  98. }
  99. break;
  100. case InputKeyRight:
  101. if(scene_state->code_length < MAX_CODE_LENGTH) {
  102. scene_state->code_input[scene_state->code_length] = PinCodeArrowRight;
  103. scene_state->code_length++;
  104. }
  105. break;
  106. case InputKeyLeft:
  107. if(scene_state->code_length < MAX_CODE_LENGTH) {
  108. scene_state->code_input[scene_state->code_length] = PinCodeArrowLeft;
  109. scene_state->code_length++;
  110. }
  111. break;
  112. case InputKeyOk: {
  113. CryptoSeedIVResult seed_result = totp_crypto_seed_iv(
  114. &plugin_state->crypto_settings,
  115. &scene_state->code_input[0],
  116. scene_state->code_length);
  117. if(seed_result & CryptoSeedIVResultFlagSuccess &&
  118. seed_result & CryptoSeedIVResultFlagNewCryptoVerifyData) {
  119. totp_config_file_update_crypto_signatures(plugin_state);
  120. }
  121. if(totp_crypto_verify_key(&plugin_state->crypto_settings)) {
  122. totp_config_file_ensure_latest_encryption(
  123. plugin_state, &scene_state->code_input[0], scene_state->code_length);
  124. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
  125. } else {
  126. memset(&scene_state->code_input[0], 0, MAX_CODE_LENGTH);
  127. memset(&plugin_state->crypto_settings.iv[0], 0, CRYPTO_IV_LENGTH);
  128. scene_state->code_length = 0;
  129. DialogMessage* message = dialog_message_alloc();
  130. dialog_message_set_buttons(message, "Try again", NULL, NULL);
  131. dialog_message_set_header(
  132. message,
  133. "You entered\ninvalid PIN",
  134. SCREEN_WIDTH_CENTER - 25,
  135. SCREEN_HEIGHT_CENTER - 5,
  136. AlignCenter,
  137. AlignCenter);
  138. #if __has_include(<assets_icons.h>)
  139. dialog_message_set_icon(message, &I_WarningDolphinFlip_45x42, 83, 22);
  140. #else
  141. dialog_message_set_icon(message, &I_DolphinCommon_56x48, 72, 17);
  142. #endif
  143. dialog_message_show(plugin_state->dialogs_app, message);
  144. dialog_message_free(message);
  145. }
  146. break;
  147. }
  148. case InputKeyBack:
  149. break;
  150. default:
  151. break;
  152. }
  153. }
  154. return true;
  155. }
  156. void totp_scene_authenticate_deactivate(PluginState* plugin_state) {
  157. if(plugin_state->current_scene_state == NULL) return;
  158. free(plugin_state->current_scene_state);
  159. plugin_state->current_scene_state = NULL;
  160. }