seader_credential.c 23 KB

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