seader_credential.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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. FURI_LOG_I(TAG, "PACS: (%d) %016llx", cred->bit_length, cred->credential);
  73. furi_string_free(temp_str);
  74. flipper_format_free(file);
  75. return parsed;
  76. }
  77. bool seader_credential_save_mfc(SeaderCredential* cred, const char* name) {
  78. furi_assert(cred);
  79. static const char* nfc_file_header = "Flipper NFC device";
  80. static const uint32_t nfc_file_version = 3;
  81. static const uint32_t nfc_mifare_classic_data_format_version = 2;
  82. uint8_t uid[4] = {0xDF, 0xC6, 0x9C, 0x05};
  83. uint8_t atqa[2] = {0x00, 0x04};
  84. uint8_t sak = 0x08;
  85. uint8_t manuf_block[16] = {
  86. 0xDF,
  87. 0xC6,
  88. 0x9C,
  89. 0x05,
  90. 0x80,
  91. 0x08,
  92. 0x04,
  93. 0x00,
  94. 0x00,
  95. 0x00,
  96. 0x73,
  97. 0x65,
  98. 0x61,
  99. 0x64,
  100. 0x65,
  101. 0x72};
  102. uint8_t sector0_trailer[16] = {
  103. 0xa0,
  104. 0xa1,
  105. 0xa2,
  106. 0xa3,
  107. 0xa4,
  108. 0xa5,
  109. 0x78,
  110. 0x77,
  111. 0x88,
  112. 0xc1,
  113. 0x89,
  114. 0xec,
  115. 0xa9,
  116. 0x7f,
  117. 0x8c,
  118. 0x2a};
  119. uint8_t sector1_trailer[16] = {
  120. 0x48,
  121. 0x49,
  122. 0x44,
  123. 0x20,
  124. 0x49,
  125. 0x53,
  126. 0x78,
  127. 0x77,
  128. 0x88,
  129. 0xaa,
  130. 0x20,
  131. 0x47,
  132. 0x52,
  133. 0x45,
  134. 0x41,
  135. 0x54};
  136. uint8_t sectorn_trailer[16] = {
  137. 0xff,
  138. 0xff,
  139. 0xff,
  140. 0xff,
  141. 0xff,
  142. 0xff,
  143. 0xff,
  144. 0x07,
  145. 0x80,
  146. 0x69,
  147. 0xff,
  148. 0xff,
  149. 0xff,
  150. 0xff,
  151. 0xff,
  152. 0xff};
  153. uint8_t mad_block[16] = {
  154. 0x1b,
  155. 0x01,
  156. 0x4d,
  157. 0x48,
  158. 0x00,
  159. 0x00,
  160. 0x00,
  161. 0x00,
  162. 0x00,
  163. 0x00,
  164. 0x00,
  165. 0x00,
  166. 0x00,
  167. 0x00,
  168. 0x00,
  169. 0x00};
  170. uint8_t empty_block[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  171. uint8_t pacs_block[16] = {0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  172. bool use_load_path = true;
  173. bool saved = false;
  174. FlipperFormat* file = flipper_format_file_alloc(cred->storage);
  175. FuriString* temp_str;
  176. temp_str = furi_string_alloc();
  177. uint64_t sentinel = 1ULL << cred->bit_length;
  178. uint64_t swapped = __builtin_bswap64(cred->credential | sentinel);
  179. memcpy(pacs_block + 8, &swapped, sizeof(swapped));
  180. do {
  181. if(use_load_path && !furi_string_empty(cred->load_path)) {
  182. // Get directory name
  183. path_extract_dirname(furi_string_get_cstr(cred->load_path), temp_str);
  184. // Make path to file to save
  185. furi_string_cat_printf(temp_str, "/%s%s", name, SEADER_APP_MFC_EXTENSION);
  186. } else {
  187. furi_string_printf(
  188. temp_str, "%s/%s%s", SEADER_APP_MFC_FOLDER, name, SEADER_APP_MFC_EXTENSION);
  189. }
  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. sectorn_trailer,
  279. sizeof(sectorn_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. // 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. if(!flipper_format_write_hex(file, "SIO", cred->sio, sizeof(cred->sio))) break;
  329. if(!flipper_format_write_hex(
  330. file, "Diversifier", cred->diversifier, sizeof(cred->diversifier)))
  331. break;
  332. }
  333. saved = true;
  334. } while(false);
  335. if(!saved) {
  336. dialog_message_show_storage_error(cred->dialogs, "Can not save\nfile");
  337. }
  338. furi_string_free(temp_str);
  339. flipper_format_free(file);
  340. return saved;
  341. }
  342. bool seader_credential_save(SeaderCredential* cred, const char* name) {
  343. uint8_t zero[PICOPASS_BLOCK_LEN] = {0};
  344. uint8_t csn[PICOPASS_BLOCK_LEN] = {0x7a, 0xf5, 0x31, 0x13, 0xfe, 0xff, 0x12, 0xe0};
  345. uint8_t cfg[PICOPASS_BLOCK_LEN] = {0x12, 0xff, 0xff, 0xff, 0x7f, 0x1f, 0xff, 0x3c};
  346. uint8_t epurse[PICOPASS_BLOCK_LEN] = {0xff, 0xff, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xff};
  347. uint8_t debit_key[PICOPASS_BLOCK_LEN] = {0xe3, 0xf3, 0x07, 0x84, 0x4a, 0x0b, 0x62, 0x04};
  348. uint8_t aia[PICOPASS_BLOCK_LEN] = {0xFF, 0xff, 0xff, 0xff, 0xFF, 0xFf, 0xff, 0xFF};
  349. uint8_t pacs_cfg[PICOPASS_BLOCK_LEN] = {0x03, 0x03, 0x03, 0x03, 0x00, 0x03, 0xe0, 0x14};
  350. if(cred->save_format == SeaderCredentialSaveFormatAgnostic) {
  351. return seader_credential_save_agnostic(cred, name);
  352. } else if(cred->save_format == SeaderCredentialSaveFormatMFC) {
  353. return seader_credential_save_mfc(cred, name);
  354. } else if(
  355. cred->save_format == SeaderCredentialSaveFormatPicopass ||
  356. cred->save_format == SeaderCredentialSaveFormatSR) {
  357. bool use_load_path = true;
  358. bool saved = false;
  359. bool withSIO = cred->save_format == SeaderCredentialSaveFormatSR;
  360. FlipperFormat* file = flipper_format_file_alloc(cred->storage);
  361. FuriString* temp_str = furi_string_alloc();
  362. if(use_load_path && !furi_string_empty(cred->load_path)) {
  363. // Get directory name
  364. path_extract_dirname(furi_string_get_cstr(cred->load_path), temp_str);
  365. // Make path to file to save
  366. furi_string_cat_printf(temp_str, "/%s%s", name, ".picopass");
  367. } else {
  368. furi_string_printf(
  369. temp_str, "%s/%s%s", STORAGE_APP_DATA_PATH_PREFIX, name, ".picopass");
  370. }
  371. FURI_LOG_D(TAG, "Save as Picopass [%s]", furi_string_get_cstr(temp_str));
  372. uint64_t sentinel = 1ULL << cred->bit_length;
  373. uint64_t swapped = __builtin_bswap64(cred->credential | sentinel);
  374. // FURI_LOG_D(TAG, "PACS: (%d) %016llx | %016llx => %016llx", cred->bit_length, cred->credential, sentinel, swapped);
  375. do {
  376. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  377. if(!flipper_format_write_header_cstr(file, "Flipper Picopass device", 1)) break;
  378. if(!flipper_format_write_comment_cstr(
  379. file, "Picopass blocks generated from Seader app"))
  380. break;
  381. bool block_saved = true;
  382. for(size_t i = 0; i < 20; i++) {
  383. furi_string_printf(temp_str, "Block %d", i);
  384. switch(i) {
  385. case CSN_INDEX:
  386. if(withSIO) {
  387. if(!flipper_format_write_hex(
  388. file,
  389. furi_string_get_cstr(temp_str),
  390. cred->diversifier,
  391. PICOPASS_BLOCK_LEN)) {
  392. block_saved = false;
  393. }
  394. } else {
  395. if(!flipper_format_write_hex(
  396. file, furi_string_get_cstr(temp_str), csn, sizeof(csn))) {
  397. block_saved = false;
  398. }
  399. }
  400. break;
  401. case EPURSE_INDEX:
  402. if(!flipper_format_write_hex(
  403. file, furi_string_get_cstr(temp_str), epurse, PICOPASS_BLOCK_LEN)) {
  404. block_saved = false;
  405. }
  406. break;
  407. case KD_INDEX:
  408. if(!flipper_format_write_hex(
  409. file, furi_string_get_cstr(temp_str), debit_key, PICOPASS_BLOCK_LEN)) {
  410. block_saved = false;
  411. }
  412. break;
  413. case AIA_INDEX:
  414. if(!flipper_format_write_hex(
  415. file, furi_string_get_cstr(temp_str), aia, PICOPASS_BLOCK_LEN)) {
  416. block_saved = false;
  417. }
  418. break;
  419. case CFG_INDEX:
  420. if(!flipper_format_write_hex(
  421. file, furi_string_get_cstr(temp_str), cfg, sizeof(cfg))) {
  422. block_saved = false;
  423. }
  424. break;
  425. case PACS_CFG_INDEX:
  426. if(withSIO) {
  427. pacs_cfg[0] = 0xA3;
  428. }
  429. if(!flipper_format_write_hex(
  430. file, furi_string_get_cstr(temp_str), pacs_cfg, sizeof(pacs_cfg))) {
  431. block_saved = false;
  432. }
  433. break;
  434. case PACS_INDEX:
  435. if(!flipper_format_write_hex(
  436. file,
  437. furi_string_get_cstr(temp_str),
  438. (uint8_t*)&swapped,
  439. PICOPASS_BLOCK_LEN)) {
  440. block_saved = false;
  441. }
  442. break;
  443. case SR_SIO_INDEX:
  444. case SR_SIO_INDEX + 1:
  445. case SR_SIO_INDEX + 2:
  446. case SR_SIO_INDEX + 3:
  447. case SR_SIO_INDEX + 4:
  448. case SR_SIO_INDEX + 5:
  449. case SR_SIO_INDEX + 6:
  450. case SR_SIO_INDEX + 7:
  451. if(withSIO) {
  452. if(!flipper_format_write_hex(
  453. file,
  454. furi_string_get_cstr(temp_str),
  455. cred->sio + ((i - SR_SIO_INDEX) * PICOPASS_BLOCK_LEN),
  456. PICOPASS_BLOCK_LEN)) {
  457. block_saved = false;
  458. }
  459. } else {
  460. if(!flipper_format_write_hex(
  461. file, furi_string_get_cstr(temp_str), zero, sizeof(zero))) {
  462. block_saved = false;
  463. }
  464. }
  465. break;
  466. default:
  467. if(!flipper_format_write_hex(
  468. file, furi_string_get_cstr(temp_str), zero, sizeof(zero))) {
  469. block_saved = false;
  470. }
  471. break;
  472. };
  473. if(!block_saved) {
  474. break;
  475. }
  476. }
  477. saved = true;
  478. } while(false);
  479. if(!saved) {
  480. dialog_message_show_storage_error(cred->dialogs, "Can not save\nfile");
  481. }
  482. furi_string_free(temp_str);
  483. flipper_format_free(file);
  484. return saved;
  485. } else if(cred->save_format == SeaderCredentialSaveFormatRFID) {
  486. bool result = false;
  487. FuriString* file_path = furi_string_alloc();
  488. furi_string_printf(file_path, "%s/%s%s", ANY_PATH("lfrfid"), name, ".rfid");
  489. ProtocolDict* dict = protocol_dict_alloc(lfrfid_protocols, LFRFIDProtocolMax);
  490. ProtocolId protocol = LFRFIDProtocolHidGeneric;
  491. FURI_LOG_D(TAG, "Original (%d): %016llx", cred->bit_length, cred->credential);
  492. uint64_t target = 0;
  493. if(cred->bit_length == 26) {
  494. //3 bytes
  495. protocol = LFRFIDProtocolH10301;
  496. // Remove parity
  497. target = (cred->credential >> 1) & 0xFFFFFF;
  498. // Reverse order since it'll get reversed again
  499. target = __builtin_bswap64(target) >> (64 - 24);
  500. } else if(cred->bit_length < 44) {
  501. // https://gist.github.com/blark/e8f125e402f576bdb7e2d7b3428bdba6
  502. protocol = LFRFIDProtocolHidGeneric;
  503. uint64_t sentinel = 1ULL << cred->bit_length;
  504. if(cred->bit_length <= 36) {
  505. uint64_t header = 1ULL << 37;
  506. FURI_LOG_D(
  507. TAG,
  508. "Prox Format (%d): %011llx",
  509. cred->bit_length,
  510. cred->credential | sentinel | header);
  511. target = __builtin_bswap64((cred->credential | sentinel | header) << 4) >>
  512. (64 - 48);
  513. } else {
  514. target = __builtin_bswap64(cred->credential << 4) >> (64 - 48);
  515. }
  516. } else {
  517. //8 bytes
  518. protocol = LFRFIDProtocolHidExGeneric;
  519. target = cred->credential;
  520. target = __builtin_bswap64(target);
  521. }
  522. FURI_LOG_D(TAG, "LFRFID (%d): %016llx", cred->bit_length, target);
  523. size_t data_size = protocol_dict_get_data_size(dict, protocol);
  524. uint8_t* data = malloc(data_size);
  525. if(data_size < 8) {
  526. memcpy(data, (void*)&target, data_size);
  527. } else {
  528. // data_size 12 for LFRFIDProtocolHidExGeneric
  529. memcpy(data + 4, (void*)&target, 8);
  530. }
  531. protocol_dict_set_data(dict, protocol, data, data_size);
  532. free(data);
  533. result = lfrfid_dict_file_save(dict, protocol, furi_string_get_cstr(file_path));
  534. FuriString* briefStr;
  535. briefStr = furi_string_alloc();
  536. protocol_dict_render_brief_data(dict, briefStr, protocol);
  537. FURI_LOG_D(TAG, "LFRFID Brief: %s", furi_string_get_cstr(briefStr));
  538. furi_string_free(briefStr);
  539. if(result) {
  540. FURI_LOG_D(TAG, "Written: %d", result);
  541. } else {
  542. FURI_LOG_D(TAG, "Failed to write");
  543. }
  544. furi_string_free(file_path);
  545. protocol_dict_free(dict);
  546. return result;
  547. }
  548. return false;
  549. }
  550. bool seader_file_select(SeaderCredential* cred) {
  551. furi_assert(cred);
  552. FuriString* seader_app_folder = furi_string_alloc_set(STORAGE_APP_DATA_PATH_PREFIX);
  553. DialogsFileBrowserOptions browser_options;
  554. dialog_file_browser_set_basic_options(&browser_options, SEADER_APP_EXTENSION, &I_Nfc_10px);
  555. browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
  556. bool res = dialog_file_browser_show(
  557. cred->dialogs, cred->load_path, seader_app_folder, &browser_options);
  558. furi_string_free(seader_app_folder);
  559. if(res) {
  560. FuriString* filename;
  561. filename = furi_string_alloc();
  562. path_extract_filename(cred->load_path, filename, true);
  563. strncpy(cred->name, furi_string_get_cstr(filename), SEADER_CRED_NAME_MAX_LEN);
  564. res = seader_credential_load(cred, cred->load_path, true);
  565. if(res) {
  566. seader_credential_set_name(cred, cred->name);
  567. }
  568. furi_string_free(filename);
  569. }
  570. return res;
  571. }
  572. void seader_credential_clear(SeaderCredential* cred) {
  573. furi_assert(cred);
  574. memset(cred->name, 0, sizeof(cred->name));
  575. cred->credential = 0;
  576. cred->bit_length = 0;
  577. cred->type = SeaderCredentialTypeNone;
  578. memset(cred->sio, 0, sizeof(cred->sio));
  579. memset(cred->diversifier, 0, sizeof(cred->diversifier));
  580. furi_string_reset(cred->load_path);
  581. }
  582. bool seader_credential_delete(SeaderCredential* cred, bool use_load_path) {
  583. furi_assert(cred);
  584. bool deleted = false;
  585. FuriString* file_path;
  586. file_path = furi_string_alloc();
  587. do {
  588. // Delete original file
  589. if(use_load_path && !furi_string_empty(cred->load_path)) {
  590. furi_string_set(file_path, cred->load_path);
  591. } else {
  592. furi_string_printf(file_path, APP_DATA_PATH("%s%s"), cred->name, SEADER_APP_EXTENSION);
  593. }
  594. if(!storage_simply_remove(cred->storage, furi_string_get_cstr(file_path))) break;
  595. deleted = true;
  596. } while(0);
  597. if(!deleted) {
  598. dialog_message_show_storage_error(cred->dialogs, "Can not remove file");
  599. }
  600. furi_string_free(file_path);
  601. return deleted;
  602. }
  603. void seader_credential_set_loading_callback(
  604. SeaderCredential* cred,
  605. SeaderLoadingCallback callback,
  606. void* context) {
  607. furi_assert(cred);
  608. cred->loading_cb = callback;
  609. cred->loading_cb_ctx = context;
  610. }