uhf_device.c 11 KB

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