nfc_worker.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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 NFC_WORKER_TAG "nfc worker"
  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 = "nfc_worker";
  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(NFC_WORKER_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(NFC_WORKER_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(NFC_WORKER_TAG, "Error during selection PPSE request: %d", err);
  161. furi_hal_nfc_deactivate();
  162. continue;
  163. }
  164. FURI_LOG_I(
  165. NFC_WORKER_TAG, "Select PPSE response received. Start parsing response");
  166. if(emv_decode_ppse_response(rx_buff, *rx_len, &emv_app)) {
  167. FURI_LOG_I(NFC_WORKER_TAG, "Select PPSE responce parced");
  168. // Notify caller and exit
  169. result->emv_data.aid_len = emv_app.aid_len;
  170. memcpy(result->emv_data.aid, emv_app.aid, emv_app.aid_len);
  171. if(nfc_worker->callback) {
  172. nfc_worker->callback(nfc_worker->context);
  173. }
  174. break;
  175. } else {
  176. FURI_LOG_E(NFC_WORKER_TAG, "Can't find pay application");
  177. furi_hal_nfc_deactivate();
  178. continue;
  179. }
  180. } else {
  181. // Can't find EMV card
  182. FURI_LOG_W(NFC_WORKER_TAG, "Card doesn't support EMV");
  183. furi_hal_nfc_deactivate();
  184. }
  185. } else {
  186. // Can't find EMV card
  187. FURI_LOG_W(NFC_WORKER_TAG, "Can't find any cards");
  188. furi_hal_nfc_deactivate();
  189. }
  190. osDelay(20);
  191. }
  192. }
  193. void nfc_worker_read_emv(NfcWorker* nfc_worker) {
  194. ReturnCode err;
  195. rfalNfcDevice* dev_list;
  196. EmvApplication emv_app = {};
  197. uint8_t dev_cnt = 0;
  198. uint8_t tx_buff[255] = {};
  199. uint16_t tx_len = 0;
  200. uint8_t* rx_buff;
  201. uint16_t* rx_len;
  202. NfcDeviceData* result = nfc_worker->dev_data;
  203. while(nfc_worker->state == NfcWorkerStateReadEMV) {
  204. memset(&emv_app, 0, sizeof(emv_app));
  205. if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 1000, false)) {
  206. // Card was found. Check that it supports EMV
  207. if(dev_list[0].rfInterface == RFAL_NFC_INTERFACE_ISODEP) {
  208. result->nfc_data.uid_len = dev_list[0].dev.nfca.nfcId1Len;
  209. result->nfc_data.atqa[0] = dev_list[0].dev.nfca.sensRes.anticollisionInfo;
  210. result->nfc_data.atqa[1] = dev_list[0].dev.nfca.sensRes.platformInfo;
  211. result->nfc_data.sak = dev_list[0].dev.nfca.selRes.sak;
  212. memcpy(
  213. result->nfc_data.uid, dev_list[0].dev.nfca.nfcId1, result->nfc_data.uid_len);
  214. result->nfc_data.protocol = NfcDeviceProtocolEMV;
  215. FURI_LOG_I(NFC_WORKER_TAG, "Send select PPSE command");
  216. tx_len = emv_prepare_select_ppse(tx_buff);
  217. err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  218. if(err != ERR_NONE) {
  219. FURI_LOG_E(NFC_WORKER_TAG, "Error during selection PPSE request: %d", err);
  220. furi_hal_nfc_deactivate();
  221. continue;
  222. }
  223. FURI_LOG_I(
  224. NFC_WORKER_TAG, "Select PPSE response received. Start parsing response");
  225. if(emv_decode_ppse_response(rx_buff, *rx_len, &emv_app)) {
  226. FURI_LOG_I(NFC_WORKER_TAG, "Select PPSE responce parced");
  227. } else {
  228. FURI_LOG_E(NFC_WORKER_TAG, "Can't find pay application");
  229. furi_hal_nfc_deactivate();
  230. continue;
  231. }
  232. FURI_LOG_I(NFC_WORKER_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(
  237. NFC_WORKER_TAG, "Error during application selection request: %d", err);
  238. furi_hal_nfc_deactivate();
  239. continue;
  240. }
  241. FURI_LOG_I(
  242. NFC_WORKER_TAG,
  243. "Select application response received. Start parsing response");
  244. if(emv_decode_select_app_response(rx_buff, *rx_len, &emv_app)) {
  245. FURI_LOG_I(NFC_WORKER_TAG, "Card name: %s", emv_app.name);
  246. memcpy(result->emv_data.name, emv_app.name, sizeof(emv_app.name));
  247. } else if(emv_app.pdol.size > 0) {
  248. FURI_LOG_W(NFC_WORKER_TAG, "Can't find card name, but PDOL is present.");
  249. } else {
  250. FURI_LOG_E(NFC_WORKER_TAG, "Can't find card name or PDOL");
  251. furi_hal_nfc_deactivate();
  252. continue;
  253. }
  254. FURI_LOG_I(NFC_WORKER_TAG, "Starting Get Processing Options command ...");
  255. tx_len = emv_prepare_get_proc_opt(tx_buff, &emv_app);
  256. err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  257. if(err != ERR_NONE) {
  258. FURI_LOG_E(
  259. NFC_WORKER_TAG, "Error during Get Processing Options command: %d", err);
  260. furi_hal_nfc_deactivate();
  261. continue;
  262. }
  263. if(emv_decode_get_proc_opt(rx_buff, *rx_len, &emv_app)) {
  264. FURI_LOG_I(NFC_WORKER_TAG, "Card number parsed");
  265. result->emv_data.number_len = emv_app.card_number_len;
  266. memcpy(result->emv_data.number, emv_app.card_number, emv_app.card_number_len);
  267. // Notify caller and exit
  268. if(nfc_worker->callback) {
  269. nfc_worker->callback(nfc_worker->context);
  270. }
  271. break;
  272. } else {
  273. // Mastercard doesn't give PAN / card number as GPO response
  274. // Iterate over all files found in application
  275. bool pan_found = false;
  276. for(uint8_t i = 0; (i < emv_app.afl.size) && !pan_found; i += 4) {
  277. uint8_t sfi = emv_app.afl.data[i] >> 3;
  278. uint8_t record_start = emv_app.afl.data[i + 1];
  279. uint8_t record_end = emv_app.afl.data[i + 2];
  280. // Iterate over all records in file
  281. for(uint8_t record = record_start; record <= record_end; ++record) {
  282. tx_len = emv_prepare_read_sfi_record(tx_buff, sfi, record);
  283. err = furi_hal_nfc_data_exchange(
  284. tx_buff, tx_len, &rx_buff, &rx_len, false);
  285. if(err != ERR_NONE) {
  286. FURI_LOG_E(
  287. NFC_WORKER_TAG,
  288. "Error reading application sfi %d, record %d",
  289. sfi,
  290. record);
  291. }
  292. if(emv_decode_read_sfi_record(rx_buff, *rx_len, &emv_app)) {
  293. pan_found = true;
  294. break;
  295. }
  296. }
  297. }
  298. if(pan_found) {
  299. FURI_LOG_I(NFC_WORKER_TAG, "Card PAN found");
  300. result->emv_data.number_len = emv_app.card_number_len;
  301. memcpy(
  302. result->emv_data.number,
  303. emv_app.card_number,
  304. result->emv_data.number_len);
  305. if(emv_app.exp_month) {
  306. result->emv_data.exp_mon = emv_app.exp_month;
  307. result->emv_data.exp_year = emv_app.exp_year;
  308. }
  309. if(emv_app.country_code) {
  310. result->emv_data.country_code = emv_app.country_code;
  311. }
  312. if(emv_app.currency_code) {
  313. result->emv_data.currency_code = emv_app.currency_code;
  314. }
  315. // Notify caller and exit
  316. if(nfc_worker->callback) {
  317. nfc_worker->callback(nfc_worker->context);
  318. }
  319. break;
  320. } else {
  321. FURI_LOG_E(NFC_WORKER_TAG, "Can't read card number");
  322. }
  323. furi_hal_nfc_deactivate();
  324. }
  325. } else {
  326. // Can't find EMV card
  327. FURI_LOG_W(NFC_WORKER_TAG, "Card doesn't support EMV");
  328. furi_hal_nfc_deactivate();
  329. }
  330. } else {
  331. // Can't find EMV card
  332. FURI_LOG_W(NFC_WORKER_TAG, "Can't find any cards");
  333. furi_hal_nfc_deactivate();
  334. }
  335. osDelay(20);
  336. }
  337. }
  338. void nfc_worker_emulate_apdu(NfcWorker* nfc_worker) {
  339. ReturnCode err;
  340. uint8_t tx_buff[255] = {};
  341. uint16_t tx_len = 0;
  342. uint8_t* rx_buff;
  343. uint16_t* rx_len;
  344. NfcDeviceCommonData params = {
  345. .uid = {0xCF, 0x72, 0xd4, 0x40},
  346. .uid_len = 4,
  347. .atqa = {0x00, 0x04},
  348. .sak = 0x20,
  349. .device = NfcDeviceNfca,
  350. .protocol = NfcDeviceProtocolEMV,
  351. };
  352. // Test RX data
  353. const uint8_t debug_rx[] = {
  354. 0xba, 0x0b, 0xba, 0xba, 0x20, 0x00, 0x02, 0x28, 0xde, 0xad, 0xbe, 0xef, 0x00, 0xca, 0xca,
  355. 0xca, 0xfe, 0xfa, 0xce, 0x14, 0x88, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  356. 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0xba,
  357. 0x0b, 0xba, 0xba, 0x20, 0x00, 0x02, 0x28, 0xde, 0xad, 0xbe, 0xef, 0x00, 0xca, 0xca, 0xca,
  358. 0xfe, 0xfa, 0xce, 0x14, 0x88, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99,
  359. 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0xba, 0x0b,
  360. 0xba, 0xba, 0x20, 0x00, 0x02, 0x28, 0xde, 0xad, 0xbe, 0xef, 0x00, 0xca, 0xca, 0xca, 0xfe,
  361. 0xfa, 0xce, 0x14, 0x88, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa,
  362. 0xbb, 0xcc, 0xdd, 0xee, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0xba, 0x0b, 0xba,
  363. 0xba, 0x20, 0x00, 0x02, 0x28, 0xde, 0xad, 0xbe, 0xef, 0x00, 0xca, 0xca, 0xca, 0xfe, 0xfa,
  364. 0xce, 0x14, 0x88, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb,
  365. 0xcc, 0xdd, 0xee, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0xba, 0x0b, 0xba, 0xba,
  366. 0x20, 0x00, 0x02, 0x28, 0xde, 0xad, 0xbe, 0xef, 0x00, 0xca, 0xca, 0xca, 0xfe, 0xfa, 0xce,
  367. 0x14, 0x88, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc,
  368. 0xdd, 0xee, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0xba, 0x0b, 0xba, 0xba, 0x20,
  369. 0x00, 0x02, 0x28, 0xde, 0xad, 0xbe, 0xef, 0x00, 0xca, 0xca, 0xca, 0xfe, 0xfa, 0xce, 0x14,
  370. 0x88, 0x00};
  371. // Test TX data
  372. const uint8_t debug_tx[] = {
  373. 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32,
  374. 0x10, 0x14, 0x88, 0x02, 0x28, 0x00, 0x00, 0xca, 0xca, 0x00, 0xc0, 0xc0, 0x00, 0xde, 0xad,
  375. 0xbe, 0xef, 0xce, 0xee, 0xec, 0xca, 0xfe, 0xba, 0xba, 0xb0, 0xb0, 0xac, 0xdc, 0x11, 0x12,
  376. 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  377. 0x14, 0x88, 0x02, 0x28, 0x00, 0x00, 0xca, 0xca, 0x00, 0xc0, 0xc0, 0x00, 0xde, 0xad, 0xbe,
  378. 0xef, 0xce, 0xee, 0xec, 0xca, 0xfe, 0xba, 0xba, 0xb0, 0xb0, 0xac, 0xdc, 0x11, 0x12, 0x34,
  379. 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x14,
  380. 0x88, 0x02, 0x28, 0x00, 0x00, 0xca, 0xca, 0x00, 0xc0, 0xc0, 0x00, 0xde, 0xad, 0xbe, 0xef,
  381. 0xce, 0xee, 0xec, 0xca, 0xfe, 0xba, 0xba, 0xb0, 0xb0, 0xac, 0xdc, 0x11, 0x12, 0x34, 0x56,
  382. 0x78, 0x9a, 0xbc, 0xde, 0xff, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x14, 0x88,
  383. 0x02, 0x28, 0x00, 0x00, 0xca, 0xca, 0x00, 0xc0, 0xc0, 0x00, 0xde, 0xad, 0xbe, 0xef, 0xce,
  384. 0xee, 0xec, 0xca, 0xfe, 0xba, 0xba, 0xb0, 0xb0, 0xac, 0xdc, 0x11, 0x12, 0x34, 0x56, 0x78,
  385. 0x9a, 0xbc, 0xde, 0xff, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x14, 0x88, 0x02,
  386. 0x28, 0x00, 0x00, 0xca, 0xca, 0x00, 0xc0, 0xc0, 0x00, 0xde, 0xad, 0xbe, 0xef, 0xce, 0xee,
  387. 0xec, 0xca, 0xfe, 0xba, 0xba, 0xb0, 0xb0, 0xac, 0xdc, 0x11, 0x12, 0x34, 0x56, 0x78, 0x9a,
  388. 0xbc, 0xde, 0xff, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x14, 0x88, 0x02, 0x28,
  389. 0x00, 0x00};
  390. while(nfc_worker->state == NfcWorkerStateEmulateApdu) {
  391. if(furi_hal_nfc_listen(params.uid, params.uid_len, params.atqa, params.sak, false, 300)) {
  392. FURI_LOG_I(NFC_WORKER_TAG, "POS terminal detected");
  393. // Read data from POS terminal
  394. err = furi_hal_nfc_data_exchange(NULL, 0, &rx_buff, &rx_len, false);
  395. if(err == ERR_NONE) {
  396. FURI_LOG_I(NFC_WORKER_TAG, "Received Select PPSE");
  397. } else {
  398. FURI_LOG_E(NFC_WORKER_TAG, "Error in 1st data exchange: select PPSE");
  399. furi_hal_nfc_deactivate();
  400. continue;
  401. }
  402. FURI_LOG_I(NFC_WORKER_TAG, "Transive SELECT PPSE ANS");
  403. tx_len = emv_select_ppse_ans(tx_buff);
  404. err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  405. if(err == ERR_NONE) {
  406. FURI_LOG_I(NFC_WORKER_TAG, "Received Select APP");
  407. } else {
  408. FURI_LOG_E(NFC_WORKER_TAG, "Error in 2nd data exchange: select APP");
  409. furi_hal_nfc_deactivate();
  410. continue;
  411. }
  412. FURI_LOG_I(NFC_WORKER_TAG, "Transive SELECT APP ANS");
  413. tx_len = emv_select_app_ans(tx_buff);
  414. err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  415. if(err == ERR_NONE) {
  416. FURI_LOG_I(NFC_WORKER_TAG, "Received PDOL");
  417. } else {
  418. FURI_LOG_E(NFC_WORKER_TAG, "Error in 3rd data exchange: receive PDOL");
  419. furi_hal_nfc_deactivate();
  420. continue;
  421. }
  422. FURI_LOG_I(NFC_WORKER_TAG, "Transive PDOL ANS");
  423. tx_len = emv_get_proc_opt_ans(tx_buff);
  424. err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  425. if(err == ERR_NONE) {
  426. FURI_LOG_I(NFC_WORKER_TAG, "Transive PDOL ANS");
  427. } else {
  428. FURI_LOG_E(NFC_WORKER_TAG, "Error in 4rd data exchange: Transive PDOL ANS");
  429. furi_hal_nfc_deactivate();
  430. continue;
  431. }
  432. if(*rx_len != sizeof(debug_rx) || memcmp(rx_buff, debug_rx, sizeof(debug_rx))) {
  433. FURI_LOG_E(NFC_WORKER_TAG, "Failed long message test");
  434. } else {
  435. FURI_LOG_I(NFC_WORKER_TAG, "Correct debug message received");
  436. tx_len = sizeof(debug_tx);
  437. err = furi_hal_nfc_data_exchange(
  438. (uint8_t*)debug_tx, tx_len, &rx_buff, &rx_len, false);
  439. if(err == ERR_NONE) {
  440. FURI_LOG_I(NFC_WORKER_TAG, "Transive Debug message");
  441. }
  442. }
  443. furi_hal_nfc_deactivate();
  444. } else {
  445. FURI_LOG_W(NFC_WORKER_TAG, "Can't find reader");
  446. }
  447. osDelay(20);
  448. }
  449. }
  450. void nfc_worker_read_mifare_ul(NfcWorker* nfc_worker) {
  451. ReturnCode err;
  452. rfalNfcDevice* dev_list;
  453. uint8_t dev_cnt = 0;
  454. uint8_t tx_buff[255] = {};
  455. uint16_t tx_len = 0;
  456. uint8_t* rx_buff;
  457. uint16_t* rx_len;
  458. MifareUlDevice mf_ul_read;
  459. NfcDeviceData* result = nfc_worker->dev_data;
  460. while(nfc_worker->state == NfcWorkerStateReadMifareUl) {
  461. furi_hal_nfc_deactivate();
  462. memset(&mf_ul_read, 0, sizeof(mf_ul_read));
  463. if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 300, false)) {
  464. if(dev_list[0].type == RFAL_NFC_LISTEN_TYPE_NFCA &&
  465. mf_ul_check_card_type(
  466. dev_list[0].dev.nfca.sensRes.anticollisionInfo,
  467. dev_list[0].dev.nfca.sensRes.platformInfo,
  468. dev_list[0].dev.nfca.selRes.sak)) {
  469. // Get Mifare Ultralight version
  470. FURI_LOG_I(NFC_WORKER_TAG, "Found Mifare Ultralight tag. Reading tag version");
  471. tx_len = mf_ul_prepare_get_version(tx_buff);
  472. err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  473. if(err == ERR_NONE) {
  474. mf_ul_parse_get_version_response(rx_buff, &mf_ul_read);
  475. FURI_LOG_I(
  476. NFC_WORKER_TAG,
  477. "Mifare Ultralight Type: %d, Pages: %d",
  478. mf_ul_read.type,
  479. mf_ul_read.pages_to_read);
  480. } else if(err == ERR_TIMEOUT) {
  481. FURI_LOG_W(
  482. NFC_WORKER_TAG,
  483. "Card doesn't respond to GET VERSION command. Setting default read parameters");
  484. err = ERR_NONE;
  485. mf_ul_set_default_version(&mf_ul_read);
  486. // Reinit device
  487. furi_hal_nfc_deactivate();
  488. if(!furi_hal_nfc_detect(&dev_list, &dev_cnt, 300, false)) {
  489. FURI_LOG_E(NFC_WORKER_TAG, "Lost connection. Restarting search");
  490. continue;
  491. }
  492. } else {
  493. FURI_LOG_E(
  494. NFC_WORKER_TAG,
  495. "Error getting Mifare Ultralight version. Error code: %d",
  496. err);
  497. continue;
  498. }
  499. if(mf_ul_read.support_fast_read) {
  500. FURI_LOG_I(NFC_WORKER_TAG, "Reading pages ...");
  501. tx_len = mf_ul_prepare_fast_read(tx_buff, 0x00, mf_ul_read.pages_to_read - 1);
  502. if(furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false)) {
  503. FURI_LOG_E(NFC_WORKER_TAG, "Failed reading pages");
  504. continue;
  505. } else {
  506. mf_ul_parse_fast_read_response(
  507. rx_buff, 0x00, mf_ul_read.pages_to_read - 1, &mf_ul_read);
  508. }
  509. FURI_LOG_I(NFC_WORKER_TAG, "Reading signature ...");
  510. tx_len = mf_ul_prepare_read_signature(tx_buff);
  511. if(furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false)) {
  512. FURI_LOG_W(NFC_WORKER_TAG, "Failed reading signature");
  513. memset(mf_ul_read.data.signature, 0, sizeof(mf_ul_read.data.signature));
  514. } else {
  515. mf_ul_parse_read_signature_response(rx_buff, &mf_ul_read);
  516. }
  517. FURI_LOG_I(NFC_WORKER_TAG, "Reading 3 counters ...");
  518. for(uint8_t i = 0; i < 3; i++) {
  519. tx_len = mf_ul_prepare_read_cnt(tx_buff, i);
  520. if(furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false)) {
  521. FURI_LOG_W(NFC_WORKER_TAG, "Failed reading Counter %d", i);
  522. mf_ul_read.data.counter[i] = 0;
  523. } else {
  524. mf_ul_parse_read_cnt_response(rx_buff, i, &mf_ul_read);
  525. }
  526. }
  527. FURI_LOG_I(NFC_WORKER_TAG, "Checking tearing flags ...");
  528. for(uint8_t i = 0; i < 3; i++) {
  529. tx_len = mf_ul_prepare_check_tearing(tx_buff, i);
  530. if(furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false)) {
  531. FURI_LOG_E(NFC_WORKER_TAG, "Error checking tearing flag %d", i);
  532. mf_ul_read.data.tearing[i] = MF_UL_TEARING_FLAG_DEFAULT;
  533. } else {
  534. mf_ul_parse_check_tearing_response(rx_buff, i, &mf_ul_read);
  535. }
  536. }
  537. } else {
  538. // READ card with READ command (4 pages at a time)
  539. for(uint8_t page = 0; page < mf_ul_read.pages_to_read; page += 4) {
  540. FURI_LOG_I(NFC_WORKER_TAG, "Reading pages %d - %d ...", page, page + 3);
  541. tx_len = mf_ul_prepare_read(tx_buff, page);
  542. if(furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false)) {
  543. FURI_LOG_E(
  544. NFC_WORKER_TAG, "Read pages %d - %d failed", page, page + 3);
  545. continue;
  546. } else {
  547. mf_ul_parse_read_response(rx_buff, page, &mf_ul_read);
  548. }
  549. }
  550. }
  551. // Fill result data
  552. result->nfc_data.uid_len = dev_list[0].dev.nfca.nfcId1Len;
  553. result->nfc_data.atqa[0] = dev_list[0].dev.nfca.sensRes.anticollisionInfo;
  554. result->nfc_data.atqa[1] = dev_list[0].dev.nfca.sensRes.platformInfo;
  555. result->nfc_data.sak = dev_list[0].dev.nfca.selRes.sak;
  556. result->nfc_data.protocol = NfcDeviceProtocolMifareUl;
  557. memcpy(
  558. result->nfc_data.uid, dev_list[0].dev.nfca.nfcId1, result->nfc_data.uid_len);
  559. result->mf_ul_data = mf_ul_read.data;
  560. // Notify caller and exit
  561. if(nfc_worker->callback) {
  562. nfc_worker->callback(nfc_worker->context);
  563. }
  564. break;
  565. } else {
  566. FURI_LOG_W(NFC_WORKER_TAG, "Tag does not support Mifare Ultralight");
  567. }
  568. } else {
  569. FURI_LOG_W(NFC_WORKER_TAG, "Can't find any tags");
  570. }
  571. osDelay(100);
  572. }
  573. }
  574. void nfc_worker_emulate_mifare_ul(NfcWorker* nfc_worker) {
  575. ReturnCode err;
  576. uint8_t tx_buff[255] = {};
  577. uint16_t tx_len = 0;
  578. uint8_t* rx_buff;
  579. uint16_t* rx_len;
  580. NfcDeviceData* data = nfc_worker->dev_data;
  581. MifareUlDevice mf_ul_emulate;
  582. // Setup emulation parameters from mifare ultralight data structure
  583. mf_ul_prepare_emulation(&mf_ul_emulate, &data->mf_ul_data);
  584. while(nfc_worker->state == NfcWorkerStateEmulateMifareUl) {
  585. if(furi_hal_nfc_listen(
  586. data->nfc_data.uid,
  587. data->nfc_data.uid_len,
  588. data->nfc_data.atqa,
  589. data->nfc_data.sak,
  590. true,
  591. 200)) {
  592. FURI_LOG_D(NFC_WORKER_TAG, "Anticollision passed");
  593. if(furi_hal_nfc_get_first_frame(&rx_buff, &rx_len)) {
  594. // Data exchange loop
  595. while(nfc_worker->state == NfcWorkerStateEmulateMifareUl) {
  596. tx_len = mf_ul_prepare_emulation_response(
  597. rx_buff, *rx_len, tx_buff, &mf_ul_emulate);
  598. if(tx_len > 0) {
  599. err =
  600. furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  601. if(err == ERR_NONE) {
  602. continue;
  603. } else {
  604. FURI_LOG_E(NFC_WORKER_TAG, "Communication error: %d", err);
  605. break;
  606. }
  607. } else {
  608. FURI_LOG_W(NFC_WORKER_TAG, "Not valid command: %02X", rx_buff[0]);
  609. furi_hal_nfc_deactivate();
  610. break;
  611. }
  612. }
  613. } else {
  614. FURI_LOG_W(NFC_WORKER_TAG, "Error in 1st data exchange");
  615. furi_hal_nfc_deactivate();
  616. }
  617. }
  618. // Check if data was modified
  619. if(mf_ul_emulate.data_changed) {
  620. nfc_worker->dev_data->mf_ul_data = mf_ul_emulate.data;
  621. if(nfc_worker->callback) {
  622. nfc_worker->callback(nfc_worker->context);
  623. }
  624. }
  625. FURI_LOG_W(NFC_WORKER_TAG, "Can't find reader");
  626. osThreadYield();
  627. }
  628. }
  629. void nfc_worker_field(NfcWorker* nfc_worker) {
  630. furi_hal_nfc_field_on();
  631. while(nfc_worker->state == NfcWorkerStateField) {
  632. osDelay(50);
  633. }
  634. furi_hal_nfc_field_off();
  635. }