config.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. #include "config.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "../list/list.h"
  5. #include "../../types/common.h"
  6. #include "../../types/token_info.h"
  7. #include "migrations/config_migration_v1_to_v2.h"
  8. #define CONFIG_FILE_DIRECTORY_PATH "/ext/apps/Misc"
  9. #define CONFIG_FILE_PATH CONFIG_FILE_DIRECTORY_PATH "/totp.conf"
  10. #define CONFIG_FILE_BACKUP_PATH CONFIG_FILE_PATH ".backup"
  11. static char* token_info_get_algo_as_cstr(const TokenInfo* token_info) {
  12. switch(token_info->algo) {
  13. case SHA1:
  14. return TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME;
  15. case SHA256:
  16. return TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME;
  17. case SHA512:
  18. return TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME;
  19. default:
  20. break;
  21. }
  22. return NULL;
  23. }
  24. static void token_info_set_algo_from_str(TokenInfo* token_info, const FuriString* str) {
  25. if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME) == 0) {
  26. token_info->algo = SHA1;
  27. } else if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME) == 0) {
  28. token_info->algo = SHA256;
  29. } else if(furi_string_cmpi_str(str, TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME) == 0) {
  30. token_info->algo = SHA512;
  31. }
  32. }
  33. Storage* totp_open_storage() {
  34. return furi_record_open(RECORD_STORAGE);
  35. }
  36. void totp_close_storage() {
  37. furi_record_close(RECORD_STORAGE);
  38. }
  39. FlipperFormat* totp_open_config_file(Storage* storage) {
  40. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  41. if(storage_common_stat(storage, CONFIG_FILE_PATH, NULL) == FSE_OK) {
  42. FURI_LOG_D(LOGGING_TAG, "Config file %s found", CONFIG_FILE_PATH);
  43. if(!flipper_format_file_open_existing(fff_data_file, CONFIG_FILE_PATH)) {
  44. FURI_LOG_E(LOGGING_TAG, "Error opening existing file %s", CONFIG_FILE_PATH);
  45. totp_close_config_file(fff_data_file);
  46. return NULL;
  47. }
  48. } else {
  49. FURI_LOG_D(LOGGING_TAG, "Config file %s is not found. Will create new.", CONFIG_FILE_PATH);
  50. if(storage_common_stat(storage, CONFIG_FILE_DIRECTORY_PATH, NULL) == FSE_NOT_EXIST) {
  51. FURI_LOG_D(
  52. LOGGING_TAG,
  53. "Directory %s doesn't exist. Will create new.",
  54. CONFIG_FILE_DIRECTORY_PATH);
  55. if(!storage_simply_mkdir(storage, CONFIG_FILE_DIRECTORY_PATH)) {
  56. FURI_LOG_E(LOGGING_TAG, "Error creating directory %s", CONFIG_FILE_DIRECTORY_PATH);
  57. return NULL;
  58. }
  59. }
  60. if(!flipper_format_file_open_new(fff_data_file, CONFIG_FILE_PATH)) {
  61. totp_close_config_file(fff_data_file);
  62. FURI_LOG_E(LOGGING_TAG, "Error creating new file %s", CONFIG_FILE_PATH);
  63. return NULL;
  64. }
  65. flipper_format_write_header_cstr(
  66. fff_data_file, CONFIG_FILE_HEADER, CONFIG_FILE_ACTUAL_VERSION);
  67. float tmp_tz = 0;
  68. flipper_format_write_comment_cstr(fff_data_file, " ");
  69. flipper_format_write_comment_cstr(
  70. fff_data_file,
  71. "Timezone offset in hours. Important note: do not put '+' sign for positive values");
  72. flipper_format_write_float(fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &tmp_tz, 1);
  73. uint32_t tmp_uint32 = NotificationMethodSound | NotificationMethodVibro;
  74. flipper_format_write_comment_cstr(fff_data_file, " ");
  75. flipper_format_write_comment_cstr(
  76. fff_data_file,
  77. "How to notify user when new token is generated or badusb mode is activated (possible values: 0 - do not notify, 1 - sound, 2 - vibro, 3 sound and vibro)");
  78. flipper_format_write_uint32(
  79. fff_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1);
  80. FuriString* temp_str = furi_string_alloc();
  81. flipper_format_write_comment_cstr(fff_data_file, " ");
  82. flipper_format_write_comment_cstr(fff_data_file, "=== TOKEN SAMPLE BEGIN ===");
  83. flipper_format_write_comment_cstr(fff_data_file, " ");
  84. flipper_format_write_comment_cstr(
  85. fff_data_file, "# Token name which will be visible in the UI.");
  86. furi_string_printf(temp_str, "%s: Sample token name", TOTP_CONFIG_KEY_TOKEN_NAME);
  87. flipper_format_write_comment(fff_data_file, temp_str);
  88. flipper_format_write_comment_cstr(fff_data_file, " ");
  89. flipper_format_write_comment_cstr(
  90. fff_data_file,
  91. "# Plain token secret without spaces, dashes and etc, just pure alpha-numeric characters. Important note: plain token will be encrypted and replaced by TOTP app");
  92. furi_string_printf(temp_str, "%s: plaintokensecret", TOTP_CONFIG_KEY_TOKEN_SECRET);
  93. flipper_format_write_comment(fff_data_file, temp_str);
  94. flipper_format_write_comment_cstr(fff_data_file, " ");
  95. furi_string_printf(
  96. temp_str,
  97. " # Token hashing algorithm to use during code generation. Supported options are %s, %s and %s. If you are not use which one to use - use %s",
  98. TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME,
  99. TOTP_CONFIG_TOKEN_ALGO_SHA256_NAME,
  100. TOTP_CONFIG_TOKEN_ALGO_SHA512_NAME,
  101. TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME);
  102. flipper_format_write_comment(fff_data_file, temp_str);
  103. furi_string_printf(
  104. temp_str, "%s: %s", TOTP_CONFIG_KEY_TOKEN_ALGO, TOTP_CONFIG_TOKEN_ALGO_SHA1_NAME);
  105. flipper_format_write_comment(fff_data_file, temp_str);
  106. flipper_format_write_comment_cstr(fff_data_file, " ");
  107. flipper_format_write_comment_cstr(
  108. fff_data_file,
  109. "# How many digits there should be in generated code. Available options are 6 and 8. Majority websites requires 6 digits code, however some rare websites wants to get 8 digits code. If you are not sure which one to use - use 6");
  110. furi_string_printf(temp_str, "%s: 6", TOTP_CONFIG_KEY_TOKEN_DIGITS);
  111. flipper_format_write_comment(fff_data_file, temp_str);
  112. flipper_format_write_comment_cstr(fff_data_file, " ");
  113. flipper_format_write_comment_cstr(fff_data_file, "=== TOKEN SAMPLE END ===");
  114. flipper_format_write_comment_cstr(fff_data_file, " ");
  115. furi_string_free(temp_str);
  116. if(!flipper_format_rewind(fff_data_file)) {
  117. totp_close_config_file(fff_data_file);
  118. FURI_LOG_E(LOGGING_TAG, "Rewind error");
  119. return NULL;
  120. }
  121. }
  122. return fff_data_file;
  123. }
  124. void totp_config_file_save_new_token_i(FlipperFormat* file, const TokenInfo* token_info) {
  125. flipper_format_seek_to_end(file);
  126. flipper_format_write_string_cstr(file, TOTP_CONFIG_KEY_TOKEN_NAME, token_info->name);
  127. bool token_is_valid = token_info->token != NULL && token_info->token_length > 0;
  128. if(!token_is_valid) {
  129. flipper_format_write_comment_cstr(file, "!!! WARNING BEGIN: INVALID TOKEN !!!");
  130. }
  131. flipper_format_write_hex(
  132. file, TOTP_CONFIG_KEY_TOKEN_SECRET, token_info->token, token_info->token_length);
  133. if(!token_is_valid) {
  134. flipper_format_write_comment_cstr(file, "!!! WARNING END !!!");
  135. }
  136. flipper_format_write_string_cstr(
  137. file, TOTP_CONFIG_KEY_TOKEN_ALGO, token_info_get_algo_as_cstr(token_info));
  138. uint32_t tmp_uint32 = token_info->digits;
  139. flipper_format_write_uint32(file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &tmp_uint32, 1);
  140. }
  141. void totp_config_file_save_new_token(const TokenInfo* token_info) {
  142. Storage* cfg_storage = totp_open_storage();
  143. FlipperFormat* file = totp_open_config_file(cfg_storage);
  144. totp_config_file_save_new_token_i(file, token_info);
  145. totp_close_config_file(file);
  146. totp_close_storage();
  147. }
  148. void totp_config_file_update_timezone_offset(float new_timezone_offset) {
  149. Storage* cfg_storage = totp_open_storage();
  150. FlipperFormat* file = totp_open_config_file(cfg_storage);
  151. flipper_format_insert_or_update_float(file, TOTP_CONFIG_KEY_TIMEZONE, &new_timezone_offset, 1);
  152. totp_close_config_file(file);
  153. totp_close_storage();
  154. }
  155. void totp_config_file_update_notification_method(NotificationMethod new_notification_method) {
  156. Storage* cfg_storage = totp_open_storage();
  157. FlipperFormat* file = totp_open_config_file(cfg_storage);
  158. uint32_t tmp_uint32 = new_notification_method;
  159. flipper_format_insert_or_update_uint32(
  160. file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1);
  161. totp_close_config_file(file);
  162. totp_close_storage();
  163. }
  164. void totp_config_file_update_user_settings(const PluginState* plugin_state) {
  165. Storage* cfg_storage = totp_open_storage();
  166. FlipperFormat* file = totp_open_config_file(cfg_storage);
  167. flipper_format_insert_or_update_float(
  168. file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1);
  169. uint32_t tmp_uint32 = plugin_state->notification_method;
  170. flipper_format_insert_or_update_uint32(
  171. file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1);
  172. totp_close_config_file(file);
  173. totp_close_storage();
  174. }
  175. void totp_full_save_config_file(const PluginState* const plugin_state) {
  176. Storage* storage = totp_open_storage();
  177. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  178. flipper_format_file_open_always(fff_data_file, CONFIG_FILE_PATH);
  179. flipper_format_write_header_cstr(
  180. fff_data_file, CONFIG_FILE_HEADER, CONFIG_FILE_ACTUAL_VERSION);
  181. flipper_format_write_hex(
  182. fff_data_file, TOTP_CONFIG_KEY_BASE_IV, &plugin_state->base_iv[0], TOTP_IV_SIZE);
  183. flipper_format_write_hex(
  184. fff_data_file,
  185. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  186. plugin_state->crypto_verify_data,
  187. plugin_state->crypto_verify_data_length);
  188. flipper_format_write_float(
  189. fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1);
  190. flipper_format_write_bool(fff_data_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1);
  191. uint32_t tmp_uint32 = plugin_state->notification_method;
  192. flipper_format_write_uint32(
  193. fff_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1);
  194. TOTP_LIST_FOREACH(plugin_state->tokens_list, node, {
  195. const TokenInfo* token_info = node->data;
  196. totp_config_file_save_new_token_i(fff_data_file, token_info);
  197. });
  198. totp_close_config_file(fff_data_file);
  199. totp_close_storage();
  200. }
  201. void totp_config_file_load_base(PluginState* const plugin_state) {
  202. Storage* storage = totp_open_storage();
  203. FlipperFormat* fff_data_file = totp_open_config_file(storage);
  204. plugin_state->timezone_offset = 0;
  205. FuriString* temp_str = furi_string_alloc();
  206. uint32_t file_version;
  207. if(!flipper_format_read_header(fff_data_file, temp_str, &file_version)) {
  208. FURI_LOG_E(LOGGING_TAG, "Missing or incorrect header");
  209. furi_string_free(temp_str);
  210. return;
  211. }
  212. if(file_version < CONFIG_FILE_ACTUAL_VERSION) {
  213. FURI_LOG_I(
  214. LOGGING_TAG,
  215. "Obsolete config file version detected. Current version: %" PRIu32
  216. "; Actual version: %" PRId16,
  217. file_version,
  218. CONFIG_FILE_ACTUAL_VERSION);
  219. totp_close_config_file(fff_data_file);
  220. if(storage_common_stat(storage, CONFIG_FILE_BACKUP_PATH, NULL) == FSE_OK) {
  221. storage_simply_remove(storage, CONFIG_FILE_BACKUP_PATH);
  222. }
  223. if(storage_common_copy(storage, CONFIG_FILE_PATH, CONFIG_FILE_BACKUP_PATH) == FSE_OK) {
  224. FURI_LOG_I(LOGGING_TAG, "Took config file backup to %s", CONFIG_FILE_BACKUP_PATH);
  225. fff_data_file = totp_open_config_file(storage);
  226. FlipperFormat* fff_backup_data_file = flipper_format_file_alloc(storage);
  227. flipper_format_file_open_existing(fff_backup_data_file, CONFIG_FILE_BACKUP_PATH);
  228. if(file_version == 1) {
  229. if(totp_config_migrate_v1_to_v2(fff_data_file, fff_backup_data_file)) {
  230. FURI_LOG_I(LOGGING_TAG, "Applied migration from v1 to v2");
  231. } else {
  232. FURI_LOG_W(LOGGING_TAG, "An error occurred during migration from v1 to v2");
  233. }
  234. }
  235. flipper_format_file_close(fff_backup_data_file);
  236. flipper_format_free(fff_backup_data_file);
  237. flipper_format_rewind(fff_data_file);
  238. } else {
  239. FURI_LOG_E(
  240. LOGGING_TAG,
  241. "An error occurred during taking backup of %s into %s before migration",
  242. CONFIG_FILE_PATH,
  243. CONFIG_FILE_BACKUP_PATH);
  244. }
  245. }
  246. if(!flipper_format_read_hex(
  247. fff_data_file, TOTP_CONFIG_KEY_BASE_IV, &plugin_state->base_iv[0], TOTP_IV_SIZE)) {
  248. FURI_LOG_D(LOGGING_TAG, "Missing base IV");
  249. }
  250. flipper_format_rewind(fff_data_file);
  251. uint32_t crypto_size;
  252. if(flipper_format_get_value_count(fff_data_file, TOTP_CONFIG_KEY_CRYPTO_VERIFY, &crypto_size) &&
  253. crypto_size > 0) {
  254. plugin_state->crypto_verify_data = malloc(sizeof(uint8_t) * crypto_size);
  255. furi_check(plugin_state->crypto_verify_data != NULL);
  256. plugin_state->crypto_verify_data_length = crypto_size;
  257. if(!flipper_format_read_hex(
  258. fff_data_file,
  259. TOTP_CONFIG_KEY_CRYPTO_VERIFY,
  260. plugin_state->crypto_verify_data,
  261. crypto_size)) {
  262. FURI_LOG_D(LOGGING_TAG, "Missing crypto verify token");
  263. free(plugin_state->crypto_verify_data);
  264. plugin_state->crypto_verify_data = NULL;
  265. plugin_state->crypto_verify_data_length = 0;
  266. }
  267. } else {
  268. plugin_state->crypto_verify_data = NULL;
  269. plugin_state->crypto_verify_data_length = 0;
  270. }
  271. flipper_format_rewind(fff_data_file);
  272. if(!flipper_format_read_float(
  273. fff_data_file, TOTP_CONFIG_KEY_TIMEZONE, &plugin_state->timezone_offset, 1)) {
  274. plugin_state->timezone_offset = 0;
  275. FURI_LOG_D(LOGGING_TAG, "Missing timezone offset information, defaulting to 0");
  276. }
  277. flipper_format_rewind(fff_data_file);
  278. if(!flipper_format_read_bool(
  279. fff_data_file, TOTP_CONFIG_KEY_PINSET, &plugin_state->pin_set, 1)) {
  280. plugin_state->pin_set = true;
  281. }
  282. flipper_format_rewind(fff_data_file);
  283. uint32_t tmp_uint32;
  284. if(!flipper_format_read_uint32(
  285. fff_data_file, TOTP_CONFIG_KEY_NOTIFICATION_METHOD, &tmp_uint32, 1)) {
  286. tmp_uint32 = NotificationMethodSound | NotificationMethodVibro;
  287. }
  288. plugin_state->notification_method = tmp_uint32;
  289. furi_string_free(temp_str);
  290. totp_close_config_file(fff_data_file);
  291. totp_close_storage();
  292. }
  293. TokenLoadingResult totp_config_file_load_tokens(PluginState* const plugin_state) {
  294. Storage* storage = totp_open_storage();
  295. FlipperFormat* fff_data_file = totp_open_config_file(storage);
  296. FuriString* temp_str = furi_string_alloc();
  297. uint32_t temp_data32;
  298. if(!flipper_format_read_header(fff_data_file, temp_str, &temp_data32)) {
  299. FURI_LOG_E(LOGGING_TAG, "Missing or incorrect header");
  300. furi_string_free(temp_str);
  301. return TokenLoadingResultError;
  302. }
  303. TokenLoadingResult result = TokenLoadingResultSuccess;
  304. uint16_t index = 0;
  305. bool has_any_plain_secret = false;
  306. while(true) {
  307. if(!flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_NAME, temp_str)) {
  308. break;
  309. }
  310. TokenInfo* tokenInfo = token_info_alloc();
  311. size_t temp_cstr_len = furi_string_size(temp_str);
  312. tokenInfo->name = malloc(temp_cstr_len + 1);
  313. furi_check(tokenInfo->name != NULL);
  314. strlcpy(tokenInfo->name, furi_string_get_cstr(temp_str), temp_cstr_len + 1);
  315. uint32_t secret_bytes_count;
  316. if(!flipper_format_get_value_count(
  317. fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, &secret_bytes_count)) {
  318. secret_bytes_count = 0;
  319. }
  320. if(secret_bytes_count == 1) { // Plain secret key
  321. if(flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_SECRET, temp_str)) {
  322. if(token_info_set_secret(
  323. tokenInfo,
  324. furi_string_get_cstr(temp_str),
  325. furi_string_size(temp_str),
  326. &plugin_state->iv[0])) {
  327. FURI_LOG_W(LOGGING_TAG, "Token \"%s\" has plain secret", tokenInfo->name);
  328. } else {
  329. tokenInfo->token = NULL;
  330. tokenInfo->token_length = 0;
  331. FURI_LOG_W(LOGGING_TAG, "Token \"%s\" has invalid secret", tokenInfo->name);
  332. result = TokenLoadingResultWarning;
  333. }
  334. } else {
  335. tokenInfo->token = NULL;
  336. tokenInfo->token_length = 0;
  337. result = TokenLoadingResultWarning;
  338. }
  339. has_any_plain_secret = true;
  340. } else { // encrypted
  341. tokenInfo->token_length = secret_bytes_count;
  342. if(secret_bytes_count > 0) {
  343. tokenInfo->token = malloc(tokenInfo->token_length);
  344. furi_check(tokenInfo->token != NULL);
  345. if(!flipper_format_read_hex(
  346. fff_data_file,
  347. TOTP_CONFIG_KEY_TOKEN_SECRET,
  348. tokenInfo->token,
  349. tokenInfo->token_length)) {
  350. free(tokenInfo->token);
  351. tokenInfo->token = NULL;
  352. tokenInfo->token_length = 0;
  353. result = TokenLoadingResultWarning;
  354. }
  355. } else {
  356. tokenInfo->token = NULL;
  357. result = TokenLoadingResultWarning;
  358. }
  359. }
  360. if(flipper_format_read_string(fff_data_file, TOTP_CONFIG_KEY_TOKEN_ALGO, temp_str)) {
  361. token_info_set_algo_from_str(tokenInfo, temp_str);
  362. } else {
  363. tokenInfo->algo = SHA1;
  364. }
  365. if(!flipper_format_read_uint32(
  366. fff_data_file, TOTP_CONFIG_KEY_TOKEN_DIGITS, &temp_data32, 1) ||
  367. !token_info_set_digits_from_int(tokenInfo, temp_data32)) {
  368. tokenInfo->digits = TOTP_6_DIGITS;
  369. }
  370. FURI_LOG_D(LOGGING_TAG, "Found token \"%s\"", tokenInfo->name);
  371. TOTP_LIST_INIT_OR_ADD(plugin_state->tokens_list, tokenInfo, furi_check);
  372. index++;
  373. }
  374. plugin_state->tokens_count = index;
  375. plugin_state->token_list_loaded = true;
  376. FURI_LOG_D(LOGGING_TAG, "Found %" PRIu16 " tokens", index);
  377. furi_string_free(temp_str);
  378. totp_close_config_file(fff_data_file);
  379. totp_close_storage();
  380. if(has_any_plain_secret) {
  381. totp_full_save_config_file(plugin_state);
  382. }
  383. return result;
  384. }
  385. void totp_close_config_file(FlipperFormat* file) {
  386. if(file == NULL) return;
  387. flipper_format_file_close(file);
  388. flipper_format_free(file);
  389. }