pin.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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(
  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. bool load_generate_token_scene = false;
  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. uint8_t new_pin_length = 0;
  101. if(do_change) {
  102. if(!totp_cli_read_pin(cli, &new_pin[0], &new_pin_length) ||
  103. !totp_cli_ensure_authenticated(plugin_state, cli)) {
  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. if(plugin_state->current_scene == TotpSceneGenerateToken) {
  112. totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
  113. load_generate_token_scene = true;
  114. }
  115. TOTP_CLI_PRINTF("Encrypting, please wait...\r\n");
  116. memset(&plugin_state->iv[0], 0, TOTP_IV_SIZE);
  117. memset(&plugin_state->base_iv[0], 0, TOTP_IV_SIZE);
  118. if(plugin_state->crypto_verify_data != NULL) {
  119. free(plugin_state->crypto_verify_data);
  120. plugin_state->crypto_verify_data = NULL;
  121. }
  122. if(!totp_crypto_seed_iv(
  123. plugin_state, new_pin_length > 0 ? &new_pin[0] : NULL, new_pin_length)) {
  124. memset_s(&new_pin[0], TOTP_IV_SIZE, 0, TOTP_IV_SIZE);
  125. TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
  126. break;
  127. }
  128. memset_s(&new_pin[0], TOTP_IV_SIZE, 0, TOTP_IV_SIZE);
  129. TOTP_LIST_FOREACH(plugin_state->tokens_list, node, {
  130. TokenInfo* token_info = node->data;
  131. size_t plain_token_length;
  132. uint8_t* plain_token = totp_crypto_decrypt(
  133. token_info->token, token_info->token_length, &old_iv[0], &plain_token_length);
  134. free(token_info->token);
  135. token_info->token = totp_crypto_encrypt(
  136. plain_token,
  137. plain_token_length,
  138. &plugin_state->iv[0],
  139. &token_info->token_length);
  140. memset_s(plain_token, plain_token_length, 0, plain_token_length);
  141. free(plain_token);
  142. });
  143. TOTP_CLI_DELETE_LAST_LINE();
  144. if(totp_full_save_config_file(plugin_state) == TotpConfigFileUpdateSuccess) {
  145. if(do_change) {
  146. TOTP_CLI_PRINTF_SUCCESS("PIN has been successfully changed\r\n");
  147. } else if(do_remove) {
  148. TOTP_CLI_PRINTF_SUCCESS("PIN has been successfully removed\r\n");
  149. }
  150. } else {
  151. TOTP_CLI_PRINT_ERROR_UPDATING_CONFIG_FILE();
  152. }
  153. } while(false);
  154. if(load_generate_token_scene) {
  155. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  156. }
  157. }
  158. furi_string_free(temp_str);
  159. }