nfc_device.c 52 KB

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