nfc_device.c 52 KB

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