Fixed FabFireCard

This commit is contained in:
TheJoKlLa 2023-02-02 22:26:06 +01:00
parent b86ecf23a2
commit b09c6dd507

View File

@ -5,6 +5,7 @@ using NFC.Helper;
using NFC.Helper.Crypto; using NFC.Helper.Crypto;
using NFC.Interfaces; using NFC.Interfaces;
using System; using System;
using System.Text;
using ZXing.Aztec.Internal; using ZXing.Aztec.Internal;
namespace Borepin.Model namespace Borepin.Model
@ -32,11 +33,13 @@ namespace Borepin.Model
/// <param name="cardConfig"></param> /// <param name="cardConfig"></param>
public void FormatCard(string readerID, CardConfig cardConfig) public void FormatCard(string readerID, CardConfig cardConfig)
{ {
CipherKey PICCKey = new CipherKey(cardConfig.PICCKey, CipherType.TDES, 0x00);
_NFCService.Connect(readerID); _NFCService.Connect(readerID);
NXP_MIFARE_DESFire card = new NXP_MIFARE_DESFire(_NFCService); NXP_MIFARE_DESFire card = new NXP_MIFARE_DESFire(_NFCService);
card.SelectApplication(0x000000); card.SelectApplication(0x000000);
card.AuthenticateISO_DES(0x00, cardConfig.PICCKey); card.AuthenticateISO_DES(0x00, PICCKey._Key);
card.Format(); card.Format();
_NFCService.Disconnect(); _NFCService.Disconnect();
@ -50,64 +53,51 @@ namespace Borepin.Model
/// <returns>Key #1 for authentication </returns> /// <returns>Key #1 for authentication </returns>
public byte[] CreateCard(string readerID, CardConfig cardConfig) public byte[] CreateCard(string readerID, CardConfig cardConfig)
{ {
CipherKey PICCKey = new CipherKey(cardConfig.PICCKey, CipherType.TDES, 0x00);
CipherKey MasterKey = new CipherKey(cardConfig.APPKey, CipherType.AES, 0x10);
CipherKey AuthKey = new CipherKey(cardConfig.GenerateRandomKey(), CipherType.AES, 0x10);
UInt32 AID = 0x464142;
CipherKey _Default_DESKey = new CipherKey(CipherType.TDES);
CipherKey _Default_AESKey = new CipherKey(CipherType.AES);
_NFCService.Connect(readerID); _NFCService.Connect(readerID);
NXP_MIFARE_DESFire card = new NXP_MIFARE_DESFire(_NFCService); NXP_MIFARE_DESFire card = new NXP_MIFARE_DESFire(_NFCService);
card.SelectApplication(0x000000);
if (cardConfig.DoFormat) if (cardConfig.DoFormat)
{ {
card.AuthenticateISO_DES(0x00, new CipherKey(CipherType.TDES)); card.AuthenticateISO_DES(0x00, _Default_DESKey._Key);
} }
else else
{ {
card.AuthenticateISO_DES(0x00, cardConfig.PICCKey); card.AuthenticateISO_DES(0x00, PICCKey._Key);
} }
byte keySettings1 = card.GenerateKeySetting1 byte keySetting1 = card.GenerateKeySetting1(ChangeApplicationKey.MASTERKEY, ChangeMasterKeySettings.WITHMASTERKEY, CreateDeleteFile.ONLYMASTERKEY, FileDirectoryAccess.NOKEY, ChangeMasterKey.CHANGEABLE);
( byte keySetting2 = card.GenerateKeySetting2(CryptoOperationsType.AES, FileIdentifies.NOTUSED, 0x02);
0x00, card.CreateApplication(AID, keySetting1, keySetting2);
ChangeMasterKeySettings.WITHMASTERKEY,
CreateDeleteFile.NOKEY,
FileDirectoryAccess.NOKEY,
ChangeMasterKey.CHANGEABLE
);
byte keySettings2 = card.GenerateKeySetting2 card.SelectApplication(AID);
( card.AuthenticateISO_AES(0x00, _Default_AESKey._Key);
CryptoOperationsType.AES,
FileIdentifies.NOTUSED,
0x02
);
card.CreateApplication(0x464142, keySettings1, keySettings2); card.ChangeKey_AES(0x00, MasterKey._Key, MasterKey._KeyVersion);
card.SelectApplication(0x464142); card.AuthenticateISO_AES(0x00, MasterKey._Key);
card.AuthenticateISO_AES(0x00, new CipherKey(CipherType.AES)); card.ChangeOtherKey_AES(0x01, AuthKey._Key, _Default_AESKey._Key, AuthKey._KeyVersion);
ushort file_access = card.GenerateFileAccessRights
(
FileAccessRights.FREE,
0x00,
0x00,
0x00
);
card.CreateFile_Standard(0x01, FileCommunication.PLAIN, file_access, cardConfig.MetaInfo.Length);
card.CreateFile_Standard(0x02, FileCommunication.PLAIN, file_access, cardConfig.SpaceInfo.Length);
card.CreateFile_Standard(0x03, FileCommunication.PLAIN, file_access, cardConfig.CardToken.Length);
card.WriteData(0x00, 0x00, cardConfig.MetaInfo); UInt16 accesRights = card.GenerateFileAccessRights((byte)FileAccessRights.FREE, 0x00, 0x00, 0x00);
card.WriteData(0x00, 0x00, cardConfig.SpaceInfo);
card.WriteData(0x00, 0x00, cardConfig.CardToken);
auth_key = cardConfig.GenerateRandomKey(); card.CreateFile_Standard(0x01, FileCommunication.PLAIN, accesRights, (uint)cardConfig.MetaInfo.Length);
card.CreateFile_Standard(0x02, FileCommunication.PLAIN, accesRights, (uint)cardConfig.SpaceInfo.Length);
card.CreateFile_Standard(0x03, FileCommunication.PLAIN, accesRights, (uint)cardConfig.CardToken.Length);
card.ChangeOtherKey_AES(0x01, auth_key, new CipherKey(CipherType.AES), 0x00); card.WriteData(0x01, 0x00, cardConfig.MetaInfo);
card.ChangeKey_AES(0x00, cardConfig.APPKey, 0x00); card.WriteData(0x02, 0x00, cardConfig.SpaceInfo);
card.WriteData(0x03, 0x00, cardConfig.CardToken);
_NFCService.Disconnect(); _NFCService.Disconnect();
return auth_key; return AuthKey._Key;
} }
#endregion #endregion
} }