nfc_worker.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. NfcDeviceCommomData* 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. NfcDeviceCommomData* 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, 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. // Notify caller and exit
  307. if(nfc_worker->callback) {
  308. nfc_worker->callback(nfc_worker->context);
  309. }
  310. break;
  311. } else {
  312. FURI_LOG_E(NFC_WORKER_TAG, "Can't read card number");
  313. }
  314. furi_hal_nfc_deactivate();
  315. }
  316. } else {
  317. // Can't find EMV card
  318. FURI_LOG_W(NFC_WORKER_TAG, "Card doesn't support EMV");
  319. furi_hal_nfc_deactivate();
  320. }
  321. } else {
  322. // Can't find EMV card
  323. FURI_LOG_W(NFC_WORKER_TAG, "Can't find any cards");
  324. furi_hal_nfc_deactivate();
  325. }
  326. osDelay(20);
  327. }
  328. }
  329. void nfc_worker_emulate_apdu(NfcWorker* nfc_worker) {
  330. ReturnCode err;
  331. uint8_t tx_buff[255] = {};
  332. uint16_t tx_len = 0;
  333. uint8_t* rx_buff;
  334. uint16_t* rx_len;
  335. NfcDeviceCommomData params = {
  336. .uid = {0xCF, 0x72, 0xd4, 0x40},
  337. .uid_len = 4,
  338. .atqa = {0x00, 0x04},
  339. .sak = 0x20,
  340. .device = NfcDeviceNfca,
  341. .protocol = NfcDeviceProtocolEMV,
  342. };
  343. // Test RX data
  344. const uint8_t debug_rx[] = {
  345. 0xba, 0x0b, 0xba, 0xba, 0x20, 0x00, 0x02, 0x28, 0xde, 0xad, 0xbe, 0xef, 0x00, 0xca, 0xca,
  346. 0xca, 0xfe, 0xfa, 0xce, 0x14, 0x88, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  347. 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0xba,
  348. 0x0b, 0xba, 0xba, 0x20, 0x00, 0x02, 0x28, 0xde, 0xad, 0xbe, 0xef, 0x00, 0xca, 0xca, 0xca,
  349. 0xfe, 0xfa, 0xce, 0x14, 0x88, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99,
  350. 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0xba, 0x0b,
  351. 0xba, 0xba, 0x20, 0x00, 0x02, 0x28, 0xde, 0xad, 0xbe, 0xef, 0x00, 0xca, 0xca, 0xca, 0xfe,
  352. 0xfa, 0xce, 0x14, 0x88, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa,
  353. 0xbb, 0xcc, 0xdd, 0xee, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0xba, 0x0b, 0xba,
  354. 0xba, 0x20, 0x00, 0x02, 0x28, 0xde, 0xad, 0xbe, 0xef, 0x00, 0xca, 0xca, 0xca, 0xfe, 0xfa,
  355. 0xce, 0x14, 0x88, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb,
  356. 0xcc, 0xdd, 0xee, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0xba, 0x0b, 0xba, 0xba,
  357. 0x20, 0x00, 0x02, 0x28, 0xde, 0xad, 0xbe, 0xef, 0x00, 0xca, 0xca, 0xca, 0xfe, 0xfa, 0xce,
  358. 0x14, 0x88, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc,
  359. 0xdd, 0xee, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0xba, 0x0b, 0xba, 0xba, 0x20,
  360. 0x00, 0x02, 0x28, 0xde, 0xad, 0xbe, 0xef, 0x00, 0xca, 0xca, 0xca, 0xfe, 0xfa, 0xce, 0x14,
  361. 0x88, 0x00};
  362. // Test TX data
  363. const uint8_t debug_tx[] = {
  364. 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32,
  365. 0x10, 0x14, 0x88, 0x02, 0x28, 0x00, 0x00, 0xca, 0xca, 0x00, 0xc0, 0xc0, 0x00, 0xde, 0xad,
  366. 0xbe, 0xef, 0xce, 0xee, 0xec, 0xca, 0xfe, 0xba, 0xba, 0xb0, 0xb0, 0xac, 0xdc, 0x11, 0x12,
  367. 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  368. 0x14, 0x88, 0x02, 0x28, 0x00, 0x00, 0xca, 0xca, 0x00, 0xc0, 0xc0, 0x00, 0xde, 0xad, 0xbe,
  369. 0xef, 0xce, 0xee, 0xec, 0xca, 0xfe, 0xba, 0xba, 0xb0, 0xb0, 0xac, 0xdc, 0x11, 0x12, 0x34,
  370. 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x14,
  371. 0x88, 0x02, 0x28, 0x00, 0x00, 0xca, 0xca, 0x00, 0xc0, 0xc0, 0x00, 0xde, 0xad, 0xbe, 0xef,
  372. 0xce, 0xee, 0xec, 0xca, 0xfe, 0xba, 0xba, 0xb0, 0xb0, 0xac, 0xdc, 0x11, 0x12, 0x34, 0x56,
  373. 0x78, 0x9a, 0xbc, 0xde, 0xff, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x14, 0x88,
  374. 0x02, 0x28, 0x00, 0x00, 0xca, 0xca, 0x00, 0xc0, 0xc0, 0x00, 0xde, 0xad, 0xbe, 0xef, 0xce,
  375. 0xee, 0xec, 0xca, 0xfe, 0xba, 0xba, 0xb0, 0xb0, 0xac, 0xdc, 0x11, 0x12, 0x34, 0x56, 0x78,
  376. 0x9a, 0xbc, 0xde, 0xff, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x14, 0x88, 0x02,
  377. 0x28, 0x00, 0x00, 0xca, 0xca, 0x00, 0xc0, 0xc0, 0x00, 0xde, 0xad, 0xbe, 0xef, 0xce, 0xee,
  378. 0xec, 0xca, 0xfe, 0xba, 0xba, 0xb0, 0xb0, 0xac, 0xdc, 0x11, 0x12, 0x34, 0x56, 0x78, 0x9a,
  379. 0xbc, 0xde, 0xff, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x14, 0x88, 0x02, 0x28,
  380. 0x00, 0x00};
  381. while(nfc_worker->state == NfcWorkerStateEmulateApdu) {
  382. if(furi_hal_nfc_listen(params.uid, params.uid_len, params.atqa, params.sak, 300)) {
  383. FURI_LOG_I(NFC_WORKER_TAG, "POS terminal detected");
  384. // Read data from POS terminal
  385. err = furi_hal_nfc_data_exchange(NULL, 0, &rx_buff, &rx_len, false);
  386. if(err == ERR_NONE) {
  387. FURI_LOG_I(NFC_WORKER_TAG, "Received Select PPSE");
  388. } else {
  389. FURI_LOG_E(NFC_WORKER_TAG, "Error in 1st data exchange: select PPSE");
  390. furi_hal_nfc_deactivate();
  391. continue;
  392. }
  393. FURI_LOG_I(NFC_WORKER_TAG, "Transive SELECT PPSE ANS");
  394. tx_len = emv_select_ppse_ans(tx_buff);
  395. err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  396. if(err == ERR_NONE) {
  397. FURI_LOG_I(NFC_WORKER_TAG, "Received Select APP");
  398. } else {
  399. FURI_LOG_E(NFC_WORKER_TAG, "Error in 2nd data exchange: select APP");
  400. furi_hal_nfc_deactivate();
  401. continue;
  402. }
  403. FURI_LOG_I(NFC_WORKER_TAG, "Transive SELECT APP ANS");
  404. tx_len = emv_select_app_ans(tx_buff);
  405. err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  406. if(err == ERR_NONE) {
  407. FURI_LOG_I(NFC_WORKER_TAG, "Received PDOL");
  408. } else {
  409. FURI_LOG_E(NFC_WORKER_TAG, "Error in 3rd data exchange: receive PDOL");
  410. furi_hal_nfc_deactivate();
  411. continue;
  412. }
  413. FURI_LOG_I(NFC_WORKER_TAG, "Transive PDOL ANS");
  414. tx_len = emv_get_proc_opt_ans(tx_buff);
  415. err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  416. if(err == ERR_NONE) {
  417. FURI_LOG_I(NFC_WORKER_TAG, "Transive PDOL ANS");
  418. } else {
  419. FURI_LOG_E(NFC_WORKER_TAG, "Error in 4rd data exchange: Transive PDOL ANS");
  420. furi_hal_nfc_deactivate();
  421. continue;
  422. }
  423. if(*rx_len != sizeof(debug_rx) || memcmp(rx_buff, debug_rx, sizeof(debug_rx))) {
  424. FURI_LOG_E(NFC_WORKER_TAG, "Failed long message test");
  425. } else {
  426. FURI_LOG_I(NFC_WORKER_TAG, "Correct debug message received");
  427. tx_len = sizeof(debug_tx);
  428. err = furi_hal_nfc_data_exchange(
  429. (uint8_t*)debug_tx, tx_len, &rx_buff, &rx_len, false);
  430. if(err == ERR_NONE) {
  431. FURI_LOG_I(NFC_WORKER_TAG, "Transive Debug message");
  432. }
  433. }
  434. furi_hal_nfc_deactivate();
  435. } else {
  436. FURI_LOG_W(NFC_WORKER_TAG, "Can't find reader");
  437. }
  438. osDelay(20);
  439. }
  440. }
  441. void nfc_worker_read_mifare_ul(NfcWorker* nfc_worker) {
  442. ReturnCode err;
  443. rfalNfcDevice* dev_list;
  444. uint8_t dev_cnt = 0;
  445. uint8_t tx_buff[255] = {};
  446. uint16_t tx_len = 0;
  447. uint8_t* rx_buff;
  448. uint16_t* rx_len;
  449. MifareUlDevice mf_ul_read;
  450. NfcDeviceData* result = nfc_worker->dev_data;
  451. while(nfc_worker->state == NfcWorkerStateReadMifareUl) {
  452. furi_hal_nfc_deactivate();
  453. memset(&mf_ul_read, 0, sizeof(mf_ul_read));
  454. if(furi_hal_nfc_detect(&dev_list, &dev_cnt, 300, false)) {
  455. if(dev_list[0].type == RFAL_NFC_LISTEN_TYPE_NFCA &&
  456. mf_ul_check_card_type(
  457. dev_list[0].dev.nfca.sensRes.anticollisionInfo,
  458. dev_list[0].dev.nfca.sensRes.platformInfo,
  459. dev_list[0].dev.nfca.selRes.sak)) {
  460. // Get Mifare Ultralight version
  461. FURI_LOG_I(NFC_WORKER_TAG, "Found Mifare Ultralight tag. Reading tag version");
  462. tx_len = mf_ul_prepare_get_version(tx_buff);
  463. err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  464. if(err == ERR_NONE) {
  465. mf_ul_parse_get_version_response(rx_buff, &mf_ul_read);
  466. FURI_LOG_I(
  467. NFC_WORKER_TAG,
  468. "Mifare Ultralight Type: %d, Pages: %d",
  469. mf_ul_read.type,
  470. mf_ul_read.pages_to_read);
  471. } else if(err == ERR_TIMEOUT) {
  472. FURI_LOG_W(
  473. NFC_WORKER_TAG,
  474. "Card doesn't respond to GET VERSION command. Setting default read parameters");
  475. err = ERR_NONE;
  476. mf_ul_set_default_version(&mf_ul_read);
  477. // Reinit device
  478. furi_hal_nfc_deactivate();
  479. if(!furi_hal_nfc_detect(&dev_list, &dev_cnt, 300, false)) {
  480. FURI_LOG_E(NFC_WORKER_TAG, "Lost connection. Restarting search");
  481. continue;
  482. }
  483. } else {
  484. FURI_LOG_E(
  485. NFC_WORKER_TAG,
  486. "Error getting Mifare Ultralight version. Error code: %d",
  487. err);
  488. continue;
  489. }
  490. if(mf_ul_read.support_fast_read) {
  491. FURI_LOG_I(NFC_WORKER_TAG, "Reading pages ...");
  492. tx_len = mf_ul_prepare_fast_read(tx_buff, 0x00, mf_ul_read.pages_to_read - 1);
  493. if(furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false)) {
  494. FURI_LOG_E(NFC_WORKER_TAG, "Failed reading pages");
  495. continue;
  496. } else {
  497. mf_ul_parse_fast_read_response(
  498. rx_buff, 0x00, mf_ul_read.pages_to_read - 1, &mf_ul_read);
  499. }
  500. FURI_LOG_I(NFC_WORKER_TAG, "Reading signature ...");
  501. tx_len = mf_ul_prepare_read_signature(tx_buff);
  502. if(furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false)) {
  503. FURI_LOG_W(NFC_WORKER_TAG, "Failed reading signature");
  504. memset(mf_ul_read.data.signature, 0, sizeof(mf_ul_read.data.signature));
  505. } else {
  506. mf_ul_parse_read_signature_response(rx_buff, &mf_ul_read);
  507. }
  508. FURI_LOG_I(NFC_WORKER_TAG, "Reading 3 counters ...");
  509. for(uint8_t i = 0; i < 3; i++) {
  510. tx_len = mf_ul_prepare_read_cnt(tx_buff, i);
  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 Counter %d", i);
  513. mf_ul_read.data.counter[i] = 0;
  514. } else {
  515. mf_ul_parse_read_cnt_response(rx_buff, i, &mf_ul_read);
  516. }
  517. }
  518. FURI_LOG_I(NFC_WORKER_TAG, "Checking tearing flags ...");
  519. for(uint8_t i = 0; i < 3; i++) {
  520. tx_len = mf_ul_prepare_check_tearing(tx_buff, i);
  521. if(furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false)) {
  522. FURI_LOG_E(NFC_WORKER_TAG, "Error checking tearing flag %d", i);
  523. mf_ul_read.data.tearing[i] = MF_UL_TEARING_FLAG_DEFAULT;
  524. } else {
  525. mf_ul_parse_check_tearing_response(rx_buff, i, &mf_ul_read);
  526. }
  527. }
  528. } else {
  529. // READ card with READ command (4 pages at a time)
  530. for(uint8_t page = 0; page < mf_ul_read.pages_to_read; page += 4) {
  531. FURI_LOG_I(NFC_WORKER_TAG, "Reading pages %d - %d ...", page, page + 3);
  532. tx_len = mf_ul_prepare_read(tx_buff, page);
  533. if(furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false)) {
  534. FURI_LOG_E(
  535. NFC_WORKER_TAG, "Read pages %d - %d failed", page, page + 3);
  536. continue;
  537. } else {
  538. mf_ul_parse_read_response(rx_buff, page, &mf_ul_read);
  539. }
  540. }
  541. }
  542. // Fill result data
  543. result->nfc_data.uid_len = dev_list[0].dev.nfca.nfcId1Len;
  544. result->nfc_data.atqa[0] = dev_list[0].dev.nfca.sensRes.anticollisionInfo;
  545. result->nfc_data.atqa[1] = dev_list[0].dev.nfca.sensRes.platformInfo;
  546. result->nfc_data.sak = dev_list[0].dev.nfca.selRes.sak;
  547. result->nfc_data.protocol = NfcDeviceProtocolMifareUl;
  548. memcpy(
  549. result->nfc_data.uid, dev_list[0].dev.nfca.nfcId1, result->nfc_data.uid_len);
  550. result->mf_ul_data = mf_ul_read.data;
  551. // Notify caller and exit
  552. if(nfc_worker->callback) {
  553. nfc_worker->callback(nfc_worker->context);
  554. }
  555. break;
  556. } else {
  557. FURI_LOG_W(NFC_WORKER_TAG, "Tag does not support Mifare Ultralight");
  558. }
  559. } else {
  560. FURI_LOG_W(NFC_WORKER_TAG, "Can't find any tags");
  561. }
  562. osDelay(100);
  563. }
  564. }
  565. void nfc_worker_emulate_mifare_ul(NfcWorker* nfc_worker) {
  566. ReturnCode err;
  567. uint8_t tx_buff[255] = {};
  568. uint16_t tx_len = 0;
  569. uint8_t* rx_buff;
  570. uint16_t* rx_len;
  571. NfcDeviceData* data = nfc_worker->dev_data;
  572. while(nfc_worker->state == NfcWorkerStateEmulateMifareUl) {
  573. if(furi_hal_nfc_listen(
  574. data->nfc_data.uid,
  575. data->nfc_data.uid_len,
  576. data->nfc_data.atqa,
  577. data->nfc_data.sak,
  578. 1000)) {
  579. FURI_LOG_I(NFC_WORKER_TAG, "Hello my dudes");
  580. // Prepare version answer
  581. tx_len = sizeof(data->mf_ul_data.version);
  582. memcpy(tx_buff, &data->mf_ul_data.version, tx_len);
  583. err = furi_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
  584. if(err == ERR_NONE) {
  585. FURI_LOG_I(NFC_WORKER_TAG, "Received 1st message:");
  586. for(uint16_t i = 0; i < *rx_len; i++) {
  587. printf("%02X ", rx_buff[i]);
  588. }
  589. printf("\r\n");
  590. } else {
  591. FURI_LOG_E(NFC_WORKER_TAG, "Error in 1st data exchange: select PPSE");
  592. furi_hal_nfc_deactivate();
  593. continue;
  594. }
  595. }
  596. FURI_LOG_W(NFC_WORKER_TAG, "Hello my dudes");
  597. osDelay(10);
  598. }
  599. }
  600. void nfc_worker_field(NfcWorker* nfc_worker) {
  601. furi_hal_nfc_field_on();
  602. while(nfc_worker->state == NfcWorkerStateField) {
  603. osDelay(50);
  604. }
  605. furi_hal_nfc_field_off();
  606. }