config.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include "config.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "../list/list.h"
  5. #include "../../types/common.h"
  6. #include "../../types/token_info.h"
  7. #include "migrations/config_migration_v1_to_v2.h"
  8. #define CONFIG_FILE_DIRECTORY_PATH "/ext/apps/Misc"
  9. #define CONFIG_FILE_PATH CONFIG_FILE_DIRECTORY_PATH "/totp.conf"
  10. #define CONFIG_FILE_BACKUP_PATH CONFIG_FILE_PATH ".backup"
  11. uint8_t token_info_get_digits_as_int(TokenInfo* token_info) {
  12. switch (token_info->digits) {
  13. case TOTP_6_DIGITS: return 6;
  14. case TOTP_8_DIGITS: return 8;
  15. }
  16. return 6;
  17. }
  18. void token_info_set_digits_from_int(TokenInfo* token_info, uint8_t digits) {
  19. switch (digits) {
  20. case 6:
  21. token_info->digits = TOTP_6_DIGITS;
  22. break;
  23. case 8:
  24. token_info->digits = TOTP_8_DIGITS;
  25. break;
  26. }
  27. }
  28. char* token_info_get_algo_as_cstr(TokenInfo* token_info) {
  29. switch (token_info->algo) {
  30. case SHA1: return TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME;
  31. case SHA256: return TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME;
  32. case SHA512: return TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME;
  33. }
  34. return NULL;
  35. }
  36. void token_info_set_algo_from_str(TokenInfo* token_info, FuriString* str) {
  37. if (furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME) == 0) {
  38. token_info->algo = SHA1;
  39. } else if (furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME)) {
  40. token_info->algo = SHA256;
  41. } else if (furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME)) {
  42. token_info->algo = SHA512;
  43. }
  44. }
  45. Storage* totp_open_storage() {
  46. return furi_record_open(RECORD_STORAGE);
  47. }
  48. void totp_close_storage() {
  49. furi_record_close(RECORD_STORAGE);
  50. }
  51. FlipperFormat* totp_open_config_file(Storage* storage) {
  52. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  53. if (storage_common_stat(storage, CONFIG_FILE_PATH, NULL) == FSE_OK) {
  54. FURI_LOG_D(LOGGING_TAG, "Config file %s found", CONFIG_FILE_PATH);
  55. if(!flipper_format_file_open_existing(fff_data_file, CONFIG_FILE_PATH)) {
  56. FURI_LOG_E(LOGGING_TAG, "Error opening existing file %s", CONFIG_FILE_PATH);
  57. totp_close_config_file(fff_data_file);
  58. return NULL;
  59. }
  60. } else {
  61. FURI_LOG_D(LOGGING_TAG, "Config file %s is not found. Will create new.", CONFIG_FILE_PATH);
  62. if (storage_common_stat(storage, CONFIG_FILE_DIRECTORY_PATH, NULL) == FSE_NOT_EXIST) {
  63. FURI_LOG_D(LOGGING_TAG, "Directory %s doesn't exist. Will create new.", CONFIG_FILE_DIRECTORY_PATH);
  64. if (!storage_simply_mkdir(storage, CONFIG_FILE_DIRECTORY_PATH)) {
  65. FURI_LOG_E(LOGGING_TAG, "Error creating directory %s", CONFIG_FILE_DIRECTORY_PATH);
  66. return NULL;
  67. }
  68. }
  69. if (!flipper_format_file_open_new(fff_data_file, CONFIG_FILE_PATH)) {
  70. totp_close_config_file(fff_data_file);
  71. FURI_LOG_E(LOGGING_TAG, "Error creating new file %s", CONFIG_FILE_PATH);
  72. return NULL;
  73. }
  74. flipper_format_write_header_cstr(fff_data_file, CONFIG_FILE_HEADER, CONFIG_FILE_ACTUAL_VERSION);
  75. float tmp_tz = 0;
  76. flipper_format_write_comment_cstr(fff_data_file, " ");
  77. flipper_format_write_comment_cstr(fff_data_file, "Timezone offset in hours. Important note: do not put '+' sign for positive values");
  78. flipper_format_write_float(fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &tmp_tz, 1);
  79. FuriString* temp_str = furi_string_alloc();
  80. flipper_format_write_comment_cstr(fff_data_file, " ");
  81. flipper_format_write_comment_cstr(fff_data_file, "=== TOKEN SAMPLE BEGIN ===");
  82. flipper_format_write_comment_cstr(fff_data_file, " ");
  83. flipper_format_write_comment_cstr(fff_data_file, "# Token name which will be visible in the UI.");
  84. furi_string_printf(temp_str, "%s: Sample token name", TOTP_CONFIG_KEY_TOKEN_NAME);
  85. flipper_format_write_comment(fff_data_file, temp_str);
  86. flipper_format_write_comment_cstr(fff_data_file, " ");
  87. flipper_format_write_comment_cstr(fff_data_file, "# Plain token secret without spaces, dashes and etc, just pure alpha-numeric characters. Important note: plain token will be encrypted and replaced by TOTP app");
  88. furi_string_printf(temp_str, "%s: plaintokensecret", TOTP_CONFIG_KEY_TOKEN_SECRET);
  89. flipper_format_write_comment(fff_data_file, temp_str);
  90. flipper_format_write_comment_cstr(fff_data_file, " ");
  91. furi_string_printf(temp_str, " # Token hashing algorithm to use during code generation. Supported options are %s, %s and %s. If you are not use which one to use - use %s", TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME, TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME, TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME, TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME);
  92. flipper_format_write_comment(fff_data_file, temp_str);
  93. furi_string_printf(temp_str, "%s: %s", TOTP_CONFIG_KEY_TOKEN_ALGO, TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME);
  94. flipper_format_write_comment(fff_data_file, temp_str);
  95. flipper_format_write_comment_cstr(fff_data_file, " ");
  96. flipper_format_write_comment_cstr(fff_data_file, "# How many digits there should be in generated code. Available options are 6 and 8. Majority websites requires 6 digits code, however some rare websites wants to get 8 digits code. If you are not sure which one to use - use 6");
  97. furi_string_printf(temp_str, "%s: 6", TOTP_CONFIG_KEY_TOKEN_DIGITS);
  98. flipper_format_write_comment(fff_data_file, temp_str);
  99. flipper_format_write_comment_cstr(fff_data_file, " ");
  100. flipper_format_write_comment_cstr(fff_data_file, "=== TOKEN SAMPLE END ===");
  101. flipper_format_write_comment_cstr(fff_data_file, " ");
  102. furi_string_free(temp_str);
  103. if(!flipper_format_rewind(fff_data_file)) {
  104. totp_close_config_file(fff_data_file);
  105. FURI_LOG_E(LOGGING_TAG, "Rewind error");
  106. return NULL;
  107. }
  108. }
  109. return fff_data_file;
  110. }
  111. void totp_config_file_save_new_token(FlipperFormat* file, TokenInfo* token_info) {
  112. flipper_format_write_string_cstr(file, TOTP_CONFIG_KEY_TOKEN_NAME, token_info->name);
  113. flipper_format_write_hex(file, TOTP_CONFIG_KEY_TOKEN_SECRET, token_info->token, token_info->token_length);
  114. flipper_format_write_string_cstr(file, TOTP_CONFIG_KEY_TOKEN_ALGO, token_info_get_algo_as_cstr(token_info));
  115. uint32_t digits_count_as_uint32 = token_info_get_digits_as_int(token_info);
  116. flipper_format_write_uint32(file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &digits_count_as_uint32, 1);
  117. }
  118. void totp_full_save_config_file(PluginState* const plugin_state) {
  119. Storage* storage = totp_open_storage();
  120. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  121. flipper_format_file_open_always(fff_data_file, CONFIG_FILE_PATH);
  122. flipper_format_write_header_cstr(fff_data_file, CONFIG_FILE_HEADER, CONFIG_FILE_ACTUAL_VERSION);
  123. flipper_format_write_hex(fff_data_file, TOTP_CONFIG_KEY_BASE_IV, &plugin_state->base_iv[0], TOTP_IV_SIZE);
  124. flipper_format_write_hex(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, plugin_state->crypto_verify_data, plugin_state->crypto_verify_data_length);
  125. flipper_format_write_float(fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1);
  126. ListNode* node = plugin_state->tokens_list;
  127. while (node != NULL) {
  128. TokenInfo* token_info = node->data;
  129. totp_config_file_save_new_token(fff_data_file, token_info);
  130. node = node->next;
  131. }
  132. totp_close_config_file(fff_data_file);
  133. totp_close_storage();
  134. }
  135. void totp_config_file_load_base(PluginState* const plugin_state) {
  136. Storage* storage = totp_open_storage();
  137. FlipperFormat* fff_data_file = totp_open_config_file(storage);
  138. plugin_state->timezone_offset = 0;
  139. FuriString* temp_str = furi_string_alloc();
  140. uint32_t file_version;
  141. if(!flipper_format_read_header(fff_data_file, temp_str, &file_version)) {
  142. FURI_LOG_E(LOGGING_TAG, "Missing or incorrect header");
  143. furi_string_free(temp_str);
  144. return;
  145. }
  146. if (file_version < CONFIG_FILE_ACTUAL_VERSION) {
  147. FURI_LOG_I(LOGGING_TAG, "Obsolete config file version detected. Current version: %d; Actual version: %d", file_version, CONFIG_FILE_ACTUAL_VERSION);
  148. totp_close_config_file(fff_data_file);
  149. if (storage_common_stat(storage, CONFIG_FILE_BACKUP_PATH, NULL) == FSE_OK) {
  150. storage_simply_remove(storage, CONFIG_FILE_BACKUP_PATH);
  151. }
  152. if (storage_common_copy(storage, CONFIG_FILE_PATH, CONFIG_FILE_BACKUP_PATH) == FSE_OK) {
  153. FURI_LOG_I(LOGGING_TAG, "Took config file backup to %s", CONFIG_FILE_BACKUP_PATH);
  154. fff_data_file = totp_open_config_file(storage);
  155. FlipperFormat* fff_backup_data_file = flipper_format_file_alloc(storage);
  156. flipper_format_file_open_existing(fff_backup_data_file, CONFIG_FILE_BACKUP_PATH);
  157. if (file_version == 1) {
  158. if (totp_config_migrate_v1_to_v2(fff_data_file, fff_backup_data_file)) {
  159. FURI_LOG_I(LOGGING_TAG, "Applied migration from v1 to v2");
  160. } else {
  161. FURI_LOG_W(LOGGING_TAG, "An error occurred during migration from v1 to v2");
  162. }
  163. }
  164. flipper_format_file_close(fff_backup_data_file);
  165. flipper_format_free(fff_backup_data_file);
  166. flipper_format_rewind(fff_data_file);
  167. } else {
  168. FURI_LOG_E(LOGGING_TAG, "An error occurred during taking backup of %s into %s before migration", CONFIG_FILE_PATH, CONFIG_FILE_BACKUP_PATH);
  169. }
  170. }
  171. if (!flipper_format_read_hex(fff_data_file, TOTP_CONFIG_KEY_BASE_IV, &plugin_state->base_iv[0], TOTP_IV_SIZE)) {
  172. FURI_LOG_D(LOGGING_TAG, "Missing base IV");
  173. }
  174. flipper_format_rewind(fff_data_file);
  175. uint32_t crypto_size;
  176. if (flipper_format_get_value_count(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, &crypto_size)) {
  177. plugin_state->crypto_verify_data = malloc(sizeof(uint8_t) * crypto_size);
  178. plugin_state->crypto_verify_data_length = crypto_size;
  179. if (!flipper_format_read_hex(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, plugin_state->crypto_verify_data, crypto_size)) {
  180. FURI_LOG_D(LOGGING_TAG, "Missing crypto verify token");
  181. free(plugin_state->crypto_verify_data);
  182. plugin_state->crypto_verify_data = NULL;
  183. }
  184. }
  185. flipper_format_rewind(fff_data_file);
  186. if (!flipper_format_read_float(fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  187. plugin_state->timezone_offset = 0;
  188. FURI_LOG_D(LOGGING_TAG, "Missing timezone offset information, defaulting to 0");
  189. }
  190. furi_string_free(temp_str);
  191. totp_close_config_file(fff_data_file);
  192. totp_close_storage();
  193. }
  194. void totp_config_file_load_tokens(PluginState* const plugin_state) {
  195. Storage* storage = totp_open_storage();
  196. FlipperFormat* fff_data_file = totp_open_config_file(storage);
  197. FuriString* temp_str = furi_string_alloc();
  198. uint32_t temp_data32;
  199. if(!flipper_format_read_header(fff_data_file, temp_str, &temp_data32)) {
  200. FURI_LOG_E(LOGGING_TAG, "Missing or incorrect header");
  201. furi_string_free(temp_str);
  202. return;
  203. }
  204. uint8_t index = 0;
  205. bool has_any_plain_secret = false;
  206. while (true) {
  207. if (!flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_NAME, temp_str)) {
  208. break;
  209. }
  210. TokenInfo* tokenInfo = token_info_alloc();
  211. const char* temp_cstr = furi_string_get_cstr(temp_str);
  212. tokenInfo->name = (char *)malloc(strlen(temp_cstr) + 1);
  213. strcpy(tokenInfo->name, temp_cstr);
  214. uint32_t secret_bytes_count;
  215. if (!flipper_format_get_value_count(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, &secret_bytes_count)) {
  216. token_info_free(tokenInfo);
  217. continue;
  218. }
  219. if (secret_bytes_count == 1) { // Plain secret key
  220. if (!flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str)) {
  221. token_info_free(tokenInfo);
  222. continue;
  223. }
  224. temp_cstr = furi_string_get_cstr(temp_str);
  225. token_info_set_secret(tokenInfo, temp_cstr, strlen(temp_cstr), &plugin_state->iv[0]);
  226. has_any_plain_secret = true;
  227. FURI_LOG_W(LOGGING_TAG, "Found token with plain secret");
  228. } else { // encrypted
  229. tokenInfo->token_length = secret_bytes_count;
  230. tokenInfo->token = malloc(tokenInfo->token_length);
  231. if (!flipper_format_read_hex(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, tokenInfo->token, tokenInfo->token_length)) {
  232. token_info_free(tokenInfo);
  233. continue;
  234. }
  235. }
  236. if (!flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str)) {
  237. token_info_free(tokenInfo);
  238. continue;
  239. }
  240. token_info_set_algo_from_str(tokenInfo, temp_str);
  241. if (!flipper_format_read_uint32(fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &temp_data32, 1)) {
  242. token_info_free(tokenInfo);
  243. continue;
  244. }
  245. token_info_set_digits_from_int(tokenInfo, temp_data32);
  246. FURI_LOG_D(LOGGING_TAG, "Found token \"%s\"", tokenInfo->name);
  247. if (plugin_state->tokens_list == NULL) {
  248. plugin_state->tokens_list = list_init_head(tokenInfo);
  249. } else {
  250. list_add(plugin_state->tokens_list, tokenInfo);
  251. }
  252. index++;
  253. }
  254. plugin_state->tokens_count = index;
  255. plugin_state->token_list_loaded = true;
  256. FURI_LOG_D(LOGGING_TAG, "Found %d tokens", index);
  257. furi_string_free(temp_str);
  258. totp_close_config_file(fff_data_file);
  259. totp_close_storage();
  260. if (has_any_plain_secret) {
  261. totp_full_save_config_file(plugin_state);
  262. }
  263. }
  264. void totp_close_config_file(FlipperFormat* file) {
  265. if (file == NULL) return;
  266. flipper_format_file_close(file);
  267. flipper_format_free(file);
  268. }