config.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. if(!flipper_format_rewind(fff_data_file)) {
  137. totp_close_config_file(fff_data_file);
  138. FURI_LOG_E(LOGGING_TAG, "Rewind error");
  139. return false;
  140. }
  141. }
  142. *file = fff_data_file;
  143. return true;
  144. }
  145. char* totp_config_file_backup(const PluginState* plugin_state) {
  146. if(plugin_state->config_file_context == NULL) return NULL;
  147. totp_close_config_file(plugin_state->config_file_context->config_file);
  148. char* result = totp_config_file_backup_i(plugin_state->config_file_context->storage);
  149. totp_open_config_file(
  150. plugin_state->config_file_context->storage,
  151. &plugin_state->config_file_context->config_file);
  152. totp_token_info_iterator_attach_to_config_file(
  153. plugin_state->config_file_context->token_info_iterator_context,
  154. plugin_state->config_file_context->config_file);
  155. return result;
  156. }
  157. bool totp_config_file_update_timezone_offset(const PluginState* plugin_state) {
  158. FlipperFormat* file = plugin_state->config_file_context->config_file;
  159. flipper_format_rewind(file);
  160. bool update_result = false;
  161. do {
  162. if(!flipper_format_insert_or_update_float(
  163. file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  164. break;
  165. }
  166. update_result = true;
  167. } while(false);
  168. return update_result;
  169. }
  170. bool totp_config_file_update_notification_method(const PluginState* plugin_state) {
  171. FlipperFormat* file = plugin_state->config_file_context->config_file;
  172. flipper_format_rewind(file);
  173. bool update_result = false;
  174. do {
  175. uint32_t tmp_uint32 = plugin_state->notification_method;
  176. if(!flipper_format_insert_or_update_uint32(
  177. file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  178. break;
  179. }
  180. update_result = true;
  181. } while(false);
  182. return update_result;
  183. }
  184. bool totp_config_file_update_automation_method(const PluginState* plugin_state) {
  185. FlipperFormat* file = plugin_state->config_file_context->config_file;
  186. flipper_format_rewind(file);
  187. bool update_result = false;
  188. do {
  189. uint32_t tmp_uint32 = plugin_state->automation_method;
  190. if(!flipper_format_insert_or_update_uint32(
  191. file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1)) {
  192. break;
  193. }
  194. update_result = true;
  195. } while(false);
  196. return update_result;
  197. }
  198. bool totp_config_file_update_user_settings(const PluginState* plugin_state) {
  199. FlipperFormat* file = plugin_state->config_file_context->config_file;
  200. flipper_format_rewind(file);
  201. bool update_result = false;
  202. do {
  203. if(!flipper_format_insert_or_update_float(
  204. file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  205. break;
  206. }
  207. uint32_t tmp_uint32 = plugin_state->notification_method;
  208. if(!flipper_format_insert_or_update_uint32(
  209. file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  210. break;
  211. }
  212. 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. update_result = true;
  218. } while(false);
  219. return update_result;
  220. }
  221. bool totp_config_file_load(PluginState* const plugin_state) {
  222. Storage* storage = totp_open_storage();
  223. FlipperFormat* fff_data_file;
  224. if(!totp_open_config_file(storage, &fff_data_file)) {
  225. totp_close_storage();
  226. return false;
  227. }
  228. flipper_format_rewind(fff_data_file);
  229. bool result = false;
  230. plugin_state->timezone_offset = 0;
  231. FuriString* temp_str = furi_string_alloc();
  232. do {
  233. uint32_t file_version;
  234. if(!flipper_format_read_header(fff_data_file, temp_str, &file_version)) {
  235. FURI_LOG_E(LOGGING_TAG, "Missing or incorrect header");
  236. break;
  237. }
  238. if(file_version < CONFIG_FILE_ACTUAL_VERSION) {
  239. FURI_LOG_I(
  240. LOGGING_TAG,
  241. "Obsolete config file version detected. Current version: %" PRIu32
  242. "; Actual version: %" PRId16,
  243. file_version,
  244. CONFIG_FILE_ACTUAL_VERSION);
  245. totp_close_config_file(fff_data_file);
  246. char* backup_path = totp_config_file_backup_i(storage);
  247. if(backup_path != NULL) {
  248. if(totp_open_config_file(storage, &fff_data_file) != true) {
  249. break;
  250. }
  251. FlipperFormat* fff_backup_data_file = flipper_format_file_alloc(storage);
  252. if(!flipper_format_file_open_existing(fff_backup_data_file, backup_path)) {
  253. flipper_format_file_close(fff_backup_data_file);
  254. flipper_format_free(fff_backup_data_file);
  255. break;
  256. }
  257. if(totp_config_migrate_to_latest(fff_data_file, fff_backup_data_file)) {
  258. FURI_LOG_I(
  259. LOGGING_TAG,
  260. "Applied migration to version %" PRId16,
  261. CONFIG_FILE_ACTUAL_VERSION);
  262. file_version = CONFIG_FILE_ACTUAL_VERSION;
  263. } else {
  264. FURI_LOG_W(
  265. LOGGING_TAG,
  266. "An error occurred during migration to version %" PRId16,
  267. CONFIG_FILE_ACTUAL_VERSION);
  268. break;
  269. }
  270. flipper_format_file_close(fff_backup_data_file);
  271. flipper_format_free(fff_backup_data_file);
  272. flipper_format_rewind(fff_data_file);
  273. free(backup_path);
  274. } else {
  275. FURI_LOG_E(
  276. LOGGING_TAG,
  277. "An error occurred during taking backup of %s before migration",
  278. CONFIG_FILE_PATH);
  279. break;
  280. }
  281. }
  282. if(!flipper_format_read_hex(
  283. fff_data_file, TOTP_CONFIG_KEY_BASE_IV, &plugin_state->base_iv[0], TOTP_IV_SIZE)) {
  284. FURI_LOG_D(LOGGING_TAG, "Missing base IV");
  285. }
  286. if(!flipper_format_rewind(fff_data_file)) {
  287. break;
  288. }
  289. uint32_t crypto_size;
  290. if(flipper_format_get_value_count(
  291. fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, &crypto_size) &&
  292. crypto_size > 0) {
  293. plugin_state->crypto_verify_data = malloc(sizeof(uint8_t) * crypto_size);
  294. furi_check(plugin_state->crypto_verify_data != NULL);
  295. plugin_state->crypto_verify_data_length = crypto_size;
  296. if(!flipper_format_read_hex(
  297. fff_data_file,
  298. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  299. plugin_state->crypto_verify_data,
  300. crypto_size)) {
  301. FURI_LOG_D(LOGGING_TAG, "Missing crypto verify token");
  302. free(plugin_state->crypto_verify_data);
  303. plugin_state->crypto_verify_data = NULL;
  304. plugin_state->crypto_verify_data_length = 0;
  305. }
  306. } else {
  307. plugin_state->crypto_verify_data = NULL;
  308. plugin_state->crypto_verify_data_length = 0;
  309. }
  310. if(!flipper_format_rewind(fff_data_file)) {
  311. break;
  312. }
  313. if(!flipper_format_read_float(
  314. fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  315. plugin_state->timezone_offset = 0;
  316. FURI_LOG_D(LOGGING_TAG, "Missing timezone offset information, defaulting to 0");
  317. }
  318. if(!flipper_format_rewind(fff_data_file)) {
  319. break;
  320. }
  321. if(!flipper_format_read_bool(
  322. fff_data_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1)) {
  323. plugin_state->pin_set = true;
  324. }
  325. flipper_format_rewind(fff_data_file);
  326. uint32_t tmp_uint32;
  327. if(!flipper_format_read_uint32(
  328. fff_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  329. tmp_uint32 = NotificationMethodSound | NotificationMethodVibro;
  330. }
  331. plugin_state->notification_method = tmp_uint32;
  332. flipper_format_rewind(fff_data_file);
  333. if(!flipper_format_read_uint32(
  334. fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, &tmp_uint32, 1)) {
  335. tmp_uint32 = AutomationMethodBadUsb;
  336. }
  337. plugin_state->automation_method = tmp_uint32;
  338. plugin_state->config_file_context = malloc(sizeof(ConfigFileContext));
  339. furi_check(plugin_state->config_file_context != NULL);
  340. plugin_state->config_file_context->storage = storage;
  341. plugin_state->config_file_context->config_file = fff_data_file;
  342. plugin_state->config_file_context->token_info_iterator_context =
  343. totp_token_info_iterator_alloc(
  344. storage, plugin_state->config_file_context->config_file, plugin_state->iv);
  345. result = true;
  346. } while(false);
  347. furi_string_free(temp_str);
  348. return result;
  349. }
  350. bool totp_config_file_update_crypto_signatures(const PluginState* plugin_state) {
  351. FlipperFormat* config_file = plugin_state->config_file_context->config_file;
  352. flipper_format_rewind(config_file);
  353. bool update_result = false;
  354. do {
  355. if(!flipper_format_insert_or_update_hex(
  356. config_file, TOTP_CONFIG_KEY_BASE_IV, plugin_state->base_iv, TOTP_IV_SIZE)) {
  357. break;
  358. }
  359. if(!flipper_format_insert_or_update_hex(
  360. config_file,
  361. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  362. plugin_state->crypto_verify_data,
  363. plugin_state->crypto_verify_data_length)) {
  364. break;
  365. }
  366. if(!flipper_format_insert_or_update_bool(
  367. config_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1)) {
  368. break;
  369. }
  370. update_result = true;
  371. } while(false);
  372. return update_result;
  373. }
  374. void totp_config_file_close(PluginState* const plugin_state) {
  375. if(plugin_state->config_file_context == NULL) return;
  376. totp_token_info_iterator_free(plugin_state->config_file_context->token_info_iterator_context);
  377. totp_close_config_file(plugin_state->config_file_context->config_file);
  378. free(plugin_state->config_file_context);
  379. plugin_state->config_file_context = NULL;
  380. totp_close_storage();
  381. }
  382. void totp_config_file_reset(PluginState* const plugin_state) {
  383. totp_config_file_close(plugin_state);
  384. Storage* storage = totp_open_storage();
  385. storage_simply_remove(storage, CONFIG_FILE_PATH);
  386. totp_close_storage();
  387. }
  388. bool totp_config_file_update_encryption(
  389. PluginState* plugin_state,
  390. const uint8_t* new_pin,
  391. uint8_t new_pin_length) {
  392. FlipperFormat* config_file = plugin_state->config_file_context->config_file;
  393. Stream* stream = flipper_format_get_raw_stream(config_file);
  394. size_t original_offset = stream_tell(stream);
  395. if(!stream_rewind(stream)) {
  396. return false;
  397. }
  398. uint8_t old_iv[TOTP_IV_SIZE];
  399. memcpy(&old_iv[0], &plugin_state->iv[0], TOTP_IV_SIZE);
  400. memset(&plugin_state->iv[0], 0, TOTP_IV_SIZE);
  401. memset(&plugin_state->base_iv[0], 0, TOTP_IV_SIZE);
  402. if(plugin_state->crypto_verify_data != NULL) {
  403. free(plugin_state->crypto_verify_data);
  404. plugin_state->crypto_verify_data = NULL;
  405. }
  406. CryptoSeedIVResult seed_result =
  407. totp_crypto_seed_iv(plugin_state, new_pin_length > 0 ? new_pin : NULL, new_pin_length);
  408. if(seed_result & CryptoSeedIVResultFlagSuccess &&
  409. seed_result & CryptoSeedIVResultFlagNewCryptoVerifyData) {
  410. if(!totp_config_file_update_crypto_signatures(plugin_state)) {
  411. return false;
  412. }
  413. } else if(seed_result == CryptoSeedIVResultFailed) {
  414. return false;
  415. }
  416. char buffer[sizeof(TOTP_CONFIG_KEY_TOKEN_SECRET) + 1];
  417. bool result = true;
  418. while(true) {
  419. if(!stream_seek_to_char(stream, '\n', StreamDirectionForward)) {
  420. break;
  421. }
  422. size_t buffer_read_size;
  423. if((buffer_read_size = stream_read(stream, (uint8_t*)&buffer[0], sizeof(buffer))) == 0) {
  424. break;
  425. }
  426. if(!stream_seek(stream, -(int32_t)buffer_read_size, StreamOffsetFromCurrent)) {
  427. result = false;
  428. break;
  429. }
  430. if(strncmp(buffer, "\n" TOTP_CONFIG_KEY_TOKEN_SECRET ":", sizeof(buffer)) == 0) {
  431. uint32_t secret_bytes_count;
  432. if(!flipper_format_get_value_count(
  433. config_file, TOTP_CONFIG_KEY_TOKEN_SECRET, &secret_bytes_count)) {
  434. secret_bytes_count = 0;
  435. }
  436. if(secret_bytes_count > 1) {
  437. size_t secret_token_start = stream_tell(stream) + 1;
  438. uint8_t* encrypted_token = malloc(secret_bytes_count);
  439. furi_check(encrypted_token != NULL);
  440. if(!flipper_format_read_hex(
  441. config_file,
  442. TOTP_CONFIG_KEY_TOKEN_SECRET,
  443. encrypted_token,
  444. secret_bytes_count)) {
  445. result = false;
  446. free(encrypted_token);
  447. break;
  448. }
  449. size_t plain_token_length;
  450. uint8_t* plain_token = totp_crypto_decrypt(
  451. encrypted_token, secret_bytes_count, &old_iv[0], &plain_token_length);
  452. free(encrypted_token);
  453. size_t encrypted_token_length;
  454. encrypted_token = totp_crypto_encrypt(
  455. plain_token, plain_token_length, &plugin_state->iv[0], &encrypted_token_length);
  456. memset_s(plain_token, plain_token_length, 0, plain_token_length);
  457. free(plain_token);
  458. if(!stream_seek(stream, secret_token_start, StreamOffsetFromStart)) {
  459. result = false;
  460. free(encrypted_token);
  461. break;
  462. }
  463. if(!flipper_format_write_hex(
  464. config_file,
  465. TOTP_CONFIG_KEY_TOKEN_SECRET,
  466. encrypted_token,
  467. encrypted_token_length)) {
  468. free(encrypted_token);
  469. result = false;
  470. break;
  471. }
  472. free(encrypted_token);
  473. }
  474. }
  475. }
  476. stream_seek(stream, original_offset, StreamOffsetFromStart);
  477. return result;
  478. }
  479. TokenInfoIteratorContext* totp_config_get_token_iterator_context(const PluginState* plugin_state) {
  480. return plugin_state->config_file_context->token_info_iterator_context;
  481. }