common_migration.c 7.9 KB

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