config.c 17 KB

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