nfc_device.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. #include "nfc_device.h"
  2. #include "nfc_types.h"
  3. #include <lib/toolbox/path.h>
  4. #include <lib/toolbox/hex.h>
  5. #include "protocols/nfc_util.h"
  6. #include <flipper_format/flipper_format.h>
  7. #define TAG "NfcDevice"
  8. #define NFC_DEVICE_KEYS_FOLDER EXT_PATH("nfc/.cache")
  9. #define NFC_DEVICE_KEYS_EXTENSION ".keys"
  10. static const char* nfc_file_header = "Flipper NFC device";
  11. static const uint32_t nfc_file_version = 3;
  12. static const char* nfc_keys_file_header = "Flipper NFC keys";
  13. static const uint32_t nfc_keys_file_version = 1;
  14. // Protocols format versions
  15. static const uint32_t nfc_mifare_classic_data_format_version = 2;
  16. static const uint32_t nfc_mifare_ultralight_data_format_version = 1;
  17. NfcDevice* nfc_device_alloc() {
  18. NfcDevice* nfc_dev = malloc(sizeof(NfcDevice));
  19. nfc_dev->storage = furi_record_open(RECORD_STORAGE);
  20. nfc_dev->dialogs = furi_record_open(RECORD_DIALOGS);
  21. nfc_dev->load_path = furi_string_alloc();
  22. nfc_dev->dev_data.parsed_data = furi_string_alloc();
  23. nfc_dev->folder = furi_string_alloc();
  24. return nfc_dev;
  25. }
  26. void nfc_device_free(NfcDevice* nfc_dev) {
  27. furi_assert(nfc_dev);
  28. nfc_device_clear(nfc_dev);
  29. furi_record_close(RECORD_STORAGE);
  30. furi_record_close(RECORD_DIALOGS);
  31. furi_string_free(nfc_dev->load_path);
  32. if(nfc_dev->dev_data.parsed_data != NULL) {
  33. furi_string_free(nfc_dev->dev_data.parsed_data);
  34. }
  35. furi_string_free(nfc_dev->folder);
  36. free(nfc_dev);
  37. }
  38. static void nfc_device_prepare_format_string(NfcDevice* dev, FuriString* format_string) {
  39. if(dev->format == NfcDeviceSaveFormatUid) {
  40. furi_string_set(format_string, "UID");
  41. } else if(dev->format == NfcDeviceSaveFormatBankCard) {
  42. furi_string_set(format_string, "Bank card");
  43. } else if(dev->format == NfcDeviceSaveFormatMifareUl) {
  44. furi_string_set(format_string, nfc_mf_ul_type(dev->dev_data.mf_ul_data.type, true));
  45. } else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
  46. furi_string_set(format_string, "Mifare Classic");
  47. } else if(dev->format == NfcDeviceSaveFormatMifareDesfire) {
  48. furi_string_set(format_string, "Mifare DESFire");
  49. } else if(dev->format == NfcDeviceSaveFormatNfcV) {
  50. furi_string_set(format_string, "ISO15693");
  51. } else {
  52. furi_string_set(format_string, "Unknown");
  53. }
  54. }
  55. static bool nfc_device_parse_format_string(NfcDevice* dev, FuriString* format_string) {
  56. if(furi_string_start_with_str(format_string, "UID")) {
  57. dev->format = NfcDeviceSaveFormatUid;
  58. dev->dev_data.protocol = NfcDeviceProtocolUnknown;
  59. return true;
  60. }
  61. if(furi_string_start_with_str(format_string, "Bank card")) {
  62. dev->format = NfcDeviceSaveFormatBankCard;
  63. dev->dev_data.protocol = NfcDeviceProtocolEMV;
  64. return true;
  65. }
  66. // Check Mifare Ultralight types
  67. for(MfUltralightType type = MfUltralightTypeUnknown; type < MfUltralightTypeNum; type++) {
  68. if(furi_string_equal(format_string, nfc_mf_ul_type(type, true))) {
  69. dev->format = NfcDeviceSaveFormatMifareUl;
  70. dev->dev_data.protocol = NfcDeviceProtocolMifareUl;
  71. dev->dev_data.mf_ul_data.type = type;
  72. return true;
  73. }
  74. }
  75. if(furi_string_start_with_str(format_string, "Mifare Classic")) {
  76. dev->format = NfcDeviceSaveFormatMifareClassic;
  77. dev->dev_data.protocol = NfcDeviceProtocolMifareClassic;
  78. return true;
  79. }
  80. if(furi_string_start_with_str(format_string, "Mifare DESFire")) {
  81. dev->format = NfcDeviceSaveFormatMifareDesfire;
  82. dev->dev_data.protocol = NfcDeviceProtocolMifareDesfire;
  83. return true;
  84. }
  85. if(furi_string_start_with_str(format_string, "ISO15693")) {
  86. dev->format = NfcDeviceSaveFormatNfcV;
  87. dev->dev_data.protocol = NfcDeviceProtocolNfcV;
  88. return true;
  89. }
  90. return false;
  91. }
  92. static bool nfc_device_save_mifare_ul_data(FlipperFormat* file, NfcDevice* dev) {
  93. bool saved = false;
  94. MfUltralightData* data = &dev->dev_data.mf_ul_data;
  95. FuriString* temp_str;
  96. temp_str = furi_string_alloc();
  97. // Save Mifare Ultralight specific data
  98. do {
  99. if(!flipper_format_write_comment_cstr(file, "Mifare Ultralight specific data")) break;
  100. if(!flipper_format_write_uint32(
  101. file, "Data format version", &nfc_mifare_ultralight_data_format_version, 1))
  102. break;
  103. if(!flipper_format_write_hex(file, "Signature", data->signature, sizeof(data->signature)))
  104. break;
  105. if(!flipper_format_write_hex(
  106. file, "Mifare version", (uint8_t*)&data->version, sizeof(data->version)))
  107. break;
  108. // Write conters and tearing flags data
  109. bool counters_saved = true;
  110. for(uint8_t i = 0; i < 3; i++) {
  111. furi_string_printf(temp_str, "Counter %d", i);
  112. if(!flipper_format_write_uint32(
  113. file, furi_string_get_cstr(temp_str), &data->counter[i], 1)) {
  114. counters_saved = false;
  115. break;
  116. }
  117. furi_string_printf(temp_str, "Tearing %d", i);
  118. if(!flipper_format_write_hex(
  119. file, furi_string_get_cstr(temp_str), &data->tearing[i], 1)) {
  120. counters_saved = false;
  121. break;
  122. }
  123. }
  124. if(!counters_saved) break;
  125. // Write pages data
  126. uint32_t pages_total = data->data_size / 4;
  127. if(!flipper_format_write_uint32(file, "Pages total", &pages_total, 1)) break;
  128. uint32_t pages_read = data->data_read / 4;
  129. if(!flipper_format_write_uint32(file, "Pages read", &pages_read, 1)) break;
  130. bool pages_saved = true;
  131. for(uint16_t i = 0; i < data->data_size; i += 4) {
  132. furi_string_printf(temp_str, "Page %d", i / 4);
  133. if(!flipper_format_write_hex(file, furi_string_get_cstr(temp_str), &data->data[i], 4)) {
  134. pages_saved = false;
  135. break;
  136. }
  137. }
  138. if(!pages_saved) break;
  139. // Write authentication counter
  140. uint32_t auth_counter = data->curr_authlim;
  141. if(!flipper_format_write_uint32(file, "Failed authentication attempts", &auth_counter, 1))
  142. break;
  143. saved = true;
  144. } while(false);
  145. furi_string_free(temp_str);
  146. return saved;
  147. }
  148. bool nfc_device_load_mifare_ul_data(FlipperFormat* file, NfcDevice* dev) {
  149. bool parsed = false;
  150. MfUltralightData* data = &dev->dev_data.mf_ul_data;
  151. FuriString* temp_str;
  152. temp_str = furi_string_alloc();
  153. uint32_t data_format_version = 0;
  154. do {
  155. // Read Mifare Ultralight format version
  156. if(!flipper_format_read_uint32(file, "Data format version", &data_format_version, 1)) {
  157. if(!flipper_format_rewind(file)) break;
  158. }
  159. // Read signature
  160. if(!flipper_format_read_hex(file, "Signature", data->signature, sizeof(data->signature)))
  161. break;
  162. // Read Mifare version
  163. if(!flipper_format_read_hex(
  164. file, "Mifare version", (uint8_t*)&data->version, sizeof(data->version)))
  165. break;
  166. // Read counters and tearing flags
  167. bool counters_parsed = true;
  168. for(uint8_t i = 0; i < 3; i++) {
  169. furi_string_printf(temp_str, "Counter %d", i);
  170. if(!flipper_format_read_uint32(
  171. file, furi_string_get_cstr(temp_str), &data->counter[i], 1)) {
  172. counters_parsed = false;
  173. break;
  174. }
  175. furi_string_printf(temp_str, "Tearing %d", i);
  176. if(!flipper_format_read_hex(
  177. file, furi_string_get_cstr(temp_str), &data->tearing[i], 1)) {
  178. counters_parsed = false;
  179. break;
  180. }
  181. }
  182. if(!counters_parsed) break;
  183. // Read pages
  184. uint32_t pages_total = 0;
  185. if(!flipper_format_read_uint32(file, "Pages total", &pages_total, 1)) break;
  186. uint32_t pages_read = 0;
  187. if(data_format_version < nfc_mifare_ultralight_data_format_version) {
  188. pages_read = pages_total;
  189. } else {
  190. if(!flipper_format_read_uint32(file, "Pages read", &pages_read, 1)) break;
  191. }
  192. data->data_size = pages_total * 4;
  193. data->data_read = pages_read * 4;
  194. if(data->data_size > MF_UL_MAX_DUMP_SIZE || data->data_read > MF_UL_MAX_DUMP_SIZE) break;
  195. bool pages_parsed = true;
  196. for(uint16_t i = 0; i < pages_total; i++) {
  197. furi_string_printf(temp_str, "Page %d", i);
  198. if(!flipper_format_read_hex(
  199. file, furi_string_get_cstr(temp_str), &data->data[i * 4], 4)) {
  200. pages_parsed = false;
  201. break;
  202. }
  203. }
  204. if(!pages_parsed) break;
  205. // Read authentication counter
  206. uint32_t auth_counter;
  207. if(!flipper_format_read_uint32(file, "Failed authentication attempts", &auth_counter, 1))
  208. auth_counter = 0;
  209. data->curr_authlim = auth_counter;
  210. data->auth_success = mf_ul_is_full_capture(data);
  211. parsed = true;
  212. } while(false);
  213. furi_string_free(temp_str);
  214. return parsed;
  215. }
  216. static void nfc_device_write_mifare_classic_block(
  217. FuriString* block_str,
  218. MfClassicData* data,
  219. uint8_t block_num) {
  220. furi_string_reset(block_str);
  221. bool is_sec_trailer = mf_classic_is_sector_trailer(block_num);
  222. if(is_sec_trailer) {
  223. uint8_t sector_num = mf_classic_get_sector_by_block(block_num);
  224. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, sector_num);
  225. // Write key A
  226. for(size_t i = 0; i < sizeof(sec_tr->key_a); i++) {
  227. if(mf_classic_is_key_found(data, sector_num, MfClassicKeyA)) {
  228. furi_string_cat_printf(block_str, "%02X ", sec_tr->key_a[i]);
  229. } else {
  230. furi_string_cat_printf(block_str, "?? ");
  231. }
  232. }
  233. // Write Access bytes
  234. for(size_t i = 0; i < MF_CLASSIC_ACCESS_BYTES_SIZE; i++) {
  235. if(mf_classic_is_block_read(data, block_num)) {
  236. furi_string_cat_printf(block_str, "%02X ", sec_tr->access_bits[i]);
  237. } else {
  238. furi_string_cat_printf(block_str, "?? ");
  239. }
  240. }
  241. // Write key B
  242. for(size_t i = 0; i < sizeof(sec_tr->key_b); i++) {
  243. if(mf_classic_is_key_found(data, sector_num, MfClassicKeyB)) {
  244. furi_string_cat_printf(block_str, "%02X ", sec_tr->key_b[i]);
  245. } else {
  246. furi_string_cat_printf(block_str, "?? ");
  247. }
  248. }
  249. } else {
  250. // Write data block
  251. for(size_t i = 0; i < MF_CLASSIC_BLOCK_SIZE; i++) {
  252. if(mf_classic_is_block_read(data, block_num)) {
  253. furi_string_cat_printf(block_str, "%02X ", data->block[block_num].value[i]);
  254. } else {
  255. furi_string_cat_printf(block_str, "?? ");
  256. }
  257. }
  258. }
  259. furi_string_trim(block_str);
  260. }
  261. static bool nfc_device_save_mifare_classic_data(FlipperFormat* file, NfcDevice* dev) {
  262. bool saved = false;
  263. MfClassicData* data = &dev->dev_data.mf_classic_data;
  264. FuriString* temp_str;
  265. temp_str = furi_string_alloc();
  266. uint16_t blocks = 0;
  267. // Save Mifare Classic specific data
  268. do {
  269. if(!flipper_format_write_comment_cstr(file, "Mifare Classic specific data")) break;
  270. if(data->type == MfClassicTypeMini) {
  271. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "MINI")) break;
  272. blocks = 20;
  273. } else if(data->type == MfClassicType1k) {
  274. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "1K")) break;
  275. blocks = 64;
  276. } else if(data->type == MfClassicType4k) {
  277. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "4K")) break;
  278. blocks = 256;
  279. }
  280. if(!flipper_format_write_uint32(
  281. file, "Data format version", &nfc_mifare_classic_data_format_version, 1))
  282. break;
  283. if(!flipper_format_write_comment_cstr(
  284. file, "Mifare Classic blocks, \'??\' means unknown data"))
  285. break;
  286. bool block_saved = true;
  287. FuriString* block_str;
  288. block_str = furi_string_alloc();
  289. for(size_t i = 0; i < blocks; i++) {
  290. furi_string_printf(temp_str, "Block %d", i);
  291. nfc_device_write_mifare_classic_block(block_str, data, i);
  292. if(!flipper_format_write_string(file, furi_string_get_cstr(temp_str), block_str)) {
  293. block_saved = false;
  294. break;
  295. }
  296. }
  297. furi_string_free(block_str);
  298. if(!block_saved) break;
  299. saved = true;
  300. } while(false);
  301. furi_string_free(temp_str);
  302. return saved;
  303. }
  304. static void nfc_device_load_mifare_classic_block(
  305. FuriString* block_str,
  306. MfClassicData* data,
  307. uint8_t block_num) {
  308. furi_string_trim(block_str);
  309. MfClassicBlock block_tmp = {};
  310. bool is_sector_trailer = mf_classic_is_sector_trailer(block_num);
  311. uint8_t sector_num = mf_classic_get_sector_by_block(block_num);
  312. uint16_t block_unknown_bytes_mask = 0;
  313. furi_string_trim(block_str);
  314. for(size_t i = 0; i < MF_CLASSIC_BLOCK_SIZE; i++) {
  315. char hi = furi_string_get_char(block_str, 3 * i);
  316. char low = furi_string_get_char(block_str, 3 * i + 1);
  317. uint8_t byte = 0;
  318. if(hex_char_to_uint8(hi, low, &byte)) {
  319. block_tmp.value[i] = byte;
  320. } else {
  321. FURI_BIT_SET(block_unknown_bytes_mask, i);
  322. }
  323. }
  324. if(block_unknown_bytes_mask == 0xffff) {
  325. // All data is unknown, exit
  326. return;
  327. }
  328. if(is_sector_trailer) {
  329. MfClassicSectorTrailer* sec_tr_tmp = (MfClassicSectorTrailer*)&block_tmp;
  330. // Load Key A
  331. // Key A mask 0b0000000000111111 = 0x003f
  332. if((block_unknown_bytes_mask & 0x003f) == 0) {
  333. uint64_t key = nfc_util_bytes2num(sec_tr_tmp->key_a, sizeof(sec_tr_tmp->key_a));
  334. mf_classic_set_key_found(data, sector_num, MfClassicKeyA, key);
  335. }
  336. // Load Access Bits
  337. // Access bits mask 0b0000001111000000 = 0x03c0
  338. if((block_unknown_bytes_mask & 0x03c0) == 0) {
  339. mf_classic_set_block_read(data, block_num, &block_tmp);
  340. }
  341. // Load Key B
  342. // Key B mask 0b1111110000000000 = 0xfc00
  343. if((block_unknown_bytes_mask & 0xfc00) == 0) {
  344. uint64_t key = nfc_util_bytes2num(sec_tr_tmp->key_b, sizeof(sec_tr_tmp->key_b));
  345. mf_classic_set_key_found(data, sector_num, MfClassicKeyB, key);
  346. }
  347. } else {
  348. if(block_unknown_bytes_mask == 0) {
  349. mf_classic_set_block_read(data, block_num, &block_tmp);
  350. }
  351. }
  352. }
  353. static bool nfc_device_load_mifare_classic_data(FlipperFormat* file, NfcDevice* dev) {
  354. bool parsed = false;
  355. MfClassicData* data = &dev->dev_data.mf_classic_data;
  356. FuriString* temp_str;
  357. uint32_t data_format_version = 0;
  358. temp_str = furi_string_alloc();
  359. uint16_t data_blocks = 0;
  360. memset(data, 0, sizeof(MfClassicData));
  361. do {
  362. // Read Mifare Classic type
  363. if(!flipper_format_read_string(file, "Mifare Classic type", temp_str)) break;
  364. if(!furi_string_cmp(temp_str, "MINI")) {
  365. data->type = MfClassicTypeMini;
  366. data_blocks = 20;
  367. } else if(!furi_string_cmp(temp_str, "1K")) {
  368. data->type = MfClassicType1k;
  369. data_blocks = 64;
  370. } else if(!furi_string_cmp(temp_str, "4K")) {
  371. data->type = MfClassicType4k;
  372. data_blocks = 256;
  373. } else {
  374. break;
  375. }
  376. bool old_format = false;
  377. // Read Mifare Classic format version
  378. if(!flipper_format_read_uint32(file, "Data format version", &data_format_version, 1)) {
  379. // Load unread sectors with zero keys access for backward compatibility
  380. if(!flipper_format_rewind(file)) break;
  381. old_format = true;
  382. } else {
  383. if(data_format_version < nfc_mifare_classic_data_format_version) {
  384. old_format = true;
  385. }
  386. }
  387. // Read Mifare Classic blocks
  388. bool block_read = true;
  389. FuriString* block_str;
  390. block_str = furi_string_alloc();
  391. for(size_t i = 0; i < data_blocks; i++) {
  392. furi_string_printf(temp_str, "Block %d", i);
  393. if(!flipper_format_read_string(file, furi_string_get_cstr(temp_str), block_str)) {
  394. block_read = false;
  395. break;
  396. }
  397. nfc_device_load_mifare_classic_block(block_str, data, i);
  398. }
  399. furi_string_free(block_str);
  400. if(!block_read) break;
  401. // Set keys and blocks as unknown for backward compatibility
  402. if(old_format) {
  403. data->key_a_mask = 0ULL;
  404. data->key_b_mask = 0ULL;
  405. memset(data->block_read_mask, 0, sizeof(data->block_read_mask));
  406. }
  407. parsed = true;
  408. } while(false);
  409. furi_string_free(temp_str);
  410. return parsed;
  411. }
  412. static void nfc_device_get_key_cache_file_path(NfcDevice* dev, FuriString* file_path) {
  413. uint8_t* uid = dev->dev_data.nfc_data.uid;
  414. uint8_t uid_len = dev->dev_data.nfc_data.uid_len;
  415. furi_string_set(file_path, NFC_DEVICE_KEYS_FOLDER "/");
  416. for(size_t i = 0; i < uid_len; i++) {
  417. furi_string_cat_printf(file_path, "%02X", uid[i]);
  418. }
  419. furi_string_cat_printf(file_path, NFC_DEVICE_KEYS_EXTENSION);
  420. }
  421. static bool nfc_device_save_mifare_classic_keys(NfcDevice* dev) {
  422. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  423. MfClassicData* data = &dev->dev_data.mf_classic_data;
  424. FuriString* temp_str;
  425. temp_str = furi_string_alloc();
  426. nfc_device_get_key_cache_file_path(dev, temp_str);
  427. bool save_success = false;
  428. do {
  429. if(!storage_simply_mkdir(dev->storage, NFC_DEVICE_KEYS_FOLDER)) break;
  430. if(!storage_simply_remove(dev->storage, furi_string_get_cstr(temp_str))) break;
  431. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  432. if(!flipper_format_write_header_cstr(file, nfc_keys_file_header, nfc_keys_file_version))
  433. break;
  434. if(data->type == MfClassicTypeMini) {
  435. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "MINI")) break;
  436. } else if(data->type == MfClassicType1k) {
  437. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "1K")) break;
  438. } else if(data->type == MfClassicType4k) {
  439. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "4K")) break;
  440. }
  441. if(!flipper_format_write_hex_uint64(file, "Key A map", &data->key_a_mask, 1)) break;
  442. if(!flipper_format_write_hex_uint64(file, "Key B map", &data->key_b_mask, 1)) break;
  443. uint8_t sector_num = mf_classic_get_total_sectors_num(data->type);
  444. bool key_save_success = true;
  445. for(size_t i = 0; (i < sector_num) && (key_save_success); i++) {
  446. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, i);
  447. if(FURI_BIT(data->key_a_mask, i)) {
  448. furi_string_printf(temp_str, "Key A sector %d", i);
  449. key_save_success = flipper_format_write_hex(
  450. file, furi_string_get_cstr(temp_str), sec_tr->key_a, 6);
  451. }
  452. if(!key_save_success) break;
  453. if(FURI_BIT(data->key_b_mask, i)) {
  454. furi_string_printf(temp_str, "Key B sector %d", i);
  455. key_save_success = flipper_format_write_hex(
  456. file, furi_string_get_cstr(temp_str), sec_tr->key_b, 6);
  457. }
  458. }
  459. save_success = key_save_success;
  460. } while(false);
  461. flipper_format_free(file);
  462. furi_string_free(temp_str);
  463. return save_success;
  464. }
  465. bool nfc_device_load_key_cache(NfcDevice* dev) {
  466. furi_assert(dev);
  467. FuriString* temp_str;
  468. temp_str = furi_string_alloc();
  469. MfClassicData* data = &dev->dev_data.mf_classic_data;
  470. nfc_device_get_key_cache_file_path(dev, temp_str);
  471. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  472. bool load_success = false;
  473. do {
  474. if(storage_common_stat(dev->storage, furi_string_get_cstr(temp_str), NULL) != FSE_OK)
  475. break;
  476. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(temp_str))) break;
  477. uint32_t version = 0;
  478. if(!flipper_format_read_header(file, temp_str, &version)) break;
  479. if(furi_string_cmp_str(temp_str, nfc_keys_file_header)) break;
  480. if(version != nfc_keys_file_version) break;
  481. if(!flipper_format_read_string(file, "Mifare Classic type", temp_str)) break;
  482. if(!furi_string_cmp(temp_str, "MINI")) {
  483. data->type = MfClassicTypeMini;
  484. } else if(!furi_string_cmp(temp_str, "1K")) {
  485. data->type = MfClassicType1k;
  486. } else if(!furi_string_cmp(temp_str, "4K")) {
  487. data->type = MfClassicType4k;
  488. } else {
  489. break;
  490. }
  491. if(!flipper_format_read_hex_uint64(file, "Key A map", &data->key_a_mask, 1)) break;
  492. if(!flipper_format_read_hex_uint64(file, "Key B map", &data->key_b_mask, 1)) break;
  493. uint8_t sectors = mf_classic_get_total_sectors_num(data->type);
  494. bool key_read_success = true;
  495. for(size_t i = 0; (i < sectors) && (key_read_success); i++) {
  496. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, i);
  497. if(FURI_BIT(data->key_a_mask, i)) {
  498. furi_string_printf(temp_str, "Key A sector %d", i);
  499. key_read_success = flipper_format_read_hex(
  500. file, furi_string_get_cstr(temp_str), sec_tr->key_a, 6);
  501. }
  502. if(!key_read_success) break;
  503. if(FURI_BIT(data->key_b_mask, i)) {
  504. furi_string_printf(temp_str, "Key B sector %d", i);
  505. key_read_success = flipper_format_read_hex(
  506. file, furi_string_get_cstr(temp_str), sec_tr->key_b, 6);
  507. }
  508. }
  509. load_success = key_read_success;
  510. } while(false);
  511. furi_string_free(temp_str);
  512. flipper_format_free(file);
  513. return load_success;
  514. }
  515. void nfc_device_set_name(NfcDevice* dev, const char* name) {
  516. furi_assert(dev);
  517. strlcpy(dev->dev_name, name, NFC_DEV_NAME_MAX_LEN);
  518. }
  519. static void nfc_device_get_path_without_ext(FuriString* orig_path, FuriString* shadow_path) {
  520. // TODO: this won't work if there is ".nfc" anywhere in the path other than
  521. // at the end
  522. size_t ext_start = furi_string_search(orig_path, NFC_APP_FILENAME_EXTENSION);
  523. furi_string_set_n(shadow_path, orig_path, 0, ext_start);
  524. }
  525. static void nfc_device_get_shadow_path(FuriString* orig_path, FuriString* shadow_path) {
  526. nfc_device_get_path_without_ext(orig_path, shadow_path);
  527. furi_string_cat_printf(shadow_path, "%s", NFC_APP_SHADOW_EXTENSION);
  528. }
  529. static void nfc_device_get_folder_from_path(FuriString* path, FuriString* folder) {
  530. size_t last_slash = furi_string_search_rchar(path, '/');
  531. if(last_slash == FURI_STRING_FAILURE) {
  532. // No slashes in the path, treat the whole path as a folder
  533. furi_string_set(folder, path);
  534. } else {
  535. furi_string_set_n(folder, path, 0, last_slash);
  536. }
  537. }
  538. bool nfc_device_save(NfcDevice* dev, const char* dev_name) {
  539. return false;
  540. furi_assert(dev);
  541. bool saved = false;
  542. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  543. FurryHalNfcDevData* data = &dev->dev_data.nfc_data;
  544. FuriString* temp_str;
  545. temp_str = furi_string_alloc();
  546. do {
  547. // Create directory if necessary
  548. FuriString* folder = furi_string_alloc();
  549. // Get folder from filename (filename is in the form of "folder/filename.nfc", so the folder is "folder/")
  550. furi_string_set(temp_str, dev_name);
  551. // Get folder from filename
  552. nfc_device_get_folder_from_path(temp_str, folder);
  553. FURI_LOG_I("Nfc", "Saving to folder %s", furi_string_get_cstr(folder));
  554. if(!storage_simply_mkdir(dev->storage, furi_string_get_cstr(folder))) {
  555. FURI_LOG_E("Nfc", "Failed to create folder %s", furi_string_get_cstr(folder));
  556. break;
  557. }
  558. furi_string_free(folder);
  559. // First remove nfc device file if it was saved
  560. // Open file
  561. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  562. // Write header
  563. if(!flipper_format_write_header_cstr(file, nfc_file_header, nfc_file_version)) break;
  564. // Write nfc device type
  565. if(!flipper_format_write_comment_cstr(
  566. file, "Nfc device type can be UID, Mifare Ultralight, Mifare Classic or ISO15693"))
  567. break;
  568. nfc_device_prepare_format_string(dev, temp_str);
  569. if(!flipper_format_write_string(file, "Device type", temp_str)) break;
  570. // Write UID
  571. if(!flipper_format_write_comment_cstr(file, "UID is common for all formats")) break;
  572. if(!flipper_format_write_hex(file, "UID", data->uid, data->uid_len)) break;
  573. if(dev->format != NfcDeviceSaveFormatNfcV) {
  574. // Write ATQA, SAK
  575. if(!flipper_format_write_comment_cstr(file, "ISO14443 specific fields")) break;
  576. // Save ATQA in MSB order for correct companion apps display
  577. uint8_t atqa[2] = {data->atqa[1], data->atqa[0]};
  578. if(!flipper_format_write_hex(file, "ATQA", atqa, 2)) break;
  579. if(!flipper_format_write_hex(file, "SAK", &data->sak, 1)) break;
  580. }
  581. // Save more data if necessary
  582. if(dev->format == NfcDeviceSaveFormatMifareUl) {
  583. if(!nfc_device_save_mifare_ul_data(file, dev)) break;
  584. } else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
  585. // Save data
  586. if(!nfc_device_save_mifare_classic_data(file, dev)) break;
  587. // Save keys cache
  588. if(!nfc_device_save_mifare_classic_keys(dev)) break;
  589. }
  590. saved = true;
  591. } while(0);
  592. if(!saved) { //-V547
  593. dialog_message_show_storage_error(dev->dialogs, "Can not save\nkey file");
  594. }
  595. furi_string_free(temp_str);
  596. flipper_format_free(file);
  597. return saved;
  598. }
  599. bool nfc_device_save_shadow(NfcDevice* dev, const char* path) {
  600. return false;
  601. dev->shadow_file_exist = true;
  602. // Replace extension from .nfc to .shd if necessary
  603. FuriString* orig_path = furi_string_alloc();
  604. furi_string_set_str(orig_path, path);
  605. FuriString* shadow_path = furi_string_alloc();
  606. nfc_device_get_shadow_path(orig_path, shadow_path);
  607. bool file_saved = nfc_device_save(dev, furi_string_get_cstr(shadow_path));
  608. furi_string_free(orig_path);
  609. furi_string_free(shadow_path);
  610. return file_saved;
  611. }
  612. static bool nfc_device_load_data(NfcDevice* dev, FuriString* path, bool show_dialog) {
  613. bool parsed = false;
  614. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  615. FurryHalNfcDevData* data = &dev->dev_data.nfc_data;
  616. uint32_t data_cnt = 0;
  617. FuriString* temp_str;
  618. temp_str = furi_string_alloc();
  619. bool deprecated_version = false;
  620. // Version 2 of file format had ATQA bytes swapped
  621. uint32_t version_with_lsb_atqa = 2;
  622. if(dev->loading_cb) {
  623. dev->loading_cb(dev->loading_cb_ctx, true);
  624. }
  625. do {
  626. // Check existence of shadow file
  627. nfc_device_get_shadow_path(path, temp_str);
  628. dev->shadow_file_exist =
  629. storage_common_stat(dev->storage, furi_string_get_cstr(temp_str), NULL) == FSE_OK;
  630. // Open shadow file if it exists. If not - open original
  631. if(dev->shadow_file_exist) {
  632. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(temp_str))) break;
  633. } else {
  634. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(path))) break;
  635. }
  636. // Read and verify file header
  637. uint32_t version = 0;
  638. if(!flipper_format_read_header(file, temp_str, &version)) break;
  639. if(furi_string_cmp_str(temp_str, nfc_file_header)) break;
  640. if(version != nfc_file_version) {
  641. if(version < version_with_lsb_atqa) {
  642. deprecated_version = true;
  643. break;
  644. }
  645. }
  646. // Read Nfc device type
  647. if(!flipper_format_read_string(file, "Device type", temp_str)) break;
  648. if(!nfc_device_parse_format_string(dev, temp_str)) break;
  649. // Read and parse UID, ATQA and SAK
  650. if(!flipper_format_get_value_count(file, "UID", &data_cnt)) break;
  651. if(!(data_cnt == 4 || data_cnt == 7 || data_cnt == 8)) break;
  652. data->uid_len = data_cnt;
  653. if(!flipper_format_read_hex(file, "UID", data->uid, data->uid_len)) break;
  654. if(dev->format != NfcDeviceSaveFormatNfcV) {
  655. if(version == version_with_lsb_atqa) {
  656. if(!flipper_format_read_hex(file, "ATQA", data->atqa, 2)) break;
  657. } else {
  658. uint8_t atqa[2] = {};
  659. if(!flipper_format_read_hex(file, "ATQA", atqa, 2)) break;
  660. data->atqa[0] = atqa[1];
  661. data->atqa[1] = atqa[0];
  662. }
  663. if(!flipper_format_read_hex(file, "SAK", &data->sak, 1)) break;
  664. }
  665. // Load CUID
  666. uint8_t* cuid_start = data->uid;
  667. if(data->uid_len == 7) {
  668. cuid_start = &data->uid[3];
  669. }
  670. data->cuid = (cuid_start[0] << 24) | (cuid_start[1] << 16) | (cuid_start[2] << 8) |
  671. (cuid_start[3]);
  672. // Parse other data
  673. if(dev->format == NfcDeviceSaveFormatMifareUl) {
  674. if(!nfc_device_load_mifare_ul_data(file, dev)) break;
  675. } else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
  676. if(!nfc_device_load_mifare_classic_data(file, dev)) break;
  677. }
  678. parsed = true;
  679. } while(false);
  680. if(dev->loading_cb) {
  681. dev->loading_cb(dev->loading_cb_ctx, false);
  682. }
  683. if((!parsed) && (show_dialog)) {
  684. if(deprecated_version) {
  685. dialog_message_show_storage_error(dev->dialogs, "File format deprecated");
  686. } else {
  687. dialog_message_show_storage_error(dev->dialogs, "Can not parse\nfile");
  688. }
  689. }
  690. furi_string_free(temp_str);
  691. flipper_format_free(file);
  692. return parsed;
  693. }
  694. bool nfc_device_load(NfcDevice* dev, const char* file_path, bool show_dialog) {
  695. furi_assert(dev);
  696. furi_assert(file_path);
  697. // Load device data
  698. furi_string_set(dev->load_path, file_path);
  699. bool dev_load = nfc_device_load_data(dev, dev->load_path, show_dialog);
  700. if(dev_load) {
  701. // Set device name
  702. FuriString* filename;
  703. filename = furi_string_alloc();
  704. path_extract_filename_no_ext(file_path, filename);
  705. nfc_device_set_name(dev, furi_string_get_cstr(filename));
  706. furi_string_free(filename);
  707. }
  708. return dev_load;
  709. }
  710. void nfc_device_data_clear(NfcDeviceData* dev_data) {
  711. if(dev_data->protocol == NfcDeviceProtocolMifareClassic) {
  712. memset(&dev_data->mf_classic_data, 0, sizeof(MfClassicData));
  713. } else if(dev_data->protocol == NfcDeviceProtocolMifareUl) {
  714. mf_ul_reset(&dev_data->mf_ul_data);
  715. }
  716. memset(&dev_data->nfc_data, 0, sizeof(FurryHalNfcDevData));
  717. dev_data->protocol = NfcDeviceProtocolUnknown;
  718. if(dev_data->parsed_data != NULL) {
  719. furi_string_reset(dev_data->parsed_data);
  720. }
  721. }
  722. void nfc_device_clear(NfcDevice* dev) {
  723. furi_assert(dev);
  724. nfc_device_set_name(dev, "");
  725. nfc_device_data_clear(&dev->dev_data);
  726. dev->format = NfcDeviceSaveFormatUid;
  727. furi_string_reset(dev->load_path);
  728. }
  729. bool nfc_device_delete(NfcDevice* dev, bool use_load_path) {
  730. furi_assert(dev);
  731. bool deleted = false;
  732. FuriString* file_path;
  733. file_path = furi_string_alloc();
  734. do {
  735. // Delete original file
  736. if(use_load_path && !furi_string_empty(dev->load_path)) {
  737. furi_string_set(file_path, dev->load_path);
  738. } else {
  739. furi_string_printf(
  740. file_path,
  741. "%s/%s%s",
  742. furi_string_get_cstr(dev->folder),
  743. dev->dev_name,
  744. NFC_APP_FILENAME_EXTENSION);
  745. }
  746. if(!storage_simply_remove(dev->storage, furi_string_get_cstr(file_path))) break;
  747. // Delete shadow file if it exists
  748. if(dev->shadow_file_exist) {
  749. if(use_load_path && !furi_string_empty(dev->load_path)) {
  750. nfc_device_get_shadow_path(dev->load_path, file_path);
  751. } else {
  752. furi_string_printf(
  753. file_path,
  754. "%s/%s%s",
  755. furi_string_get_cstr(dev->folder),
  756. dev->dev_name,
  757. NFC_APP_SHADOW_EXTENSION);
  758. }
  759. if(!storage_simply_remove(dev->storage, furi_string_get_cstr(file_path))) break;
  760. }
  761. deleted = true;
  762. } while(0);
  763. if(!deleted) {
  764. dialog_message_show_storage_error(dev->dialogs, "Can not remove file");
  765. }
  766. furi_string_free(file_path);
  767. return deleted;
  768. }
  769. bool nfc_device_restore(NfcDevice* dev, bool use_load_path) {
  770. furi_assert(dev);
  771. furi_assert(dev->shadow_file_exist);
  772. bool restored = false;
  773. FuriString* path;
  774. path = furi_string_alloc();
  775. do {
  776. if(use_load_path && !furi_string_empty(dev->load_path)) {
  777. nfc_device_get_shadow_path(dev->load_path, path);
  778. } else {
  779. furi_string_printf(
  780. path,
  781. "%s/%s%s",
  782. furi_string_get_cstr(dev->folder),
  783. dev->dev_name,
  784. NFC_APP_SHADOW_EXTENSION);
  785. }
  786. if(!storage_simply_remove(dev->storage, furi_string_get_cstr(path))) break;
  787. dev->shadow_file_exist = false;
  788. if(use_load_path && !furi_string_empty(dev->load_path)) {
  789. furi_string_set(path, dev->load_path);
  790. } else {
  791. furi_string_printf(
  792. path,
  793. "%s/%s%s",
  794. furi_string_get_cstr(dev->folder),
  795. dev->dev_name,
  796. NFC_APP_FILENAME_EXTENSION);
  797. }
  798. if(!nfc_device_load_data(dev, path, true)) break;
  799. restored = true;
  800. } while(0);
  801. furi_string_free(path);
  802. return restored;
  803. }
  804. void nfc_device_set_loading_callback(NfcDevice* dev, NfcLoadingCallback callback, void* context) {
  805. furi_assert(dev);
  806. dev->loading_cb = callback;
  807. dev->loading_cb_ctx = context;
  808. }