config.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. flipper_format_write_uint32(
  166. fff_data_file, TOTP_CONFIG_KEY_UI_TOKEN_DIGIT_GROUPING, &tmp_uint32, 1);
  167. if(!flipper_format_rewind(fff_data_file)) {
  168. totp_close_config_file(fff_data_file);
  169. FURI_LOG_E(LOGGING_TAG, "Rewind error");
  170. return false;
  171. }
  172. }
  173. *file = fff_data_file;
  174. return true;
  175. }
  176. char* totp_config_file_backup(const PluginState* plugin_state) {
  177. if(plugin_state->config_file_context == NULL) return NULL;
  178. totp_close_config_file(plugin_state->config_file_context->config_file);
  179. char* result = totp_config_file_backup_i(plugin_state->config_file_context->storage);
  180. totp_open_config_file(
  181. plugin_state->config_file_context->storage,
  182. &plugin_state->config_file_context->config_file);
  183. totp_token_info_iterator_attach_to_config_file(
  184. plugin_state->config_file_context->token_info_iterator_context,
  185. plugin_state->config_file_context->config_file);
  186. return result;
  187. }
  188. bool totp_config_file_update_timezone_offset(const PluginState* plugin_state) {
  189. FlipperFormat* file = plugin_state->config_file_context->config_file;
  190. flipper_format_rewind(file);
  191. bool update_result = false;
  192. do {
  193. if(!flipper_format_insert_or_update_float(
  194. file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  195. break;
  196. }
  197. update_result = true;
  198. } while(false);
  199. return update_result;
  200. }
  201. bool totp_config_file_update_notification_method(const PluginState* plugin_state) {
  202. FlipperFormat* file = plugin_state->config_file_context->config_file;
  203. flipper_format_rewind(file);
  204. bool update_result = false;
  205. do {
  206. uint32_t tmp_uint32 = plugin_state->notification_method;
  207. if(!flipper_format_insert_or_update_uint32(
  208. file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  209. break;
  210. }
  211. update_result = true;
  212. } while(false);
  213. return update_result;
  214. }
  215. bool totp_config_file_update_automation_method(const PluginState* plugin_state) {
  216. FlipperFormat* file = plugin_state->config_file_context->config_file;
  217. flipper_format_rewind(file);
  218. bool update_result = false;
  219. do {
  220. uint32_t 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->automation_kb_layout;
  226. if(!flipper_format_insert_or_update_uint32(
  227. file, TOTP_CONFIG_KEY_AUTOMATION_KB_LAYOUT, &tmp_uint32, 1)) {
  228. break;
  229. }
  230. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  231. tmp_uint32 = plugin_state->bt_type_code_worker_profile_index;
  232. if(!flipper_format_insert_or_update_uint32(
  233. file, TOTP_CONFIG_KEY_AUTOMATION_BADBT_PROFILE, &tmp_uint32, 1)) {
  234. break;
  235. }
  236. #endif
  237. tmp_uint32 = plugin_state->automation_initial_delay;
  238. if(!flipper_format_insert_or_update_uint32(
  239. file, TOTP_CONFIG_KEY_AUTOMATION_INITIAL_DELAY, &tmp_uint32, 1)) {
  240. break;
  241. }
  242. update_result = true;
  243. } while(false);
  244. return update_result;
  245. }
  246. bool totp_config_file_update_user_settings(const PluginState* plugin_state) {
  247. FlipperFormat* file = plugin_state->config_file_context->config_file;
  248. flipper_format_rewind(file);
  249. bool update_result = false;
  250. do {
  251. if(!flipper_format_insert_or_update_float(
  252. file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  253. break;
  254. }
  255. uint32_t tmp_uint32 = plugin_state->notification_method;
  256. if(!flipper_format_insert_or_update_uint32(
  257. file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  258. break;
  259. }
  260. tmp_uint32 = plugin_state->automation_method;
  261. if(!flipper_format_insert_or_update_uint32(
  262. file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1)) {
  263. break;
  264. }
  265. tmp_uint32 = plugin_state->active_font_index;
  266. if(!flipper_format_insert_or_update_uint32(file, TOTP_CONFIG_KEY_FONT, &tmp_uint32, 1)) {
  267. break;
  268. }
  269. tmp_uint32 = plugin_state->automation_kb_layout;
  270. if(!flipper_format_insert_or_update_uint32(
  271. file, TOTP_CONFIG_KEY_AUTOMATION_KB_LAYOUT, &tmp_uint32, 1)) {
  272. break;
  273. }
  274. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  275. tmp_uint32 = plugin_state->bt_type_code_worker_profile_index;
  276. if(!flipper_format_insert_or_update_uint32(
  277. file, TOTP_CONFIG_KEY_AUTOMATION_BADBT_PROFILE, &tmp_uint32, 1)) {
  278. break;
  279. }
  280. #endif
  281. tmp_uint32 = plugin_state->automation_initial_delay;
  282. if(!flipper_format_insert_or_update_uint32(
  283. file, TOTP_CONFIG_KEY_AUTOMATION_INITIAL_DELAY, &tmp_uint32, 1)) {
  284. break;
  285. }
  286. tmp_uint32 = plugin_state->split_token_into_groups;
  287. if(!flipper_format_insert_or_update_uint32(
  288. file, TOTP_CONFIG_KEY_UI_TOKEN_DIGIT_GROUPING, &tmp_uint32, 1)) {
  289. break;
  290. }
  291. update_result = true;
  292. } while(false);
  293. return update_result;
  294. }
  295. bool totp_config_file_load(PluginState* const plugin_state) {
  296. Storage* storage = totp_open_storage();
  297. FlipperFormat* fff_data_file;
  298. if(!totp_open_config_file(storage, &fff_data_file)) {
  299. totp_close_storage();
  300. return false;
  301. }
  302. flipper_format_rewind(fff_data_file);
  303. bool result = false;
  304. plugin_state->timezone_offset = 0;
  305. FuriString* temp_str = furi_string_alloc();
  306. do {
  307. uint32_t file_version;
  308. if(!flipper_format_read_header(fff_data_file, temp_str, &file_version)) {
  309. FURI_LOG_E(LOGGING_TAG, "Missing or incorrect header");
  310. break;
  311. }
  312. if(file_version < CONFIG_FILE_ACTUAL_VERSION) {
  313. FURI_LOG_I(
  314. LOGGING_TAG,
  315. "Obsolete config file version detected. Current version: %" PRIu32
  316. "; Actual version: %" PRId16,
  317. file_version,
  318. CONFIG_FILE_ACTUAL_VERSION);
  319. totp_close_config_file(fff_data_file);
  320. char* backup_path = totp_config_file_backup_i(storage);
  321. if(backup_path != NULL) {
  322. if(totp_open_config_file(storage, &fff_data_file) != true) {
  323. break;
  324. }
  325. FlipperFormat* fff_backup_data_file = flipper_format_file_alloc(storage);
  326. if(!flipper_format_file_open_existing(fff_backup_data_file, backup_path)) {
  327. flipper_format_file_close(fff_backup_data_file);
  328. flipper_format_free(fff_backup_data_file);
  329. break;
  330. }
  331. if(totp_config_migrate_to_latest(fff_data_file, fff_backup_data_file)) {
  332. FURI_LOG_I(
  333. LOGGING_TAG,
  334. "Applied migration to version %" PRId16,
  335. CONFIG_FILE_ACTUAL_VERSION);
  336. file_version = CONFIG_FILE_ACTUAL_VERSION;
  337. } else {
  338. FURI_LOG_W(
  339. LOGGING_TAG,
  340. "An error occurred during migration to version %" PRId16,
  341. CONFIG_FILE_ACTUAL_VERSION);
  342. break;
  343. }
  344. flipper_format_file_close(fff_backup_data_file);
  345. flipper_format_free(fff_backup_data_file);
  346. flipper_format_rewind(fff_data_file);
  347. free(backup_path);
  348. } else {
  349. FURI_LOG_E(
  350. LOGGING_TAG,
  351. "An error occurred during taking backup of %s before migration",
  352. CONFIG_FILE_PATH);
  353. break;
  354. }
  355. }
  356. uint32_t tmp_uint32;
  357. if(!flipper_format_read_uint32(
  358. fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERSION, &tmp_uint32, 1)) {
  359. FURI_LOG_E(LOGGING_TAG, "Missing required " TOTP_CONFIG_KEY_CRYPTO_VERSION "property");
  360. break;
  361. }
  362. plugin_state->crypto_settings.crypto_version = tmp_uint32;
  363. if(!flipper_format_rewind(fff_data_file)) {
  364. break;
  365. }
  366. if(!flipper_format_read_uint32(
  367. fff_data_file, TOTP_CONFIG_KEY_CRYPTO_KEY_SLOT, &tmp_uint32, 1)) {
  368. FURI_LOG_E(
  369. LOGGING_TAG, "Missing required " TOTP_CONFIG_KEY_CRYPTO_KEY_SLOT "property");
  370. break;
  371. }
  372. plugin_state->crypto_settings.crypto_key_slot = tmp_uint32;
  373. if(!flipper_format_rewind(fff_data_file)) {
  374. break;
  375. }
  376. if(!flipper_format_read_hex(
  377. fff_data_file,
  378. TOTP_CONFIG_KEY_SALT,
  379. &plugin_state->crypto_settings.salt[0],
  380. CRYPTO_SALT_LENGTH)) {
  381. FURI_LOG_D(LOGGING_TAG, "Missing salt");
  382. }
  383. if(!flipper_format_rewind(fff_data_file)) {
  384. break;
  385. }
  386. uint32_t crypto_size;
  387. if(flipper_format_get_value_count(
  388. fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, &crypto_size) &&
  389. crypto_size > 0) {
  390. plugin_state->crypto_settings.crypto_verify_data =
  391. malloc(sizeof(uint8_t) * crypto_size);
  392. furi_check(plugin_state->crypto_settings.crypto_verify_data != NULL);
  393. plugin_state->crypto_settings.crypto_verify_data_length = crypto_size;
  394. if(!flipper_format_read_hex(
  395. fff_data_file,
  396. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  397. plugin_state->crypto_settings.crypto_verify_data,
  398. crypto_size)) {
  399. FURI_LOG_D(LOGGING_TAG, "Missing crypto verify token");
  400. free(plugin_state->crypto_settings.crypto_verify_data);
  401. plugin_state->crypto_settings.crypto_verify_data = NULL;
  402. plugin_state->crypto_settings.crypto_verify_data_length = 0;
  403. }
  404. } else {
  405. plugin_state->crypto_settings.crypto_verify_data = NULL;
  406. plugin_state->crypto_settings.crypto_verify_data_length = 0;
  407. }
  408. if(!flipper_format_rewind(fff_data_file)) {
  409. break;
  410. }
  411. if(!flipper_format_read_float(
  412. fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  413. plugin_state->timezone_offset = 0;
  414. FURI_LOG_D(LOGGING_TAG, "Missing timezone offset information, defaulting to 0");
  415. }
  416. if(!flipper_format_rewind(fff_data_file)) {
  417. break;
  418. }
  419. if(!flipper_format_read_bool(
  420. fff_data_file,
  421. TOTP_CONFIG_KEY_PINSET,
  422. &plugin_state->crypto_settings.pin_required,
  423. 1)) {
  424. plugin_state->crypto_settings.pin_required = true;
  425. }
  426. if(!flipper_format_rewind(fff_data_file)) {
  427. break;
  428. }
  429. if(!flipper_format_read_uint32(
  430. fff_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  431. tmp_uint32 = NotificationMethodSound | NotificationMethodVibro;
  432. }
  433. plugin_state->notification_method = tmp_uint32;
  434. if(!flipper_format_rewind(fff_data_file)) {
  435. break;
  436. }
  437. if(!flipper_format_read_uint32(
  438. fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1)) {
  439. tmp_uint32 = AutomationMethodBadUsb;
  440. }
  441. plugin_state->automation_method = tmp_uint32;
  442. if(!flipper_format_rewind(fff_data_file)) {
  443. break;
  444. }
  445. if(!flipper_format_read_uint32(
  446. fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_KB_LAYOUT, &tmp_uint32, 1)) {
  447. tmp_uint32 = TOTP_DEFAULT_KB_LAYOUT;
  448. }
  449. plugin_state->automation_kb_layout = tmp_uint32;
  450. if(!flipper_format_rewind(fff_data_file)) {
  451. break;
  452. }
  453. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  454. if(!flipper_format_read_uint32(
  455. fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_BADBT_PROFILE, &tmp_uint32, 1)) {
  456. tmp_uint32 = 0;
  457. }
  458. plugin_state->bt_type_code_worker_profile_index = tmp_uint32;
  459. if(!flipper_format_rewind(fff_data_file)) {
  460. break;
  461. }
  462. #endif
  463. if(!flipper_format_read_uint32(
  464. fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_INITIAL_DELAY, &tmp_uint32, 1)) {
  465. tmp_uint32 = 500;
  466. }
  467. plugin_state->automation_initial_delay = tmp_uint32;
  468. if(!flipper_format_rewind(fff_data_file)) {
  469. break;
  470. }
  471. if(!flipper_format_read_uint32(fff_data_file, TOTP_CONFIG_KEY_FONT, &tmp_uint32, 1)) {
  472. tmp_uint32 = 0;
  473. }
  474. plugin_state->active_font_index = tmp_uint32;
  475. if(!flipper_format_rewind(fff_data_file)) {
  476. break;
  477. }
  478. if(!flipper_format_read_uint32(
  479. fff_data_file, TOTP_CONFIG_KEY_UI_TOKEN_DIGIT_GROUPING, &tmp_uint32, 1)) {
  480. tmp_uint32 = 0;
  481. }
  482. plugin_state->split_token_into_groups = tmp_uint32;
  483. plugin_state->config_file_context = malloc(sizeof(ConfigFileContext));
  484. furi_check(plugin_state->config_file_context != NULL);
  485. plugin_state->config_file_context->storage = storage;
  486. plugin_state->config_file_context->config_file = fff_data_file;
  487. plugin_state->config_file_context->token_info_iterator_context =
  488. totp_token_info_iterator_alloc(
  489. storage,
  490. plugin_state->config_file_context->config_file,
  491. &plugin_state->crypto_settings);
  492. result = true;
  493. } while(false);
  494. furi_string_free(temp_str);
  495. return result;
  496. }
  497. bool totp_config_file_update_crypto_signatures(const PluginState* plugin_state) {
  498. FlipperFormat* config_file = plugin_state->config_file_context->config_file;
  499. flipper_format_rewind(config_file);
  500. bool update_result = false;
  501. do {
  502. uint32_t tmp_uint32 = plugin_state->crypto_settings.crypto_version;
  503. if(!flipper_format_insert_or_update_uint32(
  504. config_file, TOTP_CONFIG_KEY_CRYPTO_VERSION, &tmp_uint32, 1)) {
  505. break;
  506. }
  507. tmp_uint32 = plugin_state->crypto_settings.crypto_key_slot;
  508. if(!flipper_format_insert_or_update_uint32(
  509. config_file, TOTP_CONFIG_KEY_CRYPTO_KEY_SLOT, &tmp_uint32, 1)) {
  510. break;
  511. }
  512. if(!flipper_format_insert_or_update_hex(
  513. config_file,
  514. TOTP_CONFIG_KEY_SALT,
  515. &plugin_state->crypto_settings.salt[0],
  516. CRYPTO_SALT_LENGTH)) {
  517. break;
  518. }
  519. if(!flipper_format_insert_or_update_hex(
  520. config_file,
  521. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  522. plugin_state->crypto_settings.crypto_verify_data,
  523. plugin_state->crypto_settings.crypto_verify_data_length)) {
  524. break;
  525. }
  526. if(!flipper_format_insert_or_update_bool(
  527. config_file,
  528. TOTP_CONFIG_KEY_PINSET,
  529. &plugin_state->crypto_settings.pin_required,
  530. 1)) {
  531. break;
  532. }
  533. update_result = true;
  534. } while(false);
  535. return update_result;
  536. }
  537. void totp_config_file_close(PluginState* const plugin_state) {
  538. if(plugin_state->config_file_context == NULL) return;
  539. totp_token_info_iterator_free(plugin_state->config_file_context->token_info_iterator_context);
  540. totp_close_config_file(plugin_state->config_file_context->config_file);
  541. free(plugin_state->config_file_context);
  542. plugin_state->config_file_context = NULL;
  543. totp_close_storage();
  544. }
  545. void totp_config_file_reset(PluginState* const plugin_state) {
  546. totp_config_file_close(plugin_state);
  547. Storage* storage = totp_open_storage();
  548. storage_simply_remove(storage, CONFIG_FILE_PATH);
  549. totp_close_storage();
  550. }
  551. bool totp_config_file_update_encryption(
  552. PluginState* plugin_state,
  553. uint8_t new_crypto_key_slot,
  554. const uint8_t* new_pin,
  555. uint8_t new_pin_length) {
  556. FlipperFormat* config_file = plugin_state->config_file_context->config_file;
  557. Stream* stream = flipper_format_get_raw_stream(config_file);
  558. size_t original_offset = stream_tell(stream);
  559. if(!stream_rewind(stream)) {
  560. return false;
  561. }
  562. if(!totp_crypto_check_key_slot(new_crypto_key_slot)) {
  563. return false;
  564. }
  565. CryptoSettings old_crypto_settings = plugin_state->crypto_settings;
  566. memset(&plugin_state->crypto_settings.iv[0], 0, CRYPTO_IV_LENGTH);
  567. memset(&plugin_state->crypto_settings.salt[0], 0, CRYPTO_SALT_LENGTH);
  568. if(plugin_state->crypto_settings.crypto_verify_data != NULL) {
  569. free(plugin_state->crypto_settings.crypto_verify_data);
  570. plugin_state->crypto_settings.crypto_verify_data = NULL;
  571. }
  572. plugin_state->crypto_settings.crypto_key_slot = new_crypto_key_slot;
  573. plugin_state->crypto_settings.crypto_version = CRYPTO_LATEST_VERSION;
  574. CryptoSeedIVResult seed_result = totp_crypto_seed_iv(
  575. &plugin_state->crypto_settings, new_pin_length > 0 ? new_pin : NULL, new_pin_length);
  576. if(seed_result & CryptoSeedIVResultFlagSuccess &&
  577. seed_result & CryptoSeedIVResultFlagNewCryptoVerifyData &&
  578. !totp_config_file_update_crypto_signatures(plugin_state)) {
  579. return false;
  580. } else if(seed_result == CryptoSeedIVResultFailed) {
  581. return false;
  582. }
  583. char buffer[sizeof(TOTP_CONFIG_KEY_TOKEN_SECRET) + 1];
  584. bool result = true;
  585. while(true) {
  586. if(!stream_seek_to_char(stream, '\n', StreamDirectionForward)) {
  587. break;
  588. }
  589. size_t buffer_read_size;
  590. if((buffer_read_size = stream_read(stream, (uint8_t*)&buffer[0], sizeof(buffer))) == 0) {
  591. break;
  592. }
  593. if(!stream_seek(stream, -(int32_t)buffer_read_size, StreamOffsetFromCurrent)) {
  594. result = false;
  595. break;
  596. }
  597. if(strncmp(buffer, "\n" TOTP_CONFIG_KEY_TOKEN_SECRET ":", sizeof(buffer)) == 0) {
  598. uint32_t secret_bytes_count;
  599. if(!flipper_format_get_value_count(
  600. config_file, TOTP_CONFIG_KEY_TOKEN_SECRET, &secret_bytes_count)) {
  601. secret_bytes_count = 0;
  602. }
  603. if(secret_bytes_count > 1) {
  604. size_t secret_token_start = stream_tell(stream) + 1;
  605. uint8_t* encrypted_token = malloc(secret_bytes_count);
  606. furi_check(encrypted_token != NULL);
  607. if(!flipper_format_read_hex(
  608. config_file,
  609. TOTP_CONFIG_KEY_TOKEN_SECRET,
  610. encrypted_token,
  611. secret_bytes_count)) {
  612. result = false;
  613. free(encrypted_token);
  614. break;
  615. }
  616. size_t plain_token_length;
  617. uint8_t* plain_token = totp_crypto_decrypt(
  618. encrypted_token, secret_bytes_count, &old_crypto_settings, &plain_token_length);
  619. free(encrypted_token);
  620. size_t encrypted_token_length;
  621. encrypted_token = totp_crypto_encrypt(
  622. plain_token,
  623. plain_token_length,
  624. &plugin_state->crypto_settings,
  625. &encrypted_token_length);
  626. memset_s(plain_token, plain_token_length, 0, plain_token_length);
  627. free(plain_token);
  628. if(!stream_seek(stream, secret_token_start, StreamOffsetFromStart)) {
  629. result = false;
  630. free(encrypted_token);
  631. break;
  632. }
  633. if(!flipper_format_write_hex(
  634. config_file,
  635. TOTP_CONFIG_KEY_TOKEN_SECRET,
  636. encrypted_token,
  637. encrypted_token_length)) {
  638. free(encrypted_token);
  639. result = false;
  640. break;
  641. }
  642. free(encrypted_token);
  643. }
  644. }
  645. }
  646. stream_seek(stream, original_offset, StreamOffsetFromStart);
  647. return result;
  648. }
  649. bool totp_config_file_ensure_latest_encryption(
  650. PluginState* plugin_state,
  651. const uint8_t* pin,
  652. uint8_t pin_length) {
  653. bool result = true;
  654. if(plugin_state->crypto_settings.crypto_version < CRYPTO_LATEST_VERSION) {
  655. FURI_LOG_I(
  656. LOGGING_TAG,
  657. "Migration crypto from v%" PRIu8 " to v%" PRIu8 " is needed",
  658. plugin_state->crypto_settings.crypto_version,
  659. CRYPTO_LATEST_VERSION);
  660. #ifndef TOTP_OBSOLETE_CRYPTO_V1_COMPATIBILITY_ENABLED
  661. if(plugin_state->crypto_settings.crypto_version == 1) {
  662. furi_crash("Authenticator: Crypto v1 is not supported");
  663. }
  664. #endif
  665. #ifndef TOTP_OBSOLETE_CRYPTO_V2_COMPATIBILITY_ENABLED
  666. if(plugin_state->crypto_settings.crypto_version == 2) {
  667. furi_crash("Authenticator: Crypto v2 is not supported");
  668. }
  669. #endif
  670. char* backup_path = totp_config_file_backup(plugin_state);
  671. if(backup_path != NULL) {
  672. free(backup_path);
  673. uint8_t crypto_key_slot = plugin_state->crypto_settings.crypto_key_slot;
  674. if(!totp_crypto_check_key_slot(crypto_key_slot)) {
  675. crypto_key_slot = DEFAULT_CRYPTO_KEY_SLOT;
  676. }
  677. result =
  678. totp_config_file_update_encryption(plugin_state, crypto_key_slot, pin, pin_length);
  679. FURI_LOG_I(
  680. LOGGING_TAG,
  681. "Migration to crypto v%d is done. Result: %d",
  682. CRYPTO_LATEST_VERSION,
  683. result);
  684. } else {
  685. result = false;
  686. }
  687. }
  688. return result;
  689. }
  690. TokenInfoIteratorContext* totp_config_get_token_iterator_context(const PluginState* plugin_state) {
  691. return plugin_state->config_file_context->token_info_iterator_context;
  692. }