nfc_device.c 55 KB

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