nfc_device.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. #include "nfc_device.h"
  2. #include "assets_icons.h"
  3. #include "nfc_types.h"
  4. #include <lib/toolbox/path.h>
  5. #include <lib/toolbox/hex.h>
  6. #include <lib/nfc/protocols/nfc_util.h>
  7. #include <flipper_format/flipper_format.h>
  8. #define TAG "NfcDevice"
  9. #define NFC_DEVICE_KEYS_FOLDER EXT_PATH("nfc/cache")
  10. #define NFC_DEVICE_KEYS_EXTENSION ".keys"
  11. static const char* nfc_file_header = "Flipper NFC device";
  12. static const uint32_t nfc_file_version = 2;
  13. static const char* nfc_keys_file_header = "Flipper NFC keys";
  14. static const uint32_t nfc_keys_file_version = 1;
  15. // Protocols format versions
  16. static const uint32_t nfc_mifare_classic_data_format_version = 2;
  17. static const uint32_t nfc_mifare_ultralight_data_format_version = 1;
  18. NfcDevice* nfc_device_alloc() {
  19. NfcDevice* nfc_dev = malloc(sizeof(NfcDevice));
  20. nfc_dev->storage = furi_record_open(RECORD_STORAGE);
  21. nfc_dev->dialogs = furi_record_open(RECORD_DIALOGS);
  22. nfc_dev->load_path = furi_string_alloc();
  23. nfc_dev->dev_data.parsed_data = 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. furi_string_free(nfc_dev->dev_data.parsed_data);
  33. free(nfc_dev);
  34. }
  35. static void nfc_device_prepare_format_string(NfcDevice* dev, FuriString* format_string) {
  36. if(dev->format == NfcDeviceSaveFormatUid) {
  37. furi_string_set(format_string, "UID");
  38. } else if(dev->format == NfcDeviceSaveFormatBankCard) {
  39. furi_string_set(format_string, "Bank card");
  40. } else if(dev->format == NfcDeviceSaveFormatMifareUl) {
  41. furi_string_set(format_string, nfc_mf_ul_type(dev->dev_data.mf_ul_data.type, true));
  42. } else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
  43. furi_string_set(format_string, "Mifare Classic");
  44. } else if(dev->format == NfcDeviceSaveFormatMifareDesfire) {
  45. furi_string_set(format_string, "Mifare DESFire");
  46. } else {
  47. furi_string_set(format_string, "Unknown");
  48. }
  49. }
  50. static bool nfc_device_parse_format_string(NfcDevice* dev, FuriString* format_string) {
  51. if(furi_string_start_with_str(format_string, "UID")) {
  52. dev->format = NfcDeviceSaveFormatUid;
  53. dev->dev_data.protocol = NfcDeviceProtocolUnknown;
  54. return true;
  55. }
  56. if(furi_string_start_with_str(format_string, "Bank card")) {
  57. dev->format = NfcDeviceSaveFormatBankCard;
  58. dev->dev_data.protocol = NfcDeviceProtocolEMV;
  59. return true;
  60. }
  61. // Check Mifare Ultralight types
  62. for(MfUltralightType type = MfUltralightTypeUnknown; type < MfUltralightTypeNum; type++) {
  63. if(furi_string_equal(format_string, nfc_mf_ul_type(type, true))) {
  64. dev->format = NfcDeviceSaveFormatMifareUl;
  65. dev->dev_data.protocol = NfcDeviceProtocolMifareUl;
  66. dev->dev_data.mf_ul_data.type = type;
  67. return true;
  68. }
  69. }
  70. if(furi_string_start_with_str(format_string, "Mifare Classic")) {
  71. dev->format = NfcDeviceSaveFormatMifareClassic;
  72. dev->dev_data.protocol = NfcDeviceProtocolMifareClassic;
  73. return true;
  74. }
  75. if(furi_string_start_with_str(format_string, "Mifare DESFire")) {
  76. dev->format = NfcDeviceSaveFormatMifareDesfire;
  77. dev->dev_data.protocol = NfcDeviceProtocolMifareDesfire;
  78. return true;
  79. }
  80. return false;
  81. }
  82. static bool nfc_device_save_mifare_ul_data(FlipperFormat* file, NfcDevice* dev) {
  83. bool saved = false;
  84. MfUltralightData* data = &dev->dev_data.mf_ul_data;
  85. FuriString* temp_str;
  86. temp_str = furi_string_alloc();
  87. // Save Mifare Ultralight specific data
  88. do {
  89. if(!flipper_format_write_comment_cstr(file, "Mifare Ultralight specific data")) break;
  90. if(!flipper_format_write_uint32(
  91. file, "Data format version", &nfc_mifare_ultralight_data_format_version, 1))
  92. break;
  93. if(!flipper_format_write_hex(file, "Signature", data->signature, sizeof(data->signature)))
  94. break;
  95. if(!flipper_format_write_hex(
  96. file, "Mifare version", (uint8_t*)&data->version, sizeof(data->version)))
  97. break;
  98. // Write conters and tearing flags data
  99. bool counters_saved = true;
  100. for(uint8_t i = 0; i < 3; i++) {
  101. furi_string_printf(temp_str, "Counter %d", i);
  102. if(!flipper_format_write_uint32(
  103. file, furi_string_get_cstr(temp_str), &data->counter[i], 1)) {
  104. counters_saved = false;
  105. break;
  106. }
  107. furi_string_printf(temp_str, "Tearing %d", i);
  108. if(!flipper_format_write_hex(
  109. file, furi_string_get_cstr(temp_str), &data->tearing[i], 1)) {
  110. counters_saved = false;
  111. break;
  112. }
  113. }
  114. if(!counters_saved) break;
  115. // Write pages data
  116. uint32_t pages_total = data->data_size / 4;
  117. if(!flipper_format_write_uint32(file, "Pages total", &pages_total, 1)) break;
  118. uint32_t pages_read = data->data_read / 4;
  119. if(!flipper_format_write_uint32(file, "Pages read", &pages_read, 1)) break;
  120. bool pages_saved = true;
  121. for(uint16_t i = 0; i < data->data_size; i += 4) {
  122. furi_string_printf(temp_str, "Page %d", i / 4);
  123. if(!flipper_format_write_hex(file, furi_string_get_cstr(temp_str), &data->data[i], 4)) {
  124. pages_saved = false;
  125. break;
  126. }
  127. }
  128. if(!pages_saved) break;
  129. // Write authentication counter
  130. uint32_t auth_counter = data->curr_authlim;
  131. if(!flipper_format_write_uint32(file, "Failed authentication attempts", &auth_counter, 1))
  132. break;
  133. saved = true;
  134. } while(false);
  135. furi_string_free(temp_str);
  136. return saved;
  137. }
  138. bool nfc_device_load_mifare_ul_data(FlipperFormat* file, NfcDevice* dev) {
  139. bool parsed = false;
  140. MfUltralightData* data = &dev->dev_data.mf_ul_data;
  141. FuriString* temp_str;
  142. temp_str = furi_string_alloc();
  143. uint32_t data_format_version = 0;
  144. do {
  145. // Read Mifare Ultralight format version
  146. if(!flipper_format_read_uint32(file, "Data format version", &data_format_version, 1)) {
  147. if(!flipper_format_rewind(file)) break;
  148. }
  149. // Read signature
  150. if(!flipper_format_read_hex(file, "Signature", data->signature, sizeof(data->signature)))
  151. break;
  152. // Read Mifare version
  153. if(!flipper_format_read_hex(
  154. file, "Mifare version", (uint8_t*)&data->version, sizeof(data->version)))
  155. break;
  156. // Read counters and tearing flags
  157. bool counters_parsed = true;
  158. for(uint8_t i = 0; i < 3; i++) {
  159. furi_string_printf(temp_str, "Counter %d", i);
  160. if(!flipper_format_read_uint32(
  161. file, furi_string_get_cstr(temp_str), &data->counter[i], 1)) {
  162. counters_parsed = false;
  163. break;
  164. }
  165. furi_string_printf(temp_str, "Tearing %d", i);
  166. if(!flipper_format_read_hex(
  167. file, furi_string_get_cstr(temp_str), &data->tearing[i], 1)) {
  168. counters_parsed = false;
  169. break;
  170. }
  171. }
  172. if(!counters_parsed) break;
  173. // Read pages
  174. uint32_t pages_total = 0;
  175. if(!flipper_format_read_uint32(file, "Pages total", &pages_total, 1)) break;
  176. uint32_t pages_read = 0;
  177. if(data_format_version < nfc_mifare_ultralight_data_format_version) {
  178. pages_read = pages_total;
  179. } else {
  180. if(!flipper_format_read_uint32(file, "Pages read", &pages_read, 1)) break;
  181. }
  182. data->data_size = pages_total * 4;
  183. data->data_read = pages_read * 4;
  184. if(data->data_size > MF_UL_MAX_DUMP_SIZE || data->data_read > MF_UL_MAX_DUMP_SIZE) break;
  185. bool pages_parsed = true;
  186. for(uint16_t i = 0; i < pages_total; i++) {
  187. furi_string_printf(temp_str, "Page %d", i);
  188. if(!flipper_format_read_hex(
  189. file, furi_string_get_cstr(temp_str), &data->data[i * 4], 4)) {
  190. pages_parsed = false;
  191. break;
  192. }
  193. }
  194. if(!pages_parsed) break;
  195. // Read authentication counter
  196. uint32_t auth_counter;
  197. if(!flipper_format_read_uint32(file, "Failed authentication attempts", &auth_counter, 1))
  198. auth_counter = 0;
  199. data->curr_authlim = auth_counter;
  200. data->auth_success = mf_ul_is_full_capture(data);
  201. parsed = true;
  202. } while(false);
  203. furi_string_free(temp_str);
  204. return parsed;
  205. }
  206. static bool nfc_device_save_mifare_df_key_settings(
  207. FlipperFormat* file,
  208. MifareDesfireKeySettings* ks,
  209. const char* prefix) {
  210. bool saved = false;
  211. FuriString* key;
  212. key = furi_string_alloc();
  213. do {
  214. furi_string_printf(key, "%s Change Key ID", prefix);
  215. if(!flipper_format_write_hex(file, furi_string_get_cstr(key), &ks->change_key_id, 1))
  216. break;
  217. furi_string_printf(key, "%s Config Changeable", prefix);
  218. if(!flipper_format_write_bool(file, furi_string_get_cstr(key), &ks->config_changeable, 1))
  219. break;
  220. furi_string_printf(key, "%s Free Create Delete", prefix);
  221. if(!flipper_format_write_bool(file, furi_string_get_cstr(key), &ks->free_create_delete, 1))
  222. break;
  223. furi_string_printf(key, "%s Free Directory List", prefix);
  224. if(!flipper_format_write_bool(file, furi_string_get_cstr(key), &ks->free_directory_list, 1))
  225. break;
  226. furi_string_printf(key, "%s Key Changeable", prefix);
  227. if(!flipper_format_write_bool(
  228. file, furi_string_get_cstr(key), &ks->master_key_changeable, 1))
  229. break;
  230. if(ks->flags) {
  231. furi_string_printf(key, "%s Flags", prefix);
  232. if(!flipper_format_write_hex(file, furi_string_get_cstr(key), &ks->flags, 1)) break;
  233. }
  234. furi_string_printf(key, "%s Max Keys", prefix);
  235. if(!flipper_format_write_hex(file, furi_string_get_cstr(key), &ks->max_keys, 1)) break;
  236. for(MifareDesfireKeyVersion* kv = ks->key_version_head; kv; kv = kv->next) {
  237. furi_string_printf(key, "%s Key %d Version", prefix, kv->id);
  238. if(!flipper_format_write_hex(file, furi_string_get_cstr(key), &kv->version, 1)) break;
  239. }
  240. saved = true;
  241. } while(false);
  242. furi_string_free(key);
  243. return saved;
  244. }
  245. bool nfc_device_load_mifare_df_key_settings(
  246. FlipperFormat* file,
  247. MifareDesfireKeySettings* ks,
  248. const char* prefix) {
  249. bool parsed = false;
  250. FuriString* key;
  251. key = furi_string_alloc();
  252. do {
  253. furi_string_printf(key, "%s Change Key ID", prefix);
  254. if(!flipper_format_read_hex(file, furi_string_get_cstr(key), &ks->change_key_id, 1)) break;
  255. furi_string_printf(key, "%s Config Changeable", prefix);
  256. if(!flipper_format_read_bool(file, furi_string_get_cstr(key), &ks->config_changeable, 1))
  257. break;
  258. furi_string_printf(key, "%s Free Create Delete", prefix);
  259. if(!flipper_format_read_bool(file, furi_string_get_cstr(key), &ks->free_create_delete, 1))
  260. break;
  261. furi_string_printf(key, "%s Free Directory List", prefix);
  262. if(!flipper_format_read_bool(file, furi_string_get_cstr(key), &ks->free_directory_list, 1))
  263. break;
  264. furi_string_printf(key, "%s Key Changeable", prefix);
  265. if(!flipper_format_read_bool(
  266. file, furi_string_get_cstr(key), &ks->master_key_changeable, 1))
  267. break;
  268. furi_string_printf(key, "%s Flags", prefix);
  269. if(flipper_format_key_exist(file, furi_string_get_cstr(key))) {
  270. if(!flipper_format_read_hex(file, furi_string_get_cstr(key), &ks->flags, 1)) break;
  271. }
  272. furi_string_printf(key, "%s Max Keys", prefix);
  273. if(!flipper_format_read_hex(file, furi_string_get_cstr(key), &ks->max_keys, 1)) break;
  274. ks->flags |= ks->max_keys >> 4;
  275. ks->max_keys &= 0xF;
  276. MifareDesfireKeyVersion** kv_head = &ks->key_version_head;
  277. for(int key_id = 0; key_id < ks->max_keys; key_id++) {
  278. furi_string_printf(key, "%s Key %d Version", prefix, key_id);
  279. uint8_t version;
  280. if(flipper_format_read_hex(file, furi_string_get_cstr(key), &version, 1)) {
  281. MifareDesfireKeyVersion* kv = malloc(sizeof(MifareDesfireKeyVersion));
  282. memset(kv, 0, sizeof(MifareDesfireKeyVersion));
  283. kv->id = key_id;
  284. kv->version = version;
  285. *kv_head = kv;
  286. kv_head = &kv->next;
  287. }
  288. }
  289. parsed = true;
  290. } while(false);
  291. furi_string_free(key);
  292. return parsed;
  293. }
  294. static bool nfc_device_save_mifare_df_app(FlipperFormat* file, MifareDesfireApplication* app) {
  295. bool saved = false;
  296. FuriString *prefix, *key;
  297. prefix =
  298. furi_string_alloc_printf("Application %02x%02x%02x", app->id[0], app->id[1], app->id[2]);
  299. key = furi_string_alloc();
  300. uint8_t* tmp = NULL;
  301. do {
  302. if(app->key_settings) {
  303. if(!nfc_device_save_mifare_df_key_settings(
  304. file, app->key_settings, furi_string_get_cstr(prefix)))
  305. break;
  306. }
  307. if(!app->file_head) break;
  308. uint32_t n_files = 0;
  309. for(MifareDesfireFile* f = app->file_head; f; f = f->next) {
  310. n_files++;
  311. }
  312. tmp = malloc(n_files);
  313. int i = 0;
  314. for(MifareDesfireFile* f = app->file_head; f; f = f->next) {
  315. tmp[i++] = f->id;
  316. }
  317. furi_string_printf(key, "%s File IDs", furi_string_get_cstr(prefix));
  318. if(!flipper_format_write_hex(file, furi_string_get_cstr(key), tmp, n_files)) break;
  319. bool saved_files = true;
  320. for(MifareDesfireFile* f = app->file_head; f; f = f->next) {
  321. saved_files = false;
  322. furi_string_printf(key, "%s File %d Type", furi_string_get_cstr(prefix), f->id);
  323. if(!flipper_format_write_hex(file, furi_string_get_cstr(key), &f->type, 1)) break;
  324. furi_string_printf(
  325. key, "%s File %d Communication Settings", furi_string_get_cstr(prefix), f->id);
  326. if(!flipper_format_write_hex(file, furi_string_get_cstr(key), &f->comm, 1)) break;
  327. furi_string_printf(
  328. key, "%s File %d Access Rights", furi_string_get_cstr(prefix), f->id);
  329. if(!flipper_format_write_hex(
  330. file, furi_string_get_cstr(key), (uint8_t*)&f->access_rights, 2))
  331. break;
  332. uint16_t size = 0;
  333. if(f->type == MifareDesfireFileTypeStandard ||
  334. f->type == MifareDesfireFileTypeBackup) {
  335. size = f->settings.data.size;
  336. furi_string_printf(key, "%s File %d Size", furi_string_get_cstr(prefix), f->id);
  337. if(!flipper_format_write_uint32(
  338. file, furi_string_get_cstr(key), &f->settings.data.size, 1))
  339. break;
  340. } else if(f->type == MifareDesfireFileTypeValue) {
  341. furi_string_printf(
  342. key, "%s File %d Hi Limit", furi_string_get_cstr(prefix), f->id);
  343. if(!flipper_format_write_uint32(
  344. file, furi_string_get_cstr(key), &f->settings.value.hi_limit, 1))
  345. break;
  346. furi_string_printf(
  347. key, "%s File %d Lo Limit", furi_string_get_cstr(prefix), f->id);
  348. if(!flipper_format_write_uint32(
  349. file, furi_string_get_cstr(key), &f->settings.value.lo_limit, 1))
  350. break;
  351. furi_string_printf(
  352. key, "%s File %d Limited Credit Value", furi_string_get_cstr(prefix), f->id);
  353. if(!flipper_format_write_uint32(
  354. file, furi_string_get_cstr(key), &f->settings.value.limited_credit_value, 1))
  355. break;
  356. furi_string_printf(
  357. key, "%s File %d Limited Credit Enabled", furi_string_get_cstr(prefix), f->id);
  358. if(!flipper_format_write_bool(
  359. file,
  360. furi_string_get_cstr(key),
  361. &f->settings.value.limited_credit_enabled,
  362. 1))
  363. break;
  364. size = 4;
  365. } else if(
  366. f->type == MifareDesfireFileTypeLinearRecord ||
  367. f->type == MifareDesfireFileTypeCyclicRecord) {
  368. furi_string_printf(key, "%s File %d Size", furi_string_get_cstr(prefix), f->id);
  369. if(!flipper_format_write_uint32(
  370. file, furi_string_get_cstr(key), &f->settings.record.size, 1))
  371. break;
  372. furi_string_printf(key, "%s File %d Max", furi_string_get_cstr(prefix), f->id);
  373. if(!flipper_format_write_uint32(
  374. file, furi_string_get_cstr(key), &f->settings.record.max, 1))
  375. break;
  376. furi_string_printf(key, "%s File %d Cur", furi_string_get_cstr(prefix), f->id);
  377. if(!flipper_format_write_uint32(
  378. file, furi_string_get_cstr(key), &f->settings.record.cur, 1))
  379. break;
  380. size = f->settings.record.size * f->settings.record.cur;
  381. }
  382. if(f->contents) {
  383. furi_string_printf(key, "%s File %d", furi_string_get_cstr(prefix), f->id);
  384. if(!flipper_format_write_hex(file, furi_string_get_cstr(key), f->contents, size))
  385. break;
  386. }
  387. saved_files = true;
  388. }
  389. if(!saved_files) {
  390. break;
  391. }
  392. saved = true;
  393. } while(false);
  394. free(tmp);
  395. furi_string_free(prefix);
  396. furi_string_free(key);
  397. return saved;
  398. }
  399. bool nfc_device_load_mifare_df_app(FlipperFormat* file, MifareDesfireApplication* app) {
  400. bool parsed = false;
  401. FuriString *prefix, *key;
  402. prefix =
  403. furi_string_alloc_printf("Application %02x%02x%02x", app->id[0], app->id[1], app->id[2]);
  404. key = furi_string_alloc();
  405. uint8_t* tmp = NULL;
  406. MifareDesfireFile* f = NULL;
  407. do {
  408. app->key_settings = malloc(sizeof(MifareDesfireKeySettings));
  409. memset(app->key_settings, 0, sizeof(MifareDesfireKeySettings));
  410. if(!nfc_device_load_mifare_df_key_settings(
  411. file, app->key_settings, furi_string_get_cstr(prefix))) {
  412. free(app->key_settings);
  413. app->key_settings = NULL;
  414. break;
  415. }
  416. furi_string_printf(key, "%s File IDs", furi_string_get_cstr(prefix));
  417. uint32_t n_files;
  418. if(!flipper_format_get_value_count(file, furi_string_get_cstr(key), &n_files)) break;
  419. tmp = malloc(n_files);
  420. if(!flipper_format_read_hex(file, furi_string_get_cstr(key), tmp, n_files)) break;
  421. MifareDesfireFile** file_head = &app->file_head;
  422. bool parsed_files = true;
  423. for(uint32_t i = 0; i < n_files; i++) {
  424. parsed_files = false;
  425. f = malloc(sizeof(MifareDesfireFile));
  426. memset(f, 0, sizeof(MifareDesfireFile));
  427. f->id = tmp[i];
  428. furi_string_printf(key, "%s File %d Type", furi_string_get_cstr(prefix), f->id);
  429. if(!flipper_format_read_hex(file, furi_string_get_cstr(key), &f->type, 1)) break;
  430. furi_string_printf(
  431. key, "%s File %d Communication Settings", furi_string_get_cstr(prefix), f->id);
  432. if(!flipper_format_read_hex(file, furi_string_get_cstr(key), &f->comm, 1)) break;
  433. furi_string_printf(
  434. key, "%s File %d Access Rights", furi_string_get_cstr(prefix), f->id);
  435. if(!flipper_format_read_hex(
  436. file, furi_string_get_cstr(key), (uint8_t*)&f->access_rights, 2))
  437. break;
  438. if(f->type == MifareDesfireFileTypeStandard ||
  439. f->type == MifareDesfireFileTypeBackup) {
  440. furi_string_printf(key, "%s File %d Size", furi_string_get_cstr(prefix), f->id);
  441. if(!flipper_format_read_uint32(
  442. file, furi_string_get_cstr(key), &f->settings.data.size, 1))
  443. break;
  444. } else if(f->type == MifareDesfireFileTypeValue) {
  445. furi_string_printf(
  446. key, "%s File %d Hi Limit", furi_string_get_cstr(prefix), f->id);
  447. if(!flipper_format_read_uint32(
  448. file, furi_string_get_cstr(key), &f->settings.value.hi_limit, 1))
  449. break;
  450. furi_string_printf(
  451. key, "%s File %d Lo Limit", furi_string_get_cstr(prefix), f->id);
  452. if(!flipper_format_read_uint32(
  453. file, furi_string_get_cstr(key), &f->settings.value.lo_limit, 1))
  454. break;
  455. furi_string_printf(
  456. key, "%s File %d Limited Credit Value", furi_string_get_cstr(prefix), f->id);
  457. if(!flipper_format_read_uint32(
  458. file, furi_string_get_cstr(key), &f->settings.value.limited_credit_value, 1))
  459. break;
  460. furi_string_printf(
  461. key, "%s File %d Limited Credit Enabled", furi_string_get_cstr(prefix), f->id);
  462. if(!flipper_format_read_bool(
  463. file,
  464. furi_string_get_cstr(key),
  465. &f->settings.value.limited_credit_enabled,
  466. 1))
  467. break;
  468. } else if(
  469. f->type == MifareDesfireFileTypeLinearRecord ||
  470. f->type == MifareDesfireFileTypeCyclicRecord) {
  471. furi_string_printf(key, "%s File %d Size", furi_string_get_cstr(prefix), f->id);
  472. if(!flipper_format_read_uint32(
  473. file, furi_string_get_cstr(key), &f->settings.record.size, 1))
  474. break;
  475. furi_string_printf(key, "%s File %d Max", furi_string_get_cstr(prefix), f->id);
  476. if(!flipper_format_read_uint32(
  477. file, furi_string_get_cstr(key), &f->settings.record.max, 1))
  478. break;
  479. furi_string_printf(key, "%s File %d Cur", furi_string_get_cstr(prefix), f->id);
  480. if(!flipper_format_read_uint32(
  481. file, furi_string_get_cstr(key), &f->settings.record.cur, 1))
  482. break;
  483. }
  484. furi_string_printf(key, "%s File %d", furi_string_get_cstr(prefix), f->id);
  485. if(flipper_format_key_exist(file, furi_string_get_cstr(key))) {
  486. uint32_t size;
  487. if(!flipper_format_get_value_count(file, furi_string_get_cstr(key), &size)) break;
  488. f->contents = malloc(size);
  489. if(!flipper_format_read_hex(file, furi_string_get_cstr(key), f->contents, size))
  490. break;
  491. }
  492. *file_head = f;
  493. file_head = &f->next;
  494. f = NULL;
  495. parsed_files = true;
  496. }
  497. if(!parsed_files) {
  498. break;
  499. }
  500. parsed = true;
  501. } while(false);
  502. if(f) {
  503. free(f->contents);
  504. free(f);
  505. }
  506. free(tmp);
  507. furi_string_free(prefix);
  508. furi_string_free(key);
  509. return parsed;
  510. }
  511. static bool nfc_device_save_mifare_df_data(FlipperFormat* file, NfcDevice* dev) {
  512. bool saved = false;
  513. MifareDesfireData* data = &dev->dev_data.mf_df_data;
  514. uint8_t* tmp = NULL;
  515. do {
  516. if(!flipper_format_write_comment_cstr(file, "Mifare DESFire specific data")) break;
  517. if(!flipper_format_write_hex(
  518. file, "PICC Version", (uint8_t*)&data->version, sizeof(data->version)))
  519. break;
  520. if(data->free_memory) {
  521. if(!flipper_format_write_uint32(file, "PICC Free Memory", &data->free_memory->bytes, 1))
  522. break;
  523. }
  524. if(data->master_key_settings) {
  525. if(!nfc_device_save_mifare_df_key_settings(file, data->master_key_settings, "PICC"))
  526. break;
  527. }
  528. uint32_t n_apps = 0;
  529. for(MifareDesfireApplication* app = data->app_head; app; app = app->next) {
  530. n_apps++;
  531. }
  532. if(!flipper_format_write_uint32(file, "Application Count", &n_apps, 1)) break;
  533. if(n_apps) {
  534. tmp = malloc(n_apps * 3);
  535. int i = 0;
  536. for(MifareDesfireApplication* app = data->app_head; app; app = app->next) {
  537. memcpy(tmp + i, app->id, 3);
  538. i += 3;
  539. }
  540. if(!flipper_format_write_hex(file, "Application IDs", tmp, n_apps * 3)) break;
  541. for(MifareDesfireApplication* app = data->app_head; app; app = app->next) {
  542. if(!nfc_device_save_mifare_df_app(file, app)) break;
  543. }
  544. }
  545. saved = true;
  546. } while(false);
  547. free(tmp);
  548. return saved;
  549. }
  550. bool nfc_device_load_mifare_df_data(FlipperFormat* file, NfcDevice* dev) {
  551. bool parsed = false;
  552. MifareDesfireData* data = &dev->dev_data.mf_df_data;
  553. memset(data, 0, sizeof(MifareDesfireData));
  554. uint8_t* tmp = NULL;
  555. do {
  556. if(!flipper_format_read_hex(
  557. file, "PICC Version", (uint8_t*)&data->version, sizeof(data->version)))
  558. break;
  559. if(flipper_format_key_exist(file, "PICC Free Memory")) {
  560. data->free_memory = malloc(sizeof(MifareDesfireFreeMemory));
  561. memset(data->free_memory, 0, sizeof(MifareDesfireFreeMemory));
  562. if(!flipper_format_read_uint32(
  563. file, "PICC Free Memory", &data->free_memory->bytes, 1)) {
  564. free(data->free_memory);
  565. break;
  566. }
  567. }
  568. if(flipper_format_key_exist(file, "PICC Change Key ID")) {
  569. data->master_key_settings = malloc(sizeof(MifareDesfireKeySettings));
  570. memset(data->master_key_settings, 0, sizeof(MifareDesfireKeySettings));
  571. if(!nfc_device_load_mifare_df_key_settings(file, data->master_key_settings, "PICC")) {
  572. free(data->master_key_settings);
  573. data->master_key_settings = NULL;
  574. break;
  575. }
  576. }
  577. uint32_t n_apps;
  578. if(!flipper_format_read_uint32(file, "Application Count", &n_apps, 1)) break;
  579. if(n_apps) {
  580. tmp = malloc(n_apps * 3);
  581. if(!flipper_format_read_hex(file, "Application IDs", tmp, n_apps * 3)) break;
  582. bool parsed_apps = true;
  583. MifareDesfireApplication** app_head = &data->app_head;
  584. for(uint32_t i = 0; i < n_apps; i++) {
  585. MifareDesfireApplication* app = malloc(sizeof(MifareDesfireApplication));
  586. memset(app, 0, sizeof(MifareDesfireApplication));
  587. memcpy(app->id, &tmp[i * 3], 3);
  588. if(!nfc_device_load_mifare_df_app(file, app)) {
  589. free(app);
  590. parsed_apps = false;
  591. break;
  592. }
  593. *app_head = app;
  594. app_head = &app->next;
  595. }
  596. if(!parsed_apps) {
  597. // accept non-parsed apps, just log a warning:
  598. FURI_LOG_W(TAG, "Non-parsed apps found!");
  599. }
  600. }
  601. parsed = true;
  602. } while(false);
  603. free(tmp);
  604. return parsed;
  605. }
  606. // Leave for backward compatibility
  607. bool nfc_device_load_bank_card_data(FlipperFormat* file, NfcDevice* dev) {
  608. bool parsed = false;
  609. EmvData* data = &dev->dev_data.emv_data;
  610. memset(data, 0, sizeof(EmvData));
  611. uint32_t data_cnt = 0;
  612. FuriString* temp_str;
  613. temp_str = furi_string_alloc();
  614. do {
  615. // Load essential data
  616. if(!flipper_format_get_value_count(file, "AID", &data_cnt)) break;
  617. data->aid_len = data_cnt;
  618. if(!flipper_format_read_hex(file, "AID", data->aid, data->aid_len)) break;
  619. if(!flipper_format_read_string(file, "Name", temp_str)) break;
  620. strlcpy(data->name, furi_string_get_cstr(temp_str), sizeof(data->name));
  621. if(!flipper_format_get_value_count(file, "Number", &data_cnt)) break;
  622. data->number_len = data_cnt;
  623. if(!flipper_format_read_hex(file, "Number", data->number, data->number_len)) break;
  624. parsed = true;
  625. // Load optional data
  626. uint8_t exp_data[2] = {};
  627. if(flipper_format_read_hex(file, "Exp data", exp_data, 2)) {
  628. data->exp_mon = exp_data[0];
  629. data->exp_year = exp_data[1];
  630. }
  631. if(flipper_format_read_uint32(file, "Country code", &data_cnt, 1)) {
  632. data->country_code = data_cnt;
  633. }
  634. if(flipper_format_read_uint32(file, "Currency code", &data_cnt, 1)) {
  635. data->currency_code = data_cnt;
  636. }
  637. } while(false);
  638. furi_string_free(temp_str);
  639. return parsed;
  640. }
  641. static void nfc_device_write_mifare_classic_block(
  642. FuriString* block_str,
  643. MfClassicData* data,
  644. uint8_t block_num) {
  645. furi_string_reset(block_str);
  646. bool is_sec_trailer = mf_classic_is_sector_trailer(block_num);
  647. if(is_sec_trailer) {
  648. uint8_t sector_num = mf_classic_get_sector_by_block(block_num);
  649. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, sector_num);
  650. // Write key A
  651. for(size_t i = 0; i < sizeof(sec_tr->key_a); i++) {
  652. if(mf_classic_is_key_found(data, sector_num, MfClassicKeyA)) {
  653. furi_string_cat_printf(block_str, "%02X ", sec_tr->key_a[i]);
  654. } else {
  655. furi_string_cat_printf(block_str, "?? ");
  656. }
  657. }
  658. // Write Access bytes
  659. for(size_t i = 0; i < MF_CLASSIC_ACCESS_BYTES_SIZE; i++) {
  660. if(mf_classic_is_block_read(data, block_num)) {
  661. furi_string_cat_printf(block_str, "%02X ", sec_tr->access_bits[i]);
  662. } else {
  663. furi_string_cat_printf(block_str, "?? ");
  664. }
  665. }
  666. // Write key B
  667. for(size_t i = 0; i < sizeof(sec_tr->key_b); i++) {
  668. if(mf_classic_is_key_found(data, sector_num, MfClassicKeyB)) {
  669. furi_string_cat_printf(block_str, "%02X ", sec_tr->key_b[i]);
  670. } else {
  671. furi_string_cat_printf(block_str, "?? ");
  672. }
  673. }
  674. } else {
  675. // Write data block
  676. for(size_t i = 0; i < MF_CLASSIC_BLOCK_SIZE; i++) {
  677. if(mf_classic_is_block_read(data, block_num)) {
  678. furi_string_cat_printf(block_str, "%02X ", data->block[block_num].value[i]);
  679. } else {
  680. furi_string_cat_printf(block_str, "?? ");
  681. }
  682. }
  683. }
  684. furi_string_trim(block_str);
  685. }
  686. static bool nfc_device_save_mifare_classic_data(FlipperFormat* file, NfcDevice* dev) {
  687. bool saved = false;
  688. MfClassicData* data = &dev->dev_data.mf_classic_data;
  689. FuriString* temp_str;
  690. temp_str = furi_string_alloc();
  691. uint16_t blocks = 0;
  692. // Save Mifare Classic specific data
  693. do {
  694. if(!flipper_format_write_comment_cstr(file, "Mifare Classic specific data")) break;
  695. if(data->type == MfClassicType1k) {
  696. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "1K")) break;
  697. blocks = 64;
  698. } else if(data->type == MfClassicType4k) {
  699. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "4K")) break;
  700. blocks = 256;
  701. }
  702. if(!flipper_format_write_uint32(
  703. file, "Data format version", &nfc_mifare_classic_data_format_version, 1))
  704. break;
  705. if(!flipper_format_write_comment_cstr(
  706. file, "Mifare Classic blocks, \'??\' means unknown data"))
  707. break;
  708. bool block_saved = true;
  709. FuriString* block_str;
  710. block_str = furi_string_alloc();
  711. for(size_t i = 0; i < blocks; i++) {
  712. furi_string_printf(temp_str, "Block %d", i);
  713. nfc_device_write_mifare_classic_block(block_str, data, i);
  714. if(!flipper_format_write_string(file, furi_string_get_cstr(temp_str), block_str)) {
  715. block_saved = false;
  716. break;
  717. }
  718. }
  719. furi_string_free(block_str);
  720. if(!block_saved) break;
  721. saved = true;
  722. } while(false);
  723. furi_string_free(temp_str);
  724. return saved;
  725. }
  726. static void nfc_device_load_mifare_classic_block(
  727. FuriString* block_str,
  728. MfClassicData* data,
  729. uint8_t block_num) {
  730. furi_string_trim(block_str);
  731. MfClassicBlock block_tmp = {};
  732. bool is_sector_trailer = mf_classic_is_sector_trailer(block_num);
  733. uint8_t sector_num = mf_classic_get_sector_by_block(block_num);
  734. uint16_t block_unknown_bytes_mask = 0;
  735. furi_string_trim(block_str);
  736. for(size_t i = 0; i < MF_CLASSIC_BLOCK_SIZE; i++) {
  737. char hi = furi_string_get_char(block_str, 3 * i);
  738. char low = furi_string_get_char(block_str, 3 * i + 1);
  739. uint8_t byte = 0;
  740. if(hex_char_to_uint8(hi, low, &byte)) {
  741. block_tmp.value[i] = byte;
  742. } else {
  743. FURI_BIT_SET(block_unknown_bytes_mask, i);
  744. }
  745. }
  746. if(block_unknown_bytes_mask == 0xffff) {
  747. // All data is unknown, exit
  748. return;
  749. }
  750. if(is_sector_trailer) {
  751. MfClassicSectorTrailer* sec_tr_tmp = (MfClassicSectorTrailer*)&block_tmp;
  752. // Load Key A
  753. // Key A mask 0b0000000000111111 = 0x003f
  754. if((block_unknown_bytes_mask & 0x003f) == 0) {
  755. uint64_t key = nfc_util_bytes2num(sec_tr_tmp->key_a, sizeof(sec_tr_tmp->key_a));
  756. mf_classic_set_key_found(data, sector_num, MfClassicKeyA, key);
  757. }
  758. // Load Access Bits
  759. // Access bits mask 0b0000001111000000 = 0x03c0
  760. if((block_unknown_bytes_mask & 0x03c0) == 0) {
  761. mf_classic_set_block_read(data, block_num, &block_tmp);
  762. }
  763. // Load Key B
  764. // Key B mask 0b1111110000000000 = 0xfc00
  765. if((block_unknown_bytes_mask & 0xfc00) == 0) {
  766. uint64_t key = nfc_util_bytes2num(sec_tr_tmp->key_b, sizeof(sec_tr_tmp->key_b));
  767. mf_classic_set_key_found(data, sector_num, MfClassicKeyB, key);
  768. }
  769. } else {
  770. if(block_unknown_bytes_mask == 0) {
  771. mf_classic_set_block_read(data, block_num, &block_tmp);
  772. }
  773. }
  774. }
  775. static bool nfc_device_load_mifare_classic_data(FlipperFormat* file, NfcDevice* dev) {
  776. bool parsed = false;
  777. MfClassicData* data = &dev->dev_data.mf_classic_data;
  778. FuriString* temp_str;
  779. uint32_t data_format_version = 0;
  780. temp_str = furi_string_alloc();
  781. uint16_t data_blocks = 0;
  782. memset(data, 0, sizeof(MfClassicData));
  783. do {
  784. // Read Mifare Classic type
  785. if(!flipper_format_read_string(file, "Mifare Classic type", temp_str)) break;
  786. if(!furi_string_cmp(temp_str, "1K")) {
  787. data->type = MfClassicType1k;
  788. data_blocks = 64;
  789. } else if(!furi_string_cmp(temp_str, "4K")) {
  790. data->type = MfClassicType4k;
  791. data_blocks = 256;
  792. } else {
  793. break;
  794. }
  795. bool old_format = false;
  796. // Read Mifare Classic format version
  797. if(!flipper_format_read_uint32(file, "Data format version", &data_format_version, 1)) {
  798. // Load unread sectors with zero keys access for backward compatability
  799. if(!flipper_format_rewind(file)) break;
  800. old_format = true;
  801. } else {
  802. if(data_format_version < nfc_mifare_classic_data_format_version) {
  803. old_format = true;
  804. }
  805. }
  806. // Read Mifare Classic blocks
  807. bool block_read = true;
  808. FuriString* block_str;
  809. block_str = furi_string_alloc();
  810. for(size_t i = 0; i < data_blocks; i++) {
  811. furi_string_printf(temp_str, "Block %d", i);
  812. if(!flipper_format_read_string(file, furi_string_get_cstr(temp_str), block_str)) {
  813. block_read = false;
  814. break;
  815. }
  816. nfc_device_load_mifare_classic_block(block_str, data, i);
  817. }
  818. furi_string_free(block_str);
  819. if(!block_read) break;
  820. // Set keys and blocks as unknown for backward compatibility
  821. if(old_format) {
  822. data->key_a_mask = 0ULL;
  823. data->key_b_mask = 0ULL;
  824. memset(data->block_read_mask, 0, sizeof(data->block_read_mask));
  825. }
  826. parsed = true;
  827. } while(false);
  828. furi_string_free(temp_str);
  829. return parsed;
  830. }
  831. static void nfc_device_get_key_cache_file_path(NfcDevice* dev, FuriString* file_path) {
  832. uint8_t* uid = dev->dev_data.nfc_data.uid;
  833. uint8_t uid_len = dev->dev_data.nfc_data.uid_len;
  834. furi_string_set(file_path, NFC_DEVICE_KEYS_FOLDER "/");
  835. for(size_t i = 0; i < uid_len; i++) {
  836. furi_string_cat_printf(file_path, "%02X", uid[i]);
  837. }
  838. furi_string_cat_printf(file_path, NFC_DEVICE_KEYS_EXTENSION);
  839. }
  840. static bool nfc_device_save_mifare_classic_keys(NfcDevice* dev) {
  841. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  842. MfClassicData* data = &dev->dev_data.mf_classic_data;
  843. FuriString* temp_str;
  844. temp_str = furi_string_alloc();
  845. nfc_device_get_key_cache_file_path(dev, temp_str);
  846. bool save_success = false;
  847. do {
  848. if(!storage_simply_mkdir(dev->storage, NFC_DEVICE_KEYS_FOLDER)) break;
  849. if(!storage_simply_remove(dev->storage, furi_string_get_cstr(temp_str))) break;
  850. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  851. if(!flipper_format_write_header_cstr(file, nfc_keys_file_header, nfc_keys_file_version))
  852. break;
  853. if(data->type == MfClassicType1k) {
  854. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "1K")) break;
  855. } else if(data->type == MfClassicType4k) {
  856. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "4K")) break;
  857. }
  858. if(!flipper_format_write_hex_uint64(file, "Key A map", &data->key_a_mask, 1)) break;
  859. if(!flipper_format_write_hex_uint64(file, "Key B map", &data->key_b_mask, 1)) break;
  860. uint8_t sector_num = mf_classic_get_total_sectors_num(data->type);
  861. bool key_save_success = true;
  862. for(size_t i = 0; (i < sector_num) && (key_save_success); i++) {
  863. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, i);
  864. if(FURI_BIT(data->key_a_mask, i)) {
  865. furi_string_printf(temp_str, "Key A sector %d", i);
  866. key_save_success = flipper_format_write_hex(
  867. file, furi_string_get_cstr(temp_str), sec_tr->key_a, 6);
  868. }
  869. if(!key_save_success) break;
  870. if(FURI_BIT(data->key_b_mask, i)) {
  871. furi_string_printf(temp_str, "Key B sector %d", i);
  872. key_save_success = flipper_format_write_hex(
  873. file, furi_string_get_cstr(temp_str), sec_tr->key_b, 6);
  874. }
  875. }
  876. save_success = key_save_success;
  877. } while(false);
  878. flipper_format_free(file);
  879. furi_string_free(temp_str);
  880. return save_success;
  881. }
  882. bool nfc_device_load_key_cache(NfcDevice* dev) {
  883. furi_assert(dev);
  884. FuriString* temp_str;
  885. temp_str = furi_string_alloc();
  886. MfClassicData* data = &dev->dev_data.mf_classic_data;
  887. nfc_device_get_key_cache_file_path(dev, temp_str);
  888. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  889. bool load_success = false;
  890. do {
  891. if(storage_common_stat(dev->storage, furi_string_get_cstr(temp_str), NULL) != FSE_OK)
  892. break;
  893. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(temp_str))) break;
  894. uint32_t version = 0;
  895. if(!flipper_format_read_header(file, temp_str, &version)) break;
  896. if(furi_string_cmp_str(temp_str, nfc_keys_file_header)) break;
  897. if(version != nfc_keys_file_version) break;
  898. if(!flipper_format_read_string(file, "Mifare Classic type", temp_str)) break;
  899. if(!furi_string_cmp(temp_str, "1K")) {
  900. data->type = MfClassicType1k;
  901. } else if(!furi_string_cmp(temp_str, "4K")) {
  902. data->type = MfClassicType4k;
  903. } else {
  904. break;
  905. }
  906. if(!flipper_format_read_hex_uint64(file, "Key A map", &data->key_a_mask, 1)) break;
  907. if(!flipper_format_read_hex_uint64(file, "Key B map", &data->key_b_mask, 1)) break;
  908. uint8_t sectors = mf_classic_get_total_sectors_num(data->type);
  909. bool key_read_success = true;
  910. for(size_t i = 0; (i < sectors) && (key_read_success); i++) {
  911. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, i);
  912. if(FURI_BIT(data->key_a_mask, i)) {
  913. furi_string_printf(temp_str, "Key A sector %d", i);
  914. key_read_success = flipper_format_read_hex(
  915. file, furi_string_get_cstr(temp_str), sec_tr->key_a, 6);
  916. }
  917. if(!key_read_success) break;
  918. if(FURI_BIT(data->key_b_mask, i)) {
  919. furi_string_printf(temp_str, "Key B sector %d", i);
  920. key_read_success = flipper_format_read_hex(
  921. file, furi_string_get_cstr(temp_str), sec_tr->key_b, 6);
  922. }
  923. }
  924. load_success = key_read_success;
  925. } while(false);
  926. furi_string_free(temp_str);
  927. flipper_format_free(file);
  928. return load_success;
  929. }
  930. void nfc_device_set_name(NfcDevice* dev, const char* name) {
  931. furi_assert(dev);
  932. strlcpy(dev->dev_name, name, NFC_DEV_NAME_MAX_LEN);
  933. }
  934. static void nfc_device_get_path_without_ext(FuriString* orig_path, FuriString* shadow_path) {
  935. // TODO: this won't work if there is ".nfc" anywhere in the path other than
  936. // at the end
  937. size_t ext_start = furi_string_search(orig_path, NFC_APP_EXTENSION);
  938. furi_string_set_n(shadow_path, orig_path, 0, ext_start);
  939. }
  940. static void nfc_device_get_shadow_path(FuriString* orig_path, FuriString* shadow_path) {
  941. nfc_device_get_path_without_ext(orig_path, shadow_path);
  942. furi_string_cat_printf(shadow_path, "%s", NFC_APP_SHADOW_EXTENSION);
  943. }
  944. bool nfc_device_save(NfcDevice* dev, const char* dev_name) {
  945. furi_assert(dev);
  946. bool saved = false;
  947. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  948. FuriHalNfcDevData* data = &dev->dev_data.nfc_data;
  949. FuriString* temp_str;
  950. temp_str = furi_string_alloc();
  951. do {
  952. // Create nfc directory if necessary
  953. if(!storage_simply_mkdir(dev->storage, NFC_APP_FOLDER)) break;
  954. // First remove nfc device file if it was saved
  955. furi_string_printf(temp_str, "%s", dev_name);
  956. // Open file
  957. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  958. // Write header
  959. if(!flipper_format_write_header_cstr(file, nfc_file_header, nfc_file_version)) break;
  960. // Write nfc device type
  961. if(!flipper_format_write_comment_cstr(
  962. file, "Nfc device type can be UID, Mifare Ultralight, Mifare Classic"))
  963. break;
  964. nfc_device_prepare_format_string(dev, temp_str);
  965. if(!flipper_format_write_string(file, "Device type", temp_str)) break;
  966. // Write UID, ATQA, SAK
  967. if(!flipper_format_write_comment_cstr(file, "UID, ATQA and SAK are common for all formats"))
  968. break;
  969. if(!flipper_format_write_hex(file, "UID", data->uid, data->uid_len)) break;
  970. if(!flipper_format_write_hex(file, "ATQA", data->atqa, 2)) break;
  971. if(!flipper_format_write_hex(file, "SAK", &data->sak, 1)) break;
  972. // Save more data if necessary
  973. if(dev->format == NfcDeviceSaveFormatMifareUl) {
  974. if(!nfc_device_save_mifare_ul_data(file, dev)) break;
  975. } else if(dev->format == NfcDeviceSaveFormatMifareDesfire) {
  976. if(!nfc_device_save_mifare_df_data(file, dev)) break;
  977. } else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
  978. // Save data
  979. if(!nfc_device_save_mifare_classic_data(file, dev)) break;
  980. // Save keys cache
  981. if(!nfc_device_save_mifare_classic_keys(dev)) break;
  982. }
  983. saved = true;
  984. } while(0);
  985. if(!saved) {
  986. dialog_message_show_storage_error(dev->dialogs, "Can not save\nkey file");
  987. }
  988. furi_string_free(temp_str);
  989. flipper_format_free(file);
  990. return saved;
  991. }
  992. bool nfc_device_save_shadow(NfcDevice* dev, const char* path) {
  993. dev->shadow_file_exist = true;
  994. // Replace extension from .nfc to .shd if necessary
  995. FuriString* orig_path = furi_string_alloc();
  996. furi_string_set_str(orig_path, path);
  997. FuriString* shadow_path = furi_string_alloc();
  998. nfc_device_get_shadow_path(orig_path, shadow_path);
  999. bool file_saved = nfc_device_save(dev, furi_string_get_cstr(shadow_path));
  1000. furi_string_free(orig_path);
  1001. furi_string_free(shadow_path);
  1002. return file_saved;
  1003. }
  1004. static bool nfc_device_load_data(NfcDevice* dev, FuriString* path, bool show_dialog) {
  1005. bool parsed = false;
  1006. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  1007. FuriHalNfcDevData* data = &dev->dev_data.nfc_data;
  1008. uint32_t data_cnt = 0;
  1009. FuriString* temp_str;
  1010. temp_str = furi_string_alloc();
  1011. bool deprecated_version = false;
  1012. if(dev->loading_cb) {
  1013. dev->loading_cb(dev->loading_cb_ctx, true);
  1014. }
  1015. do {
  1016. // Check existance of shadow file
  1017. nfc_device_get_shadow_path(path, temp_str);
  1018. dev->shadow_file_exist =
  1019. storage_common_stat(dev->storage, furi_string_get_cstr(temp_str), NULL) == FSE_OK;
  1020. // Open shadow file if it exists. If not - open original
  1021. if(dev->shadow_file_exist) {
  1022. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(temp_str))) break;
  1023. } else {
  1024. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(path))) break;
  1025. }
  1026. // Read and verify file header
  1027. uint32_t version = 0;
  1028. if(!flipper_format_read_header(file, temp_str, &version)) break;
  1029. if(furi_string_cmp_str(temp_str, nfc_file_header) || (version != nfc_file_version)) {
  1030. deprecated_version = true;
  1031. break;
  1032. }
  1033. // Read Nfc device type
  1034. if(!flipper_format_read_string(file, "Device type", temp_str)) break;
  1035. if(!nfc_device_parse_format_string(dev, temp_str)) break;
  1036. // Read and parse UID, ATQA and SAK
  1037. if(!flipper_format_get_value_count(file, "UID", &data_cnt)) break;
  1038. if(!(data_cnt == 4 || data_cnt == 7)) break;
  1039. data->uid_len = data_cnt;
  1040. if(!flipper_format_read_hex(file, "UID", data->uid, data->uid_len)) break;
  1041. if(!flipper_format_read_hex(file, "ATQA", data->atqa, 2)) break;
  1042. if(!flipper_format_read_hex(file, "SAK", &data->sak, 1)) break;
  1043. // Load CUID
  1044. uint8_t* cuid_start = data->uid;
  1045. if(data->uid_len == 7) {
  1046. cuid_start = &data->uid[3];
  1047. }
  1048. data->cuid = (cuid_start[0] << 24) | (cuid_start[1] << 16) | (cuid_start[2] << 8) |
  1049. (cuid_start[3]);
  1050. // Parse other data
  1051. if(dev->format == NfcDeviceSaveFormatMifareUl) {
  1052. if(!nfc_device_load_mifare_ul_data(file, dev)) break;
  1053. } else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
  1054. if(!nfc_device_load_mifare_classic_data(file, dev)) break;
  1055. } else if(dev->format == NfcDeviceSaveFormatMifareDesfire) {
  1056. if(!nfc_device_load_mifare_df_data(file, dev)) break;
  1057. } else if(dev->format == NfcDeviceSaveFormatBankCard) {
  1058. if(!nfc_device_load_bank_card_data(file, dev)) break;
  1059. }
  1060. parsed = true;
  1061. } while(false);
  1062. if(dev->loading_cb) {
  1063. dev->loading_cb(dev->loading_cb_ctx, false);
  1064. }
  1065. if((!parsed) && (show_dialog)) {
  1066. if(deprecated_version) {
  1067. dialog_message_show_storage_error(dev->dialogs, "File format deprecated");
  1068. } else {
  1069. dialog_message_show_storage_error(dev->dialogs, "Can not parse\nfile");
  1070. }
  1071. }
  1072. furi_string_free(temp_str);
  1073. flipper_format_free(file);
  1074. return parsed;
  1075. }
  1076. bool nfc_device_load(NfcDevice* dev, const char* file_path, bool show_dialog) {
  1077. furi_assert(dev);
  1078. furi_assert(file_path);
  1079. // Load device data
  1080. furi_string_set(dev->load_path, file_path);
  1081. bool dev_load = nfc_device_load_data(dev, dev->load_path, show_dialog);
  1082. if(dev_load) {
  1083. // Set device name
  1084. FuriString* filename;
  1085. filename = furi_string_alloc();
  1086. path_extract_filename_no_ext(file_path, filename);
  1087. nfc_device_set_name(dev, furi_string_get_cstr(filename));
  1088. furi_string_free(filename);
  1089. }
  1090. return dev_load;
  1091. }
  1092. bool nfc_file_select(NfcDevice* dev) {
  1093. furi_assert(dev);
  1094. // Input events and views are managed by file_browser
  1095. FuriString* nfc_app_folder;
  1096. nfc_app_folder = furi_string_alloc_set(NFC_APP_FOLDER);
  1097. const DialogsFileBrowserOptions browser_options = {
  1098. .extension = NFC_APP_EXTENSION,
  1099. .skip_assets = true,
  1100. .icon = &I_Nfc_10px,
  1101. .hide_ext = true,
  1102. .item_loader_callback = NULL,
  1103. .item_loader_context = NULL,
  1104. };
  1105. bool res =
  1106. dialog_file_browser_show(dev->dialogs, dev->load_path, dev->load_path, &browser_options);
  1107. furi_string_free(nfc_app_folder);
  1108. if(res) {
  1109. FuriString* filename;
  1110. filename = furi_string_alloc();
  1111. path_extract_filename(dev->load_path, filename, true);
  1112. strncpy(dev->dev_name, furi_string_get_cstr(filename), NFC_DEV_NAME_MAX_LEN);
  1113. res = nfc_device_load_data(dev, dev->load_path, true);
  1114. if(res) {
  1115. nfc_device_set_name(dev, dev->dev_name);
  1116. }
  1117. furi_string_free(filename);
  1118. }
  1119. return res;
  1120. }
  1121. void nfc_device_data_clear(NfcDeviceData* dev_data) {
  1122. if(dev_data->protocol == NfcDeviceProtocolMifareDesfire) {
  1123. mf_df_clear(&dev_data->mf_df_data);
  1124. } else if(dev_data->protocol == NfcDeviceProtocolMifareClassic) {
  1125. memset(&dev_data->mf_classic_data, 0, sizeof(MfClassicData));
  1126. } else if(dev_data->protocol == NfcDeviceProtocolMifareUl) {
  1127. mf_ul_reset(&dev_data->mf_ul_data);
  1128. } else if(dev_data->protocol == NfcDeviceProtocolEMV) {
  1129. memset(&dev_data->emv_data, 0, sizeof(EmvData));
  1130. }
  1131. memset(&dev_data->nfc_data, 0, sizeof(FuriHalNfcDevData));
  1132. dev_data->protocol = NfcDeviceProtocolUnknown;
  1133. furi_string_reset(dev_data->parsed_data);
  1134. }
  1135. void nfc_device_clear(NfcDevice* dev) {
  1136. furi_assert(dev);
  1137. nfc_device_set_name(dev, "");
  1138. nfc_device_data_clear(&dev->dev_data);
  1139. dev->format = NfcDeviceSaveFormatUid;
  1140. furi_string_reset(dev->load_path);
  1141. }
  1142. bool nfc_device_delete(NfcDevice* dev, bool use_load_path) {
  1143. furi_assert(dev);
  1144. bool deleted = false;
  1145. FuriString* file_path;
  1146. file_path = furi_string_alloc();
  1147. do {
  1148. // Delete original file
  1149. if(use_load_path && !furi_string_empty(dev->load_path)) {
  1150. furi_string_set(file_path, dev->load_path);
  1151. } else {
  1152. furi_string_printf(
  1153. file_path, "%s/%s%s", NFC_APP_FOLDER, dev->dev_name, NFC_APP_EXTENSION);
  1154. }
  1155. if(!storage_simply_remove(dev->storage, furi_string_get_cstr(file_path))) break;
  1156. // Delete shadow file if it exists
  1157. if(dev->shadow_file_exist) {
  1158. if(use_load_path && !furi_string_empty(dev->load_path)) {
  1159. nfc_device_get_shadow_path(dev->load_path, file_path);
  1160. } else {
  1161. furi_string_printf(
  1162. file_path, "%s/%s%s", NFC_APP_FOLDER, dev->dev_name, NFC_APP_SHADOW_EXTENSION);
  1163. }
  1164. if(!storage_simply_remove(dev->storage, furi_string_get_cstr(file_path))) break;
  1165. }
  1166. deleted = true;
  1167. } while(0);
  1168. if(!deleted) {
  1169. dialog_message_show_storage_error(dev->dialogs, "Can not remove file");
  1170. }
  1171. furi_string_free(file_path);
  1172. return deleted;
  1173. }
  1174. bool nfc_device_restore(NfcDevice* dev, bool use_load_path) {
  1175. furi_assert(dev);
  1176. furi_assert(dev->shadow_file_exist);
  1177. bool restored = false;
  1178. FuriString* path;
  1179. path = furi_string_alloc();
  1180. do {
  1181. if(use_load_path && !furi_string_empty(dev->load_path)) {
  1182. nfc_device_get_shadow_path(dev->load_path, path);
  1183. } else {
  1184. furi_string_printf(
  1185. path, "%s/%s%s", NFC_APP_FOLDER, dev->dev_name, NFC_APP_SHADOW_EXTENSION);
  1186. }
  1187. if(!storage_simply_remove(dev->storage, furi_string_get_cstr(path))) break;
  1188. dev->shadow_file_exist = false;
  1189. if(use_load_path && !furi_string_empty(dev->load_path)) {
  1190. furi_string_set(path, dev->load_path);
  1191. } else {
  1192. furi_string_printf(path, "%s/%s%s", NFC_APP_FOLDER, dev->dev_name, NFC_APP_EXTENSION);
  1193. }
  1194. if(!nfc_device_load_data(dev, path, true)) break;
  1195. restored = true;
  1196. } while(0);
  1197. furi_string_free(path);
  1198. return restored;
  1199. }
  1200. void nfc_device_set_loading_callback(NfcDevice* dev, NfcLoadingCallback callback, void* context) {
  1201. furi_assert(dev);
  1202. dev->loading_cb = callback;
  1203. dev->loading_cb_ctx = context;
  1204. }