pin.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "pin.h"
  2. #include <stdlib.h>
  3. #include <lib/toolbox/args.h>
  4. #include "../../../types/token_info.h"
  5. #include "../../../types/user_pin_codes.h"
  6. #include "../../../services/config/config.h"
  7. #include "../../cli_helpers.h"
  8. #include "../../../lib/polyfills/memset_s.h"
  9. #include "../../../services/crypto/crypto.h"
  10. #include "../../../ui/scene_director.h"
  11. #define TOTP_CLI_COMMAND_PIN_COMMAND_SET "set"
  12. #define TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE "remove"
  13. void totp_cli_command_pin_docopt_commands() {
  14. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_PIN " Set\\change\\remove PIN\r\n");
  15. }
  16. void totp_cli_command_pin_docopt_usage() {
  17. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_NAME " " TOTP_CLI_COMMAND_PIN " " DOCOPT_REQUIRED(TOTP_CLI_COMMAND_PIN_COMMAND_SET " | " TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE) "\r\n");
  18. }
  19. static bool totp_cli_read_pin(Cli* cli, uint8_t* pin, uint8_t* pin_length) {
  20. TOTP_CLI_PRINTF("Enter new PIN (use arrow keys on your keyboard): ");
  21. fflush(stdout);
  22. uint8_t c;
  23. *pin_length = 0;
  24. while(cli_read(cli, &c, 1) == 1) {
  25. if(c == CliSymbolAsciiEsc) {
  26. uint8_t c2;
  27. uint8_t c3;
  28. if (cli_read_timeout(cli, &c2, 1, 0) == 1 &&
  29. cli_read_timeout(cli, &c3, 1, 0) == 1 &&
  30. c2 == 0x5b) {
  31. uint8_t code = 0;
  32. switch (c3) {
  33. case 0x44: // left
  34. code = PinCodeArrowLeft;
  35. break;
  36. case 0x41: // up
  37. code = PinCodeArrowUp;
  38. break;
  39. case 0x43: // right
  40. code = PinCodeArrowRight;
  41. break;
  42. case 0x42: // down
  43. code = PinCodeArrowDown;
  44. break;
  45. default:
  46. break;
  47. }
  48. if (code > 0) {
  49. pin[*pin_length] = code;
  50. *pin_length = *pin_length + 1;
  51. putc('*', stdout);
  52. fflush(stdout);
  53. }
  54. }
  55. } else if(c == CliSymbolAsciiETX) {
  56. TOTP_CLI_DELETE_CURRENT_LINE();
  57. TOTP_CLI_PRINTF("Cancelled by user\r\n");
  58. return false;
  59. } else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
  60. if (*pin_length > 0) {
  61. *pin_length = *pin_length - 1;
  62. pin[*pin_length] = 0;
  63. TOTP_CLI_DELETE_LAST_CHAR();
  64. }
  65. } else if(c == CliSymbolAsciiCR) {
  66. cli_nl();
  67. break;
  68. }
  69. }
  70. TOTP_CLI_DELETE_LAST_LINE();
  71. return true;
  72. }
  73. void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  74. UNUSED(plugin_state);
  75. FuriString* temp_str = furi_string_alloc();
  76. bool do_change = false;
  77. bool do_remove = false;
  78. UNUSED(do_remove);
  79. do {
  80. if (!args_read_string_and_trim(args, temp_str)) {
  81. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  82. break;
  83. }
  84. if (furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_PIN_COMMAND_SET) == 0) {
  85. do_change = true;
  86. } else if (furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE) == 0) {
  87. do_remove = true;
  88. } else {
  89. TOTP_CLI_PRINT_INVALID_ARGUMENTS();
  90. break;
  91. }
  92. } while (false);
  93. if ((do_change || do_remove) && totp_cli_ensure_authenticated(plugin_state, cli)) {
  94. bool load_generate_token_scene = false;
  95. do {
  96. uint8_t old_iv[TOTP_IV_SIZE];
  97. memcpy(&old_iv[0], &plugin_state->iv[0], TOTP_IV_SIZE);
  98. uint8_t new_pin[TOTP_IV_SIZE];
  99. uint8_t new_pin_length = 0;
  100. if (do_change) {
  101. if (!totp_cli_read_pin(cli, &new_pin[0], &new_pin_length) ||
  102. !totp_cli_ensure_authenticated(plugin_state, cli)) {
  103. memset_s(&new_pin[0], TOTP_IV_SIZE, 0, TOTP_IV_SIZE);
  104. break;
  105. }
  106. } else if (do_remove) {
  107. new_pin_length = 0;
  108. memset(&new_pin[0], 0, TOTP_IV_SIZE);
  109. }
  110. if(plugin_state->current_scene == TotpSceneGenerateToken) {
  111. totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
  112. load_generate_token_scene = true;
  113. }
  114. TOTP_CLI_PRINTF("Encrypting, please wait...\r\n");
  115. memset(&plugin_state->iv[0], 0, TOTP_IV_SIZE);
  116. memset(&plugin_state->base_iv[0], 0, TOTP_IV_SIZE);
  117. if (plugin_state->crypto_verify_data != NULL) {
  118. free(plugin_state->crypto_verify_data);
  119. plugin_state->crypto_verify_data = NULL;
  120. }
  121. totp_crypto_seed_iv(plugin_state, new_pin_length > 0 ? &new_pin[0] : NULL, new_pin_length);
  122. ListNode* node = plugin_state->tokens_list;
  123. while (node != NULL) {
  124. TokenInfo* token_info = node->data;
  125. size_t plain_token_length;
  126. uint8_t* plain_token = totp_crypto_decrypt(token_info->token, token_info->token_length, &old_iv[0], &plain_token_length);
  127. free(token_info->token);
  128. token_info->token = totp_crypto_encrypt(plain_token, plain_token_length, &plugin_state->iv[0], &token_info->token_length);
  129. memset_s(plain_token, plain_token_length, 0, plain_token_length);
  130. free(plain_token);
  131. node = node->next;
  132. }
  133. totp_full_save_config_file(plugin_state);
  134. TOTP_CLI_DELETE_LAST_LINE();
  135. if (do_change) {
  136. TOTP_CLI_PRINTF("PIN has been successfully changed\r\n");
  137. } else if (do_remove) {
  138. TOTP_CLI_PRINTF("PIN has been successfully removed\r\n");
  139. }
  140. } while (false);
  141. if(load_generate_token_scene) {
  142. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  143. }
  144. }
  145. furi_string_free(temp_str);
  146. }