config.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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_i(FlipperFormat* file, TokenInfo* token_info) {
  112. flipper_format_seek_to_end(file);
  113. flipper_format_write_string_cstr(file, TOTP_CONFIG_KEY_TOKEN_NAME, token_info->name);
  114. flipper_format_write_hex(file, TOTP_CONFIG_KEY_TOKEN_SECRET, token_info->token, token_info->token_length);
  115. flipper_format_write_string_cstr(file, TOTP_CONFIG_KEY_TOKEN_ALGO, token_info_get_algo_as_cstr(token_info));
  116. uint32_t digits_count_as_uint32 = token_info_get_digits_as_int(token_info);
  117. flipper_format_write_uint32(file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &digits_count_as_uint32, 1);
  118. }
  119. void totp_config_file_save_new_token(TokenInfo* token_info) {
  120. Storage* cfg_storage = totp_open_storage();
  121. FlipperFormat* file = totp_open_config_file(cfg_storage);
  122. totp_config_file_save_new_token_i(file, token_info);
  123. totp_close_config_file(file);
  124. totp_close_storage();
  125. }
  126. void totp_config_file_update_timezone_offset(float new_timezone_offset) {
  127. Storage* cfg_storage = totp_open_storage();
  128. FlipperFormat* file = totp_open_config_file(cfg_storage);
  129. flipper_format_insert_or_update_float(file, TOTP_CONFIG_KEY_TIMEZONE, &new_timezone_offset, 1);
  130. totp_close_config_file(file);
  131. totp_close_storage();
  132. }
  133. void totp_full_save_config_file(PluginState* const plugin_state) {
  134. Storage* storage = totp_open_storage();
  135. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  136. flipper_format_file_open_always(fff_data_file, CONFIG_FILE_PATH);
  137. flipper_format_write_header_cstr(fff_data_file, CONFIG_FILE_HEADER, CONFIG_FILE_ACTUAL_VERSION);
  138. flipper_format_write_hex(fff_data_file, TOTP_CONFIG_KEY_BASE_IV, &plugin_state->base_iv[0], TOTP_IV_SIZE);
  139. flipper_format_write_hex(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, plugin_state->crypto_verify_data, plugin_state->crypto_verify_data_length);
  140. flipper_format_write_float(fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1);
  141. flipper_format_write_bool(fff_data_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1);
  142. ListNode* node = plugin_state->tokens_list;
  143. while (node != NULL) {
  144. TokenInfo* token_info = node->data;
  145. totp_config_file_save_new_token_i(fff_data_file, token_info);
  146. node = node->next;
  147. }
  148. totp_close_config_file(fff_data_file);
  149. totp_close_storage();
  150. }
  151. void totp_config_file_load_base(PluginState* const plugin_state) {
  152. Storage* storage = totp_open_storage();
  153. FlipperFormat* fff_data_file = totp_open_config_file(storage);
  154. plugin_state->timezone_offset = 0;
  155. FuriString* temp_str = furi_string_alloc();
  156. uint32_t file_version;
  157. if(!flipper_format_read_header(fff_data_file, temp_str, &file_version)) {
  158. FURI_LOG_E(LOGGING_TAG, "Missing or incorrect header");
  159. furi_string_free(temp_str);
  160. return;
  161. }
  162. if (file_version < CONFIG_FILE_ACTUAL_VERSION) {
  163. FURI_LOG_I(LOGGING_TAG, "Obsolete config file version detected. Current version: %" PRIu32 "; Actual version: %" PRId16, file_version, CONFIG_FILE_ACTUAL_VERSION);
  164. totp_close_config_file(fff_data_file);
  165. if (storage_common_stat(storage, CONFIG_FILE_BACKUP_PATH, NULL) == FSE_OK) {
  166. storage_simply_remove(storage, CONFIG_FILE_BACKUP_PATH);
  167. }
  168. if (storage_common_copy(storage, CONFIG_FILE_PATH, CONFIG_FILE_BACKUP_PATH) == FSE_OK) {
  169. FURI_LOG_I(LOGGING_TAG, "Took config file backup to %s", CONFIG_FILE_BACKUP_PATH);
  170. fff_data_file = totp_open_config_file(storage);
  171. FlipperFormat* fff_backup_data_file = flipper_format_file_alloc(storage);
  172. flipper_format_file_open_existing(fff_backup_data_file, CONFIG_FILE_BACKUP_PATH);
  173. if (file_version == 1) {
  174. if (totp_config_migrate_v1_to_v2(fff_data_file, fff_backup_data_file)) {
  175. FURI_LOG_I(LOGGING_TAG, "Applied migration from v1 to v2");
  176. } else {
  177. FURI_LOG_W(LOGGING_TAG, "An error occurred during migration from v1 to v2");
  178. }
  179. }
  180. flipper_format_file_close(fff_backup_data_file);
  181. flipper_format_free(fff_backup_data_file);
  182. flipper_format_rewind(fff_data_file);
  183. } else {
  184. FURI_LOG_E(LOGGING_TAG, "An error occurred during taking backup of %s into %s before migration", CONFIG_FILE_PATH, CONFIG_FILE_BACKUP_PATH);
  185. }
  186. }
  187. if (!flipper_format_read_hex(fff_data_file, TOTP_CONFIG_KEY_BASE_IV, &plugin_state->base_iv[0], TOTP_IV_SIZE)) {
  188. FURI_LOG_D(LOGGING_TAG, "Missing base IV");
  189. }
  190. flipper_format_rewind(fff_data_file);
  191. uint32_t crypto_size;
  192. if (flipper_format_get_value_count(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, &crypto_size) && crypto_size > 0) {
  193. plugin_state->crypto_verify_data = malloc(sizeof(uint8_t) * crypto_size);
  194. plugin_state->crypto_verify_data_length = crypto_size;
  195. if (!flipper_format_read_hex(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, plugin_state->crypto_verify_data, crypto_size)) {
  196. FURI_LOG_D(LOGGING_TAG, "Missing crypto verify token");
  197. free(plugin_state->crypto_verify_data);
  198. plugin_state->crypto_verify_data = NULL;
  199. plugin_state->crypto_verify_data_length = 0;
  200. }
  201. } else {
  202. plugin_state->crypto_verify_data = NULL;
  203. plugin_state->crypto_verify_data_length = 0;
  204. }
  205. flipper_format_rewind(fff_data_file);
  206. if (!flipper_format_read_float(fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  207. plugin_state->timezone_offset = 0;
  208. FURI_LOG_D(LOGGING_TAG, "Missing timezone offset information, defaulting to 0");
  209. }
  210. flipper_format_rewind(fff_data_file);
  211. if (!flipper_format_read_bool(fff_data_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1)) {
  212. plugin_state->pin_set = true;
  213. }
  214. furi_string_free(temp_str);
  215. totp_close_config_file(fff_data_file);
  216. totp_close_storage();
  217. }
  218. void totp_config_file_load_tokens(PluginState* const plugin_state) {
  219. Storage* storage = totp_open_storage();
  220. FlipperFormat* fff_data_file = totp_open_config_file(storage);
  221. FuriString* temp_str = furi_string_alloc();
  222. uint32_t temp_data32;
  223. if(!flipper_format_read_header(fff_data_file, temp_str, &temp_data32)) {
  224. FURI_LOG_E(LOGGING_TAG, "Missing or incorrect header");
  225. furi_string_free(temp_str);
  226. return;
  227. }
  228. uint8_t index = 0;
  229. bool has_any_plain_secret = false;
  230. while (true) {
  231. if (!flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_NAME, temp_str)) {
  232. break;
  233. }
  234. TokenInfo* tokenInfo = token_info_alloc();
  235. const char* temp_cstr = furi_string_get_cstr(temp_str);
  236. tokenInfo->name = (char *)malloc(strlen(temp_cstr) + 1);
  237. strcpy(tokenInfo->name, temp_cstr);
  238. uint32_t secret_bytes_count;
  239. if (!flipper_format_get_value_count(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, &secret_bytes_count)) {
  240. token_info_free(tokenInfo);
  241. continue;
  242. }
  243. if (secret_bytes_count == 1) { // Plain secret key
  244. if (!flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str)) {
  245. token_info_free(tokenInfo);
  246. continue;
  247. }
  248. temp_cstr = furi_string_get_cstr(temp_str);
  249. token_info_set_secret(tokenInfo, temp_cstr, strlen(temp_cstr), &plugin_state->iv[0]);
  250. has_any_plain_secret = true;
  251. FURI_LOG_W(LOGGING_TAG, "Found token with plain secret");
  252. } else { // encrypted
  253. tokenInfo->token_length = secret_bytes_count;
  254. tokenInfo->token = malloc(tokenInfo->token_length);
  255. if (!flipper_format_read_hex(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, tokenInfo->token, tokenInfo->token_length)) {
  256. token_info_free(tokenInfo);
  257. continue;
  258. }
  259. }
  260. if (!flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str)) {
  261. token_info_free(tokenInfo);
  262. continue;
  263. }
  264. token_info_set_algo_from_str(tokenInfo, temp_str);
  265. if (!flipper_format_read_uint32(fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &temp_data32, 1)) {
  266. token_info_free(tokenInfo);
  267. continue;
  268. }
  269. token_info_set_digits_from_int(tokenInfo, temp_data32);
  270. FURI_LOG_D(LOGGING_TAG, "Found token \"%s\"", tokenInfo->name);
  271. if (plugin_state->tokens_list == NULL) {
  272. plugin_state->tokens_list = list_init_head(tokenInfo);
  273. } else {
  274. list_add(plugin_state->tokens_list, tokenInfo);
  275. }
  276. index++;
  277. }
  278. plugin_state->tokens_count = index;
  279. plugin_state->token_list_loaded = true;
  280. FURI_LOG_D(LOGGING_TAG, "Found %" PRIu8 " tokens", index);
  281. furi_string_free(temp_str);
  282. totp_close_config_file(fff_data_file);
  283. totp_close_storage();
  284. if (has_any_plain_secret) {
  285. totp_full_save_config_file(plugin_state);
  286. }
  287. }
  288. void totp_close_config_file(FlipperFormat* file) {
  289. if (file == NULL) return;
  290. flipper_format_file_close(file);
  291. flipper_format_free(file);
  292. }