seader_credential.c 23 KB

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