mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-03-30 17:03:34 +08:00
Merge pull request #78 from merlokk/emv_pse_directories
Emv pse directories (SFI)
This commit is contained in:
commit
2812017a14
3 changed files with 159 additions and 34 deletions
|
@ -332,28 +332,11 @@ int EMVSelectPSE(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldO
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
int EMVSearchPSE(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t PSENum, bool decodeTLV, struct tlvdb *tlv) {
|
int EMVSelectWithRetry(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 data[APDU_RES_LEN] = {0};
|
|
||||||
size_t datalen = 0;
|
|
||||||
uint16_t sw = 0;
|
|
||||||
int res;
|
|
||||||
|
|
||||||
// select PPSE
|
|
||||||
res = EMVSelectPSE(channel, ActivateField, true, PSENum, data, sizeof(data), &datalen, &sw);
|
|
||||||
|
|
||||||
if (!res){
|
|
||||||
struct tlvdb *t = NULL;
|
|
||||||
t = tlvdb_parse_multi(data, datalen);
|
|
||||||
if (t) {
|
|
||||||
int retrycnt = 0;
|
int retrycnt = 0;
|
||||||
struct tlvdb *ttmp = tlvdb_find_path(t, (tlv_tag_t[]){0x6f, 0xa5, 0xbf0c, 0x61, 0x00});
|
int res = 0;
|
||||||
if (!ttmp)
|
do {
|
||||||
PrintAndLogEx(FAILED, "PPSE don't have records.");
|
res = EMVSelect(channel, false, true, AID, AIDLen, Result, MaxResultLen, ResultLen, sw, tlv);
|
||||||
|
|
||||||
while (ttmp) {
|
|
||||||
const struct tlv *tgAID = tlvdb_get_inchild(ttmp, 0x4f, NULL);
|
|
||||||
if (tgAID) {
|
|
||||||
res = EMVSelect(channel, false, true, (uint8_t *)tgAID->value, tgAID->len, data, sizeof(data), &datalen, &sw, tlv);
|
|
||||||
|
|
||||||
// retry if error and not returned sw error
|
// retry if error and not returned sw error
|
||||||
if (res && res != 5) {
|
if (res && res != 5) {
|
||||||
|
@ -367,14 +350,35 @@ int EMVSearchPSE(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldO
|
||||||
}
|
}
|
||||||
|
|
||||||
retrycnt = 0;
|
retrycnt = 0;
|
||||||
PrintAndLogEx(NORMAL, "Retry failed [%s]. Skiped...", sprint_hex_inrow(tgAID->value, tgAID->len));
|
PrintAndLogEx(NORMAL, "Retry failed [%s]. Skiped...", sprint_hex_inrow(AID, AIDLen));
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (res && res != 5);
|
||||||
|
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int EMVCheckAID(EMVCommandChannel channel, bool decodeTLV, struct tlvdb *tlvdbelm, struct tlvdb *tlv){
|
||||||
|
uint8_t data[APDU_RES_LEN] = {0};
|
||||||
|
size_t datalen = 0;
|
||||||
|
int res = 0;
|
||||||
|
uint16_t sw = 0;
|
||||||
|
|
||||||
|
while (tlvdbelm) {
|
||||||
|
const struct tlv *tgAID = tlvdb_get_inchild(tlvdbelm, 0x4f, NULL);
|
||||||
|
if (tgAID) {
|
||||||
|
res = EMVSelectWithRetry(channel, false, true, (uint8_t *)tgAID->value, tgAID->len, data, sizeof(data), &datalen, &sw, tlv);
|
||||||
|
|
||||||
|
// if returned sw error
|
||||||
|
if (res == 5) {
|
||||||
// next element
|
// next element
|
||||||
ttmp = tlvdb_find_next(ttmp, 0x61);
|
tlvdbelm = tlvdb_find_next(tlvdbelm, 0x61);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
retrycnt = 0;
|
|
||||||
|
if (res)
|
||||||
|
break;
|
||||||
|
|
||||||
// all is ok
|
// all is ok
|
||||||
if (decodeTLV){
|
if (decodeTLV){
|
||||||
|
@ -382,9 +386,90 @@ int EMVSearchPSE(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldO
|
||||||
TLVPrintFromBuffer(data, datalen);
|
TLVPrintFromBuffer(data, datalen);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
tlvdbelm = tlvdb_find_next(tlvdbelm, 0x61);
|
||||||
ttmp = tlvdb_find_next(ttmp, 0x61);
|
|
||||||
}
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int EMVSearchPSE(EMVCommandChannel channel, bool ActivateField, bool LeaveFieldON, uint8_t PSENum, bool decodeTLV, struct tlvdb *tlv) {
|
||||||
|
uint8_t data[APDU_RES_LEN] = {0};
|
||||||
|
size_t datalen = 0;
|
||||||
|
uint8_t sfidata[0x11][APDU_RES_LEN] = {0};
|
||||||
|
size_t sfidatalen[0x11] = {0};
|
||||||
|
uint16_t sw = 0;
|
||||||
|
int res;
|
||||||
|
bool fileFound = false;
|
||||||
|
|
||||||
|
// select PPSE
|
||||||
|
res = EMVSelectPSE(channel, ActivateField, true, PSENum, data, sizeof(data), &datalen, &sw);
|
||||||
|
|
||||||
|
if (!res){
|
||||||
|
if (sw != 0x9000) {
|
||||||
|
PrintAndLogEx(FAILED, "Select PSE error. APDU error: %04x.", sw);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct tlvdb *t = NULL;
|
||||||
|
t = tlvdb_parse_multi(data, datalen);
|
||||||
|
if (t) {
|
||||||
|
// PSE/PPSE with SFI
|
||||||
|
struct tlvdb *tsfi = tlvdb_find_path(t, (tlv_tag_t[]){0x6f, 0xa5, 0x88, 0x00});
|
||||||
|
if (tsfi) {
|
||||||
|
uint8_t sfin = 0;
|
||||||
|
tlv_get_uint8(tlvdb_get_tlv(tsfi), &sfin);
|
||||||
|
PrintAndLogEx(INFO, "* PPSE get SFI: 0x%02x.", sfin);
|
||||||
|
|
||||||
|
for (uint8_t ui = 0x01; ui <= 0x10; ui++) {
|
||||||
|
PrintAndLogEx(INFO, "* * Get SFI: 0x%02x. num: 0x%02x", sfin, ui);
|
||||||
|
res = EMVReadRecord(channel, true, sfin, ui, sfidata[ui], APDU_RES_LEN, &sfidatalen[ui], &sw, NULL);
|
||||||
|
|
||||||
|
// end of records
|
||||||
|
if (sw == 0x6a83) {
|
||||||
|
sfidatalen[ui] = 0;
|
||||||
|
PrintAndLogEx(INFO, "* * PPSE get SFI. End of records.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// error catch!
|
||||||
|
if (sw != 0x9000) {
|
||||||
|
sfidatalen[ui] = 0;
|
||||||
|
PrintAndLogEx(FAILED, "PPSE get Error. APDU error: %04x.", sw);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (decodeTLV){
|
||||||
|
TLVPrintFromBuffer(sfidata[ui], sfidatalen[ui]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint8_t ui = 0x01; ui <= 0x10; ui++) {
|
||||||
|
if (sfidatalen[ui]) {
|
||||||
|
struct tlvdb *tsfi = NULL;
|
||||||
|
tsfi = tlvdb_parse_multi(sfidata[ui], sfidatalen[ui]);
|
||||||
|
if (tsfi) {
|
||||||
|
struct tlvdb *tsfitmp = tlvdb_find_path(tsfi, (tlv_tag_t[]){0x70, 0x61, 0x00});
|
||||||
|
if (!tsfitmp) {
|
||||||
|
PrintAndLogEx(FAILED, "SFI 0x%02d don't have records.", sfidatalen[ui]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
res = EMVCheckAID(channel, decodeTLV, tsfitmp, tlv);
|
||||||
|
fileFound = true;
|
||||||
|
}
|
||||||
|
tlvdb_free(tsfi);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// PSE/PPSE plain (wo SFI)
|
||||||
|
struct tlvdb *ttmp = tlvdb_find_path(t, (tlv_tag_t[]){0x6f, 0xa5, 0xbf0c, 0x61, 0x00});
|
||||||
|
if (ttmp) {
|
||||||
|
res = EMVCheckAID(channel, decodeTLV, ttmp, tlv);
|
||||||
|
fileFound = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!fileFound)
|
||||||
|
PrintAndLogEx(FAILED, "PPSE don't have records.");
|
||||||
|
|
||||||
tlvdb_free(t);
|
tlvdb_free(t);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -534,3 +534,40 @@ struct tlvdb *tlvdb_elm_get_parent(struct tlvdb *tlvdb)
|
||||||
{
|
{
|
||||||
return tlvdb->parent;
|
return tlvdb->parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool tlv_get_uint8(const struct tlv *etlv, uint8_t *value)
|
||||||
|
{
|
||||||
|
*value = 0;
|
||||||
|
if (etlv)
|
||||||
|
{
|
||||||
|
if (etlv->len == 0)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (etlv->len == 1)
|
||||||
|
{
|
||||||
|
*value = etlv->value[0];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool tlv_get_int(const struct tlv *etlv, int *value)
|
||||||
|
{
|
||||||
|
*value = 0;
|
||||||
|
if (etlv)
|
||||||
|
{
|
||||||
|
if (etlv->len == 0)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (etlv->len <= 4)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < etlv->len; i++)
|
||||||
|
{
|
||||||
|
*value += etlv->value[i] * (1 << (i * 8));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
|
@ -61,4 +61,7 @@ unsigned char *tlv_encode(const struct tlv *tlv, size_t *len);
|
||||||
bool tlv_is_constructed(const struct tlv *tlv);
|
bool tlv_is_constructed(const struct tlv *tlv);
|
||||||
bool tlv_equal(const struct tlv *a, const struct tlv *b);
|
bool tlv_equal(const struct tlv *a, const struct tlv *b);
|
||||||
|
|
||||||
|
bool tlv_get_uint8(const struct tlv *etlv, uint8_t *value);
|
||||||
|
bool tlv_get_int(const struct tlv *etlv, int *value);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Reference in a new issue