seader_credential.c 23 KB

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