You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FreeFareTag.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //
  2. // Created by robin on 7/22/16.
  3. //
  4. #include "libnfc_cpptools/FreeFareTag.h"
  5. #include "libnfc_cpptools/StringUtils.h"
  6. #include "FreeFareTagInternal.h"
  7. namespace LibNfc
  8. {
  9. namespace FreeFare
  10. {
  11. std::string FreeFareTag::getTagTypeString(FreeFareTag::FreeFareTagType type)
  12. {
  13. if (type == FreeFareTagType::FELICA)
  14. {
  15. return "FELICA";
  16. }
  17. if (type == FreeFareTagType::MIFARE_MINI)
  18. {
  19. return "MIFARE_MINI";
  20. }
  21. if (type == FreeFareTagType::MIFARE_CLASSIC_1K)
  22. {
  23. return "MIFARE_CLASSIC_1K";
  24. }
  25. if (type == FreeFareTagType::MIFARE_CLASSIC_4K)
  26. {
  27. return "MIFARE_CLASSIC_4K";
  28. }
  29. if (type == FreeFareTagType::MIFARE_DESFIRE)
  30. {
  31. return "MIFARE_DESFIRE";
  32. }
  33. if (type == FreeFareTagType::MIFARE_ULTRALIGHT)
  34. {
  35. return "MIFARE_ULTRALIGHT";
  36. }
  37. if (type == FreeFareTagType::MIFARE_ULTRALIGHT_C)
  38. {
  39. return "MIFARE_ULTRALIGHT_C";
  40. }
  41. return "UNKNOWN";
  42. }
  43. FreeFareTag::FreeFareTag(std::shared_ptr<FreeFareTagInternal> tag)
  44. : _tag(tag)
  45. {
  46. }
  47. LibNfc::Utils::ResultBool FreeFareTag::authenticate(int sector, std::string key, int keyType)
  48. {
  49. return _tag->authenticate(sector, key, keyType);
  50. }
  51. LibNfc::Utils::Result<MappedKeys> FreeFareTag::mapKeys(std::vector<std::string> keys, std::function<void(int, int)> cb)
  52. {
  53. MappedKeys mappedKeys;
  54. int done = 0;
  55. int total = 16 * keys.size();
  56. for (int i = 0; i < 16; ++i) {
  57. std::pair<std::string, std::string> blockKeys;
  58. for (int k = 0; k < keys.size(); ++k) {
  59. auto key = keys[k];
  60. if (authenticate(i, key, MFC_KEY_A)) {
  61. blockKeys.first = key;
  62. }
  63. if (authenticate(i, key, MFC_KEY_B)) {
  64. blockKeys.second = key;
  65. }
  66. if (cb != 0) {
  67. cb(++done, total);
  68. }
  69. if (!blockKeys.first.empty() && !blockKeys.second.empty()) {
  70. break;
  71. }
  72. }
  73. mappedKeys.push_back(blockKeys);
  74. }
  75. if (cb != 0 && done < total) {
  76. cb(total, total);
  77. }
  78. return LibNfc::Utils::Result<MappedKeys>::ok(mappedKeys);
  79. }
  80. LibNfc::Utils::ResultString FreeFareTag::readBlock(int sector, int block, std::string key, int keyType)
  81. {
  82. return _tag->readBlock(sector, block, key, keyType);
  83. }
  84. LibNfc::Utils::Result<FreeFareSector> FreeFareTag::readSector(int sector, std::string key, int keyType)
  85. {
  86. std::string res;
  87. int lastBlock = _tag->getSectorBlockCount(sector);
  88. for (int i = 0; i < lastBlock; ++i) {
  89. auto data = readBlock(sector, i, key, keyType);
  90. if (data) {
  91. res += data.getData();
  92. }
  93. else {
  94. return LibNfc::Utils::Result<FreeFareSector>::error(data);
  95. }
  96. }
  97. return LibNfc::Utils::Result<FreeFareSector>::ok(FreeFareSector(res));
  98. }
  99. LibNfc::Utils::Result<std::vector<FreeFareSector>> FreeFareTag::read(MappedKeys keys, std::function<void(int, int)> cb)
  100. {
  101. if (keys.size() != 16) {
  102. return LibNfc::Utils::Result<std::vector<FreeFareSector>>::error("Must have 16 sectors keys");
  103. }
  104. int done = 0;
  105. int total = 4 * keys.size();
  106. std::vector<FreeFareSector> sectors;
  107. for (int s = 0; s < keys.size(); ++s) {
  108. auto sectorKey = keys[s];
  109. FreeFareSector sector;
  110. bool keyA = false;
  111. bool keyB = false;
  112. for (int b = 0; b < 3; ++b) {
  113. std::string data = "";
  114. if (!sectorKey.first.empty()) {
  115. auto blockResult = readBlock(s, b, sectorKey.first, MFC_KEY_A);
  116. if (blockResult) {
  117. data = blockResult.getData();
  118. keyA = true;
  119. }
  120. }
  121. if (!sectorKey.second.empty()) {
  122. auto blockResult = readBlock(s, b, sectorKey.second, MFC_KEY_B);
  123. if (blockResult) {
  124. if (data.empty()) {
  125. data = blockResult.getData();
  126. }
  127. keyB = true;
  128. }
  129. }
  130. sector.setBlock(b, data);
  131. if (cb != 0) {
  132. cb(++done, total);
  133. }
  134. }
  135. int b = 3;
  136. std::string dataA = "";
  137. std::string dataB = "";
  138. if (!sectorKey.first.empty()) {
  139. auto blockResult = readBlock(s, b, sectorKey.first, MFC_KEY_A);
  140. if (blockResult) {
  141. dataA = blockResult.getData();
  142. keyA = true;
  143. }
  144. }
  145. if (!sectorKey.second.empty()) {
  146. auto blockResult = readBlock(s, b, sectorKey.second, MFC_KEY_B);
  147. if (blockResult) {
  148. dataB = blockResult.getData();
  149. keyB = true;
  150. }
  151. }
  152. if (cb != 0) {
  153. cb(++done, total);
  154. }
  155. sector.setBlock(b, dataA);
  156. FreeFareAccessBits accessBitsDboA = sector.getAccessBitsDbo();
  157. sector.setBlock(b, dataB);
  158. FreeFareAccessBits accessBitsDboB = sector.getAccessBitsDbo();
  159. sector.setKeyA(keyA ? sectorKey.first : "");
  160. sector.setKeyB(keyB ? sectorKey.second : "");
  161. std::string accessBits;
  162. if (keyA && accessBitsDboA.canKeyAReadAccessBitsTrailer()) {
  163. accessBits = accessBitsDboA.getBits();
  164. }
  165. else if (keyB && accessBitsDboB.canKeyBReadAccessBitsTrailer()) {
  166. accessBits = accessBitsDboB.getBits();
  167. }
  168. sector.setAccessBits(accessBits);
  169. sectors.push_back(sector);
  170. }
  171. if (cb != 0 && done < total) {
  172. cb(total, total);
  173. }
  174. return LibNfc::Utils::Result<std::vector<FreeFareSector>>::ok(sectors);
  175. }
  176. LibNfc::Utils::Result<std::vector<FreeFareSector>> FreeFareTag::read(std::vector<std::string> keys, std::function<void(int, int)> mapCb,
  177. std::function<void(int, int)> readCb)
  178. {
  179. auto mappedKeysResult = mapKeys(keys, mapCb);
  180. if (!mappedKeysResult) {
  181. return LibNfc::Utils::Result<std::vector<FreeFareSector>>::error(mappedKeysResult);
  182. }
  183. return read(mappedKeysResult.getData(), readCb);
  184. }
  185. LibNfc::Utils::ResultBool FreeFareTag::writeBlock(int sector, int block, std::string key, int keyType, const std::string &data)
  186. {
  187. return _tag->writeBlock(sector, block, key, keyType, LibNfc::Utils::StringUtils::ensureSize(data, 16));
  188. }
  189. LibNfc::Utils::ResultBool FreeFareTag::writeSector(int sector, std::string key, int keyType, const std::string &data)
  190. {
  191. std::string d = LibNfc::Utils::StringUtils::ensureSize(data, 64);
  192. std::string errors;
  193. for (int b = 0; b < 4; ++b) {
  194. auto result = writeBlock(sector, b, key, keyType, d.substr(b * 16, 16));
  195. if (!result) {
  196. errors += std::string(errors.empty() ? "" : "\n") + "Failed to write sector " + std::to_string(sector) +
  197. " block " + std::to_string(b) + " with key " + (keyType == MFC_KEY_A ? "A" : "B") + ": " + result.getError();
  198. }
  199. }
  200. if (errors.empty()) {
  201. return LibNfc::Utils::ResultBool::ok(true);
  202. }
  203. return LibNfc::Utils::ResultBool::error(errors);
  204. }
  205. LibNfc::Utils::ResultBool FreeFareTag::write(MappedKeys keys, const std::string &data, bool writeSector0, std::function<void(int, int)> cb)
  206. {
  207. if (keys.size() != 16) {
  208. return LibNfc::Utils::ResultBool::error("Must have 16 sectors keys");
  209. }
  210. std::string d = LibNfc::Utils::StringUtils::ensureSize(data, 1024);
  211. std::string errors;
  212. int done = 0;
  213. int total = 4 * keys.size();
  214. for (int s = 0; s < keys.size(); ++s) {
  215. auto sectorKey = keys[s];
  216. for (int b = 0; b < 4; ++b) {
  217. if (s == 0 && b == 0 && !writeSector0) {
  218. continue;
  219. }
  220. std::string blockData = d.substr((s * 64) + (b * 16), 16);
  221. if (cb != 0 && done < total) {
  222. bool keyA = false;
  223. bool keyB = false;
  224. std::string sectorErrors;
  225. if (!sectorKey.first.empty()) {
  226. auto resultA = writeBlock(s, b, sectorKey.first, MFC_KEY_A, blockData);
  227. if (resultA) {
  228. keyA = true;
  229. }
  230. else {
  231. sectorErrors = "Failed to write sector " + std::to_string(s) +
  232. " block " + std::to_string(b) + " with key A: " + resultA.getError();
  233. }
  234. }
  235. if (!keyA && !sectorKey.second.empty()) {
  236. auto resultB = writeBlock(s, b, sectorKey.first, MFC_KEY_A, blockData);
  237. if (resultB) {
  238. keyB = true;
  239. }
  240. else {
  241. sectorErrors = std::string(sectorErrors.empty() ? "" : "\n") + "Failed to write sector " + std::to_string(s) +
  242. " block " + std::to_string(b) + " with key B: " + resultB.getError();
  243. }
  244. }
  245. if (!keyA && !keyB) {
  246. errors += std::string(errors.empty() ? "" : "\n") + sectorErrors;
  247. }
  248. cb(++done, total);
  249. }
  250. }
  251. }
  252. if (cb != 0 && done < total) {
  253. cb(total, total);
  254. }
  255. if (errors.empty()) {
  256. return LibNfc::Utils::ResultBool::ok(true);
  257. }
  258. return LibNfc::Utils::ResultBool::error(errors);
  259. }
  260. LibNfc::Utils::ResultBool FreeFareTag::write(std::vector<std::string> keys, const std::string &data, bool writeSector0,
  261. std::function<void(int, int)> mapCb, std::function<void(int, int)> writeCb)
  262. {
  263. auto mappedKeysResult = mapKeys(keys, mapCb);
  264. if (!mappedKeysResult) {
  265. return LibNfc::Utils::ResultBool::error(mappedKeysResult);
  266. }
  267. return write(mappedKeysResult.getData(), data, writeSector0, writeCb);
  268. }
  269. const std::string &FreeFareTag::getUid() const
  270. {
  271. return _tag->getUid();
  272. }
  273. FreeFareTag::FreeFareTagType FreeFareTag::getType() const
  274. {
  275. auto type = _tag->getType();
  276. if (type == freefare_tag_type::FELICA)
  277. {
  278. return FreeFareTagType::FELICA;
  279. }
  280. if (type == freefare_tag_type::MIFARE_MINI)
  281. {
  282. return FreeFareTagType::MIFARE_MINI;
  283. }
  284. if (type == freefare_tag_type::MIFARE_CLASSIC_1K)
  285. {
  286. return FreeFareTagType::MIFARE_CLASSIC_1K;
  287. }
  288. if (type == freefare_tag_type::MIFARE_CLASSIC_4K)
  289. {
  290. return FreeFareTagType::MIFARE_CLASSIC_4K;
  291. }
  292. if (type == freefare_tag_type::MIFARE_DESFIRE)
  293. {
  294. return FreeFareTagType::MIFARE_DESFIRE;
  295. }
  296. if (type == freefare_tag_type::MIFARE_ULTRALIGHT)
  297. {
  298. return FreeFareTagType::MIFARE_ULTRALIGHT;
  299. }
  300. if (type == freefare_tag_type::MIFARE_ULTRALIGHT_C)
  301. {
  302. return FreeFareTagType::MIFARE_ULTRALIGHT_C;
  303. }
  304. return FreeFareTagType::UNKNOWN;
  305. }
  306. std::shared_ptr<FreeFareTagInternal> FreeFareTag::getTag() const
  307. {
  308. return _tag;
  309. }
  310. }; // FreeFare
  311. }; // LibNfc