Added: Card Actions in Test

This commit is contained in:
TheJoKlLa 2020-09-26 18:02:44 +02:00
parent 04a41322e6
commit b9708da234
12 changed files with 377 additions and 126 deletions

View File

@ -0,0 +1,8 @@
namespace NFC.Mifare_DESFire.Enums
{
public enum AccessRights : byte
{
FREE = 0x0E,
NEVER = 0x0F
}
}

View File

@ -0,0 +1,21 @@
namespace NFC.Mifare_DESFire.Enums
{
/// <summary>
/// hold the Access Rights for changing application keys (Change Key command)
/// </summary>
public enum ChangeApplicationKey : byte
{
/// <summary>
/// Application master key authentication is necessary to change any key (default)
/// </summary>
MASTERKEY = 0x00,
/// <summary>
/// Authentication with the key to be changed (same Key#) is necessary to change a key
/// </summary>
SAMEKEY = 0x0E,
/// <summary>
/// All keys (except application master key, see Bit 0) within this application are frozen
/// </summary>
ALLKEYS = 0x0F
}
}

View File

@ -0,0 +1,18 @@
namespace NFC.Mifare_DESFire.Enums
{
/// <summary>
/// codes whether the application master key is changeable
/// </summary>
public enum ChangeMasterKey : byte
{
/// <summary>
/// Application master key is not changeable anymore (frozen)
/// </summary>
FROZEN = 0x00,
/// <summary>
/// Application master key is changeable (authentication with the current application master key necessary, default)
/// </summary>
CHANGEABLE = 0x01,
}
}

View File

@ -0,0 +1,19 @@
namespace NFC.Mifare_DESFire.Enums
{
/// <summary>
/// codes whether a change of the application master key settings is allowed
/// </summary>
public enum ChangeMasterKeySettings : byte
{
/// <summary>
/// configuration not changeable anymore (frozen)
/// </summary>
FROZEN = 0x00,
/// <summary>
/// this configuration is changeable if authenticated with the application master key (default)
/// </summary>
WITHMASTERKEY = 0x08
}
}

View File

@ -0,0 +1,18 @@
namespace NFC.Mifare_DESFire.Enums
{
/// <summary>
/// codes whether application master key authentication is needed before “Create File” / “Delete File”
/// </summary>
public enum CreateDeleteFile : byte
{
/// <summary>
/// “Create File”/ “Delete File”is permitted only with application master key authentication
/// </summary>
ONLYMASTERKEY = 0x00,
/// <summary>
/// “Create File”/ “Delete File”is permitted also without application master key authentication (default)
/// </summary>
NOKEY = 0x04,
}
}

View File

@ -0,0 +1,12 @@
namespace NFC.Mifare_DESFire.Enums
{
/// <summary>
/// Crypto method of the application
/// </summary>
public enum CryptoOperationsType : byte
{
TDES = 0x00,
TKTDES = 0x40,
AES = 0x80,
}
}

View File

@ -1,6 +1,6 @@
namespace NFC.Mifare_DESFire namespace NFC.Mifare_DESFire.Enums
{ {
enum FileCommunication : byte public enum FileCommunication : byte
{ {
/// <summary> /// <summary>
/// "Plain communication" /// "Plain communication"

View File

@ -0,0 +1,18 @@
namespace NFC.Mifare_DESFire.Enums
{
/// <summary>
/// codes whether application master key authentication is needed for file directory access
/// </summary>
public enum FileDirectoryAccess : byte
{
/// <summary>
/// Successful application master key authentication is required for executing the “Get FID List”, “Get File Settings”and “Get Key Settings”commands
/// </summary>
ONLYMASTERKEY = 0x00,
/// <summary>
/// “Get FID List”, “Get File Settings” and “Get Key Settings” commands succeed independentlyof a preceding application master key authentication (default)
/// </summary>
NOKEY = 0x02,
}
}

View File

@ -0,0 +1,11 @@
namespace NFC.Mifare_DESFire.Enums
{
/// <summary>
/// Indicates use of 2 byte ISO/IEC 7816-4 File Identifies for files within the Application
/// </summary>
public enum FileIdentifies : byte
{
NOTUSED = 0x00,
USED = 0x20
}
}

View File

@ -1,4 +1,5 @@
using PCSC.Iso7816; using NFC.Mifare_DESFire.Enums;
using PCSC.Iso7816;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
@ -7,6 +8,13 @@ namespace NFC.Mifare_DESFire
{ {
public class MifareDESFire public class MifareDESFire
{ {
private ICard _Card;
public MifareDESFire(ICard card)
{
_Card = card;
}
/// <summary> /// <summary>
/// Create new Application with AID /// Create new Application with AID
/// </summary> /// </summary>
@ -46,6 +54,16 @@ namespace NFC.Mifare_DESFire
return applicationIDs.ToArray(); return applicationIDs.ToArray();
} }
public void Format()
{
throw new NotImplementedException();
}
public void Authenticate(int v, byte[] aPP_MasterKey)
{
throw new NotImplementedException();
}
/// <summary> /// <summary>
/// Select Application by ID /// Select Application by ID
/// </summary> /// </summary>
@ -70,20 +88,9 @@ namespace NFC.Mifare_DESFire
return cmd; return cmd;
} }
public APDUCommand Authenticate_GetChallenge(byte keyid) public void ChangeApplicationMasterKey(byte[] aPP_MasterKey)
{ {
APDUCommand cmd = new APDUCommand(IsoCase.Case4Short) throw new NotImplementedException();
{
CLA = 0x90,
INS = (byte)APDUInstructions.,
Data = new byte[]
{
id_byte[0],
id_byte[1],
id_byte[2]
},
Le = 0x00
};
} }
/// <summary> /// <summary>
@ -110,6 +117,11 @@ namespace NFC.Mifare_DESFire
return cmd; return cmd;
} }
public void ChangeApplicationKey(int v, byte[] aPP_Key_1)
{
throw new NotImplementedException();
}
/// <summary> /// <summary>
/// Select Application by ID /// Select Application by ID
/// </summary> /// </summary>
@ -136,6 +148,31 @@ namespace NFC.Mifare_DESFire
return cmd; return cmd;
} }
public void CreateFile(byte fabAccessIdentFileID, FileCommunication pLAIN, ushort fileAccessRight, int v)
{
throw new NotImplementedException();
}
public ushort GenerateFileAccessRight(AccessRights fREE, int v1, int v2, int v3)
{
throw new NotImplementedException();
}
public void WirteData(byte fabAccessIdentFileID, int v1, int v2, byte[] vs)
{
throw new NotImplementedException();
}
public void CreateFile(byte fabAccessIdentFileID, object plain, AccessRights fREE, int v1, int v2, int v3)
{
throw new NotImplementedException();
}
public byte[] ReadData(byte identFileID, int v1, int v2)
{
throw new NotImplementedException();
}
/// <summary> /// <summary>
/// Genearte KeySetting1 for Application Settings or PICC Setting /// Genearte KeySetting1 for Application Settings or PICC Setting
/// </summary> /// </summary>
@ -163,7 +200,7 @@ namespace NFC.Mifare_DESFire
/// </summary> /// </summary>
/// <param name="numberOfKeys">Number of keys that can be stored within the application (0x01-0x0D)</param> /// <param name="numberOfKeys">Number of keys that can be stored within the application (0x01-0x0D)</param>
/// <returns></returns> /// <returns></returns>
public byte GenerateKeySetting2(CryptoOperations cryptoOperations, FileIdentifies fileIdentifies, byte numberOfKeys) public byte GenerateKeySetting2(CryptoOperationsType cryptoOperations, FileIdentifies fileIdentifies, byte numberOfKeys)
{ {
if(numberOfKeys < 0x01 || numberOfKeys >= 0x0D) if(numberOfKeys < 0x01 || numberOfKeys >= 0x0D)
{ {
@ -172,108 +209,5 @@ namespace NFC.Mifare_DESFire
return (byte)((byte)cryptoOperations | (byte)fileIdentifies | numberOfKeys); return (byte)((byte)cryptoOperations | (byte)fileIdentifies | numberOfKeys);
} }
/// <summary>
/// Crypto method of the application
/// </summary>
public enum CryptoOperations : byte
{
TDES = 0x00,
TKTDES = 0x40,
AES = 0x80,
}
/// <summary>
/// Indicates use of 2 byte ISO/IEC 7816-4 File Identifies for files within the Application
/// </summary>
public enum FileIdentifies : byte
{
NOTUSED = 0x00,
USED = 0x20
}
/// <summary>
/// hold the Access Rights for changing application keys (Change Key command)
/// </summary>
public enum ChangeApplicationKey : byte
{
/// <summary>
/// Application master key authentication is necessary to change any key (default)
/// </summary>
MASTERKEY = 0x00,
/// <summary>
/// Authentication with the key to be changed (same Key#) is necessary to change a key
/// </summary>
SAMEKEY = 0x0E,
/// <summary>
/// All keys (except application master key, see Bit 0) within this application are frozen
/// </summary>
ALLKEYS = 0x0F
}
/// <summary>
/// codes whether a change of the application master key settings is allowed
/// </summary>
public enum ChangeMasterKeySettings : byte
{
/// <summary>
/// configuration not changeable anymore (frozen)
/// </summary>
FROZEN = 0x00,
/// <summary>
/// this configuration is changeable if authenticated with the application master key (default)
/// </summary>
WITHMASTERKEY = 0x08
}
/// <summary>
/// codes whether application master key authentication is needed before “Create File” / “Delete File”
/// </summary>
public enum CreateDeleteFile : byte
{
/// <summary>
/// “Create File”/ “Delete File”is permitted only with application master key authentication
/// </summary>
ONLYMASTERKEY = 0x00,
/// <summary>
/// “Create File”/ “Delete File”is permitted also without application master key authentication (default)
/// </summary>
NOKEY = 0x04,
}
/// <summary>
/// codes whether application master key authentication is needed for file directory access
/// </summary>
public enum FileDirectoryAccess : byte
{
/// <summary>
/// Successful application master key authentication is required for executing the “Get FID List”, “Get File Settings”and “Get Key Settings”commands
/// </summary>
ONLYMASTERKEY = 0x00,
/// <summary>
/// “Get FID List”, “Get File Settings” and “Get Key Settings” commands succeed independentlyof a preceding application master key authentication (default)
/// </summary>
NOKEY = 0x02,
}
/// <summary>
/// codes whether the application master key is changeable
/// </summary>
public enum ChangeMasterKey : byte
{
/// <summary>
/// Application master key is not changeable anymore (frozen)
/// </summary>
FROZEN = 0x00,
/// <summary>
/// Application master key is changeable (authentication with the current application master key necessary, default)
/// </summary>
CHANGEABLE = 0x01,
}
} }
} }

View File

@ -6,6 +6,7 @@ using NFC;
using NFC.Readers.PCSC; using NFC.Readers.PCSC;
using System.Threading; using System.Threading;
using NFC.Mifare_DESFire; using NFC.Mifare_DESFire;
using NFC.Mifare_DESFire.Enums;
namespace NFC_Test namespace NFC_Test
{ {
@ -68,7 +69,7 @@ namespace NFC_Test
{ {
card.Connect(); card.Connect();
MifareDESFire desfire = new MifareDESFire(); MifareDESFire desfire = new MifareDESFire(card);
APDUCommand cmd = desfire.GetApplicationIDs(); APDUCommand cmd = desfire.GetApplicationIDs();
@ -110,7 +111,7 @@ namespace NFC_Test
{ {
card.Connect(); card.Connect();
MifareDESFire desfire = new MifareDESFire(); MifareDESFire desfire = new MifareDESFire(card);
APDUCommand cmd = desfire.SelectApplication(applicationID); APDUCommand cmd = desfire.SelectApplication(applicationID);
@ -147,7 +148,7 @@ namespace NFC_Test
{ {
card.Connect(); card.Connect();
MifareDESFire desfire = new MifareDESFire(); MifareDESFire desfire = new MifareDESFire(card);
APDUCommand cmd = desfire.DeleteApplication(applicationID); APDUCommand cmd = desfire.DeleteApplication(applicationID);
@ -184,10 +185,10 @@ namespace NFC_Test
{ {
card.Connect(); card.Connect();
MifareDESFire desfire = new MifareDESFire(); MifareDESFire desfire = new MifareDESFire(card);
byte keysetting1 = desfire.GenerateKeySetting1(MifareDESFire.ChangeApplicationKey.SAMEKEY, MifareDESFire.ChangeMasterKeySettings.WITHMASTERKEY, MifareDESFire.CreateDeleteFile.NOKEY, MifareDESFire.FileDirectoryAccess.NOKEY, MifareDESFire.ChangeMasterKey.CHANGEABLE); byte keysetting1 = desfire.GenerateKeySetting1(ChangeApplicationKey.SAMEKEY, ChangeMasterKeySettings.WITHMASTERKEY, CreateDeleteFile.NOKEY, FileDirectoryAccess.NOKEY, ChangeMasterKey.CHANGEABLE);
byte keysetting2 = desfire.GenerateKeySetting2(MifareDESFire.CryptoOperations.AES, MifareDESFire.FileIdentifies.NOTUSED, 0x01); byte keysetting2 = desfire.GenerateKeySetting2(CryptoOperationsType.AES, FileIdentifies.NOTUSED, 0x01);
APDUCommand cmd = desfire.CreateApplication(applicationID, keysetting1, keysetting2); APDUCommand cmd = desfire.CreateApplication(applicationID, keysetting1, keysetting2);

View File

@ -0,0 +1,191 @@
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;
}
}
}