mirror of
https://gitlab.com/fabinfra/fabaccess/borepin.git
synced 2025-03-12 23:01:52 +01:00
Added: GetApplication
This commit is contained in:
parent
187dfc4f4a
commit
2e68052008
@ -1,61 +1,13 @@
|
|||||||
using System;
|
using PCSC;
|
||||||
using System.Collections.Generic;
|
using PCSC.Iso7816;
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace NFC
|
namespace NFC
|
||||||
{
|
{
|
||||||
public class APDUCommand
|
public class APDUCommand : CommandApdu
|
||||||
{
|
{
|
||||||
/// <summary>
|
public APDUCommand(IsoCase isoCase) : base(isoCase, SCardProtocol.Any)
|
||||||
/// ISO 7816 - CLA - Class
|
|
||||||
/// </summary>
|
|
||||||
public byte Class { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// ISO 7816 - INS - Instruction
|
|
||||||
/// </summary>
|
|
||||||
public byte Instruction { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// ISO 7816 - P1 - Parameter 1
|
|
||||||
/// </summary>
|
|
||||||
public byte Parameter1 { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// ISO 7816 - P2 - Parameter 2
|
|
||||||
/// </summary>
|
|
||||||
public byte Parameter2 { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// ISO 7816 - Lc - Length Command
|
|
||||||
/// </summary>
|
|
||||||
public byte LengthCommand
|
|
||||||
{
|
{
|
||||||
get
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// ISO 7816 - Data - Data
|
|
||||||
/// </summary>
|
|
||||||
public byte[] Data { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// ISO 7816 - Le - Length expected
|
|
||||||
/// </summary>
|
|
||||||
public byte LengthExpected
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] APDUCommandMessage()
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -205,5 +205,10 @@ namespace NFC.Mifare_DESFire
|
|||||||
/// Kommando erfolgreich ausgeführt
|
/// Kommando erfolgreich ausgeführt
|
||||||
/// </summary>
|
/// </summary>
|
||||||
SUCCESS = 0x9000,
|
SUCCESS = 0x9000,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// OK
|
||||||
|
/// </summary>
|
||||||
|
OK = 0x9100,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,9 +41,9 @@ namespace NFC
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
event ReaderEventHandler CardLost;
|
event ReaderEventHandler CardLost;
|
||||||
|
|
||||||
void start();
|
void Start();
|
||||||
|
|
||||||
void stop();
|
void Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface ICard
|
public interface ICard
|
||||||
|
111
NFC/Mifare DESFire/MifareDESFire.cs
Normal file
111
NFC/Mifare DESFire/MifareDESFire.cs
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
using PCSC.Iso7816;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace NFC.Mifare_DESFire
|
||||||
|
{
|
||||||
|
public class MifareDESFire
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Create new Application with AID
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="aid">Appilication ID</param>
|
||||||
|
public APDUCommand CreateApplication(UInt64 aid)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public APDUCommand GetApplicationIDs()
|
||||||
|
{
|
||||||
|
APDUCommand cmd = new APDUCommand(IsoCase.Case2Short)
|
||||||
|
{
|
||||||
|
CLA = 0x90,
|
||||||
|
INS = (byte)APDUInstructions.GET_APPLICATION_IDS
|
||||||
|
};
|
||||||
|
|
||||||
|
return cmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UInt32[] ConvertApplicationIDs(APDUResponse response)
|
||||||
|
{
|
||||||
|
if(response.Body.Length % 3 != 0)
|
||||||
|
{
|
||||||
|
throw new Exception("Invalid Body Length.");
|
||||||
|
}
|
||||||
|
|
||||||
|
List<UInt32> applicationIDs = new List<UInt32>();
|
||||||
|
|
||||||
|
for(int i = 0; i < response.Body.Length; i += 3)
|
||||||
|
{
|
||||||
|
UInt32 new_applicationID = 0;
|
||||||
|
new_applicationID = (UInt32)((response.Body[i] << 16) + (response.Body[i + 1] << 8) + response.Body[i + 2]);
|
||||||
|
applicationIDs.Add(new_applicationID);
|
||||||
|
}
|
||||||
|
|
||||||
|
return applicationIDs.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Select Application by ID
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">3 Byte ID</param>
|
||||||
|
public APDUCommand SelectApplication(UInt32 id)
|
||||||
|
{
|
||||||
|
byte[] id_byte = BitConverter.GetBytes(id);
|
||||||
|
|
||||||
|
APDUCommand cmd = new APDUCommand(IsoCase.Case4Short)
|
||||||
|
{
|
||||||
|
CLA = 0x90,
|
||||||
|
INS = (byte)APDUInstructions.SELECT_APPLICATION,
|
||||||
|
Data = new byte[]
|
||||||
|
{
|
||||||
|
id_byte[0],
|
||||||
|
id_byte[1],
|
||||||
|
id_byte[2]
|
||||||
|
},
|
||||||
|
Le = 0x00
|
||||||
|
};
|
||||||
|
|
||||||
|
return cmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Select Application by ID
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">3 Byte ID</param>
|
||||||
|
public APDUCommand CreateApplication(UInt32 id)
|
||||||
|
{
|
||||||
|
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]
|
||||||
|
},
|
||||||
|
Le = 0x00
|
||||||
|
};
|
||||||
|
|
||||||
|
return cmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte GenerateKeySetting1()
|
||||||
|
{
|
||||||
|
return 0x00;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public enum ChangeKey : byte
|
||||||
|
{
|
||||||
|
MASTERKEY = 0x00,
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -31,18 +31,7 @@ namespace NFC.Readers.PCSC
|
|||||||
|
|
||||||
public CommandApdu Convert(APDUCommand apdu_cmd)
|
public CommandApdu Convert(APDUCommand apdu_cmd)
|
||||||
{
|
{
|
||||||
CommandApdu apdu = new CommandApdu(IsoCase.Case2Short, _ISOReader.ActiveProtocol)
|
CommandApdu apdu = (CommandApdu)apdu_cmd;
|
||||||
{
|
|
||||||
CLA = apdu_cmd.Class,
|
|
||||||
INS = apdu_cmd.Instruction,
|
|
||||||
P1 = apdu_cmd.Parameter1,
|
|
||||||
P2 = apdu_cmd.Parameter2,
|
|
||||||
|
|
||||||
Data = apdu_cmd.Data,
|
|
||||||
|
|
||||||
Le = apdu_cmd.LengthExpected
|
|
||||||
};
|
|
||||||
|
|
||||||
return apdu;
|
return apdu;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,10 +25,10 @@ namespace NFC.Readers.PCSC
|
|||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
stop();
|
Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start()
|
public void Start()
|
||||||
{
|
{
|
||||||
_ContextFactory = ContextFactory.Instance;
|
_ContextFactory = ContextFactory.Instance;
|
||||||
_SCardContext = _ContextFactory.Establish(SCardScope.System);
|
_SCardContext = _ContextFactory.Establish(SCardScope.System);
|
||||||
@ -40,7 +40,7 @@ namespace NFC.Readers.PCSC
|
|||||||
CardDiscovered?.Invoke(this, _Card);
|
CardDiscovered?.Invoke(this, _Card);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop()
|
public void Stop()
|
||||||
{
|
{
|
||||||
CardLost?.Invoke(this, _Card);
|
CardLost?.Invoke(this, _Card);
|
||||||
|
|
||||||
|
@ -4,6 +4,8 @@ using System.Collections.Generic;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using NFC;
|
using NFC;
|
||||||
using NFC.Readers.PCSC;
|
using NFC.Readers.PCSC;
|
||||||
|
using System.Threading;
|
||||||
|
using NFC.Mifare_DESFire;
|
||||||
|
|
||||||
namespace NFC_Test
|
namespace NFC_Test
|
||||||
{
|
{
|
||||||
@ -13,7 +15,7 @@ namespace NFC_Test
|
|||||||
[Test]
|
[Test]
|
||||||
public void GetReaders()
|
public void GetReaders()
|
||||||
{
|
{
|
||||||
Hardware hardware = new Hardware();
|
IHardware hardware = new Hardware();
|
||||||
string[] readers = hardware.GetReaders();
|
string[] readers = hardware.GetReaders();
|
||||||
|
|
||||||
Console.WriteLine("Readers detected: {0}", readers.Length);
|
Console.WriteLine("Readers detected: {0}", readers.Length);
|
||||||
@ -28,10 +30,109 @@ namespace NFC_Test
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCase("")]
|
[TestCase("ACS ACR122U PICC Interface 0")]
|
||||||
public void Connect(string readerID)
|
public void Connect(string readerID)
|
||||||
{
|
{
|
||||||
|
IHardware hardware = new Hardware();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCase("ACS ACR122U PICC Interface 0")]
|
||||||
|
public void GetApplicationIDs(string readerID)
|
||||||
|
{
|
||||||
|
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.GetApplicationIDs();
|
||||||
|
|
||||||
|
APDUResponse response = card.Transmit(cmd);
|
||||||
|
|
||||||
|
if (response.StatusWord == NFC.Mifare_DESFire.APDUStatusWords.OK)
|
||||||
|
{
|
||||||
|
UInt32[] ApplicationIDs = desfire.ConvertApplicationIDs(response);
|
||||||
|
|
||||||
|
foreach(UInt32 id in ApplicationIDs)
|
||||||
|
{
|
||||||
|
Console.WriteLine("0x{0:X3}", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 SelectApplication(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.SelectApplication(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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user