nfc_device.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  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 == MfClassicType1k) {
  702. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "1K")) break;
  703. blocks = 64;
  704. } else if(data->type == MfClassicType4k) {
  705. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "4K")) break;
  706. blocks = 256;
  707. }
  708. if(!flipper_format_write_uint32(
  709. file, "Data format version", &nfc_mifare_classic_data_format_version, 1))
  710. break;
  711. if(!flipper_format_write_comment_cstr(
  712. file, "Mifare Classic blocks, \'??\' means unknown data"))
  713. break;
  714. bool block_saved = true;
  715. FuriString* block_str;
  716. block_str = furi_string_alloc();
  717. for(size_t i = 0; i < blocks; i++) {
  718. furi_string_printf(temp_str, "Block %d", i);
  719. nfc_device_write_mifare_classic_block(block_str, data, i);
  720. if(!flipper_format_write_string(file, furi_string_get_cstr(temp_str), block_str)) {
  721. block_saved = false;
  722. break;
  723. }
  724. }
  725. furi_string_free(block_str);
  726. if(!block_saved) break;
  727. saved = true;
  728. } while(false);
  729. furi_string_free(temp_str);
  730. return saved;
  731. }
  732. static void nfc_device_load_mifare_classic_block(
  733. FuriString* block_str,
  734. MfClassicData* data,
  735. uint8_t block_num) {
  736. furi_string_trim(block_str);
  737. MfClassicBlock block_tmp = {};
  738. bool is_sector_trailer = mf_classic_is_sector_trailer(block_num);
  739. uint8_t sector_num = mf_classic_get_sector_by_block(block_num);
  740. uint16_t block_unknown_bytes_mask = 0;
  741. furi_string_trim(block_str);
  742. for(size_t i = 0; i < MF_CLASSIC_BLOCK_SIZE; i++) {
  743. char hi = furi_string_get_char(block_str, 3 * i);
  744. char low = furi_string_get_char(block_str, 3 * i + 1);
  745. uint8_t byte = 0;
  746. if(hex_char_to_uint8(hi, low, &byte)) {
  747. block_tmp.value[i] = byte;
  748. } else {
  749. FURI_BIT_SET(block_unknown_bytes_mask, i);
  750. }
  751. }
  752. if(block_unknown_bytes_mask == 0xffff) {
  753. // All data is unknown, exit
  754. return;
  755. }
  756. if(is_sector_trailer) {
  757. MfClassicSectorTrailer* sec_tr_tmp = (MfClassicSectorTrailer*)&block_tmp;
  758. // Load Key A
  759. // Key A mask 0b0000000000111111 = 0x003f
  760. if((block_unknown_bytes_mask & 0x003f) == 0) {
  761. uint64_t key = nfc_util_bytes2num(sec_tr_tmp->key_a, sizeof(sec_tr_tmp->key_a));
  762. mf_classic_set_key_found(data, sector_num, MfClassicKeyA, key);
  763. }
  764. // Load Access Bits
  765. // Access bits mask 0b0000001111000000 = 0x03c0
  766. if((block_unknown_bytes_mask & 0x03c0) == 0) {
  767. mf_classic_set_block_read(data, block_num, &block_tmp);
  768. }
  769. // Load Key B
  770. // Key B mask 0b1111110000000000 = 0xfc00
  771. if((block_unknown_bytes_mask & 0xfc00) == 0) {
  772. uint64_t key = nfc_util_bytes2num(sec_tr_tmp->key_b, sizeof(sec_tr_tmp->key_b));
  773. mf_classic_set_key_found(data, sector_num, MfClassicKeyB, key);
  774. }
  775. } else {
  776. if(block_unknown_bytes_mask == 0) {
  777. mf_classic_set_block_read(data, block_num, &block_tmp);
  778. }
  779. }
  780. }
  781. static bool nfc_device_load_mifare_classic_data(FlipperFormat* file, NfcDevice* dev) {
  782. bool parsed = false;
  783. MfClassicData* data = &dev->dev_data.mf_classic_data;
  784. FuriString* temp_str;
  785. uint32_t data_format_version = 0;
  786. temp_str = furi_string_alloc();
  787. uint16_t data_blocks = 0;
  788. memset(data, 0, sizeof(MfClassicData));
  789. do {
  790. // Read Mifare Classic type
  791. if(!flipper_format_read_string(file, "Mifare Classic type", temp_str)) break;
  792. if(!furi_string_cmp(temp_str, "1K")) {
  793. data->type = MfClassicType1k;
  794. data_blocks = 64;
  795. } else if(!furi_string_cmp(temp_str, "4K")) {
  796. data->type = MfClassicType4k;
  797. data_blocks = 256;
  798. } else {
  799. break;
  800. }
  801. bool old_format = false;
  802. // Read Mifare Classic format version
  803. if(!flipper_format_read_uint32(file, "Data format version", &data_format_version, 1)) {
  804. // Load unread sectors with zero keys access for backward compatibility
  805. if(!flipper_format_rewind(file)) break;
  806. old_format = true;
  807. } else {
  808. if(data_format_version < nfc_mifare_classic_data_format_version) {
  809. old_format = true;
  810. }
  811. }
  812. // Read Mifare Classic blocks
  813. bool block_read = true;
  814. FuriString* block_str;
  815. block_str = furi_string_alloc();
  816. for(size_t i = 0; i < data_blocks; i++) {
  817. furi_string_printf(temp_str, "Block %d", i);
  818. if(!flipper_format_read_string(file, furi_string_get_cstr(temp_str), block_str)) {
  819. block_read = false;
  820. break;
  821. }
  822. nfc_device_load_mifare_classic_block(block_str, data, i);
  823. }
  824. furi_string_free(block_str);
  825. if(!block_read) break;
  826. // Set keys and blocks as unknown for backward compatibility
  827. if(old_format) {
  828. data->key_a_mask = 0ULL;
  829. data->key_b_mask = 0ULL;
  830. memset(data->block_read_mask, 0, sizeof(data->block_read_mask));
  831. }
  832. parsed = true;
  833. } while(false);
  834. furi_string_free(temp_str);
  835. return parsed;
  836. }
  837. static void nfc_device_get_key_cache_file_path(NfcDevice* dev, FuriString* file_path) {
  838. uint8_t* uid = dev->dev_data.nfc_data.uid;
  839. uint8_t uid_len = dev->dev_data.nfc_data.uid_len;
  840. furi_string_set(file_path, NFC_DEVICE_KEYS_FOLDER "/");
  841. for(size_t i = 0; i < uid_len; i++) {
  842. furi_string_cat_printf(file_path, "%02X", uid[i]);
  843. }
  844. furi_string_cat_printf(file_path, NFC_DEVICE_KEYS_EXTENSION);
  845. }
  846. static bool nfc_device_save_mifare_classic_keys(NfcDevice* dev) {
  847. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  848. MfClassicData* data = &dev->dev_data.mf_classic_data;
  849. FuriString* temp_str;
  850. temp_str = furi_string_alloc();
  851. nfc_device_get_key_cache_file_path(dev, temp_str);
  852. bool save_success = false;
  853. do {
  854. if(!storage_simply_mkdir(dev->storage, NFC_DEVICE_KEYS_FOLDER)) break;
  855. if(!storage_simply_remove(dev->storage, furi_string_get_cstr(temp_str))) break;
  856. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  857. if(!flipper_format_write_header_cstr(file, nfc_keys_file_header, nfc_keys_file_version))
  858. break;
  859. if(data->type == MfClassicType1k) {
  860. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "1K")) break;
  861. } else if(data->type == MfClassicType4k) {
  862. if(!flipper_format_write_string_cstr(file, "Mifare Classic type", "4K")) break;
  863. }
  864. if(!flipper_format_write_hex_uint64(file, "Key A map", &data->key_a_mask, 1)) break;
  865. if(!flipper_format_write_hex_uint64(file, "Key B map", &data->key_b_mask, 1)) break;
  866. uint8_t sector_num = mf_classic_get_total_sectors_num(data->type);
  867. bool key_save_success = true;
  868. for(size_t i = 0; (i < sector_num) && (key_save_success); i++) {
  869. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, i);
  870. if(FURI_BIT(data->key_a_mask, i)) {
  871. furi_string_printf(temp_str, "Key A sector %d", i);
  872. key_save_success = flipper_format_write_hex(
  873. file, furi_string_get_cstr(temp_str), sec_tr->key_a, 6);
  874. }
  875. if(!key_save_success) break;
  876. if(FURI_BIT(data->key_b_mask, i)) {
  877. furi_string_printf(temp_str, "Key B sector %d", i);
  878. key_save_success = flipper_format_write_hex(
  879. file, furi_string_get_cstr(temp_str), sec_tr->key_b, 6);
  880. }
  881. }
  882. save_success = key_save_success;
  883. } while(false);
  884. flipper_format_free(file);
  885. furi_string_free(temp_str);
  886. return save_success;
  887. }
  888. bool nfc_device_load_key_cache(NfcDevice* dev) {
  889. furi_assert(dev);
  890. FuriString* temp_str;
  891. temp_str = furi_string_alloc();
  892. MfClassicData* data = &dev->dev_data.mf_classic_data;
  893. nfc_device_get_key_cache_file_path(dev, temp_str);
  894. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  895. bool load_success = false;
  896. do {
  897. if(storage_common_stat(dev->storage, furi_string_get_cstr(temp_str), NULL) != FSE_OK)
  898. break;
  899. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(temp_str))) break;
  900. uint32_t version = 0;
  901. if(!flipper_format_read_header(file, temp_str, &version)) break;
  902. if(furi_string_cmp_str(temp_str, nfc_keys_file_header)) break;
  903. if(version != nfc_keys_file_version) break;
  904. if(!flipper_format_read_string(file, "Mifare Classic type", temp_str)) break;
  905. if(!furi_string_cmp(temp_str, "1K")) {
  906. data->type = MfClassicType1k;
  907. } else if(!furi_string_cmp(temp_str, "4K")) {
  908. data->type = MfClassicType4k;
  909. } else {
  910. break;
  911. }
  912. if(!flipper_format_read_hex_uint64(file, "Key A map", &data->key_a_mask, 1)) break;
  913. if(!flipper_format_read_hex_uint64(file, "Key B map", &data->key_b_mask, 1)) break;
  914. uint8_t sectors = mf_classic_get_total_sectors_num(data->type);
  915. bool key_read_success = true;
  916. for(size_t i = 0; (i < sectors) && (key_read_success); i++) {
  917. MfClassicSectorTrailer* sec_tr = mf_classic_get_sector_trailer_by_sector(data, i);
  918. if(FURI_BIT(data->key_a_mask, i)) {
  919. furi_string_printf(temp_str, "Key A sector %d", i);
  920. key_read_success = flipper_format_read_hex(
  921. file, furi_string_get_cstr(temp_str), sec_tr->key_a, 6);
  922. }
  923. if(!key_read_success) break;
  924. if(FURI_BIT(data->key_b_mask, i)) {
  925. furi_string_printf(temp_str, "Key B sector %d", i);
  926. key_read_success = flipper_format_read_hex(
  927. file, furi_string_get_cstr(temp_str), sec_tr->key_b, 6);
  928. }
  929. }
  930. load_success = key_read_success;
  931. } while(false);
  932. furi_string_free(temp_str);
  933. flipper_format_free(file);
  934. return load_success;
  935. }
  936. void nfc_device_set_name(NfcDevice* dev, const char* name) {
  937. furi_assert(dev);
  938. strlcpy(dev->dev_name, name, NFC_DEV_NAME_MAX_LEN);
  939. }
  940. static void nfc_device_get_path_without_ext(FuriString* orig_path, FuriString* shadow_path) {
  941. // TODO: this won't work if there is ".nfc" anywhere in the path other than
  942. // at the end
  943. size_t ext_start = furi_string_search(orig_path, NFC_APP_EXTENSION);
  944. furi_string_set_n(shadow_path, orig_path, 0, ext_start);
  945. }
  946. static void nfc_device_get_shadow_path(FuriString* orig_path, FuriString* shadow_path) {
  947. nfc_device_get_path_without_ext(orig_path, shadow_path);
  948. furi_string_cat_printf(shadow_path, "%s", NFC_APP_SHADOW_EXTENSION);
  949. }
  950. static void nfc_device_get_folder_from_path(FuriString* path, FuriString* folder) {
  951. size_t last_slash = furi_string_search_rchar(path, '/');
  952. if(last_slash == FURI_STRING_FAILURE) {
  953. // No slashes in the path, treat the whole path as a folder
  954. furi_string_set(folder, path);
  955. } else {
  956. furi_string_set_n(folder, path, 0, last_slash);
  957. }
  958. }
  959. bool nfc_device_save(NfcDevice* dev, const char* dev_name) {
  960. furi_assert(dev);
  961. bool saved = false;
  962. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  963. FuriHalNfcDevData* data = &dev->dev_data.nfc_data;
  964. FuriString* temp_str;
  965. temp_str = furi_string_alloc();
  966. do {
  967. // Create directory if necessary
  968. FuriString* folder = furi_string_alloc();
  969. // Get folder from filename (filename is in the form of "folder/filename.nfc", so the folder is "folder/")
  970. furi_string_set(temp_str, dev_name);
  971. // Get folder from filename
  972. nfc_device_get_folder_from_path(temp_str, folder);
  973. FURI_LOG_I("Nfc", "Saving to folder %s", furi_string_get_cstr(folder));
  974. if(!storage_simply_mkdir(dev->storage, furi_string_get_cstr(folder))) {
  975. FURI_LOG_E("Nfc", "Failed to create folder %s", furi_string_get_cstr(folder));
  976. break;
  977. }
  978. furi_string_free(folder);
  979. // First remove nfc device file if it was saved
  980. // Open file
  981. if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
  982. // Write header
  983. if(!flipper_format_write_header_cstr(file, nfc_file_header, nfc_file_version)) break;
  984. // Write nfc device type
  985. if(!flipper_format_write_comment_cstr(
  986. file, "Nfc device type can be UID, Mifare Ultralight, Mifare Classic"))
  987. break;
  988. nfc_device_prepare_format_string(dev, temp_str);
  989. if(!flipper_format_write_string(file, "Device type", temp_str)) break;
  990. // Write UID, ATQA, SAK
  991. if(!flipper_format_write_comment_cstr(file, "UID, ATQA and SAK are common for all formats"))
  992. break;
  993. if(!flipper_format_write_hex(file, "UID", data->uid, data->uid_len)) break;
  994. // Save ATQA in MSB order for correct companion apps display
  995. uint8_t atqa[2] = {data->atqa[1], data->atqa[0]};
  996. if(!flipper_format_write_hex(file, "ATQA", atqa, 2)) break;
  997. if(!flipper_format_write_hex(file, "SAK", &data->sak, 1)) break;
  998. // Save more data if necessary
  999. if(dev->format == NfcDeviceSaveFormatMifareUl) {
  1000. if(!nfc_device_save_mifare_ul_data(file, dev)) break;
  1001. } else if(dev->format == NfcDeviceSaveFormatMifareDesfire) {
  1002. if(!nfc_device_save_mifare_df_data(file, dev)) break;
  1003. } else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
  1004. // Save data
  1005. if(!nfc_device_save_mifare_classic_data(file, dev)) break;
  1006. // Save keys cache
  1007. if(!nfc_device_save_mifare_classic_keys(dev)) break;
  1008. }
  1009. saved = true;
  1010. } while(0);
  1011. if(!saved) { //-V547
  1012. dialog_message_show_storage_error(dev->dialogs, "Can not save\nkey file");
  1013. }
  1014. furi_string_free(temp_str);
  1015. flipper_format_free(file);
  1016. return saved;
  1017. }
  1018. bool nfc_device_save_shadow(NfcDevice* dev, const char* path) {
  1019. dev->shadow_file_exist = true;
  1020. // Replace extension from .nfc to .shd if necessary
  1021. FuriString* orig_path = furi_string_alloc();
  1022. furi_string_set_str(orig_path, path);
  1023. FuriString* shadow_path = furi_string_alloc();
  1024. nfc_device_get_shadow_path(orig_path, shadow_path);
  1025. bool file_saved = nfc_device_save(dev, furi_string_get_cstr(shadow_path));
  1026. furi_string_free(orig_path);
  1027. furi_string_free(shadow_path);
  1028. return file_saved;
  1029. }
  1030. static bool nfc_device_load_data(NfcDevice* dev, FuriString* path, bool show_dialog) {
  1031. bool parsed = false;
  1032. FlipperFormat* file = flipper_format_file_alloc(dev->storage);
  1033. FuriHalNfcDevData* data = &dev->dev_data.nfc_data;
  1034. uint32_t data_cnt = 0;
  1035. FuriString* temp_str;
  1036. temp_str = furi_string_alloc();
  1037. bool deprecated_version = false;
  1038. // Version 2 of file format had ATQA bytes swapped
  1039. uint32_t version_with_lsb_atqa = 2;
  1040. if(dev->loading_cb) {
  1041. dev->loading_cb(dev->loading_cb_ctx, true);
  1042. }
  1043. do {
  1044. // Check existence of shadow file
  1045. nfc_device_get_shadow_path(path, temp_str);
  1046. dev->shadow_file_exist =
  1047. storage_common_stat(dev->storage, furi_string_get_cstr(temp_str), NULL) == FSE_OK;
  1048. // Open shadow file if it exists. If not - open original
  1049. if(dev->shadow_file_exist) {
  1050. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(temp_str))) break;
  1051. } else {
  1052. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(path))) break;
  1053. }
  1054. // Read and verify file header
  1055. uint32_t version = 0;
  1056. if(!flipper_format_read_header(file, temp_str, &version)) break;
  1057. if(furi_string_cmp_str(temp_str, nfc_file_header)) break;
  1058. if(version != nfc_file_version) {
  1059. if(version < version_with_lsb_atqa) {
  1060. deprecated_version = true;
  1061. break;
  1062. }
  1063. }
  1064. // Read Nfc device type
  1065. if(!flipper_format_read_string(file, "Device type", temp_str)) break;
  1066. if(!nfc_device_parse_format_string(dev, temp_str)) break;
  1067. // Read and parse UID, ATQA and SAK
  1068. if(!flipper_format_get_value_count(file, "UID", &data_cnt)) break;
  1069. if(!(data_cnt == 4 || data_cnt == 7)) break;
  1070. data->uid_len = data_cnt;
  1071. if(!flipper_format_read_hex(file, "UID", data->uid, data->uid_len)) break;
  1072. if(version == version_with_lsb_atqa) {
  1073. if(!flipper_format_read_hex(file, "ATQA", data->atqa, 2)) break;
  1074. } else {
  1075. uint8_t atqa[2] = {};
  1076. if(!flipper_format_read_hex(file, "ATQA", atqa, 2)) break;
  1077. data->atqa[0] = atqa[1];
  1078. data->atqa[1] = atqa[0];
  1079. }
  1080. if(!flipper_format_read_hex(file, "SAK", &data->sak, 1)) break;
  1081. // Load CUID
  1082. uint8_t* cuid_start = data->uid;
  1083. if(data->uid_len == 7) {
  1084. cuid_start = &data->uid[3];
  1085. }
  1086. data->cuid = (cuid_start[0] << 24) | (cuid_start[1] << 16) | (cuid_start[2] << 8) |
  1087. (cuid_start[3]);
  1088. // Parse other data
  1089. if(dev->format == NfcDeviceSaveFormatMifareUl) {
  1090. if(!nfc_device_load_mifare_ul_data(file, dev)) break;
  1091. } else if(dev->format == NfcDeviceSaveFormatMifareClassic) {
  1092. if(!nfc_device_load_mifare_classic_data(file, dev)) break;
  1093. } else if(dev->format == NfcDeviceSaveFormatMifareDesfire) {
  1094. if(!nfc_device_load_mifare_df_data(file, dev)) break;
  1095. } else if(dev->format == NfcDeviceSaveFormatBankCard) {
  1096. if(!nfc_device_load_bank_card_data(file, dev)) break;
  1097. }
  1098. parsed = true;
  1099. } while(false);
  1100. if(dev->loading_cb) {
  1101. dev->loading_cb(dev->loading_cb_ctx, false);
  1102. }
  1103. if((!parsed) && (show_dialog)) {
  1104. if(deprecated_version) {
  1105. dialog_message_show_storage_error(dev->dialogs, "File format deprecated");
  1106. } else {
  1107. dialog_message_show_storage_error(dev->dialogs, "Can not parse\nfile");
  1108. }
  1109. }
  1110. furi_string_free(temp_str);
  1111. flipper_format_free(file);
  1112. return parsed;
  1113. }
  1114. bool nfc_device_load(NfcDevice* dev, const char* file_path, bool show_dialog) {
  1115. furi_assert(dev);
  1116. furi_assert(file_path);
  1117. // Load device data
  1118. furi_string_set(dev->load_path, file_path);
  1119. bool dev_load = nfc_device_load_data(dev, dev->load_path, show_dialog);
  1120. if(dev_load) {
  1121. // Set device name
  1122. FuriString* filename;
  1123. filename = furi_string_alloc();
  1124. path_extract_filename_no_ext(file_path, filename);
  1125. nfc_device_set_name(dev, furi_string_get_cstr(filename));
  1126. furi_string_free(filename);
  1127. }
  1128. return dev_load;
  1129. }
  1130. bool nfc_file_select(NfcDevice* dev) {
  1131. furi_assert(dev);
  1132. const char* folder = furi_string_get_cstr(dev->folder);
  1133. // Input events and views are managed by file_browser
  1134. const DialogsFileBrowserOptions browser_options = {
  1135. .extension = NFC_APP_EXTENSION,
  1136. .skip_assets = true,
  1137. .hide_dot_files = true,
  1138. .icon = &I_Nfc_10px,
  1139. .hide_ext = true,
  1140. .item_loader_callback = NULL,
  1141. .item_loader_context = NULL,
  1142. .base_path = folder,
  1143. };
  1144. bool res =
  1145. dialog_file_browser_show(dev->dialogs, dev->load_path, dev->load_path, &browser_options);
  1146. if(res) {
  1147. FuriString* filename;
  1148. filename = furi_string_alloc();
  1149. path_extract_filename(dev->load_path, filename, true);
  1150. strncpy(dev->dev_name, furi_string_get_cstr(filename), NFC_DEV_NAME_MAX_LEN);
  1151. res = nfc_device_load_data(dev, dev->load_path, true);
  1152. if(res) {
  1153. nfc_device_set_name(dev, dev->dev_name);
  1154. }
  1155. furi_string_free(filename);
  1156. }
  1157. return res;
  1158. }
  1159. void nfc_device_data_clear(NfcDeviceData* dev_data) {
  1160. if(dev_data->protocol == NfcDeviceProtocolMifareDesfire) {
  1161. mf_df_clear(&dev_data->mf_df_data);
  1162. } else if(dev_data->protocol == NfcDeviceProtocolMifareClassic) {
  1163. memset(&dev_data->mf_classic_data, 0, sizeof(MfClassicData));
  1164. } else if(dev_data->protocol == NfcDeviceProtocolMifareUl) {
  1165. mf_ul_reset(&dev_data->mf_ul_data);
  1166. } else if(dev_data->protocol == NfcDeviceProtocolEMV) {
  1167. memset(&dev_data->emv_data, 0, sizeof(EmvData));
  1168. }
  1169. memset(&dev_data->nfc_data, 0, sizeof(FuriHalNfcDevData));
  1170. dev_data->protocol = NfcDeviceProtocolUnknown;
  1171. furi_string_reset(dev_data->parsed_data);
  1172. }
  1173. void nfc_device_clear(NfcDevice* dev) {
  1174. furi_assert(dev);
  1175. nfc_device_set_name(dev, "");
  1176. nfc_device_data_clear(&dev->dev_data);
  1177. dev->format = NfcDeviceSaveFormatUid;
  1178. furi_string_reset(dev->load_path);
  1179. }
  1180. bool nfc_device_delete(NfcDevice* dev, bool use_load_path) {
  1181. furi_assert(dev);
  1182. bool deleted = false;
  1183. FuriString* file_path;
  1184. file_path = furi_string_alloc();
  1185. do {
  1186. // Delete original file
  1187. if(use_load_path && !furi_string_empty(dev->load_path)) {
  1188. furi_string_set(file_path, dev->load_path);
  1189. } else {
  1190. furi_string_printf(
  1191. file_path,
  1192. "%s/%s%s",
  1193. furi_string_get_cstr(dev->folder),
  1194. dev->dev_name,
  1195. NFC_APP_EXTENSION);
  1196. }
  1197. if(!storage_simply_remove(dev->storage, furi_string_get_cstr(file_path))) break;
  1198. // Delete shadow file if it exists
  1199. if(dev->shadow_file_exist) {
  1200. if(use_load_path && !furi_string_empty(dev->load_path)) {
  1201. nfc_device_get_shadow_path(dev->load_path, file_path);
  1202. } else {
  1203. furi_string_printf(
  1204. file_path,
  1205. "%s/%s%s",
  1206. furi_string_get_cstr(dev->folder),
  1207. dev->dev_name,
  1208. NFC_APP_SHADOW_EXTENSION);
  1209. }
  1210. if(!storage_simply_remove(dev->storage, furi_string_get_cstr(file_path))) break;
  1211. }
  1212. deleted = true;
  1213. } while(0);
  1214. if(!deleted) {
  1215. dialog_message_show_storage_error(dev->dialogs, "Can not remove file");
  1216. }
  1217. furi_string_free(file_path);
  1218. return deleted;
  1219. }
  1220. bool nfc_device_restore(NfcDevice* dev, bool use_load_path) {
  1221. furi_assert(dev);
  1222. furi_assert(dev->shadow_file_exist);
  1223. bool restored = false;
  1224. FuriString* path;
  1225. path = furi_string_alloc();
  1226. do {
  1227. if(use_load_path && !furi_string_empty(dev->load_path)) {
  1228. nfc_device_get_shadow_path(dev->load_path, path);
  1229. } else {
  1230. furi_string_printf(
  1231. path,
  1232. "%s/%s%s",
  1233. furi_string_get_cstr(dev->folder),
  1234. dev->dev_name,
  1235. NFC_APP_SHADOW_EXTENSION);
  1236. }
  1237. if(!storage_simply_remove(dev->storage, furi_string_get_cstr(path))) break;
  1238. dev->shadow_file_exist = false;
  1239. if(use_load_path && !furi_string_empty(dev->load_path)) {
  1240. furi_string_set(path, dev->load_path);
  1241. } else {
  1242. furi_string_printf(
  1243. path,
  1244. "%s/%s%s",
  1245. furi_string_get_cstr(dev->folder),
  1246. dev->dev_name,
  1247. NFC_APP_EXTENSION);
  1248. }
  1249. if(!nfc_device_load_data(dev, path, true)) break;
  1250. restored = true;
  1251. } while(0);
  1252. furi_string_free(path);
  1253. return restored;
  1254. }
  1255. void nfc_device_set_loading_callback(NfcDevice* dev, NfcLoadingCallback callback, void* context) {
  1256. furi_assert(dev);
  1257. dev->loading_cb = callback;
  1258. dev->loading_cb_ctx = context;
  1259. }