common_migration.c 11 KB

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