config.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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 "../../config/app/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. bool conf_file_exists = storage_common_stat(storage, CONFIG_FILE_PATH, NULL) == FSE_OK;
  101. if(!conf_file_exists &&
  102. storage_common_stat(storage, EXT_PATH("authenticator"), NULL) == FSE_OK) {
  103. FURI_LOG_I(LOGGING_TAG, "Application catalog needs to be migrated");
  104. FS_Error migration_result =
  105. storage_common_migrate(storage, EXT_PATH("authenticator"), CONFIG_FILE_DIRECTORY_PATH);
  106. FURI_LOG_I(LOGGING_TAG, "Migrated catalog. Result code: %d", (int)migration_result);
  107. conf_file_exists = storage_common_stat(storage, CONFIG_FILE_PATH, NULL) == FSE_OK;
  108. }
  109. if(conf_file_exists) {
  110. FURI_LOG_D(LOGGING_TAG, "Config file %s found", CONFIG_FILE_PATH);
  111. if(!flipper_format_file_open_existing(fff_data_file, CONFIG_FILE_PATH)) {
  112. FURI_LOG_E(LOGGING_TAG, "Error opening existing file %s", CONFIG_FILE_PATH);
  113. totp_close_config_file(fff_data_file);
  114. return false;
  115. }
  116. } else {
  117. FURI_LOG_D(LOGGING_TAG, "Config file %s is not found. Will create new.", CONFIG_FILE_PATH);
  118. if(!flipper_format_file_open_new(fff_data_file, CONFIG_FILE_PATH)) {
  119. totp_close_config_file(fff_data_file);
  120. FURI_LOG_E(LOGGING_TAG, "Error creating new file %s", CONFIG_FILE_PATH);
  121. return false;
  122. }
  123. flipper_format_write_header_cstr(
  124. fff_data_file, CONFIG_FILE_HEADER, CONFIG_FILE_ACTUAL_VERSION);
  125. uint32_t tmp_uint32 = CRYPTO_LATEST_VERSION;
  126. flipper_format_write_uint32(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERSION, &tmp_uint32, 1);
  127. tmp_uint32 = DEFAULT_CRYPTO_KEY_SLOT;
  128. flipper_format_write_uint32(
  129. fff_data_file, TOTP_CONFIG_KEY_CRYPTO_KEY_SLOT, &tmp_uint32, 1);
  130. flipper_format_write_comment_cstr(
  131. fff_data_file,
  132. "Config file format specification can be found here: https://t.ly/zwQjE");
  133. float tmp_tz = 0;
  134. flipper_format_write_float(fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &tmp_tz, 1);
  135. tmp_uint32 = NotificationMethodSound | NotificationMethodVibro;
  136. flipper_format_write_uint32(
  137. fff_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1);
  138. tmp_uint32 = AutomationMethodBadUsb;
  139. flipper_format_write_uint32(
  140. fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1);
  141. tmp_uint32 = AutomationKeyboardLayoutQWERTY;
  142. flipper_format_write_uint32(
  143. fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_KB_LAYOUT, &tmp_uint32, 1);
  144. tmp_uint32 = 0; //-V1048
  145. flipper_format_write_uint32(fff_data_file, TOTP_CONFIG_KEY_FONT, &tmp_uint32, 1);
  146. if(!flipper_format_rewind(fff_data_file)) {
  147. totp_close_config_file(fff_data_file);
  148. FURI_LOG_E(LOGGING_TAG, "Rewind error");
  149. return false;
  150. }
  151. }
  152. *file = fff_data_file;
  153. return true;
  154. }
  155. char* totp_config_file_backup(const PluginState* plugin_state) {
  156. if(plugin_state->config_file_context == NULL) return NULL;
  157. totp_close_config_file(plugin_state->config_file_context->config_file);
  158. char* result = totp_config_file_backup_i(plugin_state->config_file_context->storage);
  159. totp_open_config_file(
  160. plugin_state->config_file_context->storage,
  161. &plugin_state->config_file_context->config_file);
  162. totp_token_info_iterator_attach_to_config_file(
  163. plugin_state->config_file_context->token_info_iterator_context,
  164. plugin_state->config_file_context->config_file);
  165. return result;
  166. }
  167. bool totp_config_file_update_timezone_offset(const PluginState* plugin_state) {
  168. FlipperFormat* file = plugin_state->config_file_context->config_file;
  169. flipper_format_rewind(file);
  170. bool update_result = false;
  171. do {
  172. if(!flipper_format_insert_or_update_float(
  173. file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  174. break;
  175. }
  176. update_result = true;
  177. } while(false);
  178. return update_result;
  179. }
  180. bool totp_config_file_update_notification_method(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. uint32_t tmp_uint32 = plugin_state->notification_method;
  186. if(!flipper_format_insert_or_update_uint32(
  187. file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  188. break;
  189. }
  190. update_result = true;
  191. } while(false);
  192. return update_result;
  193. }
  194. bool totp_config_file_update_automation_method(const PluginState* plugin_state) {
  195. FlipperFormat* file = plugin_state->config_file_context->config_file;
  196. flipper_format_rewind(file);
  197. bool update_result = false;
  198. do {
  199. uint32_t tmp_uint32 = plugin_state->automation_method;
  200. if(!flipper_format_insert_or_update_uint32(
  201. file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1)) {
  202. break;
  203. }
  204. tmp_uint32 = plugin_state->automation_kb_layout;
  205. if(!flipper_format_insert_or_update_uint32(
  206. file, TOTP_CONFIG_KEY_AUTOMATION_KB_LAYOUT, &tmp_uint32, 1)) {
  207. break;
  208. }
  209. update_result = true;
  210. } while(false);
  211. return update_result;
  212. }
  213. bool totp_config_file_update_user_settings(const PluginState* plugin_state) {
  214. FlipperFormat* file = plugin_state->config_file_context->config_file;
  215. flipper_format_rewind(file);
  216. bool update_result = false;
  217. do {
  218. if(!flipper_format_insert_or_update_float(
  219. file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  220. break;
  221. }
  222. uint32_t tmp_uint32 = plugin_state->notification_method;
  223. if(!flipper_format_insert_or_update_uint32(
  224. file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  225. break;
  226. }
  227. tmp_uint32 = plugin_state->automation_method;
  228. if(!flipper_format_insert_or_update_uint32(
  229. file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1)) {
  230. break;
  231. }
  232. tmp_uint32 = plugin_state->active_font_index;
  233. if(!flipper_format_insert_or_update_uint32(file, TOTP_CONFIG_KEY_FONT, &tmp_uint32, 1)) {
  234. break;
  235. }
  236. tmp_uint32 = plugin_state->automation_kb_layout;
  237. if(!flipper_format_insert_or_update_uint32(
  238. file, TOTP_CONFIG_KEY_AUTOMATION_KB_LAYOUT, &tmp_uint32, 1)) {
  239. break;
  240. }
  241. update_result = true;
  242. } while(false);
  243. return update_result;
  244. }
  245. bool totp_config_file_load(PluginState* const plugin_state) {
  246. Storage* storage = totp_open_storage();
  247. FlipperFormat* fff_data_file;
  248. if(!totp_open_config_file(storage, &fff_data_file)) {
  249. totp_close_storage();
  250. return false;
  251. }
  252. flipper_format_rewind(fff_data_file);
  253. bool result = false;
  254. plugin_state->timezone_offset = 0;
  255. FuriString* temp_str = furi_string_alloc();
  256. do {
  257. uint32_t file_version;
  258. if(!flipper_format_read_header(fff_data_file, temp_str, &file_version)) {
  259. FURI_LOG_E(LOGGING_TAG, "Missing or incorrect header");
  260. break;
  261. }
  262. if(file_version < CONFIG_FILE_ACTUAL_VERSION) {
  263. FURI_LOG_I(
  264. LOGGING_TAG,
  265. "Obsolete config file version detected. Current version: %" PRIu32
  266. "; Actual version: %" PRId16,
  267. file_version,
  268. CONFIG_FILE_ACTUAL_VERSION);
  269. totp_close_config_file(fff_data_file);
  270. char* backup_path = totp_config_file_backup_i(storage);
  271. if(backup_path != NULL) {
  272. if(totp_open_config_file(storage, &fff_data_file) != true) {
  273. break;
  274. }
  275. FlipperFormat* fff_backup_data_file = flipper_format_file_alloc(storage);
  276. if(!flipper_format_file_open_existing(fff_backup_data_file, backup_path)) {
  277. flipper_format_file_close(fff_backup_data_file);
  278. flipper_format_free(fff_backup_data_file);
  279. break;
  280. }
  281. if(totp_config_migrate_to_latest(fff_data_file, fff_backup_data_file)) {
  282. FURI_LOG_I(
  283. LOGGING_TAG,
  284. "Applied migration to version %" PRId16,
  285. CONFIG_FILE_ACTUAL_VERSION);
  286. file_version = CONFIG_FILE_ACTUAL_VERSION;
  287. } else {
  288. FURI_LOG_W(
  289. LOGGING_TAG,
  290. "An error occurred during migration to version %" PRId16,
  291. CONFIG_FILE_ACTUAL_VERSION);
  292. break;
  293. }
  294. flipper_format_file_close(fff_backup_data_file);
  295. flipper_format_free(fff_backup_data_file);
  296. flipper_format_rewind(fff_data_file);
  297. free(backup_path);
  298. } else {
  299. FURI_LOG_E(
  300. LOGGING_TAG,
  301. "An error occurred during taking backup of %s before migration",
  302. CONFIG_FILE_PATH);
  303. break;
  304. }
  305. }
  306. uint32_t tmp_uint32;
  307. if(!flipper_format_read_uint32(
  308. fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERSION, &tmp_uint32, 1)) {
  309. FURI_LOG_E(LOGGING_TAG, "Missing required " TOTP_CONFIG_KEY_CRYPTO_VERSION "property");
  310. break;
  311. }
  312. plugin_state->crypto_settings.crypto_version = tmp_uint32;
  313. if(!flipper_format_rewind(fff_data_file)) {
  314. break;
  315. }
  316. if(!flipper_format_read_uint32(
  317. fff_data_file, TOTP_CONFIG_KEY_CRYPTO_KEY_SLOT, &tmp_uint32, 1)) {
  318. FURI_LOG_E(
  319. LOGGING_TAG, "Missing required " TOTP_CONFIG_KEY_CRYPTO_KEY_SLOT "property");
  320. break;
  321. }
  322. plugin_state->crypto_settings.crypto_key_slot = tmp_uint32;
  323. if(!flipper_format_rewind(fff_data_file)) {
  324. break;
  325. }
  326. if(!flipper_format_read_hex(
  327. fff_data_file,
  328. TOTP_CONFIG_KEY_SALT,
  329. &plugin_state->crypto_settings.salt[0],
  330. CRYPTO_SALT_LENGTH)) {
  331. FURI_LOG_D(LOGGING_TAG, "Missing salt");
  332. }
  333. if(!flipper_format_rewind(fff_data_file)) {
  334. break;
  335. }
  336. uint32_t crypto_size;
  337. if(flipper_format_get_value_count(
  338. fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, &crypto_size) &&
  339. crypto_size > 0) {
  340. plugin_state->crypto_settings.crypto_verify_data =
  341. malloc(sizeof(uint8_t) * crypto_size);
  342. furi_check(plugin_state->crypto_settings.crypto_verify_data != NULL);
  343. plugin_state->crypto_settings.crypto_verify_data_length = crypto_size;
  344. if(!flipper_format_read_hex(
  345. fff_data_file,
  346. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  347. plugin_state->crypto_settings.crypto_verify_data,
  348. crypto_size)) {
  349. FURI_LOG_D(LOGGING_TAG, "Missing crypto verify token");
  350. free(plugin_state->crypto_settings.crypto_verify_data);
  351. plugin_state->crypto_settings.crypto_verify_data = NULL;
  352. plugin_state->crypto_settings.crypto_verify_data_length = 0;
  353. }
  354. } else {
  355. plugin_state->crypto_settings.crypto_verify_data = NULL;
  356. plugin_state->crypto_settings.crypto_verify_data_length = 0;
  357. }
  358. if(!flipper_format_rewind(fff_data_file)) {
  359. break;
  360. }
  361. if(!flipper_format_read_float(
  362. fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  363. plugin_state->timezone_offset = 0;
  364. FURI_LOG_D(LOGGING_TAG, "Missing timezone offset information, defaulting to 0");
  365. }
  366. if(!flipper_format_rewind(fff_data_file)) {
  367. break;
  368. }
  369. if(!flipper_format_read_bool(
  370. fff_data_file,
  371. TOTP_CONFIG_KEY_PINSET,
  372. &plugin_state->crypto_settings.pin_required,
  373. 1)) {
  374. plugin_state->crypto_settings.pin_required = true;
  375. }
  376. if(!flipper_format_rewind(fff_data_file)) {
  377. break;
  378. }
  379. if(!flipper_format_read_uint32(
  380. fff_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  381. tmp_uint32 = NotificationMethodSound | NotificationMethodVibro;
  382. }
  383. plugin_state->notification_method = tmp_uint32;
  384. if(!flipper_format_rewind(fff_data_file)) {
  385. break;
  386. }
  387. if(!flipper_format_read_uint32(
  388. fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1)) {
  389. tmp_uint32 = AutomationMethodBadUsb;
  390. }
  391. plugin_state->automation_method = tmp_uint32;
  392. if(!flipper_format_rewind(fff_data_file)) {
  393. break;
  394. }
  395. if(!flipper_format_read_uint32(
  396. fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_KB_LAYOUT, &tmp_uint32, 1)) {
  397. tmp_uint32 = AutomationKeyboardLayoutQWERTY;
  398. }
  399. plugin_state->automation_kb_layout = tmp_uint32;
  400. if(!flipper_format_rewind(fff_data_file)) {
  401. break;
  402. }
  403. if(!flipper_format_read_uint32(fff_data_file, TOTP_CONFIG_KEY_FONT, &tmp_uint32, 1)) {
  404. tmp_uint32 = 0;
  405. }
  406. plugin_state->active_font_index = tmp_uint32;
  407. plugin_state->config_file_context = malloc(sizeof(ConfigFileContext));
  408. furi_check(plugin_state->config_file_context != NULL);
  409. plugin_state->config_file_context->storage = storage;
  410. plugin_state->config_file_context->config_file = fff_data_file;
  411. plugin_state->config_file_context->token_info_iterator_context =
  412. totp_token_info_iterator_alloc(
  413. storage,
  414. plugin_state->config_file_context->config_file,
  415. &plugin_state->crypto_settings);
  416. result = true;
  417. } while(false);
  418. furi_string_free(temp_str);
  419. return result;
  420. }
  421. bool totp_config_file_update_crypto_signatures(const PluginState* plugin_state) {
  422. FlipperFormat* config_file = plugin_state->config_file_context->config_file;
  423. flipper_format_rewind(config_file);
  424. bool update_result = false;
  425. do {
  426. uint32_t tmp_uint32 = plugin_state->crypto_settings.crypto_version;
  427. if(!flipper_format_insert_or_update_uint32(
  428. config_file, TOTP_CONFIG_KEY_CRYPTO_VERSION, &tmp_uint32, 1)) {
  429. break;
  430. }
  431. tmp_uint32 = plugin_state->crypto_settings.crypto_key_slot;
  432. if(!flipper_format_insert_or_update_uint32(
  433. config_file, TOTP_CONFIG_KEY_CRYPTO_KEY_SLOT, &tmp_uint32, 1)) {
  434. break;
  435. }
  436. if(!flipper_format_insert_or_update_hex(
  437. config_file,
  438. TOTP_CONFIG_KEY_SALT,
  439. &plugin_state->crypto_settings.salt[0],
  440. CRYPTO_SALT_LENGTH)) {
  441. break;
  442. }
  443. if(!flipper_format_insert_or_update_hex(
  444. config_file,
  445. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  446. plugin_state->crypto_settings.crypto_verify_data,
  447. plugin_state->crypto_settings.crypto_verify_data_length)) {
  448. break;
  449. }
  450. if(!flipper_format_insert_or_update_bool(
  451. config_file,
  452. TOTP_CONFIG_KEY_PINSET,
  453. &plugin_state->crypto_settings.pin_required,
  454. 1)) {
  455. break;
  456. }
  457. update_result = true;
  458. } while(false);
  459. return update_result;
  460. }
  461. void totp_config_file_close(PluginState* const plugin_state) {
  462. if(plugin_state->config_file_context == NULL) return;
  463. totp_token_info_iterator_free(plugin_state->config_file_context->token_info_iterator_context);
  464. totp_close_config_file(plugin_state->config_file_context->config_file);
  465. free(plugin_state->config_file_context);
  466. plugin_state->config_file_context = NULL;
  467. totp_close_storage();
  468. }
  469. void totp_config_file_reset(PluginState* const plugin_state) {
  470. totp_config_file_close(plugin_state);
  471. Storage* storage = totp_open_storage();
  472. storage_simply_remove(storage, CONFIG_FILE_PATH);
  473. totp_close_storage();
  474. }
  475. bool totp_config_file_update_encryption(
  476. PluginState* plugin_state,
  477. uint8_t new_crypto_key_slot,
  478. const uint8_t* new_pin,
  479. uint8_t new_pin_length) {
  480. FlipperFormat* config_file = plugin_state->config_file_context->config_file;
  481. Stream* stream = flipper_format_get_raw_stream(config_file);
  482. size_t original_offset = stream_tell(stream);
  483. if(!stream_rewind(stream)) {
  484. return false;
  485. }
  486. if(!totp_crypto_check_key_slot(new_crypto_key_slot)) {
  487. return false;
  488. }
  489. CryptoSettings old_crypto_settings = plugin_state->crypto_settings;
  490. memset(&plugin_state->crypto_settings.iv[0], 0, CRYPTO_IV_LENGTH);
  491. memset(&plugin_state->crypto_settings.salt[0], 0, CRYPTO_SALT_LENGTH);
  492. if(plugin_state->crypto_settings.crypto_verify_data != NULL) {
  493. free(plugin_state->crypto_settings.crypto_verify_data);
  494. plugin_state->crypto_settings.crypto_verify_data = NULL;
  495. }
  496. plugin_state->crypto_settings.crypto_key_slot = new_crypto_key_slot;
  497. plugin_state->crypto_settings.crypto_version = CRYPTO_LATEST_VERSION;
  498. CryptoSeedIVResult seed_result = totp_crypto_seed_iv(
  499. &plugin_state->crypto_settings, new_pin_length > 0 ? new_pin : NULL, new_pin_length);
  500. if(seed_result & CryptoSeedIVResultFlagSuccess &&
  501. seed_result & CryptoSeedIVResultFlagNewCryptoVerifyData &&
  502. !totp_config_file_update_crypto_signatures(plugin_state)) {
  503. return false;
  504. } else if(seed_result == CryptoSeedIVResultFailed) {
  505. return false;
  506. }
  507. char buffer[sizeof(TOTP_CONFIG_KEY_TOKEN_SECRET) + 1];
  508. bool result = true;
  509. while(true) {
  510. if(!stream_seek_to_char(stream, '\n', StreamDirectionForward)) {
  511. break;
  512. }
  513. size_t buffer_read_size;
  514. if((buffer_read_size = stream_read(stream, (uint8_t*)&buffer[0], sizeof(buffer))) == 0) {
  515. break;
  516. }
  517. if(!stream_seek(stream, -(int32_t)buffer_read_size, StreamOffsetFromCurrent)) {
  518. result = false;
  519. break;
  520. }
  521. if(strncmp(buffer, "\n" TOTP_CONFIG_KEY_TOKEN_SECRET ":", sizeof(buffer)) == 0) {
  522. uint32_t secret_bytes_count;
  523. if(!flipper_format_get_value_count(
  524. config_file, TOTP_CONFIG_KEY_TOKEN_SECRET, &secret_bytes_count)) {
  525. secret_bytes_count = 0;
  526. }
  527. if(secret_bytes_count > 1) {
  528. size_t secret_token_start = stream_tell(stream) + 1;
  529. uint8_t* encrypted_token = malloc(secret_bytes_count);
  530. furi_check(encrypted_token != NULL);
  531. if(!flipper_format_read_hex(
  532. config_file,
  533. TOTP_CONFIG_KEY_TOKEN_SECRET,
  534. encrypted_token,
  535. secret_bytes_count)) {
  536. result = false;
  537. free(encrypted_token);
  538. break;
  539. }
  540. size_t plain_token_length;
  541. uint8_t* plain_token = totp_crypto_decrypt(
  542. encrypted_token, secret_bytes_count, &old_crypto_settings, &plain_token_length);
  543. free(encrypted_token);
  544. size_t encrypted_token_length;
  545. encrypted_token = totp_crypto_encrypt(
  546. plain_token,
  547. plain_token_length,
  548. &plugin_state->crypto_settings,
  549. &encrypted_token_length);
  550. memset_s(plain_token, plain_token_length, 0, plain_token_length);
  551. free(plain_token);
  552. if(!stream_seek(stream, secret_token_start, StreamOffsetFromStart)) {
  553. result = false;
  554. free(encrypted_token);
  555. break;
  556. }
  557. if(!flipper_format_write_hex(
  558. config_file,
  559. TOTP_CONFIG_KEY_TOKEN_SECRET,
  560. encrypted_token,
  561. encrypted_token_length)) {
  562. free(encrypted_token);
  563. result = false;
  564. break;
  565. }
  566. free(encrypted_token);
  567. }
  568. }
  569. }
  570. stream_seek(stream, original_offset, StreamOffsetFromStart);
  571. return result;
  572. }
  573. bool totp_config_file_ensure_latest_encryption(
  574. PluginState* plugin_state,
  575. const uint8_t* pin,
  576. uint8_t pin_length) {
  577. bool result = true;
  578. if(plugin_state->crypto_settings.crypto_version < CRYPTO_LATEST_VERSION) {
  579. FURI_LOG_I(LOGGING_TAG, "Migration to crypto v%d is needed", CRYPTO_LATEST_VERSION);
  580. char* backup_path = totp_config_file_backup(plugin_state);
  581. if(backup_path != NULL) {
  582. free(backup_path);
  583. uint8_t crypto_key_slot = plugin_state->crypto_settings.crypto_key_slot;
  584. if(!totp_crypto_check_key_slot(crypto_key_slot)) {
  585. crypto_key_slot = DEFAULT_CRYPTO_KEY_SLOT;
  586. }
  587. result =
  588. totp_config_file_update_encryption(plugin_state, crypto_key_slot, pin, pin_length);
  589. FURI_LOG_I(
  590. LOGGING_TAG,
  591. "Migration to crypto v%d is done. Result: %d",
  592. CRYPTO_LATEST_VERSION,
  593. result);
  594. } else {
  595. result = false;
  596. }
  597. }
  598. return result;
  599. }
  600. TokenInfoIteratorContext* totp_config_get_token_iterator_context(const PluginState* plugin_state) {
  601. return plugin_state->config_file_context->token_info_iterator_context;
  602. }