config.c 26 KB

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