add.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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(
  43. TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX " " TOTP_CLI_ARG(
  44. TOTP_CLI_COMMAND_ADD_ARG_ALGO)) " " TOTP_CLI_OPTIONAL_PARAM(TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX
  45. " " TOTP_CLI_ARG(
  46. TOTP_CLI_COMMAND_ADD_ARG_DIGITS)) " " TOTP_CLI_OPTIONAL_PARAM(TOTP_CLI_COMMAND_ADD_ARG_UNSECURE_PREFIX) " - add new token\r\n");
  47. TOTP_CLI_PRINTF("\t\t" TOTP_CLI_ARG(TOTP_CLI_COMMAND_ADD_ARG_NAME) " - token name\r\n");
  48. TOTP_CLI_PRINTF("\t\t" TOTP_CLI_ARG(
  49. TOTP_CLI_COMMAND_ADD_ARG_ALGO) " - " TOTP_CLI_OPTIONAL_PARAM_MARK
  50. " token hashing algorithm, could be one of: sha1, sha256, sha512; default: sha1\r\n");
  51. TOTP_CLI_PRINTF("\t\t" TOTP_CLI_ARG(
  52. TOTP_CLI_COMMAND_ADD_ARG_DIGITS) " - " TOTP_CLI_OPTIONAL_PARAM_MARK
  53. " number of digits to generate, one of: 6, 8; default: 6\r\n");
  54. TOTP_CLI_PRINTF(
  55. "\t\t" TOTP_CLI_COMMAND_ADD_ARG_UNSECURE_PREFIX " - " TOTP_CLI_OPTIONAL_PARAM_MARK
  56. " to show console user input as-is without masking; default: show masked\r\n\r\n");
  57. }
  58. void totp_cli_command_add_handle(PluginState* plugin_state, FuriString* args, Cli* cli) {
  59. FuriString* temp_str = furi_string_alloc();
  60. const char* temp_cstr;
  61. TokenInfo* token_info = token_info_alloc();
  62. // Reading token name
  63. if(!args_read_probably_quoted_string_and_trim(args, temp_str)) {
  64. totp_cli_print_invalid_arguments();
  65. furi_string_free(temp_str);
  66. token_info_free(token_info);
  67. return;
  68. }
  69. temp_cstr = furi_string_get_cstr(temp_str);
  70. token_info->name = malloc(strlen(temp_cstr) + 1);
  71. strcpy(token_info->name, temp_cstr);
  72. // Read optional arguments
  73. bool mask_user_input = true;
  74. while(args_read_string_and_trim(args, temp_str)) {
  75. bool parsed = false;
  76. if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX) == 0) {
  77. if(!args_read_string_and_trim(args, temp_str)) {
  78. TOTP_CLI_PRINTF("Missed value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX
  79. "\"\r\n");
  80. } else if(!token_info_set_algo_from_str(token_info, temp_str)) {
  81. TOTP_CLI_PRINTF(
  82. "\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_ALGO_PREFIX
  83. "\"\r\n",
  84. furi_string_get_cstr(temp_str));
  85. } else {
  86. parsed = true;
  87. }
  88. } else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX) == 0) {
  89. if(!args_read_string_and_trim(args, temp_str)) {
  90. TOTP_CLI_PRINTF(
  91. "Missed value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX
  92. "\"\r\n");
  93. } else if(!token_info_set_digits_from_str(token_info, temp_str)) {
  94. TOTP_CLI_PRINTF(
  95. "\"%s\" is incorrect value for argument \"" TOTP_CLI_COMMAND_ADD_ARG_DIGITS_PREFIX
  96. "\"\r\n",
  97. furi_string_get_cstr(temp_str));
  98. } else {
  99. parsed = true;
  100. }
  101. } else if(furi_string_cmpi_str(temp_str, TOTP_CLI_COMMAND_ADD_ARG_UNSECURE_PREFIX) == 0) {
  102. mask_user_input = false;
  103. parsed = true;
  104. }
  105. if(!parsed) {
  106. totp_cli_print_invalid_arguments();
  107. furi_string_free(temp_str);
  108. token_info_free(token_info);
  109. return;
  110. }
  111. }
  112. // Reading token secret
  113. furi_string_reset(temp_str);
  114. TOTP_CLI_PRINTF("Enter token secret and confirm with [ENTER]:\r\n");
  115. uint8_t c;
  116. while(cli_read(cli, &c, 1) == 1) {
  117. if(c == CliSymbolAsciiEsc) {
  118. uint8_t c2;
  119. cli_read_timeout(cli, &c2, 1, 0);
  120. cli_read_timeout(cli, &c2, 1, 0);
  121. } else if(c == CliSymbolAsciiETX) {
  122. TOTP_CLI_PRINTF("Cancelled by user");
  123. furi_string_free(temp_str);
  124. token_info_free(token_info);
  125. return;
  126. } else if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
  127. if(mask_user_input) {
  128. putc('*', stdout);
  129. } else {
  130. putc(c, stdout);
  131. }
  132. fflush(stdout);
  133. furi_string_push_back(temp_str, c);
  134. } else if(c == CliSymbolAsciiBackspace || c == CliSymbolAsciiDel) {
  135. size_t temp_str_size = furi_string_size(temp_str);
  136. if(temp_str_size > 0) {
  137. TOTP_CLI_PRINTF("\b \b");
  138. fflush(stdout);
  139. furi_string_left(temp_str, temp_str_size - 1);
  140. }
  141. } else if(c == CliSymbolAsciiCR) {
  142. cli_nl();
  143. break;
  144. }
  145. }
  146. temp_cstr = furi_string_get_cstr(temp_str);
  147. if(!totp_cli_ensure_authenticated(plugin_state, cli)) {
  148. furi_string_free(temp_str);
  149. token_info_free(token_info);
  150. return;
  151. }
  152. if(!token_info_set_secret(token_info, temp_cstr, strlen(temp_cstr), plugin_state->iv)) {
  153. TOTP_CLI_PRINTF("Token secret seems to be invalid and can not be parsed\r\n");
  154. furi_string_free(temp_str);
  155. token_info_free(token_info);
  156. return;
  157. }
  158. furi_string_reset(temp_str);
  159. furi_string_free(temp_str);
  160. bool load_generate_token_scene = false;
  161. if(plugin_state->current_scene == TotpSceneGenerateToken) {
  162. totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
  163. load_generate_token_scene = true;
  164. }
  165. if(plugin_state->tokens_list == NULL) {
  166. plugin_state->tokens_list = list_init_head(token_info);
  167. } else {
  168. list_add(plugin_state->tokens_list, token_info);
  169. }
  170. plugin_state->tokens_count++;
  171. totp_config_file_save_new_token(token_info);
  172. if(load_generate_token_scene) {
  173. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken, NULL);
  174. }
  175. TOTP_CLI_PRINTF("Token \"%s\" has been successfully added\r\n", token_info->name);
  176. }