pin.c 6.9 KB

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