config.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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.h"
  13. #include "migrations/common_migration.h"
  14. #define CONFIG_FILE_PATH CONFIG_FILE_DIRECTORY_PATH "/totp.conf"
  15. #define CONFIG_FILE_BACKUP_DIR CONFIG_FILE_DIRECTORY_PATH "/backups"
  16. #define CONFIG_FILE_BACKUP_BASE_PATH CONFIG_FILE_BACKUP_DIR "/totp.conf"
  17. struct ConfigFileContext {
  18. /**
  19. * @brief Config file reference
  20. */
  21. FlipperFormat* config_file;
  22. /**
  23. * @brief Storage reference
  24. */
  25. Storage* storage;
  26. /**
  27. * @brief Token list iterator context
  28. */
  29. TokenInfoIteratorContext* token_info_iterator_context;
  30. };
  31. /**
  32. * @brief Opens storage record
  33. * @return Storage record
  34. */
  35. static Storage* totp_open_storage() {
  36. return furi_record_open(RECORD_STORAGE);
  37. }
  38. /**
  39. * @brief Closes storage record
  40. */
  41. static void totp_close_storage() {
  42. furi_record_close(RECORD_STORAGE);
  43. }
  44. /**
  45. * @brief Closes config file
  46. * @param file config file reference
  47. */
  48. static void totp_close_config_file(FlipperFormat* file) {
  49. if(file == NULL) return;
  50. flipper_format_file_close(file);
  51. flipper_format_free(file);
  52. }
  53. /**
  54. * @brief Tries to take a config file backup
  55. * @param storage storage record
  56. * @return backup path if backup successfully taken; \c NULL otherwise
  57. */
  58. static char* totp_config_file_backup_i(Storage* storage) {
  59. if(!storage_dir_exists(storage, CONFIG_FILE_BACKUP_DIR) &&
  60. !storage_simply_mkdir(storage, CONFIG_FILE_BACKUP_DIR)) {
  61. return NULL;
  62. }
  63. FuriHalRtcDateTime current_datetime;
  64. furi_hal_rtc_get_datetime(&current_datetime);
  65. uint8_t backup_path_size = sizeof(CONFIG_FILE_BACKUP_BASE_PATH) + 14;
  66. char* backup_path = malloc(backup_path_size);
  67. furi_check(backup_path != NULL);
  68. memcpy(backup_path, CONFIG_FILE_BACKUP_BASE_PATH, sizeof(CONFIG_FILE_BACKUP_BASE_PATH));
  69. uint16_t i = 1;
  70. bool backup_file_exists;
  71. do {
  72. snprintf(
  73. backup_path,
  74. backup_path_size,
  75. CONFIG_FILE_BACKUP_BASE_PATH ".%4" PRIu16 "%02" PRIu8 "%02" PRIu8 "-%" PRIu16,
  76. current_datetime.year,
  77. current_datetime.month,
  78. current_datetime.day,
  79. i);
  80. i++;
  81. } while((backup_file_exists = storage_common_exists(storage, backup_path)) && i <= 9999);
  82. if(backup_file_exists ||
  83. storage_common_copy(storage, CONFIG_FILE_PATH, backup_path) != FSE_OK) {
  84. FURI_LOG_E(LOGGING_TAG, "Unable to take a backup");
  85. free(backup_path);
  86. return NULL;
  87. }
  88. FURI_LOG_I(LOGGING_TAG, "Took config file backup to %s", backup_path);
  89. return backup_path;
  90. }
  91. /**
  92. * @brief Opens or creates TOTP application standard config file
  93. * @param storage storage record to use
  94. * @param[out] file opened config file
  95. * @return Config file open result
  96. */
  97. static bool totp_open_config_file(Storage* storage, FlipperFormat** file) {
  98. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  99. if(storage_common_stat(storage, CONFIG_FILE_PATH, NULL) == FSE_OK) {
  100. FURI_LOG_D(LOGGING_TAG, "Config file %s found", CONFIG_FILE_PATH);
  101. if(!flipper_format_file_open_existing(fff_data_file, CONFIG_FILE_PATH)) {
  102. FURI_LOG_E(LOGGING_TAG, "Error opening existing file %s", CONFIG_FILE_PATH);
  103. totp_close_config_file(fff_data_file);
  104. return false;
  105. }
  106. } else {
  107. FURI_LOG_D(LOGGING_TAG, "Config file %s is not found. Will create new.", CONFIG_FILE_PATH);
  108. if(storage_common_stat(storage, CONFIG_FILE_DIRECTORY_PATH, NULL) == FSE_NOT_EXIST) {
  109. FURI_LOG_D(
  110. LOGGING_TAG,
  111. "Directory %s doesn't exist. Will create new.",
  112. CONFIG_FILE_DIRECTORY_PATH);
  113. if(!storage_simply_mkdir(storage, CONFIG_FILE_DIRECTORY_PATH)) {
  114. FURI_LOG_E(LOGGING_TAG, "Error creating directory %s", CONFIG_FILE_DIRECTORY_PATH);
  115. return false;
  116. }
  117. }
  118. if(!flipper_format_file_open_new(fff_data_file, CONFIG_FILE_PATH)) {
  119. totp_close_config_file(fff_data_file);
  120. FURI_LOG_E(LOGGING_TAG, "Error creating new file %s", CONFIG_FILE_PATH);
  121. return false;
  122. }
  123. flipper_format_write_header_cstr(
  124. fff_data_file, CONFIG_FILE_HEADER, CONFIG_FILE_ACTUAL_VERSION);
  125. flipper_format_write_comment_cstr(
  126. fff_data_file,
  127. "Config file format specification can be found here: https://github.com/akopachov/flipper-zero_authenticator/blob/master/docs/conf-file_description.md");
  128. float tmp_tz = 0;
  129. flipper_format_write_float(fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &tmp_tz, 1);
  130. uint32_t tmp_uint32 = NotificationMethodSound | NotificationMethodVibro;
  131. flipper_format_write_uint32(
  132. fff_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1);
  133. tmp_uint32 = AutomationMethodBadUsb;
  134. flipper_format_write_uint32(
  135. fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1);
  136. tmp_uint32 = 0;
  137. flipper_format_write_uint32(fff_data_file, TOTP_CONFIG_KEY_FONT, &tmp_uint32, 1);
  138. if(!flipper_format_rewind(fff_data_file)) {
  139. totp_close_config_file(fff_data_file);
  140. FURI_LOG_E(LOGGING_TAG, "Rewind error");
  141. return false;
  142. }
  143. }
  144. *file = fff_data_file;
  145. return true;
  146. }
  147. char* totp_config_file_backup(const PluginState* plugin_state) {
  148. if(plugin_state->config_file_context == NULL) return NULL;
  149. totp_close_config_file(plugin_state->config_file_context->config_file);
  150. char* result = totp_config_file_backup_i(plugin_state->config_file_context->storage);
  151. totp_open_config_file(
  152. plugin_state->config_file_context->storage,
  153. &plugin_state->config_file_context->config_file);
  154. totp_token_info_iterator_attach_to_config_file(
  155. plugin_state->config_file_context->token_info_iterator_context,
  156. plugin_state->config_file_context->config_file);
  157. return result;
  158. }
  159. bool totp_config_file_update_timezone_offset(const PluginState* plugin_state) {
  160. FlipperFormat* file = plugin_state->config_file_context->config_file;
  161. flipper_format_rewind(file);
  162. bool update_result = false;
  163. do {
  164. if(!flipper_format_insert_or_update_float(
  165. file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  166. break;
  167. }
  168. update_result = true;
  169. } while(false);
  170. return update_result;
  171. }
  172. bool totp_config_file_update_notification_method(const PluginState* plugin_state) {
  173. FlipperFormat* file = plugin_state->config_file_context->config_file;
  174. flipper_format_rewind(file);
  175. bool update_result = false;
  176. do {
  177. uint32_t tmp_uint32 = plugin_state->notification_method;
  178. if(!flipper_format_insert_or_update_uint32(
  179. file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  180. break;
  181. }
  182. update_result = true;
  183. } while(false);
  184. return update_result;
  185. }
  186. bool totp_config_file_update_automation_method(const PluginState* plugin_state) {
  187. FlipperFormat* file = plugin_state->config_file_context->config_file;
  188. flipper_format_rewind(file);
  189. bool update_result = false;
  190. do {
  191. uint32_t tmp_uint32 = plugin_state->automation_method;
  192. if(!flipper_format_insert_or_update_uint32(
  193. file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1)) {
  194. break;
  195. }
  196. update_result = true;
  197. } while(false);
  198. return update_result;
  199. }
  200. bool totp_config_file_update_user_settings(const PluginState* plugin_state) {
  201. FlipperFormat* file = plugin_state->config_file_context->config_file;
  202. flipper_format_rewind(file);
  203. bool update_result = false;
  204. do {
  205. if(!flipper_format_insert_or_update_float(
  206. file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  207. break;
  208. }
  209. uint32_t tmp_uint32 = plugin_state->notification_method;
  210. if(!flipper_format_insert_or_update_uint32(
  211. file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  212. break;
  213. }
  214. tmp_uint32 = plugin_state->automation_method;
  215. if(!flipper_format_insert_or_update_uint32(
  216. file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1)) {
  217. break;
  218. }
  219. tmp_uint32 = plugin_state->active_font_index;
  220. if(!flipper_format_insert_or_update_uint32(file, TOTP_CONFIG_KEY_FONT, &tmp_uint32, 1)) {
  221. break;
  222. }
  223. update_result = true;
  224. } while(false);
  225. return update_result;
  226. }
  227. bool totp_config_file_load(PluginState* const plugin_state) {
  228. Storage* storage = totp_open_storage();
  229. FlipperFormat* fff_data_file;
  230. if(!totp_open_config_file(storage, &fff_data_file)) {
  231. totp_close_storage();
  232. return false;
  233. }
  234. flipper_format_rewind(fff_data_file);
  235. bool result = false;
  236. plugin_state->timezone_offset = 0;
  237. FuriString* temp_str = furi_string_alloc();
  238. do {
  239. uint32_t file_version;
  240. if(!flipper_format_read_header(fff_data_file, temp_str, &file_version)) {
  241. FURI_LOG_E(LOGGING_TAG, "Missing or incorrect header");
  242. break;
  243. }
  244. if(file_version < CONFIG_FILE_ACTUAL_VERSION) {
  245. FURI_LOG_I(
  246. LOGGING_TAG,
  247. "Obsolete config file version detected. Current version: %" PRIu32
  248. "; Actual version: %" PRId16,
  249. file_version,
  250. CONFIG_FILE_ACTUAL_VERSION);
  251. totp_close_config_file(fff_data_file);
  252. char* backup_path = totp_config_file_backup_i(storage);
  253. if(backup_path != NULL) {
  254. if(totp_open_config_file(storage, &fff_data_file) != true) {
  255. break;
  256. }
  257. FlipperFormat* fff_backup_data_file = flipper_format_file_alloc(storage);
  258. if(!flipper_format_file_open_existing(fff_backup_data_file, backup_path)) {
  259. flipper_format_file_close(fff_backup_data_file);
  260. flipper_format_free(fff_backup_data_file);
  261. break;
  262. }
  263. if(totp_config_migrate_to_latest(fff_data_file, fff_backup_data_file)) {
  264. FURI_LOG_I(
  265. LOGGING_TAG,
  266. "Applied migration to version %" PRId16,
  267. CONFIG_FILE_ACTUAL_VERSION);
  268. file_version = CONFIG_FILE_ACTUAL_VERSION;
  269. } else {
  270. FURI_LOG_W(
  271. LOGGING_TAG,
  272. "An error occurred during migration to version %" PRId16,
  273. CONFIG_FILE_ACTUAL_VERSION);
  274. break;
  275. }
  276. flipper_format_file_close(fff_backup_data_file);
  277. flipper_format_free(fff_backup_data_file);
  278. flipper_format_rewind(fff_data_file);
  279. free(backup_path);
  280. } else {
  281. FURI_LOG_E(
  282. LOGGING_TAG,
  283. "An error occurred during taking backup of %s before migration",
  284. CONFIG_FILE_PATH);
  285. break;
  286. }
  287. }
  288. if(!flipper_format_read_hex(
  289. fff_data_file, TOTP_CONFIG_KEY_BASE_IV, &plugin_state->base_iv[0], TOTP_IV_SIZE)) {
  290. FURI_LOG_D(LOGGING_TAG, "Missing base IV");
  291. }
  292. if(!flipper_format_rewind(fff_data_file)) {
  293. break;
  294. }
  295. uint32_t crypto_size;
  296. if(flipper_format_get_value_count(
  297. fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, &crypto_size) &&
  298. crypto_size > 0) {
  299. plugin_state->crypto_verify_data = malloc(sizeof(uint8_t) * crypto_size);
  300. furi_check(plugin_state->crypto_verify_data != NULL);
  301. plugin_state->crypto_verify_data_length = crypto_size;
  302. if(!flipper_format_read_hex(
  303. fff_data_file,
  304. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  305. plugin_state->crypto_verify_data,
  306. crypto_size)) {
  307. FURI_LOG_D(LOGGING_TAG, "Missing crypto verify token");
  308. free(plugin_state->crypto_verify_data);
  309. plugin_state->crypto_verify_data = NULL;
  310. plugin_state->crypto_verify_data_length = 0;
  311. }
  312. } else {
  313. plugin_state->crypto_verify_data = NULL;
  314. plugin_state->crypto_verify_data_length = 0;
  315. }
  316. if(!flipper_format_rewind(fff_data_file)) {
  317. break;
  318. }
  319. if(!flipper_format_read_float(
  320. fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  321. plugin_state->timezone_offset = 0;
  322. FURI_LOG_D(LOGGING_TAG, "Missing timezone offset information, defaulting to 0");
  323. }
  324. if(!flipper_format_rewind(fff_data_file)) {
  325. break;
  326. }
  327. if(!flipper_format_read_bool(
  328. fff_data_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1)) {
  329. plugin_state->pin_set = true;
  330. }
  331. flipper_format_rewind(fff_data_file);
  332. uint32_t tmp_uint32;
  333. if(!flipper_format_read_uint32(
  334. fff_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  335. tmp_uint32 = NotificationMethodSound | NotificationMethodVibro;
  336. }
  337. plugin_state->notification_method = tmp_uint32;
  338. flipper_format_rewind(fff_data_file);
  339. if(!flipper_format_read_uint32(
  340. fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1)) {
  341. tmp_uint32 = AutomationMethodBadUsb;
  342. }
  343. plugin_state->automation_method = tmp_uint32;
  344. if(!flipper_format_read_uint32(fff_data_file, TOTP_CONFIG_KEY_FONT, &tmp_uint32, 1)) {
  345. tmp_uint32 = 0;
  346. }
  347. plugin_state->active_font_index = tmp_uint32;
  348. plugin_state->config_file_context = malloc(sizeof(ConfigFileContext));
  349. furi_check(plugin_state->config_file_context != NULL);
  350. plugin_state->config_file_context->storage = storage;
  351. plugin_state->config_file_context->config_file = fff_data_file;
  352. plugin_state->config_file_context->token_info_iterator_context =
  353. totp_token_info_iterator_alloc(
  354. storage, plugin_state->config_file_context->config_file, plugin_state->iv);
  355. result = true;
  356. } while(false);
  357. furi_string_free(temp_str);
  358. return result;
  359. }
  360. bool totp_config_file_update_crypto_signatures(const PluginState* plugin_state) {
  361. FlipperFormat* config_file = plugin_state->config_file_context->config_file;
  362. flipper_format_rewind(config_file);
  363. bool update_result = false;
  364. do {
  365. if(!flipper_format_insert_or_update_hex(
  366. config_file, TOTP_CONFIG_KEY_BASE_IV, plugin_state->base_iv, TOTP_IV_SIZE)) {
  367. break;
  368. }
  369. if(!flipper_format_insert_or_update_hex(
  370. config_file,
  371. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  372. plugin_state->crypto_verify_data,
  373. plugin_state->crypto_verify_data_length)) {
  374. break;
  375. }
  376. if(!flipper_format_insert_or_update_bool(
  377. config_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1)) {
  378. break;
  379. }
  380. update_result = true;
  381. } while(false);
  382. return update_result;
  383. }
  384. void totp_config_file_close(PluginState* const plugin_state) {
  385. if(plugin_state->config_file_context == NULL) return;
  386. totp_token_info_iterator_free(plugin_state->config_file_context->token_info_iterator_context);
  387. totp_close_config_file(plugin_state->config_file_context->config_file);
  388. free(plugin_state->config_file_context);
  389. plugin_state->config_file_context = NULL;
  390. totp_close_storage();
  391. }
  392. void totp_config_file_reset(PluginState* const plugin_state) {
  393. totp_config_file_close(plugin_state);
  394. Storage* storage = totp_open_storage();
  395. storage_simply_remove(storage, CONFIG_FILE_PATH);
  396. totp_close_storage();
  397. }
  398. bool totp_config_file_update_encryption(
  399. PluginState* plugin_state,
  400. const uint8_t* new_pin,
  401. uint8_t new_pin_length) {
  402. FlipperFormat* config_file = plugin_state->config_file_context->config_file;
  403. Stream* stream = flipper_format_get_raw_stream(config_file);
  404. size_t original_offset = stream_tell(stream);
  405. if(!stream_rewind(stream)) {
  406. return false;
  407. }
  408. uint8_t old_iv[TOTP_IV_SIZE];
  409. memcpy(&old_iv[0], &plugin_state->iv[0], TOTP_IV_SIZE);
  410. memset(&plugin_state->iv[0], 0, TOTP_IV_SIZE);
  411. memset(&plugin_state->base_iv[0], 0, TOTP_IV_SIZE);
  412. if(plugin_state->crypto_verify_data != NULL) {
  413. free(plugin_state->crypto_verify_data);
  414. plugin_state->crypto_verify_data = NULL;
  415. }
  416. CryptoSeedIVResult seed_result =
  417. totp_crypto_seed_iv(plugin_state, new_pin_length > 0 ? new_pin : NULL, new_pin_length);
  418. if(seed_result & CryptoSeedIVResultFlagSuccess &&
  419. seed_result & CryptoSeedIVResultFlagNewCryptoVerifyData) {
  420. if(!totp_config_file_update_crypto_signatures(plugin_state)) {
  421. return false;
  422. }
  423. } else if(seed_result == CryptoSeedIVResultFailed) {
  424. return false;
  425. }
  426. char buffer[sizeof(TOTP_CONFIG_KEY_TOKEN_SECRET) + 1];
  427. bool result = true;
  428. while(true) {
  429. if(!stream_seek_to_char(stream, '\n', StreamDirectionForward)) {
  430. break;
  431. }
  432. size_t buffer_read_size;
  433. if((buffer_read_size = stream_read(stream, (uint8_t*)&buffer[0], sizeof(buffer))) == 0) {
  434. break;
  435. }
  436. if(!stream_seek(stream, -(int32_t)buffer_read_size, StreamOffsetFromCurrent)) {
  437. result = false;
  438. break;
  439. }
  440. if(strncmp(buffer, "\n" TOTP_CONFIG_KEY_TOKEN_SECRET ":", sizeof(buffer)) == 0) {
  441. uint32_t secret_bytes_count;
  442. if(!flipper_format_get_value_count(
  443. config_file, TOTP_CONFIG_KEY_TOKEN_SECRET, &secret_bytes_count)) {
  444. secret_bytes_count = 0;
  445. }
  446. if(secret_bytes_count > 1) {
  447. size_t secret_token_start = stream_tell(stream) + 1;
  448. uint8_t* encrypted_token = malloc(secret_bytes_count);
  449. furi_check(encrypted_token != NULL);
  450. if(!flipper_format_read_hex(
  451. config_file,
  452. TOTP_CONFIG_KEY_TOKEN_SECRET,
  453. encrypted_token,
  454. secret_bytes_count)) {
  455. result = false;
  456. free(encrypted_token);
  457. break;
  458. }
  459. size_t plain_token_length;
  460. uint8_t* plain_token = totp_crypto_decrypt(
  461. encrypted_token, secret_bytes_count, &old_iv[0], &plain_token_length);
  462. free(encrypted_token);
  463. size_t encrypted_token_length;
  464. encrypted_token = totp_crypto_encrypt(
  465. plain_token, plain_token_length, &plugin_state->iv[0], &encrypted_token_length);
  466. memset_s(plain_token, plain_token_length, 0, plain_token_length);
  467. free(plain_token);
  468. if(!stream_seek(stream, secret_token_start, StreamOffsetFromStart)) {
  469. result = false;
  470. free(encrypted_token);
  471. break;
  472. }
  473. if(!flipper_format_write_hex(
  474. config_file,
  475. TOTP_CONFIG_KEY_TOKEN_SECRET,
  476. encrypted_token,
  477. encrypted_token_length)) {
  478. free(encrypted_token);
  479. result = false;
  480. break;
  481. }
  482. free(encrypted_token);
  483. }
  484. }
  485. }
  486. stream_seek(stream, original_offset, StreamOffsetFromStart);
  487. return result;
  488. }
  489. TokenInfoIteratorContext* totp_config_get_token_iterator_context(const PluginState* plugin_state) {
  490. return plugin_state->config_file_context->token_info_iterator_context;
  491. }