nfc_worker.c 30 KB

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