Adding support for standard USB Smartcard Readers (#769)

* add PCSC reader support to 'sc raw' and all 'emv' commands
* move all APDU -> TPDU mapping to ExchangeAPDUSC()
* print "PSE" instead of "PPSE" when using contact interface
* fix some #defines in protocols.h
* DropField only when using contactless
* some refactoring
This commit is contained in:
pwpiwi 2019-02-01 21:12:20 +01:00 committed by GitHub
commit 6b5105bea9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 388 additions and 311 deletions

View file

@ -45,7 +45,7 @@ void ParamLoadDefaults(struct tlvdb *tlvRoot) {
}
int CmdEMVSelect(const char *cmd) {
uint8_t data[APDU_AID_LEN] = {0};
uint8_t data[APDU_DATA_LEN] = {0};
int datalen = 0;
CLIParserInit("emv select",
@ -83,7 +83,7 @@ int CmdEMVSelect(const char *cmd) {
SetAPDULogging(APDULogging);
// exec
uint8_t buf[APDU_RES_LEN] = {0};
uint8_t buf[APDU_RESPONSE_LEN] = {0};
size_t len = 0;
uint16_t sw = 0;
int res = EMVSelect(channel, activateField, leaveSignalON, data, datalen, buf, sizeof(buf), &len, &sw, NULL);
@ -193,7 +193,7 @@ int CmdEMVPPSE(const char *cmd) {
SetAPDULogging(APDULogging);
// exec
uint8_t buf[APDU_RES_LEN] = {0};
uint8_t buf[APDU_RESPONSE_LEN] = {0};
size_t len = 0;
uint16_t sw = 0;
int res = EMVSelectPSE(channel, activateField, leaveSignalON, PSENum, buf, sizeof(buf), &len, &sw);
@ -212,7 +212,7 @@ int CmdEMVPPSE(const char *cmd) {
}
int CmdEMVGPO(const char *cmd) {
uint8_t data[APDU_RES_LEN] = {0};
uint8_t data[APDU_RESPONSE_LEN] = {0};
int datalen = 0;
CLIParserInit("emv gpo",
@ -295,7 +295,7 @@ int CmdEMVGPO(const char *cmd) {
PrintAndLogEx(INFO, "PDOL data[%d]: %s", pdol_data_tlv_data_len, sprint_hex(pdol_data_tlv_data, pdol_data_tlv_data_len));
// exec
uint8_t buf[APDU_RES_LEN] = {0};
uint8_t buf[APDU_RESPONSE_LEN] = {0};
size_t len = 0;
uint16_t sw = 0;
int res = EMVGPO(channel, leaveSignalON, pdol_data_tlv_data, pdol_data_tlv_data_len, buf, sizeof(buf), &len, &sw, tlvRoot);
@ -317,7 +317,7 @@ int CmdEMVGPO(const char *cmd) {
}
int CmdEMVReadRecord(const char *cmd) {
uint8_t data[APDU_RES_LEN] = {0};
uint8_t data[APDU_RESPONSE_LEN] = {0};
int datalen = 0;
CLIParserInit("emv readrec",
@ -356,7 +356,7 @@ int CmdEMVReadRecord(const char *cmd) {
SetAPDULogging(APDULogging);
// exec
uint8_t buf[APDU_RES_LEN] = {0};
uint8_t buf[APDU_RESPONSE_LEN] = {0};
size_t len = 0;
uint16_t sw = 0;
int res = EMVReadRecord(channel, leaveSignalON, data[0], data[1], buf, sizeof(buf), &len, &sw, NULL);
@ -375,7 +375,7 @@ int CmdEMVReadRecord(const char *cmd) {
}
int CmdEMVAC(const char *cmd) {
uint8_t data[APDU_RES_LEN] = {0};
uint8_t data[APDU_RESPONSE_LEN] = {0};
int datalen = 0;
CLIParserInit("emv genac",
@ -474,7 +474,7 @@ int CmdEMVAC(const char *cmd) {
PrintAndLogEx(INFO, "CDOL data[%d]: %s", cdol_data_tlv->len, sprint_hex(cdol_data_tlv->value, cdol_data_tlv->len));
// exec
uint8_t buf[APDU_RES_LEN] = {0};
uint8_t buf[APDU_RESPONSE_LEN] = {0};
size_t len = 0;
uint16_t sw = 0;
int res = EMVAC(channel, leaveSignalON, termDecision, (uint8_t *)cdol_data_tlv->value, cdol_data_tlv->len, buf, sizeof(buf), &len, &sw, tlvRoot);
@ -524,7 +524,7 @@ int CmdEMVGenerateChallenge(const char *cmd) {
SetAPDULogging(APDULogging);
// exec
uint8_t buf[APDU_RES_LEN] = {0};
uint8_t buf[APDU_RESPONSE_LEN] = {0};
size_t len = 0;
uint16_t sw = 0;
int res = EMVGenerateChallenge(channel, leaveSignalON, buf, sizeof(buf), &len, &sw, NULL);
@ -544,7 +544,7 @@ int CmdEMVGenerateChallenge(const char *cmd) {
}
int CmdEMVInternalAuthenticate(const char *cmd) {
uint8_t data[APDU_RES_LEN] = {0};
uint8_t data[APDU_RESPONSE_LEN] = {0};
int datalen = 0;
CLIParserInit("emv intauth",
@ -624,7 +624,7 @@ int CmdEMVInternalAuthenticate(const char *cmd) {
PrintAndLogEx(INFO, "DDOL data[%d]: %s", ddol_data_tlv->len, sprint_hex(ddol_data_tlv->value, ddol_data_tlv->len));
// exec
uint8_t buf[APDU_RES_LEN] = {0};
uint8_t buf[APDU_RESPONSE_LEN] = {0};
size_t len = 0;
uint16_t sw = 0;
int res = EMVInternalAuthenticate(channel, leaveSignalON, data, datalen, buf, sizeof(buf), &len, &sw, NULL);
@ -711,10 +711,10 @@ void ProcessGPOResponseFormat1(struct tlvdb *tlvRoot, uint8_t *buf, size_t len,
}
int CmdEMVExec(const char *cmd) {
uint8_t buf[APDU_RES_LEN] = {0};
uint8_t buf[APDU_RESPONSE_LEN] = {0};
size_t len = 0;
uint16_t sw = 0;
uint8_t AID[APDU_AID_LEN] = {0};
uint8_t AID[APDU_DATA_LEN] = {0};
size_t AIDlen = 0;
uint8_t ODAiList[4096];
size_t ODAiListLen = 0;
@ -769,6 +769,8 @@ int CmdEMVExec(const char *cmd) {
channel = ECC_CONTACT;
#endif
uint8_t psenum = (channel == ECC_CONTACT) ? 1 : 2;
char *PSE_or_PPSE = psenum == 1 ? "PSE" : "PPSE";
CLIParserFree();
SetAPDULogging(showAPDU);
@ -780,12 +782,12 @@ int CmdEMVExec(const char *cmd) {
// Application Selection
// https://www.openscdp.org/scripts/tutorial/emv/applicationselection.html
if (!forceSearch) {
// PPSE
PrintAndLogEx(NORMAL, "\n* PPSE.");
// PPSE / PSE
PrintAndLogEx(NORMAL, "\n* %s.", PSE_or_PPSE);
SetAPDULogging(showAPDU);
res = EMVSearchPSE(channel, activateField, true, psenum, decodeTLV, tlvSelect);
// check PPSE and select application id
// check PPSE / PSE and select application id
if (!res) {
TLVPrintAIDlistFromSelectTLV(tlvSelect);
EMVSelectApplication(tlvSelect, AID, &AIDlen);
@ -1152,7 +1154,9 @@ int CmdEMVExec(const char *cmd) {
}
}
DropField();
if (channel == ECC_CONTACTLESS) {
DropField();
}
// Destroy TLV's
free(pdol_data_tlv);
@ -1164,9 +1168,9 @@ int CmdEMVExec(const char *cmd) {
}
int CmdEMVScan(const char *cmd) {
uint8_t AID[APDU_AID_LEN] = {0};
uint8_t AID[APDU_DATA_LEN] = {0};
size_t AIDlen = 0;
uint8_t buf[APDU_RES_LEN] = {0};
uint8_t buf[APDU_RESPONSE_LEN] = {0};
size_t len = 0;
uint16_t sw = 0;
int res;
@ -1260,8 +1264,10 @@ int CmdEMVScan(const char *cmd) {
}
// drop field at start
DropField();
if (channel == ECC_CONTACTLESS) {
DropField();
}
// iso 14443 select
PrintAndLogEx(NORMAL, "--> GET UID, ATS.");
@ -1329,7 +1335,9 @@ int CmdEMVScan(const char *cmd) {
if (!AIDlen) {
PrintAndLogEx(INFO, "Can't select AID. EMV AID not found. Exit...");
DropField();
if (channel == ECC_CONTACTLESS) {
DropField();
}
return 4;
}
@ -1376,7 +1384,9 @@ int CmdEMVScan(const char *cmd) {
if (!pdol_data_tlv){
PrintAndLogEx(ERR, "Can't create PDOL TLV.");
tlvdb_free(tlvRoot);
DropField();
if (channel == ECC_CONTACTLESS) {
DropField();
}
return 6;
}
@ -1399,7 +1409,9 @@ int CmdEMVScan(const char *cmd) {
if (res) {
PrintAndLogEx(ERR, "GPO error(%d): %4x. Exit...", res, sw);
tlvdb_free(tlvRoot);
DropField();
if (channel == ECC_CONTACTLESS) {
DropField();
}
return 7;
}
ProcessGPOResponseFormat1(tlvRoot, buf, len, decodeTLV);
@ -1491,8 +1503,9 @@ int CmdEMVScan(const char *cmd) {
// free tlv object
tlvdb_free(tlvRoot);
// DropField
DropField();
if (channel == ECC_CONTACTLESS) {
DropField();
}
res = json_dump_file(root, fname, JSON_INDENT(2));
if (res) {
@ -1512,9 +1525,9 @@ int CmdEMVTest(const char *cmd) {
}
int CmdEMVRoca(const char *cmd) {
uint8_t AID[APDU_AID_LEN] = {0};
uint8_t AID[APDU_DATA_LEN] = {0};
size_t AIDlen = 0;
uint8_t buf[APDU_RES_LEN] = {0};
uint8_t buf[APDU_RESPONSE_LEN] = {0};
size_t len = 0;
uint16_t sw = 0;
int res;
@ -1573,7 +1586,9 @@ int CmdEMVRoca(const char *cmd) {
if (!AIDlen) {
PrintAndLogEx(INFO, "Can't select AID. EMV AID not found. Exit...");
DropField();
if (channel == ECC_CONTACTLESS) {
DropField();
}
return 4;
}
@ -1588,7 +1603,9 @@ int CmdEMVRoca(const char *cmd) {
if (res) {
PrintAndLogEx(ERR, "Can't select AID (%d). Exit...", res);
tlvdb_free(tlvRoot);
DropField();
if (channel == ECC_CONTACTLESS) {
DropField();
}
return 5;
}
@ -1600,7 +1617,9 @@ int CmdEMVRoca(const char *cmd) {
if (!pdol_data_tlv){
PrintAndLogEx(ERR, "Can't create PDOL TLV.");
tlvdb_free(tlvRoot);
DropField();
if (channel == ECC_CONTACTLESS) {
DropField();
}
return 6;
}
@ -1623,7 +1642,9 @@ int CmdEMVRoca(const char *cmd) {
if (res) {
PrintAndLogEx(ERR, "GPO error(%d): %4x. Exit...", res, sw);
tlvdb_free(tlvRoot);
DropField();
if (channel == ECC_CONTACTLESS) {
DropField();
}
return 7;
}
ProcessGPOResponseFormat1(tlvRoot, buf, len, false);
@ -1721,9 +1742,9 @@ out:
// free tlv object
tlvdb_free(tlvRoot);
if ( channel == ECC_CONTACTLESS)
if (channel == ECC_CONTACTLESS) {
DropField();
}
return 0;
}
@ -1732,18 +1753,18 @@ int CmdHelp(const char *Cmd);
static command_t CommandTable[] = {
{"help", CmdHelp, 1, "This help"},
{"exec", CmdEMVExec, 0, "Executes EMV contactless transaction."},
{"pse", CmdEMVPPSE, 0, "Execute PPSE. It selects 2PAY.SYS.DDF01 or 1PAY.SYS.DDF01 directory."},
{"search", CmdEMVSearch, 0, "Try to select all applets from applets list and print installed applets."},
{"select", CmdEMVSelect, 0, "Select applet."},
{"gpo", CmdEMVGPO, 0, "Execute GetProcessingOptions."},
{"readrec", CmdEMVReadRecord, 0, "Read files from card."},
{"genac", CmdEMVAC, 0, "Generate ApplicationCryptogram."},
{"challenge", CmdEMVGenerateChallenge, 0, "Generate challenge."},
{"intauth", CmdEMVInternalAuthenticate, 0, "Internal authentication."},
{"scan", CmdEMVScan, 0, "Scan EMV card and save it contents to json file for emulator."},
{"test", CmdEMVTest, 0, "Crypto logic test."},
{"roca", CmdEMVRoca, 0, "Extract public keys and run ROCA test"},
{"exec", CmdEMVExec, 1, "Executes EMV contactless transaction."},
{"pse", CmdEMVPPSE, 1, "Execute PPSE. It selects 2PAY.SYS.DDF01 or 1PAY.SYS.DDF01 directory."},
{"search", CmdEMVSearch, 1, "Try to select all applets from applets list and print installed applets."},
{"select", CmdEMVSelect, 1, "Select applet."},
{"gpo", CmdEMVGPO, 1, "Execute GetProcessingOptions."},
{"readrec", CmdEMVReadRecord, 1, "Read files from card."},
{"genac", CmdEMVAC, 1, "Generate ApplicationCryptogram."},
{"challenge", CmdEMVGenerateChallenge, 1, "Generate challenge."},
{"intauth", CmdEMVInternalAuthenticate, 1, "Internal authentication."},
{"scan", CmdEMVScan, 1, "Scan EMV card and save it contents to json file for emulator."},
{"test", CmdEMVTest, 1, "Crypto logic test."},
{"roca", CmdEMVRoca, 1, "Extract public keys and run ROCA test"},
{NULL, NULL, 0, NULL}
};

View file

@ -11,6 +11,7 @@
#include "emvcore.h"
#include "emvjson.h"
#include "util_posix.h"
#include "protocols.h"
#ifdef WITH_SMARTCARD
#include "cmdsmartcard.h"
#endif
@ -235,44 +236,39 @@ struct tlvdb *GetdCVVRawFromTrack2(const struct tlv *track2) {
return tlvdb_fixed(0x02, dCVVlen, dCVV);
}
int EMVExchangeEx(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON, sAPDU apdu, bool IncludeLe, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
uint8_t data[APDU_RES_LEN] = {0};
static int EMVExchangeEx(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t *apdu, int apdu_len, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv)
{
*ResultLen = 0;
if (sw) *sw = 0;
uint16_t isw = 0;
int res = 0;
if (ActivateField) {
if (ActivateField && channel == ECC_CONTACTLESS) {
DropField();
msleep(50);
}
// COMPUTE APDU
memcpy(data, &apdu, 5);
if (apdu.data)
memcpy(&data[5], apdu.data, apdu.Lc);
if (APDULogging)
PrintAndLogEx(SUCCESS, ">>>> %s", sprint_hex(data, (IncludeLe?6:5) + apdu.Lc));
PrintAndLogEx(SUCCESS, ">>>> %s", sprint_hex(apdu, apdu_len));
switch(channel) {
case ECC_CONTACTLESS:
// 6 byes + data = INS + CLA + P1 + P2 + Lc + <data = Nc> + Le(?IncludeLe)
res = ExchangeAPDU14a(data, (IncludeLe?6:5) + apdu.Lc, ActivateField, LeaveFieldON, Result, (int)MaxResultLen, (int *)ResultLen);
if (res) {
return res;
}
break;
case ECC_CONTACT:
//int ExchangeAPDUSC(uint8_t *datain, int datainlen, bool activateCard, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen);
case ECC_CONTACTLESS:
// 6 byes + data = INS + CLA + P1 + P2 + Lc + <data = Nc> + Le(?IncludeLe)
res = ExchangeAPDU14a(apdu, apdu_len, ActivateField, LeaveFieldON, Result, (int)MaxResultLen, (int *)ResultLen);
if (res) {
return res;
}
break;
case ECC_CONTACT:
//int ExchangeAPDUSC(uint8_t *datain, int datainlen, bool activateCard, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen);
#ifdef WITH_SMARTCARD
res = ExchangeAPDUSC(data, (IncludeLe?6:5) + apdu.Lc, ActivateField, LeaveFieldON, Result, (int)MaxResultLen, (int *)ResultLen);
if (res) {
return res;
}
res = ExchangeAPDUSC(apdu, apdu_len, ActivateField, LeaveFieldON, Result, (int)MaxResultLen, (int *)ResultLen);
if (res) {
return res;
}
#endif
break;
break;
}
if (APDULogging)
@ -282,6 +278,12 @@ int EMVExchangeEx(EMVCommandChannel channel, bool ActivateField, bool LeaveField
return 200;
}
if (Result[*ResultLen-2] == 0x61) {
uint8_t La = Result[*ResultLen-1];
uint8_t get_response[5] = {apdu[0], ISO7816_GET_RESPONSE, 0x00, 0x00, La};
return EMVExchangeEx(channel, false, LeaveFieldON, get_response, sizeof(get_response), Result, MaxResultLen, ResultLen, sw, tlv);
}
*ResultLen -= 2;
isw = Result[*ResultLen] * 0x0100 + Result[*ResultLen + 1];
if (sw)
@ -289,12 +291,8 @@ int EMVExchangeEx(EMVCommandChannel channel, bool ActivateField, bool LeaveField
if (isw != 0x9000) {
if (APDULogging) {
if (*sw >> 8 == 0x61) {
PrintAndLogEx(ERR, "APDU chaining len:%02x -->", *sw & 0xff);
} else {
PrintAndLogEx(ERR, "APDU(%02x%02x) ERROR: [%4X] %s", apdu.CLA, apdu.INS, isw, GetAPDUCodeDescription(*sw >> 8, *sw & 0xff));
return 5;
}
PrintAndLogEx(ERR, "APDU(%02x%02x) ERROR: [%4X] %s", apdu[0], apdu[1], isw, GetAPDUCodeDescription(*sw >> 8, *sw & 0xff));
return 5;
}
}
@ -307,16 +305,36 @@ int EMVExchangeEx(EMVCommandChannel channel, bool ActivateField, bool LeaveField
return 0;
}
int EMVExchange(EMVCommandChannel channel, bool LeaveFieldON, sAPDU apdu, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
return EMVExchangeEx(channel, false, LeaveFieldON, apdu, (channel == ECC_CONTACTLESS), Result, MaxResultLen, ResultLen, sw, tlv);
int EMVExchange(EMVCommandChannel channel, bool LeaveFieldON, uint8_t *apdu, int apdu_len, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv)
{
uint8_t APDU[APDU_COMMAND_LEN];
memcpy(APDU, apdu, apdu_len);
APDU[apdu_len] = 0x00;
if (channel == ECC_CONTACTLESS) {
if (apdu_len == 5 && apdu[4] == 0) {
// there is no Lc but an Le == 0 already
} else if (apdu_len > 5 && apdu_len == 5 + apdu[4] + 1) {
// there is Lc, data and Le
} else {
apdu_len++; // no Le, add Le = 0x00 because some vendors require it for contactless
}
}
return EMVExchangeEx(channel, false, LeaveFieldON, APDU, apdu_len, Result, MaxResultLen, ResultLen, sw, tlv);
}
int EMVSelect(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t *AID, size_t AIDLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
return EMVExchangeEx(channel, ActivateField, LeaveFieldON, (sAPDU){0x00, 0xa4, 0x04, 0x00, AIDLen, AID}, (channel == ECC_CONTACTLESS), Result, MaxResultLen, ResultLen, sw, tlv);
int EMVSelect(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t *AID, size_t AIDLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv)
{
uint8_t Select_APDU[APDU_COMMAND_LEN] = {0x00, ISO7816_SELECT_FILE, 0x04, 0x00, AIDLen, 0x00};
memcpy(Select_APDU + 5, AID, AIDLen);
int apdulen = 5 + AIDLen;
if (channel == ECC_CONTACTLESS) {
apdulen++; // some vendors require Le = 0x00 for contactless operations
}
return EMVExchangeEx(channel, ActivateField, LeaveFieldON, Select_APDU, apdulen, Result, MaxResultLen, ResultLen, sw, tlv);
}
int EMVSelectPSE(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t PSENum, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw) {
uint8_t buf[APDU_AID_LEN] = {0};
uint8_t buf[APDU_DATA_LEN] = {0};
*ResultLen = 0;
int len = 0;
int res = 0;
@ -325,6 +343,7 @@ int EMVSelectPSE(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldO
param_gethex_to_eol(PSElist[1], 0, buf, sizeof(buf), &len);
break;
case 2:
param_gethex_to_eol(PSElist[0], 0, buf, sizeof(buf), &len);
break;
default:
@ -338,11 +357,13 @@ int EMVSelectPSE(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldO
}
int EMVSearchPSE(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t PSENum, bool decodeTLV, struct tlvdb *tlv) {
uint8_t data[APDU_RES_LEN] = {0};
uint8_t data[APDU_RESPONSE_LEN] = {0};
size_t datalen = 0;
uint16_t sw = 0;
int res;
char *PSE_or_PPSE = PSENum == 1 ? "PSE" : "PPSE";
// select PPSE
res = EMVSelectPSE(channel, ActivateField, true, PSENum, data, sizeof(data), &datalen, &sw);
@ -353,7 +374,7 @@ int EMVSearchPSE(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldO
int retrycnt = 0;
struct tlvdb *ttmp = tlvdb_find_path(t, (tlv_tag_t[]){0x6f, 0xa5, 0xbf0c, 0x61, 0x00});
if (!ttmp)
PrintAndLogEx(FAILED, "PPSE don't have records.");
PrintAndLogEx(FAILED, "%s doesn't have any records.", PSE_or_PPSE);
while (ttmp) {
const struct tlv *tgAID = tlvdb_get_inchild(ttmp, 0x4f, NULL);
@ -393,22 +414,22 @@ int EMVSearchPSE(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldO
tlvdb_free(t);
} else {
PrintAndLogEx(WARNING, "PPSE ERROR: Can't get TLV from response.");
PrintAndLogEx(WARNING, "%s ERROR: Can't get TLV from response.", PSE_or_PPSE);
}
} else {
PrintAndLogEx(WARNING, "PPSE ERROR: Can't select PPSE AID. Error: %d", res);
PrintAndLogEx(WARNING, "%s ERROR: Can't select PPSE AID. Error: %d", PSE_or_PPSE, res);
}
if(!LeaveFieldON)
if(!LeaveFieldON && channel == ECC_CONTACTLESS)
DropField();
return res;
}
int EMVSearch(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON, bool decodeTLV, struct tlvdb *tlv) {
uint8_t aidbuf[APDU_AID_LEN] = {0};
uint8_t aidbuf[APDU_DATA_LEN] = {0};
int aidlen = 0;
uint8_t data[APDU_RES_LEN] = {0};
uint8_t data[APDU_RESPONSE_LEN] = {0};
size_t datalen = 0;
uint16_t sw = 0;
@ -489,38 +510,63 @@ int EMVSelectApplication(struct tlvdb *tlv, uint8_t *AID, size_t *AIDlen) {
return 0;
}
int EMVGPO(EMVCommandChannel channel, bool LeaveFieldON, uint8_t *PDOL, size_t PDOLLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
return EMVExchange(channel, LeaveFieldON, (sAPDU){0x80, 0xa8, 0x00, 0x00, PDOLLen, PDOL}, Result, MaxResultLen, ResultLen, sw, tlv);
int EMVGPO(EMVCommandChannel channel, bool LeaveFieldON, uint8_t *PDOL, size_t PDOLLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv)
{
uint8_t GPO_APDU[APDU_COMMAND_LEN] = {0x80, 0xa8, 0x00, 0x00, PDOLLen, 0x00};
memcpy(GPO_APDU + 5, PDOL, PDOLLen);
int apdulen = 5 + PDOLLen;
return EMVExchange(channel, LeaveFieldON, GPO_APDU, apdulen, Result, MaxResultLen, ResultLen, sw, tlv);
}
int EMVReadRecord(EMVCommandChannel channel, bool LeaveFieldON, uint8_t SFI, uint8_t SFIrec, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
int res = EMVExchange(channel, LeaveFieldON, (sAPDU){0x00, 0xb2, SFIrec, (SFI << 3) | 0x04, 0, NULL}, Result, MaxResultLen, ResultLen, sw, tlv);
int EMVReadRecord(EMVCommandChannel channel, bool LeaveFieldON, uint8_t SFI, uint8_t SFIrec, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv)
{
uint8_t read_APDU[5] = {0x00, ISO7816_READ_RECORDS, SFIrec, (SFI << 3) | 0x04, 0x00};
int res = EMVExchange(channel, LeaveFieldON, read_APDU, sizeof(read_APDU), Result, MaxResultLen, ResultLen, sw, tlv);
if (*sw == 0x6700) {
PrintAndLogEx(INFO, ">>> trying to reissue command withouth Le...");
res = EMVExchangeEx(channel, false, LeaveFieldON, (sAPDU){0x00, 0xb2, SFIrec, (SFI << 3) | 0x04, 0, NULL}, false, Result, MaxResultLen, ResultLen, sw, tlv);
res = EMVExchangeEx(channel, false, LeaveFieldON, read_APDU, sizeof(read_APDU), Result, MaxResultLen, ResultLen, sw, tlv);
}
return res;
}
int EMVAC(EMVCommandChannel channel, bool LeaveFieldON, uint8_t RefControl, uint8_t *CDOL, size_t CDOLLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
return EMVExchange(channel, LeaveFieldON, (sAPDU){0x80, 0xae, RefControl, 0x00, CDOLLen, CDOL}, Result, MaxResultLen, ResultLen, sw, tlv);
int EMVAC(EMVCommandChannel channel, bool LeaveFieldON, uint8_t RefControl, uint8_t *CDOL, size_t CDOLLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv)
{
uint8_t CDOL_APDU[APDU_COMMAND_LEN] = {0x80, 0xae, RefControl, 0x00, CDOLLen, 0x00};
memcpy(CDOL_APDU + 5, CDOL, CDOLLen);
int apdulen = 5 + CDOLLen;
return EMVExchange(channel, LeaveFieldON, CDOL_APDU, apdulen, Result, MaxResultLen, ResultLen, sw, tlv);
}
int EMVGenerateChallenge(EMVCommandChannel channel, bool LeaveFieldON, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
int res = EMVExchange(channel, LeaveFieldON, (sAPDU){0x00, 0x84, 0x00, 0x00, 0x00, NULL}, Result, MaxResultLen, ResultLen, sw, tlv);
int EMVGenerateChallenge(EMVCommandChannel channel, bool LeaveFieldON, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv)
{
uint8_t get_challenge_APDU[APDU_COMMAND_LEN] = {0x00, ISO7816_GET_CHALLENGE, 0x00, 0x00};
int res = EMVExchange(channel, LeaveFieldON, get_challenge_APDU, 4, Result, MaxResultLen, ResultLen, sw, tlv);
if (*sw == 0x6700) {
PrintAndLogEx(INFO, ">>> trying to reissue command withouth Le...");
res = EMVExchangeEx(channel, false, LeaveFieldON, (sAPDU){0x00, 0x84, 0x00, 0x00, 0x00, NULL}, false, Result, MaxResultLen, ResultLen, sw, tlv);
res = EMVExchangeEx(channel, false, LeaveFieldON, get_challenge_APDU, 4, Result, MaxResultLen, ResultLen, sw, tlv);
}
return res;
}
int EMVInternalAuthenticate(EMVCommandChannel channel, bool LeaveFieldON, uint8_t *DDOL, size_t DDOLLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
return EMVExchange(channel, LeaveFieldON, (sAPDU){0x00, 0x88, 0x00, 0x00, DDOLLen, DDOL}, Result, MaxResultLen, ResultLen, sw, tlv);
int EMVInternalAuthenticate(EMVCommandChannel channel, bool LeaveFieldON, uint8_t *DDOL, size_t DDOLLen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv)
{
uint8_t authenticate_APDU[APDU_COMMAND_LEN] = {0x00, ISO7816_INTERNAL_AUTHENTICATION, 0x00, 0x00, DDOLLen, 0x00};
memcpy(authenticate_APDU + 5, DDOL, DDOLLen);
int apdulen = 5 + DDOLLen;
return EMVExchange(channel, LeaveFieldON, authenticate_APDU, apdulen, Result, MaxResultLen, ResultLen, sw, tlv);
}
int MSCComputeCryptoChecksum(EMVCommandChannel channel, bool LeaveFieldON, uint8_t *UDOL, uint8_t UDOLlen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv) {
return EMVExchange(channel, LeaveFieldON, (sAPDU){0x80, 0x2a, 0x8e, 0x80, UDOLlen, UDOL}, Result, MaxResultLen, ResultLen, sw, tlv);
int MSCComputeCryptoChecksum(EMVCommandChannel channel, bool LeaveFieldON, uint8_t *UDOL, uint8_t UDOLlen, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv)
{
uint8_t checksum_APDU[APDU_COMMAND_LEN] = {0x80, 0x2a, 0x8e, 0x80, UDOLlen, 0x00};
memcpy(checksum_APDU + 5, UDOL, UDOLlen);
int apdulen = 5 + UDOLlen;
return EMVExchange(channel, LeaveFieldON, checksum_APDU, apdulen, Result, MaxResultLen, ResultLen, sw, tlv);
}
// Authentication
@ -591,7 +637,7 @@ static const unsigned char default_ddol_value[] = {0x9f, 0x37, 0x04};
static struct tlv default_ddol_tlv = {.tag = 0x9f49, .len = 3, .value = default_ddol_value };
int trDDA(EMVCommandChannel channel, bool decodeTLV, struct tlvdb *tlv) {
uint8_t buf[APDU_RES_LEN] = {0};
uint8_t buf[APDU_RESPONSE_LEN] = {0};
size_t len = 0;
uint16_t sw = 0;

View file

@ -29,8 +29,10 @@
#include "emv_pk.h"
#include "emv_pki.h"
#define APDU_RES_LEN 260
#define APDU_AID_LEN 50
// maximum APDU lengths. Long APDUs not yet supported/needed
#define APDU_DATA_LEN 255
#define APDU_COMMAND_LEN (4 + 1 + APDU_DATA_LEN + 1)
#define APDU_RESPONSE_LEN (256 + 2)
typedef enum {
ECC_CONTACTLESS,
@ -45,15 +47,6 @@ enum TransactionType {
};
extern char *TransactionTypeStr[];
typedef struct {
uint8_t CLA;
uint8_t INS;
uint8_t P1;
uint8_t P2;
uint8_t Lc;
uint8_t *data;
} sAPDU;
enum CardPSVendor {
CV_NA,
CV_VISA,
@ -76,7 +69,7 @@ extern struct tlvdb *GetdCVVRawFromTrack2(const struct tlv *track2);
extern void SetAPDULogging(bool logging);
// exchange
extern int EMVExchange(EMVCommandChannel channel, bool LeaveFieldON, sAPDU apdu, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv);
extern int EMVExchange(EMVCommandChannel channel, bool LeaveFieldON, uint8_t *APDU, int APDU_len, uint8_t *Result, size_t MaxResultLen, size_t *ResultLen, uint16_t *sw, struct tlvdb *tlv);
// search application
extern int EMVSearchPSE(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t PSENum, bool decodeTLV, struct tlvdb *tlv);