common_migration.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "common_migration.h"
  2. #include "../constants.h"
  3. #include "../../../types/token_info.h"
  4. bool totp_config_migrate_to_latest(
  5. FlipperFormat* fff_data_file,
  6. FlipperFormat* fff_backup_data_file) {
  7. FuriString* temp_str = furi_string_alloc();
  8. uint32_t current_version = 0;
  9. bool result = false;
  10. do {
  11. flipper_format_write_header_cstr(
  12. fff_data_file, CONFIG_FILE_HEADER, CONFIG_FILE_ACTUAL_VERSION);
  13. if(!flipper_format_read_header(fff_backup_data_file, temp_str, &current_version)) {
  14. break;
  15. }
  16. if(flipper_format_read_string(fff_backup_data_file, TOTP_CONFIG_KEY_BASE_IV, temp_str)) {
  17. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_BASE_IV, temp_str);
  18. }
  19. flipper_format_rewind(fff_backup_data_file);
  20. if(flipper_format_read_string(
  21. fff_backup_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, temp_str)) {
  22. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, temp_str);
  23. }
  24. flipper_format_rewind(fff_backup_data_file);
  25. if(flipper_format_read_string(fff_backup_data_file, TOTP_CONFIG_KEY_TIMEZONE, temp_str)) {
  26. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, temp_str);
  27. }
  28. flipper_format_rewind(fff_backup_data_file);
  29. if(flipper_format_read_string(fff_backup_data_file, TOTP_CONFIG_KEY_PINSET, temp_str)) {
  30. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_PINSET, temp_str);
  31. }
  32. flipper_format_rewind(fff_backup_data_file);
  33. if(flipper_format_read_string(
  34. fff_backup_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, temp_str)) {
  35. flipper_format_write_string(
  36. fff_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, 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_AUTOMATION_METHOD, temp_str)) {
  41. flipper_format_write_string(
  42. fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, temp_str);
  43. }
  44. flipper_format_rewind(fff_backup_data_file);
  45. FuriString* comment_str = furi_string_alloc();
  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. furi_string_printf(
  52. comment_str, "=== BEGIN \"%s\" ===", furi_string_get_cstr(temp_str));
  53. flipper_format_write_comment(fff_data_file, comment_str);
  54. furi_string_printf(comment_str, "=== END \"%s\" ===", furi_string_get_cstr(temp_str));
  55. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_NAME, temp_str);
  56. flipper_format_read_string(
  57. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str);
  58. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str);
  59. if(current_version > 1) {
  60. flipper_format_read_string(
  61. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str);
  62. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str);
  63. flipper_format_read_string(
  64. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, temp_str);
  65. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, temp_str);
  66. } else {
  67. flipper_format_write_string_cstr(
  68. fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, TOTP_TOKEN_ALGO_SHA1_NAME);
  69. const uint32_t default_digits = TOTP_6_DIGITS;
  70. flipper_format_write_uint32(
  71. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &default_digits, 1);
  72. }
  73. if(current_version > 2) {
  74. flipper_format_read_string(
  75. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, temp_str);
  76. flipper_format_write_string(
  77. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, temp_str);
  78. } else {
  79. const uint32_t default_duration = TOTP_TOKEN_DURATION_DEFAULT;
  80. flipper_format_write_uint32(
  81. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, &default_duration, 1);
  82. }
  83. if(current_version > 3) {
  84. flipper_format_read_string(
  85. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES, temp_str);
  86. flipper_format_write_string(
  87. fff_data_file, TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES, temp_str);
  88. } else {
  89. const uint32_t default_automation_features = TOKEN_AUTOMATION_FEATURE_NONE;
  90. flipper_format_write_uint32(
  91. fff_data_file,
  92. TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES,
  93. &default_automation_features,
  94. 1);
  95. }
  96. flipper_format_write_comment(fff_data_file, comment_str);
  97. }
  98. furi_string_free(comment_str);
  99. result = true;
  100. } while(false);
  101. furi_string_free(temp_str);
  102. return result;
  103. }