mirror of
https://gitlab.com/fabinfra/fabaccess/borepin.git
synced 2025-04-20 18:36:31 +02:00
BackUp
This commit is contained in:
parent
78b832187a
commit
04a41322e6
@ -70,18 +70,34 @@ namespace NFC.Mifare_DESFire
|
|||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public APDUCommand Authenticate_GetChallenge(byte keyid)
|
||||||
|
{
|
||||||
|
APDUCommand cmd = new APDUCommand(IsoCase.Case4Short)
|
||||||
|
{
|
||||||
|
CLA = 0x90,
|
||||||
|
INS = (byte)APDUInstructions.,
|
||||||
|
Data = new byte[]
|
||||||
|
{
|
||||||
|
id_byte[0],
|
||||||
|
id_byte[1],
|
||||||
|
id_byte[2]
|
||||||
|
},
|
||||||
|
Le = 0x00
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Select Application by ID
|
/// Delete Application by ID
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id">3 Byte ID</param>
|
/// <param name="id">3 Byte ID</param>
|
||||||
public APDUCommand CreateApplication(UInt32 id)
|
public APDUCommand DeleteApplication(UInt32 id)
|
||||||
{
|
{
|
||||||
byte[] id_byte = BitConverter.GetBytes(id);
|
byte[] id_byte = BitConverter.GetBytes(id);
|
||||||
|
|
||||||
APDUCommand cmd = new APDUCommand(IsoCase.Case4Short)
|
APDUCommand cmd = new APDUCommand(IsoCase.Case4Short)
|
||||||
{
|
{
|
||||||
CLA = 0x90,
|
CLA = 0x90,
|
||||||
INS = (byte)APDUInstructions.CREATE_APPLICATION,
|
INS = (byte)APDUInstructions.DELETE_APPLICATION,
|
||||||
Data = new byte[]
|
Data = new byte[]
|
||||||
{
|
{
|
||||||
id_byte[0],
|
id_byte[0],
|
||||||
@ -94,18 +110,170 @@ namespace NFC.Mifare_DESFire
|
|||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte GenerateKeySetting1()
|
/// <summary>
|
||||||
|
/// Select Application by ID
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">3 Byte ID</param>
|
||||||
|
public APDUCommand CreateApplication(UInt32 id, byte keysetting1, byte keysetting2)
|
||||||
{
|
{
|
||||||
return 0x00;
|
byte[] id_byte = BitConverter.GetBytes(id);
|
||||||
|
|
||||||
|
APDUCommand cmd = new APDUCommand(IsoCase.Case4Short)
|
||||||
|
{
|
||||||
|
CLA = 0x90,
|
||||||
|
INS = (byte)APDUInstructions.CREATE_APPLICATION,
|
||||||
|
Data = new byte[]
|
||||||
|
{
|
||||||
|
id_byte[0],
|
||||||
|
id_byte[1],
|
||||||
|
id_byte[2],
|
||||||
|
keysetting1,
|
||||||
|
keysetting2
|
||||||
|
},
|
||||||
|
Le = 0x00
|
||||||
|
};
|
||||||
|
|
||||||
|
return cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
///
|
/// Genearte KeySetting1 for Application Settings or PICC Setting
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public enum ChangeKey : byte
|
public byte GenerateKeySetting1(ChangeApplicationKey changeKey, ChangeMasterKeySettings changeMasterKeySettings, CreateDeleteFile createDeleteFile, FileDirectoryAccess fileDirectoryAccess, ChangeMasterKey changeMasterKey)
|
||||||
{
|
{
|
||||||
MASTERKEY = 0x00,
|
return (byte)(((byte)changeKey << 4) | (byte)changeMasterKeySettings | (byte)createDeleteFile | (byte)fileDirectoryAccess | (byte)changeMasterKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Genearte KeySetting1 for Application Settings or PICC Setting
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="changeKey">ID of Key for changing Application Keys</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public byte GenerateKeySetting1(byte changeKey, ChangeMasterKeySettings changeMasterKeySettings, CreateDeleteFile createDeleteFile, FileDirectoryAccess fileDirectoryAccess, ChangeMasterKey changeMasterKey)
|
||||||
|
{
|
||||||
|
if(changeKey < 0x01 || changeKey >= 0x0E)
|
||||||
|
{
|
||||||
|
throw new ArgumentOutOfRangeException();
|
||||||
|
}
|
||||||
|
return GenerateKeySetting1(changeKey, changeMasterKeySettings, createDeleteFile, fileDirectoryAccess, changeMasterKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Genearte KeySetting2 for Application Creation
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="numberOfKeys">Number of keys that can be stored within the application (0x01-0x0D)</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public byte GenerateKeySetting2(CryptoOperations cryptoOperations, FileIdentifies fileIdentifies, byte numberOfKeys)
|
||||||
|
{
|
||||||
|
if(numberOfKeys < 0x01 || numberOfKeys >= 0x0D)
|
||||||
|
{
|
||||||
|
throw new ArgumentOutOfRangeException();
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
33
NFC_Test/MifareDESFire_Commands.cs
Normal file
33
NFC_Test/MifareDESFire_Commands.cs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
using NFC;
|
||||||
|
using NFC.Mifare_DESFire;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace NFC_Test
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class MifareDESFire_Commands
|
||||||
|
{
|
||||||
|
private MifareDESFire _MifareDESFire;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_MifareDESFire = new MifareDESFire();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetApplicationIDs()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GenerateKeySetting1()
|
||||||
|
{
|
||||||
|
Assert.AreEqual(0xEF, _MifareDESFire.GenerateKeySetting1(MifareDESFire.ChangeApplicationKey.SAMEKEY, MifareDESFire.ChangeMasterKeySettings.WITHMASTERKEY, MifareDESFire.CreateDeleteFile.NOKEY, MifareDESFire.FileDirectoryAccess.NOKEY, MifareDESFire.ChangeMasterKey.CHANGEABLE));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -134,5 +134,82 @@ namespace NFC_Test
|
|||||||
reader.Stop();
|
reader.Stop();
|
||||||
reader.CardDiscovered -= handler;
|
reader.CardDiscovered -= handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestCase("ACS ACR122U PICC Interface 0", (UInt32)0xC0FFEE)]
|
||||||
|
public void DeleteApplication(string readerID, UInt32 applicationID)
|
||||||
|
{
|
||||||
|
IHardware hardware = new Hardware();
|
||||||
|
IReader reader = hardware.OpenReader(readerID);
|
||||||
|
|
||||||
|
bool transmit_successfully = false;
|
||||||
|
|
||||||
|
ReaderEventHandler handler = (sender, card) =>
|
||||||
|
{
|
||||||
|
card.Connect();
|
||||||
|
|
||||||
|
MifareDESFire desfire = new MifareDESFire();
|
||||||
|
|
||||||
|
APDUCommand cmd = desfire.DeleteApplication(applicationID);
|
||||||
|
|
||||||
|
cmd.ToArray();
|
||||||
|
|
||||||
|
APDUResponse response = card.Transmit(cmd);
|
||||||
|
|
||||||
|
if (response.StatusWord == NFC.Mifare_DESFire.APDUStatusWords.OK)
|
||||||
|
{
|
||||||
|
transmit_successfully = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
card.Disconnect();
|
||||||
|
};
|
||||||
|
|
||||||
|
reader.CardDiscovered += handler;
|
||||||
|
reader.Start();
|
||||||
|
|
||||||
|
Assert.AreEqual(true, transmit_successfully);
|
||||||
|
|
||||||
|
reader.Stop();
|
||||||
|
reader.CardDiscovered -= handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCase("ACS ACR122U PICC Interface 0", (UInt32)0xC0FFEE)]
|
||||||
|
public void CreateApplication(string readerID, UInt32 applicationID)
|
||||||
|
{
|
||||||
|
IHardware hardware = new Hardware();
|
||||||
|
IReader reader = hardware.OpenReader(readerID);
|
||||||
|
|
||||||
|
bool transmit_successfully = false;
|
||||||
|
|
||||||
|
ReaderEventHandler handler = (sender, card) =>
|
||||||
|
{
|
||||||
|
card.Connect();
|
||||||
|
|
||||||
|
MifareDESFire desfire = new MifareDESFire();
|
||||||
|
|
||||||
|
byte keysetting1 = desfire.GenerateKeySetting1(MifareDESFire.ChangeApplicationKey.SAMEKEY, MifareDESFire.ChangeMasterKeySettings.WITHMASTERKEY, MifareDESFire.CreateDeleteFile.NOKEY, MifareDESFire.FileDirectoryAccess.NOKEY, MifareDESFire.ChangeMasterKey.CHANGEABLE);
|
||||||
|
byte keysetting2 = desfire.GenerateKeySetting2(MifareDESFire.CryptoOperations.AES, MifareDESFire.FileIdentifies.NOTUSED, 0x01);
|
||||||
|
|
||||||
|
APDUCommand cmd = desfire.CreateApplication(applicationID, keysetting1, keysetting2);
|
||||||
|
|
||||||
|
Console.WriteLine(cmd.ToArray());
|
||||||
|
|
||||||
|
APDUResponse response = card.Transmit(cmd);
|
||||||
|
|
||||||
|
if (response.StatusWord == NFC.Mifare_DESFire.APDUStatusWords.OK)
|
||||||
|
{
|
||||||
|
transmit_successfully = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
card.Disconnect();
|
||||||
|
};
|
||||||
|
|
||||||
|
reader.CardDiscovered += handler;
|
||||||
|
reader.Start();
|
||||||
|
|
||||||
|
Assert.AreEqual(true, transmit_successfully);
|
||||||
|
|
||||||
|
reader.Stop();
|
||||||
|
reader.CardDiscovered -= handler;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user