common_migration.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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(
  18. fff_backup_data_file, TOTP_CONFIG_KEY_CRYPTO_VERSION, temp_str)) {
  19. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERSION, temp_str);
  20. } else {
  21. uint32_t old_crypto_version = 1;
  22. flipper_format_write_uint32(
  23. fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERSION, &old_crypto_version, 1);
  24. }
  25. flipper_format_rewind(fff_backup_data_file);
  26. if(flipper_format_read_string(
  27. fff_backup_data_file, TOTP_CONFIG_KEY_CRYPTO_KEY_SLOT, temp_str)) {
  28. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_KEY_SLOT, temp_str);
  29. } else {
  30. uint32_t default_old_key_slot = 2;
  31. flipper_format_write_uint32(
  32. fff_data_file, TOTP_CONFIG_KEY_CRYPTO_KEY_SLOT, &default_old_key_slot, 1);
  33. }
  34. flipper_format_rewind(fff_backup_data_file);
  35. if(flipper_format_read_string(fff_backup_data_file, TOTP_CONFIG_KEY_BASE_IV, temp_str)) {
  36. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_BASE_IV, temp_str);
  37. }
  38. flipper_format_rewind(fff_backup_data_file);
  39. if(flipper_format_read_string(
  40. fff_backup_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, temp_str)) {
  41. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, temp_str);
  42. }
  43. flipper_format_rewind(fff_backup_data_file);
  44. if(flipper_format_read_string(fff_backup_data_file, TOTP_CONFIG_KEY_TIMEZONE, temp_str)) {
  45. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, temp_str);
  46. }
  47. flipper_format_rewind(fff_backup_data_file);
  48. if(flipper_format_read_string(fff_backup_data_file, TOTP_CONFIG_KEY_PINSET, temp_str)) {
  49. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_PINSET, temp_str);
  50. }
  51. flipper_format_rewind(fff_backup_data_file);
  52. if(flipper_format_read_string(
  53. fff_backup_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, temp_str)) {
  54. flipper_format_write_string(
  55. fff_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, temp_str);
  56. }
  57. flipper_format_rewind(fff_backup_data_file);
  58. if(flipper_format_read_string(
  59. fff_backup_data_file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, temp_str)) {
  60. flipper_format_write_string(
  61. fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, temp_str);
  62. }
  63. flipper_format_rewind(fff_backup_data_file);
  64. if(flipper_format_read_string(fff_backup_data_file, TOTP_CONFIG_KEY_FONT, temp_str)) {
  65. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_FONT, temp_str);
  66. } else {
  67. uint32_t default_font_index = 0;
  68. flipper_format_write_uint32(
  69. fff_data_file, TOTP_CONFIG_KEY_FONT, &default_font_index, 1);
  70. }
  71. flipper_format_rewind(fff_backup_data_file);
  72. while(true) {
  73. if(!flipper_format_read_string(
  74. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_NAME, temp_str)) {
  75. break;
  76. }
  77. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_NAME, temp_str);
  78. flipper_format_read_string(
  79. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str);
  80. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str);
  81. if(current_version > 1) {
  82. flipper_format_read_string(
  83. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str);
  84. if(current_version < 5) {
  85. uint32_t algo_as_uint32t = TokenHashAlgoDefault;
  86. if(furi_string_cmpi_str(temp_str, TOKEN_HASH_ALGO_SHA256_NAME) == 0) {
  87. algo_as_uint32t = TokenHashAlgoSha256;
  88. } else if(furi_string_cmpi_str(temp_str, TOKEN_HASH_ALGO_SHA512_NAME) == 0) {
  89. algo_as_uint32t = TokenHashAlgoSha512;
  90. } else if(furi_string_cmpi_str(temp_str, TOKEN_HASH_ALGO_STEAM_NAME) == 0) {
  91. algo_as_uint32t = TokenHashAlgoSteam;
  92. }
  93. flipper_format_write_uint32(
  94. fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, &algo_as_uint32t, 1);
  95. } else {
  96. flipper_format_write_string(
  97. fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str);
  98. }
  99. flipper_format_read_string(
  100. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, temp_str);
  101. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, temp_str);
  102. } else {
  103. const uint32_t default_algo = TokenHashAlgoDefault;
  104. flipper_format_write_uint32(
  105. fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, &default_algo, 1);
  106. const uint32_t default_digits = TokenDigitsCountSix;
  107. flipper_format_write_uint32(
  108. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &default_digits, 1);
  109. }
  110. if(current_version > 2) {
  111. flipper_format_read_string(
  112. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, temp_str);
  113. flipper_format_write_string(
  114. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, temp_str);
  115. } else {
  116. const uint32_t default_duration = TokenDurationDefault;
  117. flipper_format_write_uint32(
  118. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, &default_duration, 1);
  119. }
  120. if(current_version > 3) {
  121. flipper_format_read_string(
  122. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES, temp_str);
  123. flipper_format_write_string(
  124. fff_data_file, TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES, temp_str);
  125. } else {
  126. const uint32_t default_automation_features = TokenAutomationFeatureNone;
  127. flipper_format_write_uint32(
  128. fff_data_file,
  129. TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES,
  130. &default_automation_features,
  131. 1);
  132. }
  133. }
  134. Stream* stream = flipper_format_get_raw_stream(fff_data_file);
  135. size_t current_pos = stream_tell(stream);
  136. size_t total_size = stream_size(stream);
  137. if(current_pos < total_size) {
  138. stream_delete(stream, total_size - current_pos);
  139. }
  140. result = true;
  141. } while(false);
  142. furi_string_free(temp_str);
  143. return result;
  144. }