seader_credential.c 23 KB

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