seader_credential.c 14 KB

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