EMV protocol added

This commit is contained in:
Methodius 2024-01-11 00:48:55 +09:00
parent e7bf9b4df2
commit d0c466ccc0
No known key found for this signature in database
GPG key ID: 122FA99A00B41679
10 changed files with 1037 additions and 23 deletions

111
lib/nfc/protocols/emv/emv.c Normal file
View file

@ -0,0 +1,111 @@
//#include "emv_i.h"
#include <core/common_defines.h>
#include "protocols/emv/emv.h"
#include <furi.h>
#include <stdlib.h>
#define EMV_PROTOCOL_NAME "EMV"
const NfcDeviceBase nfc_device_emv = {
.protocol_name = EMV_PROTOCOL_NAME,
.alloc = (NfcDeviceAlloc)emv_alloc,
.free = (NfcDeviceFree)emv_free,
.reset = (NfcDeviceReset)emv_reset,
.copy = (NfcDeviceCopy)emv_copy,
.verify = (NfcDeviceVerify)emv_verify,
.load = (NfcDeviceLoad)emv_load,
.save = (NfcDeviceSave)emv_save,
.is_equal = (NfcDeviceEqual)emv_is_equal,
.get_name = (NfcDeviceGetName)emv_get_device_name,
.get_uid = (NfcDeviceGetUid)emv_get_uid,
.set_uid = (NfcDeviceSetUid)emv_set_uid,
.get_base_data = (NfcDeviceGetBaseData)emv_get_base_data,
};
EmvData* emv_alloc() {
EmvData* data = malloc(sizeof(EmvData));
data->iso14443_4a_data = iso14443_4a_alloc();
return data;
}
void emv_free(EmvData* data) {
furi_assert(data);
emv_reset(data);
iso14443_4a_free(data->iso14443_4a_data);
free(data);
}
void emv_reset(EmvData* data) {
furi_assert(data);
iso14443_4a_reset(data->iso14443_4a_data);
memset(&data->emv_application, 0, sizeof(EmvApplication));
}
void emv_copy(EmvData* destination, const EmvData* source) {
furi_assert(destination);
furi_assert(source);
emv_reset(destination);
iso14443_4a_copy(destination->iso14443_4a_data, source->iso14443_4a_data);
destination->emv_application = source->emv_application;
}
bool emv_verify(EmvData* data, const FuriString* device_type) {
UNUSED(data);
return furi_string_equal_str(device_type, EMV_PROTOCOL_NAME);
}
bool emv_load(EmvData* data, FlipperFormat* ff, uint32_t version) {
furi_assert(data);
UNUSED(data);
UNUSED(ff);
UNUSED(version);
return false;
}
bool emv_save(const EmvData* data, FlipperFormat* ff) {
furi_assert(data);
UNUSED(data);
UNUSED(ff);
return false;
}
bool emv_is_equal(const EmvData* data, const EmvData* other) {
furi_assert(data);
furi_assert(other);
return iso14443_4a_is_equal(data->iso14443_4a_data, other->iso14443_4a_data) &&
memcmp(&data->emv_application, &other->emv_application, sizeof(EmvApplication)) == 0;
}
const char* emv_get_device_name(const EmvData* data, NfcDeviceNameType name_type) {
UNUSED(data);
UNUSED(name_type);
return EMV_PROTOCOL_NAME;
}
const uint8_t* emv_get_uid(const EmvData* data, size_t* uid_len) {
furi_assert(data);
return iso14443_4a_get_uid(data->iso14443_4a_data, uid_len);
}
bool emv_set_uid(EmvData* data, const uint8_t* uid, size_t uid_len) {
furi_assert(data);
return iso14443_4a_set_uid(data->iso14443_4a_data, uid, uid_len);
}
Iso14443_4aData* emv_get_base_data(const EmvData* data) {
furi_assert(data);
return data->iso14443_4a_data;
}

100
lib/nfc/protocols/emv/emv.h Normal file
View file

@ -0,0 +1,100 @@
#pragma once
#include <lib/nfc/protocols/iso14443_4a/iso14443_4a.h>
#ifdef __cplusplus
extern "C" {
#endif
#define MAX_APDU_LEN 255
#define EMV_TAG_APP_TEMPLATE 0x61
#define EMV_TAG_AID 0x4F
#define EMV_TAG_PRIORITY 0x87
#define EMV_TAG_PDOL 0x9F38
#define EMV_TAG_CARD_NAME 0x50
#define EMV_TAG_FCI 0xBF0C
#define EMV_TAG_LOG_CTRL 0x9F4D
#define EMV_TAG_TRACK_1_EQUIV 0x56
#define EMV_TAG_TRACK_2_EQUIV 0x57
#define EMV_TAG_PAN 0x5A
#define EMV_TAG_AFL 0x94
#define EMV_TAG_EXP_DATE 0x5F24
#define EMV_TAG_COUNTRY_CODE 0x5F28
#define EMV_TAG_CURRENCY_CODE 0x9F42
#define EMV_TAG_CARDHOLDER_NAME 0x5F20
typedef struct {
uint16_t tag;
uint8_t data[];
} PDOLValue;
typedef struct {
uint8_t size;
uint8_t data[MAX_APDU_LEN];
} APDU;
typedef struct {
uint8_t priority;
uint8_t aid[16];
uint8_t aid_len;
bool app_started;
char name[32];
bool name_found;
uint8_t card_number[10];
uint8_t card_number_len;
uint8_t exp_month;
uint8_t exp_year;
uint16_t country_code;
uint16_t currency_code;
APDU pdol;
APDU afl;
} EmvApplication;
typedef enum {
EmvErrorNone = 0,
EmvErrorNotPresent,
EmvErrorProtocol,
EmvErrorTimeout,
} EmvError;
typedef struct {
Iso14443_4aData* iso14443_4a_data;
EmvApplication emv_application;
} EmvData;
extern const NfcDeviceBase nfc_device_emv;
// Virtual methods
EmvData* emv_alloc();
void emv_free(EmvData* data);
void emv_reset(EmvData* data);
void emv_copy(EmvData* data, const EmvData* other);
bool emv_verify(EmvData* data, const FuriString* device_type);
bool emv_load(EmvData* data, FlipperFormat* ff, uint32_t version);
bool emv_save(const EmvData* data, FlipperFormat* ff);
bool emv_is_equal(const EmvData* data, const EmvData* other);
const char* emv_get_device_name(const EmvData* data, NfcDeviceNameType name_type);
const uint8_t* emv_get_uid(const EmvData* data, size_t* uid_len);
bool emv_set_uid(EmvData* data, const uint8_t* uid, size_t uid_len);
Iso14443_4aData* emv_get_base_data(const EmvData* data);
// Getters and tests
const EmvApplication* emv_get_application(const EmvData* data);
#ifdef __cplusplus
}
#endif

View file

@ -0,0 +1,206 @@
#include "emv_poller_i.h"
#include <nfc/protocols/nfc_poller_base.h>
#include <furi.h>
#define TAG "EMVPoller"
// SKOLKO?????????????????????????????????????????????????????????????????
#define EMV_BUF_SIZE (512U)
#define EMV_RESULT_BUF_SIZE (512U)
typedef NfcCommand (*EmvPollerReadHandler)(EmvPoller* instance);
const EmvData* emv_poller_get_data(EmvPoller* instance) {
furi_assert(instance);
return instance->data;
}
static EmvPoller* emv_poller_alloc(Iso14443_4aPoller* iso14443_4a_poller) {
EmvPoller* instance = malloc(sizeof(EmvPoller));
instance->iso14443_4a_poller = iso14443_4a_poller;
instance->data = emv_alloc();
instance->tx_buffer = bit_buffer_alloc(EMV_BUF_SIZE);
instance->rx_buffer = bit_buffer_alloc(EMV_BUF_SIZE);
instance->input_buffer = bit_buffer_alloc(EMV_BUF_SIZE);
instance->result_buffer = bit_buffer_alloc(EMV_RESULT_BUF_SIZE);
instance->emv_event.data = &instance->emv_event_data;
instance->general_event.protocol = NfcProtocolEmv;
instance->general_event.event_data = &instance->emv_event;
instance->general_event.instance = instance;
return instance;
}
static void emv_poller_free(EmvPoller* instance) {
furi_assert(instance);
emv_free(instance->data);
bit_buffer_free(instance->tx_buffer);
bit_buffer_free(instance->rx_buffer);
bit_buffer_free(instance->input_buffer);
bit_buffer_free(instance->result_buffer);
free(instance);
}
static NfcCommand emv_poller_handler_idle(EmvPoller* instance) {
bit_buffer_reset(instance->input_buffer);
bit_buffer_reset(instance->result_buffer);
bit_buffer_reset(instance->tx_buffer);
bit_buffer_reset(instance->rx_buffer);
iso14443_4a_copy(
instance->data->iso14443_4a_data,
iso14443_4a_poller_get_data(instance->iso14443_4a_poller));
instance->state = EmvPollerStateSelectPPSE;
return NfcCommandContinue;
}
static NfcCommand emv_poller_handler_select_ppse(EmvPoller* instance) {
instance->error = emv_poller_select_ppse(instance);
if(instance->error == EmvErrorNone) {
FURI_LOG_D(TAG, "Select PPSE success");
instance->state = EmvPollerStateSelectApplication;
} else {
FURI_LOG_E(TAG, "Failed to select PPSE");
iso14443_4a_poller_halt(instance->iso14443_4a_poller);
instance->state = EmvPollerStateReadFailed;
}
return NfcCommandContinue;
}
static NfcCommand emv_poller_handler_select_application(EmvPoller* instance) {
instance->error = emv_poller_select_ppse(instance);
if(instance->error == EmvErrorNone) {
FURI_LOG_D(TAG, "Select application success");
instance->state = EmvPollerStateGetProcessingOptions;
} else {
FURI_LOG_E(TAG, "Failed to select application");
iso14443_4a_poller_halt(instance->iso14443_4a_poller);
instance->state = EmvPollerStateReadFailed;
}
return NfcCommandContinue;
}
static NfcCommand emv_poller_handler_get_processing_options(EmvPoller* instance) {
instance->error = emv_poller_get_processing_options(instance);
if(instance->error == EmvErrorNone) {
FURI_LOG_D(TAG, "Get processing options success");
instance->state = EmvPollerStateReadSuccess;
} else {
FURI_LOG_E(TAG, "Failed to get processing options");
iso14443_4a_poller_halt(instance->iso14443_4a_poller);
instance->state = EmvPollerStateReadFiles;
}
return NfcCommandContinue;
}
static NfcCommand emv_poller_handler_read_files(EmvPoller* instance) {
instance->error = emv_poller_read_files(instance);
if(instance->error == EmvErrorNone) {
FURI_LOG_D(TAG, "Read files success");
instance->state = EmvPollerStateReadSuccess;
} else {
FURI_LOG_E(TAG, "Failed to read files");
iso14443_4a_poller_halt(instance->iso14443_4a_poller);
instance->state = EmvPollerStateReadFailed;
}
return NfcCommandContinue;
}
static NfcCommand emv_poller_handler_read_fail(EmvPoller* instance) {
FURI_LOG_D(TAG, "Read failed");
iso14443_4a_poller_halt(instance->iso14443_4a_poller);
instance->emv_event.data->error = instance->error;
NfcCommand command = instance->callback(instance->general_event, instance->context);
instance->state = EmvPollerStateIdle;
return command;
}
static NfcCommand emv_poller_handler_read_success(EmvPoller* instance) {
FURI_LOG_D(TAG, "Read success.");
iso14443_4a_poller_halt(instance->iso14443_4a_poller);
instance->emv_event.type = EmvPollerEventTypeReadSuccess;
NfcCommand command = instance->callback(instance->general_event, instance->context);
return command;
}
static const EmvPollerReadHandler emv_poller_read_handler[EmvPollerStateNum] = {
[EmvPollerStateIdle] = emv_poller_handler_idle,
[EmvPollerStateSelectPPSE] = emv_poller_handler_select_ppse,
[EmvPollerStateSelectApplication] = emv_poller_handler_select_application,
[EmvPollerStateGetProcessingOptions] = emv_poller_handler_get_processing_options,
[EmvPollerStateReadFiles] = emv_poller_handler_read_files,
[EmvPollerStateReadFailed] = emv_poller_handler_read_fail,
[EmvPollerStateReadSuccess] = emv_poller_handler_read_success,
};
static void
emv_poller_set_callback(EmvPoller* instance, NfcGenericCallback callback, void* context) {
furi_assert(instance);
furi_assert(callback);
instance->callback = callback;
instance->context = context;
}
static NfcCommand emv_poller_run(NfcGenericEvent event, void* context) {
furi_assert(event.protocol == NfcProtocolIso14443_4a);
EmvPoller* instance = context;
furi_assert(instance);
furi_assert(instance->callback);
const Iso14443_4aPollerEvent* iso14443_4a_event = event.event_data;
furi_assert(iso14443_4a_event);
NfcCommand command = NfcCommandContinue;
if(iso14443_4a_event->type == Iso14443_4aPollerEventTypeReady) {
command = emv_poller_read_handler[instance->state](instance);
} else if(iso14443_4a_event->type == Iso14443_4aPollerEventTypeError) {
instance->emv_event.type = EmvPollerEventTypeReadFailed;
command = instance->callback(instance->general_event, instance->context);
}
return command;
}
static bool emv_poller_detect(NfcGenericEvent event, void* context) {
furi_assert(event.protocol == NfcProtocolIso14443_4a);
EmvPoller* instance = context;
furi_assert(instance);
const Iso14443_4aPollerEvent* iso14443_4a_event = event.event_data;
furi_assert(iso14443_4a_event);
bool protocol_detected = false;
if(iso14443_4a_event->type == Iso14443_4aPollerEventTypeReady) {
const EmvError error = emv_poller_select_ppse(instance);
protocol_detected = (error == EmvErrorNone);
}
return protocol_detected;
}
const NfcPollerBase emv_poller = {
.alloc = (NfcPollerAlloc)emv_poller_alloc,
.free = (NfcPollerFree)emv_poller_free,
.set_callback = (NfcPollerSetCallback)emv_poller_set_callback,
.run = (NfcPollerRun)emv_poller_run,
.detect = (NfcPollerDetect)emv_poller_detect,
.get_data = (NfcPollerGetData)emv_poller_get_data,
};

View file

@ -0,0 +1,51 @@
#pragma once
#include "emv.h"
#include <lib/nfc/protocols/iso14443_4a/iso14443_4a_poller.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief EmvPoller opaque type definition.
*/
typedef struct EmvPoller EmvPoller;
/**
* @brief Enumeration of possible Emv poller event types.
*/
typedef enum {
EmvPollerEventTypeReadSuccess, /**< Card was read successfully. */
EmvPollerEventTypeReadFailed, /**< Poller failed to read card. */
} EmvPollerEventType;
/**
* @brief Emv poller event data.
*/
typedef union {
EmvError error; /**< Error code indicating card reading fail reason. */
} EmvPollerEventData;
/**
* @brief Emv poller event structure.
*
* Upon emission of an event, an instance of this struct will be passed to the callback.
*/
typedef struct {
EmvPollerEventType type; /**< Type of emmitted event. */
EmvPollerEventData* data; /**< Pointer to event specific data. */
} EmvPollerEvent;
EmvError emv_poller_select_ppse(EmvPoller* instance);
EmvError emv_poller_select_application(EmvPoller* instance);
EmvError emv_poller_get_processing_options(EmvPoller* instance);
EmvError emv_poller_read_sfi_record(EmvPoller* instance, uint8_t sfi, uint8_t record_num);
EmvError emv_poller_read_files(EmvPoller* instance);
EmvError emv_poller_read(EmvPoller* instance);

View file

@ -0,0 +1,5 @@
#pragma once
#include <nfc/protocols/nfc_poller_base.h>
extern const NfcPollerBase emv_poller;

View file

@ -0,0 +1,477 @@
#include "emv_poller_i.h"
#include "protocols/emv/emv.h"
#define TAG "EMVPoller"
const PDOLValue pdol_term_info = {0x9F59, {0xC8, 0x80, 0x00}}; // Terminal transaction information
const PDOLValue pdol_term_type = {0x9F5A, {0x00}}; // Terminal transaction type
const PDOLValue pdol_merchant_type = {0x9F58, {0x01}}; // Merchant type indicator
const PDOLValue pdol_term_trans_qualifies = {
0x9F66,
{0x79, 0x00, 0x40, 0x80}}; // Terminal transaction qualifiers
const PDOLValue pdol_addtnl_term_qualifies = {
0x9F40,
{0x79, 0x00, 0x40, 0x80}}; // Terminal transaction qualifiers
const PDOLValue pdol_amount_authorise = {
0x9F02,
{0x00, 0x00, 0x00, 0x10, 0x00, 0x00}}; // Amount, authorised
const PDOLValue pdol_amount = {0x9F03, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}; // Amount
const PDOLValue pdol_country_code = {0x9F1A, {0x01, 0x24}}; // Terminal country code
const PDOLValue pdol_currency_code = {0x5F2A, {0x01, 0x24}}; // Transaction currency code
const PDOLValue pdol_term_verification = {
0x95,
{0x00, 0x00, 0x00, 0x00, 0x00}}; // Terminal verification results
const PDOLValue pdol_transaction_date = {0x9A, {0x19, 0x01, 0x01}}; // Transaction date
const PDOLValue pdol_transaction_type = {0x9C, {0x00}}; // Transaction type
const PDOLValue pdol_transaction_cert = {0x98, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; // Transaction cert
const PDOLValue pdol_unpredict_number = {0x9F37, {0x82, 0x3D, 0xDE, 0x7A}}; // Unpredictable number
const PDOLValue* const pdol_values[] = {
&pdol_term_info,
&pdol_term_type,
&pdol_merchant_type,
&pdol_term_trans_qualifies,
&pdol_addtnl_term_qualifies,
&pdol_amount_authorise,
&pdol_amount,
&pdol_country_code,
&pdol_currency_code,
&pdol_term_verification,
&pdol_transaction_date,
&pdol_transaction_type,
&pdol_transaction_cert,
&pdol_unpredict_number,
};
EmvError emv_process_error(Iso14443_4aError error) {
switch(error) {
case Iso14443_4aErrorNone:
return EmvErrorNone;
case Iso14443_4aErrorNotPresent:
return EmvErrorNotPresent;
case Iso14443_4aErrorTimeout:
return EmvErrorTimeout;
default:
return EmvErrorProtocol;
}
}
static void emv_trace(EmvPoller* instance, const char* message) {
if(furi_log_get_level() == FuriLogLevelTrace) {
FURI_LOG_T(TAG, "%s", message);
printf("TX: ");
size_t size = bit_buffer_get_size_bytes(instance->tx_buffer);
for(size_t i = 0; i < size; i++) {
printf("%02X ", bit_buffer_get_byte(instance->tx_buffer, i));
}
printf("\r\nRX: ");
size = bit_buffer_get_size_bytes(instance->rx_buffer);
for(size_t i = 0; i < size; i++) {
printf("%02X ", bit_buffer_get_byte(instance->rx_buffer, i));
}
printf("\r\n");
}
}
static uint16_t emv_prepare_pdol(APDU* dest, APDU* src) {
bool tag_found;
for(uint16_t i = 0; i < src->size; i++) {
tag_found = false;
for(uint8_t j = 0; j < sizeof(pdol_values) / sizeof(PDOLValue*); j++) {
if(src->data[i] == pdol_values[j]->tag) {
// Found tag with 1 byte length
uint8_t len = src->data[++i];
memcpy(dest->data + dest->size, pdol_values[j]->data, len);
dest->size += len;
tag_found = true;
break;
} else if(((src->data[i] << 8) | src->data[i + 1]) == pdol_values[j]->tag) {
// Found tag with 2 byte length
i += 2;
uint8_t len = src->data[i];
memcpy(dest->data + dest->size, pdol_values[j]->data, len);
dest->size += len;
tag_found = true;
break;
}
}
if(!tag_found) {
// Unknown tag, fill zeros
i += 2;
uint8_t len = src->data[i];
memset(dest->data + dest->size, 0, len);
dest->size += len;
}
}
return dest->size;
}
static bool emv_decode_response(const uint8_t* buff, uint16_t len, EmvApplication* app) {
uint16_t i = 0;
uint16_t tag = 0, first_byte = 0;
uint16_t tlen = 0;
bool success = false;
while(i < len) {
first_byte = buff[i];
if((first_byte & 31) == 31) { // 2-byte tag
tag = buff[i] << 8 | buff[i + 1];
i++;
FURI_LOG_T(TAG, " 2-byte TLV EMV tag: %x", tag);
} else {
tag = buff[i];
FURI_LOG_T(TAG, " 1-byte TLV EMV tag: %x", tag);
}
i++;
tlen = buff[i];
if((tlen & 128) == 128) { // long length value
i++;
tlen = buff[i];
FURI_LOG_T(TAG, " 2-byte TLV length: %d", tlen);
} else {
FURI_LOG_T(TAG, " 1-byte TLV length: %d", tlen);
}
i++;
if((first_byte & 32) == 32) { // "Constructed" -- contains more TLV data to parse
FURI_LOG_T(TAG, "Constructed TLV %x", tag);
if(!emv_decode_response(&buff[i], tlen, app)) {
FURI_LOG_T(TAG, "Failed to decode response for %x", tag);
// return false;
} else {
success = true;
}
} else {
switch(tag) {
case EMV_TAG_AID:
app->aid_len = tlen;
memcpy(app->aid, &buff[i], tlen);
success = true;
FURI_LOG_T(TAG, "found EMV_TAG_AID %x", tag);
break;
case EMV_TAG_PRIORITY:
memcpy(&app->priority, &buff[i], tlen);
success = true;
break;
case EMV_TAG_CARD_NAME:
memcpy(app->name, &buff[i], tlen);
app->name[tlen] = '\0';
app->name_found = true;
success = true;
FURI_LOG_T(TAG, "found EMV_TAG_CARD_NAME %x : %s", tag, app->name);
break;
case EMV_TAG_PDOL:
memcpy(app->pdol.data, &buff[i], tlen);
app->pdol.size = tlen;
success = true;
FURI_LOG_T(TAG, "found EMV_TAG_PDOL %x (len=%d)", tag, tlen);
break;
case EMV_TAG_AFL:
memcpy(app->afl.data, &buff[i], tlen);
app->afl.size = tlen;
success = true;
FURI_LOG_T(TAG, "found EMV_TAG_AFL %x (len=%d)", tag, tlen);
break;
case EMV_TAG_TRACK_1_EQUIV: {
char track_1_equiv[80];
memcpy(track_1_equiv, &buff[i], tlen);
track_1_equiv[tlen] = '\0';
success = true;
FURI_LOG_T(TAG, "found EMV_TAG_TRACK_1_EQUIV %x : %s", tag, track_1_equiv);
break;
}
case EMV_TAG_TRACK_2_EQUIV: {
// 0xD0 delimits PAN from expiry (YYMM)
for(int x = 1; x < tlen; x++) {
if(buff[i + x + 1] > 0xD0) {
memcpy(app->card_number, &buff[i], x + 1);
app->card_number_len = x + 1;
app->exp_year = (buff[i + x + 1] << 4) | (buff[i + x + 2] >> 4);
app->exp_month = (buff[i + x + 2] << 4) | (buff[i + x + 3] >> 4);
break;
}
}
// Convert 4-bit to ASCII representation
char track_2_equiv[41];
uint8_t track_2_equiv_len = 0;
for(int x = 0; x < tlen; x++) {
char top = (buff[i + x] >> 4) + '0';
char bottom = (buff[i + x] & 0x0F) + '0';
track_2_equiv[x * 2] = top;
track_2_equiv_len++;
if(top == '?') break;
track_2_equiv[x * 2 + 1] = bottom;
track_2_equiv_len++;
if(bottom == '?') break;
}
track_2_equiv[track_2_equiv_len] = '\0';
success = true;
FURI_LOG_T(TAG, "found EMV_TAG_TRACK_2_EQUIV %x : %s", tag, track_2_equiv);
break;
}
case EMV_TAG_PAN:
memcpy(app->card_number, &buff[i], tlen);
app->card_number_len = tlen;
success = true;
break;
case EMV_TAG_EXP_DATE:
app->exp_year = buff[i];
app->exp_month = buff[i + 1];
success = true;
break;
case EMV_TAG_CURRENCY_CODE:
app->currency_code = (buff[i] << 8 | buff[i + 1]);
success = true;
break;
case EMV_TAG_COUNTRY_CODE:
app->country_code = (buff[i] << 8 | buff[i + 1]);
success = true;
break;
}
}
i += tlen;
}
return success;
}
EmvError emv_poller_select_ppse(EmvPoller* instance) {
EmvError error = EmvErrorNone;
const uint8_t emv_select_ppse_cmd[] = {
0x00, 0xA4, // SELECT ppse
0x04, 0x00, // P1:By name, P2: empty
0x0e, // Lc: Data length
0x32, 0x50, 0x41, 0x59, 0x2e, 0x53, 0x59, // Data string:
0x53, 0x2e, 0x44, 0x44, 0x46, 0x30, 0x31, // 2PAY.SYS.DDF01 (PPSE)
0x00 // Le
};
bit_buffer_reset(instance->tx_buffer);
bit_buffer_reset(instance->rx_buffer);
bit_buffer_copy_bytes(instance->tx_buffer, emv_select_ppse_cmd, sizeof(emv_select_ppse_cmd));
do {
FURI_LOG_D(TAG, "Send select PPSE");
Iso14443_4aError iso14443_4a_error = iso14443_4a_poller_send_block(
instance->iso14443_4a_poller, instance->tx_buffer, instance->rx_buffer);
if(iso14443_4a_error != Iso14443_4aErrorNone) {
FURI_LOG_E(TAG, "Failed select PPSE");
error = emv_process_error(iso14443_4a_error);
break;
}
emv_trace(instance, "Select PPSE answer:");
const uint8_t* buff = bit_buffer_get_data(instance->rx_buffer);
if(!emv_decode_response(
buff,
bit_buffer_get_size_bytes(instance->rx_buffer),
&instance->data->emv_application)) {
error = EmvErrorProtocol;
FURI_LOG_E(TAG, "Failed to parse application");
}
} while(false);
return error;
}
EmvError emv_poller_select_application(EmvPoller* instance) {
EmvError error = EmvErrorNone;
// DELETE IT???????????????????????????????????????????????????????????????????????????????????????
instance->data->emv_application.app_started = false;
const uint8_t emv_select_header[] = {
0x00,
0xA4, // SELECT application
0x04,
0x00 // P1:By name, P2:First or only occurence
};
bit_buffer_reset(instance->tx_buffer);
bit_buffer_reset(instance->rx_buffer);
// Copy header
bit_buffer_copy_bytes(instance->tx_buffer, emv_select_header, sizeof(emv_select_header));
// Copy AID
bit_buffer_append_byte(instance->tx_buffer, instance->data->emv_application.aid_len);
bit_buffer_append_bytes(
instance->tx_buffer,
instance->data->emv_application.aid,
instance->data->emv_application.aid_len);
bit_buffer_append_byte(instance->tx_buffer, 0x00);
do {
FURI_LOG_D(TAG, "Start application");
Iso14443_4aError iso14443_4a_error = iso14443_4a_poller_send_block(
instance->iso14443_4a_poller, instance->tx_buffer, instance->rx_buffer);
if(iso14443_4a_error != Iso14443_4aErrorNone) {
FURI_LOG_E(TAG, "Failed to read PAN or PDOL");
error = emv_process_error(iso14443_4a_error);
break;
}
emv_trace(instance, "Start application answer:");
const uint8_t* buff = bit_buffer_get_data(instance->rx_buffer);
if(!emv_decode_response(
buff,
bit_buffer_get_size_bytes(instance->rx_buffer),
&instance->data->emv_application)) {
error = EmvErrorProtocol;
FURI_LOG_E(TAG, "Failed to parse application");
break;
}
instance->data->emv_application.app_started = true;
} while(false);
return error;
}
EmvError emv_poller_get_processing_options(EmvPoller* instance) {
EmvError error = EmvErrorNone;
const uint8_t emv_gpo_header[] = {0x80, 0xA8, 0x00, 0x00};
bit_buffer_reset(instance->tx_buffer);
bit_buffer_reset(instance->rx_buffer);
// Copy header
bit_buffer_copy_bytes(instance->tx_buffer, emv_gpo_header, sizeof(emv_gpo_header));
// Prepare and copy pdol parameters
APDU pdol_data = {0, {0}};
emv_prepare_pdol(&pdol_data, &instance->data->emv_application.pdol);
bit_buffer_append_byte(instance->tx_buffer, 0x02 + pdol_data.size);
bit_buffer_append_byte(instance->tx_buffer, 0x83);
bit_buffer_append_byte(instance->tx_buffer, pdol_data.size);
bit_buffer_append_bytes(instance->tx_buffer, pdol_data.data, pdol_data.size);
bit_buffer_append_byte(instance->tx_buffer, 0x00);
do {
FURI_LOG_D(TAG, "Get proccessing options");
Iso14443_4aError iso14443_4a_error = iso14443_4a_poller_send_block(
instance->iso14443_4a_poller, instance->tx_buffer, instance->rx_buffer);
if(iso14443_4a_error != Iso14443_4aErrorNone) {
FURI_LOG_E(TAG, "Failed to get processing options");
error = emv_process_error(iso14443_4a_error);
break;
}
emv_trace(instance, "Get processing options answer:");
const uint8_t* buff = bit_buffer_get_data(instance->rx_buffer);
if(!emv_decode_response(
buff,
bit_buffer_get_size_bytes(instance->rx_buffer),
&instance->data->emv_application)) {
error = EmvErrorProtocol;
FURI_LOG_E(TAG, "Failed to parse processing options");
}
} while(false);
return error;
}
EmvError emv_poller_read_sfi_record(EmvPoller* instance, uint8_t sfi, uint8_t record_num) {
EmvError error = EmvErrorNone;
uint8_t sfi_param = (sfi << 3) | (1 << 2);
uint8_t emv_sfi_header[] = {
0x00,
0xB2, // READ RECORD
record_num, // P1:record_number
sfi_param, // P2:SFI
0x00 // Le
};
bit_buffer_reset(instance->tx_buffer);
bit_buffer_reset(instance->rx_buffer);
bit_buffer_copy_bytes(instance->tx_buffer, emv_sfi_header, sizeof(emv_sfi_header));
do {
Iso14443_4aError iso14443_4a_error = iso14443_4a_poller_send_block(
instance->iso14443_4a_poller, instance->tx_buffer, instance->rx_buffer);
if(iso14443_4a_error != Iso14443_4aErrorNone) {
error = emv_process_error(iso14443_4a_error);
break;
}
emv_trace(instance, "SFI record:");
const uint8_t* buff = bit_buffer_get_data(instance->rx_buffer);
if(!emv_decode_response(
buff,
bit_buffer_get_size_bytes(instance->rx_buffer),
&instance->data->emv_application)) {
error = EmvErrorProtocol;
FURI_LOG_E(TAG, "Failed to read SFI record %d", record_num);
}
} while(false);
return error;
}
EmvError emv_poller_read_files(EmvPoller* instance) {
EmvError error = EmvErrorNone;
APDU* afl = &instance->data->emv_application.afl;
if(afl->size == 0) {
return false;
}
FURI_LOG_D(TAG, "Search PAN in SFI");
// Iterate through all files
for(size_t i = 0; i < instance->data->emv_application.afl.size; i += 4) {
uint8_t sfi = afl->data[i] >> 3;
uint8_t record_start = afl->data[i + 1];
uint8_t record_end = afl->data[i + 2];
// Iterate through all records in file
for(uint8_t record = record_start; record <= record_end; ++record) {
error |= emv_poller_read_sfi_record(instance, sfi, record);
}
}
return error;
}
EmvError emv_poller_read(EmvPoller* instance) {
furi_assert(instance);
EmvError error = EmvErrorNone;
memset(&instance->data->emv_application, 0, sizeof(EmvApplication));
do {
error |= emv_poller_select_ppse(instance);
if(error != EmvErrorNone) break;
error |= emv_poller_select_application(instance);
if(error != EmvErrorNone) break;
if(emv_poller_get_processing_options(instance) != EmvErrorNone)
error = emv_poller_read_files(instance);
} while(false);
return error;
}

View file

@ -0,0 +1,52 @@
#pragma once
#include "emv_poller.h"
#include <lib/nfc/protocols/iso14443_4a/iso14443_4a_poller_i.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
EmvPollerStateIdle,
EmvPollerStateSelectPPSE,
EmvPollerStateSelectApplication,
EmvPollerStateGetProcessingOptions,
EmvPollerStateReadFiles,
EmvPollerStateReadFailed,
EmvPollerStateReadSuccess,
EmvPollerStateNum,
} EmvPollerState;
typedef enum {
EmvPollerSessionStateIdle,
EmvPollerSessionStateActive,
EmvPollerSessionStateStopRequest,
} EmvPollerSessionState;
struct EmvPoller {
Iso14443_4aPoller* iso14443_4a_poller;
EmvPollerSessionState session_state;
EmvPollerState state;
EmvError error;
EmvData* data;
BitBuffer* tx_buffer;
BitBuffer* rx_buffer;
BitBuffer* input_buffer;
BitBuffer* result_buffer;
EmvPollerEventData emv_event_data;
EmvPollerEvent emv_event;
NfcGenericEvent general_event;
NfcGenericCallback callback;
void* context;
};
EmvError emv_process_error(Iso14443_4aError error);
const EmvData* emv_poller_get_data(EmvPoller* instance);
#ifdef __cplusplus
}
#endif

View file

@ -9,6 +9,7 @@
#include <nfc/protocols/mf_ultralight/mf_ultralight_poller_defs.h>
#include <nfc/protocols/mf_classic/mf_classic_poller_defs.h>
#include <nfc/protocols/mf_desfire/mf_desfire_poller_defs.h>
#include <nfc/protocols/emv/emv_poller_defs.h>
#include <nfc/protocols/slix/slix_poller_defs.h>
#include <nfc/protocols/st25tb/st25tb_poller_defs.h>
@ -22,6 +23,7 @@ const NfcPollerBase* nfc_pollers_api[NfcProtocolNum] = {
[NfcProtocolMfUltralight] = &mf_ultralight_poller,
[NfcProtocolMfClassic] = &mf_classic_poller,
[NfcProtocolMfDesfire] = &mf_desfire_poller,
[NfcProtocolEmv] = &emv_poller,
[NfcProtocolSlix] = &nfc_poller_slix,
/* Add new pollers here */
[NfcProtocolSt25tb] = &nfc_poller_st25tb,

View file

@ -12,17 +12,19 @@
* ```
* **************************** Protocol tree structure ***************************
*
* (Start)
* |
* +------------------------+-----------+---------+------------+
* | | | | |
* ISO14443-3A ISO14443-3B Felica ISO15693-3 ST25TB
* | | |
* +---------------+-------------+ ISO14443-4B SLIX
* | | |
* ISO14443-4A Mf Ultralight Mf Classic
* |
* Mf Desfire
* (Start)
* |
* +------------------------+-----------+---------+------------+
* | | | | |
* ISO14443-3A ISO14443-3B Felica ISO15693-3 ST25TB
* | | |
* +---------------+-------------+ ISO14443-4B SLIX
* | | |
* ISO14443-4A Mf Ultralight Mf Classic
* |
* +-----+-----+
* | |
* Mf Desfire EMV
* ```
*
* When implementing a new protocol, its place in the tree must be determined first.
@ -61,6 +63,7 @@ static const NfcProtocol nfc_protocol_iso14443_3b_children_protocol[] = {
/** List of ISO14443-4A child protocols. */
static const NfcProtocol nfc_protocol_iso14443_4a_children_protocol[] = {
NfcProtocolMfDesfire,
NfcProtocolEmv,
};
/** List of ISO115693-3 child protocols. */
@ -134,6 +137,12 @@ static const NfcProtocolTreeNode nfc_protocol_nodes[NfcProtocolNum] = {
.children_num = 0,
.children_protocol = NULL,
},
[NfcProtocolEmv] =
{
.parent_protocol = NfcProtocolIso14443_4a,
.children_num = 0,
.children_protocol = NULL,
},
[NfcProtocolSlix] =
{
.parent_protocol = NfcProtocolIso15693_3,

View file

@ -72,19 +72,19 @@
*
* ### 2.2 File structure explanation
*
* | Filename | Explanation |
* |:------------------------------|:------------|
* | protocol_name.h | Main protocol data structure and associated functions declarations. It is recommended to keep the former as opaque pointer. |
* | protocol_name.c | Implementations of functions declared in `protocol_name.h`. |
* | protocol_name_device_defs.h | Declarations for use by the NfcDevice library. See nfc_device_base_i.h for more info. |
* | protocol_name_poller.h | Protocol-specific poller and associated functions declarations. |
* | protocol_name_poller.c | Implementation of functions declared in `protocol_name_poller.h`. |
* | protocol_name_poller_defs.h | Declarations for use by the NfcPoller library. See nfc_poller_base.h for more info. |
* | protocol_name_listener.h | Protocol-specific listener and associated functions declarations. Optional, needed for emulation support. |
* | protocol_name_listener.c | Implementation of functions declared in `protocol_name_listener.h`. Optional, needed for emulation support. |
* | Filename | Explanation |
* |:------------------------------|:--------------------------------------------------------------------------------------------------------------------------------|
* | protocol_name.h | Main protocol data structure and associated functions declarations. It is recommended to keep the former as opaque pointer. |
* | protocol_name.c | Implementations of functions declared in `protocol_name.h`. |
* | protocol_name_device_defs.h | Declarations for use by the NfcDevice library. See nfc_device_base_i.h for more info. |
* | protocol_name_poller.h | Protocol-specific poller and associated functions declarations. |
* | protocol_name_poller.c | Implementation of functions declared in `protocol_name_poller.h`. |
* | protocol_name_poller_defs.h | Declarations for use by the NfcPoller library. See nfc_poller_base.h for more info. |
* | protocol_name_listener.h | Protocol-specific listener and associated functions declarations. Optional, needed for emulation support. |
* | protocol_name_listener.c | Implementation of functions declared in `protocol_name_listener.h`. Optional, needed for emulation support. |
* | protocol_name_listener_defs.h | Declarations for use by the NfcListener library. See nfc_listener_base.h for more info. Optional, needed for emulation support. |
* | protocol_name_sync.h | Synchronous API declarations. (See below for sync API explanation). Optional.|
* | protocol_name_sync.c | Synchronous API implementation. Optional. |
* | protocol_name_sync.h | Synchronous API declarations. (See below for sync API explanation). Optional. |
* | protocol_name_sync.c | Synchronous API implementation. Optional. |
*
* ## 3 Implement the code
*
@ -185,6 +185,7 @@ typedef enum {
NfcProtocolMfUltralight,
NfcProtocolMfClassic,
NfcProtocolMfDesfire,
NfcProtocolEmv,
NfcProtocolSlix,
NfcProtocolSt25tb,
/* Add new protocols here */