picopass_device.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. #include "picopass_device.h"
  2. #include <toolbox/path.h>
  3. #include <flipper_format/flipper_format.h>
  4. #include <picopass_icons.h>
  5. #include <assets_icons.h>
  6. #include <toolbox/protocols/protocol_dict.h>
  7. #include <lfrfid/protocols/lfrfid_protocols.h>
  8. #include <lfrfid/lfrfid_dict_file.h>
  9. #define TAG "PicopassDevice"
  10. static const char* picopass_file_header = "Flipper Picopass device";
  11. static const uint32_t picopass_file_version = 1;
  12. const uint8_t picopass_iclass_decryptionkey[] =
  13. {0xb4, 0x21, 0x2c, 0xca, 0xb7, 0xed, 0x21, 0x0f, 0x7b, 0x93, 0xd4, 0x59, 0x39, 0xc7, 0xdd, 0x36};
  14. PicopassDevice* picopass_device_alloc() {
  15. PicopassDevice* picopass_dev = malloc(sizeof(PicopassDevice));
  16. picopass_dev->dev_data.pacs.legacy = false;
  17. picopass_dev->dev_data.pacs.se_enabled = false;
  18. picopass_dev->dev_data.pacs.elite_kdf = false;
  19. picopass_dev->dev_data.pacs.pin_length = 0;
  20. picopass_dev->storage = furi_record_open(RECORD_STORAGE);
  21. picopass_dev->dialogs = furi_record_open(RECORD_DIALOGS);
  22. picopass_dev->load_path = furi_string_alloc();
  23. return picopass_dev;
  24. }
  25. void picopass_device_set_name(PicopassDevice* dev, const char* name) {
  26. furi_assert(dev);
  27. strlcpy(dev->dev_name, name, sizeof(dev->dev_name));
  28. }
  29. // For use with Seader's virtual card processing.
  30. static bool picopass_device_save_file_seader(
  31. PicopassDevice* dev,
  32. FlipperFormat* file,
  33. FuriString* file_path) {
  34. furi_assert(dev);
  35. PicopassPacs* pacs = &dev->dev_data.pacs;
  36. PicopassBlock* card_data = dev->dev_data.card_data;
  37. bool result = false;
  38. const char* seader_file_header = "Flipper Seader Credential";
  39. const uint32_t seader_file_version = 1;
  40. do {
  41. FURI_LOG_D(
  42. TAG,
  43. "Save %s %ld to %s",
  44. seader_file_header,
  45. seader_file_version,
  46. furi_string_get_cstr(file_path));
  47. storage_simply_mkdir(dev->storage, EXT_PATH("apps_data/seader"));
  48. if(!flipper_format_file_open_always(file, furi_string_get_cstr(file_path))) break;
  49. if(!flipper_format_write_header_cstr(file, seader_file_header, seader_file_version)) break;
  50. if(!flipper_format_write_uint32(file, "Bits", (uint32_t*)&pacs->bitLength, 1)) break;
  51. if(!flipper_format_write_hex(file, "Credential", pacs->credential, PICOPASS_BLOCK_LEN))
  52. break;
  53. // Seader only captures 64 byte SIO so I'm going to leave it at that
  54. uint8_t sio[64];
  55. // TODO: save SR vs SE more properly
  56. if(pacs->sio) { // SR
  57. for(uint8_t i = 0; i < 8; i++) {
  58. memcpy(sio + (i * 8), card_data[10 + i].data, PICOPASS_BLOCK_LEN);
  59. }
  60. if(!flipper_format_write_hex(file, "SIO", sio, sizeof(sio))) break;
  61. } else if(pacs->se_enabled) { //SE
  62. for(uint8_t i = 0; i < 8; i++) {
  63. memcpy(sio + (i * 8), card_data[6 + i].data, PICOPASS_BLOCK_LEN);
  64. }
  65. if(!flipper_format_write_hex(file, "SIO", sio, sizeof(sio))) break;
  66. }
  67. if(!flipper_format_write_hex(
  68. file, "Diversifier", card_data[PICOPASS_CSN_BLOCK_INDEX].data, PICOPASS_BLOCK_LEN))
  69. break;
  70. result = true;
  71. } while(false);
  72. return result;
  73. }
  74. static bool picopass_device_save_file_lfrfid(PicopassDevice* dev, FuriString* file_path) {
  75. furi_assert(dev);
  76. PicopassPacs* pacs = &dev->dev_data.pacs;
  77. ProtocolDict* dict = protocol_dict_alloc(lfrfid_protocols, LFRFIDProtocolMax);
  78. ProtocolId protocol = LFRFIDProtocolHidGeneric;
  79. bool result = false;
  80. uint64_t target = 0;
  81. uint64_t sentinel = 1ULL << pacs->bitLength;
  82. memcpy(&target, pacs->credential, PICOPASS_BLOCK_LEN);
  83. target = __builtin_bswap64(target);
  84. FURI_LOG_D(TAG, "Original (%d): %016llx", pacs->bitLength, target);
  85. if(pacs->bitLength == 26) {
  86. //3 bytes
  87. protocol = LFRFIDProtocolH10301;
  88. // Remove parity
  89. target = (target >> 1) & 0xFFFFFF;
  90. // Reverse order since it'll get reversed again
  91. target = __builtin_bswap64(target) >> (64 - 24);
  92. } else if(pacs->bitLength < 44) {
  93. // https://gist.github.com/blark/e8f125e402f576bdb7e2d7b3428bdba6
  94. protocol = LFRFIDProtocolHidGeneric;
  95. if(pacs->bitLength <= 36) {
  96. uint64_t header = 1ULL << 37;
  97. target = __builtin_bswap64((target | sentinel | header) << 4) >> (64 - 48);
  98. } else {
  99. target = __builtin_bswap64((target | sentinel) << 4) >> (64 - 48);
  100. }
  101. } else {
  102. //8 bytes
  103. protocol = LFRFIDProtocolHidExGeneric;
  104. target = __builtin_bswap64(target);
  105. }
  106. size_t data_size = protocol_dict_get_data_size(dict, protocol);
  107. uint8_t* data = malloc(data_size);
  108. if(data_size < 8) {
  109. memcpy(data, (void*)&target, data_size);
  110. } else {
  111. // data_size 12 for LFRFIDProtocolHidExGeneric
  112. memcpy(data + 4, (void*)&target, 8);
  113. }
  114. protocol_dict_set_data(dict, protocol, data, data_size);
  115. free(data);
  116. FuriString* briefStr;
  117. briefStr = furi_string_alloc();
  118. protocol_dict_render_brief_data(dict, briefStr, protocol);
  119. FURI_LOG_D(TAG, "LFRFID Brief: %s", furi_string_get_cstr(briefStr));
  120. furi_string_free(briefStr);
  121. result = lfrfid_dict_file_save(dict, protocol, furi_string_get_cstr(file_path));
  122. if(result) {
  123. FURI_LOG_D(TAG, "Written: %d", result);
  124. } else {
  125. FURI_LOG_D(TAG, "Failed to write");
  126. }
  127. protocol_dict_free(dict);
  128. return result;
  129. }
  130. static bool picopass_device_save_file(
  131. PicopassDevice* dev,
  132. const char* dev_name,
  133. const char* folder,
  134. const char* extension,
  135. bool use_load_path) {
  136. furi_assert(dev);
  137. FURI_LOG_D(TAG, "Save File");
  138. bool saved = false;
  139. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  140. PicopassPacs* pacs = &dev->dev_data.pacs;
  141. PicopassBlock* card_data = dev->dev_data.card_data;
  142. FuriString* temp_str;
  143. temp_str = furi_string_alloc();
  144. if(dev->format == PicopassDeviceSaveFormatPartial) {
  145. // Clear key that may have been set when doing key tests for legacy
  146. memset(card_data[PICOPASS_SECURE_KD_BLOCK_INDEX].data, 0, PICOPASS_BLOCK_LEN);
  147. }
  148. do {
  149. if(use_load_path && !furi_string_empty(dev->load_path)) {
  150. // Get directory name
  151. path_extract_dirname(furi_string_get_cstr(dev->load_path), temp_str);
  152. // Make path to file to save
  153. furi_string_cat_printf(temp_str, "/%s%s", dev_name, extension);
  154. } else {
  155. // First remove picopass device file if it was saved
  156. furi_string_printf(temp_str, "%s/%s%s", folder, dev_name, extension);
  157. }
  158. if(dev->format == PicopassDeviceSaveFormatHF ||
  159. dev->format == PicopassDeviceSaveFormatPartial) {
  160. // Open file
  161. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  162. // Write header
  163. if(!flipper_format_write_header_cstr(file, picopass_file_header, picopass_file_version))
  164. break;
  165. if(!flipper_format_write_hex(file, "Credential", pacs->credential, PICOPASS_BLOCK_LEN))
  166. break;
  167. // TODO: Add elite
  168. if(!flipper_format_write_comment_cstr(file, "Picopass blocks")) break;
  169. bool block_saved = true;
  170. size_t app_limit = card_data[PICOPASS_CONFIG_BLOCK_INDEX].data[0] <
  171. PICOPASS_MAX_APP_LIMIT ?
  172. card_data[PICOPASS_CONFIG_BLOCK_INDEX].data[0] :
  173. PICOPASS_MAX_APP_LIMIT;
  174. for(size_t i = 0; i < app_limit; i++) {
  175. furi_string_printf(temp_str, "Block %d", i);
  176. if(!flipper_format_write_hex(
  177. file,
  178. furi_string_get_cstr(temp_str),
  179. card_data[i].data,
  180. PICOPASS_BLOCK_LEN)) {
  181. block_saved = false;
  182. break;
  183. }
  184. }
  185. if(!block_saved) break;
  186. saved = true;
  187. } else if(dev->format == PicopassDeviceSaveFormatLF) {
  188. saved = picopass_device_save_file_lfrfid(dev, temp_str);
  189. } else if(dev->format == PicopassDeviceSaveFormatSeader) {
  190. saved = picopass_device_save_file_seader(dev, file, temp_str);
  191. }
  192. } while(false);
  193. if(!saved) {
  194. dialog_message_show_storage_error(dev->dialogs, "Can not save\nfile");
  195. }
  196. furi_string_free(temp_str);
  197. flipper_format_free(file);
  198. return saved;
  199. }
  200. bool picopass_device_save(PicopassDevice* dev, const char* dev_name) {
  201. if(dev->format == PicopassDeviceSaveFormatHF) {
  202. return picopass_device_save_file(
  203. dev, dev_name, STORAGE_APP_DATA_PATH_PREFIX, PICOPASS_APP_EXTENSION, true);
  204. } else if(dev->format == PicopassDeviceSaveFormatLF) {
  205. return picopass_device_save_file(dev, dev_name, ANY_PATH("lfrfid"), ".rfid", true);
  206. } else if(dev->format == PicopassDeviceSaveFormatSeader) {
  207. return picopass_device_save_file(
  208. dev, dev_name, EXT_PATH("apps_data/seader"), ".credential", true);
  209. } else if(dev->format == PicopassDeviceSaveFormatPartial) {
  210. return picopass_device_save_file(
  211. dev, dev_name, STORAGE_APP_DATA_PATH_PREFIX, PICOPASS_APP_EXTENSION, true);
  212. }
  213. return false;
  214. }
  215. static bool picopass_device_load_data(PicopassDevice* dev, FuriString* path, bool show_dialog) {
  216. bool parsed = false;
  217. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  218. PicopassBlock* card_data = dev->dev_data.card_data;
  219. PicopassPacs* pacs = &dev->dev_data.pacs;
  220. FuriString* temp_str;
  221. temp_str = furi_string_alloc();
  222. bool deprecated_version = false;
  223. if(dev->loading_cb) {
  224. dev->loading_cb(dev->loading_cb_ctx, true);
  225. }
  226. do {
  227. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(path))) break;
  228. // Read and verify file header
  229. uint32_t version = 0;
  230. if(!flipper_format_read_header(file, temp_str, &version)) break;
  231. if(furi_string_cmp_str(temp_str, picopass_file_header) ||
  232. (version != picopass_file_version)) {
  233. deprecated_version = true;
  234. break;
  235. }
  236. // Parse header blocks
  237. bool block_read = true;
  238. for(size_t i = 0; i < 6; i++) {
  239. furi_string_printf(temp_str, "Block %d", i);
  240. if(!flipper_format_read_hex(
  241. file, furi_string_get_cstr(temp_str), card_data[i].data, PICOPASS_BLOCK_LEN)) {
  242. block_read = false;
  243. break;
  244. }
  245. }
  246. size_t app_limit = card_data[PICOPASS_CONFIG_BLOCK_INDEX].data[0];
  247. // Fix for unpersonalized cards that have app_limit set to 0xFF
  248. if(app_limit > PICOPASS_MAX_APP_LIMIT) app_limit = PICOPASS_MAX_APP_LIMIT;
  249. for(size_t i = 6; i < app_limit; i++) {
  250. furi_string_printf(temp_str, "Block %d", i);
  251. if(!flipper_format_read_hex(
  252. file, furi_string_get_cstr(temp_str), card_data[i].data, PICOPASS_BLOCK_LEN)) {
  253. block_read = false;
  254. break;
  255. }
  256. }
  257. if(!block_read) break;
  258. picopass_device_parse_credential(card_data, pacs);
  259. picopass_device_parse_wiegand(pacs->credential, pacs);
  260. parsed = true;
  261. } while(false);
  262. if(dev->loading_cb) {
  263. dev->loading_cb(dev->loading_cb_ctx, false);
  264. }
  265. if((!parsed) && (show_dialog)) {
  266. if(deprecated_version) {
  267. dialog_message_show_storage_error(dev->dialogs, "File format deprecated");
  268. } else {
  269. dialog_message_show_storage_error(dev->dialogs, "Can not parse\nfile");
  270. }
  271. }
  272. furi_string_free(temp_str);
  273. flipper_format_free(file);
  274. return parsed;
  275. }
  276. void picopass_device_clear(PicopassDevice* dev) {
  277. furi_assert(dev);
  278. picopass_device_data_clear(&dev->dev_data);
  279. memset(&dev->dev_data, 0, sizeof(dev->dev_data));
  280. dev->format = PicopassDeviceSaveFormatHF;
  281. furi_string_reset(dev->load_path);
  282. }
  283. void picopass_device_free(PicopassDevice* picopass_dev) {
  284. furi_assert(picopass_dev);
  285. picopass_device_clear(picopass_dev);
  286. furi_record_close(RECORD_STORAGE);
  287. furi_record_close(RECORD_DIALOGS);
  288. furi_string_free(picopass_dev->load_path);
  289. free(picopass_dev);
  290. }
  291. bool picopass_file_select(PicopassDevice* dev) {
  292. furi_assert(dev);
  293. FuriString* picopass_app_folder;
  294. picopass_app_folder = furi_string_alloc_set(STORAGE_APP_DATA_PATH_PREFIX);
  295. DialogsFileBrowserOptions browser_options;
  296. dialog_file_browser_set_basic_options(&browser_options, PICOPASS_APP_EXTENSION, &I_Nfc_10px);
  297. browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
  298. bool res = dialog_file_browser_show(
  299. dev->dialogs, dev->load_path, picopass_app_folder, &browser_options);
  300. furi_string_free(picopass_app_folder);
  301. if(res) {
  302. FuriString* filename;
  303. filename = furi_string_alloc();
  304. path_extract_filename(dev->load_path, filename, true);
  305. strlcpy(dev->dev_name, furi_string_get_cstr(filename), sizeof(dev->dev_name));
  306. res = picopass_device_load_data(dev, dev->load_path, true);
  307. if(res) {
  308. picopass_device_set_name(dev, dev->dev_name);
  309. }
  310. furi_string_free(filename);
  311. }
  312. return res;
  313. }
  314. void picopass_device_data_clear(PicopassDeviceData* dev_data) {
  315. for(size_t i = 0; i < PICOPASS_MAX_APP_LIMIT; i++) {
  316. memset(dev_data->card_data[i].data, 0, sizeof(dev_data->card_data[i].data));
  317. }
  318. dev_data->pacs.legacy = false;
  319. dev_data->pacs.se_enabled = false;
  320. dev_data->pacs.elite_kdf = false;
  321. dev_data->pacs.pin_length = 0;
  322. }
  323. bool picopass_device_delete(PicopassDevice* dev, bool use_load_path) {
  324. furi_assert(dev);
  325. bool deleted = false;
  326. FuriString* file_path;
  327. file_path = furi_string_alloc();
  328. do {
  329. // Delete original file
  330. if(use_load_path && !furi_string_empty(dev->load_path)) {
  331. furi_string_set(file_path, dev->load_path);
  332. } else {
  333. furi_string_printf(
  334. file_path, APP_DATA_PATH("%s%s"), dev->dev_name, PICOPASS_APP_EXTENSION);
  335. }
  336. if(!storage_simply_remove(dev->storage, furi_string_get_cstr(file_path))) break;
  337. deleted = true;
  338. } while(0);
  339. if(!deleted) {
  340. dialog_message_show_storage_error(dev->dialogs, "Can not remove file");
  341. }
  342. furi_string_free(file_path);
  343. return deleted;
  344. }
  345. void picopass_device_set_loading_callback(
  346. PicopassDevice* dev,
  347. PicopassLoadingCallback callback,
  348. void* context) {
  349. furi_assert(dev);
  350. dev->loading_cb = callback;
  351. dev->loading_cb_ctx = context;
  352. }
  353. void picopass_device_decrypt(uint8_t* enc_data, uint8_t* dec_data) {
  354. uint8_t key[32] = {0};
  355. memcpy(key, picopass_iclass_decryptionkey, sizeof(picopass_iclass_decryptionkey));
  356. mbedtls_des3_context ctx;
  357. mbedtls_des3_init(&ctx);
  358. mbedtls_des3_set2key_dec(&ctx, key);
  359. mbedtls_des3_crypt_ecb(&ctx, enc_data, dec_data);
  360. mbedtls_des3_free(&ctx);
  361. }
  362. void picopass_device_parse_credential(PicopassBlock* card_data, PicopassPacs* pacs) {
  363. pacs->biometrics = card_data[6].data[4];
  364. pacs->pin_length = card_data[6].data[6] & 0x0F;
  365. pacs->encryption = card_data[6].data[7];
  366. if(pacs->encryption == PicopassDeviceEncryption3DES) {
  367. FURI_LOG_D(TAG, "3DES Encrypted");
  368. picopass_device_decrypt(card_data[7].data, pacs->credential);
  369. picopass_device_decrypt(card_data[8].data, pacs->pin0);
  370. picopass_device_decrypt(card_data[9].data, pacs->pin1);
  371. } else if(pacs->encryption == PicopassDeviceEncryptionNone) {
  372. FURI_LOG_D(TAG, "No Encryption");
  373. memcpy(pacs->credential, card_data[7].data, PICOPASS_BLOCK_LEN);
  374. memcpy(pacs->pin0, card_data[8].data, PICOPASS_BLOCK_LEN);
  375. memcpy(pacs->pin1, card_data[9].data, PICOPASS_BLOCK_LEN);
  376. } else if(pacs->encryption == PicopassDeviceEncryptionDES) {
  377. FURI_LOG_D(TAG, "DES Encrypted");
  378. } else {
  379. FURI_LOG_D(TAG, "Unknown encryption");
  380. }
  381. pacs->sio = (card_data[10].data[0] == 0x30); // rough check
  382. }
  383. void picopass_device_parse_wiegand(uint8_t* credential, PicopassPacs* pacs) {
  384. uint32_t* halves = (uint32_t*)credential;
  385. if(halves[0] == 0) {
  386. uint8_t leading0s = __builtin_clz(REVERSE_BYTES_U32(halves[1]));
  387. pacs->bitLength = 31 - leading0s;
  388. } else {
  389. uint8_t leading0s = __builtin_clz(REVERSE_BYTES_U32(halves[0]));
  390. pacs->bitLength = 63 - leading0s;
  391. }
  392. // Remove sentinel bit from credential. Byteswapping to handle array of bytes vs 64bit value
  393. uint64_t sentinel = __builtin_bswap64(1ULL << pacs->bitLength);
  394. uint64_t swapped = 0;
  395. memcpy(&swapped, credential, sizeof(uint64_t));
  396. swapped = swapped ^ sentinel;
  397. memcpy(credential, &swapped, sizeof(uint64_t));
  398. FURI_LOG_D(TAG, "PACS: (%d) %016llx", pacs->bitLength, swapped);
  399. }
  400. bool picopass_device_hid_csn(PicopassDevice* dev) {
  401. furi_assert(dev);
  402. PicopassBlock* card_data = dev->dev_data.card_data;
  403. uint8_t* csn = card_data[PICOPASS_CSN_BLOCK_INDEX].data;
  404. // From Proxmark3 RRG sourcecode
  405. bool isHidRange = (memcmp(csn + 5, "\xFF\x12\xE0", 3) == 0) && ((csn[4] & 0xF0) == 0xF0);
  406. return isHidRange;
  407. }