config.c 17 KB

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