totp_scene_authenticate.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. break;
  114. }
  115. case InputKeyBack:
  116. break;
  117. default:
  118. break;
  119. }
  120. } else if(event->input.type == InputTypeRelease && event->input.key == InputKeyOk) {
  121. CryptoSeedIVResult seed_result = totp_crypto_seed_iv(
  122. &plugin_state->crypto_settings, &scene_state->code_input[0], scene_state->code_length);
  123. if(seed_result & CryptoSeedIVResultFlagSuccess &&
  124. seed_result & CryptoSeedIVResultFlagNewCryptoVerifyData) {
  125. totp_config_file_update_crypto_signatures(plugin_state);
  126. }
  127. if(totp_crypto_verify_key(&plugin_state->crypto_settings)) {
  128. totp_config_file_ensure_latest_encryption(
  129. plugin_state, &scene_state->code_input[0], scene_state->code_length);
  130. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
  131. } else {
  132. memset(&scene_state->code_input[0], 0, MAX_CODE_LENGTH);
  133. memset(&plugin_state->crypto_settings.iv[0], 0, CRYPTO_IV_LENGTH);
  134. scene_state->code_length = 0;
  135. DialogMessage* message = dialog_message_alloc();
  136. dialog_message_set_buttons(message, "Try again", NULL, NULL);
  137. dialog_message_set_header(
  138. message,
  139. "You entered\ninvalid PIN",
  140. SCREEN_WIDTH_CENTER - 25,
  141. SCREEN_HEIGHT_CENTER - 5,
  142. AlignCenter,
  143. AlignCenter);
  144. #if __has_include(<assets_icons.h>)
  145. dialog_message_set_icon(message, &I_WarningDolphinFlip_45x42, 83, 22);
  146. #else
  147. dialog_message_set_icon(message, &I_DolphinCommon_56x48, 72, 17);
  148. #endif
  149. dialog_message_show(plugin_state->dialogs_app, message);
  150. dialog_message_free(message);
  151. }
  152. }
  153. return true;
  154. }
  155. void totp_scene_authenticate_deactivate(PluginState* plugin_state) {
  156. if(plugin_state->current_scene_state == NULL) return;
  157. free(plugin_state->current_scene_state);
  158. plugin_state->current_scene_state = NULL;
  159. }