nfc_device.c 53 KB

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