pin.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <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(
  18. TOTP_CLI_COMMAND_PIN_COMMAND_SET " | " TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE) "\r\n");
  19. }
  20. static inline uint8_t totp_cli_key_to_pin_code(uint8_t key) {
  21. uint8_t code = 0;
  22. switch(key) {
  23. case 0x44: // left
  24. code = PinCodeArrowLeft;
  25. break;
  26. case 0x41: // up
  27. code = PinCodeArrowUp;
  28. break;
  29. case 0x43: // right
  30. code = PinCodeArrowRight;
  31. break;
  32. case 0x42: // down
  33. code = PinCodeArrowDown;
  34. break;
  35. default:
  36. break;
  37. }
  38. return code;
  39. }
  40. static bool totp_cli_read_pin(Cli* cli, uint8_t* pin, uint8_t* pin_length) {
  41. TOTP_CLI_PRINTF("Enter new PIN (use arrow keys on your keyboard): ");
  42. fflush(stdout);
  43. uint8_t c;
  44. *pin_length = 0;
  45. while(cli_read(cli, &c, 1) == 1) {
  46. if(c == CliSymbolAsciiEsc) {
  47. uint8_t c2;
  48. uint8_t c3;
  49. if(cli_read_timeout(cli, &c2, 1, 0) == 1 && cli_read_timeout(cli, &c3, 1, 0) == 1 &&
  50. c2 == 0x5b) {
  51. uint8_t code = totp_cli_key_to_pin_code(c3);
  52. if(code > 0) {
  53. pin[*pin_length] = code;
  54. *pin_length = *pin_length + 1;
  55. putc('*', stdout);
  56. fflush(stdout);
  57. }
  58. }
  59. } else if(c == CliSymbolAsciiETX) {
  60. totp_cli_delete_current_line();
  61. TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n");
  62. return false;
  63. } else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
  64. if(*pin_length > 0) {
  65. *pin_length = *pin_length - 1;
  66. pin[*pin_length] = 0;
  67. totp_cli_delete_last_char();
  68. }
  69. } else if(c == CliSymbolAsciiCR) {
  70. cli_nl();
  71. break;
  72. }
  73. }
  74. totp_cli_delete_last_line();
  75. return true;
  76. }
  77. void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  78. UNUSED(plugin_state);
  79. FuriString* temp_str = furi_string_alloc();
  80. bool do_change = false;
  81. bool do_remove = false;
  82. UNUSED(do_remove);
  83. if(args_read_string_and_trim(args, temp_str)) {
  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. }
  91. } else {
  92. totp_cli_print_invalid_arguments();
  93. }
  94. if((do_change || do_remove) && totp_cli_ensure_authenticated(plugin_state, cli)) {
  95. TOTP_CLI_LOCK_UI(plugin_state);
  96. do {
  97. uint8_t old_iv[TOTP_IV_SIZE];
  98. memcpy(&old_iv[0], &plugin_state->iv[0], TOTP_IV_SIZE);
  99. uint8_t new_pin[TOTP_IV_SIZE];
  100. memset(&new_pin[0], 0, 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. memset_s(&new_pin[0], TOTP_IV_SIZE, 0, TOTP_IV_SIZE);
  105. break;
  106. }
  107. } else if(do_remove) {
  108. new_pin_length = 0;
  109. memset(&new_pin[0], 0, TOTP_IV_SIZE);
  110. }
  111. char* backup_path = totp_config_file_backup(plugin_state);
  112. if(backup_path != NULL) {
  113. TOTP_CLI_PRINTF_WARNING("Backup conf file %s has been created\r\n", backup_path);
  114. TOTP_CLI_PRINTF_WARNING(
  115. "Once you make sure everything is fine and works as expected, please delete this backup file\r\n");
  116. free(backup_path);
  117. } else {
  118. memset_s(&new_pin[0], TOTP_IV_SIZE, 0, TOTP_IV_SIZE);
  119. TOTP_CLI_PRINTF_ERROR(
  120. "An error has occurred during taking backup of config file\r\n");
  121. break;
  122. }
  123. TOTP_CLI_PRINTF("Encrypting...\r\n");
  124. bool update_result =
  125. totp_config_file_update_encryption(plugin_state, new_pin, new_pin_length);
  126. memset_s(&new_pin[0], TOTP_IV_SIZE, 0, TOTP_IV_SIZE);
  127. totp_cli_delete_last_line();
  128. if(update_result) {
  129. if(do_change) {
  130. TOTP_CLI_PRINTF_SUCCESS("PIN has been successfully changed\r\n");
  131. } else if(do_remove) {
  132. TOTP_CLI_PRINTF_SUCCESS("PIN has been successfully removed\r\n");
  133. }
  134. } else {
  135. totp_cli_print_error_updating_config_file();
  136. }
  137. } while(false);
  138. TOTP_CLI_UNLOCK_UI(plugin_state);
  139. }
  140. furi_string_free(temp_str);
  141. }