common_migration.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. if(flipper_format_read_string(
  93. fff_backup_data_file, TOTP_CONFIG_KEY_AUTOMATION_INITIAL_DELAY, temp_str)) {
  94. flipper_format_write_string(
  95. fff_data_file, TOTP_CONFIG_KEY_AUTOMATION_INITIAL_DELAY, temp_str);
  96. } else {
  97. uint32_t default_automation_initial_delay = 500;
  98. flipper_format_write_uint32(
  99. fff_data_file,
  100. TOTP_CONFIG_KEY_AUTOMATION_INITIAL_DELAY,
  101. &default_automation_initial_delay,
  102. 1);
  103. }
  104. flipper_format_rewind(fff_backup_data_file);
  105. while(true) {
  106. if(!flipper_format_read_string(
  107. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_NAME, temp_str)) {
  108. break;
  109. }
  110. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_NAME, temp_str);
  111. flipper_format_read_string(
  112. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str);
  113. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str);
  114. if(current_version > 1) {
  115. flipper_format_read_string(
  116. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str);
  117. if(current_version < 5) {
  118. uint32_t algo_as_uint32t = TokenHashAlgoDefault;
  119. if(furi_string_cmpi_str(temp_str, TOKEN_HASH_ALGO_SHA256_NAME) == 0) {
  120. algo_as_uint32t = TokenHashAlgoSha256;
  121. } else if(furi_string_cmpi_str(temp_str, TOKEN_HASH_ALGO_SHA512_NAME) == 0) {
  122. algo_as_uint32t = TokenHashAlgoSha512;
  123. } else if(furi_string_cmpi_str(temp_str, TOKEN_HASH_ALGO_STEAM_NAME) == 0) {
  124. algo_as_uint32t = TokenHashAlgoSteam;
  125. }
  126. flipper_format_write_uint32(
  127. fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, &algo_as_uint32t, 1);
  128. } else {
  129. flipper_format_write_string(
  130. fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str);
  131. }
  132. flipper_format_read_string(
  133. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, temp_str);
  134. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, temp_str);
  135. } else {
  136. const uint32_t default_algo = TokenHashAlgoDefault;
  137. flipper_format_write_uint32(
  138. fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, &default_algo, 1);
  139. const uint32_t default_digits = TokenDigitsCountSix;
  140. flipper_format_write_uint32(
  141. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &default_digits, 1);
  142. }
  143. if(current_version > 2) {
  144. flipper_format_read_string(
  145. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, temp_str);
  146. flipper_format_write_string(
  147. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, temp_str);
  148. } else {
  149. const uint32_t default_duration = TokenDurationDefault;
  150. flipper_format_write_uint32(
  151. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DURATION, &default_duration, 1);
  152. }
  153. if(current_version > 3) {
  154. flipper_format_read_string(
  155. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES, temp_str);
  156. flipper_format_write_string(
  157. fff_data_file, TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES, temp_str);
  158. } else {
  159. const uint32_t default_automation_features = TokenAutomationFeatureNone;
  160. flipper_format_write_uint32(
  161. fff_data_file,
  162. TOTP_CONFIG_KEY_TOKEN_AUTOMATION_FEATURES,
  163. &default_automation_features,
  164. 1);
  165. }
  166. if(current_version > 9) {
  167. flipper_format_read_string(
  168. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_TYPE, temp_str);
  169. flipper_format_write_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_TYPE, temp_str);
  170. flipper_format_read_string(
  171. fff_backup_data_file, TOTP_CONFIG_KEY_TOKEN_COUNTER, temp_str);
  172. flipper_format_write_string(
  173. fff_data_file, TOTP_CONFIG_KEY_TOKEN_COUNTER, temp_str);
  174. } else {
  175. const uint32_t default_token_type = TokenTypeTOTP;
  176. flipper_format_write_uint32(
  177. fff_data_file, TOTP_CONFIG_KEY_TOKEN_TYPE, &default_token_type, 1);
  178. const uint64_t default_counter = 0;
  179. flipper_format_write_hex(
  180. fff_data_file,
  181. TOTP_CONFIG_KEY_TOKEN_COUNTER,
  182. (const uint8_t*)&default_counter,
  183. sizeof(default_counter));
  184. }
  185. }
  186. Stream* stream = flipper_format_get_raw_stream(fff_data_file);
  187. size_t current_pos = stream_tell(stream);
  188. size_t total_size = stream_size(stream);
  189. if(current_pos < total_size) {
  190. stream_delete(stream, total_size - current_pos);
  191. }
  192. result = true;
  193. } while(false);
  194. furi_string_free(temp_str);
  195. return result;
  196. }