config.c 17 KB

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