nfc_worker.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. #include "nfc_worker_i.h"
  2. #include <furi-hal.h>
  3. #include "nfc_protocols/emv_decoder.h"
  4. #include "nfc_protocols/mifare_ultralight.h"
  5. #define TAG "NfcWorker"
  6. /***************************** NFC Worker API *******************************/
  7. NfcWorker* nfc_worker_alloc() {
  8. NfcWorker* nfc_worker = furi_alloc(sizeof(NfcWorker));
  9. // Worker thread attributes
  10. nfc_worker->thread_attr.name = "NfcWorker";
  11. nfc_worker->thread_attr.stack_size = 8192;
  12. nfc_worker->callback = NULL;
  13. nfc_worker->context = NULL;
  14. // Initialize rfal
  15. if(!furi_hal_nfc_is_busy()) {
  16. nfc_worker_change_state(nfc_worker, NfcWorkerStateReady);
  17. } else {
  18. nfc_worker_change_state(nfc_worker, NfcWorkerStateBroken);
  19. }
  20. return nfc_worker;
  21. }
  22. void nfc_worker_free(NfcWorker* nfc_worker) {
  23. furi_assert(nfc_worker);
  24. free(nfc_worker);
  25. }
  26. NfcWorkerState nfc_worker_get_state(NfcWorker* nfc_worker) {
  27. return nfc_worker->state;
  28. }
  29. void nfc_worker_start(
  30. NfcWorker* nfc_worker,
  31. NfcWorkerState state,
  32. NfcDeviceData* dev_data,
  33. NfcWorkerCallback callback,
  34. void* context) {
  35. furi_assert(nfc_worker);
  36. furi_assert(dev_data);
  37. while(nfc_worker->state != NfcWorkerStateReady) {
  38. osDelay(10);
  39. }
  40. nfc_worker->callback = callback;
  41. nfc_worker->context = context;
  42. nfc_worker->dev_data = dev_data;
  43. nfc_worker_change_state(nfc_worker, state);
  44. nfc_worker->thread = osThreadNew(nfc_worker_task, nfc_worker, &nfc_worker->thread_attr);
  45. }
  46. void nfc_worker_stop(NfcWorker* nfc_worker) {
  47. furi_assert(nfc_worker);
  48. if(nfc_worker->state == NfcWorkerStateBroken || nfc_worker->state == NfcWorkerStateReady) {
  49. return;
  50. }
  51. nfc_worker_change_state(nfc_worker, NfcWorkerStateStop);
  52. }
  53. void nfc_worker_change_state(NfcWorker* nfc_worker, NfcWorkerState state) {
  54. nfc_worker->state = state;
  55. }
  56. /***************************** NFC Worker Thread *******************************/
  57. void nfc_worker_task(void* context) {
  58. NfcWorker* nfc_worker = context;
  59. furi_hal_power_insomnia_enter();
  60. furi_hal_nfc_exit_sleep();
  61. if(nfc_worker->state == NfcWorkerStateDetect) {
  62. nfc_worker_detect(nfc_worker);
  63. } else if(nfc_worker->state == NfcWorkerStateEmulate) {
  64. nfc_worker_emulate(nfc_worker);
  65. } else if(nfc_worker->state == NfcWorkerStateReadEMVApp) {
  66. nfc_worker_read_emv_app(nfc_worker);
  67. } else if(nfc_worker->state == NfcWorkerStateReadEMV) {
  68. nfc_worker_read_emv(nfc_worker);
  69. } else if(nfc_worker->state == NfcWorkerStateEmulateApdu) {
  70. nfc_worker_emulate_apdu(nfc_worker);
  71. } else if(nfc_worker->state == NfcWorkerStateReadMifareUl) {
  72. nfc_worker_read_mifare_ul(nfc_worker);
  73. } else if(nfc_worker->state == NfcWorkerStateEmulateMifareUl) {
  74. nfc_worker_emulate_mifare_ul(nfc_worker);
  75. } else if(nfc_worker->state == NfcWorkerStateField) {
  76. nfc_worker_field(nfc_worker);
  77. }
  78. furi_hal_nfc_deactivate();
  79. nfc_worker_change_state(nfc_worker, NfcWorkerStateReady);
  80. furi_hal_power_insomnia_exit();
  81. osThreadExit();
  82. }
  83. void nfc_worker_detect(NfcWorker* nfc_worker) {
  84. rfalNfcDevice* dev_list;
  85. rfalNfcDevice* dev;
  86. uint8_t dev_cnt;
  87. NfcDeviceCommonData* result = &nfc_worker->dev_data->nfc_data;
  88. while(nfc_worker->state == NfcWorkerStateDetect) {
  89. if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 1000, true)) {
  90. // Process first found device
  91. dev = &dev_list[0];
  92. result->uid_len = dev->nfcidLen;
  93. memcpy(result->uid, dev->nfcid, dev->nfcidLen);
  94. if(dev->type == RFAL_NFC_LISTEN_TYPE_NFCA) {
  95. result->device = NfcDeviceNfca;
  96. result->atqa[0] = dev->dev.nfca.sensRes.anticollisionInfo;
  97. result->atqa[1] = dev->dev.nfca.sensRes.platformInfo;
  98. result->sak = dev->dev.nfca.selRes.sak;
  99. if(mf_ul_check_card_type(
  100. dev->dev.nfca.sensRes.anticollisionInfo,
  101. dev->dev.nfca.sensRes.platformInfo,
  102. dev->dev.nfca.selRes.sak)) {
  103. result->protocol = NfcDeviceProtocolMifareUl;
  104. } else if(dev->rfInterface == RFAL_NFC_INTERFACE_ISODEP) {
  105. result->protocol = NfcDeviceProtocolEMV;
  106. } else {
  107. result->protocol = NfcDeviceProtocolUnknown;
  108. }
  109. } else if(dev->type == RFAL_NFC_LISTEN_TYPE_NFCB) {
  110. result->device = NfcDeviceNfcb;
  111. } else if(dev->type == RFAL_NFC_LISTEN_TYPE_NFCF) {
  112. result->device = NfcDeviceNfcf;
  113. } else if(dev->type == RFAL_NFC_LISTEN_TYPE_NFCV) {
  114. result->device = NfcDeviceNfcv;
  115. }
  116. // Notify caller and exit
  117. if(nfc_worker->callback) {
  118. nfc_worker->callback(nfc_worker->context);
  119. }
  120. break;
  121. }
  122. osDelay(100);
  123. }
  124. }
  125. void nfc_worker_emulate(NfcWorker* nfc_worker) {
  126. NfcDeviceCommonData* data = &nfc_worker->dev_data->nfc_data;
  127. while(nfc_worker->state == NfcWorkerStateEmulate) {
  128. if(furi_hal_nfc_listen(data->uid, data->uid_len, data->atqa, data->sak, false, 100)) {
  129. FURI_LOG_I(TAG, "Reader detected");
  130. }
  131. osDelay(10);
  132. }
  133. }
  134. void nfc_worker_read_emv_app(NfcWorker* nfc_worker) {
  135. ReturnCode err;
  136. rfalNfcDevice* dev_list;
  137. EmvApplication emv_app = {};
  138. uint8_t dev_cnt = 0;
  139. uint8_t tx_buff[255] = {};
  140. uint16_t tx_len = 0;
  141. uint8_t* rx_buff;
  142. uint16_t* rx_len;
  143. NfcDeviceData* result = nfc_worker->dev_data;
  144. while(nfc_worker->state == NfcWorkerStateReadEMVApp) {
  145. memset(&emv_app, 0, sizeof(emv_app));
  146. if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 1000, false)) {
  147. // Card was found. Check that it supports EMV
  148. if(dev_list[0].rfInterface == RFAL_NFC_INTERFACE_ISODEP) {
  149. result->nfc_data.uid_len = dev_list[0].dev.nfca.nfcId1Len;
  150. result->nfc_data.atqa[0] = dev_list[0].dev.nfca.sensRes.anticollisionInfo;
  151. result->nfc_data.atqa[1] = dev_list[0].dev.nfca.sensRes.platformInfo;
  152. result->nfc_data.sak = dev_list[0].dev.nfca.selRes.sak;
  153. memcpy(
  154. result->nfc_data.uid, dev_list[0].dev.nfca.nfcId1, result->nfc_data.uid_len);
  155. result->nfc_data.protocol = NfcDeviceProtocolEMV;
  156. FURI_LOG_I(TAG, "Send select PPSE command");
  157. tx_len = emv_prepare_select_ppse(tx_buff);
  158. err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  159. if(err != ERR_NONE) {
  160. FURI_LOG_E(TAG, "Error during selection PPSE request: %d", err);
  161. furi_hal_nfc_deactivate();
  162. continue;
  163. }
  164. FURI_LOG_I(TAG, "Select PPSE response received. Start parsing response");
  165. if(emv_decode_ppse_response(rx_buff, *rx_len, &emv_app)) {
  166. FURI_LOG_I(TAG, "Select PPSE responce parced");
  167. // Notify caller and exit
  168. result->emv_data.aid_len = emv_app.aid_len;
  169. memcpy(result->emv_data.aid, emv_app.aid, emv_app.aid_len);
  170. if(nfc_worker->callback) {
  171. nfc_worker->callback(nfc_worker->context);
  172. }
  173. break;
  174. } else {
  175. FURI_LOG_E(TAG, "Can't find pay application");
  176. furi_hal_nfc_deactivate();
  177. continue;
  178. }
  179. } else {
  180. // Can't find EMV card
  181. FURI_LOG_W(TAG, "Card doesn't support EMV");
  182. furi_hal_nfc_deactivate();
  183. }
  184. } else {
  185. // Can't find EMV card
  186. FURI_LOG_W(TAG, "Can't find any cards");
  187. furi_hal_nfc_deactivate();
  188. }
  189. osDelay(20);
  190. }
  191. }
  192. void nfc_worker_read_emv(NfcWorker* nfc_worker) {
  193. ReturnCode err;
  194. rfalNfcDevice* dev_list;
  195. EmvApplication emv_app = {};
  196. uint8_t dev_cnt = 0;
  197. uint8_t tx_buff[255] = {};
  198. uint16_t tx_len = 0;
  199. uint8_t* rx_buff;
  200. uint16_t* rx_len;
  201. NfcDeviceData* result = nfc_worker->dev_data;
  202. while(nfc_worker->state == NfcWorkerStateReadEMV) {
  203. memset(&emv_app, 0, sizeof(emv_app));
  204. if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 1000, false)) {
  205. // Card was found. Check that it supports EMV
  206. if(dev_list[0].rfInterface == RFAL_NFC_INTERFACE_ISODEP) {
  207. result->nfc_data.uid_len = dev_list[0].dev.nfca.nfcId1Len;
  208. result->nfc_data.atqa[0] = dev_list[0].dev.nfca.sensRes.anticollisionInfo;
  209. result->nfc_data.atqa[1] = dev_list[0].dev.nfca.sensRes.platformInfo;
  210. result->nfc_data.sak = dev_list[0].dev.nfca.selRes.sak;
  211. memcpy(
  212. result->nfc_data.uid, dev_list[0].dev.nfca.nfcId1, result->nfc_data.uid_len);
  213. result->nfc_data.protocol = NfcDeviceProtocolEMV;
  214. FURI_LOG_I(TAG, "Send select PPSE command");
  215. tx_len = emv_prepare_select_ppse(tx_buff);
  216. err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  217. if(err != ERR_NONE) {
  218. FURI_LOG_E(TAG, "Error during selection PPSE request: %d", err);
  219. furi_hal_nfc_deactivate();
  220. continue;
  221. }
  222. FURI_LOG_I(TAG, "Select PPSE response received. Start parsing response");
  223. if(emv_decode_ppse_response(rx_buff, *rx_len, &emv_app)) {
  224. FURI_LOG_I(TAG, "Select PPSE responce parced");
  225. result->emv_data.aid_len = emv_app.aid_len;
  226. memcpy(result->emv_data.aid, emv_app.aid, emv_app.aid_len);
  227. } else {
  228. FURI_LOG_E(TAG, "Can't find pay application");
  229. furi_hal_nfc_deactivate();
  230. continue;
  231. }
  232. FURI_LOG_I(TAG, "Starting application ...");
  233. tx_len = emv_prepare_select_app(tx_buff, &emv_app);
  234. err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  235. if(err != ERR_NONE) {
  236. FURI_LOG_E(TAG, "Error during application selection request: %d", err);
  237. furi_hal_nfc_deactivate();
  238. continue;
  239. }
  240. FURI_LOG_I(TAG, "Select application response received. Start parsing response");
  241. if(emv_decode_select_app_response(rx_buff, *rx_len, &emv_app)) {
  242. FURI_LOG_I(TAG, "Card name: %s", emv_app.name);
  243. memcpy(result->emv_data.name, emv_app.name, sizeof(emv_app.name));
  244. } else if(emv_app.pdol.size > 0) {
  245. FURI_LOG_W(TAG, "Can't find card name, but PDOL is present.");
  246. } else {
  247. FURI_LOG_E(TAG, "Can't find card name or PDOL");
  248. furi_hal_nfc_deactivate();
  249. continue;
  250. }
  251. FURI_LOG_I(TAG, "Starting Get Processing Options command ...");
  252. tx_len = emv_prepare_get_proc_opt(tx_buff, &emv_app);
  253. err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  254. if(err != ERR_NONE) {
  255. FURI_LOG_E(TAG, "Error during Get Processing Options command: %d", err);
  256. furi_hal_nfc_deactivate();
  257. continue;
  258. }
  259. if(emv_decode_get_proc_opt(rx_buff, *rx_len, &emv_app)) {
  260. FURI_LOG_I(TAG, "Card number parsed");
  261. result->emv_data.number_len = emv_app.card_number_len;
  262. memcpy(result->emv_data.number, emv_app.card_number, emv_app.card_number_len);
  263. // Notify caller and exit
  264. if(nfc_worker->callback) {
  265. nfc_worker->callback(nfc_worker->context);
  266. }
  267. break;
  268. } else {
  269. // Mastercard doesn't give PAN / card number as GPO response
  270. // Iterate over all files found in application
  271. bool pan_found = false;
  272. for(uint8_t i = 0; (i < emv_app.afl.size) && !pan_found; i += 4) {
  273. uint8_t sfi = emv_app.afl.data[i] >> 3;
  274. uint8_t record_start = emv_app.afl.data[i + 1];
  275. uint8_t record_end = emv_app.afl.data[i + 2];
  276. // Iterate over all records in file
  277. for(uint8_t record = record_start; record <= record_end; ++record) {
  278. tx_len = emv_prepare_read_sfi_record(tx_buff, sfi, record);
  279. err = furi_hal_nfc_data_exchange(
  280. tx_buff, tx_len, &rx_buff, &rx_len, false);
  281. if(err != ERR_NONE) {
  282. FURI_LOG_E(
  283. TAG,
  284. "Error reading application sfi %d, record %d",
  285. sfi,
  286. record);
  287. }
  288. if(emv_decode_read_sfi_record(rx_buff, *rx_len, &emv_app)) {
  289. pan_found = true;
  290. break;
  291. }
  292. }
  293. }
  294. if(pan_found) {
  295. FURI_LOG_I(TAG, "Card PAN found");
  296. result->emv_data.number_len = emv_app.card_number_len;
  297. memcpy(
  298. result->emv_data.number,
  299. emv_app.card_number,
  300. result->emv_data.number_len);
  301. if(emv_app.exp_month) {
  302. result->emv_data.exp_mon = emv_app.exp_month;
  303. result->emv_data.exp_year = emv_app.exp_year;
  304. }
  305. if(emv_app.country_code) {
  306. result->emv_data.country_code = emv_app.country_code;
  307. }
  308. if(emv_app.currency_code) {
  309. result->emv_data.currency_code = emv_app.currency_code;
  310. }
  311. // Notify caller and exit
  312. if(nfc_worker->callback) {
  313. nfc_worker->callback(nfc_worker->context);
  314. }
  315. break;
  316. } else {
  317. FURI_LOG_E(TAG, "Can't read card number");
  318. }
  319. furi_hal_nfc_deactivate();
  320. }
  321. } else {
  322. // Can't find EMV card
  323. FURI_LOG_W(TAG, "Card doesn't support EMV");
  324. furi_hal_nfc_deactivate();
  325. }
  326. } else {
  327. // Can't find EMV card
  328. FURI_LOG_W(TAG, "Can't find any cards");
  329. furi_hal_nfc_deactivate();
  330. }
  331. osDelay(20);
  332. }
  333. }
  334. void nfc_worker_emulate_apdu(NfcWorker* nfc_worker) {
  335. ReturnCode err;
  336. uint8_t tx_buff[255] = {};
  337. uint16_t tx_len = 0;
  338. uint8_t* rx_buff;
  339. uint16_t* rx_len;
  340. NfcDeviceCommonData params = {
  341. .uid = {0xCF, 0x72, 0xd4, 0x40},
  342. .uid_len = 4,
  343. .atqa = {0x00, 0x04},
  344. .sak = 0x20,
  345. .device = NfcDeviceNfca,
  346. .protocol = NfcDeviceProtocolEMV,
  347. };
  348. // Test RX data
  349. const uint8_t debug_rx[] = {
  350. 0xba, 0x0b, 0xba, 0xba, 0x20, 0x00, 0x02, 0x28, 0xde, 0xad, 0xbe, 0xef, 0x00, 0xca, 0xca,
  351. 0xca, 0xfe, 0xfa, 0xce, 0x14, 0x88, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  352. 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0xba,
  353. 0x0b, 0xba, 0xba, 0x20, 0x00, 0x02, 0x28, 0xde, 0xad, 0xbe, 0xef, 0x00, 0xca, 0xca, 0xca,
  354. 0xfe, 0xfa, 0xce, 0x14, 0x88, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99,
  355. 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0xba, 0x0b,
  356. 0xba, 0xba, 0x20, 0x00, 0x02, 0x28, 0xde, 0xad, 0xbe, 0xef, 0x00, 0xca, 0xca, 0xca, 0xfe,
  357. 0xfa, 0xce, 0x14, 0x88, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa,
  358. 0xbb, 0xcc, 0xdd, 0xee, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0xba, 0x0b, 0xba,
  359. 0xba, 0x20, 0x00, 0x02, 0x28, 0xde, 0xad, 0xbe, 0xef, 0x00, 0xca, 0xca, 0xca, 0xfe, 0xfa,
  360. 0xce, 0x14, 0x88, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb,
  361. 0xcc, 0xdd, 0xee, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0xba, 0x0b, 0xba, 0xba,
  362. 0x20, 0x00, 0x02, 0x28, 0xde, 0xad, 0xbe, 0xef, 0x00, 0xca, 0xca, 0xca, 0xfe, 0xfa, 0xce,
  363. 0x14, 0x88, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc,
  364. 0xdd, 0xee, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0xba, 0x0b, 0xba, 0xba, 0x20,
  365. 0x00, 0x02, 0x28, 0xde, 0xad, 0xbe, 0xef, 0x00, 0xca, 0xca, 0xca, 0xfe, 0xfa, 0xce, 0x14,
  366. 0x88, 0x00};
  367. // Test TX data
  368. const uint8_t debug_tx[] = {
  369. 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32,
  370. 0x10, 0x14, 0x88, 0x02, 0x28, 0x00, 0x00, 0xca, 0xca, 0x00, 0xc0, 0xc0, 0x00, 0xde, 0xad,
  371. 0xbe, 0xef, 0xce, 0xee, 0xec, 0xca, 0xfe, 0xba, 0xba, 0xb0, 0xb0, 0xac, 0xdc, 0x11, 0x12,
  372. 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  373. 0x14, 0x88, 0x02, 0x28, 0x00, 0x00, 0xca, 0xca, 0x00, 0xc0, 0xc0, 0x00, 0xde, 0xad, 0xbe,
  374. 0xef, 0xce, 0xee, 0xec, 0xca, 0xfe, 0xba, 0xba, 0xb0, 0xb0, 0xac, 0xdc, 0x11, 0x12, 0x34,
  375. 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x14,
  376. 0x88, 0x02, 0x28, 0x00, 0x00, 0xca, 0xca, 0x00, 0xc0, 0xc0, 0x00, 0xde, 0xad, 0xbe, 0xef,
  377. 0xce, 0xee, 0xec, 0xca, 0xfe, 0xba, 0xba, 0xb0, 0xb0, 0xac, 0xdc, 0x11, 0x12, 0x34, 0x56,
  378. 0x78, 0x9a, 0xbc, 0xde, 0xff, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x14, 0x88,
  379. 0x02, 0x28, 0x00, 0x00, 0xca, 0xca, 0x00, 0xc0, 0xc0, 0x00, 0xde, 0xad, 0xbe, 0xef, 0xce,
  380. 0xee, 0xec, 0xca, 0xfe, 0xba, 0xba, 0xb0, 0xb0, 0xac, 0xdc, 0x11, 0x12, 0x34, 0x56, 0x78,
  381. 0x9a, 0xbc, 0xde, 0xff, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x14, 0x88, 0x02,
  382. 0x28, 0x00, 0x00, 0xca, 0xca, 0x00, 0xc0, 0xc0, 0x00, 0xde, 0xad, 0xbe, 0xef, 0xce, 0xee,
  383. 0xec, 0xca, 0xfe, 0xba, 0xba, 0xb0, 0xb0, 0xac, 0xdc, 0x11, 0x12, 0x34, 0x56, 0x78, 0x9a,
  384. 0xbc, 0xde, 0xff, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x14, 0x88, 0x02, 0x28,
  385. 0x00, 0x00};
  386. while(nfc_worker->state == NfcWorkerStateEmulateApdu) {
  387. if(furi_hal_nfc_listen(params.uid, params.uid_len, params.atqa, params.sak, false, 300)) {
  388. FURI_LOG_I(TAG, "POS terminal detected");
  389. // Read data from POS terminal
  390. err = furi_hal_nfc_data_exchange(NULL, 0, &rx_buff, &rx_len, false);
  391. if(err == ERR_NONE) {
  392. FURI_LOG_I(TAG, "Received Select PPSE");
  393. } else {
  394. FURI_LOG_E(TAG, "Error in 1st data exchange: select PPSE");
  395. furi_hal_nfc_deactivate();
  396. continue;
  397. }
  398. FURI_LOG_I(TAG, "Transive SELECT PPSE ANS");
  399. tx_len = emv_select_ppse_ans(tx_buff);
  400. err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  401. if(err == ERR_NONE) {
  402. FURI_LOG_I(TAG, "Received Select APP");
  403. } else {
  404. FURI_LOG_E(TAG, "Error in 2nd data exchange: select APP");
  405. furi_hal_nfc_deactivate();
  406. continue;
  407. }
  408. FURI_LOG_I(TAG, "Transive SELECT APP ANS");
  409. tx_len = emv_select_app_ans(tx_buff);
  410. err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  411. if(err == ERR_NONE) {
  412. FURI_LOG_I(TAG, "Received PDOL");
  413. } else {
  414. FURI_LOG_E(TAG, "Error in 3rd data exchange: receive PDOL");
  415. furi_hal_nfc_deactivate();
  416. continue;
  417. }
  418. FURI_LOG_I(TAG, "Transive PDOL ANS");
  419. tx_len = emv_get_proc_opt_ans(tx_buff);
  420. err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  421. if(err == ERR_NONE) {
  422. FURI_LOG_I(TAG, "Transive PDOL ANS");
  423. } else {
  424. FURI_LOG_E(TAG, "Error in 4rd data exchange: Transive PDOL ANS");
  425. furi_hal_nfc_deactivate();
  426. continue;
  427. }
  428. if(*rx_len != sizeof(debug_rx) || memcmp(rx_buff, debug_rx, sizeof(debug_rx))) {
  429. FURI_LOG_E(TAG, "Failed long message test");
  430. } else {
  431. FURI_LOG_I(TAG, "Correct debug message received");
  432. tx_len = sizeof(debug_tx);
  433. err = furi_hal_nfc_data_exchange(
  434. (uint8_t*)debug_tx, tx_len, &rx_buff, &rx_len, false);
  435. if(err == ERR_NONE) {
  436. FURI_LOG_I(TAG, "Transive Debug message");
  437. }
  438. }
  439. furi_hal_nfc_deactivate();
  440. } else {
  441. FURI_LOG_W(TAG, "Can't find reader");
  442. }
  443. osDelay(20);
  444. }
  445. }
  446. void nfc_worker_read_mifare_ul(NfcWorker* nfc_worker) {
  447. ReturnCode err;
  448. rfalNfcDevice* dev_list;
  449. uint8_t dev_cnt = 0;
  450. uint8_t tx_buff[255] = {};
  451. uint16_t tx_len = 0;
  452. uint8_t* rx_buff;
  453. uint16_t* rx_len;
  454. MifareUlDevice mf_ul_read;
  455. NfcDeviceData* result = nfc_worker->dev_data;
  456. while(nfc_worker->state == NfcWorkerStateReadMifareUl) {
  457. furi_hal_nfc_deactivate();
  458. memset(&mf_ul_read, 0, sizeof(mf_ul_read));
  459. if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 300, false)) {
  460. if(dev_list[0].type == RFAL_NFC_LISTEN_TYPE_NFCA &&
  461. mf_ul_check_card_type(
  462. dev_list[0].dev.nfca.sensRes.anticollisionInfo,
  463. dev_list[0].dev.nfca.sensRes.platformInfo,
  464. dev_list[0].dev.nfca.selRes.sak)) {
  465. // Get Mifare Ultralight version
  466. FURI_LOG_I(TAG, "Found Mifare Ultralight tag. Reading tag version");
  467. tx_len = mf_ul_prepare_get_version(tx_buff);
  468. err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  469. if(err == ERR_NONE) {
  470. mf_ul_parse_get_version_response(rx_buff, &mf_ul_read);
  471. FURI_LOG_I(
  472. TAG,
  473. "Mifare Ultralight Type: %d, Pages: %d",
  474. mf_ul_read.type,
  475. mf_ul_read.pages_to_read);
  476. FURI_LOG_I(TAG, "Reading signature ...");
  477. tx_len = mf_ul_prepare_read_signature(tx_buff);
  478. if(furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false)) {
  479. FURI_LOG_W(TAG, "Failed reading signature");
  480. memset(mf_ul_read.data.signature, 0, sizeof(mf_ul_read.data.signature));
  481. } else {
  482. mf_ul_parse_read_signature_response(rx_buff, &mf_ul_read);
  483. }
  484. } else if(err == ERR_TIMEOUT) {
  485. FURI_LOG_W(
  486. TAG,
  487. "Card doesn't respond to GET VERSION command. Setting default read parameters");
  488. err = ERR_NONE;
  489. mf_ul_set_default_version(&mf_ul_read);
  490. // Reinit device
  491. furi_hal_nfc_deactivate();
  492. if(!furi_hal_nfc_detect(&dev_list, &dev_cnt, 300, false)) {
  493. FURI_LOG_E(TAG, "Lost connection. Restarting search");
  494. continue;
  495. }
  496. } else {
  497. FURI_LOG_E(
  498. TAG, "Error getting Mifare Ultralight version. Error code: %d", err);
  499. continue;
  500. }
  501. if(mf_ul_read.support_fast_read) {
  502. FURI_LOG_I(TAG, "Reading pages ...");
  503. tx_len = mf_ul_prepare_fast_read(tx_buff, 0x00, mf_ul_read.pages_to_read - 1);
  504. if(furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false)) {
  505. FURI_LOG_E(TAG, "Failed reading pages");
  506. continue;
  507. } else {
  508. mf_ul_parse_fast_read_response(
  509. rx_buff, 0x00, mf_ul_read.pages_to_read - 1, &mf_ul_read);
  510. }
  511. FURI_LOG_I(TAG, "Reading 3 counters ...");
  512. for(uint8_t i = 0; i < 3; i++) {
  513. tx_len = mf_ul_prepare_read_cnt(tx_buff, i);
  514. if(furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false)) {
  515. FURI_LOG_W(TAG, "Failed reading Counter %d", i);
  516. mf_ul_read.data.counter[i] = 0;
  517. } else {
  518. mf_ul_parse_read_cnt_response(rx_buff, i, &mf_ul_read);
  519. }
  520. }
  521. FURI_LOG_I(TAG, "Checking tearing flags ...");
  522. for(uint8_t i = 0; i < 3; i++) {
  523. tx_len = mf_ul_prepare_check_tearing(tx_buff, i);
  524. if(furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false)) {
  525. FURI_LOG_E(TAG, "Error checking tearing flag %d", i);
  526. mf_ul_read.data.tearing[i] = MF_UL_TEARING_FLAG_DEFAULT;
  527. } else {
  528. mf_ul_parse_check_tearing_response(rx_buff, i, &mf_ul_read);
  529. }
  530. }
  531. } else {
  532. // READ card with READ command (4 pages at a time)
  533. for(uint8_t page = 0; page < mf_ul_read.pages_to_read; page += 4) {
  534. FURI_LOG_I(TAG, "Reading pages %d - %d ...", page, page + 3);
  535. tx_len = mf_ul_prepare_read(tx_buff, page);
  536. if(furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false)) {
  537. FURI_LOG_E(TAG, "Read pages %d - %d failed", page, page + 3);
  538. continue;
  539. } else {
  540. mf_ul_parse_read_response(rx_buff, page, &mf_ul_read);
  541. }
  542. }
  543. }
  544. // Fill result data
  545. result->nfc_data.uid_len = dev_list[0].dev.nfca.nfcId1Len;
  546. result->nfc_data.atqa[0] = dev_list[0].dev.nfca.sensRes.anticollisionInfo;
  547. result->nfc_data.atqa[1] = dev_list[0].dev.nfca.sensRes.platformInfo;
  548. result->nfc_data.sak = dev_list[0].dev.nfca.selRes.sak;
  549. result->nfc_data.protocol = NfcDeviceProtocolMifareUl;
  550. memcpy(
  551. result->nfc_data.uid, dev_list[0].dev.nfca.nfcId1, result->nfc_data.uid_len);
  552. result->mf_ul_data = mf_ul_read.data;
  553. // Notify caller and exit
  554. if(nfc_worker->callback) {
  555. nfc_worker->callback(nfc_worker->context);
  556. }
  557. break;
  558. } else {
  559. FURI_LOG_W(TAG, "Tag does not support Mifare Ultralight");
  560. }
  561. } else {
  562. FURI_LOG_W(TAG, "Can't find any tags");
  563. }
  564. osDelay(100);
  565. }
  566. }
  567. void nfc_worker_emulate_mifare_ul(NfcWorker* nfc_worker) {
  568. ReturnCode err;
  569. uint8_t tx_buff[255] = {};
  570. uint16_t tx_len = 0;
  571. uint8_t* rx_buff;
  572. uint16_t* rx_len;
  573. NfcDeviceData* data = nfc_worker->dev_data;
  574. MifareUlDevice mf_ul_emulate;
  575. // Setup emulation parameters from mifare ultralight data structure
  576. mf_ul_prepare_emulation(&mf_ul_emulate, &data->mf_ul_data);
  577. while(nfc_worker->state == NfcWorkerStateEmulateMifareUl) {
  578. if(furi_hal_nfc_listen(
  579. data->nfc_data.uid,
  580. data->nfc_data.uid_len,
  581. data->nfc_data.atqa,
  582. data->nfc_data.sak,
  583. true,
  584. 200)) {
  585. FURI_LOG_D(TAG, "Anticollision passed");
  586. if(furi_hal_nfc_get_first_frame(&rx_buff, &rx_len)) {
  587. // Data exchange loop
  588. while(nfc_worker->state == NfcWorkerStateEmulateMifareUl) {
  589. tx_len = mf_ul_prepare_emulation_response(
  590. rx_buff, *rx_len, tx_buff, &mf_ul_emulate);
  591. if(tx_len > 0) {
  592. err =
  593. furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  594. if(err == ERR_NONE) {
  595. continue;
  596. } else {
  597. FURI_LOG_E(TAG, "Communication error: %d", err);
  598. break;
  599. }
  600. } else {
  601. FURI_LOG_W(TAG, "Not valid command: %02X", rx_buff[0]);
  602. furi_hal_nfc_deactivate();
  603. break;
  604. }
  605. }
  606. } else {
  607. FURI_LOG_W(TAG, "Error in 1st data exchange");
  608. furi_hal_nfc_deactivate();
  609. }
  610. }
  611. // Check if data was modified
  612. if(mf_ul_emulate.data_changed) {
  613. nfc_worker->dev_data->mf_ul_data = mf_ul_emulate.data;
  614. if(nfc_worker->callback) {
  615. nfc_worker->callback(nfc_worker->context);
  616. }
  617. }
  618. FURI_LOG_W(TAG, "Can't find reader");
  619. osThreadYield();
  620. }
  621. }
  622. void nfc_worker_field(NfcWorker* nfc_worker) {
  623. furi_hal_nfc_field_on();
  624. while(nfc_worker->state == NfcWorkerStateField) {
  625. osDelay(50);
  626. }
  627. furi_hal_nfc_field_off();
  628. }