seader_credential.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. if(cred->loading_cb) {
  48. cred->loading_cb(cred->loading_cb_ctx, true);
  49. }
  50. do {
  51. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(path))) break;
  52. // Read and verify file header
  53. uint32_t version = 0;
  54. if(!flipper_format_read_header(file, temp_str, &version)) break;
  55. if(furi_string_cmp_str(temp_str, seader_file_header) || (version != seader_file_version)) {
  56. deprecated_version = true;
  57. break;
  58. }
  59. if(!flipper_format_read_uint32(file, "Bits", (uint32_t*)&(cred->bit_length), 1)) break;
  60. if(!flipper_format_read_hex(
  61. file, "Credential", (uint8_t*)&cred->credential, sizeof(cred->credential)))
  62. break;
  63. // The order is reversed for storage and for the user opening the file
  64. uint64_t swapped = __builtin_bswap64(cred->credential);
  65. cred->credential = swapped;
  66. // Optional SIO/Diversifier
  67. flipper_format_read_hex(file, "SIO", cred->sio, sizeof(cred->sio));
  68. flipper_format_read_hex(file, "Diversifier", cred->diversifier, sizeof(cred->diversifier));
  69. parsed = true;
  70. } while(false);
  71. if(cred->loading_cb) {
  72. cred->loading_cb(cred->loading_cb_ctx, false);
  73. }
  74. if((!parsed) && (show_dialog)) {
  75. if(deprecated_version) {
  76. dialog_message_show_storage_error(cred->dialogs, "File format deprecated");
  77. } else {
  78. dialog_message_show_storage_error(cred->dialogs, "Can not parse\nfile");
  79. }
  80. }
  81. FURI_LOG_I(TAG, "PACS: (%d) %016llx", cred->bit_length, cred->credential);
  82. furi_string_free(temp_str);
  83. flipper_format_free(file);
  84. return parsed;
  85. }
  86. bool seader_credential_save_agnostic(SeaderCredential* cred, const char* name) {
  87. furi_assert(cred);
  88. bool use_load_path = true;
  89. bool saved = false;
  90. FlipperFormat* file = flipper_format_file_alloc(cred->storage);
  91. FuriString* temp_str;
  92. temp_str = furi_string_alloc();
  93. do {
  94. if(use_load_path && !furi_string_empty(cred->load_path)) {
  95. // Get directory name
  96. path_extract_dirname(furi_string_get_cstr(cred->load_path), temp_str);
  97. // Make path to file to save
  98. furi_string_cat_printf(temp_str, "/%s%s", name, SEADER_APP_EXTENSION);
  99. } else {
  100. furi_string_printf(
  101. temp_str, "%s/%s%s", STORAGE_APP_DATA_PATH_PREFIX, name, SEADER_APP_EXTENSION);
  102. }
  103. // Open file
  104. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  105. if(!flipper_format_write_header_cstr(file, seader_file_header, seader_file_version)) break;
  106. if(!flipper_format_write_uint32(file, "Bits", (uint32_t*)&cred->bit_length, 1)) break;
  107. uint64_t swapped = __builtin_bswap64(cred->credential);
  108. if(!flipper_format_write_hex(
  109. file, "Credential", (uint8_t*)&swapped, sizeof(cred->credential)))
  110. break;
  111. if(cred->sio[0] == 0x30) {
  112. if(!flipper_format_write_hex(file, "SIO", cred->sio, sizeof(cred->sio))) break;
  113. if(!flipper_format_write_hex(
  114. file, "Diversifier", cred->diversifier, sizeof(cred->diversifier)))
  115. break;
  116. }
  117. saved = true;
  118. } while(false);
  119. if(!saved) {
  120. dialog_message_show_storage_error(cred->dialogs, "Can not save\nfile");
  121. }
  122. furi_string_free(temp_str);
  123. flipper_format_free(file);
  124. return saved;
  125. }
  126. bool seader_credential_save(SeaderCredential* cred, const char* name) {
  127. uint8_t zero[PICOPASS_BLOCK_LEN] = {0};
  128. uint8_t csn[PICOPASS_BLOCK_LEN] = {0x7a, 0xf5, 0x31, 0x13, 0xfe, 0xff, 0x12, 0xe0};
  129. uint8_t cfg[PICOPASS_BLOCK_LEN] = {0x12, 0xff, 0xff, 0xff, 0x7f, 0x1f, 0xff, 0x3c};
  130. uint8_t epurse[PICOPASS_BLOCK_LEN] = {0xff, 0xff, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xff};
  131. uint8_t debit_key[PICOPASS_BLOCK_LEN] = {0xe3, 0xf3, 0x07, 0x84, 0x4a, 0x0b, 0x62, 0x04};
  132. uint8_t aia[PICOPASS_BLOCK_LEN] = {0xFF, 0xff, 0xff, 0xff, 0xFF, 0xFf, 0xff, 0xFF};
  133. uint8_t pacs_cfg[PICOPASS_BLOCK_LEN] = {0x03, 0x03, 0x03, 0x03, 0x00, 0x03, 0xe0, 0x14};
  134. if(cred->save_format == SeaderCredentialSaveFormatAgnostic) {
  135. return seader_credential_save_agnostic(cred, name);
  136. } else if(
  137. cred->save_format == SeaderCredentialSaveFormatPicopass ||
  138. cred->save_format == SeaderCredentialSaveFormatSR) {
  139. bool use_load_path = true;
  140. bool saved = false;
  141. bool withSIO = cred->save_format == SeaderCredentialSaveFormatSR;
  142. FlipperFormat* file = flipper_format_file_alloc(cred->storage);
  143. FuriString* temp_str = furi_string_alloc();
  144. if(use_load_path && !furi_string_empty(cred->load_path)) {
  145. // Get directory name
  146. path_extract_dirname(furi_string_get_cstr(cred->load_path), temp_str);
  147. // Make path to file to save
  148. furi_string_cat_printf(temp_str, "/%s%s", name, ".picopass");
  149. } else {
  150. furi_string_printf(
  151. temp_str, "%s/%s%s", STORAGE_APP_DATA_PATH_PREFIX, name, ".picopass");
  152. }
  153. FURI_LOG_D(TAG, "Save as Picopass [%s]", furi_string_get_cstr(temp_str));
  154. uint64_t sentinel = 1ULL << cred->bit_length;
  155. uint64_t swapped = __builtin_bswap64(cred->credential | sentinel);
  156. // FURI_LOG_D(TAG, "PACS: (%d) %016llx | %016llx => %016llx", cred->bit_length, cred->credential, sentinel, swapped);
  157. do {
  158. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  159. if(!flipper_format_write_header_cstr(file, "Flipper Picopass device", 1)) break;
  160. if(!flipper_format_write_comment_cstr(
  161. file, "Picopass blocks generated from Seader app"))
  162. break;
  163. bool block_saved = true;
  164. for(size_t i = 0; i < 20; i++) {
  165. furi_string_printf(temp_str, "Block %d", i);
  166. switch(i) {
  167. case CSN_INDEX:
  168. if(!flipper_format_write_hex(
  169. file, furi_string_get_cstr(temp_str), csn, sizeof(csn))) {
  170. block_saved = false;
  171. }
  172. break;
  173. case EPURSE_INDEX:
  174. if(!flipper_format_write_hex(
  175. file, furi_string_get_cstr(temp_str), epurse, PICOPASS_BLOCK_LEN)) {
  176. block_saved = false;
  177. }
  178. break;
  179. case KD_INDEX:
  180. if(!flipper_format_write_hex(
  181. file, furi_string_get_cstr(temp_str), debit_key, PICOPASS_BLOCK_LEN)) {
  182. block_saved = false;
  183. }
  184. break;
  185. case AIA_INDEX:
  186. if(!flipper_format_write_hex(
  187. file, furi_string_get_cstr(temp_str), aia, PICOPASS_BLOCK_LEN)) {
  188. block_saved = false;
  189. }
  190. break;
  191. case CFG_INDEX:
  192. if(!flipper_format_write_hex(
  193. file, furi_string_get_cstr(temp_str), cfg, sizeof(cfg))) {
  194. block_saved = false;
  195. }
  196. break;
  197. case PACS_CFG_INDEX:
  198. if(withSIO) {
  199. pacs_cfg[0] = 0xA3;
  200. }
  201. if(!flipper_format_write_hex(
  202. file, furi_string_get_cstr(temp_str), pacs_cfg, sizeof(pacs_cfg))) {
  203. block_saved = false;
  204. }
  205. break;
  206. case PACS_INDEX:
  207. if(!flipper_format_write_hex(
  208. file,
  209. furi_string_get_cstr(temp_str),
  210. (uint8_t*)&swapped,
  211. PICOPASS_BLOCK_LEN)) {
  212. block_saved = false;
  213. }
  214. break;
  215. case SR_SIO_INDEX:
  216. case SR_SIO_INDEX + 1:
  217. case SR_SIO_INDEX + 2:
  218. case SR_SIO_INDEX + 3:
  219. case SR_SIO_INDEX + 4:
  220. case SR_SIO_INDEX + 5:
  221. case SR_SIO_INDEX + 6:
  222. case SR_SIO_INDEX + 7:
  223. if(withSIO) {
  224. if(!flipper_format_write_hex(
  225. file,
  226. furi_string_get_cstr(temp_str),
  227. cred->sio + ((i - SR_SIO_INDEX) * PICOPASS_BLOCK_LEN),
  228. PICOPASS_BLOCK_LEN)) {
  229. block_saved = false;
  230. }
  231. } else {
  232. if(!flipper_format_write_hex(
  233. file, furi_string_get_cstr(temp_str), zero, sizeof(zero))) {
  234. block_saved = false;
  235. }
  236. }
  237. break;
  238. default:
  239. if(!flipper_format_write_hex(
  240. file, furi_string_get_cstr(temp_str), zero, sizeof(zero))) {
  241. block_saved = false;
  242. }
  243. break;
  244. };
  245. if(!block_saved) {
  246. break;
  247. }
  248. }
  249. saved = true;
  250. } while(false);
  251. if(!saved) {
  252. dialog_message_show_storage_error(cred->dialogs, "Can not save\nfile");
  253. }
  254. furi_string_free(temp_str);
  255. flipper_format_free(file);
  256. return saved;
  257. } else if(cred->save_format == SeaderCredentialSaveFormatRFID) {
  258. bool result = false;
  259. FuriString* file_path = furi_string_alloc();
  260. furi_string_printf(file_path, "%s/%s%s", ANY_PATH("lfrfid"), name, ".rfid");
  261. ProtocolDict* dict = protocol_dict_alloc(lfrfid_protocols, LFRFIDProtocolMax);
  262. ProtocolId protocol = LFRFIDProtocolHidGeneric;
  263. uint64_t target = 0;
  264. if(cred->bit_length == 26) {
  265. //3 bytes
  266. protocol = LFRFIDProtocolH10301;
  267. // Remove parity
  268. target = (cred->credential >> 1) & 0xFFFFFF;
  269. // Reverse order since it'll get reversed again
  270. target = __builtin_bswap64(target) >> (64 - 24);
  271. } else if(cred->bit_length <= 43) {
  272. //6 bytes
  273. protocol = LFRFIDProtocolHidGeneric;
  274. target = cred->credential;
  275. target = __builtin_bswap64(target) >> (64 - 48);
  276. } else {
  277. //8 bytes
  278. protocol = LFRFIDProtocolHidExGeneric;
  279. target = cred->credential;
  280. target = __builtin_bswap64(target);
  281. }
  282. size_t data_size = protocol_dict_get_data_size(dict, protocol);
  283. uint8_t* data = malloc(data_size);
  284. if(data_size < 8) {
  285. memcpy(data, (void*)&target, data_size);
  286. } else {
  287. // data_size 12 for LFRFIDProtocolHidExGeneric
  288. memcpy(data + 4, (void*)&target, 8);
  289. }
  290. protocol_dict_set_data(dict, protocol, data, data_size);
  291. free(data);
  292. result = lfrfid_dict_file_save(dict, protocol, furi_string_get_cstr(file_path));
  293. if(result) {
  294. FURI_LOG_D(TAG, "Written: %d", result);
  295. } else {
  296. FURI_LOG_D(TAG, "Failed to write");
  297. }
  298. furi_string_free(file_path);
  299. protocol_dict_free(dict);
  300. return result;
  301. }
  302. return false;
  303. }
  304. bool seader_file_select(SeaderCredential* cred) {
  305. furi_assert(cred);
  306. FuriString* seader_app_folder = furi_string_alloc_set(STORAGE_APP_DATA_PATH_PREFIX);
  307. DialogsFileBrowserOptions browser_options;
  308. dialog_file_browser_set_basic_options(&browser_options, SEADER_APP_EXTENSION, &I_Nfc_10px);
  309. browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
  310. bool res = dialog_file_browser_show(
  311. cred->dialogs, cred->load_path, seader_app_folder, &browser_options);
  312. furi_string_free(seader_app_folder);
  313. if(res) {
  314. FuriString* filename;
  315. filename = furi_string_alloc();
  316. path_extract_filename(cred->load_path, filename, true);
  317. strncpy(cred->name, furi_string_get_cstr(filename), SEADER_CRED_NAME_MAX_LEN);
  318. res = seader_credential_load(cred, cred->load_path, true);
  319. if(res) {
  320. seader_credential_set_name(cred, cred->name);
  321. }
  322. furi_string_free(filename);
  323. }
  324. return res;
  325. }
  326. void seader_credential_clear(SeaderCredential* cred) {
  327. furi_assert(cred);
  328. cred->credential = 0;
  329. cred->bit_length = 0;
  330. cred->type = SeaderCredentialTypeNone;
  331. furi_string_reset(cred->load_path);
  332. }
  333. bool seader_credential_delete(SeaderCredential* cred, bool use_load_path) {
  334. furi_assert(cred);
  335. bool deleted = false;
  336. FuriString* file_path;
  337. file_path = furi_string_alloc();
  338. do {
  339. // Delete original file
  340. if(use_load_path && !furi_string_empty(cred->load_path)) {
  341. furi_string_set(file_path, cred->load_path);
  342. } else {
  343. furi_string_printf(file_path, APP_DATA_PATH("%s%s"), cred->name, SEADER_APP_EXTENSION);
  344. }
  345. if(!storage_simply_remove(cred->storage, furi_string_get_cstr(file_path))) break;
  346. deleted = true;
  347. } while(0);
  348. if(!deleted) {
  349. dialog_message_show_storage_error(cred->dialogs, "Can not remove file");
  350. }
  351. furi_string_free(file_path);
  352. return deleted;
  353. }
  354. void seader_credential_set_loading_callback(
  355. SeaderCredential* cred,
  356. SeaderLoadingCallback callback,
  357. void* context) {
  358. furi_assert(cred);
  359. cred->loading_cb = callback;
  360. cred->loading_cb_ctx = context;
  361. }