common_migration.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. while(true) {
  47. if(!flipper_format_read_string(
  48. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_NAME, temp_str)) {
  49. break;
  50. }
  51. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_NAME, temp_str);
  52. flipper_format_read_string(
  53. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str);
  54. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str);
  55. if(current_version > 1) {
  56. flipper_format_read_string(
  57. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str);
  58. if(current_version < 5) {
  59. uint32_t algo_as_uint32t = SHA1;
  60. if(furi_string_cmpi_str(temp_str, TOTP_TOKEN_ALGO_SHA256_NAME) == 0) {
  61. algo_as_uint32t = SHA256;
  62. } else if(furi_string_cmpi_str(temp_str, TOTP_TOKEN_ALGO_SHA512_NAME) == 0) {
  63. algo_as_uint32t = SHA512;
  64. } else if(furi_string_cmpi_str(temp_str, TOTP_TOKEN_ALGO_STEAM_NAME) == 0) {
  65. algo_as_uint32t = STEAM;
  66. }
  67. flipper_format_write_uint32(
  68. fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, &algo_as_uint32t, 1);
  69. } else {
  70. flipper_format_write_string(
  71. fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str);
  72. }
  73. flipper_format_read_string(
  74. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, temp_str);
  75. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, temp_str);
  76. } else {
  77. const uint32_t default_algo = SHA1;
  78. flipper_format_write_uint32(
  79. fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, &default_algo, 1);
  80. const uint32_t default_digits = TotpSixDigitsCount;
  81. flipper_format_write_uint32(
  82. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &default_digits, 1);
  83. }
  84. if(current_version > 2) {
  85. flipper_format_read_string(
  86. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, temp_str);
  87. flipper_format_write_string(
  88. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, temp_str);
  89. } else {
  90. const uint32_t default_duration = TOTP_TOKEN_DURATION_DEFAULT;
  91. flipper_format_write_uint32(
  92. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, &default_duration, 1);
  93. }
  94. if(current_version > 3) {
  95. flipper_format_read_string(
  96. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES, temp_str);
  97. flipper_format_write_string(
  98. fff_data_file, TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES, temp_str);
  99. } else {
  100. const uint32_t default_automation_features = TokenAutomationFeatureNone;
  101. flipper_format_write_uint32(
  102. fff_data_file,
  103. TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES,
  104. &default_automation_features,
  105. 1);
  106. }
  107. }
  108. Stream* stream = flipper_format_get_raw_stream(fff_data_file);
  109. size_t current_pos = stream_tell(stream);
  110. size_t total_size = stream_size(stream);
  111. if(current_pos < total_size) {
  112. stream_delete(stream, total_size - current_pos);
  113. }
  114. result = true;
  115. } while(false);
  116. furi_string_free(temp_str);
  117. return result;
  118. }