seader_credential.c 22 KB

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