common_migration.c 9.6 KB

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