[FL-1039] Emv emulation (#491)
* nfc: add emulate emv to submenu * api-hal-nfc: add emv emulation
This commit is contained in:
@@ -27,8 +27,10 @@ void nfc_menu_callback(void* context, uint32_t index) {
|
||||
} else if(index == 1) {
|
||||
message.type = NfcMessageTypeReadEMV;
|
||||
} else if(index == 2) {
|
||||
message.type = NfcMessageTypeEmulate;
|
||||
message.type = NfcMessageTypeEmulateEMV;
|
||||
} else if(index == 3) {
|
||||
message.type = NfcMessageTypeEmulate;
|
||||
} else if(index == 4) {
|
||||
message.type = NfcMessageTypeField;
|
||||
}
|
||||
furi_check(osMessageQueuePut(message_queue, &message, 0, osWaitForever) == osOK);
|
||||
@@ -52,8 +54,9 @@ Nfc* nfc_alloc() {
|
||||
nfc->submenu = submenu_alloc();
|
||||
submenu_add_item(nfc->submenu, "Detect", 0, nfc_menu_callback, nfc);
|
||||
submenu_add_item(nfc->submenu, "Read EMV", 1, nfc_menu_callback, nfc);
|
||||
submenu_add_item(nfc->submenu, "Emulate", 2, nfc_menu_callback, nfc);
|
||||
submenu_add_item(nfc->submenu, "Field", 3, nfc_menu_callback, nfc);
|
||||
submenu_add_item(nfc->submenu, "Emulate EMV", 2, nfc_menu_callback, nfc);
|
||||
submenu_add_item(nfc->submenu, "Emulate", 3, nfc_menu_callback, nfc);
|
||||
submenu_add_item(nfc->submenu, "Field", 4, nfc_menu_callback, nfc);
|
||||
View* submenu_view = submenu_get_view(nfc->submenu);
|
||||
view_set_previous_callback(submenu_view, nfc_view_exit);
|
||||
view_dispatcher_add_view(nfc->view_dispatcher, NfcViewMenu, submenu_view);
|
||||
@@ -74,6 +77,13 @@ Nfc* nfc_alloc() {
|
||||
view_allocate_model(nfc->view_read_emv, ViewModelTypeLocking, sizeof(NfcViewReadModel));
|
||||
view_dispatcher_add_view(nfc->view_dispatcher, NfcViewReadEmv, nfc->view_read_emv);
|
||||
|
||||
// Emulate EMV
|
||||
nfc->view_emulate_emv = view_alloc();
|
||||
view_set_context(nfc->view_emulate_emv, nfc);
|
||||
view_set_draw_callback(nfc->view_emulate_emv, nfc_view_emulate_emv_draw);
|
||||
view_set_previous_callback(nfc->view_emulate_emv, nfc_view_stop);
|
||||
view_dispatcher_add_view(nfc->view_dispatcher, NfcViewEmulateEMV, nfc->view_emulate_emv);
|
||||
|
||||
// Emulate
|
||||
nfc->view_emulate = view_alloc();
|
||||
view_set_context(nfc->view_emulate, nfc);
|
||||
@@ -118,6 +128,14 @@ void nfc_free(Nfc* nfc) {
|
||||
view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewRead);
|
||||
view_free(nfc->view_detect);
|
||||
|
||||
// Read EMV
|
||||
view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewReadEmv);
|
||||
view_free(nfc->view_read_emv);
|
||||
|
||||
// Emulate EMV
|
||||
view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewEmulateEMV);
|
||||
view_free(nfc->view_emulate_emv);
|
||||
|
||||
// Emulate
|
||||
view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewEmulate);
|
||||
view_free(nfc->view_emulate);
|
||||
@@ -214,6 +232,8 @@ int32_t nfc_task(void* p) {
|
||||
return true;
|
||||
});
|
||||
nfc_start(nfc, NfcViewReadEmv, NfcWorkerStateReadEMV);
|
||||
} else if(message.type == NfcMessageTypeEmulateEMV) {
|
||||
nfc_start(nfc, NfcViewEmulateEMV, NfcWorkerStateEmulateEMV);
|
||||
} else if(message.type == NfcMessageTypeEmulate) {
|
||||
nfc_start(nfc, NfcViewEmulate, NfcWorkerStateEmulate);
|
||||
} else if(message.type == NfcMessageTypeField) {
|
||||
|
@@ -28,6 +28,7 @@ struct Nfc {
|
||||
|
||||
View* view_detect;
|
||||
View* view_read_emv;
|
||||
View* view_emulate_emv;
|
||||
View* view_emulate;
|
||||
View* view_field;
|
||||
View* view_cli;
|
||||
|
@@ -72,6 +72,7 @@ typedef enum {
|
||||
// Main worker states
|
||||
NfcWorkerStatePoll,
|
||||
NfcWorkerStateReadEMV,
|
||||
NfcWorkerStateEmulateEMV,
|
||||
NfcWorkerStateEmulate,
|
||||
NfcWorkerStateField,
|
||||
// Transition
|
||||
@@ -81,6 +82,7 @@ typedef enum {
|
||||
typedef enum {
|
||||
NfcMessageTypeDetect,
|
||||
NfcMessageTypeReadEMV,
|
||||
NfcMessageTypeEmulateEMV,
|
||||
NfcMessageTypeEmulate,
|
||||
NfcMessageTypeField,
|
||||
NfcMessageTypeStop,
|
||||
|
@@ -133,6 +133,12 @@ void nfc_view_read_emv_draw(Canvas* canvas, void* model) {
|
||||
}
|
||||
}
|
||||
|
||||
void nfc_view_emulate_emv_draw(Canvas* canvas, void* model) {
|
||||
canvas_clear(canvas);
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
canvas_draw_str(canvas, 0, 12, "Emulating EMV");
|
||||
}
|
||||
|
||||
void nfc_view_emulate_draw(Canvas* canvas, void* model) {
|
||||
canvas_clear(canvas);
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
|
@@ -11,6 +11,7 @@ typedef enum {
|
||||
NfcViewMenu,
|
||||
NfcViewRead,
|
||||
NfcViewReadEmv,
|
||||
NfcViewEmulateEMV,
|
||||
NfcViewEmulate,
|
||||
NfcViewField,
|
||||
NfcViewError,
|
||||
@@ -28,6 +29,7 @@ void nfc_view_read_nfcf_draw(Canvas* canvas, NfcViewReadModel* model);
|
||||
void nfc_view_read_nfcv_draw(Canvas* canvas, NfcViewReadModel* model);
|
||||
void nfc_view_read_emv_draw(Canvas* canvas, void* model);
|
||||
|
||||
void nfc_view_emulate_emv_draw(Canvas* canvas, void* model);
|
||||
void nfc_view_emulate_draw(Canvas* canvas, void* model);
|
||||
|
||||
void nfc_view_field_draw(Canvas* canvas, void* model);
|
||||
|
72
applications/nfc/nfc_worker.c
Executable file → Normal file
72
applications/nfc/nfc_worker.c
Executable file → Normal file
@@ -64,6 +64,8 @@ void nfc_worker_task(void* context) {
|
||||
nfc_worker_poll(nfc_worker);
|
||||
} else if(nfc_worker->state == NfcWorkerStateReadEMV) {
|
||||
nfc_worker_read_emv(nfc_worker);
|
||||
} else if(nfc_worker->state == NfcWorkerStateEmulateEMV) {
|
||||
nfc_worker_emulate_emv(nfc_worker);
|
||||
} else if(nfc_worker->state == NfcWorkerStateEmulate) {
|
||||
nfc_worker_emulate(nfc_worker);
|
||||
} else if(nfc_worker->state == NfcWorkerStateField) {
|
||||
@@ -77,7 +79,6 @@ void nfc_worker_task(void* context) {
|
||||
void nfc_worker_read_emv(NfcWorker* nfc_worker) {
|
||||
ReturnCode err;
|
||||
rfalNfcDevice* dev_list;
|
||||
rfalNfcDevice* dev_active;
|
||||
EmvApplication emv_app = {};
|
||||
uint8_t dev_cnt = 0;
|
||||
uint8_t tx_buff[255] = {};
|
||||
@@ -94,11 +95,9 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) {
|
||||
if(api_hal_nfc_detect(&dev_list, &dev_cnt, 100, false)) {
|
||||
// Card was found. Check that it supports EMV
|
||||
if(dev_list[0].rfInterface == RFAL_NFC_INTERFACE_ISODEP) {
|
||||
dev_active = &dev_list[0];
|
||||
FURI_LOG_I(NFC_WORKER_TAG, "Send select PPSE command");
|
||||
tx_len = emv_prepare_select_ppse(tx_buff);
|
||||
err = api_hal_nfc_data_exchange(
|
||||
dev_active, tx_buff, tx_len, &rx_buff, &rx_len, false);
|
||||
err = api_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
|
||||
if(err != ERR_NONE) {
|
||||
FURI_LOG_E(NFC_WORKER_TAG, "Error during selection PPSE request: %d", err);
|
||||
message.type = NfcMessageTypeEMVNotFound;
|
||||
@@ -117,8 +116,7 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) {
|
||||
}
|
||||
FURI_LOG_I(NFC_WORKER_TAG, "Starting application ...");
|
||||
tx_len = emv_prepare_select_app(tx_buff, &emv_app);
|
||||
err = api_hal_nfc_data_exchange(
|
||||
dev_active, tx_buff, tx_len, &rx_buff, &rx_len, false);
|
||||
err = api_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
|
||||
if(err != ERR_NONE) {
|
||||
FURI_LOG_E(
|
||||
NFC_WORKER_TAG, "Error during application selection request: %d", err);
|
||||
@@ -140,8 +138,7 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) {
|
||||
}
|
||||
FURI_LOG_I(NFC_WORKER_TAG, "Starting Get Processing Options command ...");
|
||||
tx_len = emv_prepare_get_proc_opt(tx_buff, &emv_app);
|
||||
err = api_hal_nfc_data_exchange(
|
||||
dev_active, tx_buff, tx_len, &rx_buff, &rx_len, false);
|
||||
err = api_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
|
||||
if(err != ERR_NONE) {
|
||||
FURI_LOG_E(
|
||||
NFC_WORKER_TAG, "Error during Get Processing Options command: %d", err);
|
||||
@@ -171,7 +168,7 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) {
|
||||
for(uint8_t record = record_start; record <= record_end; ++record) {
|
||||
tx_len = emv_prepare_read_sfi_record(tx_buff, sfi, record);
|
||||
err = api_hal_nfc_data_exchange(
|
||||
dev_active, tx_buff, tx_len, &rx_buff, &rx_len, false);
|
||||
tx_buff, tx_len, &rx_buff, &rx_len, false);
|
||||
if(err != ERR_NONE) {
|
||||
FURI_LOG_E(
|
||||
NFC_WORKER_TAG,
|
||||
@@ -215,6 +212,61 @@ void nfc_worker_read_emv(NfcWorker* nfc_worker) {
|
||||
api_hal_nfc_deactivate();
|
||||
}
|
||||
|
||||
void nfc_worker_emulate_emv(NfcWorker* nfc_worker) {
|
||||
ReturnCode err;
|
||||
uint8_t tx_buff[255] = {};
|
||||
uint16_t tx_len = 0;
|
||||
uint8_t* rx_buff;
|
||||
uint16_t* rx_len;
|
||||
|
||||
while(nfc_worker->state == NfcWorkerStateEmulateEMV) {
|
||||
if(api_hal_nfc_listen(1000)) {
|
||||
FURI_LOG_I(NFC_WORKER_TAG, "POS terminal detected");
|
||||
// Read data from POS terminal
|
||||
err = api_hal_nfc_data_exchange(NULL, 0, &rx_buff, &rx_len, false);
|
||||
if(err == ERR_NONE) {
|
||||
FURI_LOG_I(NFC_WORKER_TAG, "Received Select PPSE");
|
||||
} else {
|
||||
FURI_LOG_E(NFC_WORKER_TAG, "Error in 1st data exchange: select PPSE");
|
||||
api_hal_nfc_deactivate();
|
||||
continue;
|
||||
}
|
||||
FURI_LOG_I(NFC_WORKER_TAG, "Transive SELECT PPSE ANS");
|
||||
tx_len = emv_select_ppse_ans(tx_buff);
|
||||
err = api_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
|
||||
if(err == ERR_NONE) {
|
||||
FURI_LOG_I(NFC_WORKER_TAG, "Received Select APP");
|
||||
} else {
|
||||
FURI_LOG_E(NFC_WORKER_TAG, "Error in 2nd data exchange: select APP");
|
||||
api_hal_nfc_deactivate();
|
||||
continue;
|
||||
}
|
||||
|
||||
FURI_LOG_I(NFC_WORKER_TAG, "Transive SELECT APP ANS");
|
||||
tx_len = emv_select_app_ans(tx_buff);
|
||||
err = api_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
|
||||
if(err == ERR_NONE) {
|
||||
FURI_LOG_I(NFC_WORKER_TAG, "Received PDOL");
|
||||
} else {
|
||||
FURI_LOG_E(NFC_WORKER_TAG, "Error in 3rd data exchange: receive PDOL");
|
||||
api_hal_nfc_deactivate();
|
||||
continue;
|
||||
}
|
||||
|
||||
FURI_LOG_I(NFC_WORKER_TAG, "Transive PDOL ANS");
|
||||
tx_len = emv_get_proc_opt_ans(tx_buff);
|
||||
err = api_hal_nfc_data_exchange(tx_buff, tx_len, &rx_buff, &rx_len, false);
|
||||
if(err == ERR_NONE) {
|
||||
FURI_LOG_I(NFC_WORKER_TAG, "Received PDOL");
|
||||
}
|
||||
api_hal_nfc_deactivate();
|
||||
} else {
|
||||
FURI_LOG_W(NFC_WORKER_TAG, "Can't find reader");
|
||||
}
|
||||
osDelay(20);
|
||||
}
|
||||
}
|
||||
|
||||
void nfc_worker_poll(NfcWorker* nfc_worker) {
|
||||
rfalNfcDevice* dev_list;
|
||||
uint8_t dev_cnt;
|
||||
@@ -249,7 +301,7 @@ void nfc_worker_poll(NfcWorker* nfc_worker) {
|
||||
furi_check(
|
||||
osMessageQueuePut(nfc_worker->message_queue, &message, 0, osWaitForever) == osOK);
|
||||
}
|
||||
osDelay(20);
|
||||
osDelay(5);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -31,6 +31,8 @@ void nfc_worker_task(void* context);
|
||||
|
||||
void nfc_worker_read_emv(NfcWorker* nfc_worker);
|
||||
|
||||
void nfc_worker_emulate_emv(NfcWorker* nfc_worker);
|
||||
|
||||
void nfc_worker_poll(NfcWorker* nfc_worker);
|
||||
|
||||
void nfc_worker_emulate(NfcWorker* nfc_worker);
|
||||
|
Reference in New Issue
Block a user