config.c 28 KB

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