Skip to content
This repository has been archived by the owner on Mar 18, 2023. It is now read-only.

Commit

Permalink
First order approximation to solving issue emsec#313 ; Needs testing
Browse files Browse the repository at this point in the history
  • Loading branch information
maxieds committed Jan 16, 2022
1 parent f6b7037 commit 67e8668
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,27 @@ This notice must be retained at the top of all source files where indicated.
#include "DESFirePICCHeaderLayout.h"
#include "DESFireISO7816Support.h"
#include "DESFireInstructions.h"
#include "../ISO14443-3A.h"

Iso7816WrappedParams_t Iso7816P1Data = ISO7816_NO_DATA;
Iso7816WrappedParams_t Iso7816P2Data = ISO7816_NO_DATA;
bool Iso7816FileSelected = false;
uint8_t Iso7816FileOffset = 0;
uint8_t Iso7816EfIdNumber = ISO7816_EF_NOT_SPECIFIED;

bool IsWrappedISO7816CommandType(uint8_t *Buffer, uint16_t ByteCount) {
if(ByteCount <= ISO7816_PROLOGUE_SIZE + ISO14443A_CRCA_SIZE + 2) {
return false;
}
else if(!ISO14443ACheckCRCA(Buffer, ByteCount - 2)) {
return false;
}
else if(!Iso7816CLA(Buffer[2])) {
return false;
}
return true;
}

uint16_t SetIso7816WrappedParametersType(uint8_t *Buffer, uint16_t ByteCount) {
if(ByteCount < 8 || !Iso7816CLA(Buffer[0])) {
Iso7816P1Data = ISO7816_UNSUPPORTED_MODE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This notice must be retained at the top of all source files where indicated.
#define Iso7816CLA(cmdCode) \
(cmdCode == DESFIRE_ISO7816_CLA)

#define ISO7816_PROLOGUE_SIZE (2)
#define ISO7816_STATUS_RESPONSE_SIZE (0x02)
#define ISO7816_EF_NOT_SPECIFIED (0xff)
#define ISO7816_EFID_NUMBER_MAX (0x0f)
Expand Down Expand Up @@ -79,6 +80,7 @@ extern bool Iso7816FileSelected;
extern uint8_t Iso7816FileOffset;
extern uint8_t Iso7816EfIdNumber;

bool IsWrappedISO7816CommandType(uint8_t *Buffer, uint16_t ByteCount);
uint16_t SetIso7816WrappedParametersType(uint8_t *Buffer, uint16_t ByteCount);

#endif
25 changes: 20 additions & 5 deletions Firmware/Chameleon-Mini/Application/MifareDESFire.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ This notice must be retained at the top of all source files where indicated.
#include "DESFire/DESFirePICCControl.h"
#include "DESFire/DESFireCrypto.h"
#include "DESFire/DESFireISO14443Support.h"
#include "DESFire/DESFireISO7816Support.h"
#include "DESFire/DESFireStatusCodes.h"
#include "DESFire/DESFireLogging.h"
#include "DESFire/DESFireUtils.h"
Expand Down Expand Up @@ -173,11 +174,18 @@ uint16_t MifareDesfireProcessCommand(uint8_t* Buffer, uint16_t ByteCount) {

uint16_t MifareDesfireProcess(uint8_t* Buffer, uint16_t BitCount) {
size_t ByteCount = (BitCount + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
if(ByteCount >= 8 && DesfireCLA(Buffer[0]) && Buffer[2] == 0x00 &&
Buffer[3] == 0x00 && Buffer[4] == ByteCount - 8) { // Wrapped native command structure:
bool IsISO7816Cmd = IsWrappedISO7816CommandType(Buffer, ByteCount);
uint8_t ISO7816PrologueBytes[2] = { 0x00, 0x00 };
if(IsISO7816Cmd) {
memcpy(&ISO7816PrologueBytes[0], Buffer, 2);
memmove(&Buffer[0], &Buffer[2], ByteCount - 2);
}
if((ByteCount >= 8 && DesfireCLA(Buffer[0]) && Buffer[2] == 0x00 &&
Buffer[3] == 0x00 && Buffer[4] == ByteCount - 8) || IsISO7816Cmd) { // Wrapped native command structure:
/* Unwrap the PDU from ISO 7816-4 */
// Check CRC bytes appended to the buffer:
// -- Actually, just ignore parity problems if they exist
// -- Actually, just ignore parity problems if they exist,
// -- except for in the wrapped ISO7816 command cases
DesfireCmdCLA = Buffer[0];
if(Iso7816CLA(DesfireCmdCLA)) {
uint16_t iso7816ParamsStatus = SetIso7816WrappedParametersType(Buffer, ByteCount);
Expand Down Expand Up @@ -207,12 +215,19 @@ uint16_t MifareDesfireProcess(uint8_t* Buffer, uint16_t BitCount) {
BitCount += 2;
}
else {
/* Re-wrap into ISO 7816-4 */
Buffer[BitCount] = Buffer[0];
/* Re-wrap into ISO 7816-4 */
Buffer[BitCount] = Buffer[0];
Buffer[BitCount + 1] = Buffer[1];
memmove(&Buffer[0], &Buffer[2], BitCount - 2);
ISO14443AAppendCRCA(Buffer, BitCount);
BitCount += 2;
/* Append the same ISO7816 prologue bytes to the response: */
if(IsISO7816Cmd) {
memmove(&Buffer[2], &Buffer[0], BitCount);
memcpy(&Buffer[0], &ISO7816PrologueBytes[0], 2);
BitCount += 2;
ISO14443AAppendCRCA(Buffer, BitCount);
}
}
LogEntry(LOG_INFO_DESFIRE_OUTGOING_DATA, Buffer, BitCount);
return BitCount * BITS_PER_BYTE;
Expand Down

0 comments on commit 67e8668

Please sign in to comment.