uhf_device.c 11 KB

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