seader_credential.c 23 KB

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