config.c 25 KB

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