seader_credential.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. SeaderCredential* seader_credential_alloc() {
  13. SeaderCredential* seader_dev = malloc(sizeof(SeaderCredential));
  14. seader_dev->credential = 0;
  15. seader_dev->bit_length = 0;
  16. memset(seader_dev->sio, 0xff, sizeof(seader_dev->sio));
  17. seader_dev->storage = furi_record_open(RECORD_STORAGE);
  18. seader_dev->dialogs = furi_record_open(RECORD_DIALOGS);
  19. seader_dev->load_path = furi_string_alloc();
  20. return seader_dev;
  21. }
  22. void seader_credential_free(SeaderCredential* seader_dev) {
  23. furi_assert(seader_dev);
  24. furi_record_close(RECORD_STORAGE);
  25. furi_record_close(RECORD_DIALOGS);
  26. furi_string_free(seader_dev->load_path);
  27. free(seader_dev);
  28. }
  29. void seader_credential_set_name(SeaderCredential* cred, const char* name) {
  30. furi_assert(cred);
  31. strlcpy(cred->name, name, SEADER_CRED_NAME_MAX_LEN);
  32. }
  33. static bool seader_credential_load(SeaderCredential* cred, FuriString* path, bool show_dialog) {
  34. bool parsed = false;
  35. FlipperFormat* file = flipper_format_file_alloc(cred->storage);
  36. FuriString* temp_str;
  37. temp_str = furi_string_alloc();
  38. bool deprecated_version = false;
  39. cred->type = SeaderCredentialTypeNone;
  40. if(cred->loading_cb) {
  41. cred->loading_cb(cred->loading_cb_ctx, true);
  42. }
  43. do {
  44. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(path))) break;
  45. // Read and verify file header
  46. uint32_t version = 0;
  47. if(!flipper_format_read_header(file, temp_str, &version)) break;
  48. if(furi_string_cmp_str(temp_str, seader_file_header) || (version != seader_file_version)) {
  49. deprecated_version = true;
  50. break;
  51. }
  52. if(!flipper_format_read_uint32(file, "Bits", (uint32_t*)&(cred->bit_length), 1)) break;
  53. if(!flipper_format_read_hex(
  54. file, "Credential", (uint8_t*)&cred->credential, sizeof(cred->credential)))
  55. break;
  56. // The order is reversed for storage and for the user opening the file
  57. uint64_t swapped = __builtin_bswap64(cred->credential);
  58. cred->credential = swapped;
  59. // Optional SIO/Diversifier
  60. flipper_format_read_hex(file, "SIO", cred->sio, sizeof(cred->sio));
  61. cred->sio_len = sizeof(cred->sio); // No way to know real length;
  62. flipper_format_read_hex(file, "Diversifier", cred->diversifier, sizeof(cred->diversifier));
  63. cred->diversifier_len = sizeof(cred->diversifier); // No way to know real length;
  64. parsed = true;
  65. } while(false);
  66. if(cred->loading_cb) {
  67. cred->loading_cb(cred->loading_cb_ctx, false);
  68. }
  69. if((!parsed) && (show_dialog)) {
  70. if(deprecated_version) {
  71. dialog_message_show_storage_error(cred->dialogs, "File format deprecated");
  72. } else {
  73. dialog_message_show_storage_error(cred->dialogs, "Can not parse\nfile");
  74. }
  75. }
  76. if(parsed) {
  77. FURI_LOG_I(TAG, "PACS: (%d) %016llx", cred->bit_length, cred->credential);
  78. }
  79. furi_string_free(temp_str);
  80. flipper_format_free(file);
  81. return parsed;
  82. }
  83. bool seader_credential_save_mfc(SeaderCredential* cred, const char* name) {
  84. furi_assert(cred);
  85. static const char* nfc_file_header = "Flipper NFC device";
  86. static const uint32_t nfc_file_version = 3;
  87. static const uint32_t nfc_mifare_classic_data_format_version = 2;
  88. uint8_t uid[4] = {0xDF, 0xC6, 0x9C, 0x05};
  89. uint8_t atqa[2] = {0x00, 0x04};
  90. uint8_t sak = 0x08;
  91. uint8_t manuf_block[16] = {
  92. 0xDF,
  93. 0xC6,
  94. 0x9C,
  95. 0x05,
  96. 0x80,
  97. 0x08,
  98. 0x04,
  99. 0x00,
  100. 0x00,
  101. 0x00,
  102. 0x73,
  103. 0x65,
  104. 0x61,
  105. 0x64,
  106. 0x65,
  107. 0x72};
  108. uint8_t sector0_trailer[16] = {
  109. 0xa0,
  110. 0xa1,
  111. 0xa2,
  112. 0xa3,
  113. 0xa4,
  114. 0xa5,
  115. 0x78,
  116. 0x77,
  117. 0x88,
  118. 0xc1,
  119. 0x89,
  120. 0xec,
  121. 0xa9,
  122. 0x7f,
  123. 0x8c,
  124. 0x2a};
  125. uint8_t sector1_trailer[16] = {
  126. 0x48,
  127. 0x49,
  128. 0x44,
  129. 0x20,
  130. 0x49,
  131. 0x53,
  132. 0x78,
  133. 0x77,
  134. 0x88,
  135. 0xaa,
  136. 0x20,
  137. 0x47,
  138. 0x52,
  139. 0x45,
  140. 0x41,
  141. 0x54};
  142. uint8_t section_trailer[16] = {
  143. 0xff,
  144. 0xff,
  145. 0xff,
  146. 0xff,
  147. 0xff,
  148. 0xff,
  149. 0xff,
  150. 0x07,
  151. 0x80,
  152. 0x69,
  153. 0xff,
  154. 0xff,
  155. 0xff,
  156. 0xff,
  157. 0xff,
  158. 0xff};
  159. uint8_t mad_block[16] = {
  160. 0x1b,
  161. 0x01,
  162. 0x4d,
  163. 0x48,
  164. 0x00,
  165. 0x00,
  166. 0x00,
  167. 0x00,
  168. 0x00,
  169. 0x00,
  170. 0x00,
  171. 0x00,
  172. 0x00,
  173. 0x00,
  174. 0x00,
  175. 0x00};
  176. uint8_t empty_block[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  177. uint8_t pacs_block[16] = {0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  178. bool use_load_path = true;
  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. if(use_load_path && !furi_string_empty(cred->load_path)) {
  188. // Get directory name
  189. path_extract_dirname(furi_string_get_cstr(cred->load_path), temp_str);
  190. // Make path to file to save
  191. furi_string_cat_printf(temp_str, "/%s%s", name, SEADER_APP_MFC_EXTENSION);
  192. } else {
  193. furi_string_printf(
  194. temp_str, "%s/%s%s", SEADER_APP_MFC_FOLDER, name, SEADER_APP_MFC_EXTENSION);
  195. }
  196. FURI_LOG_D(TAG, "Save as MFC [%s]", furi_string_get_cstr(temp_str));
  197. // Open file
  198. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  199. if(!flipper_format_write_header_cstr(file, nfc_file_header, nfc_file_version)) break;
  200. // Write nfc device type
  201. if(!flipper_format_write_comment_cstr(
  202. file, "Nfc device type can be UID, Mifare Ultralight, Mifare Classic or ISO15693"))
  203. break;
  204. furi_string_set(temp_str, "Mifare Classic");
  205. if(!flipper_format_write_string(file, "Device type", temp_str)) break;
  206. // Write UID
  207. if(!flipper_format_write_comment_cstr(file, "UID is common for all formats")) break;
  208. if(!flipper_format_write_hex(file, "UID", uid, 4)) break;
  209. // Write ATQA, SAK
  210. if(!flipper_format_write_comment_cstr(file, "ISO14443 specific fields")) break;
  211. // Save ATQA in MSB order for correct companion apps display
  212. if(!flipper_format_write_hex(file, "ATQA", atqa, 2)) break;
  213. if(!flipper_format_write_hex(file, "SAK", &sak, 1)) break;
  214. if(!flipper_format_write_comment_cstr(file, "Mifare Classic specific data")) break;
  215. if(!flipper_format_write_comment_cstr(file, "Made with Seader")) break;
  216. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "1K")) break;
  217. uint8_t blocks = 64;
  218. if(!flipper_format_write_uint32(
  219. file, "Data format version", &nfc_mifare_classic_data_format_version, 1))
  220. break;
  221. if(!flipper_format_write_comment_cstr(
  222. file, "Mifare Classic blocks, \'??\' means unknown data"))
  223. break;
  224. bool block_saved = true;
  225. FuriString* block_str;
  226. block_str = furi_string_alloc();
  227. for(size_t i = 0; i < blocks; i++) {
  228. furi_string_printf(temp_str, "Block %d", i);
  229. switch(i) {
  230. case 0:
  231. if(!flipper_format_write_hex(
  232. file, furi_string_get_cstr(temp_str), manuf_block, sizeof(manuf_block))) {
  233. block_saved = false;
  234. }
  235. break;
  236. case 1:
  237. if(!flipper_format_write_hex(
  238. file, furi_string_get_cstr(temp_str), mad_block, sizeof(mad_block))) {
  239. block_saved = false;
  240. }
  241. break;
  242. case 3:
  243. if(!flipper_format_write_hex(
  244. file,
  245. furi_string_get_cstr(temp_str),
  246. sector0_trailer,
  247. sizeof(sector0_trailer))) {
  248. block_saved = false;
  249. }
  250. break;
  251. case 5:
  252. if(!flipper_format_write_hex(
  253. file, furi_string_get_cstr(temp_str), pacs_block, sizeof(pacs_block))) {
  254. block_saved = false;
  255. }
  256. break;
  257. case 7:
  258. if(!flipper_format_write_hex(
  259. file,
  260. furi_string_get_cstr(temp_str),
  261. sector1_trailer,
  262. sizeof(sector1_trailer))) {
  263. block_saved = false;
  264. }
  265. break;
  266. // Trailers
  267. case 11:
  268. case 15:
  269. case 19:
  270. case 23:
  271. case 27:
  272. case 31:
  273. case 35:
  274. case 39:
  275. case 43:
  276. case 47:
  277. case 51:
  278. case 55:
  279. case 59:
  280. case 63:
  281. if(!flipper_format_write_hex(
  282. file,
  283. furi_string_get_cstr(temp_str),
  284. section_trailer,
  285. sizeof(section_trailer))) {
  286. block_saved = false;
  287. }
  288. break;
  289. default:
  290. if(!flipper_format_write_hex(
  291. file, furi_string_get_cstr(temp_str), empty_block, sizeof(empty_block))) {
  292. block_saved = false;
  293. }
  294. break;
  295. }
  296. }
  297. furi_string_free(block_str);
  298. if(!block_saved) break;
  299. saved = true;
  300. } while(false);
  301. if(!saved) {
  302. dialog_message_show_storage_error(cred->dialogs, "Can not save\nfile");
  303. }
  304. furi_string_free(temp_str);
  305. flipper_format_free(file);
  306. return saved;
  307. }
  308. bool seader_credential_save_agnostic(SeaderCredential* cred, const char* name) {
  309. furi_assert(cred);
  310. bool use_load_path = true;
  311. bool saved = false;
  312. FlipperFormat* file = flipper_format_file_alloc(cred->storage);
  313. FuriString* temp_str;
  314. temp_str = furi_string_alloc();
  315. do {
  316. if(use_load_path && !furi_string_empty(cred->load_path)) {
  317. // Get directory name
  318. path_extract_dirname(furi_string_get_cstr(cred->load_path), temp_str);
  319. // Make path to file to save
  320. furi_string_cat_printf(temp_str, "/%s%s", name, SEADER_APP_EXTENSION);
  321. } else {
  322. furi_string_printf(
  323. temp_str, "%s/%s%s", STORAGE_APP_DATA_PATH_PREFIX, name, SEADER_APP_EXTENSION);
  324. }
  325. // Open file
  326. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  327. if(!flipper_format_write_header_cstr(file, seader_file_header, seader_file_version)) break;
  328. if(!flipper_format_write_uint32(file, "Bits", (uint32_t*)&cred->bit_length, 1)) break;
  329. uint64_t swapped = __builtin_bswap64(cred->credential);
  330. if(!flipper_format_write_hex(
  331. file, "Credential", (uint8_t*)&swapped, sizeof(cred->credential)))
  332. break;
  333. if(cred->sio[0] == 0x30) {
  334. // TODO: update to writing sio_len bytes, when that value has been seen to work well
  335. if(!flipper_format_write_hex(file, "SIO", cred->sio, sizeof(cred->sio))) break;
  336. if(!flipper_format_write_hex(
  337. file, "Diversifier", cred->diversifier, cred->diversifier_len))
  338. break;
  339. }
  340. saved = true;
  341. } while(false);
  342. if(!saved) {
  343. dialog_message_show_storage_error(cred->dialogs, "Can not save\nfile");
  344. }
  345. furi_string_free(temp_str);
  346. flipper_format_free(file);
  347. return saved;
  348. }
  349. bool seader_credential_save_picopass(SeaderCredential* cred, const char* name) {
  350. uint8_t zero[PICOPASS_BLOCK_LEN] = {0};
  351. uint8_t fake_csn[PICOPASS_BLOCK_LEN] = {0x7a, 0xf5, 0x31, 0x13, 0xfe, 0xff, 0x12, 0xe0};
  352. uint8_t cfg[PICOPASS_BLOCK_LEN] = {0x12, 0xff, 0xff, 0xff, 0x7f, 0x1f, 0xff, 0x3c};
  353. uint8_t epurse[PICOPASS_BLOCK_LEN] = {0xff, 0xff, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xff};
  354. uint8_t debit_key[PICOPASS_BLOCK_LEN] = {0xe3, 0xf3, 0x07, 0x84, 0x4a, 0x0b, 0x62, 0x04};
  355. uint8_t aia[PICOPASS_BLOCK_LEN] = {0xFF, 0xff, 0xff, 0xff, 0xFF, 0xFf, 0xff, 0xFF};
  356. uint8_t pacs_cfg[PICOPASS_BLOCK_LEN] = {0x03, 0x03, 0x03, 0x03, 0x00, 0x03, 0xe0, 0x14};
  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. storage_simply_mkdir(cred->storage, EXT_PATH("apps_data/picopass"));
  363. if(use_load_path && !furi_string_empty(cred->load_path)) {
  364. // Get directory name
  365. path_extract_dirname(furi_string_get_cstr(cred->load_path), temp_str);
  366. // Make path to file to save
  367. furi_string_cat_printf(temp_str, "/%s%s", name, ".picopass");
  368. } else {
  369. furi_string_printf(temp_str, "%s/%s%s", EXT_PATH("apps_data/picopass"), 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(file, "Picopass blocks generated from Seader app"))
  379. break;
  380. bool block_saved = true;
  381. for(size_t i = 0; i < 20; i++) {
  382. furi_string_printf(temp_str, "Block %d", i);
  383. switch(i) {
  384. case CSN_INDEX:
  385. if(memcmp(cred->diversifier, zero, PICOPASS_BLOCK_LEN) == 0) {
  386. // when doing a downgrade from a non-picopass, we need to use a fake csn
  387. if(!flipper_format_write_hex(
  388. file, furi_string_get_cstr(temp_str), fake_csn, sizeof(fake_csn))) {
  389. block_saved = false;
  390. }
  391. } else {
  392. if(!flipper_format_write_hex(
  393. file,
  394. furi_string_get_cstr(temp_str),
  395. cred->diversifier,
  396. PICOPASS_BLOCK_LEN)) {
  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. }
  486. bool seader_credential_save_rfid(SeaderCredential* cred, const char* name) {
  487. bool result = false;
  488. FuriString* file_path = furi_string_alloc();
  489. furi_string_printf(file_path, "%s/%s%s", ANY_PATH("lfrfid"), name, ".rfid");
  490. ProtocolDict* dict = protocol_dict_alloc(lfrfid_protocols, LFRFIDProtocolMax);
  491. ProtocolId protocol = LFRFIDProtocolHidGeneric;
  492. FURI_LOG_D(TAG, "Original (%d): %016llx", cred->bit_length, cred->credential);
  493. uint64_t target = 0;
  494. if(cred->bit_length == 26) {
  495. //3 bytes
  496. protocol = LFRFIDProtocolH10301;
  497. // Remove parity
  498. target = (cred->credential >> 1) & 0xFFFFFF;
  499. // Reverse order since it'll get reversed again
  500. target = __builtin_bswap64(target) >> (64 - 24);
  501. } else if(cred->bit_length < 44) {
  502. // https://gist.github.com/blark/e8f125e402f576bdb7e2d7b3428bdba6
  503. protocol = LFRFIDProtocolHidGeneric;
  504. uint64_t sentinel = 1ULL << cred->bit_length;
  505. if(cred->bit_length <= 36) {
  506. uint64_t header = 1ULL << 37;
  507. FURI_LOG_D(
  508. TAG,
  509. "Prox Format (%d): %011llx",
  510. cred->bit_length,
  511. cred->credential | sentinel | header);
  512. target = __builtin_bswap64((cred->credential | sentinel | header) << 4) >> (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. bool seader_credential_save(SeaderCredential* cred, const char* name) {
  549. if(cred->save_format == SeaderCredentialSaveFormatAgnostic) {
  550. return seader_credential_save_agnostic(cred, name);
  551. } else if(cred->save_format == SeaderCredentialSaveFormatMFC) {
  552. return seader_credential_save_mfc(cred, name);
  553. } else if(
  554. cred->save_format == SeaderCredentialSaveFormatPicopass ||
  555. cred->save_format == SeaderCredentialSaveFormatSR) {
  556. return seader_credential_save_picopass(cred, name);
  557. } else if(cred->save_format == SeaderCredentialSaveFormatRFID) {
  558. return seader_credential_save_rfid(cred, name);
  559. }
  560. return false;
  561. }
  562. bool seader_file_select(SeaderCredential* cred) {
  563. furi_assert(cred);
  564. FuriString* seader_app_folder = furi_string_alloc_set(STORAGE_APP_DATA_PATH_PREFIX);
  565. DialogsFileBrowserOptions browser_options;
  566. dialog_file_browser_set_basic_options(&browser_options, SEADER_APP_EXTENSION, &I_Nfc_10px);
  567. browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
  568. bool res = dialog_file_browser_show(
  569. cred->dialogs, cred->load_path, seader_app_folder, &browser_options);
  570. furi_string_free(seader_app_folder);
  571. if(res) {
  572. FuriString* filename;
  573. filename = furi_string_alloc();
  574. path_extract_filename(cred->load_path, filename, true);
  575. strncpy(cred->name, furi_string_get_cstr(filename), SEADER_CRED_NAME_MAX_LEN);
  576. res = seader_credential_load(cred, cred->load_path, true);
  577. if(res) {
  578. seader_credential_set_name(cred, cred->name);
  579. }
  580. furi_string_free(filename);
  581. }
  582. return res;
  583. }
  584. void seader_credential_clear(SeaderCredential* cred) {
  585. furi_assert(cred);
  586. memset(cred->name, 0, sizeof(cred->name));
  587. cred->credential = 0;
  588. cred->bit_length = 0;
  589. cred->type = SeaderCredentialTypeNone;
  590. memset(cred->sio, 0, sizeof(cred->sio));
  591. cred->sio_len = 0;
  592. memset(cred->diversifier, 0, sizeof(cred->diversifier));
  593. cred->diversifier_len = 0;
  594. furi_string_reset(cred->load_path);
  595. }
  596. bool seader_credential_delete(SeaderCredential* cred, bool use_load_path) {
  597. furi_assert(cred);
  598. bool deleted = false;
  599. FuriString* file_path;
  600. file_path = furi_string_alloc();
  601. do {
  602. // Delete original file
  603. if(use_load_path && !furi_string_empty(cred->load_path)) {
  604. furi_string_set(file_path, cred->load_path);
  605. } else {
  606. furi_string_printf(file_path, APP_DATA_PATH("%s%s"), cred->name, SEADER_APP_EXTENSION);
  607. }
  608. if(!storage_simply_remove(cred->storage, furi_string_get_cstr(file_path))) break;
  609. deleted = true;
  610. } while(0);
  611. if(!deleted) {
  612. dialog_message_show_storage_error(cred->dialogs, "Can not remove file");
  613. }
  614. furi_string_free(file_path);
  615. return deleted;
  616. }
  617. void seader_credential_set_loading_callback(
  618. SeaderCredential* cred,
  619. SeaderLoadingCallback callback,
  620. void* context) {
  621. furi_assert(cred);
  622. cred->loading_cb = callback;
  623. cred->loading_cb_ctx = context;
  624. }