config.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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:
  14. return 6;
  15. case TOTP_8_DIGITS:
  16. return 8;
  17. }
  18. return 6;
  19. }
  20. void token_info_set_digits_from_int(TokenInfo* token_info, uint8_t digits) {
  21. switch(digits) {
  22. case 6:
  23. token_info->digits = TOTP_6_DIGITS;
  24. break;
  25. case 8:
  26. token_info->digits = TOTP_8_DIGITS;
  27. break;
  28. }
  29. }
  30. char* token_info_get_algo_as_cstr(TokenInfo* token_info) {
  31. switch(token_info->algo) {
  32. case SHA1:
  33. return TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME;
  34. case SHA256:
  35. return TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME;
  36. case SHA512:
  37. return TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME;
  38. }
  39. return NULL;
  40. }
  41. void token_info_set_algo_from_str(TokenInfo* token_info, FuriString* str) {
  42. if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME) == 0) {
  43. token_info->algo = SHA1;
  44. } else if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME)) {
  45. token_info->algo = SHA256;
  46. } else if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME)) {
  47. token_info->algo = SHA512;
  48. }
  49. }
  50. Storage* totp_open_storage() {
  51. return furi_record_open(RECORD_STORAGE);
  52. }
  53. void totp_close_storage() {
  54. furi_record_close(RECORD_STORAGE);
  55. }
  56. FlipperFormat* totp_open_config_file(Storage* storage) {
  57. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  58. if(storage_common_stat(storage, CONFIG_FILE_PATH, NULL) == FSE_OK) {
  59. FURI_LOG_D(LOGGING_TAG, "Config file %s found", CONFIG_FILE_PATH);
  60. if(!flipper_format_file_open_existing(fff_data_file, CONFIG_FILE_PATH)) {
  61. FURI_LOG_E(LOGGING_TAG, "Error opening existing file %s", CONFIG_FILE_PATH);
  62. totp_close_config_file(fff_data_file);
  63. return NULL;
  64. }
  65. } else {
  66. FURI_LOG_D(LOGGING_TAG, "Config file %s is not found. Will create new.", CONFIG_FILE_PATH);
  67. if(storage_common_stat(storage, CONFIG_FILE_DIRECTORY_PATH, NULL) == FSE_NOT_EXIST) {
  68. FURI_LOG_D(
  69. LOGGING_TAG,
  70. "Directory %s doesn't exist. Will create new.",
  71. CONFIG_FILE_DIRECTORY_PATH);
  72. if(!storage_simply_mkdir(storage, CONFIG_FILE_DIRECTORY_PATH)) {
  73. FURI_LOG_E(LOGGING_TAG, "Error creating directory %s", CONFIG_FILE_DIRECTORY_PATH);
  74. return NULL;
  75. }
  76. }
  77. if(!flipper_format_file_open_new(fff_data_file, CONFIG_FILE_PATH)) {
  78. totp_close_config_file(fff_data_file);
  79. FURI_LOG_E(LOGGING_TAG, "Error creating new file %s", CONFIG_FILE_PATH);
  80. return NULL;
  81. }
  82. flipper_format_write_header_cstr(
  83. fff_data_file, CONFIG_FILE_HEADER, CONFIG_FILE_ACTUAL_VERSION);
  84. float tmp_tz = 0;
  85. flipper_format_write_comment_cstr(fff_data_file, " ");
  86. flipper_format_write_comment_cstr(
  87. fff_data_file,
  88. "Timezone offset in hours. Important note: do not put '+' sign for positive values");
  89. flipper_format_write_float(fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &tmp_tz, 1);
  90. FuriString* temp_str = furi_string_alloc();
  91. flipper_format_write_comment_cstr(fff_data_file, " ");
  92. flipper_format_write_comment_cstr(fff_data_file, "=== TOKEN SAMPLE BEGIN ===");
  93. flipper_format_write_comment_cstr(fff_data_file, " ");
  94. flipper_format_write_comment_cstr(
  95. fff_data_file, "# Token name which will be visible in the UI.");
  96. furi_string_printf(temp_str, "%s: Sample token name", TOTP_CONFIG_KEY_TOKEN_NAME);
  97. flipper_format_write_comment(fff_data_file, temp_str);
  98. flipper_format_write_comment_cstr(fff_data_file, " ");
  99. flipper_format_write_comment_cstr(
  100. fff_data_file,
  101. "# 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");
  102. furi_string_printf(temp_str, "%s: plaintokensecret", TOTP_CONFIG_KEY_TOKEN_SECRET);
  103. flipper_format_write_comment(fff_data_file, temp_str);
  104. flipper_format_write_comment_cstr(fff_data_file, " ");
  105. furi_string_printf(
  106. temp_str,
  107. " # 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",
  108. TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME,
  109. TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME,
  110. TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME,
  111. TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME);
  112. flipper_format_write_comment(fff_data_file, temp_str);
  113. furi_string_printf(
  114. temp_str, "%s: %s", TOTP_CONFIG_KEY_TOKEN_ALGO, TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME);
  115. flipper_format_write_comment(fff_data_file, temp_str);
  116. flipper_format_write_comment_cstr(fff_data_file, " ");
  117. flipper_format_write_comment_cstr(
  118. fff_data_file,
  119. "# 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");
  120. furi_string_printf(temp_str, "%s: 6", TOTP_CONFIG_KEY_TOKEN_DIGITS);
  121. flipper_format_write_comment(fff_data_file, temp_str);
  122. flipper_format_write_comment_cstr(fff_data_file, " ");
  123. flipper_format_write_comment_cstr(fff_data_file, "=== TOKEN SAMPLE END ===");
  124. flipper_format_write_comment_cstr(fff_data_file, " ");
  125. furi_string_free(temp_str);
  126. if(!flipper_format_rewind(fff_data_file)) {
  127. totp_close_config_file(fff_data_file);
  128. FURI_LOG_E(LOGGING_TAG, "Rewind error");
  129. return NULL;
  130. }
  131. }
  132. return fff_data_file;
  133. }
  134. void totp_config_file_save_new_token_i(FlipperFormat* file, TokenInfo* token_info) {
  135. flipper_format_seek_to_end(file);
  136. flipper_format_write_string_cstr(file, TOTP_CONFIG_KEY_TOKEN_NAME, token_info->name);
  137. flipper_format_write_hex(
  138. file, TOTP_CONFIG_KEY_TOKEN_SECRET, token_info->token, token_info->token_length);
  139. flipper_format_write_string_cstr(
  140. file, TOTP_CONFIG_KEY_TOKEN_ALGO, token_info_get_algo_as_cstr(token_info));
  141. uint32_t digits_count_as_uint32 = token_info_get_digits_as_int(token_info);
  142. flipper_format_write_uint32(file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &digits_count_as_uint32, 1);
  143. }
  144. void totp_config_file_save_new_token(TokenInfo* token_info) {
  145. Storage* cfg_storage = totp_open_storage();
  146. FlipperFormat* file = totp_open_config_file(cfg_storage);
  147. totp_config_file_save_new_token_i(file, token_info);
  148. totp_close_config_file(file);
  149. totp_close_storage();
  150. }
  151. void totp_config_file_update_timezone_offset(float new_timezone_offset) {
  152. Storage* cfg_storage = totp_open_storage();
  153. FlipperFormat* file = totp_open_config_file(cfg_storage);
  154. flipper_format_insert_or_update_float(file, TOTP_CONFIG_KEY_TIMEZONE, &new_timezone_offset, 1);
  155. totp_close_config_file(file);
  156. totp_close_storage();
  157. }
  158. void totp_full_save_config_file(PluginState* const plugin_state) {
  159. Storage* storage = totp_open_storage();
  160. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  161. flipper_format_file_open_always(fff_data_file, CONFIG_FILE_PATH);
  162. flipper_format_write_header_cstr(
  163. fff_data_file, CONFIG_FILE_HEADER, CONFIG_FILE_ACTUAL_VERSION);
  164. flipper_format_write_hex(
  165. fff_data_file, TOTP_CONFIG_KEY_BASE_IV, &plugin_state->base_iv[0], TOTP_IV_SIZE);
  166. flipper_format_write_hex(
  167. fff_data_file,
  168. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  169. plugin_state->crypto_verify_data,
  170. plugin_state->crypto_verify_data_length);
  171. flipper_format_write_float(
  172. fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1);
  173. flipper_format_write_bool(fff_data_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1);
  174. ListNode* node = plugin_state->tokens_list;
  175. while(node != NULL) {
  176. TokenInfo* token_info = node->data;
  177. totp_config_file_save_new_token_i(fff_data_file, token_info);
  178. node = node->next;
  179. }
  180. totp_close_config_file(fff_data_file);
  181. totp_close_storage();
  182. }
  183. void totp_config_file_load_base(PluginState* const plugin_state) {
  184. Storage* storage = totp_open_storage();
  185. FlipperFormat* fff_data_file = totp_open_config_file(storage);
  186. plugin_state->timezone_offset = 0;
  187. FuriString* temp_str = furi_string_alloc();
  188. uint32_t file_version;
  189. if(!flipper_format_read_header(fff_data_file, temp_str, &file_version)) {
  190. FURI_LOG_E(LOGGING_TAG, "Missing or incorrect header");
  191. furi_string_free(temp_str);
  192. return;
  193. }
  194. if(file_version < CONFIG_FILE_ACTUAL_VERSION) {
  195. FURI_LOG_I(
  196. LOGGING_TAG,
  197. "Obsolete config file version detected. Current version: %" PRIu32
  198. "; Actual version: %" PRId16,
  199. file_version,
  200. CONFIG_FILE_ACTUAL_VERSION);
  201. totp_close_config_file(fff_data_file);
  202. if(storage_common_stat(storage, CONFIG_FILE_BACKUP_PATH, NULL) == FSE_OK) {
  203. storage_simply_remove(storage, CONFIG_FILE_BACKUP_PATH);
  204. }
  205. if(storage_common_copy(storage, CONFIG_FILE_PATH, CONFIG_FILE_BACKUP_PATH) == FSE_OK) {
  206. FURI_LOG_I(LOGGING_TAG, "Took config file backup to %s", CONFIG_FILE_BACKUP_PATH);
  207. fff_data_file = totp_open_config_file(storage);
  208. FlipperFormat* fff_backup_data_file = flipper_format_file_alloc(storage);
  209. flipper_format_file_open_existing(fff_backup_data_file, CONFIG_FILE_BACKUP_PATH);
  210. if(file_version == 1) {
  211. if(totp_config_migrate_v1_to_v2(fff_data_file, fff_backup_data_file)) {
  212. FURI_LOG_I(LOGGING_TAG, "Applied migration from v1 to v2");
  213. } else {
  214. FURI_LOG_W(LOGGING_TAG, "An error occurred during migration from v1 to v2");
  215. }
  216. }
  217. flipper_format_file_close(fff_backup_data_file);
  218. flipper_format_free(fff_backup_data_file);
  219. flipper_format_rewind(fff_data_file);
  220. } else {
  221. FURI_LOG_E(
  222. LOGGING_TAG,
  223. "An error occurred during taking backup of %s into %s before migration",
  224. CONFIG_FILE_PATH,
  225. CONFIG_FILE_BACKUP_PATH);
  226. }
  227. }
  228. if(!flipper_format_read_hex(
  229. fff_data_file, TOTP_CONFIG_KEY_BASE_IV, &plugin_state->base_iv[0], TOTP_IV_SIZE)) {
  230. FURI_LOG_D(LOGGING_TAG, "Missing base IV");
  231. }
  232. flipper_format_rewind(fff_data_file);
  233. uint32_t crypto_size;
  234. if(flipper_format_get_value_count(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, &crypto_size) &&
  235. crypto_size > 0) {
  236. plugin_state->crypto_verify_data = malloc(sizeof(uint8_t) * crypto_size);
  237. plugin_state->crypto_verify_data_length = crypto_size;
  238. if(!flipper_format_read_hex(
  239. fff_data_file,
  240. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  241. plugin_state->crypto_verify_data,
  242. crypto_size)) {
  243. FURI_LOG_D(LOGGING_TAG, "Missing crypto verify token");
  244. free(plugin_state->crypto_verify_data);
  245. plugin_state->crypto_verify_data = NULL;
  246. plugin_state->crypto_verify_data_length = 0;
  247. }
  248. } else {
  249. plugin_state->crypto_verify_data = NULL;
  250. plugin_state->crypto_verify_data_length = 0;
  251. }
  252. flipper_format_rewind(fff_data_file);
  253. if(!flipper_format_read_float(
  254. fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  255. plugin_state->timezone_offset = 0;
  256. FURI_LOG_D(LOGGING_TAG, "Missing timezone offset information, defaulting to 0");
  257. }
  258. flipper_format_rewind(fff_data_file);
  259. if(!flipper_format_read_bool(
  260. fff_data_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1)) {
  261. plugin_state->pin_set = true;
  262. }
  263. furi_string_free(temp_str);
  264. totp_close_config_file(fff_data_file);
  265. totp_close_storage();
  266. }
  267. void totp_config_file_load_tokens(PluginState* const plugin_state) {
  268. Storage* storage = totp_open_storage();
  269. FlipperFormat* fff_data_file = totp_open_config_file(storage);
  270. FuriString* temp_str = furi_string_alloc();
  271. uint32_t temp_data32;
  272. if(!flipper_format_read_header(fff_data_file, temp_str, &temp_data32)) {
  273. FURI_LOG_E(LOGGING_TAG, "Missing or incorrect header");
  274. furi_string_free(temp_str);
  275. return;
  276. }
  277. uint8_t index = 0;
  278. bool has_any_plain_secret = false;
  279. while(true) {
  280. if(!flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_NAME, temp_str)) {
  281. break;
  282. }
  283. TokenInfo* tokenInfo = token_info_alloc();
  284. const char* temp_cstr = furi_string_get_cstr(temp_str);
  285. tokenInfo->name = (char*)malloc(strlen(temp_cstr) + 1);
  286. strcpy(tokenInfo->name, temp_cstr);
  287. uint32_t secret_bytes_count;
  288. if(!flipper_format_get_value_count(
  289. fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, &secret_bytes_count)) {
  290. token_info_free(tokenInfo);
  291. continue;
  292. }
  293. if(secret_bytes_count == 1) { // Plain secret key
  294. if(!flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str)) {
  295. token_info_free(tokenInfo);
  296. continue;
  297. }
  298. temp_cstr = furi_string_get_cstr(temp_str);
  299. token_info_set_secret(tokenInfo, temp_cstr, strlen(temp_cstr), &plugin_state->iv[0]);
  300. has_any_plain_secret = true;
  301. FURI_LOG_W(LOGGING_TAG, "Found token with plain secret");
  302. } else { // encrypted
  303. tokenInfo->token_length = secret_bytes_count;
  304. tokenInfo->token = malloc(tokenInfo->token_length);
  305. if(!flipper_format_read_hex(
  306. fff_data_file,
  307. TOTP_CONFIG_KEY_TOKEN_SECRET,
  308. tokenInfo->token,
  309. tokenInfo->token_length)) {
  310. token_info_free(tokenInfo);
  311. continue;
  312. }
  313. }
  314. if(!flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str)) {
  315. token_info_free(tokenInfo);
  316. continue;
  317. }
  318. token_info_set_algo_from_str(tokenInfo, temp_str);
  319. if(!flipper_format_read_uint32(
  320. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &temp_data32, 1)) {
  321. token_info_free(tokenInfo);
  322. continue;
  323. }
  324. token_info_set_digits_from_int(tokenInfo, temp_data32);
  325. FURI_LOG_D(LOGGING_TAG, "Found token \"%s\"", tokenInfo->name);
  326. if(plugin_state->tokens_list == NULL) {
  327. plugin_state->tokens_list = list_init_head(tokenInfo);
  328. } else {
  329. list_add(plugin_state->tokens_list, tokenInfo);
  330. }
  331. index++;
  332. }
  333. plugin_state->tokens_count = index;
  334. plugin_state->token_list_loaded = true;
  335. FURI_LOG_D(LOGGING_TAG, "Found %" PRIu8 " tokens", index);
  336. furi_string_free(temp_str);
  337. totp_close_config_file(fff_data_file);
  338. totp_close_storage();
  339. if(has_any_plain_secret) {
  340. totp_full_save_config_file(plugin_state);
  341. }
  342. }
  343. void totp_close_config_file(FlipperFormat* file) {
  344. if(file == NULL) return;
  345. flipper_format_file_close(file);
  346. flipper_format_free(file);
  347. }