seader_credential.c 15 KB

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