uhf_device.c 11 KB

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