config.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. static 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. static 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. static 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. static 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) == 0) {
  45. token_info->algo = SHA256;
  46. } else if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME) == 0) {
  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. bool token_is_valid = token_info->token != NULL && token_info->token_length > 0;
  138. if(!token_is_valid) {
  139. flipper_format_write_comment_cstr(file, "!!! WARNING BEGIN: INVALID TOKEN !!!");
  140. }
  141. flipper_format_write_hex(
  142. file, TOTP_CONFIG_KEY_TOKEN_SECRET, token_info->token, token_info->token_length);
  143. if(!token_is_valid) {
  144. flipper_format_write_comment_cstr(file, "!!! WARNING END !!!");
  145. }
  146. flipper_format_write_string_cstr(
  147. file, TOTP_CONFIG_KEY_TOKEN_ALGO, token_info_get_algo_as_cstr(token_info));
  148. uint32_t digits_count_as_uint32 = token_info_get_digits_as_int(token_info);
  149. flipper_format_write_uint32(file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &digits_count_as_uint32, 1);
  150. }
  151. void totp_config_file_save_new_token(TokenInfo* token_info) {
  152. Storage* cfg_storage = totp_open_storage();
  153. FlipperFormat* file = totp_open_config_file(cfg_storage);
  154. totp_config_file_save_new_token_i(file, token_info);
  155. totp_close_config_file(file);
  156. totp_close_storage();
  157. }
  158. void totp_config_file_update_timezone_offset(float new_timezone_offset) {
  159. Storage* cfg_storage = totp_open_storage();
  160. FlipperFormat* file = totp_open_config_file(cfg_storage);
  161. flipper_format_insert_or_update_float(file, TOTP_CONFIG_KEY_TIMEZONE, &new_timezone_offset, 1);
  162. totp_close_config_file(file);
  163. totp_close_storage();
  164. }
  165. void totp_full_save_config_file(PluginState* const plugin_state) {
  166. Storage* storage = totp_open_storage();
  167. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  168. flipper_format_file_open_always(fff_data_file, CONFIG_FILE_PATH);
  169. flipper_format_write_header_cstr(
  170. fff_data_file, CONFIG_FILE_HEADER, CONFIG_FILE_ACTUAL_VERSION);
  171. flipper_format_write_hex(
  172. fff_data_file, TOTP_CONFIG_KEY_BASE_IV, &plugin_state->base_iv[0], TOTP_IV_SIZE);
  173. flipper_format_write_hex(
  174. fff_data_file,
  175. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  176. plugin_state->crypto_verify_data,
  177. plugin_state->crypto_verify_data_length);
  178. flipper_format_write_float(
  179. fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1);
  180. flipper_format_write_bool(fff_data_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1);
  181. ListNode* node = plugin_state->tokens_list;
  182. while(node != NULL) {
  183. TokenInfo* token_info = node->data;
  184. totp_config_file_save_new_token_i(fff_data_file, token_info);
  185. node = node->next;
  186. }
  187. totp_close_config_file(fff_data_file);
  188. totp_close_storage();
  189. }
  190. void totp_config_file_load_base(PluginState* const plugin_state) {
  191. Storage* storage = totp_open_storage();
  192. FlipperFormat* fff_data_file = totp_open_config_file(storage);
  193. plugin_state->timezone_offset = 0;
  194. FuriString* temp_str = furi_string_alloc();
  195. uint32_t file_version;
  196. if(!flipper_format_read_header(fff_data_file, temp_str, &file_version)) {
  197. FURI_LOG_E(LOGGING_TAG, "Missing or incorrect header");
  198. furi_string_free(temp_str);
  199. return;
  200. }
  201. if(file_version < CONFIG_FILE_ACTUAL_VERSION) {
  202. FURI_LOG_I(
  203. LOGGING_TAG,
  204. "Obsolete config file version detected. Current version: %" PRIu32
  205. "; Actual version: %" PRId16,
  206. file_version,
  207. CONFIG_FILE_ACTUAL_VERSION);
  208. totp_close_config_file(fff_data_file);
  209. if(storage_common_stat(storage, CONFIG_FILE_BACKUP_PATH, NULL) == FSE_OK) {
  210. storage_simply_remove(storage, CONFIG_FILE_BACKUP_PATH);
  211. }
  212. if(storage_common_copy(storage, CONFIG_FILE_PATH, CONFIG_FILE_BACKUP_PATH) == FSE_OK) {
  213. FURI_LOG_I(LOGGING_TAG, "Took config file backup to %s", CONFIG_FILE_BACKUP_PATH);
  214. fff_data_file = totp_open_config_file(storage);
  215. FlipperFormat* fff_backup_data_file = flipper_format_file_alloc(storage);
  216. flipper_format_file_open_existing(fff_backup_data_file, CONFIG_FILE_BACKUP_PATH);
  217. if(file_version == 1) {
  218. if(totp_config_migrate_v1_to_v2(fff_data_file, fff_backup_data_file)) {
  219. FURI_LOG_I(LOGGING_TAG, "Applied migration from v1 to v2");
  220. } else {
  221. FURI_LOG_W(LOGGING_TAG, "An error occurred during migration from v1 to v2");
  222. }
  223. }
  224. flipper_format_file_close(fff_backup_data_file);
  225. flipper_format_free(fff_backup_data_file);
  226. flipper_format_rewind(fff_data_file);
  227. } else {
  228. FURI_LOG_E(
  229. LOGGING_TAG,
  230. "An error occurred during taking backup of %s into %s before migration",
  231. CONFIG_FILE_PATH,
  232. CONFIG_FILE_BACKUP_PATH);
  233. }
  234. }
  235. if(!flipper_format_read_hex(
  236. fff_data_file, TOTP_CONFIG_KEY_BASE_IV, &plugin_state->base_iv[0], TOTP_IV_SIZE)) {
  237. FURI_LOG_D(LOGGING_TAG, "Missing base IV");
  238. }
  239. flipper_format_rewind(fff_data_file);
  240. uint32_t crypto_size;
  241. if(flipper_format_get_value_count(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, &crypto_size) &&
  242. crypto_size > 0) {
  243. plugin_state->crypto_verify_data = malloc(sizeof(uint8_t) * crypto_size);
  244. plugin_state->crypto_verify_data_length = crypto_size;
  245. if(!flipper_format_read_hex(
  246. fff_data_file,
  247. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  248. plugin_state->crypto_verify_data,
  249. crypto_size)) {
  250. FURI_LOG_D(LOGGING_TAG, "Missing crypto verify token");
  251. free(plugin_state->crypto_verify_data);
  252. plugin_state->crypto_verify_data = NULL;
  253. plugin_state->crypto_verify_data_length = 0;
  254. }
  255. } else {
  256. plugin_state->crypto_verify_data = NULL;
  257. plugin_state->crypto_verify_data_length = 0;
  258. }
  259. flipper_format_rewind(fff_data_file);
  260. if(!flipper_format_read_float(
  261. fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  262. plugin_state->timezone_offset = 0;
  263. FURI_LOG_D(LOGGING_TAG, "Missing timezone offset information, defaulting to 0");
  264. }
  265. flipper_format_rewind(fff_data_file);
  266. if(!flipper_format_read_bool(
  267. fff_data_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1)) {
  268. plugin_state->pin_set = true;
  269. }
  270. furi_string_free(temp_str);
  271. totp_close_config_file(fff_data_file);
  272. totp_close_storage();
  273. }
  274. TokenLoadingResult totp_config_file_load_tokens(PluginState* const plugin_state) {
  275. Storage* storage = totp_open_storage();
  276. FlipperFormat* fff_data_file = totp_open_config_file(storage);
  277. FuriString* temp_str = furi_string_alloc();
  278. uint32_t temp_data32;
  279. if(!flipper_format_read_header(fff_data_file, temp_str, &temp_data32)) {
  280. FURI_LOG_E(LOGGING_TAG, "Missing or incorrect header");
  281. furi_string_free(temp_str);
  282. return TokenLoadingResultError;
  283. }
  284. TokenLoadingResult result = TokenLoadingResultSuccess;
  285. uint8_t index = 0;
  286. bool has_any_plain_secret = false;
  287. while(true) {
  288. if(!flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_NAME, temp_str)) {
  289. break;
  290. }
  291. TokenInfo* tokenInfo = token_info_alloc();
  292. const char* temp_cstr = furi_string_get_cstr(temp_str);
  293. tokenInfo->name = (char*)malloc(strlen(temp_cstr) + 1);
  294. strcpy(tokenInfo->name, temp_cstr);
  295. uint32_t secret_bytes_count;
  296. if(!flipper_format_get_value_count(
  297. fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, &secret_bytes_count)) {
  298. secret_bytes_count = 0;
  299. }
  300. if(secret_bytes_count == 1) { // Plain secret key
  301. if(flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str)) {
  302. temp_cstr = furi_string_get_cstr(temp_str);
  303. if(token_info_set_secret(
  304. tokenInfo, temp_cstr, strlen(temp_cstr), &plugin_state->iv[0])) {
  305. FURI_LOG_W(LOGGING_TAG, "Token \"%s\" has plain secret", tokenInfo->name);
  306. } else {
  307. tokenInfo->token = NULL;
  308. tokenInfo->token_length = 0;
  309. FURI_LOG_W(LOGGING_TAG, "Token \"%s\" has invalid secret", tokenInfo->name);
  310. result = TokenLoadingResultWarning;
  311. }
  312. } else {
  313. tokenInfo->token = NULL;
  314. tokenInfo->token_length = 0;
  315. result = TokenLoadingResultWarning;
  316. }
  317. has_any_plain_secret = true;
  318. } else { // encrypted
  319. tokenInfo->token_length = secret_bytes_count;
  320. if(secret_bytes_count > 0) {
  321. tokenInfo->token = malloc(tokenInfo->token_length);
  322. if(!flipper_format_read_hex(
  323. fff_data_file,
  324. TOTP_CONFIG_KEY_TOKEN_SECRET,
  325. tokenInfo->token,
  326. tokenInfo->token_length)) {
  327. free(tokenInfo->token);
  328. tokenInfo->token = NULL;
  329. tokenInfo->token_length = 0;
  330. result = TokenLoadingResultWarning;
  331. }
  332. } else {
  333. tokenInfo->token = NULL;
  334. result = TokenLoadingResultWarning;
  335. }
  336. }
  337. if(flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str)) {
  338. token_info_set_algo_from_str(tokenInfo, temp_str);
  339. } else {
  340. tokenInfo->algo = SHA1;
  341. }
  342. if(flipper_format_read_uint32(
  343. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &temp_data32, 1)) {
  344. token_info_set_digits_from_int(tokenInfo, temp_data32);
  345. } else {
  346. tokenInfo->digits = TOTP_6_DIGITS;
  347. }
  348. FURI_LOG_D(LOGGING_TAG, "Found token \"%s\"", tokenInfo->name);
  349. if(plugin_state->tokens_list == NULL) {
  350. plugin_state->tokens_list = list_init_head(tokenInfo);
  351. } else {
  352. list_add(plugin_state->tokens_list, tokenInfo);
  353. }
  354. index++;
  355. }
  356. plugin_state->tokens_count = index;
  357. plugin_state->token_list_loaded = true;
  358. FURI_LOG_D(LOGGING_TAG, "Found %" PRIu8 " tokens", index);
  359. furi_string_free(temp_str);
  360. totp_close_config_file(fff_data_file);
  361. totp_close_storage();
  362. if(has_any_plain_secret) {
  363. totp_full_save_config_file(plugin_state);
  364. }
  365. return result;
  366. }
  367. void totp_close_config_file(FlipperFormat* file) {
  368. if(file == NULL) return;
  369. flipper_format_file_close(file);
  370. flipper_format_free(file);
  371. }