picopass_device.c 14 KB

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