seader_credential.c 24 KB

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