pin.c 6.6 KB

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