Skip to content

Commit

Permalink
Fix: MAD different Keys for MAD and App
Browse files Browse the repository at this point in the history
  • Loading branch information
c3rebro committed Jul 22, 2022
1 parent d734239 commit c133e06
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 131 deletions.
27 changes: 15 additions & 12 deletions RFiDGear/DataAccessLayer/Remote/FromIO/RFiDDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public ERROR ReadMiFareClassicSingleSector(int sectorNumber, string aKey, string
}
catch
{
return ERROR.NoError;
return ERROR.AuthenticationError;
}
return ERROR.NoError;
}
Expand Down Expand Up @@ -705,7 +705,8 @@ public ERROR ReadMiFareClassicSingleBlock(int _blockNumber, string _aKey, string

public ERROR WriteMiFareClassicWithMAD(int _madApplicationID, int _madStartSector,
string _aKeyToUse, string _bKeyToUse, string _aKeyToWrite, string _bKeyToWrite,
byte[] buffer, byte _madGPB, bool _useMADToAuth = false, bool _keyToWriteUseMAD = false)
string _madAKeyToUse, string _madBKeyToUse, string _madAKeyToWrite, string _madBKeyToWrite,
byte[] buffer, byte _madGPB, SectorAccessBits _sab, bool _useMADToAuth = false, bool _keyToWriteUseMAD = false)
{
var settings = new SettingsReaderWriter();
Sector = new MifareClassicSectorModel();
Expand All @@ -718,11 +719,11 @@ public ERROR WriteMiFareClassicWithMAD(int _madApplicationID, int _madStartSecto
var mAKeyToWrite = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_aKeyToWrite) ? _aKeyToWrite : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_aKeyToWrite) };
var mBKeyToWrite = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_bKeyToWrite) ? _bKeyToWrite : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_bKeyToWrite) };

var madAKeyToUse = mAKeyToUse;
var madBKeyToUse = mBKeyToUse;
var madAKeyToUse = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_madAKeyToUse) ? _madAKeyToUse : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_madAKeyToUse) };
var madBKeyToUse = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_madBKeyToUse) ? _madBKeyToUse : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_madBKeyToUse) };

var madAKeyToWrite = mAKeyToWrite;
var madBKeyToWrite = mBKeyToWrite;
var madAKeyToWrite = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_madAKeyToWrite) ? _madAKeyToWrite : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_madAKeyToWrite) };
var madBKeyToWrite = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_madBKeyToWrite) ? _madBKeyToWrite : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_madBKeyToWrite) };

try
{
Expand Down Expand Up @@ -757,13 +758,15 @@ public ERROR WriteMiFareClassicWithMAD(int _madApplicationID, int _madStartSecto

MifareAccessInfo aiToWrite = new MifareAccessInfoClass
{
UseMAD = _keyToWriteUseMAD
UseMAD = _keyToWriteUseMAD,

};
aiToWrite.MADKeyA.Value = _aKeyToUse == _aKeyToWrite ? madAKeyToUse.Value : madAKeyToWrite.Value;
aiToWrite.MADKeyB.Value = _bKeyToUse == _bKeyToWrite ? madBKeyToUse.Value : madBKeyToWrite.Value;
aiToWrite.MADKeyA.Value = _aKeyToUse == _madAKeyToWrite ? madAKeyToUse.Value : madAKeyToWrite.Value;
aiToWrite.MADKeyB.Value = _bKeyToUse == _madBKeyToWrite ? madBKeyToUse.Value : madBKeyToWrite.Value;
aiToWrite.KeyA.Value = _aKeyToUse == _aKeyToWrite ? mAKeyToUse.Value : mAKeyToWrite.Value;
aiToWrite.KeyB.Value = _bKeyToUse == _bKeyToWrite ? mBKeyToUse.Value : mBKeyToWrite.Value;
aiToWrite.MADGPB = _madGPB;
aiToWrite.SAB = _sab;

var aiToUse = new MifareAccessInfoClass
{
Expand Down Expand Up @@ -804,7 +807,7 @@ public ERROR WriteMiFareClassicWithMAD(int _madApplicationID, int _madStartSecto
return ERROR.NoError;
}

public ERROR ReadMiFareClassicWithMAD(int madApplicationID, string _aKeyToUse, string _bKeyToUse, int _length, byte _madGPB, bool _useMADToAuth = true, bool _aiToUseIsMAD = false)
public ERROR ReadMiFareClassicWithMAD(int madApplicationID, string _aKeyToUse, string _bKeyToUse, string _madAKeyToUse, string _madBKeyToUse, int _length, byte _madGPB, bool _useMADToAuth = true, bool _aiToUseIsMAD = false)
{
var settings = new SettingsReaderWriter();
Sector = new MifareClassicSectorModel();
Expand All @@ -814,8 +817,8 @@ public ERROR ReadMiFareClassicWithMAD(int madApplicationID, string _aKeyToUse, s
var mAKeyToUse = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_aKeyToUse) ? _aKeyToUse : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_aKeyToUse) };
var mBKeyToUse = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_bKeyToUse) ? _bKeyToUse : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_bKeyToUse) };

var madAKeyToUse = mAKeyToUse;
var madBKeyToUse = mBKeyToUse;
var madAKeyToUse = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_madAKeyToUse) ? _madAKeyToUse : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_madAKeyToUse) };
var madBKeyToUse = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_madBKeyToUse) ? _madBKeyToUse : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_madBKeyToUse) };

try
{
Expand Down
27 changes: 22 additions & 5 deletions RFiDGear/Model/MifareClassic/MifareClassicSectorModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ public string AccessBitsAsString
public AccessCondition_MifareClassicSectorTrailer Read_KeyB { get => SectorAccessCondition.Read_KeyB; set => SectorAccessCondition.Read_KeyB = value; }
public AccessCondition_MifareClassicSectorTrailer Write_KeyB { get => SectorAccessCondition.Read_KeyB; set => SectorAccessCondition.Read_KeyB = value; }

public LibLogicalAccess.SectorAccessBits SAB { get => sab; set => sab = value; }
private LibLogicalAccess.SectorAccessBits sab;

public bool IsAuthenticated { get; set; }
public short Cx { get => SectorAccessCondition.Cx; set => SectorAccessCondition.Cx = value; }

Expand All @@ -240,8 +243,6 @@ private bool decodeSectorTrailer(byte[] st, ref MifareClassicSectorModel _sector
{
uint C1x, C2x;

LibLogicalAccess.SectorAccessBits sab;

uint tmpAccessBitCx;

if (CustomConverter.SectorTrailerHasWrongFormat(st))
Expand Down Expand Up @@ -468,28 +469,44 @@ private string encodeSectorTrailer(MifareClassicSectorModel _sector)

// DataBlock 0 = C1/0; C2/0; C3/0

st[1] |= (byte)((dataBlock0AccessBitsIndex & 0x01) << 4); // C1/0
st[2] |= (byte)((dataBlock0AccessBitsIndex & 0x02) >> 1); // C2/0
st[2] |= (byte)((dataBlock0AccessBitsIndex & 0x04) << 2); // C3/0
st[1] |= (byte)((dataBlock0AccessBitsIndex & 0x0001) << 4); // C1/0
st[2] |= (byte)((dataBlock0AccessBitsIndex & 0x0002) >> 1); // C2/0
st[2] |= (byte)((dataBlock0AccessBitsIndex & 0x0004) << 2); // C3/0

sab.d_data_block0_access_bits.c1 |= (short)(dataBlock0AccessBitsIndex & 0x0001);
sab.d_data_block0_access_bits.c2 |= (short)((dataBlock0AccessBitsIndex & 0x0002) >> 1);
sab.d_data_block0_access_bits.c3 |= (short)((dataBlock0AccessBitsIndex & 0x0004) >> 2);

// DataBlock 1 = C1/1; C2/1; C3/1

st[1] |= (byte)((dataBlock1AccessBitsIndex & 0x01) << 5); // C1/1
st[2] |= (byte)(dataBlock1AccessBitsIndex & 0x02); // C2/1
st[2] |= (byte)((dataBlock1AccessBitsIndex & 0x04) << 3); // C3/1

sab.d_data_block1_access_bits.c1 |= (short)(dataBlock1AccessBitsIndex & 0x01);
sab.d_data_block1_access_bits.c2 |= (short)((dataBlock1AccessBitsIndex & 0x02) >> 1);
sab.d_data_block1_access_bits.c3 |= (short)((dataBlock1AccessBitsIndex & 0x04) >> 2);

// DataBlock 2 = C1/2; C2/2; C3/2

st[1] |= (byte)((dataBlock2AccessBitsIndex & 0x01) << 6); // C1/2
st[2] |= (byte)((dataBlock2AccessBitsIndex & 0x02) << 1); // C2/2
st[2] |= (byte)((dataBlock2AccessBitsIndex & 0x04) << 4); // C3/2

sab.d_data_block2_access_bits.c1 |= (short)(dataBlock2AccessBitsIndex & 0x01);
sab.d_data_block2_access_bits.c2 |= (short)((dataBlock2AccessBitsIndex & 0x02) >> 1);
sab.d_data_block2_access_bits.c3 |= (short)((dataBlock2AccessBitsIndex & 0x04) >> 2);

// SectorAccessBits = C1/3; C2/3; C3/3

st[1] |= (byte)((sectorAccessBitsIndex & 0x01) << 7); // C1/3
st[2] |= (byte)((sectorAccessBitsIndex & 0x02) << 2); // C2/3
st[2] |= (byte)((sectorAccessBitsIndex & 0x04) << 5); // C3/3

sab.d_sector_trailer_access_bits.c1 |= (short)(sectorAccessBitsIndex & 0x01);
sab.d_sector_trailer_access_bits.c2 |= (short)((sectorAccessBitsIndex & 0x02) >> 1);
sab.d_sector_trailer_access_bits.c3 |= (short)((sectorAccessBitsIndex & 0x04) >> 2);

st = CustomConverter.buildSectorTrailerInvNibble(st);
string[] stAsString;

Expand Down
Loading

0 comments on commit c133e06

Please sign in to comment.