common_migration.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "common_migration.h"
  2. #include "../constants.h"
  3. #include "../../../types/token_info.h"
  4. #include <flipper_format/flipper_format_i.h>
  5. bool totp_config_migrate_to_latest(
  6. FlipperFormat* fff_data_file,
  7. FlipperFormat* fff_backup_data_file) {
  8. FuriString* temp_str = furi_string_alloc();
  9. uint32_t current_version = 0;
  10. bool result = false;
  11. do {
  12. flipper_format_write_header_cstr(
  13. fff_data_file, CONFIG_FILE_HEADER, CONFIG_FILE_ACTUAL_VERSION);
  14. if(!flipper_format_read_header(fff_backup_data_file, temp_str, &current_version)) {
  15. break;
  16. }
  17. if(flipper_format_read_string(fff_backup_data_file, TOTP_CONFIG_KEY_BASE_IV, temp_str)) {
  18. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_BASE_IV, temp_str);
  19. }
  20. flipper_format_rewind(fff_backup_data_file);
  21. if(flipper_format_read_string(
  22. fff_backup_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, temp_str)) {
  23. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, temp_str);
  24. }
  25. flipper_format_rewind(fff_backup_data_file);
  26. if(flipper_format_read_string(fff_backup_data_file, TOTP_CONFIG_KEY_TIMEZONE, temp_str)) {
  27. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, temp_str);
  28. }
  29. flipper_format_rewind(fff_backup_data_file);
  30. if(flipper_format_read_string(fff_backup_data_file, TOTP_CONFIG_KEY_PINSET, temp_str)) {
  31. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_PINSET, temp_str);
  32. }
  33. flipper_format_rewind(fff_backup_data_file);
  34. if(flipper_format_read_string(
  35. fff_backup_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, temp_str)) {
  36. flipper_format_write_string(
  37. fff_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, temp_str);
  38. }
  39. flipper_format_rewind(fff_backup_data_file);
  40. if(flipper_format_read_string(
  41. fff_backup_data_file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, temp_str)) {
  42. flipper_format_write_string(
  43. fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, temp_str);
  44. }
  45. flipper_format_rewind(fff_backup_data_file);
  46. if(flipper_format_read_string(fff_backup_data_file, TOTP_CONFIG_KEY_FONT, temp_str)) {
  47. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_FONT, temp_str);
  48. } else {
  49. uint32_t default_font_index = 0;
  50. flipper_format_write_uint32(
  51. fff_data_file, TOTP_CONFIG_KEY_FONT, &default_font_index, 1);
  52. }
  53. flipper_format_rewind(fff_backup_data_file);
  54. while(true) {
  55. if(!flipper_format_read_string(
  56. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_NAME, temp_str)) {
  57. break;
  58. }
  59. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_NAME, temp_str);
  60. flipper_format_read_string(
  61. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str);
  62. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str);
  63. if(current_version > 1) {
  64. flipper_format_read_string(
  65. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str);
  66. if(current_version < 5) {
  67. uint32_t algo_as_uint32t = TokenHashAlgoDefault;
  68. if(furi_string_cmpi_str(temp_str, TOKEN_HASH_ALGO_SHA256_NAME) == 0) {
  69. algo_as_uint32t = TokenHashAlgoSha256;
  70. } else if(furi_string_cmpi_str(temp_str, TOKEN_HASH_ALGO_SHA512_NAME) == 0) {
  71. algo_as_uint32t = TokenHashAlgoSha512;
  72. } else if(furi_string_cmpi_str(temp_str, TOKEN_HASH_ALGO_STEAM_NAME) == 0) {
  73. algo_as_uint32t = TokenHashAlgoSteam;
  74. }
  75. flipper_format_write_uint32(
  76. fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, &algo_as_uint32t, 1);
  77. } else {
  78. flipper_format_write_string(
  79. fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str);
  80. }
  81. flipper_format_read_string(
  82. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, temp_str);
  83. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, temp_str);
  84. } else {
  85. const uint32_t default_algo = TokenHashAlgoDefault;
  86. flipper_format_write_uint32(
  87. fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, &default_algo, 1);
  88. const uint32_t default_digits = TokenDigitsCountSix;
  89. flipper_format_write_uint32(
  90. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &default_digits, 1);
  91. }
  92. if(current_version > 2) {
  93. flipper_format_read_string(
  94. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, temp_str);
  95. flipper_format_write_string(
  96. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, temp_str);
  97. } else {
  98. const uint32_t default_duration = TokenDurationDefault;
  99. flipper_format_write_uint32(
  100. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, &default_duration, 1);
  101. }
  102. if(current_version > 3) {
  103. flipper_format_read_string(
  104. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES, temp_str);
  105. flipper_format_write_string(
  106. fff_data_file, TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES, temp_str);
  107. } else {
  108. const uint32_t default_automation_features = TokenAutomationFeatureNone;
  109. flipper_format_write_uint32(
  110. fff_data_file,
  111. TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES,
  112. &default_automation_features,
  113. 1);
  114. }
  115. }
  116. Stream* stream = flipper_format_get_raw_stream(fff_data_file);
  117. size_t current_pos = stream_tell(stream);
  118. size_t total_size = stream_size(stream);
  119. if(current_pos < total_size) {
  120. stream_delete(stream, total_size - current_pos);
  121. }
  122. result = true;
  123. } while(false);
  124. furi_string_free(temp_str);
  125. return result;
  126. }