From d03c9cacdb0e1a1191a04f77165041b8b63b35ec Mon Sep 17 00:00:00 2001 From: TheJoKlLa Date: Sun, 29 Jan 2023 18:43:53 +0100 Subject: [PATCH] Updated to .NET 7 --- NFC/APDUResponse.cs | 17 ++- .../NXP MIFARE DESFire/NXP_MIFARE_DESFire.cs | 26 +++++ NFC/NFC.csproj | 4 +- NFC_Android/NFC_Android.csproj | 4 +- NFC_PCSC/NFC_PCSC.csproj | 4 +- NFC_Test/Cards/MIFARE_DESFire_Test.cs | 4 +- NFC_Test/NFC_Test.csproj | 2 +- NFC_Test/REAL/REAL_EncFile.cs | 100 ++++++++++++++++++ NFC_iOS/NFC_iOS.csproj | 2 +- 9 files changed, 152 insertions(+), 11 deletions(-) create mode 100644 NFC_Test/REAL/REAL_EncFile.cs diff --git a/NFC/APDUResponse.cs b/NFC/APDUResponse.cs index 4b98a3c..f3fcc73 100644 --- a/NFC/APDUResponse.cs +++ b/NFC/APDUResponse.cs @@ -1,9 +1,24 @@ -namespace NFC +using System; + +namespace NFC { public class APDUResponse { public byte SW1 { get; set; } public byte SW2 { get; set; } public byte[] Body { get; set; } + + public override string ToString() + { + if(Body != null) + { + return string.Format("SW: {0:x} {1:x} Body: {2:x}", SW1, SW2, BitConverter.ToString(Body).Replace("-", "").ToLower()); + } + else + { + return string.Format("SW: {0:x} {1:x} Body: null", SW1, SW2); + } + + } } } diff --git a/NFC/Cards/NXP MIFARE DESFire/NXP_MIFARE_DESFire.cs b/NFC/Cards/NXP MIFARE DESFire/NXP_MIFARE_DESFire.cs index 35a15dd..c45af86 100644 --- a/NFC/Cards/NXP MIFARE DESFire/NXP_MIFARE_DESFire.cs +++ b/NFC/Cards/NXP MIFARE DESFire/NXP_MIFARE_DESFire.cs @@ -5,6 +5,10 @@ using NFC.Helper; using NFC.Helper.Crypto.Cipher; using NFC.Helper.Crypto.CRC; using NFC.Interfaces; +using Org.BouncyCastle.Crypto; +using Org.BouncyCastle.Crypto.Engines; +using Org.BouncyCastle.Crypto.Macs; +using Org.BouncyCastle.Crypto.Parameters; using System; using System.Collections.Generic; using System.Linq; @@ -738,6 +742,28 @@ namespace NFC.Cards.NXP_MIFARE_DESFire }; _Log.Debug(cmd_WriteData.ToString()); + + + + + IBlockCipher cipher = new AesEngine(); + CMac mac = new CMac(cipher, 8 * 8); + KeyParameter keyParameter = new KeyParameter(_SessionKey); + + mac.Init(keyParameter); + + byte[] mac_buffer = ByteOperation.GenerateEmptyArray(8); + + byte[] cmd_raw_buffer = cmd_WriteData.ToArray(); + mac.BlockUpdate(ByteOperation.Concatenate(new byte[] { 0x90, 0x3D }, cmd_WriteData.Data), 0, cmd_WriteData.Data.Length); + mac.DoFinal(mac_buffer, 0); + + cmd_WriteData.Data = ByteOperation.Concatenate(cmd_WriteData.Data, mac_buffer); + _Log.Debug(cmd_WriteData.ToString()); + + + + APDUResponse response = _Card.Transmit(cmd_WriteData); _Log.Debug(response.ToString()); diff --git a/NFC/NFC.csproj b/NFC/NFC.csproj index bcc5c3f..0d585ed 100644 --- a/NFC/NFC.csproj +++ b/NFC/NFC.csproj @@ -1,7 +1,7 @@ - + - netstandard2.0 + netstandard2.1 diff --git a/NFC_Android/NFC_Android.csproj b/NFC_Android/NFC_Android.csproj index cb63190..082dac9 100644 --- a/NFC_Android/NFC_Android.csproj +++ b/NFC_Android/NFC_Android.csproj @@ -1,7 +1,7 @@ - + - netcoreapp3.1 + net7.0 diff --git a/NFC_PCSC/NFC_PCSC.csproj b/NFC_PCSC/NFC_PCSC.csproj index 56d2f70..f9d0cdc 100644 --- a/NFC_PCSC/NFC_PCSC.csproj +++ b/NFC_PCSC/NFC_PCSC.csproj @@ -1,7 +1,7 @@ - + - netcoreapp3.1 + net7.0 diff --git a/NFC_Test/Cards/MIFARE_DESFire_Test.cs b/NFC_Test/Cards/MIFARE_DESFire_Test.cs index 8777815..816c923 100644 --- a/NFC_Test/Cards/MIFARE_DESFire_Test.cs +++ b/NFC_Test/Cards/MIFARE_DESFire_Test.cs @@ -423,9 +423,9 @@ namespace NFC_Test.Cards SW2 = 0x00 }; - card.Transmit(Arg.Is(x => HexConverter.ConvertToHexString(x.ToArray()) == "905a00000333221100")).Returns(response); + card.Transmit(Arg.Is(x => HexConverter.ConvertToHexString(x.ToArray()) == "905a00000342414600")).Returns(response); - desfire.SelectApplication(0x112233); + desfire.SelectApplication(0x464142); } [Test] diff --git a/NFC_Test/NFC_Test.csproj b/NFC_Test/NFC_Test.csproj index 807f203..d2f6e30 100644 --- a/NFC_Test/NFC_Test.csproj +++ b/NFC_Test/NFC_Test.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net7.0 diff --git a/NFC_Test/REAL/REAL_EncFile.cs b/NFC_Test/REAL/REAL_EncFile.cs new file mode 100644 index 0000000..c02c879 --- /dev/null +++ b/NFC_Test/REAL/REAL_EncFile.cs @@ -0,0 +1,100 @@ +using NFC; +using NFC.Cards.NXP_MIFARE_DESFire; +using NFC.Cards.NXP_MIFARE_DESFire.Enums; +using NFC.Helper; +using NFC.Helper.Crypto; +using NFC.Interfaces; +using NFC_PCSC; +using NUnit.Framework; +using System; +using System.Text; + +namespace NFC_Test.REAL +{ + /// + /// Test all DESFire Commands with an Empty Card + /// The Test are ordered to check the Commands one by one + /// + [TestFixture, Explicit] + public class REAL_EncFile + { + /// + /// Set ReaderID for PCSC Interface + /// You can get the ID from REAL_Reader_PCSC + /// + public readonly string ReaderID = "ACS ACR122U PICC Interface 0"; + + #region Fixed Config Properties + public readonly UInt32 ApplicationID = 0xAAFFEE; + public readonly string ApplicationMasterKey = "25432A462D4A614E645267556B587032"; + public readonly string ApplicationKey_1 = "25432A462D4A614E645267556B587032"; + public readonly byte FileID = 0x01; + public readonly byte FileSize = 0xF0; + #endregion + + [Test, Order(1)] + public void CreateFile() + { + IHardware hardware = new Hardware_PCSC(); + IReader reader = hardware.OpenReader(ReaderID); + + bool test_successfully = false; + + ReaderEventHandler handler = (sender, card) => + { + card.Connect(); + + NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card); + + desfire.SelectApplication(0x000000); + + CipherKey key = new CipherKey(CipherType.TDES); + desfire.AuthenticateISO_DES(0x00, key._Key); + + desfire.Format(); + + desfire.AuthenticateISO_DES(0x00, key._Key); + + byte keysetting1 = desfire.GenerateKeySetting1(ChangeApplicationKey.MASTERKEY, ChangeMasterKeySettings.WITHMASTERKEY, CreateDeleteFile.ONLYMASTERKEY, FileDirectoryAccess.NOKEY, ChangeMasterKey.CHANGEABLE); + byte keysetting2 = desfire.GenerateKeySetting2(CryptoOperationsType.AES, FileIdentifies.NOTUSED, 2); + + desfire.CreateApplication(ApplicationID, keysetting1, keysetting2); + + desfire.SelectApplication(ApplicationID); + + CipherKey key_aes = new CipherKey(CipherType.AES); + desfire.AuthenticateISO_AES(0x00, key_aes._Key); + + UInt16 accesRights = desfire.GenerateFileAccessRights((byte)FileAccessRights.FREE, 0x00, 0x00, 0x00); + desfire.CreateFile_Standard(FileID, FileCommunication.ENCRYPT, accesRights, FileSize); + + desfire.WriteData(FileID, 0, Encoding.ASCII.GetBytes("Test1234")); + + APDUCommand cmd_WriteData = new APDUCommand(IsoCase.Case4Short) + { + CLA = 0x90, + INS = 0x3D, + Data = ByteOperation.GenerateEmptyArray(8) + }; + Console.WriteLine(cmd_WriteData.ToString()); + APDUResponse response = card.Transmit(cmd_WriteData); + Console.WriteLine(response.ToString()); + + byte[] data = desfire.ReadData(FileID, 0, FileSize); + Console.WriteLine(Encoding.ASCII.GetString(data).Replace("\u0000", "")); + + test_successfully = true; + + card.Disconnect(); + }; + + reader.CardDiscovered += handler; + reader.Start(); + + Assert.AreEqual(true, test_successfully); + + reader.Stop(); + reader.CardDiscovered -= handler; + } + } +} diff --git a/NFC_iOS/NFC_iOS.csproj b/NFC_iOS/NFC_iOS.csproj index a285d64..d6b49b6 100644 --- a/NFC_iOS/NFC_iOS.csproj +++ b/NFC_iOS/NFC_iOS.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1 + net7.0