pin.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_facade.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. #define TOTP_CLI_COMMAND_PIN_ARG_NEW_CRYPTO_KEY_SLOT_PREFIX "-c"
  14. #define TOTP_CLI_COMMAND_PIN_ARG_NEW_CRYPTO_KEY_SLOT "slot"
  15. #ifdef TOTP_CLI_RICH_HELP_ENABLED
  16. void totp_cli_command_pin_docopt_commands() {
  17. TOTP_CLI_PRINTF(" " TOTP_CLI_COMMAND_PIN " Set\\change\\remove PIN\r\n");
  18. }
  19. void totp_cli_command_pin_docopt_usage() {
  20. TOTP_CLI_PRINTF(
  21. " " TOTP_CLI_COMMAND_NAME " " TOTP_CLI_COMMAND_PIN
  22. " " DOCOPT_REQUIRED(TOTP_CLI_COMMAND_PIN_COMMAND_SET " | " TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE) " " DOCOPT_OPTIONAL(
  23. DOCOPT_OPTION(
  24. TOTP_CLI_COMMAND_PIN_ARG_NEW_CRYPTO_KEY_SLOT_PREFIX,
  25. DOCOPT_ARGUMENT(TOTP_CLI_COMMAND_PIN_ARG_NEW_CRYPTO_KEY_SLOT))) "\r\n");
  26. }
  27. void totp_cli_command_pin_docopt_options() {
  28. TOTP_CLI_PRINTF(
  29. " " DOCOPT_OPTION(
  30. TOTP_CLI_COMMAND_PIN_ARG_NEW_CRYPTO_KEY_SLOT_PREFIX,
  31. DOCOPT_ARGUMENT(
  32. TOTP_CLI_COMMAND_PIN_ARG_NEW_CRYPTO_KEY_SLOT)) " New crypto key slot. Must be between %d and %d\r\n",
  33. ACCEPTABLE_CRYPTO_KEY_SLOT_START,
  34. ACCEPTABLE_CRYPTO_KEY_SLOT_END);
  35. }
  36. #endif
  37. static inline uint8_t totp_cli_key_to_pin_code(uint8_t key) {
  38. uint8_t code = 0;
  39. switch(key) {
  40. case 0x44: // left
  41. code = PinCodeArrowLeft;
  42. break;
  43. case 0x41: // up
  44. code = PinCodeArrowUp;
  45. break;
  46. case 0x43: // right
  47. code = PinCodeArrowRight;
  48. break;
  49. case 0x42: // down
  50. code = PinCodeArrowDown;
  51. break;
  52. default:
  53. break;
  54. }
  55. return code;
  56. }
  57. static bool totp_cli_read_pin(Cli* cli, uint8_t* pin, uint8_t* pin_length) {
  58. TOTP_CLI_PRINTF("Enter new PIN (use arrow keys on your keyboard): ");
  59. fflush(stdout);
  60. uint8_t c;
  61. *pin_length = 0;
  62. while(cli_read(cli, &c, 1) == 1) {
  63. if(c == CliSymbolAsciiEsc) {
  64. uint8_t c2;
  65. uint8_t c3;
  66. if(cli_read_timeout(cli, &c2, 1, 0) == 1 && cli_read_timeout(cli, &c3, 1, 0) == 1 &&
  67. c2 == 0x5b) {
  68. uint8_t code = totp_cli_key_to_pin_code(c3);
  69. if(code > 0) {
  70. pin[*pin_length] = code;
  71. *pin_length = *pin_length + 1;
  72. putc('*', stdout);
  73. fflush(stdout);
  74. }
  75. }
  76. } else if(c == CliSymbolAsciiETX) {
  77. totp_cli_delete_current_line();
  78. TOTP_CLI_PRINTF_INFO("Cancelled by user\r\n");
  79. return false;
  80. } else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
  81. if(*pin_length > 0) {
  82. *pin_length = *pin_length - 1;
  83. pin[*pin_length] = 0;
  84. totp_cli_delete_last_char();
  85. }
  86. } else if(c == CliSymbolAsciiCR) {
  87. cli_nl();
  88. break;
  89. }
  90. }
  91. totp_cli_delete_last_line();
  92. return true;
  93. }
  94. void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  95. UNUSED(plugin_state);
  96. FuriString* temp_str = furi_string_alloc();
  97. bool do_change = false;
  98. bool do_remove = false;
  99. uint8_t crypto_key_slot = plugin_state->crypto_settings.crypto_key_slot;
  100. bool arguments_parsed = true;
  101. while(args_read_string_and_trim(args, temp_str)) {
  102. if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_PIN_COMMAND_SET) == 0) {
  103. do_change = true;
  104. } else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_PIN_COMMAND_REMOVE) == 0) {
  105. do_remove = true;
  106. } else if(
  107. furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_PIN_ARG_NEW_CRYPTO_KEY_SLOT_PREFIX) ==
  108. 0) {
  109. if(!args_read_uint8_and_trim(args, &crypto_key_slot) ||
  110. !totp_crypto_check_key_slot(crypto_key_slot)) {
  111. TOTP_CLI_PRINTF_ERROR("Slot \"%" PRIu8 "\" can not be used\r\n", crypto_key_slot);
  112. arguments_parsed = false;
  113. break;
  114. }
  115. } else {
  116. totp_cli_print_invalid_arguments();
  117. arguments_parsed = false;
  118. break;
  119. }
  120. }
  121. if(!(do_change || do_remove) || (do_change && do_remove)) {
  122. totp_cli_print_invalid_arguments();
  123. arguments_parsed = false;
  124. }
  125. if(arguments_parsed && totp_cli_ensure_authenticated(plugin_state, cli)) {
  126. TOTP_CLI_LOCK_UI(plugin_state);
  127. do {
  128. uint8_t new_pin[CRYPTO_IV_LENGTH];
  129. memset(&new_pin[0], 0, CRYPTO_IV_LENGTH);
  130. uint8_t new_pin_length = 0;
  131. if(do_change) {
  132. if(!totp_cli_read_pin(cli, &new_pin[0], &new_pin_length)) {
  133. memset_s(&new_pin[0], CRYPTO_IV_LENGTH, 0, CRYPTO_IV_LENGTH);
  134. break;
  135. }
  136. } else if(do_remove) {
  137. new_pin_length = 0;
  138. memset(&new_pin[0], 0, CRYPTO_IV_LENGTH);
  139. }
  140. char* backup_path = totp_config_file_backup(plugin_state);
  141. if(backup_path != NULL) {
  142. TOTP_CLI_PRINTF_WARNING("Backup conf file %s has been created\r\n", backup_path);
  143. TOTP_CLI_PRINTF_WARNING(
  144. "Once you make sure everything is fine and works as expected, please delete this backup file\r\n");
  145. free(backup_path);
  146. } else {
  147. memset_s(&new_pin[0], CRYPTO_IV_LENGTH, 0, CRYPTO_IV_LENGTH);
  148. TOTP_CLI_PRINTF_ERROR(
  149. "An error has occurred during taking backup of config file\r\n");
  150. break;
  151. }
  152. TOTP_CLI_PRINTF("Encrypting...\r\n");
  153. bool update_result = totp_config_file_update_encryption(
  154. plugin_state, crypto_key_slot, new_pin, new_pin_length);
  155. memset_s(&new_pin[0], CRYPTO_IV_LENGTH, 0, CRYPTO_IV_LENGTH);
  156. totp_cli_delete_last_line();
  157. if(update_result) {
  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. TOTP_CLI_UNLOCK_UI(plugin_state);
  168. }
  169. furi_string_free(temp_str);
  170. }