From b09c6dd507c6038f6fa5a4902978f9df77503391 Mon Sep 17 00:00:00 2001 From: TheJoKlLa Date: Thu, 2 Feb 2023 22:26:06 +0100 Subject: [PATCH] Fixed FabFireCard --- Borepin/Borepin/Model/FabFireCard.cs | 72 ++++++++++++---------------- 1 file changed, 31 insertions(+), 41 deletions(-) diff --git a/Borepin/Borepin/Model/FabFireCard.cs b/Borepin/Borepin/Model/FabFireCard.cs index 0d5fb37..fccb91e 100644 --- a/Borepin/Borepin/Model/FabFireCard.cs +++ b/Borepin/Borepin/Model/FabFireCard.cs @@ -5,6 +5,7 @@ using NFC.Helper; using NFC.Helper.Crypto; using NFC.Interfaces; using System; +using System.Text; using ZXing.Aztec.Internal; namespace Borepin.Model @@ -32,11 +33,13 @@ namespace Borepin.Model /// public void FormatCard(string readerID, CardConfig cardConfig) { + CipherKey PICCKey = new CipherKey(cardConfig.PICCKey, CipherType.TDES, 0x00); + _NFCService.Connect(readerID); NXP_MIFARE_DESFire card = new NXP_MIFARE_DESFire(_NFCService); card.SelectApplication(0x000000); - card.AuthenticateISO_DES(0x00, cardConfig.PICCKey); + card.AuthenticateISO_DES(0x00, PICCKey._Key); card.Format(); _NFCService.Disconnect(); @@ -50,64 +53,51 @@ namespace Borepin.Model /// Key #1 for authentication public byte[] CreateCard(string readerID, CardConfig cardConfig) { - _NFCService.Connect(readerID); + 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); 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 { - card.AuthenticateISO_DES(0x00, cardConfig.PICCKey); + card.AuthenticateISO_DES(0x00, PICCKey._Key); } - byte keySettings1 = card.GenerateKeySetting1 - ( - 0x00, - ChangeMasterKeySettings.WITHMASTERKEY, - CreateDeleteFile.NOKEY, - FileDirectoryAccess.NOKEY, - ChangeMasterKey.CHANGEABLE - ); + byte keySetting1 = card.GenerateKeySetting1(ChangeApplicationKey.MASTERKEY, ChangeMasterKeySettings.WITHMASTERKEY, CreateDeleteFile.ONLYMASTERKEY, FileDirectoryAccess.NOKEY, ChangeMasterKey.CHANGEABLE); + byte keySetting2 = card.GenerateKeySetting2(CryptoOperationsType.AES, FileIdentifies.NOTUSED, 0x02); + card.CreateApplication(AID, keySetting1, keySetting2); - byte keySettings2 = card.GenerateKeySetting2 - ( - CryptoOperationsType.AES, - FileIdentifies.NOTUSED, - 0x02 - ); + card.SelectApplication(AID); + card.AuthenticateISO_AES(0x00, _Default_AESKey._Key); - card.CreateApplication(0x464142, keySettings1, keySettings2); + card.ChangeKey_AES(0x00, MasterKey._Key, MasterKey._KeyVersion); - card.SelectApplication(0x464142); - card.AuthenticateISO_AES(0x00, new CipherKey(CipherType.AES)); - 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.AuthenticateISO_AES(0x00, MasterKey._Key); + card.ChangeOtherKey_AES(0x01, AuthKey._Key, _Default_AESKey._Key, AuthKey._KeyVersion); - card.WriteData(0x00, 0x00, cardConfig.MetaInfo); - card.WriteData(0x00, 0x00, cardConfig.SpaceInfo); - card.WriteData(0x00, 0x00, cardConfig.CardToken); + UInt16 accesRights = card.GenerateFileAccessRights((byte)FileAccessRights.FREE, 0x00, 0x00, 0x00); - 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.ChangeKey_AES(0x00, cardConfig.APPKey, 0x00); + card.WriteData(0x01, 0x00, cardConfig.MetaInfo); + card.WriteData(0x02, 0x00, cardConfig.SpaceInfo); + card.WriteData(0x03, 0x00, cardConfig.CardToken); _NFCService.Disconnect(); - return auth_key; + return AuthKey._Key; } #endregion }