uhf_device.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #include "uhf_device.h"
  2. #include <toolbox/path.h>
  3. #include <flipper_format/flipper_format.h>
  4. #include <uhf_rfid_icons.h>
  5. #define TAG "UHFDevice"
  6. static const char* uhf_file_header = "Flipper UHF RFID device";
  7. static const uint32_t uhf_file_version = 1;
  8. // static const uint8_t bank_data_start = 20;
  9. // static const uint8_t bank_data_length = 16;
  10. UHFDevice* uhf_device_alloc() {
  11. UHFDevice* uhf_device = malloc(sizeof(UHFDevice));
  12. uhf_device->storage = furi_record_open(RECORD_STORAGE);
  13. uhf_device->dialogs = furi_record_open(RECORD_DIALOGS);
  14. uhf_device->load_path = furi_string_alloc();
  15. return uhf_device;
  16. }
  17. void uhf_device_set_name(UHFDevice* dev, const char* name) {
  18. furi_assert(dev);
  19. strlcpy(dev->dev_name, name, UHF_DEV_NAME_MAX_LEN);
  20. }
  21. static bool uhf_device_save_file(
  22. UHFDevice* dev,
  23. const char* dev_name,
  24. const char* folder,
  25. const char* extension,
  26. bool use_load_path) {
  27. furi_assert(dev);
  28. UHFTag* uhf_tag = dev->uhf_tag;
  29. bool saved = false;
  30. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  31. FuriString* temp_str;
  32. temp_str = furi_string_alloc();
  33. FURI_LOG_E("TAG", "epc_len = %d", uhf_tag->epc_length);
  34. FURI_LOG_E("TAG", "tid_len = %d", uhf_tag->tid_length);
  35. FURI_LOG_E("TAG", "user_len = %d", uhf_tag->user_length);
  36. do {
  37. if(use_load_path && !furi_string_empty(dev->load_path)) {
  38. // Get directory name
  39. path_extract_dirname(furi_string_get_cstr(dev->load_path), temp_str);
  40. // Make path to file to save
  41. furi_string_cat_printf(temp_str, "/%s%s", dev_name, extension);
  42. } else {
  43. // First remove uhf device file if it was saved
  44. furi_string_printf(temp_str, "%s/%s%s", folder, dev_name, extension);
  45. }
  46. // Open file
  47. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  48. // Write header
  49. if(!flipper_format_write_header_cstr(file, uhf_file_header, uhf_file_version)) break;
  50. // Reserved bank might be added
  51. // todo : maybe
  52. uint32_t temp_arr[1];
  53. // write epc
  54. temp_arr[0] = uhf_tag->epc_length;
  55. if(!flipper_format_write_uint32(file, UHF_EPC_BANK_LENGTH_LABEL, temp_arr, 1)) break;
  56. if(!flipper_format_write_hex(file, UHF_EPC_BANK_LABEL, uhf_tag->epc, uhf_tag->epc_length))
  57. break;
  58. // write tid
  59. temp_arr[0] = uhf_tag->tid_length;
  60. if(!flipper_format_write_uint32(file, UHF_TID_BANK_LENGTH_LABEL, temp_arr, 1)) break;
  61. if(!flipper_format_write_hex(file, UHF_TID_BANK_LABEL, uhf_tag->tid, uhf_tag->tid_length))
  62. break;
  63. // write user
  64. temp_arr[0] = uhf_tag->user_length;
  65. if(!flipper_format_write_uint32(file, UHF_USER_BANK_LENGTH_LABEL, temp_arr, 1)) break;
  66. if(!flipper_format_write_hex(
  67. file, UHF_USER_BANK_LABEL, uhf_tag->user, uhf_tag->user_length))
  68. break;
  69. saved = true;
  70. } while(0);
  71. if(!saved) {
  72. dialog_message_show_storage_error(dev->dialogs, "Can not save\nfile");
  73. }
  74. furi_string_free(temp_str);
  75. flipper_format_free(file);
  76. return saved;
  77. }
  78. bool uhf_device_save(UHFDevice* dev, const char* dev_name) {
  79. return uhf_device_save_file(
  80. dev, dev_name, STORAGE_APP_DATA_PATH_PREFIX, UHF_APP_EXTENSION, true);
  81. return false;
  82. }
  83. // uncomment
  84. static bool uhf_device_load_data(UHFDevice* dev, FuriString* path, bool show_dialog) {
  85. bool parsed = false;
  86. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  87. // UHFResponseData* uhf_response_data = dev->dev_data;
  88. FuriString* temp_str;
  89. temp_str = furi_string_alloc();
  90. bool deprecated_version = false;
  91. UHFTag* uhf_tag = dev->uhf_tag;
  92. uint32_t temp_arr[1];
  93. if(dev->loading_cb) {
  94. dev->loading_cb(dev->loading_cb_ctx, true);
  95. }
  96. do {
  97. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(path))) break;
  98. // Read and verify file header
  99. uint32_t version = 0;
  100. if(!flipper_format_read_header(file, temp_str, &version)) break;
  101. if(furi_string_cmp_str(temp_str, uhf_file_header) || (version != uhf_file_version)) {
  102. deprecated_version = true;
  103. break;
  104. }
  105. // read epc
  106. if(!flipper_format_read_uint32(file, UHF_EPC_BANK_LENGTH_LABEL, temp_arr, 1)) break;
  107. uhf_tag->epc_length = temp_arr[0];
  108. if(!flipper_format_read_hex(file, UHF_EPC_BANK_LABEL, uhf_tag->epc, uhf_tag->epc_length))
  109. break;
  110. // read tid
  111. if(!flipper_format_read_uint32(file, UHF_TID_BANK_LENGTH_LABEL, temp_arr, 1)) break;
  112. uhf_tag->tid_length = temp_arr[0];
  113. if(!flipper_format_read_hex(file, UHF_TID_BANK_LABEL, uhf_tag->tid, uhf_tag->tid_length))
  114. break;
  115. // read user
  116. if(!flipper_format_read_uint32(file, UHF_USER_BANK_LENGTH_LABEL, temp_arr, 1)) break;
  117. uhf_tag->user_length = temp_arr[0];
  118. if(!flipper_format_read_hex(file, UHF_USER_BANK_LABEL, uhf_tag->user, uhf_tag->user_length))
  119. break;
  120. parsed = true;
  121. } while(false);
  122. if(dev->loading_cb) {
  123. dev->loading_cb(dev->loading_cb_ctx, false);
  124. }
  125. if((!parsed) && (show_dialog)) {
  126. if(deprecated_version) {
  127. dialog_message_show_storage_error(dev->dialogs, "File format deprecated");
  128. } else {
  129. dialog_message_show_storage_error(dev->dialogs, "Can not parse\nfile");
  130. }
  131. }
  132. furi_string_free(temp_str);
  133. flipper_format_free(file);
  134. return parsed;
  135. }
  136. // void picopass_device_clear(UHFDevice* dev) {
  137. // furi_assert(dev);
  138. // picopass_device_data_clear(&dev->dev_data);
  139. // memset(&dev->dev_data, 0, sizeof(dev->dev_data));
  140. // dev->format = PicopassDeviceSaveFormatHF;
  141. // furi_string_reset(dev->load_path);
  142. // }
  143. void uhf_device_free(UHFDevice* uhf_dev) {
  144. furi_assert(uhf_dev);
  145. furi_record_close(RECORD_STORAGE);
  146. furi_record_close(RECORD_DIALOGS);
  147. furi_string_free(uhf_dev->load_path);
  148. free(uhf_dev);
  149. }
  150. bool uhf_file_select(UHFDevice* dev) {
  151. furi_assert(dev);
  152. FuriString* uhf_app_folder;
  153. uhf_app_folder = furi_string_alloc_set(STORAGE_APP_DATA_PATH_PREFIX);
  154. DialogsFileBrowserOptions browser_options;
  155. dialog_file_browser_set_basic_options(&browser_options, UHF_APP_EXTENSION, &I_Nfc_10px);
  156. browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
  157. bool res =
  158. dialog_file_browser_show(dev->dialogs, dev->load_path, uhf_app_folder, &browser_options);
  159. furi_string_free(uhf_app_folder);
  160. if(res) {
  161. FuriString* filename;
  162. filename = furi_string_alloc();
  163. path_extract_filename(dev->load_path, filename, true);
  164. strncpy(dev->dev_name, furi_string_get_cstr(filename), UHF_DEV_NAME_MAX_LEN);
  165. res = uhf_device_load_data(dev, dev->load_path, true);
  166. if(res) {
  167. uhf_device_set_name(dev, dev->dev_name);
  168. }
  169. furi_string_free(filename);
  170. }
  171. return res;
  172. }
  173. // void uhf_device_data_clear(UHFDevice* dev_data) {
  174. // for(size_t i = 0; i < PICOPASS_MAX_APP_LIMIT; i++) {
  175. // memset(dev_data->AA1[i].data, 0, sizeof(dev_data->AA1[i].data));
  176. // }
  177. // dev_data->pacs.legacy = false;
  178. // dev_data->pacs.se_enabled = false;
  179. // dev_data->pacs.elite_kdf = false;
  180. // dev_data->pacs.pin_length = 0;
  181. // }
  182. bool uhf_device_delete(UHFDevice* dev, bool use_load_path) {
  183. furi_assert(dev);
  184. bool deleted = false;
  185. FuriString* file_path;
  186. file_path = furi_string_alloc();
  187. do {
  188. // Delete original file
  189. if(use_load_path && !furi_string_empty(dev->load_path)) {
  190. furi_string_set(file_path, dev->load_path);
  191. } else {
  192. furi_string_printf(file_path, APP_DATA_PATH("%s%s"), dev->dev_name, UHF_APP_EXTENSION);
  193. }
  194. if(!storage_simply_remove(dev->storage, furi_string_get_cstr(file_path))) break;
  195. deleted = true;
  196. } while(0);
  197. if(!deleted) {
  198. dialog_message_show_storage_error(dev->dialogs, "Can not remove file");
  199. }
  200. furi_string_free(file_path);
  201. return deleted;
  202. }
  203. void uhf_device_set_loading_callback(UHFDevice* dev, UHFLoadingCallback callback, void* context) {
  204. furi_assert(dev);
  205. dev->loading_cb = callback;
  206. dev->loading_cb_ctx = context;
  207. }
  208. // ReturnCode picopass_device_decrypt(uint8_t* enc_data, uint8_t* dec_data) {
  209. // uint8_t key[32] = {0};
  210. // memcpy(key, picopass_iclass_decryptionkey, sizeof(picopass_iclass_decryptionkey));
  211. // mbedtls_des3_context ctx;
  212. // mbedtls_des3_init(&ctx);
  213. // mbedtls_des3_set2key_dec(&ctx, key);
  214. // mbedtls_des3_crypt_ecb(&ctx, enc_data, dec_data);
  215. // mbedtls_des3_free(&ctx);
  216. // return ERR_NONE;
  217. // }
  218. // ReturnCode picopass_device_parse_credential(PicopassBlock* AA1, PicopassPacs* pacs) {
  219. // ReturnCode err;
  220. // pacs->biometrics = AA1[6].data[4];
  221. // pacs->pin_length = AA1[6].data[6] & 0x0F;
  222. // pacs->encryption = AA1[6].data[7];
  223. // if(pacs->encryption == PicopassDeviceEncryption3DES) {
  224. // FURI_LOG_D(TAG, "3DES Encrypted");
  225. // err = picopass_device_decrypt(AA1[7].data, pacs->credential);
  226. // if(err != ERR_NONE) {
  227. // FURI_LOG_E(TAG, "decrypt error %d", err);
  228. // return err;
  229. // }
  230. // err = picopass_device_decrypt(AA1[8].data, pacs->pin0);
  231. // if(err != ERR_NONE) {
  232. // FURI_LOG_E(TAG, "decrypt error %d", err);
  233. // return err;
  234. // }
  235. // err = picopass_device_decrypt(AA1[9].data, pacs->pin1);
  236. // if(err != ERR_NONE) {
  237. // FURI_LOG_E(TAG, "decrypt error %d", err);
  238. // return err;
  239. // }
  240. // } else if(pacs->encryption == PicopassDeviceEncryptionNone) {
  241. // FURI_LOG_D(TAG, "No Encryption");
  242. // memcpy(pacs->credential, AA1[7].data, PICOPASS_BLOCK_LEN);
  243. // memcpy(pacs->pin0, AA1[8].data, PICOPASS_BLOCK_LEN);
  244. // memcpy(pacs->pin1, AA1[9].data, PICOPASS_BLOCK_LEN);
  245. // } else if(pacs->encryption == PicopassDeviceEncryptionDES) {
  246. // FURI_LOG_D(TAG, "DES Encrypted");
  247. // } else {
  248. // FURI_LOG_D(TAG, "Unknown encryption");
  249. // }
  250. // pacs->sio = (AA1[10].data[0] == 0x30); // rough check
  251. // return ERR_NONE;
  252. // }
  253. // ReturnCode picopass_device_parse_wiegand(uint8_t* data, PicopassWiegandRecord* record) {
  254. // uint32_t* halves = (uint32_t*)data;
  255. // if(halves[0] == 0) {
  256. // uint8_t leading0s = __builtin_clz(REVERSE_BYTES_U32(halves[1]));
  257. // record->bitLength = 31 - leading0s;
  258. // } else {
  259. // uint8_t leading0s = __builtin_clz(REVERSE_BYTES_U32(halves[0]));
  260. // record->bitLength = 63 - leading0s;
  261. // }
  262. // FURI_LOG_D(TAG, "bitLength: %d", record->bitLength);
  263. // if(record->bitLength == 26) {
  264. // uint8_t* v4 = data + 4;
  265. // uint32_t bot = v4[3] | (v4[2] << 8) | (v4[1] << 16) | (v4[0] << 24);
  266. // record->CardNumber = (bot >> 1) & 0xFFFF;
  267. // record->FacilityCode = (bot >> 17) & 0xFF;
  268. // FURI_LOG_D(TAG, "FC: %u CN: %u", record->FacilityCode, record->CardNumber);
  269. // record->valid = true;
  270. // } else {
  271. // record->CardNumber = 0;
  272. // record->FacilityCode = 0;
  273. // record->valid = false;
  274. // }
  275. // return ERR_NONE;
  276. // }