uhf_device.c 11 KB

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