seader_credential.c 13 KB

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