mirror of
https://github.com/Proxmark/proxmark3.git
synced 2025-08-22 06:13:27 -07:00
starting refactoring
This commit is contained in:
parent
117f0df729
commit
33f52dbda0
2 changed files with 71 additions and 2 deletions
|
@ -1994,7 +1994,7 @@ int iso14_apdu(uint8_t *cmd, uint16_t cmd_len, void *data, uint8_t *res) {
|
||||||
*res = data_bytes[0];
|
*res = data_bytes[0];
|
||||||
|
|
||||||
// crc check
|
// crc check
|
||||||
if (len >=3 && !CheckCrc14443(CRC_14443_A, data_bytes, len)) {
|
if (len >= 3 && !CheckCrc14443(CRC_14443_A, data_bytes, len)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -747,11 +747,80 @@ int ExchangeRAW14a(uint8_t *datain, int datainlen, bool activateField, bool leav
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int CmdExchangeAPDU(uint8_t *datain, int datainlen, bool activateField, uint8_t *dataout, int maxdataoutlen, int *dataoutlen) {
|
||||||
|
|
||||||
|
if (activateField) {
|
||||||
|
cmdc |= ISO14A_CONNECT | ISO14A_CLEAR_TRACE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// "Command APDU" length should be 5+255+1, but javacard's APDU buffer might be smaller - 133 bytes
|
||||||
|
// https://stackoverflow.com/questions/32994936/safe-max-java-card-apdu-data-command-and-respond-size
|
||||||
|
// here length USB_CMD_DATA_SIZE=512
|
||||||
|
// timeout must be authomatically set by "get ATS"
|
||||||
|
UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_APDU | ISO14A_NO_DISCONNECT | cmdc, (datainlen & 0xFFFF), 0}};
|
||||||
|
memcpy(c.d.asBytes, datain, datainlen);
|
||||||
|
SendCommand(&c);
|
||||||
|
|
||||||
|
uint8_t *recv;
|
||||||
|
UsbCommand resp;
|
||||||
|
|
||||||
|
if (activateField) {
|
||||||
|
if (!WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
|
||||||
|
PrintAndLog("APDU ERROR: Proxmark connection timeout.");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (resp.arg[0] != 1) {
|
||||||
|
PrintAndLog("APDU ERROR: Proxmark error %d.", resp.arg[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (WaitForResponseTimeout(CMD_ACK, &resp, 1500)) {
|
||||||
|
recv = resp.d.asBytes;
|
||||||
|
int iLen = resp.arg[0];
|
||||||
|
uint8_t res = resp.arg[1];
|
||||||
|
|
||||||
|
int dlen = iLen - 2;
|
||||||
|
if (dlen < 0)
|
||||||
|
dlen = 0;
|
||||||
|
*dataoutlen += dlen;
|
||||||
|
|
||||||
|
if (maxdataoutlen && *dataoutlen > maxdataoutlen) {
|
||||||
|
PrintAndLog("APDU ERROR: Buffer too small(%d). Needs %d bytes", *dataoutlen, maxdataoutlen);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!iLen) {
|
||||||
|
PrintAndLog("APDU ERROR: No APDU response.");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check block TODO
|
||||||
|
if (iLen == -2) {
|
||||||
|
PrintAndLog("APDU ERROR: Block type mismatch.");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(dataout, recv, dlen);
|
||||||
|
|
||||||
|
// CRC Check
|
||||||
|
if (iLen == -1) {
|
||||||
|
PrintAndLog("APDU ERROR: ISO 14443A CRC error.");
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int ExchangeAPDU14a(uint8_t *datain, int datainlen, bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen) {
|
int ExchangeAPDU14a(uint8_t *datain, int datainlen, bool activateField, bool leaveSignalON, uint8_t *dataout, int maxdataoutlen, int *dataoutlen) {
|
||||||
uint16_t cmdc = 0;
|
uint16_t cmdc = 0;
|
||||||
int dlen;
|
int dlen;
|
||||||
*dataoutlen = 0;
|
*dataoutlen = 0;
|
||||||
|
|
||||||
|
int res = CmdExchangeAPDU(datain, datainlen, activateField, dataout, maxdataoutlen, dataoutlen);
|
||||||
|
//start refactoring...
|
||||||
if (activateField) {
|
if (activateField) {
|
||||||
cmdc |= ISO14A_CONNECT | ISO14A_CLEAR_TRACE;
|
cmdc |= ISO14A_CONNECT | ISO14A_CLEAR_TRACE;
|
||||||
}
|
}
|
||||||
|
@ -830,7 +899,7 @@ int ExchangeAPDU14a(uint8_t *datain, int datainlen, bool activateField, bool lea
|
||||||
*dataoutlen += dlen;
|
*dataoutlen += dlen;
|
||||||
|
|
||||||
recv1 = resp1.d.asBytes;
|
recv1 = resp1.d.asBytes;
|
||||||
printf("vlen:%d dlen:%d\n", resp1.arg[0] & 0xFFFF, dlen);
|
printf("dlen:%d\n", dlen);
|
||||||
|
|
||||||
if (maxdataoutlen && *dataoutlen > maxdataoutlen) {
|
if (maxdataoutlen && *dataoutlen > maxdataoutlen) {
|
||||||
PrintAndLog("APDU ERROR: Buffer too small(%d). Needs %d bytes", *dataoutlen, maxdataoutlen);
|
PrintAndLog("APDU ERROR: Buffer too small(%d). Needs %d bytes", *dataoutlen, maxdataoutlen);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue