add.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "add.h"
  2. #include <stdlib.h>
  3. #include <lib/toolbox/args.h>
  4. #include "../../../list/list.h"
  5. #include "../../../../types/token_info.h"
  6. #include "../../../config/config.h"
  7. #include "../../cli_common_helpers.h"
  8. #include "../../../../scenes/scene_director.h"
  9. #define TOTP_CLI_COMMAND_ADD_ARG_NAME "NAME"
  10. #define TOTP_CLI_COMMAND_ADD_ARG_ALGO "ALGO"
  11. #define TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX "-a"
  12. #define TOTP_CLI_COMMAND_ADD_ARG_DIGITS "DIGITS"
  13. #define TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX "-d"
  14. #define TOTP_CLI_COMMAND_ADD_ARG_UNSECURE_PREFIX "-u"
  15. static bool token_info_set_digits_from_str(TokenInfo* token_info, FuriString* str) {
  16. switch(furi_string_get_char(str, 0)) {
  17. case '6':
  18. token_info->digits = TOTP_6_DIGITS;
  19. return true;
  20. case '8':
  21. token_info->digits = TOTP_8_DIGITS;
  22. return true;
  23. }
  24. return false;
  25. }
  26. static bool token_info_set_algo_from_str(TokenInfo* token_info, FuriString* str) {
  27. if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME) == 0) {
  28. token_info->algo = SHA1;
  29. return true;
  30. }
  31. if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME) == 0) {
  32. token_info->algo = SHA256;
  33. return true;
  34. }
  35. if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME) == 0) {
  36. token_info->algo = SHA512;
  37. return true;
  38. }
  39. return false;
  40. }
  41. void totp_cli_command_add_print_help() {
  42. TOTP_CLI_PRINTF("\t" TOTP_CLI_COMMAND_ADD " " TOTP_CLI_ARG(TOTP_CLI_COMMAND_ADD_ARG_NAME) " " TOTP_CLI_OPTIONAL_PARAM(TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX " " TOTP_CLI_ARG(TOTP_CLI_COMMAND_ADD_ARG_ALGO)) " " TOTP_CLI_OPTIONAL_PARAM(TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX " " TOTP_CLI_ARG(TOTP_CLI_COMMAND_ADD_ARG_DIGITS)) " " TOTP_CLI_OPTIONAL_PARAM(TOTP_CLI_COMMAND_ADD_ARG_UNSECURE_PREFIX) " - add new token\r\n");
  43. TOTP_CLI_PRINTF("\t\t" TOTP_CLI_ARG(TOTP_CLI_COMMAND_ADD_ARG_NAME) " - token name\r\n");
  44. TOTP_CLI_PRINTF("\t\t" TOTP_CLI_ARG(TOTP_CLI_COMMAND_ADD_ARG_ALGO) " - " TOTP_CLI_OPTIONAL_PARAM_MARK " token hashing algorithm, could be one of: sha1, sha256, sha512; default: sha1\r\n");
  45. TOTP_CLI_PRINTF("\t\t" TOTP_CLI_ARG(TOTP_CLI_COMMAND_ADD_ARG_DIGITS) " - " TOTP_CLI_OPTIONAL_PARAM_MARK " number of digits to generate, one of: 6, 8; default: 6\r\n");
  46. TOTP_CLI_PRINTF("\t\t" TOTP_CLI_COMMAND_ADD_ARG_UNSECURE_PREFIX " - " TOTP_CLI_OPTIONAL_PARAM_MARK " to show console user input as-is without masking; default: show masked\r\n\r\n");
  47. }
  48. void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  49. FuriString* temp_str = furi_string_alloc();
  50. const char* temp_cstr;
  51. TokenInfo* token_info = token_info_alloc();
  52. // Reading token name
  53. if (!args_read_probably_quoted_string_and_trim(args, temp_str)) {
  54. totp_cli_print_invalid_arguments();
  55. furi_string_free(temp_str);
  56. token_info_free(token_info);
  57. return;
  58. }
  59. temp_cstr = furi_string_get_cstr(temp_str);
  60. token_info->name = malloc(strlen(temp_cstr) + 1);
  61. strcpy(token_info->name, temp_cstr);
  62. // Read optional arguments
  63. bool mask_user_input = true;
  64. while (args_read_string_and_trim(args, temp_str)) {
  65. bool parsed = false;
  66. if (furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX) == 0) {
  67. if (!args_read_string_and_trim(args, temp_str)) {
  68. TOTP_CLI_PRINTF("Missed value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX "\"\r\n");
  69. } else if (!token_info_set_algo_from_str(token_info, temp_str)) {
  70. TOTP_CLI_PRINTF("\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX "\"\r\n", furi_string_get_cstr(temp_str));
  71. } else {
  72. parsed = true;
  73. }
  74. } else if (furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX) == 0) {
  75. if (!args_read_string_and_trim(args, temp_str)) {
  76. TOTP_CLI_PRINTF("Missed value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX "\"\r\n");
  77. } else if (!token_info_set_digits_from_str(token_info, temp_str)) {
  78. TOTP_CLI_PRINTF("\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX "\"\r\n", furi_string_get_cstr(temp_str));
  79. } else {
  80. parsed = true;
  81. }
  82. } else if (furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_UNSECURE_PREFIX) == 0) {
  83. mask_user_input = false;
  84. parsed = true;
  85. }
  86. if (!parsed) {
  87. totp_cli_print_invalid_arguments();
  88. furi_string_free(temp_str);
  89. token_info_free(token_info);
  90. return;
  91. }
  92. }
  93. // Reading token secret
  94. furi_string_reset(temp_str);
  95. TOTP_CLI_PRINTF("Enter token secret and confirm with [ENTER]:\r\n");
  96. uint8_t c;
  97. while(cli_read(cli, &c, 1) == 1) {
  98. if (c == CliSymbolAsciiEsc) {
  99. uint8_t c2;
  100. cli_read_timeout(cli, &c2, 1, 0);
  101. cli_read_timeout(cli, &c2, 1, 0);
  102. } else if(c == CliSymbolAsciiETX) {
  103. TOTP_CLI_PRINTF("Cancelled by user");
  104. furi_string_free(temp_str);
  105. token_info_free(token_info);
  106. return;
  107. } else if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
  108. if (mask_user_input) {
  109. putc('*', stdout);
  110. } else {
  111. putc(c, stdout);
  112. }
  113. fflush(stdout);
  114. furi_string_push_back(temp_str, c);
  115. } else if (c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
  116. size_t temp_str_size = furi_string_size(temp_str);
  117. if (temp_str_size > 0) {
  118. TOTP_CLI_PRINTF("\b \b");
  119. fflush(stdout);
  120. furi_string_left(temp_str, temp_str_size - 1);
  121. }
  122. }
  123. else if(c == CliSymbolAsciiCR) {
  124. cli_nl();
  125. break;
  126. }
  127. }
  128. temp_cstr = furi_string_get_cstr(temp_str);
  129. if (!totp_cli_ensure_authenticated(plugin_state, cli)) {
  130. furi_string_free(temp_str);
  131. token_info_free(token_info);
  132. return;
  133. }
  134. if (!token_info_set_secret(token_info, temp_cstr, strlen(temp_cstr), plugin_state->iv)) {
  135. TOTP_CLI_PRINTF("Token secret seems to be invalid and can not be parsed\r\n");
  136. furi_string_free(temp_str);
  137. token_info_free(token_info);
  138. return;
  139. }
  140. furi_string_reset(temp_str);
  141. furi_string_free(temp_str);
  142. bool load_generate_token_scene = false;
  143. if (plugin_state->current_scene == TotpSceneGenerateToken) {
  144. totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
  145. load_generate_token_scene = true;
  146. }
  147. if(plugin_state->tokens_list == NULL) {
  148. plugin_state->tokens_list = list_init_head(token_info);
  149. } else {
  150. list_add(plugin_state->tokens_list, token_info);
  151. }
  152. plugin_state->tokens_count++;
  153. totp_config_file_save_new_token(token_info);
  154. if (load_generate_token_scene) {
  155. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  156. }
  157. TOTP_CLI_PRINTF("Token \"%s\" has been successfully added\r\n", token_info->name);
  158. }