config.c 27 KB

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