Added: GetApplication

This commit is contained in:
TheJoKlLa
2020-09-17 16:07:58 +02:00
parent 187dfc4f4a
commit 2e68052008
7 changed files with 230 additions and 72 deletions

View File

@ -1,61 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;
using PCSC;
using PCSC.Iso7816;
namespace NFC
{
public class APDUCommand
public class APDUCommand : CommandApdu
{
/// <summary>
/// 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
public APDUCommand(IsoCase isoCase) : base(isoCase, SCardProtocol.Any)
{
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();
}
}
}

View File

@ -204,6 +204,11 @@ namespace NFC.Mifare_DESFire
/// <summary>
/// Kommando erfolgreich ausgef<65>hrt
/// </summary>
SUCCESS = 0x9000,
SUCCESS = 0x9000,
/// <summary>
/// OK
/// </summary>
OK = 0x9100,
}
}

View File

@ -41,9 +41,9 @@ namespace NFC
/// </summary>
event ReaderEventHandler CardLost;
void start();
void Start();
void stop();
void Stop();
}
public interface ICard

View 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,
}
}
}

View File

@ -31,18 +31,7 @@ namespace NFC.Readers.PCSC
public CommandApdu Convert(APDUCommand apdu_cmd)
{
CommandApdu apdu = new CommandApdu(IsoCase.Case2Short, _ISOReader.ActiveProtocol)
{
CLA = apdu_cmd.Class,
INS = apdu_cmd.Instruction,
P1 = apdu_cmd.Parameter1,
P2 = apdu_cmd.Parameter2,
Data = apdu_cmd.Data,
Le = apdu_cmd.LengthExpected
};
CommandApdu apdu = (CommandApdu)apdu_cmd;
return apdu;
}

View File

@ -25,10 +25,10 @@ namespace NFC.Readers.PCSC
public void Dispose()
{
stop();
Stop();
}
public void start()
public void Start()
{
_ContextFactory = ContextFactory.Instance;
_SCardContext = _ContextFactory.Establish(SCardScope.System);
@ -40,7 +40,7 @@ namespace NFC.Readers.PCSC
CardDiscovered?.Invoke(this, _Card);
}
public void stop()
public void Stop()
{
CardLost?.Invoke(this, _Card);