config.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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_PATH("authenticator")
  9. #define CONFIG_FILE_PATH CONFIG_FILE_DIRECTORY_PATH "/totp.conf"
  10. #define CONFIG_FILE_BACKUP_PATH CONFIG_FILE_PATH ".backup"
  11. #define CONFIG_FILE_TEMP_PATH CONFIG_FILE_PATH ".tmp"
  12. #define CONFIG_FILE_ORIG_PATH CONFIG_FILE_PATH ".orig"
  13. #define CONFIG_FILE_PATH_PREVIOUS EXT_PATH("apps/Misc") "/totp.conf"
  14. static char* token_info_get_algo_as_cstr(const TokenInfo* token_info) {
  15. switch(token_info->algo) {
  16. case SHA1:
  17. return TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME;
  18. case SHA256:
  19. return TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME;
  20. case SHA512:
  21. return TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME;
  22. default:
  23. break;
  24. }
  25. return NULL;
  26. }
  27. static void token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str) {
  28. if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME) == 0) {
  29. token_info->algo = SHA1;
  30. } else if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME) == 0) {
  31. token_info->algo = SHA256;
  32. } else if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME) == 0) {
  33. token_info->algo = SHA512;
  34. }
  35. }
  36. /**
  37. * @brief Opens storage record
  38. * @return Storage record
  39. */
  40. static Storage* totp_open_storage() {
  41. return furi_record_open(RECORD_STORAGE);
  42. }
  43. /**
  44. * @brief Closes storage record
  45. */
  46. static void totp_close_storage() {
  47. furi_record_close(RECORD_STORAGE);
  48. }
  49. /**
  50. * @brief Closes config file
  51. * @param file config file reference
  52. */
  53. static void totp_close_config_file(FlipperFormat* file) {
  54. if(file == NULL) return;
  55. flipper_format_file_close(file);
  56. flipper_format_free(file);
  57. }
  58. /**
  59. * @brief Opens or creates TOTP application standard config file
  60. * @param storage storage record to use
  61. * @param[out] file opened config file
  62. * @return Config file open result
  63. */
  64. static TotpConfigFileOpenResult totp_open_config_file(Storage* storage, FlipperFormat** file) {
  65. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  66. if(storage_common_stat(storage, CONFIG_FILE_PATH, NULL) == FSE_OK) {
  67. FURI_LOG_D(LOGGING_TAG, "Config file %s found", CONFIG_FILE_PATH);
  68. if(!flipper_format_file_open_existing(fff_data_file, CONFIG_FILE_PATH)) {
  69. FURI_LOG_E(LOGGING_TAG, "Error opening existing file %s", CONFIG_FILE_PATH);
  70. totp_close_config_file(fff_data_file);
  71. return TotpConfigFileOpenError;
  72. }
  73. } else if(storage_common_stat(storage, CONFIG_FILE_PATH_PREVIOUS, NULL) == FSE_OK) {
  74. FURI_LOG_D(LOGGING_TAG, "Old config file %s found", CONFIG_FILE_PATH_PREVIOUS);
  75. if(storage_common_stat(storage, CONFIG_FILE_DIRECTORY_PATH, NULL) == FSE_NOT_EXIST) {
  76. FURI_LOG_D(
  77. LOGGING_TAG,
  78. "Directory %s doesn't exist. Will create new.",
  79. CONFIG_FILE_DIRECTORY_PATH);
  80. if(!storage_simply_mkdir(storage, CONFIG_FILE_DIRECTORY_PATH)) {
  81. FURI_LOG_E(LOGGING_TAG, "Error creating directory %s", CONFIG_FILE_DIRECTORY_PATH);
  82. totp_close_config_file(fff_data_file);
  83. return TotpConfigFileOpenError;
  84. }
  85. }
  86. if(storage_common_rename(storage, CONFIG_FILE_PATH_PREVIOUS, CONFIG_FILE_PATH) != FSE_OK) {
  87. FURI_LOG_E(LOGGING_TAG, "Error moving config to %s", CONFIG_FILE_PATH);
  88. totp_close_config_file(fff_data_file);
  89. return TotpConfigFileOpenError;
  90. }
  91. FURI_LOG_I(LOGGING_TAG, "Applied config file path migration");
  92. return totp_open_config_file(storage, file);
  93. } else {
  94. FURI_LOG_D(LOGGING_TAG, "Config file %s is not found. Will create new.", CONFIG_FILE_PATH);
  95. if(storage_common_stat(storage, CONFIG_FILE_DIRECTORY_PATH, NULL) == FSE_NOT_EXIST) {
  96. FURI_LOG_D(
  97. LOGGING_TAG,
  98. "Directory %s doesn't exist. Will create new.",
  99. CONFIG_FILE_DIRECTORY_PATH);
  100. if(!storage_simply_mkdir(storage, CONFIG_FILE_DIRECTORY_PATH)) {
  101. FURI_LOG_E(LOGGING_TAG, "Error creating directory %s", CONFIG_FILE_DIRECTORY_PATH);
  102. return TotpConfigFileOpenError;
  103. }
  104. }
  105. if(!flipper_format_file_open_new(fff_data_file, CONFIG_FILE_PATH)) {
  106. totp_close_config_file(fff_data_file);
  107. FURI_LOG_E(LOGGING_TAG, "Error creating new file %s", CONFIG_FILE_PATH);
  108. return TotpConfigFileOpenError;
  109. }
  110. flipper_format_write_header_cstr(
  111. fff_data_file, CONFIG_FILE_HEADER, CONFIG_FILE_ACTUAL_VERSION);
  112. float tmp_tz = 0;
  113. flipper_format_write_comment_cstr(fff_data_file, " ");
  114. flipper_format_write_comment_cstr(
  115. fff_data_file,
  116. "Timezone offset in hours. Important note: do not put '+' sign for positive values");
  117. flipper_format_write_float(fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &tmp_tz, 1);
  118. uint32_t tmp_uint32 = NotificationMethodSound | NotificationMethodVibro;
  119. flipper_format_write_comment_cstr(fff_data_file, " ");
  120. flipper_format_write_comment_cstr(
  121. fff_data_file,
  122. "How to notify user when new token is generated or badusb mode is activated (possible values: 0 - do not notify, 1 - sound, 2 - vibro, 3 sound and vibro)");
  123. flipper_format_write_uint32(
  124. fff_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1);
  125. FuriString* temp_str = furi_string_alloc();
  126. flipper_format_write_comment_cstr(fff_data_file, " ");
  127. flipper_format_write_comment_cstr(fff_data_file, "=== TOKEN SAMPLE BEGIN ===");
  128. flipper_format_write_comment_cstr(fff_data_file, " ");
  129. flipper_format_write_comment_cstr(
  130. fff_data_file, "# Token name which will be visible in the UI.");
  131. furi_string_printf(temp_str, "%s: Sample token name", TOTP_CONFIG_KEY_TOKEN_NAME);
  132. flipper_format_write_comment(fff_data_file, temp_str);
  133. flipper_format_write_comment_cstr(fff_data_file, " ");
  134. flipper_format_write_comment_cstr(
  135. fff_data_file,
  136. "# 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");
  137. furi_string_printf(temp_str, "%s: plaintokensecret", TOTP_CONFIG_KEY_TOKEN_SECRET);
  138. flipper_format_write_comment(fff_data_file, temp_str);
  139. flipper_format_write_comment_cstr(fff_data_file, " ");
  140. furi_string_printf(
  141. temp_str,
  142. " # 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",
  143. TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME,
  144. TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME,
  145. TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME,
  146. TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME);
  147. flipper_format_write_comment(fff_data_file, temp_str);
  148. furi_string_printf(
  149. temp_str, "%s: %s", TOTP_CONFIG_KEY_TOKEN_ALGO, TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME);
  150. flipper_format_write_comment(fff_data_file, temp_str);
  151. flipper_format_write_comment_cstr(fff_data_file, " ");
  152. flipper_format_write_comment_cstr(
  153. fff_data_file,
  154. "# 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");
  155. furi_string_printf(temp_str, "%s: 6", TOTP_CONFIG_KEY_TOKEN_DIGITS);
  156. flipper_format_write_comment(fff_data_file, temp_str);
  157. flipper_format_write_comment_cstr(fff_data_file, " ");
  158. flipper_format_write_comment_cstr(fff_data_file, "=== TOKEN SAMPLE END ===");
  159. flipper_format_write_comment_cstr(fff_data_file, " ");
  160. furi_string_free(temp_str);
  161. if(!flipper_format_rewind(fff_data_file)) {
  162. totp_close_config_file(fff_data_file);
  163. FURI_LOG_E(LOGGING_TAG, "Rewind error");
  164. return TotpConfigFileOpenError;
  165. }
  166. }
  167. *file = fff_data_file;
  168. return TotpConfigFileOpenSuccess;
  169. }
  170. TotpConfigFileUpdateResult
  171. totp_config_file_save_new_token_i(FlipperFormat* file, const TokenInfo* token_info) {
  172. TotpConfigFileUpdateResult update_result;
  173. do {
  174. if(!flipper_format_seek_to_end(file)) {
  175. update_result = TotpConfigFileUpdateError;
  176. break;
  177. }
  178. if(!flipper_format_write_string_cstr(file, TOTP_CONFIG_KEY_TOKEN_NAME, token_info->name)) {
  179. update_result = TotpConfigFileUpdateError;
  180. break;
  181. }
  182. bool token_is_valid = token_info->token != NULL && token_info->token_length > 0;
  183. if(!token_is_valid &&
  184. !flipper_format_write_comment_cstr(file, "!!! WARNING BEGIN: INVALID TOKEN !!!")) {
  185. update_result = TotpConfigFileUpdateError;
  186. break;
  187. }
  188. if(!flipper_format_write_hex(
  189. file, TOTP_CONFIG_KEY_TOKEN_SECRET, token_info->token, token_info->token_length)) {
  190. update_result = TotpConfigFileUpdateError;
  191. break;
  192. }
  193. if(!token_is_valid && !flipper_format_write_comment_cstr(file, "!!! WARNING END !!!")) {
  194. update_result = TotpConfigFileUpdateError;
  195. break;
  196. }
  197. if(!flipper_format_write_string_cstr(
  198. file, TOTP_CONFIG_KEY_TOKEN_ALGO, token_info_get_algo_as_cstr(token_info))) {
  199. update_result = TotpConfigFileUpdateError;
  200. break;
  201. }
  202. uint32_t tmp_uint32 = token_info->digits;
  203. if(!flipper_format_write_uint32(file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &tmp_uint32, 1)) {
  204. update_result = TotpConfigFileUpdateError;
  205. break;
  206. }
  207. update_result = TotpConfigFileUpdateSuccess;
  208. } while(false);
  209. return update_result;
  210. }
  211. TotpConfigFileUpdateResult totp_config_file_save_new_token(const TokenInfo* token_info) {
  212. Storage* cfg_storage = totp_open_storage();
  213. FlipperFormat* file;
  214. TotpConfigFileUpdateResult update_result;
  215. if(totp_open_config_file(cfg_storage, &file) == TotpConfigFileOpenSuccess) {
  216. do {
  217. if(totp_config_file_save_new_token_i(file, token_info) !=
  218. TotpConfigFileUpdateSuccess) {
  219. update_result = TotpConfigFileUpdateError;
  220. break;
  221. }
  222. update_result = TotpConfigFileUpdateSuccess;
  223. } while(false);
  224. totp_close_config_file(file);
  225. } else {
  226. update_result = TotpConfigFileUpdateError;
  227. }
  228. totp_close_storage();
  229. return update_result;
  230. }
  231. TotpConfigFileUpdateResult totp_config_file_update_timezone_offset(float new_timezone_offset) {
  232. Storage* cfg_storage = totp_open_storage();
  233. FlipperFormat* file;
  234. TotpConfigFileUpdateResult update_result;
  235. if(totp_open_config_file(cfg_storage, &file) == TotpConfigFileOpenSuccess) {
  236. do {
  237. if(!flipper_format_insert_or_update_float(
  238. file, TOTP_CONFIG_KEY_TIMEZONE, &new_timezone_offset, 1)) {
  239. update_result = TotpConfigFileUpdateError;
  240. break;
  241. }
  242. update_result = TotpConfigFileUpdateSuccess;
  243. } while(false);
  244. totp_close_config_file(file);
  245. } else {
  246. update_result = TotpConfigFileUpdateError;
  247. }
  248. totp_close_storage();
  249. return update_result;
  250. }
  251. TotpConfigFileUpdateResult
  252. totp_config_file_update_notification_method(NotificationMethod new_notification_method) {
  253. Storage* cfg_storage = totp_open_storage();
  254. FlipperFormat* file;
  255. TotpConfigFileUpdateResult update_result;
  256. if(totp_open_config_file(cfg_storage, &file) == TotpConfigFileOpenSuccess) {
  257. do {
  258. uint32_t tmp_uint32 = new_notification_method;
  259. if(!flipper_format_insert_or_update_uint32(
  260. file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  261. update_result = TotpConfigFileUpdateError;
  262. break;
  263. }
  264. update_result = TotpConfigFileUpdateSuccess;
  265. } while(false);
  266. totp_close_config_file(file);
  267. } else {
  268. update_result = TotpConfigFileUpdateError;
  269. }
  270. totp_close_storage();
  271. return update_result;
  272. }
  273. TotpConfigFileUpdateResult totp_config_file_update_user_settings(const PluginState* plugin_state) {
  274. Storage* cfg_storage = totp_open_storage();
  275. FlipperFormat* file;
  276. TotpConfigFileUpdateResult update_result;
  277. if(totp_open_config_file(cfg_storage, &file) == TotpConfigFileOpenSuccess) {
  278. do {
  279. if(!flipper_format_insert_or_update_float(
  280. file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  281. update_result = TotpConfigFileUpdateError;
  282. break;
  283. }
  284. uint32_t tmp_uint32 = plugin_state->notification_method;
  285. if(!flipper_format_insert_or_update_uint32(
  286. file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  287. update_result = TotpConfigFileUpdateError;
  288. break;
  289. }
  290. update_result = TotpConfigFileUpdateSuccess;
  291. } while(false);
  292. totp_close_config_file(file);
  293. } else {
  294. update_result = TotpConfigFileUpdateError;
  295. }
  296. totp_close_storage();
  297. return update_result;
  298. }
  299. TotpConfigFileUpdateResult totp_full_save_config_file(const PluginState* const plugin_state) {
  300. Storage* storage = totp_open_storage();
  301. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  302. TotpConfigFileUpdateResult result = TotpConfigFileUpdateSuccess;
  303. do {
  304. if(!flipper_format_file_open_always(fff_data_file, CONFIG_FILE_TEMP_PATH)) {
  305. result = TotpConfigFileUpdateError;
  306. break;
  307. }
  308. if(!flipper_format_write_header_cstr(
  309. fff_data_file, CONFIG_FILE_HEADER, CONFIG_FILE_ACTUAL_VERSION)) {
  310. result = TotpConfigFileUpdateError;
  311. break;
  312. }
  313. if(!flipper_format_write_hex(
  314. fff_data_file, TOTP_CONFIG_KEY_BASE_IV, &plugin_state->base_iv[0], TOTP_IV_SIZE)) {
  315. result = TotpConfigFileUpdateError;
  316. break;
  317. }
  318. if(!flipper_format_write_hex(
  319. fff_data_file,
  320. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  321. plugin_state->crypto_verify_data,
  322. plugin_state->crypto_verify_data_length)) {
  323. result = TotpConfigFileUpdateError;
  324. break;
  325. }
  326. if(!flipper_format_write_float(
  327. fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  328. result = TotpConfigFileUpdateError;
  329. break;
  330. }
  331. if(!flipper_format_write_bool(
  332. fff_data_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1)) {
  333. result = TotpConfigFileUpdateError;
  334. break;
  335. }
  336. uint32_t tmp_uint32 = plugin_state->notification_method;
  337. if(!flipper_format_write_uint32(
  338. fff_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  339. result = TotpConfigFileUpdateError;
  340. break;
  341. }
  342. bool tokens_written = true;
  343. TOTP_LIST_FOREACH(plugin_state->tokens_list, node, {
  344. const TokenInfo* token_info = node->data;
  345. tokens_written = tokens_written &&
  346. totp_config_file_save_new_token_i(fff_data_file, token_info) ==
  347. TotpConfigFileUpdateSuccess;
  348. });
  349. if(!tokens_written) {
  350. result = TotpConfigFileUpdateError;
  351. break;
  352. }
  353. } while(false);
  354. totp_close_config_file(fff_data_file);
  355. if(result == TotpConfigFileUpdateSuccess) {
  356. if(storage_file_exists(storage, CONFIG_FILE_ORIG_PATH)) {
  357. storage_simply_remove(storage, CONFIG_FILE_ORIG_PATH);
  358. }
  359. if(storage_common_rename(storage, CONFIG_FILE_PATH, CONFIG_FILE_ORIG_PATH) != FSE_OK) {
  360. result = TotpConfigFileUpdateError;
  361. } else if(storage_common_rename(storage, CONFIG_FILE_TEMP_PATH, CONFIG_FILE_PATH) != FSE_OK) {
  362. result = TotpConfigFileUpdateError;
  363. } else if(!storage_simply_remove(storage, CONFIG_FILE_ORIG_PATH)) {
  364. result = TotpConfigFileUpdateError;
  365. }
  366. }
  367. totp_close_storage();
  368. return result;
  369. }
  370. TotpConfigFileOpenResult totp_config_file_load_base(PluginState* const plugin_state) {
  371. Storage* storage = totp_open_storage();
  372. FlipperFormat* fff_data_file;
  373. TotpConfigFileOpenResult result;
  374. if((result = totp_open_config_file(storage, &fff_data_file)) != TotpConfigFileOpenSuccess) {
  375. totp_close_storage();
  376. return result;
  377. }
  378. plugin_state->timezone_offset = 0;
  379. FuriString* temp_str = furi_string_alloc();
  380. do {
  381. uint32_t file_version;
  382. if(!flipper_format_read_header(fff_data_file, temp_str, &file_version)) {
  383. FURI_LOG_E(LOGGING_TAG, "Missing or incorrect header");
  384. result = TotpConfigFileOpenError;
  385. break;
  386. }
  387. if(file_version < CONFIG_FILE_ACTUAL_VERSION) {
  388. FURI_LOG_I(
  389. LOGGING_TAG,
  390. "Obsolete config file version detected. Current version: %" PRIu32
  391. "; Actual version: %" PRId16,
  392. file_version,
  393. CONFIG_FILE_ACTUAL_VERSION);
  394. totp_close_config_file(fff_data_file);
  395. if(storage_common_stat(storage, CONFIG_FILE_BACKUP_PATH, NULL) == FSE_OK) {
  396. storage_simply_remove(storage, CONFIG_FILE_BACKUP_PATH);
  397. }
  398. if(storage_common_copy(storage, CONFIG_FILE_PATH, CONFIG_FILE_BACKUP_PATH) == FSE_OK) {
  399. FURI_LOG_I(LOGGING_TAG, "Took config file backup to %s", CONFIG_FILE_BACKUP_PATH);
  400. if(totp_open_config_file(storage, &fff_data_file) != TotpConfigFileOpenSuccess) {
  401. result = TotpConfigFileOpenError;
  402. break;
  403. }
  404. FlipperFormat* fff_backup_data_file = flipper_format_file_alloc(storage);
  405. if(!flipper_format_file_open_existing(
  406. fff_backup_data_file, CONFIG_FILE_BACKUP_PATH)) {
  407. flipper_format_file_close(fff_backup_data_file);
  408. flipper_format_free(fff_backup_data_file);
  409. result = TotpConfigFileOpenError;
  410. break;
  411. }
  412. if(file_version == 1) {
  413. if(totp_config_migrate_v1_to_v2(fff_data_file, fff_backup_data_file)) {
  414. FURI_LOG_I(LOGGING_TAG, "Applied migration from v1 to v2");
  415. } else {
  416. FURI_LOG_W(
  417. LOGGING_TAG, "An error occurred during migration from v1 to v2");
  418. result = TotpConfigFileOpenError;
  419. break;
  420. }
  421. }
  422. flipper_format_file_close(fff_backup_data_file);
  423. flipper_format_free(fff_backup_data_file);
  424. flipper_format_rewind(fff_data_file);
  425. } else {
  426. FURI_LOG_E(
  427. LOGGING_TAG,
  428. "An error occurred during taking backup of %s into %s before migration",
  429. CONFIG_FILE_PATH,
  430. CONFIG_FILE_BACKUP_PATH);
  431. result = TotpConfigFileOpenError;
  432. break;
  433. }
  434. }
  435. if(!flipper_format_read_hex(
  436. fff_data_file, TOTP_CONFIG_KEY_BASE_IV, &plugin_state->base_iv[0], TOTP_IV_SIZE)) {
  437. FURI_LOG_D(LOGGING_TAG, "Missing base IV");
  438. }
  439. if(!flipper_format_rewind(fff_data_file)) {
  440. result = TotpConfigFileOpenError;
  441. break;
  442. }
  443. uint32_t crypto_size;
  444. if(flipper_format_get_value_count(
  445. fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, &crypto_size) &&
  446. crypto_size > 0) {
  447. plugin_state->crypto_verify_data = malloc(sizeof(uint8_t) * crypto_size);
  448. furi_check(plugin_state->crypto_verify_data != NULL);
  449. plugin_state->crypto_verify_data_length = crypto_size;
  450. if(!flipper_format_read_hex(
  451. fff_data_file,
  452. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  453. plugin_state->crypto_verify_data,
  454. crypto_size)) {
  455. FURI_LOG_D(LOGGING_TAG, "Missing crypto verify token");
  456. free(plugin_state->crypto_verify_data);
  457. plugin_state->crypto_verify_data = NULL;
  458. plugin_state->crypto_verify_data_length = 0;
  459. }
  460. } else {
  461. plugin_state->crypto_verify_data = NULL;
  462. plugin_state->crypto_verify_data_length = 0;
  463. }
  464. if(!flipper_format_rewind(fff_data_file)) {
  465. result = TotpConfigFileOpenError;
  466. break;
  467. }
  468. if(!flipper_format_read_float(
  469. fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  470. plugin_state->timezone_offset = 0;
  471. FURI_LOG_D(LOGGING_TAG, "Missing timezone offset information, defaulting to 0");
  472. }
  473. if(!flipper_format_rewind(fff_data_file)) {
  474. result = TotpConfigFileOpenError;
  475. break;
  476. }
  477. if(!flipper_format_read_bool(
  478. fff_data_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1)) {
  479. plugin_state->pin_set = true;
  480. }
  481. flipper_format_rewind(fff_data_file);
  482. uint32_t tmp_uint32;
  483. if(!flipper_format_read_uint32(
  484. fff_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  485. tmp_uint32 = NotificationMethodSound | NotificationMethodVibro;
  486. }
  487. plugin_state->notification_method = tmp_uint32;
  488. } while(false);
  489. furi_string_free(temp_str);
  490. totp_close_config_file(fff_data_file);
  491. totp_close_storage();
  492. return result;
  493. }
  494. TokenLoadingResult totp_config_file_load_tokens(PluginState* const plugin_state) {
  495. Storage* storage = totp_open_storage();
  496. FlipperFormat* fff_data_file;
  497. if(totp_open_config_file(storage, &fff_data_file) != TotpConfigFileOpenSuccess) {
  498. totp_close_storage();
  499. return TokenLoadingResultError;
  500. }
  501. FuriString* temp_str = furi_string_alloc();
  502. uint32_t temp_data32;
  503. if(!flipper_format_read_header(fff_data_file, temp_str, &temp_data32)) {
  504. FURI_LOG_E(LOGGING_TAG, "Missing or incorrect header");
  505. totp_close_storage();
  506. furi_string_free(temp_str);
  507. return TokenLoadingResultError;
  508. }
  509. TokenLoadingResult result = TokenLoadingResultSuccess;
  510. uint16_t index = 0;
  511. bool has_any_plain_secret = false;
  512. while(true) {
  513. if(!flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_NAME, temp_str)) {
  514. break;
  515. }
  516. TokenInfo* tokenInfo = token_info_alloc();
  517. size_t temp_cstr_len = furi_string_size(temp_str);
  518. tokenInfo->name = malloc(temp_cstr_len + 1);
  519. furi_check(tokenInfo->name != NULL);
  520. strlcpy(tokenInfo->name, furi_string_get_cstr(temp_str), temp_cstr_len + 1);
  521. uint32_t secret_bytes_count;
  522. if(!flipper_format_get_value_count(
  523. fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, &secret_bytes_count)) {
  524. secret_bytes_count = 0;
  525. }
  526. if(secret_bytes_count == 1) { // Plain secret key
  527. if(flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str)) {
  528. if(token_info_set_secret(
  529. tokenInfo,
  530. furi_string_get_cstr(temp_str),
  531. furi_string_size(temp_str),
  532. &plugin_state->iv[0])) {
  533. FURI_LOG_W(LOGGING_TAG, "Token \"%s\" has plain secret", tokenInfo->name);
  534. } else {
  535. tokenInfo->token = NULL;
  536. tokenInfo->token_length = 0;
  537. FURI_LOG_W(LOGGING_TAG, "Token \"%s\" has invalid secret", tokenInfo->name);
  538. result = TokenLoadingResultWarning;
  539. }
  540. } else {
  541. tokenInfo->token = NULL;
  542. tokenInfo->token_length = 0;
  543. result = TokenLoadingResultWarning;
  544. }
  545. has_any_plain_secret = true;
  546. } else { // encrypted
  547. tokenInfo->token_length = secret_bytes_count;
  548. if(secret_bytes_count > 0) {
  549. tokenInfo->token = malloc(tokenInfo->token_length);
  550. furi_check(tokenInfo->token != NULL);
  551. if(!flipper_format_read_hex(
  552. fff_data_file,
  553. TOTP_CONFIG_KEY_TOKEN_SECRET,
  554. tokenInfo->token,
  555. tokenInfo->token_length)) {
  556. free(tokenInfo->token);
  557. tokenInfo->token = NULL;
  558. tokenInfo->token_length = 0;
  559. result = TokenLoadingResultWarning;
  560. }
  561. } else {
  562. tokenInfo->token = NULL;
  563. result = TokenLoadingResultWarning;
  564. }
  565. }
  566. if(flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str)) {
  567. token_info_set_algo_from_str(tokenInfo, temp_str);
  568. } else {
  569. tokenInfo->algo = SHA1;
  570. }
  571. if(!flipper_format_read_uint32(
  572. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &temp_data32, 1) ||
  573. !token_info_set_digits_from_int(tokenInfo, temp_data32)) {
  574. tokenInfo->digits = TOTP_6_DIGITS;
  575. }
  576. FURI_LOG_D(LOGGING_TAG, "Found token \"%s\"", tokenInfo->name);
  577. TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, tokenInfo, furi_check);
  578. index++;
  579. }
  580. plugin_state->tokens_count = index;
  581. plugin_state->token_list_loaded = true;
  582. FURI_LOG_D(LOGGING_TAG, "Found %" PRIu16 " tokens", index);
  583. furi_string_free(temp_str);
  584. totp_close_config_file(fff_data_file);
  585. totp_close_storage();
  586. if(has_any_plain_secret) {
  587. totp_full_save_config_file(plugin_state);
  588. }
  589. return result;
  590. }
  591. TotpConfigFileUpdateResult
  592. totp_config_file_update_crypto_signatures(const PluginState* plugin_state) {
  593. Storage* storage = totp_open_storage();
  594. FlipperFormat* config_file;
  595. TotpConfigFileUpdateResult update_result;
  596. if(totp_open_config_file(storage, &config_file) == TotpConfigFileOpenSuccess) {
  597. do {
  598. if(!flipper_format_insert_or_update_hex(
  599. config_file, TOTP_CONFIG_KEY_BASE_IV, plugin_state->base_iv, TOTP_IV_SIZE)) {
  600. update_result = TotpConfigFileUpdateError;
  601. break;
  602. }
  603. if(!flipper_format_insert_or_update_hex(
  604. config_file,
  605. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  606. plugin_state->crypto_verify_data,
  607. plugin_state->crypto_verify_data_length)) {
  608. update_result = TotpConfigFileUpdateError;
  609. break;
  610. }
  611. if(!flipper_format_insert_or_update_bool(
  612. config_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1)) {
  613. update_result = TotpConfigFileUpdateError;
  614. break;
  615. }
  616. update_result = TotpConfigFileUpdateSuccess;
  617. } while(false);
  618. totp_close_config_file(config_file);
  619. } else {
  620. update_result = TotpConfigFileUpdateError;
  621. }
  622. totp_close_storage();
  623. return update_result;
  624. }