config.c 24 KB

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