seader_credential.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. #include "seader_credential.h"
  2. #include <toolbox/path.h>
  3. #include <flipper_format/flipper_format.h>
  4. #include <seader_icons.h>
  5. #include <toolbox/protocols/protocol_dict.h>
  6. #include <lfrfid/protocols/lfrfid_protocols.h>
  7. #include <lfrfid/lfrfid_dict_file.h>
  8. #define TAG "SeaderCredential"
  9. static const char* seader_file_header = "Flipper Seader Credential";
  10. static const uint32_t seader_file_version = 1;
  11. SeaderCredential* seader_credential_alloc() {
  12. SeaderCredential* seader_dev = malloc(sizeof(SeaderCredential));
  13. seader_dev->credential = 0;
  14. seader_dev->bit_length = 0;
  15. memset(seader_dev->sio, 0xff, sizeof(seader_dev->sio));
  16. seader_dev->storage = furi_record_open(RECORD_STORAGE);
  17. seader_dev->dialogs = furi_record_open(RECORD_DIALOGS);
  18. seader_dev->load_path = furi_string_alloc();
  19. return seader_dev;
  20. }
  21. void seader_credential_free(SeaderCredential* seader_dev) {
  22. furi_assert(seader_dev);
  23. furi_record_close(RECORD_STORAGE);
  24. furi_record_close(RECORD_DIALOGS);
  25. furi_string_free(seader_dev->load_path);
  26. free(seader_dev);
  27. }
  28. void seader_credential_set_name(SeaderCredential* cred, const char* name) {
  29. furi_assert(cred);
  30. strlcpy(cred->name, name, SEADER_CRED_NAME_MAX_LEN);
  31. }
  32. static bool seader_credential_load(SeaderCredential* cred, FuriString* path, bool show_dialog) {
  33. bool parsed = false;
  34. FlipperFormat* file = flipper_format_file_alloc(cred->storage);
  35. FuriString* temp_str;
  36. temp_str = furi_string_alloc();
  37. bool deprecated_version = false;
  38. cred->type = SeaderCredentialTypeNone;
  39. if(cred->loading_cb) {
  40. cred->loading_cb(cred->loading_cb_ctx, true);
  41. }
  42. do {
  43. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(path))) break;
  44. // Read and verify file header
  45. uint32_t version = 0;
  46. if(!flipper_format_read_header(file, temp_str, &version)) break;
  47. if(furi_string_cmp_str(temp_str, seader_file_header) || (version != seader_file_version)) {
  48. deprecated_version = true;
  49. break;
  50. }
  51. if(!flipper_format_read_uint32(file, "Bits", (uint32_t*)&(cred->bit_length), 1)) break;
  52. if(!flipper_format_read_hex(
  53. file, "Credential", (uint8_t*)&cred->credential, sizeof(cred->credential)))
  54. break;
  55. // The order is reversed for storage and for the user opening the file
  56. uint64_t swapped = __builtin_bswap64(cred->credential);
  57. cred->credential = swapped;
  58. // Optional SIO/Diversifier
  59. flipper_format_read_hex(file, "SIO", cred->sio, sizeof(cred->sio));
  60. cred->sio_len = sizeof(cred->sio); // No way to know real length;
  61. flipper_format_read_hex(file, "Diversifier", cred->diversifier, sizeof(cred->diversifier));
  62. cred->diversifier_len = sizeof(cred->diversifier); // No way to know real length;
  63. parsed = true;
  64. } while(false);
  65. if(cred->loading_cb) {
  66. cred->loading_cb(cred->loading_cb_ctx, false);
  67. }
  68. if((!parsed) && (show_dialog)) {
  69. if(deprecated_version) {
  70. dialog_message_show_storage_error(cred->dialogs, "File format deprecated");
  71. } else {
  72. dialog_message_show_storage_error(cred->dialogs, "Can not parse\nfile");
  73. }
  74. }
  75. if(parsed) {
  76. FURI_LOG_I(TAG, "PACS: (%d) %016llx", cred->bit_length, cred->credential);
  77. }
  78. furi_string_free(temp_str);
  79. flipper_format_free(file);
  80. return parsed;
  81. }
  82. bool seader_credential_save_mfc(SeaderCredential* cred, const char* name) {
  83. furi_assert(cred);
  84. static const char* nfc_file_header = "Flipper NFC device";
  85. static const uint32_t nfc_file_version = 3;
  86. static const uint32_t nfc_mifare_classic_data_format_version = 2;
  87. uint8_t uid[4] = {0xDF, 0xC6, 0x9C, 0x05};
  88. uint8_t atqa[2] = {0x00, 0x04};
  89. uint8_t sak = 0x08;
  90. uint8_t manuf_block[16] = {
  91. 0xDF,
  92. 0xC6,
  93. 0x9C,
  94. 0x05,
  95. 0x80,
  96. 0x08,
  97. 0x04,
  98. 0x00,
  99. 0x00,
  100. 0x00,
  101. 0x73,
  102. 0x65,
  103. 0x61,
  104. 0x64,
  105. 0x65,
  106. 0x72};
  107. uint8_t sector0_trailer[16] = {
  108. 0xa0,
  109. 0xa1,
  110. 0xa2,
  111. 0xa3,
  112. 0xa4,
  113. 0xa5,
  114. 0x78,
  115. 0x77,
  116. 0x88,
  117. 0xc1,
  118. 0x89,
  119. 0xec,
  120. 0xa9,
  121. 0x7f,
  122. 0x8c,
  123. 0x2a};
  124. uint8_t sector1_trailer[16] = {
  125. 0x48,
  126. 0x49,
  127. 0x44,
  128. 0x20,
  129. 0x49,
  130. 0x53,
  131. 0x78,
  132. 0x77,
  133. 0x88,
  134. 0xaa,
  135. 0x20,
  136. 0x47,
  137. 0x52,
  138. 0x45,
  139. 0x41,
  140. 0x54};
  141. uint8_t section_trailer[16] = {
  142. 0xff,
  143. 0xff,
  144. 0xff,
  145. 0xff,
  146. 0xff,
  147. 0xff,
  148. 0xff,
  149. 0x07,
  150. 0x80,
  151. 0x69,
  152. 0xff,
  153. 0xff,
  154. 0xff,
  155. 0xff,
  156. 0xff,
  157. 0xff};
  158. uint8_t mad_block[16] = {
  159. 0x1b,
  160. 0x01,
  161. 0x4d,
  162. 0x48,
  163. 0x00,
  164. 0x00,
  165. 0x00,
  166. 0x00,
  167. 0x00,
  168. 0x00,
  169. 0x00,
  170. 0x00,
  171. 0x00,
  172. 0x00,
  173. 0x00,
  174. 0x00};
  175. uint8_t empty_block[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  176. uint8_t pacs_block[16] = {0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  177. bool use_load_path = true;
  178. bool saved = false;
  179. FlipperFormat* file = flipper_format_file_alloc(cred->storage);
  180. FuriString* temp_str;
  181. temp_str = furi_string_alloc();
  182. uint64_t sentinel = 1ULL << cred->bit_length;
  183. uint64_t swapped = __builtin_bswap64(cred->credential | sentinel);
  184. memcpy(pacs_block + 8, &swapped, sizeof(swapped));
  185. do {
  186. if(use_load_path && !furi_string_empty(cred->load_path)) {
  187. // Get directory name
  188. path_extract_dirname(furi_string_get_cstr(cred->load_path), temp_str);
  189. // Make path to file to save
  190. furi_string_cat_printf(temp_str, "/%s%s", name, SEADER_APP_MFC_EXTENSION);
  191. } else {
  192. furi_string_printf(
  193. temp_str, "%s/%s%s", SEADER_APP_MFC_FOLDER, name, SEADER_APP_MFC_EXTENSION);
  194. }
  195. FURI_LOG_D(TAG, "Save as MFC [%s]", furi_string_get_cstr(temp_str));
  196. // Open file
  197. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  198. if(!flipper_format_write_header_cstr(file, nfc_file_header, nfc_file_version)) break;
  199. // Write nfc device type
  200. if(!flipper_format_write_comment_cstr(
  201. file, "Nfc device type can be UID, Mifare Ultralight, Mifare Classic or ISO15693"))
  202. break;
  203. furi_string_set(temp_str, "Mifare Classic");
  204. if(!flipper_format_write_string(file, "Device type", temp_str)) break;
  205. // Write UID
  206. if(!flipper_format_write_comment_cstr(file, "UID is common for all formats")) break;
  207. if(!flipper_format_write_hex(file, "UID", uid, 4)) break;
  208. // Write ATQA, SAK
  209. if(!flipper_format_write_comment_cstr(file, "ISO14443 specific fields")) break;
  210. // Save ATQA in MSB order for correct companion apps display
  211. if(!flipper_format_write_hex(file, "ATQA", atqa, 2)) break;
  212. if(!flipper_format_write_hex(file, "SAK", &sak, 1)) break;
  213. if(!flipper_format_write_comment_cstr(file, "Mifare Classic specific data")) break;
  214. if(!flipper_format_write_comment_cstr(file, "Made with Seader")) break;
  215. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "1K")) break;
  216. uint8_t blocks = 64;
  217. if(!flipper_format_write_uint32(
  218. file, "Data format version", &nfc_mifare_classic_data_format_version, 1))
  219. break;
  220. if(!flipper_format_write_comment_cstr(
  221. file, "Mifare Classic blocks, \'??\' means unknown data"))
  222. break;
  223. bool block_saved = true;
  224. FuriString* block_str;
  225. block_str = furi_string_alloc();
  226. for(size_t i = 0; i < blocks; i++) {
  227. furi_string_printf(temp_str, "Block %d", i);
  228. switch(i) {
  229. case 0:
  230. if(!flipper_format_write_hex(
  231. file, furi_string_get_cstr(temp_str), manuf_block, sizeof(manuf_block))) {
  232. block_saved = false;
  233. }
  234. break;
  235. case 1:
  236. if(!flipper_format_write_hex(
  237. file, furi_string_get_cstr(temp_str), mad_block, sizeof(mad_block))) {
  238. block_saved = false;
  239. }
  240. break;
  241. case 3:
  242. if(!flipper_format_write_hex(
  243. file,
  244. furi_string_get_cstr(temp_str),
  245. sector0_trailer,
  246. sizeof(sector0_trailer))) {
  247. block_saved = false;
  248. }
  249. break;
  250. case 5:
  251. if(!flipper_format_write_hex(
  252. file, furi_string_get_cstr(temp_str), pacs_block, sizeof(pacs_block))) {
  253. block_saved = false;
  254. }
  255. break;
  256. case 7:
  257. if(!flipper_format_write_hex(
  258. file,
  259. furi_string_get_cstr(temp_str),
  260. sector1_trailer,
  261. sizeof(sector1_trailer))) {
  262. block_saved = false;
  263. }
  264. break;
  265. // Trailers
  266. case 11:
  267. case 15:
  268. case 19:
  269. case 23:
  270. case 27:
  271. case 31:
  272. case 35:
  273. case 39:
  274. case 43:
  275. case 47:
  276. case 51:
  277. case 55:
  278. case 59:
  279. case 63:
  280. if(!flipper_format_write_hex(
  281. file,
  282. furi_string_get_cstr(temp_str),
  283. section_trailer,
  284. sizeof(section_trailer))) {
  285. block_saved = false;
  286. }
  287. break;
  288. default:
  289. if(!flipper_format_write_hex(
  290. file, furi_string_get_cstr(temp_str), empty_block, sizeof(empty_block))) {
  291. block_saved = false;
  292. }
  293. break;
  294. }
  295. }
  296. furi_string_free(block_str);
  297. if(!block_saved) break;
  298. saved = true;
  299. } while(false);
  300. if(!saved) {
  301. dialog_message_show_storage_error(cred->dialogs, "Can not save\nfile");
  302. }
  303. furi_string_free(temp_str);
  304. flipper_format_free(file);
  305. return saved;
  306. }
  307. bool seader_credential_save_agnostic(SeaderCredential* cred, const char* name) {
  308. furi_assert(cred);
  309. bool use_load_path = true;
  310. bool saved = false;
  311. FlipperFormat* file = flipper_format_file_alloc(cred->storage);
  312. FuriString* temp_str;
  313. temp_str = furi_string_alloc();
  314. do {
  315. if(use_load_path && !furi_string_empty(cred->load_path)) {
  316. // Get directory name
  317. path_extract_dirname(furi_string_get_cstr(cred->load_path), temp_str);
  318. // Make path to file to save
  319. furi_string_cat_printf(temp_str, "/%s%s", name, SEADER_APP_EXTENSION);
  320. } else {
  321. furi_string_printf(
  322. temp_str, "%s/%s%s", STORAGE_APP_DATA_PATH_PREFIX, name, SEADER_APP_EXTENSION);
  323. }
  324. // Open file
  325. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  326. if(!flipper_format_write_header_cstr(file, seader_file_header, seader_file_version)) break;
  327. if(!flipper_format_write_uint32(file, "Bits", (uint32_t*)&cred->bit_length, 1)) break;
  328. uint64_t swapped = __builtin_bswap64(cred->credential);
  329. if(!flipper_format_write_hex(
  330. file, "Credential", (uint8_t*)&swapped, sizeof(cred->credential)))
  331. break;
  332. if(cred->sio[0] == 0x30) {
  333. // TODO: update to writing sio_len bytes, when that value has been seen to work well
  334. if(!flipper_format_write_hex(file, "SIO", cred->sio, sizeof(cred->sio))) break;
  335. if(!flipper_format_write_hex(
  336. file, "Diversifier", cred->diversifier, cred->diversifier_len))
  337. break;
  338. }
  339. saved = true;
  340. } while(false);
  341. if(!saved) {
  342. dialog_message_show_storage_error(cred->dialogs, "Can not save\nfile");
  343. }
  344. furi_string_free(temp_str);
  345. flipper_format_free(file);
  346. return saved;
  347. }
  348. bool seader_credential_save_picopass(SeaderCredential* cred, const char* name) {
  349. uint8_t zero[PICOPASS_BLOCK_LEN] = {0};
  350. uint8_t cfg[PICOPASS_BLOCK_LEN] = {0x12, 0xff, 0xff, 0xff, 0x7f, 0x1f, 0xff, 0x3c};
  351. uint8_t epurse[PICOPASS_BLOCK_LEN] = {0xff, 0xff, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xff};
  352. uint8_t debit_key[PICOPASS_BLOCK_LEN] = {0xe3, 0xf3, 0x07, 0x84, 0x4a, 0x0b, 0x62, 0x04};
  353. uint8_t aia[PICOPASS_BLOCK_LEN] = {0xFF, 0xff, 0xff, 0xff, 0xFF, 0xFf, 0xff, 0xFF};
  354. uint8_t pacs_cfg[PICOPASS_BLOCK_LEN] = {0x03, 0x03, 0x03, 0x03, 0x00, 0x03, 0xe0, 0x14};
  355. bool use_load_path = true;
  356. bool saved = false;
  357. bool withSIO = cred->save_format == SeaderCredentialSaveFormatSR;
  358. FlipperFormat* file = flipper_format_file_alloc(cred->storage);
  359. FuriString* temp_str = furi_string_alloc();
  360. if(use_load_path && !furi_string_empty(cred->load_path)) {
  361. // Get directory name
  362. path_extract_dirname(furi_string_get_cstr(cred->load_path), temp_str);
  363. // Make path to file to save
  364. furi_string_cat_printf(temp_str, "/%s%s", name, ".picopass");
  365. } else {
  366. furi_string_printf(temp_str, "%s/%s%s", EXT_PATH("apps_data/picopass"), name, ".picopass");
  367. }
  368. FURI_LOG_D(TAG, "Save as Picopass [%s]", furi_string_get_cstr(temp_str));
  369. uint64_t sentinel = 1ULL << cred->bit_length;
  370. uint64_t swapped = __builtin_bswap64(cred->credential | sentinel);
  371. // FURI_LOG_D(TAG, "PACS: (%d) %016llx | %016llx => %016llx", cred->bit_length, cred->credential, sentinel, swapped);
  372. do {
  373. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  374. if(!flipper_format_write_header_cstr(file, "Flipper Picopass device", 1)) break;
  375. if(!flipper_format_write_comment_cstr(file, "Picopass blocks generated from Seader app"))
  376. break;
  377. bool block_saved = true;
  378. for(size_t i = 0; i < 20; i++) {
  379. furi_string_printf(temp_str, "Block %d", i);
  380. switch(i) {
  381. case CSN_INDEX:
  382. if(!flipper_format_write_hex(
  383. file,
  384. furi_string_get_cstr(temp_str),
  385. cred->diversifier,
  386. PICOPASS_BLOCK_LEN)) {
  387. block_saved = false;
  388. }
  389. break;
  390. case EPURSE_INDEX:
  391. if(!flipper_format_write_hex(
  392. file, furi_string_get_cstr(temp_str), epurse, PICOPASS_BLOCK_LEN)) {
  393. block_saved = false;
  394. }
  395. break;
  396. case KD_INDEX:
  397. if(!flipper_format_write_hex(
  398. file, furi_string_get_cstr(temp_str), debit_key, PICOPASS_BLOCK_LEN)) {
  399. block_saved = false;
  400. }
  401. break;
  402. case AIA_INDEX:
  403. if(!flipper_format_write_hex(
  404. file, furi_string_get_cstr(temp_str), aia, PICOPASS_BLOCK_LEN)) {
  405. block_saved = false;
  406. }
  407. break;
  408. case CFG_INDEX:
  409. if(!flipper_format_write_hex(
  410. file, furi_string_get_cstr(temp_str), cfg, sizeof(cfg))) {
  411. block_saved = false;
  412. }
  413. break;
  414. case PACS_CFG_INDEX:
  415. if(withSIO) {
  416. pacs_cfg[0] = 0xA3;
  417. }
  418. if(!flipper_format_write_hex(
  419. file, furi_string_get_cstr(temp_str), pacs_cfg, sizeof(pacs_cfg))) {
  420. block_saved = false;
  421. }
  422. break;
  423. case PACS_INDEX:
  424. if(!flipper_format_write_hex(
  425. file,
  426. furi_string_get_cstr(temp_str),
  427. (uint8_t*)&swapped,
  428. PICOPASS_BLOCK_LEN)) {
  429. block_saved = false;
  430. }
  431. break;
  432. case SR_SIO_INDEX:
  433. case SR_SIO_INDEX + 1:
  434. case SR_SIO_INDEX + 2:
  435. case SR_SIO_INDEX + 3:
  436. case SR_SIO_INDEX + 4:
  437. case SR_SIO_INDEX + 5:
  438. case SR_SIO_INDEX + 6:
  439. case SR_SIO_INDEX + 7:
  440. if(withSIO) {
  441. if(!flipper_format_write_hex(
  442. file,
  443. furi_string_get_cstr(temp_str),
  444. cred->sio + ((i - SR_SIO_INDEX) * PICOPASS_BLOCK_LEN),
  445. PICOPASS_BLOCK_LEN)) {
  446. block_saved = false;
  447. }
  448. } else {
  449. if(!flipper_format_write_hex(
  450. file, furi_string_get_cstr(temp_str), zero, sizeof(zero))) {
  451. block_saved = false;
  452. }
  453. }
  454. break;
  455. default:
  456. if(!flipper_format_write_hex(
  457. file, furi_string_get_cstr(temp_str), zero, sizeof(zero))) {
  458. block_saved = false;
  459. }
  460. break;
  461. };
  462. if(!block_saved) {
  463. break;
  464. }
  465. }
  466. saved = true;
  467. } while(false);
  468. if(!saved) {
  469. dialog_message_show_storage_error(cred->dialogs, "Can not save\nfile");
  470. }
  471. furi_string_free(temp_str);
  472. flipper_format_free(file);
  473. return saved;
  474. }
  475. bool seader_credential_save_rfid(SeaderCredential* cred, const char* name) {
  476. bool result = false;
  477. FuriString* file_path = furi_string_alloc();
  478. furi_string_printf(file_path, "%s/%s%s", ANY_PATH("lfrfid"), name, ".rfid");
  479. ProtocolDict* dict = protocol_dict_alloc(lfrfid_protocols, LFRFIDProtocolMax);
  480. ProtocolId protocol = LFRFIDProtocolHidGeneric;
  481. FURI_LOG_D(TAG, "Original (%d): %016llx", cred->bit_length, cred->credential);
  482. uint64_t target = 0;
  483. if(cred->bit_length == 26) {
  484. //3 bytes
  485. protocol = LFRFIDProtocolH10301;
  486. // Remove parity
  487. target = (cred->credential >> 1) & 0xFFFFFF;
  488. // Reverse order since it'll get reversed again
  489. target = __builtin_bswap64(target) >> (64 - 24);
  490. } else if(cred->bit_length < 44) {
  491. // https://gist.github.com/blark/e8f125e402f576bdb7e2d7b3428bdba6
  492. protocol = LFRFIDProtocolHidGeneric;
  493. uint64_t sentinel = 1ULL << cred->bit_length;
  494. if(cred->bit_length <= 36) {
  495. uint64_t header = 1ULL << 37;
  496. FURI_LOG_D(
  497. TAG,
  498. "Prox Format (%d): %011llx",
  499. cred->bit_length,
  500. cred->credential | sentinel | header);
  501. target = __builtin_bswap64((cred->credential | sentinel | header) << 4) >> (64 - 48);
  502. } else {
  503. target = __builtin_bswap64(cred->credential << 4) >> (64 - 48);
  504. }
  505. } else {
  506. //8 bytes
  507. protocol = LFRFIDProtocolHidExGeneric;
  508. target = cred->credential;
  509. target = __builtin_bswap64(target);
  510. }
  511. FURI_LOG_D(TAG, "LFRFID (%d): %016llx", cred->bit_length, target);
  512. size_t data_size = protocol_dict_get_data_size(dict, protocol);
  513. uint8_t* data = malloc(data_size);
  514. if(data_size < 8) {
  515. memcpy(data, (void*)&target, data_size);
  516. } else {
  517. // data_size 12 for LFRFIDProtocolHidExGeneric
  518. memcpy(data + 4, (void*)&target, 8);
  519. }
  520. protocol_dict_set_data(dict, protocol, data, data_size);
  521. free(data);
  522. result = lfrfid_dict_file_save(dict, protocol, furi_string_get_cstr(file_path));
  523. FuriString* briefStr;
  524. briefStr = furi_string_alloc();
  525. protocol_dict_render_brief_data(dict, briefStr, protocol);
  526. FURI_LOG_D(TAG, "LFRFID Brief: %s", furi_string_get_cstr(briefStr));
  527. furi_string_free(briefStr);
  528. if(result) {
  529. FURI_LOG_D(TAG, "Written: %d", result);
  530. } else {
  531. FURI_LOG_D(TAG, "Failed to write");
  532. }
  533. furi_string_free(file_path);
  534. protocol_dict_free(dict);
  535. return result;
  536. }
  537. bool seader_credential_save(SeaderCredential* cred, const char* name) {
  538. if(cred->save_format == SeaderCredentialSaveFormatAgnostic) {
  539. return seader_credential_save_agnostic(cred, name);
  540. } else if(cred->save_format == SeaderCredentialSaveFormatMFC) {
  541. return seader_credential_save_mfc(cred, name);
  542. } else if(
  543. cred->save_format == SeaderCredentialSaveFormatPicopass ||
  544. cred->save_format == SeaderCredentialSaveFormatSR) {
  545. return seader_credential_save_picopass(cred, name);
  546. } else if(cred->save_format == SeaderCredentialSaveFormatRFID) {
  547. return seader_credential_save_rfid(cred, name);
  548. }
  549. return false;
  550. }
  551. bool seader_file_select(SeaderCredential* cred) {
  552. furi_assert(cred);
  553. FuriString* seader_app_folder = furi_string_alloc_set(STORAGE_APP_DATA_PATH_PREFIX);
  554. DialogsFileBrowserOptions browser_options;
  555. dialog_file_browser_set_basic_options(&browser_options, SEADER_APP_EXTENSION, &I_Nfc_10px);
  556. browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
  557. bool res = dialog_file_browser_show(
  558. cred->dialogs, cred->load_path, seader_app_folder, &browser_options);
  559. furi_string_free(seader_app_folder);
  560. if(res) {
  561. FuriString* filename;
  562. filename = furi_string_alloc();
  563. path_extract_filename(cred->load_path, filename, true);
  564. strncpy(cred->name, furi_string_get_cstr(filename), SEADER_CRED_NAME_MAX_LEN);
  565. res = seader_credential_load(cred, cred->load_path, true);
  566. if(res) {
  567. seader_credential_set_name(cred, cred->name);
  568. }
  569. furi_string_free(filename);
  570. }
  571. return res;
  572. }
  573. void seader_credential_clear(SeaderCredential* cred) {
  574. furi_assert(cred);
  575. memset(cred->name, 0, sizeof(cred->name));
  576. cred->credential = 0;
  577. cred->bit_length = 0;
  578. cred->type = SeaderCredentialTypeNone;
  579. memset(cred->sio, 0, sizeof(cred->sio));
  580. cred->sio_len = 0;
  581. memset(cred->diversifier, 0, sizeof(cred->diversifier));
  582. cred->diversifier_len = 0;
  583. furi_string_reset(cred->load_path);
  584. }
  585. bool seader_credential_delete(SeaderCredential* cred, bool use_load_path) {
  586. furi_assert(cred);
  587. bool deleted = false;
  588. FuriString* file_path;
  589. file_path = furi_string_alloc();
  590. do {
  591. // Delete original file
  592. if(use_load_path && !furi_string_empty(cred->load_path)) {
  593. furi_string_set(file_path, cred->load_path);
  594. } else {
  595. furi_string_printf(file_path, APP_DATA_PATH("%s%s"), cred->name, SEADER_APP_EXTENSION);
  596. }
  597. if(!storage_simply_remove(cred->storage, furi_string_get_cstr(file_path))) break;
  598. deleted = true;
  599. } while(0);
  600. if(!deleted) {
  601. dialog_message_show_storage_error(cred->dialogs, "Can not remove file");
  602. }
  603. furi_string_free(file_path);
  604. return deleted;
  605. }
  606. void seader_credential_set_loading_callback(
  607. SeaderCredential* cred,
  608. SeaderLoadingCallback callback,
  609. void* context) {
  610. furi_assert(cred);
  611. cred->loading_cb = callback;
  612. cred->loading_cb_ctx = context;
  613. }