borepin/NFC_Test/REAL_Windows_CardSetUp.cs
2020-09-26 18:02:44 +02:00

192 lines
5.8 KiB
C#

using NFC;
using NFC.Mifare_DESFire;
using NFC.Mifare_DESFire.Enums;
using NFC.Readers.PCSC;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Text;
namespace NFC_Test
{
[TestFixture]
public class REAL_Windows_CardSetUp
{
public static UInt32 FabAccessAID = 0x001100;
public static string CardReaderID = "ACS ACR122U PICC Interface 0";
public static byte[] PICC_MasterKey = new byte[]
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
public static byte[] APP_MasterKey = new byte[]
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
public static byte[] APP_Key_1 = new byte[]
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
public static byte[] APP_Key_2 = new byte[]
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
public static byte[] Empty_Key = new byte[]
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
public static byte FabAccessIdentFileID = 0x01;
public static string UserDomain = "user1@fabaccess.org";
[Test]
public void ResetCard()
{
IHardware hardware = new Hardware();
IReader reader = hardware.OpenReader(CardReaderID);
bool connected_successfully = false;
ReaderEventHandler handler = (sender, card) =>
{
card.Connect();
MifareDESFire mifareDESFire = new MifareDESFire(card);
mifareDESFire.Authenticate(0x01, APP_MasterKey);
mifareDESFire.Format();
connected_successfully = true;
card.Disconnect();
};
reader.CardDiscovered += handler;
reader.Start();
Assert.AreEqual(true, connected_successfully);
reader.Stop();
reader.CardDiscovered -= handler;
}
[Test]
public void ProvisionCard()
{
IHardware hardware = new Hardware();
IReader reader = hardware.OpenReader(CardReaderID);
bool connected_successfully = false;
ReaderEventHandler handler = (sender, card) =>
{
card.Connect();
MifareDESFire mifareDESFire = new MifareDESFire(card);
mifareDESFire.Authenticate(0x01, APP_MasterKey);
mifareDESFire.CreateApplication(FabAccessAID);
mifareDESFire.SelectApplication(FabAccessAID);
mifareDESFire.Authenticate(0x00, Empty_Key);
mifareDESFire.ChangeApplicationMasterKey(APP_MasterKey);
mifareDESFire.Authenticate(0x00, APP_MasterKey);
mifareDESFire.ChangeApplicationKey(0x01, APP_Key_1);
connected_successfully = true;
card.Disconnect();
};
reader.CardDiscovered += handler;
reader.Start();
Assert.AreEqual(true, connected_successfully);
reader.Stop();
reader.CardDiscovered -= handler;
}
[Test]
public void SetUpUserCard()
{
IHardware hardware = new Hardware();
IReader reader = hardware.OpenReader(CardReaderID);
bool connected_successfully = false;
ReaderEventHandler handler = (sender, card) =>
{
card.Connect();
MifareDESFire mifareDESFire = new MifareDESFire(card);
mifareDESFire.SelectApplication(FabAccessAID);
mifareDESFire.Authenticate(0x00, Empty_Key);
UInt16 fileAccessRight = mifareDESFire.GenerateFileAccessRight(AccessRights.FREE, 0x00, 0x00, 0x00);
mifareDESFire.CreateFile(FabAccessIdentFileID, FileCommunication.PLAIN, fileAccessRight, (UInt32)0x90);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
mifareDESFire.WirteData(FabAccessIdentFileID, 0x00000000, 0x00000000, enc.GetBytes(UserDomain));
connected_successfully = true;
card.Disconnect();
};
reader.CardDiscovered += handler;
reader.Start();
Assert.AreEqual(true, connected_successfully);
reader.Stop();
reader.CardDiscovered -= handler;
}
[Test]
public void AuthenticateCard()
{
IHardware hardware = new Hardware();
IReader reader = hardware.OpenReader(CardReaderID);
bool connected_successfully = false;
ReaderEventHandler handler = (sender, card) =>
{
card.Connect();
MifareDESFire mifareDESFire = new MifareDESFire(card);
mifareDESFire.SelectApplication(FabAccessAID);
byte[] filedata = mifareDESFire.ReadData(FabAccessIdentFileID, 0x00000000, 0x00000000);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
string userdomain = enc.GetString(filedata);
Console.WriteLine(userdomain);
mifareDESFire.Authenticate(0x01, APP_Key_1);
connected_successfully = true;
card.Disconnect();
};
reader.CardDiscovered += handler;
reader.Start();
Assert.AreEqual(true, connected_successfully);
reader.Stop();
reader.CardDiscovered -= handler;
}
}
}