common_migration.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #define TOTP_OLD_CONFIG_KEY_BASE_IV "BaseIV"
  7. bool totp_config_migrate_to_latest(
  8. FlipperFormat* fff_data_file,
  9. FlipperFormat* fff_backup_data_file) {
  10. FuriString* temp_str = furi_string_alloc();
  11. uint32_t current_version = 0;
  12. bool result = false;
  13. do {
  14. flipper_format_write_header_cstr(
  15. fff_data_file, CONFIG_FILE_HEADER, CONFIG_FILE_ACTUAL_VERSION);
  16. if(!flipper_format_read_header(fff_backup_data_file, temp_str, &current_version)) {
  17. break;
  18. }
  19. if(flipper_format_read_string(
  20. fff_backup_data_file, TOTP_CONFIG_KEY_CRYPTO_VERSION, temp_str)) {
  21. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERSION, temp_str);
  22. } else {
  23. uint32_t old_crypto_version = 1;
  24. flipper_format_write_uint32(
  25. fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERSION, &old_crypto_version, 1);
  26. }
  27. flipper_format_rewind(fff_backup_data_file);
  28. if(flipper_format_read_string(
  29. fff_backup_data_file, TOTP_CONFIG_KEY_CRYPTO_KEY_SLOT, temp_str)) {
  30. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_KEY_SLOT, temp_str);
  31. } else {
  32. uint32_t default_old_key_slot = 2;
  33. flipper_format_write_uint32(
  34. fff_data_file, TOTP_CONFIG_KEY_CRYPTO_KEY_SLOT, &default_old_key_slot, 1);
  35. }
  36. flipper_format_rewind(fff_backup_data_file);
  37. if(flipper_format_read_string(fff_backup_data_file, TOTP_CONFIG_KEY_SALT, temp_str)) {
  38. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_SALT, temp_str);
  39. } else if(
  40. flipper_format_rewind(fff_backup_data_file) &&
  41. flipper_format_read_string(
  42. fff_backup_data_file, TOTP_OLD_CONFIG_KEY_BASE_IV, temp_str)) {
  43. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_SALT, temp_str);
  44. }
  45. flipper_format_rewind(fff_backup_data_file);
  46. if(flipper_format_read_string(
  47. fff_backup_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, temp_str)) {
  48. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, temp_str);
  49. }
  50. flipper_format_rewind(fff_backup_data_file);
  51. if(flipper_format_read_string(fff_backup_data_file, TOTP_CONFIG_KEY_TIMEZONE, temp_str)) {
  52. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, temp_str);
  53. }
  54. flipper_format_rewind(fff_backup_data_file);
  55. if(flipper_format_read_string(fff_backup_data_file, TOTP_CONFIG_KEY_PINSET, temp_str)) {
  56. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_PINSET, 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_NOTIFICATION_METHOD, temp_str)) {
  61. flipper_format_write_string(
  62. fff_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, temp_str);
  63. }
  64. flipper_format_rewind(fff_backup_data_file);
  65. if(flipper_format_read_string(
  66. fff_backup_data_file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, temp_str)) {
  67. flipper_format_write_string(
  68. fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_METHOD, temp_str);
  69. }
  70. flipper_format_rewind(fff_backup_data_file);
  71. if(flipper_format_read_string(fff_backup_data_file, TOTP_CONFIG_KEY_FONT, temp_str)) {
  72. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_FONT, temp_str);
  73. } else {
  74. uint32_t default_font_index = 0;
  75. flipper_format_write_uint32(
  76. fff_data_file, TOTP_CONFIG_KEY_FONT, &default_font_index, 1);
  77. }
  78. flipper_format_rewind(fff_backup_data_file);
  79. if(flipper_format_read_string(
  80. fff_backup_data_file, TOTP_CONFIG_KEY_AUTOMATION_KB_LAYOUT, temp_str)) {
  81. flipper_format_write_string(
  82. fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_KB_LAYOUT, temp_str);
  83. } else {
  84. uint32_t default_automation_kb_layout = AutomationKeyboardLayoutQWERTY;
  85. flipper_format_write_uint32(
  86. fff_data_file,
  87. TOTP_CONFIG_KEY_AUTOMATION_KB_LAYOUT,
  88. &default_automation_kb_layout,
  89. 1);
  90. }
  91. flipper_format_rewind(fff_backup_data_file);
  92. while(true) {
  93. if(!flipper_format_read_string(
  94. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_NAME, temp_str)) {
  95. break;
  96. }
  97. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_NAME, temp_str);
  98. flipper_format_read_string(
  99. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str);
  100. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str);
  101. if(current_version > 1) {
  102. flipper_format_read_string(
  103. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str);
  104. if(current_version < 5) {
  105. uint32_t algo_as_uint32t = TokenHashAlgoDefault;
  106. if(furi_string_cmpi_str(temp_str, TOKEN_HASH_ALGO_SHA256_NAME) == 0) {
  107. algo_as_uint32t = TokenHashAlgoSha256;
  108. } else if(furi_string_cmpi_str(temp_str, TOKEN_HASH_ALGO_SHA512_NAME) == 0) {
  109. algo_as_uint32t = TokenHashAlgoSha512;
  110. } else if(furi_string_cmpi_str(temp_str, TOKEN_HASH_ALGO_STEAM_NAME) == 0) {
  111. algo_as_uint32t = TokenHashAlgoSteam;
  112. }
  113. flipper_format_write_uint32(
  114. fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, &algo_as_uint32t, 1);
  115. } else {
  116. flipper_format_write_string(
  117. fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str);
  118. }
  119. flipper_format_read_string(
  120. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, temp_str);
  121. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, temp_str);
  122. } else {
  123. const uint32_t default_algo = TokenHashAlgoDefault;
  124. flipper_format_write_uint32(
  125. fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, &default_algo, 1);
  126. const uint32_t default_digits = TokenDigitsCountSix;
  127. flipper_format_write_uint32(
  128. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &default_digits, 1);
  129. }
  130. if(current_version > 2) {
  131. flipper_format_read_string(
  132. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, temp_str);
  133. flipper_format_write_string(
  134. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, temp_str);
  135. } else {
  136. const uint32_t default_duration = TokenDurationDefault;
  137. flipper_format_write_uint32(
  138. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, &default_duration, 1);
  139. }
  140. if(current_version > 3) {
  141. flipper_format_read_string(
  142. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES, temp_str);
  143. flipper_format_write_string(
  144. fff_data_file, TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES, temp_str);
  145. } else {
  146. const uint32_t default_automation_features = TokenAutomationFeatureNone;
  147. flipper_format_write_uint32(
  148. fff_data_file,
  149. TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES,
  150. &default_automation_features,
  151. 1);
  152. }
  153. if(current_version > 9) {
  154. flipper_format_read_string(
  155. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_TYPE, temp_str);
  156. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_TYPE, temp_str);
  157. flipper_format_read_string(
  158. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_COUNTER, temp_str);
  159. flipper_format_write_string(
  160. fff_data_file, TOTP_CONFIG_KEY_TOKEN_COUNTER, temp_str);
  161. } else {
  162. const uint32_t default_token_type = TokenTypeTOTP;
  163. flipper_format_write_uint32(
  164. fff_data_file, TOTP_CONFIG_KEY_TOKEN_TYPE, &default_token_type, 1);
  165. const uint64_t default_counter = 0;
  166. flipper_format_write_hex(
  167. fff_data_file,
  168. TOTP_CONFIG_KEY_TOKEN_COUNTER,
  169. (const uint8_t*)&default_counter,
  170. sizeof(default_counter));
  171. }
  172. }
  173. Stream* stream = flipper_format_get_raw_stream(fff_data_file);
  174. size_t current_pos = stream_tell(stream);
  175. size_t total_size = stream_size(stream);
  176. if(current_pos < total_size) {
  177. stream_delete(stream, total_size - current_pos);
  178. }
  179. result = true;
  180. } while(false);
  181. furi_string_free(temp_str);
  182. return result;
  183. }