seader_credential.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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 fake_csn[PICOPASS_BLOCK_LEN] = {0x7a, 0xf5, 0x31, 0x13, 0xfe, 0xff, 0x12, 0xe0};
  351. uint8_t cfg[PICOPASS_BLOCK_LEN] = {0x12, 0xff, 0xff, 0xff, 0x7f, 0x1f, 0xff, 0x3c};
  352. uint8_t epurse[PICOPASS_BLOCK_LEN] = {0xff, 0xff, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xff};
  353. uint8_t debit_key[PICOPASS_BLOCK_LEN] = {0xe3, 0xf3, 0x07, 0x84, 0x4a, 0x0b, 0x62, 0x04};
  354. uint8_t aia[PICOPASS_BLOCK_LEN] = {0xFF, 0xff, 0xff, 0xff, 0xFF, 0xFf, 0xff, 0xFF};
  355. uint8_t pacs_cfg[PICOPASS_BLOCK_LEN] = {0x03, 0x03, 0x03, 0x03, 0x00, 0x03, 0xe0, 0x14};
  356. bool use_load_path = true;
  357. bool saved = false;
  358. bool withSIO = cred->save_format == SeaderCredentialSaveFormatSR;
  359. FlipperFormat* file = flipper_format_file_alloc(cred->storage);
  360. FuriString* temp_str = furi_string_alloc();
  361. if(use_load_path && !furi_string_empty(cred->load_path)) {
  362. // Get directory name
  363. path_extract_dirname(furi_string_get_cstr(cred->load_path), temp_str);
  364. // Make path to file to save
  365. furi_string_cat_printf(temp_str, "/%s%s", name, ".picopass");
  366. } else {
  367. furi_string_printf(temp_str, "%s/%s%s", EXT_PATH("apps_data/picopass"), name, ".picopass");
  368. }
  369. FURI_LOG_D(TAG, "Save as Picopass [%s]", furi_string_get_cstr(temp_str));
  370. uint64_t sentinel = 1ULL << cred->bit_length;
  371. uint64_t swapped = __builtin_bswap64(cred->credential | sentinel);
  372. // FURI_LOG_D(TAG, "PACS: (%d) %016llx | %016llx => %016llx", cred->bit_length, cred->credential, sentinel, swapped);
  373. do {
  374. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  375. if(!flipper_format_write_header_cstr(file, "Flipper Picopass device", 1)) break;
  376. if(!flipper_format_write_comment_cstr(file, "Picopass blocks generated from Seader app"))
  377. break;
  378. bool block_saved = true;
  379. for(size_t i = 0; i < 20; i++) {
  380. furi_string_printf(temp_str, "Block %d", i);
  381. switch(i) {
  382. case CSN_INDEX:
  383. if(memcmp(cred->diversifier, zero, PICOPASS_BLOCK_LEN) == 0) {
  384. // when doing a downgrade from a non-picopass, we need to use a fake csn
  385. if(!flipper_format_write_hex(
  386. file, furi_string_get_cstr(temp_str), fake_csn, sizeof(fake_csn))) {
  387. block_saved = false;
  388. }
  389. } else {
  390. if(!flipper_format_write_hex(
  391. file,
  392. furi_string_get_cstr(temp_str),
  393. cred->diversifier,
  394. PICOPASS_BLOCK_LEN)) {
  395. block_saved = false;
  396. }
  397. }
  398. break;
  399. case EPURSE_INDEX:
  400. if(!flipper_format_write_hex(
  401. file, furi_string_get_cstr(temp_str), epurse, PICOPASS_BLOCK_LEN)) {
  402. block_saved = false;
  403. }
  404. break;
  405. case KD_INDEX:
  406. if(!flipper_format_write_hex(
  407. file, furi_string_get_cstr(temp_str), debit_key, PICOPASS_BLOCK_LEN)) {
  408. block_saved = false;
  409. }
  410. break;
  411. case AIA_INDEX:
  412. if(!flipper_format_write_hex(
  413. file, furi_string_get_cstr(temp_str), aia, PICOPASS_BLOCK_LEN)) {
  414. block_saved = false;
  415. }
  416. break;
  417. case CFG_INDEX:
  418. if(!flipper_format_write_hex(
  419. file, furi_string_get_cstr(temp_str), cfg, sizeof(cfg))) {
  420. block_saved = false;
  421. }
  422. break;
  423. case PACS_CFG_INDEX:
  424. if(withSIO) {
  425. pacs_cfg[0] = 0xA3;
  426. }
  427. if(!flipper_format_write_hex(
  428. file, furi_string_get_cstr(temp_str), pacs_cfg, sizeof(pacs_cfg))) {
  429. block_saved = false;
  430. }
  431. break;
  432. case PACS_INDEX:
  433. if(!flipper_format_write_hex(
  434. file,
  435. furi_string_get_cstr(temp_str),
  436. (uint8_t*)&swapped,
  437. PICOPASS_BLOCK_LEN)) {
  438. block_saved = false;
  439. }
  440. break;
  441. case SR_SIO_INDEX:
  442. case SR_SIO_INDEX + 1:
  443. case SR_SIO_INDEX + 2:
  444. case SR_SIO_INDEX + 3:
  445. case SR_SIO_INDEX + 4:
  446. case SR_SIO_INDEX + 5:
  447. case SR_SIO_INDEX + 6:
  448. case SR_SIO_INDEX + 7:
  449. if(withSIO) {
  450. if(!flipper_format_write_hex(
  451. file,
  452. furi_string_get_cstr(temp_str),
  453. cred->sio + ((i - SR_SIO_INDEX) * PICOPASS_BLOCK_LEN),
  454. PICOPASS_BLOCK_LEN)) {
  455. block_saved = false;
  456. }
  457. } else {
  458. if(!flipper_format_write_hex(
  459. file, furi_string_get_cstr(temp_str), zero, sizeof(zero))) {
  460. block_saved = false;
  461. }
  462. }
  463. break;
  464. default:
  465. if(!flipper_format_write_hex(
  466. file, furi_string_get_cstr(temp_str), zero, sizeof(zero))) {
  467. block_saved = false;
  468. }
  469. break;
  470. };
  471. if(!block_saved) {
  472. break;
  473. }
  474. }
  475. saved = true;
  476. } while(false);
  477. if(!saved) {
  478. dialog_message_show_storage_error(cred->dialogs, "Can not save\nfile");
  479. }
  480. furi_string_free(temp_str);
  481. flipper_format_free(file);
  482. return saved;
  483. }
  484. bool seader_credential_save_rfid(SeaderCredential* cred, const char* name) {
  485. bool result = false;
  486. FuriString* file_path = furi_string_alloc();
  487. furi_string_printf(file_path, "%s/%s%s", ANY_PATH("lfrfid"), name, ".rfid");
  488. ProtocolDict* dict = protocol_dict_alloc(lfrfid_protocols, LFRFIDProtocolMax);
  489. ProtocolId protocol = LFRFIDProtocolHidGeneric;
  490. FURI_LOG_D(TAG, "Original (%d): %016llx", cred->bit_length, cred->credential);
  491. uint64_t target = 0;
  492. if(cred->bit_length == 26) {
  493. //3 bytes
  494. protocol = LFRFIDProtocolH10301;
  495. // Remove parity
  496. target = (cred->credential >> 1) & 0xFFFFFF;
  497. // Reverse order since it'll get reversed again
  498. target = __builtin_bswap64(target) >> (64 - 24);
  499. } else if(cred->bit_length < 44) {
  500. // https://gist.github.com/blark/e8f125e402f576bdb7e2d7b3428bdba6
  501. protocol = LFRFIDProtocolHidGeneric;
  502. uint64_t sentinel = 1ULL << cred->bit_length;
  503. if(cred->bit_length <= 36) {
  504. uint64_t header = 1ULL << 37;
  505. FURI_LOG_D(
  506. TAG,
  507. "Prox Format (%d): %011llx",
  508. cred->bit_length,
  509. cred->credential | sentinel | header);
  510. target = __builtin_bswap64((cred->credential | sentinel | header) << 4) >> (64 - 48);
  511. } else {
  512. target = __builtin_bswap64(cred->credential << 4) >> (64 - 48);
  513. }
  514. } else {
  515. //8 bytes
  516. protocol = LFRFIDProtocolHidExGeneric;
  517. target = cred->credential;
  518. target = __builtin_bswap64(target);
  519. }
  520. FURI_LOG_D(TAG, "LFRFID (%d): %016llx", cred->bit_length, target);
  521. size_t data_size = protocol_dict_get_data_size(dict, protocol);
  522. uint8_t* data = malloc(data_size);
  523. if(data_size < 8) {
  524. memcpy(data, (void*)&target, data_size);
  525. } else {
  526. // data_size 12 for LFRFIDProtocolHidExGeneric
  527. memcpy(data + 4, (void*)&target, 8);
  528. }
  529. protocol_dict_set_data(dict, protocol, data, data_size);
  530. free(data);
  531. result = lfrfid_dict_file_save(dict, protocol, furi_string_get_cstr(file_path));
  532. FuriString* briefStr;
  533. briefStr = furi_string_alloc();
  534. protocol_dict_render_brief_data(dict, briefStr, protocol);
  535. FURI_LOG_D(TAG, "LFRFID Brief: %s", furi_string_get_cstr(briefStr));
  536. furi_string_free(briefStr);
  537. if(result) {
  538. FURI_LOG_D(TAG, "Written: %d", result);
  539. } else {
  540. FURI_LOG_D(TAG, "Failed to write");
  541. }
  542. furi_string_free(file_path);
  543. protocol_dict_free(dict);
  544. return result;
  545. }
  546. bool seader_credential_save(SeaderCredential* cred, const char* name) {
  547. if(cred->save_format == SeaderCredentialSaveFormatAgnostic) {
  548. return seader_credential_save_agnostic(cred, name);
  549. } else if(cred->save_format == SeaderCredentialSaveFormatMFC) {
  550. return seader_credential_save_mfc(cred, name);
  551. } else if(
  552. cred->save_format == SeaderCredentialSaveFormatPicopass ||
  553. cred->save_format == SeaderCredentialSaveFormatSR) {
  554. return seader_credential_save_picopass(cred, name);
  555. } else if(cred->save_format == SeaderCredentialSaveFormatRFID) {
  556. return seader_credential_save_rfid(cred, name);
  557. }
  558. return false;
  559. }
  560. bool seader_file_select(SeaderCredential* cred) {
  561. furi_assert(cred);
  562. FuriString* seader_app_folder = furi_string_alloc_set(STORAGE_APP_DATA_PATH_PREFIX);
  563. DialogsFileBrowserOptions browser_options;
  564. dialog_file_browser_set_basic_options(&browser_options, SEADER_APP_EXTENSION, &I_Nfc_10px);
  565. browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
  566. bool res = dialog_file_browser_show(
  567. cred->dialogs, cred->load_path, seader_app_folder, &browser_options);
  568. furi_string_free(seader_app_folder);
  569. if(res) {
  570. FuriString* filename;
  571. filename = furi_string_alloc();
  572. path_extract_filename(cred->load_path, filename, true);
  573. strncpy(cred->name, furi_string_get_cstr(filename), SEADER_CRED_NAME_MAX_LEN);
  574. res = seader_credential_load(cred, cred->load_path, true);
  575. if(res) {
  576. seader_credential_set_name(cred, cred->name);
  577. }
  578. furi_string_free(filename);
  579. }
  580. return res;
  581. }
  582. void seader_credential_clear(SeaderCredential* cred) {
  583. furi_assert(cred);
  584. memset(cred->name, 0, sizeof(cred->name));
  585. cred->credential = 0;
  586. cred->bit_length = 0;
  587. cred->type = SeaderCredentialTypeNone;
  588. memset(cred->sio, 0, sizeof(cred->sio));
  589. cred->sio_len = 0;
  590. memset(cred->diversifier, 0, sizeof(cred->diversifier));
  591. cred->diversifier_len = 0;
  592. furi_string_reset(cred->load_path);
  593. }
  594. bool seader_credential_delete(SeaderCredential* cred, bool use_load_path) {
  595. furi_assert(cred);
  596. bool deleted = false;
  597. FuriString* file_path;
  598. file_path = furi_string_alloc();
  599. do {
  600. // Delete original file
  601. if(use_load_path && !furi_string_empty(cred->load_path)) {
  602. furi_string_set(file_path, cred->load_path);
  603. } else {
  604. furi_string_printf(file_path, APP_DATA_PATH("%s%s"), cred->name, SEADER_APP_EXTENSION);
  605. }
  606. if(!storage_simply_remove(cred->storage, furi_string_get_cstr(file_path))) break;
  607. deleted = true;
  608. } while(0);
  609. if(!deleted) {
  610. dialog_message_show_storage_error(cred->dialogs, "Can not remove file");
  611. }
  612. furi_string_free(file_path);
  613. return deleted;
  614. }
  615. void seader_credential_set_loading_callback(
  616. SeaderCredential* cred,
  617. SeaderLoadingCallback callback,
  618. void* context) {
  619. furi_assert(cred);
  620. cred->loading_cb = callback;
  621. cred->loading_cb_ctx = context;
  622. }