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