config.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #include "config.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "../../types/common.h"
  5. #include "../../types/token_info.h"
  6. #include "migrations/config_migration_v1_to_v2.h"
  7. #define CONFIG_FILE_DIRECTORY_PATH "/ext/apps/Misc"
  8. #define CONFIG_FILE_PATH CONFIG_FILE_DIRECTORY_PATH "/totp.conf"
  9. #define CONFIG_FILE_BACKUP_PATH CONFIG_FILE_PATH ".backup"
  10. uint8_t token_info_get_digits_as_int(TokenInfo* token_info) {
  11. switch (token_info->digits) {
  12. case TOTP_6_DIGITS: return 6;
  13. case TOTP_8_DIGITS: return 8;
  14. }
  15. return 6;
  16. }
  17. void token_info_set_digits_from_int(TokenInfo* token_info, uint8_t digits) {
  18. switch (digits) {
  19. case 6:
  20. token_info->digits = TOTP_6_DIGITS;
  21. break;
  22. case 8:
  23. token_info->digits = TOTP_8_DIGITS;
  24. break;
  25. }
  26. }
  27. char* token_info_get_algo_as_cstr(TokenInfo* token_info) {
  28. switch (token_info->algo) {
  29. case SHA1: return TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME;
  30. case SHA256: return TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME;
  31. case SHA512: return TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME;
  32. }
  33. return NULL;
  34. }
  35. void token_info_set_algo_from_str(TokenInfo* token_info, string_t str) {
  36. if (string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME) == 0) {
  37. token_info->algo = SHA1;
  38. } else if (string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME)) {
  39. token_info->algo = SHA256;
  40. } else if (string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME)) {
  41. token_info->algo = SHA512;
  42. }
  43. }
  44. Storage* totp_open_storage() {
  45. return furi_record_open(RECORD_STORAGE);
  46. }
  47. void totp_close_storage() {
  48. furi_record_close(RECORD_STORAGE);
  49. }
  50. FlipperFormat* totp_open_config_file(Storage* storage) {
  51. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  52. if (storage_common_stat(storage, CONFIG_FILE_PATH, NULL) == FSE_OK) {
  53. FURI_LOG_D(LOGGING_TAG, "Config file %s found", CONFIG_FILE_PATH);
  54. if(!flipper_format_file_open_existing(fff_data_file, CONFIG_FILE_PATH)) {
  55. FURI_LOG_E(LOGGING_TAG, "Error opening existing file %s", CONFIG_FILE_PATH);
  56. totp_close_config_file(fff_data_file);
  57. return NULL;
  58. }
  59. } else {
  60. FURI_LOG_D(LOGGING_TAG, "Config file %s is not found. Will create new.", CONFIG_FILE_PATH);
  61. if (storage_common_stat(storage, CONFIG_FILE_DIRECTORY_PATH, NULL) == FSE_NOT_EXIST) {
  62. FURI_LOG_D(LOGGING_TAG, "Directory %s doesn't exist. Will create new.", CONFIG_FILE_DIRECTORY_PATH);
  63. if (!storage_simply_mkdir(storage, CONFIG_FILE_DIRECTORY_PATH)) {
  64. FURI_LOG_E(LOGGING_TAG, "Error creating directory %s", CONFIG_FILE_DIRECTORY_PATH);
  65. return NULL;
  66. }
  67. }
  68. if (!flipper_format_file_open_new(fff_data_file, CONFIG_FILE_PATH)) {
  69. totp_close_config_file(fff_data_file);
  70. FURI_LOG_E(LOGGING_TAG, "Error creating new file %s", CONFIG_FILE_PATH);
  71. return NULL;
  72. }
  73. flipper_format_write_header_cstr(fff_data_file, CONFIG_FILE_HEADER, CONFIG_FILE_ACTUAL_VERSION);
  74. float tmp_tz = 0;
  75. flipper_format_write_comment_cstr(fff_data_file, " ");
  76. flipper_format_write_comment_cstr(fff_data_file, "Timezone offset in hours. Important note: do not put '+' sign for positive values");
  77. flipper_format_write_float(fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &tmp_tz, 1);
  78. string_t temp_str;
  79. string_init(temp_str);
  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. 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. 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. 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. 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. 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. string_clear(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. string_t temp_str;
  140. string_init(temp_str);
  141. uint32_t file_version;
  142. if(!flipper_format_read_header(fff_data_file, temp_str, &file_version)) {
  143. FURI_LOG_E(LOGGING_TAG, "Missing or incorrect header");
  144. string_clear(temp_str);
  145. return;
  146. }
  147. if (file_version < CONFIG_FILE_ACTUAL_VERSION) {
  148. FURI_LOG_I(LOGGING_TAG, "Obsolete config file version detected. Current version: %d; Actual version: %d", file_version, CONFIG_FILE_ACTUAL_VERSION);
  149. totp_close_config_file(fff_data_file);
  150. if (storage_common_stat(storage, CONFIG_FILE_BACKUP_PATH, NULL) == FSE_OK) {
  151. storage_simply_remove(storage, CONFIG_FILE_BACKUP_PATH);
  152. }
  153. if (storage_common_copy(storage, CONFIG_FILE_PATH, CONFIG_FILE_BACKUP_PATH) == FSE_OK) {
  154. FURI_LOG_I(LOGGING_TAG, "Took config file backup to %s", CONFIG_FILE_BACKUP_PATH);
  155. fff_data_file = totp_open_config_file(storage);
  156. FlipperFormat* fff_backup_data_file = flipper_format_file_alloc(storage);
  157. flipper_format_file_open_existing(fff_backup_data_file, CONFIG_FILE_BACKUP_PATH);
  158. if (file_version == 1) {
  159. if (totp_config_migrate_v1_to_v2(fff_data_file, fff_backup_data_file)) {
  160. FURI_LOG_I(LOGGING_TAG, "Applied migration from v1 to v2");
  161. } else {
  162. FURI_LOG_W(LOGGING_TAG, "An error occurred during migration from v1 to v2");
  163. }
  164. }
  165. flipper_format_file_close(fff_backup_data_file);
  166. flipper_format_free(fff_backup_data_file);
  167. flipper_format_rewind(fff_data_file);
  168. } else {
  169. FURI_LOG_E(LOGGING_TAG, "An error occurred during taking backup of %s into %s before migration", CONFIG_FILE_PATH, CONFIG_FILE_BACKUP_PATH);
  170. }
  171. }
  172. if (!flipper_format_read_hex(fff_data_file, TOTP_CONFIG_KEY_BASE_IV, &plugin_state->base_iv[0], TOTP_IV_SIZE)) {
  173. FURI_LOG_D(LOGGING_TAG, "Missing base IV");
  174. }
  175. flipper_format_rewind(fff_data_file);
  176. uint32_t crypto_size;
  177. if (flipper_format_get_value_count(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, &crypto_size)) {
  178. plugin_state->crypto_verify_data = malloc(sizeof(uint8_t) * crypto_size);
  179. plugin_state->crypto_verify_data_length = crypto_size;
  180. if (!flipper_format_read_hex(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, plugin_state->crypto_verify_data, crypto_size)) {
  181. FURI_LOG_D(LOGGING_TAG, "Missing crypto verify token");
  182. free(plugin_state->crypto_verify_data);
  183. plugin_state->crypto_verify_data = NULL;
  184. }
  185. }
  186. flipper_format_rewind(fff_data_file);
  187. if (!flipper_format_read_float(fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  188. plugin_state->timezone_offset = 0;
  189. FURI_LOG_D(LOGGING_TAG, "Missing timezone offset information, defaulting to 0");
  190. }
  191. string_clear(temp_str);
  192. totp_close_config_file(fff_data_file);
  193. totp_close_storage();
  194. }
  195. void totp_config_file_load_tokens(PluginState* const plugin_state) {
  196. Storage* storage = totp_open_storage();
  197. FlipperFormat* fff_data_file = totp_open_config_file(storage);
  198. string_t temp_str;
  199. uint32_t temp_data32;
  200. string_init(temp_str);
  201. if(!flipper_format_read_header(fff_data_file, temp_str, &temp_data32)) {
  202. FURI_LOG_E(LOGGING_TAG, "Missing or incorrect header");
  203. string_clear(temp_str);
  204. return;
  205. }
  206. uint8_t index = 0;
  207. bool has_any_plain_secret = false;
  208. while (true) {
  209. if (!flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_NAME, temp_str)) {
  210. break;
  211. }
  212. TokenInfo* tokenInfo = token_info_alloc();
  213. const char* temp_cstr = string_get_cstr(temp_str);
  214. tokenInfo->name = (char *)malloc(strlen(temp_cstr) + 1);
  215. strcpy(tokenInfo->name, temp_cstr);
  216. uint32_t secret_bytes_count;
  217. if (!flipper_format_get_value_count(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, &secret_bytes_count)) {
  218. token_info_free(tokenInfo);
  219. continue;
  220. }
  221. if (secret_bytes_count == 1) { // Plain secret key
  222. if (!flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str)) {
  223. token_info_free(tokenInfo);
  224. continue;
  225. }
  226. temp_cstr = string_get_cstr(temp_str);
  227. token_info_set_secret(tokenInfo, temp_cstr, strlen(temp_cstr), &plugin_state->iv[0]);
  228. has_any_plain_secret = true;
  229. FURI_LOG_W(LOGGING_TAG, "Found token with plain secret");
  230. } else { // encrypted
  231. tokenInfo->token_length = secret_bytes_count;
  232. tokenInfo->token = malloc(tokenInfo->token_length);
  233. if (!flipper_format_read_hex(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, tokenInfo->token, tokenInfo->token_length)) {
  234. token_info_free(tokenInfo);
  235. continue;
  236. }
  237. }
  238. if (!flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str)) {
  239. token_info_free(tokenInfo);
  240. continue;
  241. }
  242. token_info_set_algo_from_str(tokenInfo, temp_str);
  243. if (!flipper_format_read_uint32(fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &temp_data32, 1)) {
  244. token_info_free(tokenInfo);
  245. continue;
  246. }
  247. token_info_set_digits_from_int(tokenInfo, temp_data32);
  248. FURI_LOG_D(LOGGING_TAG, "Found token \"%s\"", tokenInfo->name);
  249. if (plugin_state->tokens_list == NULL) {
  250. plugin_state->tokens_list = list_init_head(tokenInfo);
  251. } else {
  252. list_add(plugin_state->tokens_list, tokenInfo);
  253. }
  254. index++;
  255. }
  256. plugin_state->tokens_count = index;
  257. plugin_state->token_list_loaded = true;
  258. FURI_LOG_D(LOGGING_TAG, "Found %d tokens", index);
  259. string_clear(temp_str);
  260. totp_close_config_file(fff_data_file);
  261. totp_close_storage();
  262. if (has_any_plain_secret) {
  263. totp_full_save_config_file(plugin_state);
  264. }
  265. }
  266. void totp_close_config_file(FlipperFormat* file) {
  267. if (file == NULL) return;
  268. flipper_format_file_close(file);
  269. flipper_format_free(file);
  270. }