mirror of
https://gitlab.com/fabinfra/fabaccess/nfc.git
synced 2025-03-12 14:51:51 +01:00
Added: PCSC Reader & Tests
This commit is contained in:
parent
3c43f83a89
commit
33893cb979
24
NFC.sln
24
NFC.sln
@ -5,6 +5,14 @@ VisualStudioVersion = 16.0.30717.126
|
|||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NFC", "NFC\NFC.csproj", "{1D12BCDF-033F-40DE-ABA9-8BA5ABE0CA3A}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NFC", "NFC\NFC.csproj", "{1D12BCDF-033F-40DE-ABA9-8BA5ABE0CA3A}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NFC_PCSC", "NFC_PCSC\NFC_PCSC.csproj", "{62DE4EBC-6F35-4D31-8717-DBC62D46035C}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NFC_Android", "NFC_Android\NFC_Android.csproj", "{B2609012-9D21-42F0-A2F9-3FE97D356392}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NFC_iOS", "NFC_iOS\NFC_iOS.csproj", "{C56A1E1A-976C-42ED-B7A2-08C6111AA0E8}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NFC_Test", "NFC_Test\NFC_Test.csproj", "{FE8A1426-8B19-4CDF-A75E-80397E55BA95}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -15,6 +23,22 @@ Global
|
|||||||
{1D12BCDF-033F-40DE-ABA9-8BA5ABE0CA3A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{1D12BCDF-033F-40DE-ABA9-8BA5ABE0CA3A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{1D12BCDF-033F-40DE-ABA9-8BA5ABE0CA3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{1D12BCDF-033F-40DE-ABA9-8BA5ABE0CA3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{1D12BCDF-033F-40DE-ABA9-8BA5ABE0CA3A}.Release|Any CPU.Build.0 = Release|Any CPU
|
{1D12BCDF-033F-40DE-ABA9-8BA5ABE0CA3A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{62DE4EBC-6F35-4D31-8717-DBC62D46035C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{62DE4EBC-6F35-4D31-8717-DBC62D46035C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{62DE4EBC-6F35-4D31-8717-DBC62D46035C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{62DE4EBC-6F35-4D31-8717-DBC62D46035C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{B2609012-9D21-42F0-A2F9-3FE97D356392}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{B2609012-9D21-42F0-A2F9-3FE97D356392}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{B2609012-9D21-42F0-A2F9-3FE97D356392}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{B2609012-9D21-42F0-A2F9-3FE97D356392}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{C56A1E1A-976C-42ED-B7A2-08C6111AA0E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{C56A1E1A-976C-42ED-B7A2-08C6111AA0E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{C56A1E1A-976C-42ED-B7A2-08C6111AA0E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{C56A1E1A-976C-42ED-B7A2-08C6111AA0E8}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{FE8A1426-8B19-4CDF-A75E-80397E55BA95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{FE8A1426-8B19-4CDF-A75E-80397E55BA95}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{FE8A1426-8B19-4CDF-A75E-80397E55BA95}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{FE8A1426-8B19-4CDF-A75E-80397E55BA95}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
@ -4,18 +4,17 @@ namespace NFC
|
|||||||
{
|
{
|
||||||
public class APDUCommand
|
public class APDUCommand
|
||||||
{
|
{
|
||||||
private IsoCase case4Short;
|
public APDUCommand(IsoCase isoCase)
|
||||||
|
|
||||||
public APDUCommand(IsoCase case4Short)
|
|
||||||
{
|
{
|
||||||
this.case4Short = case4Short;
|
Case = isoCase;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int CLA { get; internal set; }
|
public IsoCase Case { get; set; }
|
||||||
public byte INS { get; internal set; }
|
public byte CLA { get; set; }
|
||||||
public byte[] Data { get; internal set; }
|
public byte INS { get; set; }
|
||||||
|
public byte[] Data { get; set; }
|
||||||
|
|
||||||
internal byte[] ToArray()
|
public byte[] ToArray()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
{
|
{
|
||||||
public class APDUResponse
|
public class APDUResponse
|
||||||
{
|
{
|
||||||
public byte SW1 { get; internal set; }
|
public byte SW1 { get; set; }
|
||||||
public byte SW2 { get; internal set; }
|
public byte SW2 { get; set; }
|
||||||
public byte[] Body { get; internal set; }
|
public byte[] Body { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
public enum IsoCase
|
public enum IsoCase
|
||||||
{
|
{
|
||||||
Case4Short,
|
Case4Short,
|
||||||
Case2Short
|
Case2Short,
|
||||||
|
Case3Short,
|
||||||
|
Case1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
7
NFC_Android/NFC_Android.csproj
Normal file
7
NFC_Android/NFC_Android.csproj
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
75
NFC_PCSC/Card_PCSC.cs
Normal file
75
NFC_PCSC/Card_PCSC.cs
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
using PCSC;
|
||||||
|
using PCSC.Iso7816;
|
||||||
|
using NFC.Interfaces;
|
||||||
|
using NFC;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace NFC_PCSC
|
||||||
|
{
|
||||||
|
public class Card_PCSC : ICard
|
||||||
|
{
|
||||||
|
private IsoReader _ISOReader;
|
||||||
|
private string _ReaderID;
|
||||||
|
|
||||||
|
public Card_PCSC(IsoReader isoreader, string readerID)
|
||||||
|
{
|
||||||
|
_ISOReader = isoreader;
|
||||||
|
_ReaderID = readerID;
|
||||||
|
}
|
||||||
|
public void Connect()
|
||||||
|
{
|
||||||
|
_ISOReader.Connect(_ReaderID, SCardShareMode.Shared, SCardProtocol.Any);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Disconnect()
|
||||||
|
{
|
||||||
|
_ISOReader.Disconnect(SCardReaderDisposition.Eject);
|
||||||
|
}
|
||||||
|
|
||||||
|
public APDUResponse Transmit(APDUCommand apdu_cmd)
|
||||||
|
{
|
||||||
|
Response response = _ISOReader.Transmit(Convert(apdu_cmd));
|
||||||
|
return Convert(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandApdu Convert(APDUCommand apdu_cmd)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
return new CommandApdu(ConvertISOCase(apdu_cmd.Case), SCardProtocol.Any)
|
||||||
|
{
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public PCSC.Iso7816.IsoCase ConvertISOCase(NFC.IsoCase isoCase)
|
||||||
|
{
|
||||||
|
switch(isoCase)
|
||||||
|
{
|
||||||
|
case NFC.IsoCase.Case1:
|
||||||
|
return PCSC.Iso7816.IsoCase.Case1;
|
||||||
|
case NFC.IsoCase.Case2Short:
|
||||||
|
return PCSC.Iso7816.IsoCase.Case2Short;
|
||||||
|
case NFC.IsoCase.Case3Short:
|
||||||
|
return PCSC.Iso7816.IsoCase.Case3Short;
|
||||||
|
case NFC.IsoCase.Case4Short:
|
||||||
|
return PCSC.Iso7816.IsoCase.Case4Short;
|
||||||
|
default:
|
||||||
|
throw new Exception("Unknown IsoCase");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public APDUResponse Convert(Response response)
|
||||||
|
{
|
||||||
|
ResponseApdu responseApdu = response.Get(0);
|
||||||
|
|
||||||
|
APDUResponse apduResponse = new APDUResponse()
|
||||||
|
{
|
||||||
|
SW1 = responseApdu.SW1,
|
||||||
|
SW2 = responseApdu.SW2,
|
||||||
|
Body = responseApdu.GetData()
|
||||||
|
};
|
||||||
|
|
||||||
|
return apduResponse;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
34
NFC_PCSC/Hardware_PCSC.cs
Normal file
34
NFC_PCSC/Hardware_PCSC.cs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
using NFC.Interfaces;
|
||||||
|
using PCSC;
|
||||||
|
|
||||||
|
namespace NFC_PCSC
|
||||||
|
{
|
||||||
|
public class Hardware_PCSC : IHardware
|
||||||
|
{
|
||||||
|
public string[] GetReaders()
|
||||||
|
{
|
||||||
|
var contextFactory = ContextFactory.Instance;
|
||||||
|
using (var context = contextFactory.Establish(SCardScope.System))
|
||||||
|
{
|
||||||
|
return context.GetReaders();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsAvailable()
|
||||||
|
{
|
||||||
|
if(GetReaders().Length == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IReader OpenReader(string readerID)
|
||||||
|
{
|
||||||
|
return new Reader_PCSC(readerID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
16
NFC_PCSC/NFC_PCSC.csproj
Normal file
16
NFC_PCSC/NFC_PCSC.csproj
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="PCSC" Version="5.0.0" />
|
||||||
|
<PackageReference Include="PCSC.Iso7816" Version="5.0.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\NFC\NFC.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
50
NFC_PCSC/Reader_PCSC.cs
Normal file
50
NFC_PCSC/Reader_PCSC.cs
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
using NFC.Interfaces;
|
||||||
|
using PCSC;
|
||||||
|
using PCSC.Iso7816;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace NFC_PCSC
|
||||||
|
{
|
||||||
|
|
||||||
|
public class Reader_PCSC : IReader, IDisposable
|
||||||
|
{
|
||||||
|
private string _ReaderID;
|
||||||
|
private IContextFactory _ContextFactory;
|
||||||
|
private ISCardContext _SCardContext;
|
||||||
|
private IsoReader _ISOReader;
|
||||||
|
private ICard _Card;
|
||||||
|
|
||||||
|
public Reader_PCSC(string readerID)
|
||||||
|
{
|
||||||
|
_ReaderID = readerID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public event ReaderEventHandler CardDiscovered;
|
||||||
|
public event ReaderEventHandler CardLost;
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
Stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Start()
|
||||||
|
{
|
||||||
|
_ContextFactory = ContextFactory.Instance;
|
||||||
|
_SCardContext = _ContextFactory.Establish(SCardScope.System);
|
||||||
|
|
||||||
|
_ISOReader = new IsoReader(_SCardContext);
|
||||||
|
|
||||||
|
_Card = new Card_PCSC(_ISOReader, _ReaderID);
|
||||||
|
|
||||||
|
CardDiscovered?.Invoke(this, _Card);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Stop()
|
||||||
|
{
|
||||||
|
CardLost?.Invoke(this, _Card);
|
||||||
|
|
||||||
|
_ISOReader.Dispose();
|
||||||
|
_SCardContext.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
118
NFC_Test/APDUCommand_Test.cs
Normal file
118
NFC_Test/APDUCommand_Test.cs
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
using System;
|
||||||
|
using NFC;
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace NFC_Test.Helper
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class APDUCommand_Test
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void Compare()
|
||||||
|
{
|
||||||
|
APDUCommand command1 = new APDUCommand(IsoCase.Case4Short)
|
||||||
|
{
|
||||||
|
CLA = 0x90,
|
||||||
|
INS = 0xAA,
|
||||||
|
Data = new byte[]
|
||||||
|
{
|
||||||
|
0x01, 0x02, 0x03
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
APDUCommand command2 = new APDUCommand(IsoCase.Case4Short)
|
||||||
|
{
|
||||||
|
CLA = 0x90,
|
||||||
|
INS = 0xAA,
|
||||||
|
Data = new byte[]
|
||||||
|
{
|
||||||
|
0x01, 0x02, 0x03
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Assert.IsTrue(command1 == command2);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Compare_Diff()
|
||||||
|
{
|
||||||
|
APDUCommand command1 = new APDUCommand(IsoCase.Case4Short)
|
||||||
|
{
|
||||||
|
CLA = 0x90,
|
||||||
|
INS = 0xAA,
|
||||||
|
Data = new byte[]
|
||||||
|
{
|
||||||
|
0x01, 0x02, 0x03
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
APDUCommand command2 = new APDUCommand(IsoCase.Case4Short)
|
||||||
|
{
|
||||||
|
CLA = 0x90,
|
||||||
|
INS = 0x1A,
|
||||||
|
Data = new byte[]
|
||||||
|
{
|
||||||
|
0x01, 0x02, 0x03
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Assert.IsFalse(command1 == command2);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ToString_Case1()
|
||||||
|
{
|
||||||
|
APDUCommand command = new APDUCommand(IsoCase.Case1)
|
||||||
|
{
|
||||||
|
CLA = 0x90,
|
||||||
|
INS = 0x1A
|
||||||
|
};
|
||||||
|
|
||||||
|
Console.WriteLine(command.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ToString_Case2()
|
||||||
|
{
|
||||||
|
APDUCommand command = new APDUCommand(IsoCase.Case2Short)
|
||||||
|
{
|
||||||
|
CLA = 0x90,
|
||||||
|
INS = 0x1A
|
||||||
|
};
|
||||||
|
|
||||||
|
Console.WriteLine(command.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ToString_Case3()
|
||||||
|
{
|
||||||
|
APDUCommand command = new APDUCommand(IsoCase.Case3Short)
|
||||||
|
{
|
||||||
|
CLA = 0x90,
|
||||||
|
INS = 0x1A,
|
||||||
|
Data = new byte[]
|
||||||
|
{
|
||||||
|
0x01, 0x02, 0x03
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Console.WriteLine(command.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ToString_Case4()
|
||||||
|
{
|
||||||
|
APDUCommand command = new APDUCommand(IsoCase.Case4Short)
|
||||||
|
{
|
||||||
|
CLA = 0x90,
|
||||||
|
INS = 0x1A,
|
||||||
|
Data = new byte[]
|
||||||
|
{
|
||||||
|
0x01, 0x02, 0x03
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Console.WriteLine(command.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1136
NFC_Test/Cards/MIFARE_DESFire_Test.cs
Normal file
1136
NFC_Test/Cards/MIFARE_DESFire_Test.cs
Normal file
File diff suppressed because it is too large
Load Diff
24
NFC_Test/Helper/AES_Test.cs
Normal file
24
NFC_Test/Helper/AES_Test.cs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
using NFC.Helper;
|
||||||
|
using NFC.Helper.Crypto.Cipher;
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace NFC_Test.Helper
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class AES_Test
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void Encrypt()
|
||||||
|
{
|
||||||
|
byte[] data = HexConverter.ConvertFromHexString("8db1f942f2d7cc82f6fa1486a30f8c12104a3b07e8eb77a7ac00000000000000");
|
||||||
|
byte[] key = HexConverter.ConvertFromHexString("e7aff3361c3e85347993c3219a87d24b");
|
||||||
|
byte[] iv = HexConverter.ConvertFromHexString("00000000000000000000000000000000");
|
||||||
|
|
||||||
|
AES aes = new AES();
|
||||||
|
byte[] data_enc = aes.Encrypt(data, key, iv);
|
||||||
|
|
||||||
|
byte[] data_enc_expected = HexConverter.ConvertFromHexString("3c79d74a4969ba7123e5d8f6df24493112d221fd131a4617d0eda5d92ccc1b46");
|
||||||
|
Assert.AreEqual(data_enc_expected, data_enc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
23
NFC_Test/Helper/CRC16_Test.cs
Normal file
23
NFC_Test/Helper/CRC16_Test.cs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
using NFC.Helper;
|
||||||
|
using NFC.Helper.Crypto.CRC;
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace NFC_Test.Helper
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class CRC16_Test
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
[Ignore("Unknown Expected Data")]
|
||||||
|
public void Caluclate()
|
||||||
|
{
|
||||||
|
byte[] data = HexConverter.ConvertFromHexString("");
|
||||||
|
byte[] crc_expected = HexConverter.ConvertFromHexString("");
|
||||||
|
|
||||||
|
CRC16 crc16 = new CRC16();
|
||||||
|
byte[] crc = crc16.Calculate(data);
|
||||||
|
|
||||||
|
Assert.AreEqual(crc_expected, crc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
22
NFC_Test/Helper/CRC32_Test.cs
Normal file
22
NFC_Test/Helper/CRC32_Test.cs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
using NFC.Helper;
|
||||||
|
using NFC.Helper.Crypto.CRC;
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace NFC_Test.Helper
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class CRC32_Test
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void Caluclate()
|
||||||
|
{
|
||||||
|
byte[] data = HexConverter.ConvertFromHexString("c40045eeb8338ae8f49a032e85bb1114353010");
|
||||||
|
byte[] crc_expected = HexConverter.ConvertFromHexString("95c3894b");
|
||||||
|
|
||||||
|
CRC32 crc32 = new CRC32();
|
||||||
|
byte[] crc = crc32.Calculate(data);
|
||||||
|
|
||||||
|
Assert.AreEqual(crc_expected, crc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
34
NFC_Test/Helper/HexConverter_Test.cs
Normal file
34
NFC_Test/Helper/HexConverter_Test.cs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
using NFC.Helper;
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace NFC_Test.Helper
|
||||||
|
{
|
||||||
|
public class HexConverter_Test
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
public void ConvertFromHexString()
|
||||||
|
{
|
||||||
|
string s = "0180ff0a";
|
||||||
|
|
||||||
|
byte[] expected_s =
|
||||||
|
{
|
||||||
|
0x01, 0x80, 0xFF, 0x0A
|
||||||
|
};
|
||||||
|
|
||||||
|
Assert.AreEqual(expected_s, HexConverter.ConvertFromHexString(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ConvertToHexString()
|
||||||
|
{
|
||||||
|
byte[] s =
|
||||||
|
{
|
||||||
|
0x01, 0x80, 0xFF, 0x0A
|
||||||
|
};
|
||||||
|
|
||||||
|
string expected_s = "0180ff0a";
|
||||||
|
|
||||||
|
Assert.AreEqual(expected_s, HexConverter.ConvertToHexString(s));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
55
NFC_Test/Helper/TDES_Test.cs
Normal file
55
NFC_Test/Helper/TDES_Test.cs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
using NFC.Helper;
|
||||||
|
using NFC.Helper.Crypto.Cipher;
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace NFC_Test.Helper
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class TDES_Test
|
||||||
|
{
|
||||||
|
[Test]
|
||||||
|
[Ignore("Unknown Expected Data")]
|
||||||
|
public void Encrypt_TDES()
|
||||||
|
{
|
||||||
|
byte[] data = HexConverter.ConvertFromHexString("");
|
||||||
|
byte[] key = HexConverter.ConvertFromHexString("");
|
||||||
|
byte[] iv = HexConverter.ConvertFromHexString("0000000000000000");
|
||||||
|
|
||||||
|
TDES des = new TDES();
|
||||||
|
byte[] data_enc = des.Encrypt(data, key, iv);
|
||||||
|
|
||||||
|
byte[] data_enc_expected = HexConverter.ConvertFromHexString("");
|
||||||
|
Assert.AreEqual(data_enc_expected, data_enc);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
[Ignore("Unknown Expected Data")]
|
||||||
|
public void Encrypt_TDES_2K()
|
||||||
|
{
|
||||||
|
byte[] data = HexConverter.ConvertFromHexString("");
|
||||||
|
byte[] key = HexConverter.ConvertFromHexString("");
|
||||||
|
byte[] iv = HexConverter.ConvertFromHexString("0000000000000000");
|
||||||
|
|
||||||
|
TDES_2K des = new TDES_2K();
|
||||||
|
byte[] data_enc = des.Encrypt(data, key, iv);
|
||||||
|
|
||||||
|
byte[] data_enc_expected = HexConverter.ConvertFromHexString("");
|
||||||
|
Assert.AreEqual(data_enc_expected, data_enc);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
[Ignore("Unknown Expected Data")]
|
||||||
|
public void Encrypt_TDES_3K()
|
||||||
|
{
|
||||||
|
byte[] data = HexConverter.ConvertFromHexString("");
|
||||||
|
byte[] key = HexConverter.ConvertFromHexString("");
|
||||||
|
byte[] iv = HexConverter.ConvertFromHexString("0000000000000000");
|
||||||
|
|
||||||
|
TDES_3K des = new TDES_3K();
|
||||||
|
byte[] data_enc = des.Encrypt(data, key, iv);
|
||||||
|
|
||||||
|
byte[] data_enc_expected = HexConverter.ConvertFromHexString("");
|
||||||
|
Assert.AreEqual(data_enc_expected, data_enc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
18
NFC_Test/NFC_Test.csproj
Normal file
18
NFC_Test/NFC_Test.csproj
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="NSubstitute" Version="4.2.2" />
|
||||||
|
<PackageReference Include="NUnit" Version="3.13.1" />
|
||||||
|
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\NFC\NFC.csproj" />
|
||||||
|
<ProjectReference Include="..\NFC_PCSC\NFC_PCSC.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
34
NFC_Test/NamespaceSetUp.cs
Normal file
34
NFC_Test/NamespaceSetUp.cs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
using log4net.Config;
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace NFC_Test
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Add log4net Output to Console Out
|
||||||
|
/// </summary>
|
||||||
|
[SetUpFixture]
|
||||||
|
public class NamespaceSetUp
|
||||||
|
{
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
BasicConfigurator.Configure();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace NFC_Real_Test
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Add log4net Output to Console Out
|
||||||
|
/// </summary>
|
||||||
|
[SetUpFixture]
|
||||||
|
public class NamespaceSetUp
|
||||||
|
{
|
||||||
|
[OneTimeSetUp]
|
||||||
|
public void OneTimeSetUp()
|
||||||
|
{
|
||||||
|
BasicConfigurator.Configure();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
477
NFC_Test/REAL/REAL_DESFireCommands.cs
Normal file
477
NFC_Test/REAL/REAL_DESFireCommands.cs
Normal file
@ -0,0 +1,477 @@
|
|||||||
|
using NFC.Cards.NXP_MIFARE_DESFire;
|
||||||
|
using NFC.Cards.NXP_MIFARE_DESFire.Enums;
|
||||||
|
using NFC.Helper.Crypto;
|
||||||
|
using NFC.Interfaces;
|
||||||
|
using NFC_PCSC;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace NFC_Test.REAL
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Test all DESFire Commands with an Empty Card
|
||||||
|
/// The Test are ordered to check the Commands one by one
|
||||||
|
/// </summary>
|
||||||
|
[TestFixture, Explicit]
|
||||||
|
public class REAL_DESFireCommands
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Set ReaderID for PCSC Interface
|
||||||
|
/// You can get the ID from REAL_Reader_PCSC
|
||||||
|
/// </summary>
|
||||||
|
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 SelectApplication()
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
|
||||||
|
test_successfully = true;
|
||||||
|
|
||||||
|
card.Disconnect();
|
||||||
|
};
|
||||||
|
|
||||||
|
reader.CardDiscovered += handler;
|
||||||
|
reader.Start();
|
||||||
|
|
||||||
|
Assert.AreEqual(true, test_successfully);
|
||||||
|
|
||||||
|
reader.Stop();
|
||||||
|
reader.CardDiscovered -= handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test, Order(2)]
|
||||||
|
public void Authenticate_DES()
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
|
||||||
|
test_successfully = true;
|
||||||
|
|
||||||
|
card.Disconnect();
|
||||||
|
};
|
||||||
|
|
||||||
|
reader.CardDiscovered += handler;
|
||||||
|
reader.Start();
|
||||||
|
|
||||||
|
Assert.AreEqual(true, test_successfully);
|
||||||
|
|
||||||
|
reader.Stop();
|
||||||
|
reader.CardDiscovered -= handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test, Order(3)]
|
||||||
|
public void Format()
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
|
||||||
|
test_successfully = true;
|
||||||
|
|
||||||
|
card.Disconnect();
|
||||||
|
};
|
||||||
|
|
||||||
|
reader.CardDiscovered += handler;
|
||||||
|
reader.Start();
|
||||||
|
|
||||||
|
Assert.AreEqual(true, test_successfully);
|
||||||
|
|
||||||
|
reader.Stop();
|
||||||
|
reader.CardDiscovered -= handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test, Order(4)]
|
||||||
|
public void CreateApplication()
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
|
||||||
|
test_successfully = true;
|
||||||
|
|
||||||
|
card.Disconnect();
|
||||||
|
};
|
||||||
|
|
||||||
|
reader.CardDiscovered += handler;
|
||||||
|
reader.Start();
|
||||||
|
|
||||||
|
Assert.AreEqual(true, test_successfully);
|
||||||
|
|
||||||
|
reader.Stop();
|
||||||
|
reader.CardDiscovered -= handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test, Order(5)]
|
||||||
|
public void Authenticate_AES()
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
|
||||||
|
test_successfully = true;
|
||||||
|
|
||||||
|
card.Disconnect();
|
||||||
|
};
|
||||||
|
|
||||||
|
reader.CardDiscovered += handler;
|
||||||
|
reader.Start();
|
||||||
|
|
||||||
|
Assert.AreEqual(true, test_successfully);
|
||||||
|
|
||||||
|
reader.Stop();
|
||||||
|
reader.CardDiscovered -= handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test, Order(6)]
|
||||||
|
public void ChangeApplicationMasterKey()
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
|
||||||
|
CipherKey key_aes_new = new CipherKey(ApplicationMasterKey, CipherType.AES, 0x10);
|
||||||
|
desfire.ChangeKey_AES(0x00, key_aes_new._Key, key_aes_new._KeyVersion);
|
||||||
|
|
||||||
|
test_successfully = true;
|
||||||
|
|
||||||
|
card.Disconnect();
|
||||||
|
};
|
||||||
|
|
||||||
|
reader.CardDiscovered += handler;
|
||||||
|
reader.Start();
|
||||||
|
|
||||||
|
Assert.AreEqual(true, test_successfully);
|
||||||
|
|
||||||
|
reader.Stop();
|
||||||
|
reader.CardDiscovered -= handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test, Order(7)]
|
||||||
|
public void ChangeApplicationKey_1()
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
|
||||||
|
CipherKey key_new = new CipherKey(ApplicationKey_1, CipherType.AES, 0x10);
|
||||||
|
desfire.ChangeOtherKey_AES(0x01, key_new._Key, key_aes._Key, key_new._KeyVersion);
|
||||||
|
|
||||||
|
test_successfully = true;
|
||||||
|
|
||||||
|
card.Disconnect();
|
||||||
|
};
|
||||||
|
|
||||||
|
reader.CardDiscovered += handler;
|
||||||
|
reader.Start();
|
||||||
|
|
||||||
|
Assert.AreEqual(true, test_successfully);
|
||||||
|
|
||||||
|
reader.Stop();
|
||||||
|
reader.CardDiscovered -= handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test, Order(8)]
|
||||||
|
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.PLAIN, accesRights, FileSize);
|
||||||
|
|
||||||
|
test_successfully = true;
|
||||||
|
|
||||||
|
card.Disconnect();
|
||||||
|
};
|
||||||
|
|
||||||
|
reader.CardDiscovered += handler;
|
||||||
|
reader.Start();
|
||||||
|
|
||||||
|
Assert.AreEqual(true, test_successfully);
|
||||||
|
|
||||||
|
reader.Stop();
|
||||||
|
reader.CardDiscovered -= handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test, Order(9)]
|
||||||
|
public void WriteFile()
|
||||||
|
{
|
||||||
|
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.PLAIN, accesRights, FileSize);
|
||||||
|
|
||||||
|
desfire.WriteData(FileID, 0, Encoding.ASCII.GetBytes("Test1234"));
|
||||||
|
|
||||||
|
test_successfully = true;
|
||||||
|
|
||||||
|
card.Disconnect();
|
||||||
|
};
|
||||||
|
|
||||||
|
reader.CardDiscovered += handler;
|
||||||
|
reader.Start();
|
||||||
|
|
||||||
|
Assert.AreEqual(true, test_successfully);
|
||||||
|
|
||||||
|
reader.Stop();
|
||||||
|
reader.CardDiscovered -= handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test, Order(10)]
|
||||||
|
public void ReadFile()
|
||||||
|
{
|
||||||
|
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.PLAIN, accesRights, FileSize);
|
||||||
|
|
||||||
|
desfire.WriteData(FileID, 0, Encoding.ASCII.GetBytes("Test1234"));
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
130
NFC_Test/REAL/REAL_FabAccess_OTA.cs
Normal file
130
NFC_Test/REAL/REAL_FabAccess_OTA.cs
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
using NFC.Cards.NXP_MIFARE_DESFire;
|
||||||
|
using NFC.Cards.NXP_MIFARE_DESFire.Enums;
|
||||||
|
using NFC.Helper.Crypto;
|
||||||
|
using NFC.Interfaces;
|
||||||
|
using NFC_PCSC;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace NFC_Test.REAL
|
||||||
|
{
|
||||||
|
[TestFixture, Explicit]
|
||||||
|
public class REAL_FabAccess_OTA
|
||||||
|
{
|
||||||
|
private string _ReaderID = "ACS ACR122U PICC Interface 0";
|
||||||
|
private UInt32 _FabAccess_AID = 0x2A472D;
|
||||||
|
private byte _FabAccess_FID = 0x01;
|
||||||
|
private UInt32 _FabAccess_FSize = 0xF0;
|
||||||
|
|
||||||
|
// Change of PICC Key is not implementet yet
|
||||||
|
// private CipherKey _FabAccess_Card_MasterKey = new CipherKey("294A404E635266556A576E5A72347537", CipherType.AES, 0x10);
|
||||||
|
|
||||||
|
private CipherKey _FabAccess_Application_MasterKey = new CipherKey("50645367566B59703273357638792F42", CipherType.AES, 0x10);
|
||||||
|
private CipherKey _FabAccess_Application_AuthKey = new CipherKey("6D5A7134743677397A24432646294A40", CipherType.AES, 0x10);
|
||||||
|
|
||||||
|
private string _FabAccess_UserDomain = "verrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrylooooooooooooooooooongusssssssssssssssssssssssernaaaaaaaaaaaaaaaaaaaaaaaame@fvm.fab-access.org";
|
||||||
|
private string _FabAccess_Domain = "fvm.fab-access.org";
|
||||||
|
|
||||||
|
private CipherKey _Default_DESKey = new CipherKey(CipherType.TDES);
|
||||||
|
private CipherKey _Default_AESKey = new CipherKey(CipherType.AES);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create FabAccess Application and UserData File
|
||||||
|
/// </summary>
|
||||||
|
[Test, Order(1)]
|
||||||
|
public void Init_EmptyCard()
|
||||||
|
{
|
||||||
|
IHardware hardware = new Hardware_PCSC();
|
||||||
|
IReader reader = hardware.OpenReader(_ReaderID);
|
||||||
|
|
||||||
|
bool transmit_successfully = false;
|
||||||
|
|
||||||
|
ReaderEventHandler handler = (sender, card) =>
|
||||||
|
{
|
||||||
|
card.Connect();
|
||||||
|
|
||||||
|
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card);
|
||||||
|
|
||||||
|
desfire.SelectApplication(0x000000);
|
||||||
|
desfire.AuthenticateISO_DES(0x00, _Default_DESKey._Key);
|
||||||
|
desfire.Format();
|
||||||
|
|
||||||
|
desfire.AuthenticateISO_DES(0x00, _Default_DESKey._Key);
|
||||||
|
|
||||||
|
byte keySetting1 = desfire.GenerateKeySetting1(ChangeApplicationKey.MASTERKEY, ChangeMasterKeySettings.WITHMASTERKEY, CreateDeleteFile.ONLYMASTERKEY, FileDirectoryAccess.NOKEY, ChangeMasterKey.CHANGEABLE);
|
||||||
|
byte keySetting2 = desfire.GenerateKeySetting2(CryptoOperationsType.AES, FileIdentifies.NOTUSED, 0x02);
|
||||||
|
desfire.CreateApplication(_FabAccess_AID, keySetting1, keySetting2);
|
||||||
|
|
||||||
|
desfire.SelectApplication(_FabAccess_AID);
|
||||||
|
desfire.AuthenticateISO_AES(0x00, _Default_AESKey._Key);
|
||||||
|
|
||||||
|
desfire.ChangeKey_AES(0x00, _FabAccess_Application_MasterKey._Key, _FabAccess_Application_MasterKey._KeyVersion);
|
||||||
|
|
||||||
|
desfire.AuthenticateISO_AES(0x00, _FabAccess_Application_MasterKey._Key);
|
||||||
|
desfire.ChangeOtherKey_AES(0x01, _FabAccess_Application_AuthKey._Key, _Default_AESKey._Key, _FabAccess_Application_AuthKey._KeyVersion);
|
||||||
|
|
||||||
|
UInt16 accesRights = desfire.GenerateFileAccessRights((byte)FileAccessRights.FREE, 0x00, 0x00, 0x00);
|
||||||
|
desfire.CreateFile_Standard(_FabAccess_FID, FileCommunication.PLAIN, accesRights, _FabAccess_FSize);
|
||||||
|
|
||||||
|
desfire.WriteData(_FabAccess_FID, 0, Encoding.ASCII.GetBytes(_FabAccess_UserDomain));
|
||||||
|
|
||||||
|
transmit_successfully = true;
|
||||||
|
|
||||||
|
card.Disconnect();
|
||||||
|
};
|
||||||
|
|
||||||
|
reader.CardDiscovered += handler;
|
||||||
|
reader.Start();
|
||||||
|
|
||||||
|
Assert.AreEqual(true, transmit_successfully);
|
||||||
|
|
||||||
|
reader.Stop();
|
||||||
|
reader.CardDiscovered -= handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Authenticate with UserData File and AuthKey
|
||||||
|
/// </summary>
|
||||||
|
[Test, Order(2)]
|
||||||
|
public void Authenticate()
|
||||||
|
{
|
||||||
|
IHardware hardware = new Hardware_PCSC();
|
||||||
|
IReader reader = hardware.OpenReader(_ReaderID);
|
||||||
|
|
||||||
|
bool transmit_successfully = false;
|
||||||
|
|
||||||
|
ReaderEventHandler handler = (sender, card) =>
|
||||||
|
{
|
||||||
|
card.Connect();
|
||||||
|
|
||||||
|
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card);
|
||||||
|
|
||||||
|
desfire.SelectApplication(_FabAccess_AID);
|
||||||
|
byte[] card_data = desfire.ReadData(_FabAccess_FID, 0x00, _FabAccess_FSize);
|
||||||
|
string userdomain = Encoding.ASCII.GetString(card_data).Replace("\u0000", "");
|
||||||
|
|
||||||
|
string domain = userdomain.Split('@')[1];
|
||||||
|
if(domain != _FabAccess_Domain)
|
||||||
|
{
|
||||||
|
throw new Exception("Incorrect Domain");
|
||||||
|
}
|
||||||
|
|
||||||
|
desfire.SelectApplication(_FabAccess_AID);
|
||||||
|
desfire.AuthenticateISO_AES(0x01, _FabAccess_Application_AuthKey._Key);
|
||||||
|
|
||||||
|
transmit_successfully = true;
|
||||||
|
|
||||||
|
card.Disconnect();
|
||||||
|
};
|
||||||
|
|
||||||
|
reader.CardDiscovered += handler;
|
||||||
|
reader.Start();
|
||||||
|
|
||||||
|
Assert.AreEqual(true, transmit_successfully);
|
||||||
|
|
||||||
|
reader.Stop();
|
||||||
|
reader.CardDiscovered -= handler;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
62
NFC_Test/REAL/REAL_Reader_PCSC.cs
Normal file
62
NFC_Test/REAL/REAL_Reader_PCSC.cs
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
using NFC.Interfaces;
|
||||||
|
using NFC_PCSC;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace NFC_Test.REAL
|
||||||
|
{
|
||||||
|
[TestFixture, Explicit]
|
||||||
|
public class REAL_Reader_PCSC
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Print PCSC ReaderIDs to Console Out
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void GetReaders()
|
||||||
|
{
|
||||||
|
IHardware hardware = new Hardware_PCSC();
|
||||||
|
string[] readers = hardware.GetReaders();
|
||||||
|
|
||||||
|
Console.WriteLine("PCSC Readers detected: {0}", readers.Length);
|
||||||
|
|
||||||
|
if (readers.Length > 0)
|
||||||
|
{
|
||||||
|
Console.WriteLine("List of ReaderIDs:");
|
||||||
|
foreach (string readerID in readers)
|
||||||
|
{
|
||||||
|
Console.WriteLine("{0}", readerID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Connect to specific PCSC Reader by ReaderID
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="readerID">ReaderID from GetReaders</param>
|
||||||
|
[TestCase("ACS ACR122U PICC Interface 0")]
|
||||||
|
public void Connect(string readerID)
|
||||||
|
{
|
||||||
|
IHardware hardware = new Hardware_PCSC();
|
||||||
|
IReader reader = hardware.OpenReader(readerID);
|
||||||
|
|
||||||
|
bool connected_successfully = false;
|
||||||
|
|
||||||
|
ReaderEventHandler handler = (sender, card) =>
|
||||||
|
{
|
||||||
|
card.Connect();
|
||||||
|
|
||||||
|
connected_successfully = true;
|
||||||
|
|
||||||
|
card.Disconnect();
|
||||||
|
};
|
||||||
|
|
||||||
|
reader.CardDiscovered += handler;
|
||||||
|
reader.Start();
|
||||||
|
|
||||||
|
Assert.AreEqual(true, connected_successfully);
|
||||||
|
|
||||||
|
reader.Stop();
|
||||||
|
reader.CardDiscovered -= handler;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
7
NFC_iOS/NFC_iOS.csproj
Normal file
7
NFC_iOS/NFC_iOS.csproj
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
Loading…
x
Reference in New Issue
Block a user