totp_scene_authenticate.c 5.9 KB

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