seader_credential.c 15 KB

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