config.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. #include "config.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <flipper_format/flipper_format.h>
  5. #include <furi_hal_rtc.h>
  6. #include <flipper_format/flipper_format_i.h>
  7. #include <flipper_format/flipper_format_stream.h>
  8. #include <memset_s.h>
  9. #include "../../types/common.h"
  10. #include "../../types/token_info.h"
  11. #include "../../features_config.h"
  12. #include "../crypto/crypto.h"
  13. #include "migrations/common_migration.h"
  14. #define CONFIG_FILE_PATH CONFIG_FILE_DIRECTORY_PATH "/totp.conf"
  15. #define CONFIG_FILE_BACKUP_DIR CONFIG_FILE_DIRECTORY_PATH "/backups"
  16. #define CONFIG_FILE_BACKUP_BASE_PATH CONFIG_FILE_BACKUP_DIR "/totp.conf"
  17. #define CONFIG_FILE_TEMP_PATH CONFIG_FILE_PATH ".tmp"
  18. #define CONFIG_FILE_ORIG_PATH CONFIG_FILE_PATH ".orig"
  19. #define CONFIG_FILE_PATH_PREVIOUS EXT_PATH("apps/Misc") "/totp.conf"
  20. struct ConfigFileContext {
  21. /**
  22. * @brief Config file reference
  23. */
  24. FlipperFormat* config_file;
  25. /**
  26. * @brief Storage reference
  27. */
  28. Storage* storage;
  29. /**
  30. * @brief Token list iterator context
  31. */
  32. TokenInfoIteratorContext* token_info_iterator_context;
  33. };
  34. /**
  35. * @brief Opens storage record
  36. * @return Storage record
  37. */
  38. static Storage* totp_open_storage() {
  39. return furi_record_open(RECORD_STORAGE);
  40. }
  41. /**
  42. * @brief Closes storage record
  43. */
  44. static void totp_close_storage() {
  45. furi_record_close(RECORD_STORAGE);
  46. }
  47. /**
  48. * @brief Closes config file
  49. * @param file config file reference
  50. */
  51. static void totp_close_config_file(FlipperFormat* file) {
  52. if(file == NULL) return;
  53. flipper_format_file_close(file);
  54. flipper_format_free(file);
  55. }
  56. /**
  57. * @brief Tries to take a config file backup
  58. * @param storage storage record
  59. * @return backup path if backup successfully taken; \c NULL otherwise
  60. */
  61. static char* totp_config_file_backup_i(Storage* storage) {
  62. if(!storage_dir_exists(storage, CONFIG_FILE_BACKUP_DIR) &&
  63. !storage_simply_mkdir(storage, CONFIG_FILE_BACKUP_DIR)) {
  64. return NULL;
  65. }
  66. FuriHalRtcDateTime current_datetime;
  67. furi_hal_rtc_get_datetime(&current_datetime);
  68. uint8_t backup_path_size = sizeof(CONFIG_FILE_BACKUP_BASE_PATH) + 14;
  69. char* backup_path = malloc(backup_path_size);
  70. furi_check(backup_path != NULL);
  71. memcpy(backup_path, CONFIG_FILE_BACKUP_BASE_PATH, sizeof(CONFIG_FILE_BACKUP_BASE_PATH));
  72. uint16_t i = 1;
  73. bool backup_file_exists;
  74. do {
  75. snprintf(
  76. backup_path,
  77. backup_path_size,
  78. CONFIG_FILE_BACKUP_BASE_PATH ".%4" PRIu16 "%02" PRIu8 "%02" PRIu8 "-%" PRIu16,
  79. current_datetime.year,
  80. current_datetime.month,
  81. current_datetime.day,
  82. i);
  83. i++;
  84. } while((backup_file_exists = storage_common_exists(storage, backup_path)) && i <= 9999);
  85. if(backup_file_exists ||
  86. storage_common_copy(storage, CONFIG_FILE_PATH, backup_path) != FSE_OK) {
  87. FURI_LOG_E(LOGGING_TAG, "Unable to take a backup");
  88. free(backup_path);
  89. return NULL;
  90. }
  91. FURI_LOG_I(LOGGING_TAG, "Took config file backup to %s", backup_path);
  92. return backup_path;
  93. }
  94. /**
  95. * @brief Opens or creates TOTP application standard config file
  96. * @param storage storage record to use
  97. * @param[out] file opened config file
  98. * @return Config file open result
  99. */
  100. static bool totp_open_config_file(Storage* storage, FlipperFormat** file) {
  101. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  102. if(storage_common_stat(storage, CONFIG_FILE_PATH, NULL) == FSE_OK) {
  103. FURI_LOG_D(LOGGING_TAG, "Config file %s found", CONFIG_FILE_PATH);
  104. if(!flipper_format_file_open_existing(fff_data_file, CONFIG_FILE_PATH)) {
  105. FURI_LOG_E(LOGGING_TAG, "Error opening existing file %s", CONFIG_FILE_PATH);
  106. totp_close_config_file(fff_data_file);
  107. return false;
  108. }
  109. } else if(storage_common_stat(storage, CONFIG_FILE_PATH_PREVIOUS, NULL) == FSE_OK) {
  110. FURI_LOG_D(LOGGING_TAG, "Old config file %s found", CONFIG_FILE_PATH_PREVIOUS);
  111. if(storage_common_stat(storage, CONFIG_FILE_DIRECTORY_PATH, NULL) == FSE_NOT_EXIST) {
  112. FURI_LOG_D(
  113. LOGGING_TAG,
  114. "Directory %s doesn't exist. Will create new.",
  115. CONFIG_FILE_DIRECTORY_PATH);
  116. if(!storage_simply_mkdir(storage, CONFIG_FILE_DIRECTORY_PATH)) {
  117. FURI_LOG_E(LOGGING_TAG, "Error creating directory %s", CONFIG_FILE_DIRECTORY_PATH);
  118. totp_close_config_file(fff_data_file);
  119. return false;
  120. }
  121. }
  122. if(storage_common_rename(storage, CONFIG_FILE_PATH_PREVIOUS, CONFIG_FILE_PATH) != FSE_OK) {
  123. FURI_LOG_E(LOGGING_TAG, "Error moving config to %s", CONFIG_FILE_PATH);
  124. totp_close_config_file(fff_data_file);
  125. return false;
  126. }
  127. FURI_LOG_I(LOGGING_TAG, "Applied config file path migration");
  128. return totp_open_config_file(storage, file);
  129. } else {
  130. FURI_LOG_D(LOGGING_TAG, "Config file %s is not found. Will create new.", CONFIG_FILE_PATH);
  131. if(storage_common_stat(storage, CONFIG_FILE_DIRECTORY_PATH, NULL) == FSE_NOT_EXIST) {
  132. FURI_LOG_D(
  133. LOGGING_TAG,
  134. "Directory %s doesn't exist. Will create new.",
  135. CONFIG_FILE_DIRECTORY_PATH);
  136. if(!storage_simply_mkdir(storage, CONFIG_FILE_DIRECTORY_PATH)) {
  137. FURI_LOG_E(LOGGING_TAG, "Error creating directory %s", CONFIG_FILE_DIRECTORY_PATH);
  138. return false;
  139. }
  140. }
  141. if(!flipper_format_file_open_new(fff_data_file, CONFIG_FILE_PATH)) {
  142. totp_close_config_file(fff_data_file);
  143. FURI_LOG_E(LOGGING_TAG, "Error creating new file %s", CONFIG_FILE_PATH);
  144. return false;
  145. }
  146. flipper_format_write_header_cstr(
  147. fff_data_file, CONFIG_FILE_HEADER, CONFIG_FILE_ACTUAL_VERSION);
  148. flipper_format_write_comment_cstr(
  149. fff_data_file,
  150. "Config file format specification can be found here: https://github.com/akopachov/flipper-zero_authenticator/blob/master/docs/conf-file_description.md");
  151. float tmp_tz = 0;
  152. flipper_format_write_float(fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &tmp_tz, 1);
  153. uint32_t tmp_uint32 = NotificationMethodSound | NotificationMethodVibro;
  154. flipper_format_write_uint32(
  155. fff_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1);
  156. tmp_uint32 = AutomationMethodBadUsb;
  157. flipper_format_write_uint32(
  158. fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1);
  159. if(!flipper_format_rewind(fff_data_file)) {
  160. totp_close_config_file(fff_data_file);
  161. FURI_LOG_E(LOGGING_TAG, "Rewind error");
  162. return false;
  163. }
  164. }
  165. *file = fff_data_file;
  166. return true;
  167. }
  168. char* totp_config_file_backup(const PluginState* plugin_state) {
  169. if(plugin_state->config_file_context == NULL) return NULL;
  170. totp_close_config_file(plugin_state->config_file_context->config_file);
  171. char* result = totp_config_file_backup_i(plugin_state->config_file_context->storage);
  172. totp_open_config_file(
  173. plugin_state->config_file_context->storage,
  174. &plugin_state->config_file_context->config_file);
  175. totp_token_info_iterator_attach_to_config_file(
  176. plugin_state->config_file_context->token_info_iterator_context,
  177. plugin_state->config_file_context->config_file);
  178. return result;
  179. }
  180. bool totp_config_file_update_timezone_offset(const PluginState* plugin_state) {
  181. FlipperFormat* file = plugin_state->config_file_context->config_file;
  182. flipper_format_rewind(file);
  183. bool update_result = false;
  184. do {
  185. if(!flipper_format_insert_or_update_float(
  186. file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  187. break;
  188. }
  189. update_result = true;
  190. } while(false);
  191. return update_result;
  192. }
  193. bool totp_config_file_update_notification_method(const PluginState* plugin_state) {
  194. FlipperFormat* file = plugin_state->config_file_context->config_file;
  195. flipper_format_rewind(file);
  196. bool update_result = false;
  197. do {
  198. uint32_t tmp_uint32 = plugin_state->notification_method;
  199. if(!flipper_format_insert_or_update_uint32(
  200. file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  201. break;
  202. }
  203. update_result = true;
  204. } while(false);
  205. return update_result;
  206. }
  207. bool totp_config_file_update_automation_method(const PluginState* plugin_state) {
  208. FlipperFormat* file = plugin_state->config_file_context->config_file;
  209. flipper_format_rewind(file);
  210. bool update_result = false;
  211. do {
  212. uint32_t tmp_uint32 = plugin_state->automation_method;
  213. if(!flipper_format_insert_or_update_uint32(
  214. file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1)) {
  215. break;
  216. }
  217. update_result = true;
  218. } while(false);
  219. return update_result;
  220. }
  221. bool totp_config_file_update_user_settings(const PluginState* plugin_state) {
  222. FlipperFormat* file = plugin_state->config_file_context->config_file;
  223. flipper_format_rewind(file);
  224. bool update_result = false;
  225. do {
  226. if(!flipper_format_insert_or_update_float(
  227. file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  228. break;
  229. }
  230. uint32_t tmp_uint32 = plugin_state->notification_method;
  231. if(!flipper_format_insert_or_update_uint32(
  232. file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  233. break;
  234. }
  235. tmp_uint32 = plugin_state->automation_method;
  236. if(!flipper_format_insert_or_update_uint32(
  237. file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1)) {
  238. break;
  239. }
  240. update_result = true;
  241. } while(false);
  242. return update_result;
  243. }
  244. bool totp_config_file_load(PluginState* const plugin_state) {
  245. Storage* storage = totp_open_storage();
  246. FlipperFormat* fff_data_file;
  247. if(!totp_open_config_file(storage, &fff_data_file)) {
  248. totp_close_storage();
  249. return false;
  250. }
  251. flipper_format_rewind(fff_data_file);
  252. bool result = false;
  253. plugin_state->timezone_offset = 0;
  254. FuriString* temp_str = furi_string_alloc();
  255. do {
  256. uint32_t file_version;
  257. if(!flipper_format_read_header(fff_data_file, temp_str, &file_version)) {
  258. FURI_LOG_E(LOGGING_TAG, "Missing or incorrect header");
  259. break;
  260. }
  261. if(file_version < CONFIG_FILE_ACTUAL_VERSION) {
  262. FURI_LOG_I(
  263. LOGGING_TAG,
  264. "Obsolete config file version detected. Current version: %" PRIu32
  265. "; Actual version: %" PRId16,
  266. file_version,
  267. CONFIG_FILE_ACTUAL_VERSION);
  268. totp_close_config_file(fff_data_file);
  269. char* backup_path = totp_config_file_backup_i(storage);
  270. if(backup_path != NULL) {
  271. if(totp_open_config_file(storage, &fff_data_file) != true) {
  272. break;
  273. }
  274. FlipperFormat* fff_backup_data_file = flipper_format_file_alloc(storage);
  275. if(!flipper_format_file_open_existing(fff_backup_data_file, backup_path)) {
  276. flipper_format_file_close(fff_backup_data_file);
  277. flipper_format_free(fff_backup_data_file);
  278. break;
  279. }
  280. if(totp_config_migrate_to_latest(fff_data_file, fff_backup_data_file)) {
  281. FURI_LOG_I(
  282. LOGGING_TAG,
  283. "Applied migration to version %" PRId16,
  284. CONFIG_FILE_ACTUAL_VERSION);
  285. file_version = CONFIG_FILE_ACTUAL_VERSION;
  286. } else {
  287. FURI_LOG_W(
  288. LOGGING_TAG,
  289. "An error occurred during migration to version %" PRId16,
  290. CONFIG_FILE_ACTUAL_VERSION);
  291. break;
  292. }
  293. flipper_format_file_close(fff_backup_data_file);
  294. flipper_format_free(fff_backup_data_file);
  295. flipper_format_rewind(fff_data_file);
  296. free(backup_path);
  297. } else {
  298. FURI_LOG_E(
  299. LOGGING_TAG,
  300. "An error occurred during taking backup of %s before migration",
  301. CONFIG_FILE_PATH);
  302. break;
  303. }
  304. }
  305. if(!flipper_format_read_hex(
  306. fff_data_file, TOTP_CONFIG_KEY_BASE_IV, &plugin_state->base_iv[0], TOTP_IV_SIZE)) {
  307. FURI_LOG_D(LOGGING_TAG, "Missing base IV");
  308. }
  309. if(!flipper_format_rewind(fff_data_file)) {
  310. break;
  311. }
  312. uint32_t crypto_size;
  313. if(flipper_format_get_value_count(
  314. fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, &crypto_size) &&
  315. crypto_size > 0) {
  316. plugin_state->crypto_verify_data = malloc(sizeof(uint8_t) * crypto_size);
  317. furi_check(plugin_state->crypto_verify_data != NULL);
  318. plugin_state->crypto_verify_data_length = crypto_size;
  319. if(!flipper_format_read_hex(
  320. fff_data_file,
  321. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  322. plugin_state->crypto_verify_data,
  323. crypto_size)) {
  324. FURI_LOG_D(LOGGING_TAG, "Missing crypto verify token");
  325. free(plugin_state->crypto_verify_data);
  326. plugin_state->crypto_verify_data = NULL;
  327. plugin_state->crypto_verify_data_length = 0;
  328. }
  329. } else {
  330. plugin_state->crypto_verify_data = NULL;
  331. plugin_state->crypto_verify_data_length = 0;
  332. }
  333. if(!flipper_format_rewind(fff_data_file)) {
  334. break;
  335. }
  336. if(!flipper_format_read_float(
  337. fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  338. plugin_state->timezone_offset = 0;
  339. FURI_LOG_D(LOGGING_TAG, "Missing timezone offset information, defaulting to 0");
  340. }
  341. if(!flipper_format_rewind(fff_data_file)) {
  342. break;
  343. }
  344. if(!flipper_format_read_bool(
  345. fff_data_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1)) {
  346. plugin_state->pin_set = true;
  347. }
  348. flipper_format_rewind(fff_data_file);
  349. uint32_t tmp_uint32;
  350. if(!flipper_format_read_uint32(
  351. fff_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  352. tmp_uint32 = NotificationMethodSound | NotificationMethodVibro;
  353. }
  354. plugin_state->notification_method = tmp_uint32;
  355. flipper_format_rewind(fff_data_file);
  356. if(!flipper_format_read_uint32(
  357. fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1)) {
  358. tmp_uint32 = AutomationMethodBadUsb;
  359. }
  360. plugin_state->automation_method = tmp_uint32;
  361. plugin_state->config_file_context = malloc(sizeof(ConfigFileContext));
  362. furi_check(plugin_state->config_file_context != NULL);
  363. plugin_state->config_file_context->storage = storage;
  364. plugin_state->config_file_context->config_file = fff_data_file;
  365. plugin_state->config_file_context->token_info_iterator_context =
  366. totp_token_info_iterator_alloc(
  367. storage, plugin_state->config_file_context->config_file, plugin_state->iv);
  368. result = true;
  369. } while(false);
  370. furi_string_free(temp_str);
  371. return result;
  372. }
  373. bool totp_config_file_update_crypto_signatures(const PluginState* plugin_state) {
  374. FlipperFormat* config_file = plugin_state->config_file_context->config_file;
  375. flipper_format_rewind(config_file);
  376. bool update_result = false;
  377. do {
  378. if(!flipper_format_insert_or_update_hex(
  379. config_file, TOTP_CONFIG_KEY_BASE_IV, plugin_state->base_iv, TOTP_IV_SIZE)) {
  380. break;
  381. }
  382. if(!flipper_format_insert_or_update_hex(
  383. config_file,
  384. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  385. plugin_state->crypto_verify_data,
  386. plugin_state->crypto_verify_data_length)) {
  387. break;
  388. }
  389. if(!flipper_format_insert_or_update_bool(
  390. config_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1)) {
  391. break;
  392. }
  393. update_result = true;
  394. } while(false);
  395. return update_result;
  396. }
  397. void totp_config_file_close(PluginState* const plugin_state) {
  398. if(plugin_state->config_file_context == NULL) return;
  399. totp_token_info_iterator_free(plugin_state->config_file_context->token_info_iterator_context);
  400. totp_close_config_file(plugin_state->config_file_context->config_file);
  401. free(plugin_state->config_file_context);
  402. plugin_state->config_file_context = NULL;
  403. totp_close_storage();
  404. }
  405. void totp_config_file_reset(PluginState* const plugin_state) {
  406. totp_config_file_close(plugin_state);
  407. Storage* storage = totp_open_storage();
  408. storage_simply_remove(storage, CONFIG_FILE_PATH);
  409. totp_close_storage();
  410. }
  411. bool totp_config_file_update_encryption(
  412. PluginState* plugin_state,
  413. const uint8_t* new_pin,
  414. uint8_t new_pin_length) {
  415. FlipperFormat* config_file = plugin_state->config_file_context->config_file;
  416. Stream* stream = flipper_format_get_raw_stream(config_file);
  417. size_t original_offset = stream_tell(stream);
  418. if(!stream_rewind(stream)) {
  419. return false;
  420. }
  421. uint8_t old_iv[TOTP_IV_SIZE];
  422. memcpy(&old_iv[0], &plugin_state->iv[0], TOTP_IV_SIZE);
  423. memset(&plugin_state->iv[0], 0, TOTP_IV_SIZE);
  424. memset(&plugin_state->base_iv[0], 0, TOTP_IV_SIZE);
  425. if(plugin_state->crypto_verify_data != NULL) {
  426. free(plugin_state->crypto_verify_data);
  427. plugin_state->crypto_verify_data = NULL;
  428. }
  429. CryptoSeedIVResult seed_result =
  430. totp_crypto_seed_iv(plugin_state, new_pin_length > 0 ? new_pin : NULL, new_pin_length);
  431. if(seed_result & CryptoSeedIVResultFlagSuccess &&
  432. seed_result & CryptoSeedIVResultFlagNewCryptoVerifyData) {
  433. if(!totp_config_file_update_crypto_signatures(plugin_state)) {
  434. return false;
  435. }
  436. } else if(seed_result == CryptoSeedIVResultFailed) {
  437. return false;
  438. }
  439. char buffer[sizeof(TOTP_CONFIG_KEY_TOKEN_SECRET) + 1];
  440. bool result = true;
  441. while(true) {
  442. if(!stream_seek_to_char(stream, '\n', StreamDirectionForward)) {
  443. break;
  444. }
  445. size_t buffer_read_size;
  446. if((buffer_read_size = stream_read(stream, (uint8_t*)&buffer[0], sizeof(buffer))) == 0) {
  447. break;
  448. }
  449. if(!stream_seek(stream, -(int32_t)buffer_read_size, StreamOffsetFromCurrent)) {
  450. result = false;
  451. break;
  452. }
  453. if(strncmp(buffer, "\n" TOTP_CONFIG_KEY_TOKEN_SECRET ":", sizeof(buffer)) == 0) {
  454. uint32_t secret_bytes_count;
  455. if(!flipper_format_get_value_count(
  456. config_file, TOTP_CONFIG_KEY_TOKEN_SECRET, &secret_bytes_count)) {
  457. secret_bytes_count = 0;
  458. }
  459. if(secret_bytes_count > 1) {
  460. size_t secret_token_start = stream_tell(stream) + 1;
  461. uint8_t* encrypted_token = malloc(secret_bytes_count);
  462. furi_check(encrypted_token != NULL);
  463. if(!flipper_format_read_hex(
  464. config_file,
  465. TOTP_CONFIG_KEY_TOKEN_SECRET,
  466. encrypted_token,
  467. secret_bytes_count)) {
  468. result = false;
  469. free(encrypted_token);
  470. break;
  471. }
  472. size_t plain_token_length;
  473. uint8_t* plain_token = totp_crypto_decrypt(
  474. encrypted_token, secret_bytes_count, &old_iv[0], &plain_token_length);
  475. free(encrypted_token);
  476. size_t encrypted_token_length;
  477. encrypted_token = totp_crypto_encrypt(
  478. plain_token, plain_token_length, &plugin_state->iv[0], &encrypted_token_length);
  479. memset_s(plain_token, plain_token_length, 0, plain_token_length);
  480. free(plain_token);
  481. if(!stream_seek(stream, secret_token_start, StreamOffsetFromStart)) {
  482. result = false;
  483. free(encrypted_token);
  484. break;
  485. }
  486. if(!flipper_format_write_hex(
  487. config_file,
  488. TOTP_CONFIG_KEY_TOKEN_SECRET,
  489. encrypted_token,
  490. encrypted_token_length)) {
  491. free(encrypted_token);
  492. result = false;
  493. break;
  494. }
  495. free(encrypted_token);
  496. }
  497. }
  498. }
  499. stream_seek(stream, original_offset, StreamOffsetFromStart);
  500. return result;
  501. }
  502. TokenInfoIteratorContext* totp_config_get_token_iterator_context(const PluginState* plugin_state) {
  503. return plugin_state->config_file_context->token_info_iterator_context;
  504. }