Changed to NFCService

This commit is contained in:
TheJoKlLa 2023-01-30 23:27:00 +01:00
parent 327472bea0
commit e79b6aace2
28 changed files with 790 additions and 849 deletions

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

316
NFC.PCSC/NFCService.cs Normal file
View File

@ -0,0 +1,316 @@
using NFC.Interfaces;
using PCSClib = PCSC;
using PCSCIso7816 = PCSC.Iso7816;
using System;
using System.Collections.Generic;
using PCSC.Exceptions;
using NFC.Exceptions;
using PCSC.Iso7816;
namespace NFC.PCSC
{
public class NFCService : INFCService
{
#region Private Members
private PCSCIso7816.IsoReader _IsoReader;
private PCSClib.IContextFactory _ContextFactory;
private PCSClib.ISCardContext _SCardContext;
#endregion
#region Constructors
public NFCService()
{
_ContextFactory = PCSClib.ContextFactory.Instance;
_SCardContext = _ContextFactory.Establish(PCSClib.SCardScope.System);
}
#endregion
#region Members
/// <summary>
/// Indicates if NFC Hardware is available
/// </summary>
public bool IsAvailable
{
get
{
return true;
}
}
/// <summary>
/// Indicates if NFC Hardware is enabled
/// </summary>
public bool IsEnabled
{
get
{
return true;
}
}
/// <summary>
/// Indicates if NFC Card is connected
/// </summary>
public bool IsConnected
{
get
{
return _IsoReader != null;
}
}
#endregion
#region Methods
/// <summary>
/// Get a list of availible NFC ReaderIDs
/// </summary>
public IList<string> GetReaderIDs()
{
return new List<string>(_SCardContext.GetReaders());
}
/// <summary>
/// Connect with ReaderID to NFC Card
/// </summary>
/// <param name="readerID">ReaderID from GetReaderIDs</param>
/// <exception cref="ReaderUnavailableException"></exception>
/// <exception cref="CardException"></exception>
/// <exception cref="ConnectionException"></exception>
/// <exception cref="ArgumentNullException"></exception>
public void Connect(string readerID)
{
if(string.IsNullOrEmpty(readerID))
{
throw new ArgumentNullException();
}
try
{
_IsoReader = new PCSCIso7816.IsoReader(_SCardContext);
_IsoReader.Connect(readerID, PCSClib.SCardShareMode.Shared, PCSClib.SCardProtocol.Any);
}
catch (Exception exception)
{
switch(exception)
{
case InvalidContextException _:
case InvalidProtocolException _:
case InvalidValueException _:
case UnsupportedFeatureException _:
case CommunicationErrorException _:
case InternalErrorException _:
case WinErrorInsufficientBufferException _:
case InsufficientBufferException _:
throw new ConnectionException("Connect failed", exception);
case NoServiceException _:
case NoReadersAvailableException _:
case NotReadyException _:
case PCSClib.Exceptions.ReaderUnavailableException _:
case SharingViolationException _:
case UnknownReaderException _:
throw new NFC.Exceptions.ReaderException("Reader connecting failed", exception);
case NoSmartcardException _:
case UnpoweredCardException _:
case UnresponsiveCardException _:
case RemovedCardException _:
throw new CardException("Reader connecting failed", exception);
case PCSCException _:
default:
throw new ConnectionException("Connect failed", exception);
}
}
}
/// <summary>
/// Disconnects Reader from NFC Card
/// </summary>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="ConnectionException"></exception>
public void Disconnect()
{
if(_IsoReader == null)
{
throw new InvalidOperationException();
}
try
{
_IsoReader.Disconnect(PCSClib.SCardReaderDisposition.Eject);
}
catch(Exception exception)
{
throw new ConnectionException("Disconnect failed", exception);
}
finally
{
_IsoReader.Dispose();
_IsoReader = null;
}
}
/// <summary>
/// Transmit APDUCommand to Card if connected
/// </summary>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="TransmitException"></exception>
/// <exception cref="CardException"></exception>
/// <exception cref="ReaderException"></exception>
/// <exception cref="ArgumentNullException"></exception>
public APDUResponse Transmit(APDUCommand command)
{
if(_IsoReader == null)
{
throw new InvalidOperationException();
}
if(command == null)
{
throw new ArgumentNullException();
}
try
{
PCSCIso7816.Response response = _IsoReader.Transmit(ConvertAPDUCommand(command));
return Convert(response);
}
catch(Exception exception)
{
switch (exception)
{
case InvalidContextException _:
case InvalidProtocolException _:
case InvalidValueException _:
case UnsupportedFeatureException _:
case InternalErrorException _:
case WinErrorInsufficientBufferException _:
throw new ConnectionException("Connect failed", exception);
case InvalidApduException _:
case InsufficientBufferException _:
case CommunicationErrorException _:
throw new ConnectionException("Transmit failed", exception);
case NoServiceException _:
case NoReadersAvailableException _:
case NotReadyException _:
case ReaderUnavailableException _:
case SharingViolationException _:
case UnknownReaderException _:
throw new ReaderException("Reader connecting failed", exception);
case NoSmartcardException _:
case UnpoweredCardException _:
case UnresponsiveCardException _:
case RemovedCardException _:
throw new CardException("Reader connecting failed", exception);
case PCSCException _:
default:
throw new ConnectionException("Connect failed", exception);
}
}
}
#endregion
#region Convert Methods
public PCSCIso7816.CommandApdu ConvertAPDUCommand(APDUCommand apdu_cmd)
{
switch (apdu_cmd.Case)
{
case NFC.IsoCase.Case1:
return new PCSCIso7816.CommandApdu(ConvertISOCase(apdu_cmd.Case), ConvertSCardProtocol(apdu_cmd.Protocol))
{
CLA = apdu_cmd.CLA,
INS = apdu_cmd.INS,
P1 = apdu_cmd.P1,
P2 = apdu_cmd.P2
};
case NFC.IsoCase.Case2Short:
return new PCSCIso7816.CommandApdu(ConvertISOCase(apdu_cmd.Case), ConvertSCardProtocol(apdu_cmd.Protocol))
{
CLA = apdu_cmd.CLA,
INS = apdu_cmd.INS,
P1 = apdu_cmd.P1,
P2 = apdu_cmd.P2,
Le = apdu_cmd.LE
};
case NFC.IsoCase.Case3Short:
return new PCSCIso7816.CommandApdu(ConvertISOCase(apdu_cmd.Case), ConvertSCardProtocol(apdu_cmd.Protocol))
{
CLA = apdu_cmd.CLA,
INS = apdu_cmd.INS,
P1 = apdu_cmd.P1,
P2 = apdu_cmd.P2,
Data = apdu_cmd.Data
};
case NFC.IsoCase.Case4Short:
return new PCSCIso7816.CommandApdu(ConvertISOCase(apdu_cmd.Case), ConvertSCardProtocol(apdu_cmd.Protocol))
{
CLA = apdu_cmd.CLA,
INS = apdu_cmd.INS,
P1 = apdu_cmd.P1,
P2 = apdu_cmd.P2,
Data = apdu_cmd.Data,
Le = apdu_cmd.LE
};
default:
throw new Exception("Unknown IsoCase");
}
}
public PCSCIso7816.IsoCase ConvertISOCase(NFC.IsoCase isoCase)
{
switch (isoCase)
{
case NFC.IsoCase.Case1:
return PCSCIso7816.IsoCase.Case1;
case NFC.IsoCase.Case2Short:
return PCSCIso7816.IsoCase.Case2Short;
case NFC.IsoCase.Case3Short:
return PCSCIso7816.IsoCase.Case3Short;
case NFC.IsoCase.Case4Short:
return PCSCIso7816.IsoCase.Case4Short;
default:
throw new Exception("Unknown IsoCase");
}
}
public PCSClib.SCardProtocol ConvertSCardProtocol(NFC.SCardProtocol sCardProtocol)
{
switch (sCardProtocol)
{
case NFC.SCardProtocol.UNSET:
return PCSClib.SCardProtocol.Unset;
case NFC.SCardProtocol.T0:
return PCSClib.SCardProtocol.T0;
case NFC.SCardProtocol.T1:
return PCSClib.SCardProtocol.T1;
case NFC.SCardProtocol.RAW:
return PCSClib.SCardProtocol.Raw;
case NFC.SCardProtocol.T15:
return PCSClib.SCardProtocol.T15;
case NFC.SCardProtocol.ANY:
return PCSClib.SCardProtocol.Any;
default:
throw new NotSupportedException("Unknown SCardProtocol");
}
}
public APDUResponse Convert(PCSCIso7816.Response response)
{
PCSCIso7816.ResponseApdu responseApdu = response.Get(0);
APDUResponse apduResponse = new APDUResponse()
{
SW1 = responseApdu.SW1,
SW2 = responseApdu.SW2,
Body = responseApdu.GetData()
};
return apduResponse;
}
#endregion
}
}

22
NFC.sln
View File

@ -1,17 +1,17 @@
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16 # Visual Studio Version 17
VisualStudioVersion = 16.0.30717.126 VisualStudioVersion = 17.4.33122.133
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NFC", "NFC\NFC.csproj", "{1D12BCDF-033F-40DE-ABA9-8BA5ABE0CA3A}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NFC", "NFC\NFC.csproj", "{1D12BCDF-033F-40DE-ABA9-8BA5ABE0CA3A}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NFC_PCSC", "NFC_PCSC\NFC_PCSC.csproj", "{62DE4EBC-6F35-4D31-8717-DBC62D46035C}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NFC.Android", "NFC.Android\NFC.Android.csproj", "{B2609012-9D21-42F0-A2F9-3FE97D356392}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NFC_Android", "NFC_Android\NFC_Android.csproj", "{B2609012-9D21-42F0-A2F9-3FE97D356392}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NFC.iOS", "NFC.iOS\NFC.iOS.csproj", "{C56A1E1A-976C-42ED-B7A2-08C6111AA0E8}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NFC_iOS", "NFC_iOS\NFC_iOS.csproj", "{C56A1E1A-976C-42ED-B7A2-08C6111AA0E8}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NFC_Test", "NFC_Test\NFC_Test.csproj", "{FE8A1426-8B19-4CDF-A75E-80397E55BA95}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NFC_Test", "NFC_Test\NFC_Test.csproj", "{FE8A1426-8B19-4CDF-A75E-80397E55BA95}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NFC.PCSC", "NFC.PCSC\NFC.PCSC.csproj", "{888BBBCA-D9F8-4D17-943F-81E0D83EE864}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -23,10 +23,6 @@ Global
{1D12BCDF-033F-40DE-ABA9-8BA5ABE0CA3A}.Debug|Any CPU.Build.0 = Debug|Any CPU {1D12BCDF-033F-40DE-ABA9-8BA5ABE0CA3A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D12BCDF-033F-40DE-ABA9-8BA5ABE0CA3A}.Release|Any CPU.ActiveCfg = Release|Any CPU {1D12BCDF-033F-40DE-ABA9-8BA5ABE0CA3A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D12BCDF-033F-40DE-ABA9-8BA5ABE0CA3A}.Release|Any CPU.Build.0 = Release|Any CPU {1D12BCDF-033F-40DE-ABA9-8BA5ABE0CA3A}.Release|Any CPU.Build.0 = Release|Any CPU
{62DE4EBC-6F35-4D31-8717-DBC62D46035C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{62DE4EBC-6F35-4D31-8717-DBC62D46035C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{62DE4EBC-6F35-4D31-8717-DBC62D46035C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{62DE4EBC-6F35-4D31-8717-DBC62D46035C}.Release|Any CPU.Build.0 = Release|Any CPU
{B2609012-9D21-42F0-A2F9-3FE97D356392}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {B2609012-9D21-42F0-A2F9-3FE97D356392}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B2609012-9D21-42F0-A2F9-3FE97D356392}.Debug|Any CPU.Build.0 = Debug|Any CPU {B2609012-9D21-42F0-A2F9-3FE97D356392}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B2609012-9D21-42F0-A2F9-3FE97D356392}.Release|Any CPU.ActiveCfg = Release|Any CPU {B2609012-9D21-42F0-A2F9-3FE97D356392}.Release|Any CPU.ActiveCfg = Release|Any CPU
@ -39,6 +35,10 @@ Global
{FE8A1426-8B19-4CDF-A75E-80397E55BA95}.Debug|Any CPU.Build.0 = Debug|Any CPU {FE8A1426-8B19-4CDF-A75E-80397E55BA95}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FE8A1426-8B19-4CDF-A75E-80397E55BA95}.Release|Any CPU.ActiveCfg = Release|Any CPU {FE8A1426-8B19-4CDF-A75E-80397E55BA95}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FE8A1426-8B19-4CDF-A75E-80397E55BA95}.Release|Any CPU.Build.0 = Release|Any CPU {FE8A1426-8B19-4CDF-A75E-80397E55BA95}.Release|Any CPU.Build.0 = Release|Any CPU
{888BBBCA-D9F8-4D17-943F-81E0D83EE864}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{888BBBCA-D9F8-4D17-943F-81E0D83EE864}.Debug|Any CPU.Build.0 = Debug|Any CPU
{888BBBCA-D9F8-4D17-943F-81E0D83EE864}.Release|Any CPU.ActiveCfg = Release|Any CPU
{888BBBCA-D9F8-4D17-943F-81E0D83EE864}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -27,10 +27,10 @@ namespace NFC.Cards.NXP_MIFARE_DESFire
/// <summary> /// <summary>
/// Construct MIFRARE_DESFire Object with ICard Interface /// Construct MIFRARE_DESFire Object with ICard Interface
/// </summary> /// </summary>
/// <param name="card">Implementation of ICard, only transmit is used</param> /// <param name="nfcService">Implementation of ICard, only transmit is used</param>
public NXP_MIFARE_DESFire(ICard card) public NXP_MIFARE_DESFire(INFCService nfcService)
{ {
_Card = card; _NFCService = nfcService;
} }
#endregion #endregion
@ -38,7 +38,7 @@ namespace NFC.Cards.NXP_MIFARE_DESFire
/// <summary> /// <summary>
/// ICard Implementation used to transmit APDUCommands and recive APDUResponses /// ICard Implementation used to transmit APDUCommands and recive APDUResponses
/// </summary> /// </summary>
private readonly ICard _Card; private readonly INFCService _NFCService;
/// <summary> /// <summary>
/// SessionKey, is set after Successfull Authentication /// SessionKey, is set after Successfull Authentication
@ -251,7 +251,7 @@ namespace NFC.Cards.NXP_MIFARE_DESFire
}; };
_Log.Debug(cmd_SelectApplication.ToString()); _Log.Debug(cmd_SelectApplication.ToString());
HexConverter.ConvertToHexString(cmd_SelectApplication.ToArray()); HexConverter.ConvertToHexString(cmd_SelectApplication.ToArray());
APDUResponse response = _Card.Transmit(cmd_SelectApplication); APDUResponse response = _NFCService.Transmit(cmd_SelectApplication);
_Log.DebugFormat(response.ToString()); _Log.DebugFormat(response.ToString());
CheckAPDUResponse(response); CheckAPDUResponse(response);
@ -286,7 +286,7 @@ namespace NFC.Cards.NXP_MIFARE_DESFire
}; };
_Log.Debug(cmd_challange_request.ToString()); _Log.Debug(cmd_challange_request.ToString());
APDUResponse response = _Card.Transmit(cmd_challange_request); APDUResponse response = _NFCService.Transmit(cmd_challange_request);
_Log.Debug(response.ToString()); _Log.Debug(response.ToString());
CheckAPDUResponse(response); CheckAPDUResponse(response);
@ -326,7 +326,7 @@ namespace NFC.Cards.NXP_MIFARE_DESFire
}; };
_Log.Debug(cmd_challange_response.ToString()); _Log.Debug(cmd_challange_response.ToString());
response = _Card.Transmit(cmd_challange_response); response = _NFCService.Transmit(cmd_challange_response);
_Log.Debug(response.ToString()); _Log.Debug(response.ToString());
CheckAPDUResponse(response); CheckAPDUResponse(response);
@ -369,7 +369,7 @@ namespace NFC.Cards.NXP_MIFARE_DESFire
}; };
_Log.Debug(cmd_format.ToString()); _Log.Debug(cmd_format.ToString());
APDUResponse response = _Card.Transmit(cmd_format); APDUResponse response = _NFCService.Transmit(cmd_format);
_Log.Debug(response.ToString()); _Log.Debug(response.ToString());
CheckAPDUResponse(response); CheckAPDUResponse(response);
@ -404,7 +404,7 @@ namespace NFC.Cards.NXP_MIFARE_DESFire
}; };
_Log.Debug(cmd_CreateApplication.ToString()); _Log.Debug(cmd_CreateApplication.ToString());
APDUResponse response = _Card.Transmit(cmd_CreateApplication); APDUResponse response = _NFCService.Transmit(cmd_CreateApplication);
_Log.Debug(response.ToString()); _Log.Debug(response.ToString());
CheckAPDUResponse(response); CheckAPDUResponse(response);
@ -439,7 +439,7 @@ namespace NFC.Cards.NXP_MIFARE_DESFire
}; };
_Log.Debug(cmd_challange_request.ToString()); _Log.Debug(cmd_challange_request.ToString());
APDUResponse response = _Card.Transmit(cmd_challange_request); APDUResponse response = _NFCService.Transmit(cmd_challange_request);
_Log.Debug(response.ToString()); _Log.Debug(response.ToString());
CheckAPDUResponse(response); CheckAPDUResponse(response);
@ -479,7 +479,7 @@ namespace NFC.Cards.NXP_MIFARE_DESFire
}; };
_Log.Debug(cmd_challange_response.ToString()); _Log.Debug(cmd_challange_response.ToString());
response = _Card.Transmit(cmd_challange_response); response = _NFCService.Transmit(cmd_challange_response);
_Log.Debug(response.ToString()); _Log.Debug(response.ToString());
CheckAPDUResponse(response); CheckAPDUResponse(response);
@ -559,7 +559,7 @@ namespace NFC.Cards.NXP_MIFARE_DESFire
}; };
_Log.Debug(cmd_ChangeKey.ToString()); _Log.Debug(cmd_ChangeKey.ToString());
APDUResponse response = _Card.Transmit(cmd_ChangeKey); APDUResponse response = _NFCService.Transmit(cmd_ChangeKey);
_Log.Debug(response.ToString()); _Log.Debug(response.ToString());
CheckAPDUResponse(response); CheckAPDUResponse(response);
@ -624,7 +624,7 @@ namespace NFC.Cards.NXP_MIFARE_DESFire
}; };
_Log.Debug(cmd_ChangeKey.ToString()); _Log.Debug(cmd_ChangeKey.ToString());
APDUResponse response = _Card.Transmit(cmd_ChangeKey); APDUResponse response = _NFCService.Transmit(cmd_ChangeKey);
_Log.Debug(response.ToString()); _Log.Debug(response.ToString());
CheckAPDUResponse(response); CheckAPDUResponse(response);
@ -669,7 +669,7 @@ namespace NFC.Cards.NXP_MIFARE_DESFire
}; };
_Log.Debug(cmd_CreateFile_Standard.ToString()); _Log.Debug(cmd_CreateFile_Standard.ToString());
APDUResponse response = _Card.Transmit(cmd_CreateFile_Standard); APDUResponse response = _NFCService.Transmit(cmd_CreateFile_Standard);
_Log.DebugFormat(response.ToString()); _Log.DebugFormat(response.ToString());
CheckAPDUResponse(response); CheckAPDUResponse(response);
@ -741,7 +741,7 @@ namespace NFC.Cards.NXP_MIFARE_DESFire
Data = ByteOperation.Concatenate(file_id_array, offset_byte, length_byte, write_buffer) Data = ByteOperation.Concatenate(file_id_array, offset_byte, length_byte, write_buffer)
}; };
_Log.Debug(cmd_WriteData.ToString()); _Log.Debug(cmd_WriteData.ToString());
/*
@ -762,9 +762,9 @@ namespace NFC.Cards.NXP_MIFARE_DESFire
_Log.Debug(cmd_WriteData.ToString()); _Log.Debug(cmd_WriteData.ToString());
*/
APDUResponse response = _NFCService.Transmit(cmd_WriteData);
APDUResponse response = _Card.Transmit(cmd_WriteData);
_Log.Debug(response.ToString()); _Log.Debug(response.ToString());
CheckAPDUResponse(response); CheckAPDUResponse(response);
@ -835,7 +835,7 @@ namespace NFC.Cards.NXP_MIFARE_DESFire
}; };
_Log.Debug(cmd_ReadData.ToString()); _Log.Debug(cmd_ReadData.ToString());
APDUResponse response = _Card.Transmit(cmd_ReadData); APDUResponse response = _NFCService.Transmit(cmd_ReadData);
_Log.Debug(response.ToString()); _Log.Debug(response.ToString());
CheckAPDUResponse(response); CheckAPDUResponse(response);
@ -861,7 +861,7 @@ namespace NFC.Cards.NXP_MIFARE_DESFire
// INS = (byte)APDUInstructions.GET_APPLICATION_IDS // INS = (byte)APDUInstructions.GET_APPLICATION_IDS
// }; // };
// APDUResponse response = _Card.Transmit(cmd); // APDUResponse response = _NFCService.Transmit(cmd);
// CheckAPDUResponse(response); // CheckAPDUResponse(response);
@ -910,7 +910,7 @@ namespace NFC.Cards.NXP_MIFARE_DESFire
// Le = 0x00 // Le = 0x00
// }; // };
// APDUResponse response = _Card.Transmit(cmd); // APDUResponse response = _NFCService.Transmit(cmd);
// CheckAPDUResponse(response); // CheckAPDUResponse(response);
//} //}
#endregion #endregion

View File

@ -0,0 +1,22 @@
using System;
namespace NFC.Exceptions
{
public class CardException : Exception
{
public CardException()
{
}
public CardException(string message) : base(message)
{
}
public CardException(string message, Exception inner) : base(message, inner)
{
}
}
}

View File

@ -1,9 +0,0 @@
using System;
namespace NFC.Exceptions
{
public class CardUnavailableException : Exception
{
}
}

View File

@ -0,0 +1,22 @@
using System;
namespace NFC.Exceptions
{
public class ConnectionException : Exception
{
public ConnectionException()
{
}
public ConnectionException(string message) : base(message)
{
}
public ConnectionException(string message, Exception inner) : base(message, inner)
{
}
}
}

View File

@ -0,0 +1,22 @@
using System;
namespace NFC.Exceptions
{
public class ReaderException : Exception
{
public ReaderException()
{
}
public ReaderException(string message) : base(message)
{
}
public ReaderException(string message, Exception inner) : base(message, inner)
{
}
}
}

View File

@ -1,9 +0,0 @@
using System;
namespace NFC.Exceptions
{
public class ReaderUnavailableException : Exception
{
}
}

View File

@ -0,0 +1,22 @@
using System;
namespace NFC.Exceptions
{
public class TransmitException : Exception
{
public TransmitException()
{
}
public TransmitException(string message) : base(message)
{
}
public TransmitException(string message, Exception inner) : base(message, inner)
{
}
}
}

View File

@ -1,22 +0,0 @@
namespace NFC.Interfaces
{
public interface ICard
{
/// <summary>
/// Connect to Smartcard
/// </summary>
void Connect();
/// <summary>
/// Disconnect from Smartcard
/// </summary>
void Disconnect();
/// <summary>
/// Transmit APDU Command to Smartcard
/// </summary>
/// <param name="apdu_cmd">Application Protocol Data Unit Command - ISO 7816</param>
/// <returns>Application Protocol Data Unit Response - ISO 7816</returns>
APDUResponse Transmit(APDUCommand apdu_cmd);
}
}

View File

@ -1,26 +0,0 @@
using System;
namespace NFC.Interfaces
{
/// <summary>
/// Abstract representation of the platform specific NFC Hardware
/// </summary>
public interface IHardware
{
/// <summary>
/// Check if the device has nfc support
/// </summary>
/// <returns>Returns true if the device supports NFC</returns>
bool IsAvailable();
/// <returns>Returns all available readers</returns>
string[] GetReaders();
/// <summary>
/// Create a new reader instance from the specified id
/// </summary>
/// <returns>Returns the spatform specific reader that corresponds to the id</returns>
/// <exception cref="ArgumentException">Invalid reader id</exception>
IReader OpenReader(string readerID);
}
}

View File

@ -0,0 +1,59 @@
using System.Collections.Generic;
using System;
using NFC.Exceptions;
namespace NFC.Interfaces
{
/// <summary>
/// Service to provide hardware specific NFC Interfaces
/// </summary>
public interface INFCService
{
/// <summary>
/// Indicates if NFC Hardware is available
/// </summary>
bool IsAvailable { get; }
/// <summary>
/// Indicates if NFC Hardware is enabled
/// </summary>
bool IsEnabled { get; }
/// <summary>
/// Indicates if NFC Card is connected
/// </summary>
bool IsConnected { get; }
/// <summary>
/// Get a list of availible NFC ReaderIDs
/// </summary>
IList<string> GetReaderIDs();
/// <summary>
/// Connect with ReaderID to NFC Card
/// </summary>
/// <param name="readerID">ReaderID from GetReaderIDs</param>
/// <exception cref="ReaderException"></exception>
/// <exception cref="CardException"></exception>
/// <exception cref="ConnectionException"></exception>
/// <exception cref="ArgumentNullException"></exception>
void Connect(string readerID);
/// <summary>
/// Disconnects Reader from NFC Card
/// </summary>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="ConnectionException"></exception>
void Disconnect();
/// <summary>
/// Transmit APDUCommand to Card if connected
/// </summary>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="TransmitException"></exception>
/// <exception cref="CardException"></exception>
/// <exception cref="ReaderException"></exception>
/// <exception cref="ArgumentNullException"></exception>
APDUResponse Transmit(APDUCommand command);
}
}

View File

@ -1,24 +0,0 @@
namespace NFC.Interfaces
{
public delegate void ReaderEventHandler(object sender, ICard card);
/// <summary>
/// Abstraction of a platform-specifc reader that can communicate with NFC cards
/// </summary>
public interface IReader
{
/// <summary>
/// Event that will be called when a new tag was discovered
/// </summary>
event ReaderEventHandler CardDiscovered;
/// <summary>
/// Event that will be called when a tag that is in use gets disconnected
/// </summary>
event ReaderEventHandler CardLost;
void Start();
void Stop();
}
}

View File

@ -1,132 +0,0 @@
using PCSC;
using PCSC.Iso7816;
using NFC.Interfaces;
using NFC;
using System;
namespace NFC_PCSC
{
public class Card_PCSC : ICard
{
private readonly IsoReader _ISOReader;
private readonly string _ReaderID;
public Card_PCSC(IsoReader isoreader, string readerID)
{
_ISOReader = isoreader;
_ReaderID = readerID;
}
public void Connect()
{
_ISOReader.Connect(_ReaderID, SCardShareMode.Shared, PCSC.SCardProtocol.Any);
}
public void Disconnect()
{
_ISOReader.Disconnect(SCardReaderDisposition.Eject);
}
public APDUResponse Transmit(APDUCommand apdu_cmd)
{
Response response = _ISOReader.Transmit(ConvertAPDUCommand(apdu_cmd));
return Convert(response);
}
public CommandApdu ConvertAPDUCommand(APDUCommand apdu_cmd)
{
switch(apdu_cmd.Case)
{
case NFC.IsoCase.Case1:
return new CommandApdu(ConvertISOCase(apdu_cmd.Case), ConvertSCardProtocol(apdu_cmd.Protocol))
{
CLA = apdu_cmd.CLA,
INS = apdu_cmd.INS,
P1 = apdu_cmd.P1,
P2 = apdu_cmd.P2
};
case NFC.IsoCase.Case2Short:
return new CommandApdu(ConvertISOCase(apdu_cmd.Case), ConvertSCardProtocol(apdu_cmd.Protocol))
{
CLA = apdu_cmd.CLA,
INS = apdu_cmd.INS,
P1 = apdu_cmd.P1,
P2 = apdu_cmd.P2,
Le = apdu_cmd.LE
};
case NFC.IsoCase.Case3Short:
return new CommandApdu(ConvertISOCase(apdu_cmd.Case), ConvertSCardProtocol(apdu_cmd.Protocol))
{
CLA = apdu_cmd.CLA,
INS = apdu_cmd.INS,
P1 = apdu_cmd.P1,
P2 = apdu_cmd.P2,
Data = apdu_cmd.Data
};
case NFC.IsoCase.Case4Short:
return new CommandApdu(ConvertISOCase(apdu_cmd.Case), ConvertSCardProtocol(apdu_cmd.Protocol))
{
CLA = apdu_cmd.CLA,
INS = apdu_cmd.INS,
P1 = apdu_cmd.P1,
P2 = apdu_cmd.P2,
Data = apdu_cmd.Data,
Le = apdu_cmd.LE
};
default:
throw new Exception("Unknown IsoCase");
}
}
public PCSC.Iso7816.IsoCase ConvertISOCase(NFC.IsoCase isoCase)
{
switch(isoCase)
{
case NFC.IsoCase.Case1:
return PCSC.Iso7816.IsoCase.Case1;
case NFC.IsoCase.Case2Short:
return PCSC.Iso7816.IsoCase.Case2Short;
case NFC.IsoCase.Case3Short:
return PCSC.Iso7816.IsoCase.Case3Short;
case NFC.IsoCase.Case4Short:
return PCSC.Iso7816.IsoCase.Case4Short;
default:
throw new Exception("Unknown IsoCase");
}
}
public PCSC.SCardProtocol ConvertSCardProtocol(NFC.SCardProtocol sCardProtocol)
{
switch (sCardProtocol)
{
case NFC.SCardProtocol.UNSET:
return PCSC.SCardProtocol.Unset;
case NFC.SCardProtocol.T0:
return PCSC.SCardProtocol.T0;
case NFC.SCardProtocol.T1:
return PCSC.SCardProtocol.T1;
case NFC.SCardProtocol.RAW:
return PCSC.SCardProtocol.Raw;
case NFC.SCardProtocol.T15:
return PCSC.SCardProtocol.T15;
case NFC.SCardProtocol.ANY:
return PCSC.SCardProtocol.Any;
default:
throw new NotSupportedException("Unknown SCardProtocol");
}
}
public APDUResponse Convert(Response response)
{
ResponseApdu responseApdu = response.Get(0);
APDUResponse apduResponse = new APDUResponse()
{
SW1 = responseApdu.SW1,
SW2 = responseApdu.SW2,
Body = responseApdu.GetData()
};
return apduResponse;
}
}
}

View File

@ -1,32 +0,0 @@
using NFC.Interfaces;
using PCSC;
namespace NFC_PCSC
{
public class Hardware_PCSC : IHardware
{
public string[] GetReaders()
{
var contextFactory = ContextFactory.Instance;
using var context = contextFactory.Establish(SCardScope.System);
return context.GetReaders();
}
public bool IsAvailable()
{
if(GetReaders().Length == 0)
{
return false;
}
else
{
return true;
}
}
public IReader OpenReader(string readerID)
{
return new Reader_PCSC(readerID);
}
}
}

View File

@ -1,50 +0,0 @@
using NFC.Interfaces;
using PCSC;
using PCSC.Iso7816;
using System;
namespace NFC_PCSC
{
public class Reader_PCSC : IReader, IDisposable
{
private string _ReaderID;
private IContextFactory _ContextFactory;
private ISCardContext _SCardContext;
private IsoReader _ISOReader;
private ICard _Card;
public Reader_PCSC(string readerID)
{
_ReaderID = readerID;
}
public event ReaderEventHandler CardDiscovered;
public event ReaderEventHandler CardLost;
public void Dispose()
{
Stop();
}
public void Start()
{
_ContextFactory = ContextFactory.Instance;
_SCardContext = _ContextFactory.Establish(SCardScope.System);
_ISOReader = new IsoReader(_SCardContext);
_Card = new Card_PCSC(_ISOReader, _ReaderID);
CardDiscovered?.Invoke(this, _Card);
}
public void Stop()
{
CardLost?.Invoke(this, _Card);
_ISOReader.Dispose();
_SCardContext.Dispose();
}
}
}

View File

@ -413,9 +413,9 @@ namespace NFC_Test.Cards
[Test] [Test]
public void SelectApplication() public void SelectApplication()
{ {
ICard card = Substitute.For<ICard>(); INFCService nfcService = Substitute.For<INFCService>();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card); NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
APDUResponse response = new APDUResponse() APDUResponse response = new APDUResponse()
{ {
@ -423,7 +423,7 @@ namespace NFC_Test.Cards
SW2 = 0x00 SW2 = 0x00
}; };
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "905a00000342414600")).Returns(response); nfcService.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "905a00000342414600")).Returns(response);
desfire.SelectApplication(0x464142); desfire.SelectApplication(0x464142);
} }
@ -431,7 +431,7 @@ namespace NFC_Test.Cards
[Test] [Test]
public void SelectApplication_InvalidAID() public void SelectApplication_InvalidAID()
{ {
ICard card = Substitute.For<ICard>(); INFCService nfcService = Substitute.For<INFCService>();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(null); NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(null);
@ -445,9 +445,9 @@ namespace NFC_Test.Cards
[Test] [Test]
public void AuthenticateISO_DES() public void AuthenticateISO_DES()
{ {
ICard card = Substitute.For<ICard>(); INFCService nfcService = Substitute.For<INFCService>();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card); NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
APDUResponse response_challenge_request = new APDUResponse() APDUResponse response_challenge_request = new APDUResponse()
{ {
@ -466,8 +466,8 @@ namespace NFC_Test.Cards
byte[] rndA = HexConverter.ConvertFromHexString("5f7d1dd12d979173"); byte[] rndA = HexConverter.ConvertFromHexString("5f7d1dd12d979173");
byte[] key = HexConverter.ConvertFromHexString("00000000000000000000000000000000"); byte[] key = HexConverter.ConvertFromHexString("00000000000000000000000000000000");
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "901a0000010000")).Returns(response_challenge_request); nfcService.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "901a0000010000")).Returns(response_challenge_request);
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90af000010f8cdb2eaa42a3167dfcb53852ce267fd00")).Returns(response_challenge_response); nfcService.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90af000010f8cdb2eaa42a3167dfcb53852ce267fd00")).Returns(response_challenge_response);
desfire.AuthenticateISO_DES(0x00, key, rndA); desfire.AuthenticateISO_DES(0x00, key, rndA);
@ -493,9 +493,9 @@ namespace NFC_Test.Cards
[Test] [Test]
public void Format() public void Format()
{ {
ICard card = Substitute.For<ICard>(); INFCService nfcService = Substitute.For<INFCService>();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card); NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
APDUResponse response = new APDUResponse() APDUResponse response = new APDUResponse()
{ {
@ -503,7 +503,7 @@ namespace NFC_Test.Cards
SW2 = 0x00 SW2 = 0x00
}; };
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90fc000000")).Returns(response); nfcService.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90fc000000")).Returns(response);
desfire.Format(); desfire.Format();
} }
@ -511,9 +511,9 @@ namespace NFC_Test.Cards
[Test] [Test]
public void CreateApplication() public void CreateApplication()
{ {
ICard card = Substitute.For<ICard>(); INFCService nfcService = Substitute.For<INFCService>();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card); NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
APDUResponse response = new APDUResponse() APDUResponse response = new APDUResponse()
{ {
@ -521,7 +521,7 @@ namespace NFC_Test.Cards
SW2 = 0x00 SW2 = 0x00
}; };
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90ca000005eeffaa0b8200")).Returns(response); nfcService.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90ca000005eeffaa0b8200")).Returns(response);
desfire.CreateApplication(0xAAFFEE, 0x0b, 0x82); desfire.CreateApplication(0xAAFFEE, 0x0b, 0x82);
} }
@ -529,7 +529,7 @@ namespace NFC_Test.Cards
[Test] [Test]
public void CreateApplication_InvalidAID() public void CreateApplication_InvalidAID()
{ {
ICard card = Substitute.For<ICard>(); INFCService nfcService = Substitute.For<INFCService>();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(null); NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(null);
@ -543,9 +543,9 @@ namespace NFC_Test.Cards
[Test] [Test]
public void AuthenticateISO_AES() public void AuthenticateISO_AES()
{ {
ICard card = Substitute.For<ICard>(); INFCService nfcService = Substitute.For<INFCService>();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card); NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
APDUResponse response_challenge_request = new APDUResponse() APDUResponse response_challenge_request = new APDUResponse()
{ {
@ -564,8 +564,8 @@ namespace NFC_Test.Cards
byte[] rndA = HexConverter.ConvertFromHexString("2176770e7a6eb4bef00d5e4b201d1e57"); byte[] rndA = HexConverter.ConvertFromHexString("2176770e7a6eb4bef00d5e4b201d1e57");
byte[] key = HexConverter.ConvertFromHexString("00000000000000000000000000000000"); byte[] key = HexConverter.ConvertFromHexString("00000000000000000000000000000000");
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90aa0000010000")).Returns(response_challenge_request); nfcService.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90aa0000010000")).Returns(response_challenge_request);
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90af000020cbe9726faf54bc76b2055d0b9700e7dc97ecad5627f1d1702a16e8408d2a0ada00")).Returns(response_challenge_response); nfcService.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90af000020cbe9726faf54bc76b2055d0b9700e7dc97ecad5627f1d1702a16e8408d2a0ada00")).Returns(response_challenge_response);
desfire.AuthenticateISO_AES(0x00, key, rndA); desfire.AuthenticateISO_AES(0x00, key, rndA);
@ -591,9 +591,9 @@ namespace NFC_Test.Cards
[Test] [Test]
public void ChangeKey_AES() public void ChangeKey_AES()
{ {
ICard card = Substitute.For<ICard>(); INFCService nfcService = Substitute.For<INFCService>();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card); NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
APDUResponse response = new APDUResponse() APDUResponse response = new APDUResponse()
{ {
@ -601,7 +601,7 @@ namespace NFC_Test.Cards
SW2 = 0x00 SW2 = 0x00
}; };
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90c400002100c2b54a718d0251845653199909bb32e8e38bd6719e8dc21799c29c922a0984fc00")).Returns(response); nfcService.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90c400002100c2b54a718d0251845653199909bb32e8e38bd6719e8dc21799c29c922a0984fc00")).Returns(response);
byte[] new_key = HexConverter.ConvertFromHexString("25432a462d4a614e645267556b587032"); byte[] new_key = HexConverter.ConvertFromHexString("25432a462d4a614e645267556b587032");
@ -629,9 +629,9 @@ namespace NFC_Test.Cards
[Test] [Test]
public void ChangeOtherKey_AES() public void ChangeOtherKey_AES()
{ {
ICard card = Substitute.For<ICard>(); INFCService nfcService = Substitute.For<INFCService>();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card); NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
APDUResponse response = new APDUResponse() APDUResponse response = new APDUResponse()
{ {
@ -639,7 +639,7 @@ namespace NFC_Test.Cards
SW2 = 0x00 SW2 = 0x00
}; };
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90c400002101a8c5a61a06f56f38dc91266fed2e87dc00a5ad72a634ff0e62c8d6d80707dd6000")).Returns(response); nfcService.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90c400002101a8c5a61a06f56f38dc91266fed2e87dc00a5ad72a634ff0e62c8d6d80707dd6000")).Returns(response);
byte[] new_key = HexConverter.ConvertFromHexString("25432a462d4a614e645267556b587032"); byte[] new_key = HexConverter.ConvertFromHexString("25432a462d4a614e645267556b587032");
byte[] old_key = HexConverter.ConvertFromHexString("00000000000000000000000000000000"); byte[] old_key = HexConverter.ConvertFromHexString("00000000000000000000000000000000");
@ -668,9 +668,9 @@ namespace NFC_Test.Cards
[Test] [Test]
public void CreateFile_Standard() public void CreateFile_Standard()
{ {
ICard card = Substitute.For<ICard>(); INFCService nfcService = Substitute.For<INFCService>();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card); NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
APDUResponse response = new APDUResponse() APDUResponse response = new APDUResponse()
{ {
@ -678,7 +678,7 @@ namespace NFC_Test.Cards
SW2 = 0x00 SW2 = 0x00
}; };
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90cd000007010000e0f0000000")).Returns(response); nfcService.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90cd000007010000e0f0000000")).Returns(response);
UInt16 accesRights = desfire.GenerateFileAccessRights((byte)FileAccessRights.FREE, 0x00, 0x00, 0x00); UInt16 accesRights = desfire.GenerateFileAccessRights((byte)FileAccessRights.FREE, 0x00, 0x00, 0x00);
desfire.CreateFile_Standard(0x01, FileCommunication.PLAIN, accesRights, 0xF0); desfire.CreateFile_Standard(0x01, FileCommunication.PLAIN, accesRights, 0xF0);
@ -687,9 +687,9 @@ namespace NFC_Test.Cards
[Test] [Test]
public void CreateFile_Standard_InvalidFID() public void CreateFile_Standard_InvalidFID()
{ {
ICard card = Substitute.For<ICard>(); INFCService nfcService = Substitute.For<INFCService>();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card); NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
Assert.Throws<ArgumentOutOfRangeException>( Assert.Throws<ArgumentOutOfRangeException>(
delegate delegate
@ -701,9 +701,9 @@ namespace NFC_Test.Cards
[Test] [Test]
public void WriteData() public void WriteData()
{ {
ICard card = Substitute.For<ICard>(); INFCService nfcService = Substitute.For<INFCService>();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card); NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
APDUResponse response = new APDUResponse() APDUResponse response = new APDUResponse()
{ {
@ -711,7 +711,9 @@ namespace NFC_Test.Cards
SW2 = 0x00 SW2 = 0x00
}; };
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "903d00000f01000000080000546573743132333400")).Returns(response); nfcService.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "903d00000f01000000080000546573743132333400")).Returns(response);
desfire._SessionKey = HexConverter.ConvertFromHexString("00000000000000000000000000000000");
desfire.WriteData(0x01, 0, Encoding.ASCII.GetBytes("Test1234")); desfire.WriteData(0x01, 0, Encoding.ASCII.GetBytes("Test1234"));
} }
@ -719,9 +721,9 @@ namespace NFC_Test.Cards
[Test] [Test]
public void WriteData_Long() public void WriteData_Long()
{ {
ICard card = Substitute.For<ICard>(); INFCService nfcService = Substitute.For<INFCService>();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card); NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
APDUResponse response = new APDUResponse() APDUResponse response = new APDUResponse()
{ {
@ -729,9 +731,11 @@ namespace NFC_Test.Cards
SW2 = 0x00 SW2 = 0x00
}; };
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "903d000036010000002f0000546573743132333454657374313233345465737431323334546573743132333454657374313233345465737431323300")).Returns(response); nfcService.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "903d000036010000002f0000546573743132333454657374313233345465737431323334546573743132333454657374313233345465737431323300")).Returns(response);
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "903d000036012f00002f0000345465737431323334546573743132333454657374313233345465737431323334546573743132333454657374313200")).Returns(response); nfcService.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "903d000036012f00002f0000345465737431323334546573743132333454657374313233345465737431323334546573743132333454657374313200")).Returns(response);
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "903d000019015e000012000033345465737431323334546573743132333400")).Returns(response); nfcService.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "903d000019015e000012000033345465737431323334546573743132333400")).Returns(response);
desfire._SessionKey = HexConverter.ConvertFromHexString("00000000000000000000000000000000");
desfire.WriteData(0x01, 0, Encoding.ASCII.GetBytes("Test1234Test1234Test1234Test1234Test1234Test1234Test1234Test1234Test1234Test1234Test1234Test1234Test1234Test1234")); desfire.WriteData(0x01, 0, Encoding.ASCII.GetBytes("Test1234Test1234Test1234Test1234Test1234Test1234Test1234Test1234Test1234Test1234Test1234Test1234Test1234Test1234"));
} }
@ -739,9 +743,9 @@ namespace NFC_Test.Cards
[Test] [Test]
public void WriteData_InvalidFileID() public void WriteData_InvalidFileID()
{ {
ICard card = Substitute.For<ICard>(); INFCService nfcService = Substitute.For<INFCService>();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card); NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
Assert.Throws<ArgumentOutOfRangeException>( Assert.Throws<ArgumentOutOfRangeException>(
delegate delegate
@ -753,9 +757,9 @@ namespace NFC_Test.Cards
[Test] [Test]
public void ReadData() public void ReadData()
{ {
ICard card = Substitute.For<ICard>(); INFCService nfcService = Substitute.For<INFCService>();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card); NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
APDUResponse response = new APDUResponse() APDUResponse response = new APDUResponse()
{ {
@ -764,7 +768,7 @@ namespace NFC_Test.Cards
Body = HexConverter.ConvertFromHexString("54657374313233340000000000000000000000000000000000000000000000009100") Body = HexConverter.ConvertFromHexString("54657374313233340000000000000000000000000000000000000000000000009100")
}; };
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90bd0000070100000020000000")).Returns(response); nfcService.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90bd0000070100000020000000")).Returns(response);
byte[] data = desfire.ReadData(0x01, 0x00, 0x20); byte[] data = desfire.ReadData(0x01, 0x00, 0x20);
@ -774,9 +778,9 @@ namespace NFC_Test.Cards
[Test] [Test]
public void ReadData_CMAC() public void ReadData_CMAC()
{ {
ICard card = Substitute.For<ICard>(); INFCService nfcService = Substitute.For<INFCService>();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card); NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
APDUResponse response = new APDUResponse() APDUResponse response = new APDUResponse()
{ {
@ -785,7 +789,7 @@ namespace NFC_Test.Cards
Body = HexConverter.ConvertFromHexString("5465737431323334000000000000000000000000000000000000000000000000809a9bedbc559a5b9100") Body = HexConverter.ConvertFromHexString("5465737431323334000000000000000000000000000000000000000000000000809a9bedbc559a5b9100")
}; };
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90bd0000070100000020000000")).Returns(response); nfcService.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90bd0000070100000020000000")).Returns(response);
byte[] data = desfire.ReadData(0x01, 0x00, 0x20); byte[] data = desfire.ReadData(0x01, 0x00, 0x20);
@ -795,9 +799,9 @@ namespace NFC_Test.Cards
[Test] [Test]
public void ReadData_Long() public void ReadData_Long()
{ {
ICard card = Substitute.For<ICard>(); INFCService nfcService = Substitute.For<INFCService>();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card); NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
APDUResponse response_1 = new APDUResponse() APDUResponse response_1 = new APDUResponse()
{ {
@ -820,9 +824,9 @@ namespace NFC_Test.Cards
Body = HexConverter.ConvertFromHexString("00009100") Body = HexConverter.ConvertFromHexString("00009100")
}; };
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90bd000007010000002f000000")).Returns(response_1); nfcService.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90bd000007010000002f000000")).Returns(response_1);
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90bd000007012f00002f000000")).Returns(response_2); nfcService.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90bd000007012f00002f000000")).Returns(response_2);
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90bd000007015e000002000000")).Returns(response_3); nfcService.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "90bd000007015e000002000000")).Returns(response_3);
byte[] data = desfire.ReadData(0x01, 0x00, 0x60); byte[] data = desfire.ReadData(0x01, 0x00, 0x60);
@ -832,9 +836,9 @@ namespace NFC_Test.Cards
[Test] [Test]
public void ReadData_InvalidFileID() public void ReadData_InvalidFileID()
{ {
ICard card = Substitute.For<ICard>(); INFCService nfcService = Substitute.For<INFCService>();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card); NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
Assert.Throws<ArgumentOutOfRangeException>( Assert.Throws<ArgumentOutOfRangeException>(
delegate delegate

View File

@ -12,8 +12,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\NFC.PCSC\NFC.PCSC.csproj" />
<ProjectReference Include="..\NFC\NFC.csproj" /> <ProjectReference Include="..\NFC\NFC.csproj" />
<ProjectReference Include="..\NFC_PCSC\NFC_PCSC.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,16 +1,16 @@
using NFC.Cards.NXP_MIFARE_DESFire; using NFC.Helper.Crypto;
using NFC.Cards.NXP_MIFARE_DESFire.Enums;
using NFC.Helper.Crypto;
using NFC.Interfaces; using NFC.Interfaces;
using NFC_PCSC;
using NUnit.Framework; using NUnit.Framework;
using System; using System;
using System.Text; using System.Text;
using NFC.PCSC;
using NFC.Cards.NXP_MIFARE_DESFire;
using NFC.Cards.NXP_MIFARE_DESFire.Enums;
namespace NFC_Test.REAL namespace NFC_Test.REAL
{ {
/// <summary> /// <summary>
/// Test all DESFire Commands with an Empty Card /// Test all DESFire Commands with an Empty nfcService
/// The Test are ordered to check the Commands one by one /// The Test are ordered to check the Commands one by one
/// </summary> /// </summary>
[TestFixture, Explicit] [TestFixture, Explicit]
@ -20,7 +20,7 @@ namespace NFC_Test.REAL
/// Set ReaderID for PCSC Interface /// Set ReaderID for PCSC Interface
/// You can get the ID from REAL_Reader_PCSC /// You can get the ID from REAL_Reader_PCSC
/// </summary> /// </summary>
public readonly string ReaderID = "ACS ACR122U PICC Interface 0"; public readonly string ReaderID = "ACS ACR122 0";//"ACS ACR122U PICC Interface 0";
#region Fixed Config Properties #region Fixed Config Properties
public readonly UInt32 ApplicationID = 0xAAFFEE; public readonly UInt32 ApplicationID = 0xAAFFEE;
@ -33,79 +33,42 @@ namespace NFC_Test.REAL
[Test, Order(1)] [Test, Order(1)]
public void SelectApplication() public void SelectApplication()
{ {
IHardware hardware = new Hardware_PCSC(); INFCService nfcService = new NFCService();
IReader reader = hardware.OpenReader(ReaderID);
bool test_successfully = false; nfcService.Connect(ReaderID);
ReaderEventHandler handler = (sender, card) => NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
{
card.Connect();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card);
desfire.SelectApplication(0x000000); desfire.SelectApplication(0x000000);
test_successfully = true; nfcService.Disconnect();
card.Disconnect();
};
reader.CardDiscovered += handler;
reader.Start();
Assert.AreEqual(true, test_successfully);
reader.Stop();
reader.CardDiscovered -= handler;
} }
[Test, Order(2)] [Test, Order(2)]
public void Authenticate_DES() public void Authenticate_DES()
{ {
IHardware hardware = new Hardware_PCSC(); INFCService nfcService = new NFCService();
IReader reader = hardware.OpenReader(ReaderID);
bool test_successfully = false; nfcService.Connect(ReaderID);
ReaderEventHandler handler = (sender, card) => NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
{
card.Connect();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card);
desfire.SelectApplication(0x000000); desfire.SelectApplication(0x000000);
CipherKey key = new CipherKey(CipherType.TDES); CipherKey key = new CipherKey(CipherType.TDES);
desfire.AuthenticateISO_DES(0x00, key._Key); desfire.AuthenticateISO_DES(0x00, key._Key);
test_successfully = true; nfcService.Disconnect();
card.Disconnect();
};
reader.CardDiscovered += handler;
reader.Start();
Assert.AreEqual(true, test_successfully);
reader.Stop();
reader.CardDiscovered -= handler;
} }
[Test, Order(3)] [Test, Order(3)]
public void Format() public void Format()
{ {
IHardware hardware = new Hardware_PCSC(); INFCService nfcService = new NFCService();
IReader reader = hardware.OpenReader(ReaderID);
bool test_successfully = false; nfcService.Connect(ReaderID);
ReaderEventHandler handler = (sender, card) => NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
{
card.Connect();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card);
desfire.SelectApplication(0x000000); desfire.SelectApplication(0x000000);
@ -114,33 +77,17 @@ namespace NFC_Test.REAL
desfire.Format(); desfire.Format();
test_successfully = true; nfcService.Disconnect();
card.Disconnect();
};
reader.CardDiscovered += handler;
reader.Start();
Assert.AreEqual(true, test_successfully);
reader.Stop();
reader.CardDiscovered -= handler;
} }
[Test, Order(4)] [Test, Order(4)]
public void CreateApplication() public void CreateApplication()
{ {
IHardware hardware = new Hardware_PCSC(); INFCService nfcService = new NFCService();
IReader reader = hardware.OpenReader(ReaderID);
bool test_successfully = false; nfcService.Connect(ReaderID);
ReaderEventHandler handler = (sender, card) => NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
{
card.Connect();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card);
desfire.SelectApplication(0x000000); desfire.SelectApplication(0x000000);
@ -156,33 +103,17 @@ namespace NFC_Test.REAL
desfire.CreateApplication(ApplicationID, keysetting1, keysetting2); desfire.CreateApplication(ApplicationID, keysetting1, keysetting2);
test_successfully = true; nfcService.Disconnect();
card.Disconnect();
};
reader.CardDiscovered += handler;
reader.Start();
Assert.AreEqual(true, test_successfully);
reader.Stop();
reader.CardDiscovered -= handler;
} }
[Test, Order(5)] [Test, Order(5)]
public void Authenticate_AES() public void Authenticate_AES()
{ {
IHardware hardware = new Hardware_PCSC(); INFCService nfcService = new NFCService();
IReader reader = hardware.OpenReader(ReaderID);
bool test_successfully = false; nfcService.Connect(ReaderID);
ReaderEventHandler handler = (sender, card) => NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
{
card.Connect();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card);
desfire.SelectApplication(0x000000); desfire.SelectApplication(0x000000);
@ -203,33 +134,17 @@ namespace NFC_Test.REAL
CipherKey key_aes = new CipherKey(CipherType.AES); CipherKey key_aes = new CipherKey(CipherType.AES);
desfire.AuthenticateISO_AES(0x00, key_aes._Key); desfire.AuthenticateISO_AES(0x00, key_aes._Key);
test_successfully = true; nfcService.Disconnect();
card.Disconnect();
};
reader.CardDiscovered += handler;
reader.Start();
Assert.AreEqual(true, test_successfully);
reader.Stop();
reader.CardDiscovered -= handler;
} }
[Test, Order(6)] [Test, Order(6)]
public void ChangeApplicationMasterKey() public void ChangeApplicationMasterKey()
{ {
IHardware hardware = new Hardware_PCSC(); INFCService nfcService = new NFCService();
IReader reader = hardware.OpenReader(ReaderID);
bool test_successfully = false; nfcService.Connect(ReaderID);
ReaderEventHandler handler = (sender, card) => NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
{
card.Connect();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card);
desfire.SelectApplication(0x000000); desfire.SelectApplication(0x000000);
@ -253,33 +168,17 @@ namespace NFC_Test.REAL
CipherKey key_aes_new = new CipherKey(ApplicationMasterKey, CipherType.AES, 0x10); CipherKey key_aes_new = new CipherKey(ApplicationMasterKey, CipherType.AES, 0x10);
desfire.ChangeKey_AES(0x00, key_aes_new._Key, key_aes_new._KeyVersion); desfire.ChangeKey_AES(0x00, key_aes_new._Key, key_aes_new._KeyVersion);
test_successfully = true; nfcService.Disconnect();
card.Disconnect();
};
reader.CardDiscovered += handler;
reader.Start();
Assert.AreEqual(true, test_successfully);
reader.Stop();
reader.CardDiscovered -= handler;
} }
[Test, Order(7)] [Test, Order(7)]
public void ChangeApplicationKey_1() public void ChangeApplicationKey_1()
{ {
IHardware hardware = new Hardware_PCSC(); INFCService nfcService = new NFCService();
IReader reader = hardware.OpenReader(ReaderID);
bool test_successfully = false; nfcService.Connect(ReaderID);
ReaderEventHandler handler = (sender, card) => NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
{
card.Connect();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card);
desfire.SelectApplication(0x000000); desfire.SelectApplication(0x000000);
@ -303,33 +202,17 @@ namespace NFC_Test.REAL
CipherKey key_new = new CipherKey(ApplicationKey_1, CipherType.AES, 0x10); CipherKey key_new = new CipherKey(ApplicationKey_1, CipherType.AES, 0x10);
desfire.ChangeOtherKey_AES(0x01, key_new._Key, key_aes._Key, key_new._KeyVersion); desfire.ChangeOtherKey_AES(0x01, key_new._Key, key_aes._Key, key_new._KeyVersion);
test_successfully = true; nfcService.Disconnect();
card.Disconnect();
};
reader.CardDiscovered += handler;
reader.Start();
Assert.AreEqual(true, test_successfully);
reader.Stop();
reader.CardDiscovered -= handler;
} }
[Test, Order(8)] [Test, Order(8)]
public void CreateFile() public void CreateFile()
{ {
IHardware hardware = new Hardware_PCSC(); INFCService nfcService = new NFCService();
IReader reader = hardware.OpenReader(ReaderID);
bool test_successfully = false; nfcService.Connect(ReaderID);
ReaderEventHandler handler = (sender, card) => NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
{
card.Connect();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card);
desfire.SelectApplication(0x000000); desfire.SelectApplication(0x000000);
@ -353,33 +236,17 @@ namespace NFC_Test.REAL
UInt16 accesRights = desfire.GenerateFileAccessRights((byte)FileAccessRights.FREE, 0x00, 0x00, 0x00); UInt16 accesRights = desfire.GenerateFileAccessRights((byte)FileAccessRights.FREE, 0x00, 0x00, 0x00);
desfire.CreateFile_Standard(FileID, FileCommunication.PLAIN, accesRights, FileSize); desfire.CreateFile_Standard(FileID, FileCommunication.PLAIN, accesRights, FileSize);
test_successfully = true; nfcService.Disconnect();
card.Disconnect();
};
reader.CardDiscovered += handler;
reader.Start();
Assert.AreEqual(true, test_successfully);
reader.Stop();
reader.CardDiscovered -= handler;
} }
[Test, Order(9)] [Test, Order(9)]
public void WriteFile() public void WriteFile()
{ {
IHardware hardware = new Hardware_PCSC(); INFCService nfcService = new NFCService();
IReader reader = hardware.OpenReader(ReaderID);
bool test_successfully = false; nfcService.Connect(ReaderID);
ReaderEventHandler handler = (sender, card) => NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
{
card.Connect();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card);
desfire.SelectApplication(0x000000); desfire.SelectApplication(0x000000);
@ -405,33 +272,17 @@ namespace NFC_Test.REAL
desfire.WriteData(FileID, 0, Encoding.ASCII.GetBytes("Test1234")); desfire.WriteData(FileID, 0, Encoding.ASCII.GetBytes("Test1234"));
test_successfully = true; nfcService.Disconnect();
card.Disconnect();
};
reader.CardDiscovered += handler;
reader.Start();
Assert.AreEqual(true, test_successfully);
reader.Stop();
reader.CardDiscovered -= handler;
} }
[Test, Order(10)] [Test, Order(10)]
public void ReadFile() public void ReadFile()
{ {
IHardware hardware = new Hardware_PCSC(); INFCService nfcService = new NFCService();
IReader reader = hardware.OpenReader(ReaderID);
bool test_successfully = false; nfcService.Connect(ReaderID);
ReaderEventHandler handler = (sender, card) => NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
{
card.Connect();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card);
desfire.SelectApplication(0x000000); desfire.SelectApplication(0x000000);
@ -460,18 +311,7 @@ namespace NFC_Test.REAL
byte[] data = desfire.ReadData(FileID, 0, FileSize); byte[] data = desfire.ReadData(FileID, 0, FileSize);
Console.WriteLine(Encoding.ASCII.GetString(data).Replace("\u0000", "")); Console.WriteLine(Encoding.ASCII.GetString(data).Replace("\u0000", ""));
test_successfully = true; nfcService.Disconnect();
card.Disconnect();
};
reader.CardDiscovered += handler;
reader.Start();
Assert.AreEqual(true, test_successfully);
reader.Stop();
reader.CardDiscovered -= handler;
} }
} }
} }

View File

@ -1,18 +1,18 @@
using NFC; using NFC;
using NFC.Cards.NXP_MIFARE_DESFire;
using NFC.Cards.NXP_MIFARE_DESFire.Enums;
using NFC.Helper; using NFC.Helper;
using NFC.Helper.Crypto; using NFC.Helper.Crypto;
using NFC.Interfaces; using NFC.Interfaces;
using NFC_PCSC;
using NUnit.Framework; using NUnit.Framework;
using System; using System;
using System.Text; using System.Text;
using NFC.PCSC;
using NFC.Cards.NXP_MIFARE_DESFire.Enums;
using NFC.Cards.NXP_MIFARE_DESFire;
namespace NFC_Test.REAL namespace NFC_Test.REAL
{ {
/// <summary> /// <summary>
/// Test all DESFire Commands with an Empty Card /// Test all DESFire Commands with an Empty nfcService
/// The Test are ordered to check the Commands one by one /// The Test are ordered to check the Commands one by one
/// </summary> /// </summary>
[TestFixture, Explicit] [TestFixture, Explicit]
@ -22,7 +22,7 @@ namespace NFC_Test.REAL
/// Set ReaderID for PCSC Interface /// Set ReaderID for PCSC Interface
/// You can get the ID from REAL_Reader_PCSC /// You can get the ID from REAL_Reader_PCSC
/// </summary> /// </summary>
public readonly string ReaderID = "ACS ACR122U PICC Interface 0"; public readonly string ReaderID = "ACS ACR122 0";// "ACS ACR122U PICC Interface 0";
#region Fixed Config Properties #region Fixed Config Properties
public readonly UInt32 ApplicationID = 0xAAFFEE; public readonly UInt32 ApplicationID = 0xAAFFEE;
@ -35,16 +35,11 @@ namespace NFC_Test.REAL
[Test, Order(1)] [Test, Order(1)]
public void CreateFile() public void CreateFile()
{ {
IHardware hardware = new Hardware_PCSC(); INFCService nfcService = new NFCService();
IReader reader = hardware.OpenReader(ReaderID);
bool test_successfully = false; nfcService.Connect(ReaderID);
ReaderEventHandler handler = (sender, card) => NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
{
card.Connect();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card);
desfire.SelectApplication(0x000000); desfire.SelectApplication(0x000000);
@ -77,24 +72,13 @@ namespace NFC_Test.REAL
Data = ByteOperation.GenerateEmptyArray(8) Data = ByteOperation.GenerateEmptyArray(8)
}; };
Console.WriteLine(cmd_WriteData.ToString()); Console.WriteLine(cmd_WriteData.ToString());
APDUResponse response = card.Transmit(cmd_WriteData); APDUResponse response = nfcService.Transmit(cmd_WriteData);
Console.WriteLine(response.ToString()); Console.WriteLine(response.ToString());
byte[] data = desfire.ReadData(FileID, 0, FileSize); byte[] data = desfire.ReadData(FileID, 0, FileSize);
Console.WriteLine(Encoding.ASCII.GetString(data).Replace("\u0000", "")); Console.WriteLine(Encoding.ASCII.GetString(data).Replace("\u0000", ""));
test_successfully = true; nfcService.Disconnect();
card.Disconnect();
};
reader.CardDiscovered += handler;
reader.Start();
Assert.AreEqual(true, test_successfully);
reader.Stop();
reader.CardDiscovered -= handler;
} }
} }
} }

View File

@ -1,24 +1,24 @@
using NFC.Cards.NXP_MIFARE_DESFire; using NFC.Helper.Crypto;
using NFC.Cards.NXP_MIFARE_DESFire.Enums;
using NFC.Helper.Crypto;
using NFC.Interfaces; using NFC.Interfaces;
using NFC_PCSC;
using NUnit.Framework; using NUnit.Framework;
using System; using System;
using System.Text; using System.Text;
using NFC.PCSC;
using NFC.Cards.NXP_MIFARE_DESFire;
using NFC.Cards.NXP_MIFARE_DESFire.Enums;
namespace NFC_Test.REAL namespace NFC_Test.REAL
{ {
[TestFixture, Explicit] [TestFixture, Explicit]
public class REAL_FabAccess_OTA public class REAL_FabAccess_OTA
{ {
private string _ReaderID = "ACS ACR122U PICC Interface 0"; private string _ReaderID = "ACS ACR122 0";//"ACS ACR122U PICC Interface 0";
private UInt32 _FabAccess_AID = 0x2A472D; private UInt32 _FabAccess_AID = 0x2A472D;
private byte _FabAccess_FID = 0x01; private byte _FabAccess_FID = 0x01;
private UInt32 _FabAccess_FSize = 0xF0; private UInt32 _FabAccess_FSize = 0xF0;
// Change of PICC Key is not implementet yet // Change of PICC Key is not implementet yet
// private CipherKey _FabAccess_Card_MasterKey = new CipherKey("294A404E635266556A576E5A72347537", CipherType.AES, 0x10); // private CipherKey _FabAccess_nfcService_MasterKey = new CipherKey("294A404E635266556A576E5A72347537", CipherType.AES, 0x10);
private CipherKey _FabAccess_Application_MasterKey = new CipherKey("50645367566B59703273357638792F42", CipherType.AES, 0x10); private CipherKey _FabAccess_Application_MasterKey = new CipherKey("50645367566B59703273357638792F42", CipherType.AES, 0x10);
private CipherKey _FabAccess_Application_AuthKey = new CipherKey("6D5A7134743677397A24432646294A40", CipherType.AES, 0x10); private CipherKey _FabAccess_Application_AuthKey = new CipherKey("6D5A7134743677397A24432646294A40", CipherType.AES, 0x10);
@ -35,16 +35,11 @@ namespace NFC_Test.REAL
[Test, Order(1)] [Test, Order(1)]
public void Init_EmptyCard() public void Init_EmptyCard()
{ {
IHardware hardware = new Hardware_PCSC(); INFCService nfcService = new NFCService();
IReader reader = hardware.OpenReader(_ReaderID);
bool transmit_successfully = false; nfcService.Connect(_ReaderID);
ReaderEventHandler handler = (sender, card) => NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
{
card.Connect();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card);
desfire.SelectApplication(0x000000); desfire.SelectApplication(0x000000);
desfire.AuthenticateISO_DES(0x00, _Default_DESKey._Key); desfire.AuthenticateISO_DES(0x00, _Default_DESKey._Key);
@ -69,18 +64,7 @@ namespace NFC_Test.REAL
desfire.WriteData(_FabAccess_FID, 0, Encoding.ASCII.GetBytes(_FabAccess_UserDomain)); desfire.WriteData(_FabAccess_FID, 0, Encoding.ASCII.GetBytes(_FabAccess_UserDomain));
transmit_successfully = true; nfcService.Disconnect();
card.Disconnect();
};
reader.CardDiscovered += handler;
reader.Start();
Assert.AreEqual(true, transmit_successfully);
reader.Stop();
reader.CardDiscovered -= handler;
} }
/// <summary> /// <summary>
@ -89,23 +73,18 @@ namespace NFC_Test.REAL
[Test, Order(2)] [Test, Order(2)]
public void Authenticate() public void Authenticate()
{ {
IHardware hardware = new Hardware_PCSC(); INFCService nfcService = new NFCService();
IReader reader = hardware.OpenReader(_ReaderID);
bool transmit_successfully = false; nfcService.Connect(_ReaderID);
ReaderEventHandler handler = (sender, card) => NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
{
card.Connect();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card);
desfire.SelectApplication(_FabAccess_AID); desfire.SelectApplication(_FabAccess_AID);
byte[] card_data = desfire.ReadData(_FabAccess_FID, 0x00, _FabAccess_FSize); byte[] nfcService_data = desfire.ReadData(_FabAccess_FID, 0x00, _FabAccess_FSize);
string userdomain = Encoding.ASCII.GetString(card_data).Replace("\u0000", ""); string userdomain = Encoding.ASCII.GetString(nfcService_data).Replace("\u0000", "");
string domain = userdomain.Split('@')[1]; string domain = userdomain.Split('@')[1];
if(domain != _FabAccess_Domain) if (domain != _FabAccess_Domain)
{ {
throw new Exception("Incorrect Domain"); throw new Exception("Incorrect Domain");
} }
@ -113,18 +92,7 @@ namespace NFC_Test.REAL
desfire.SelectApplication(_FabAccess_AID); desfire.SelectApplication(_FabAccess_AID);
desfire.AuthenticateISO_AES(0x01, _FabAccess_Application_AuthKey._Key); desfire.AuthenticateISO_AES(0x01, _FabAccess_Application_AuthKey._Key);
transmit_successfully = true; nfcService.Disconnect();
card.Disconnect();
};
reader.CardDiscovered += handler;
reader.Start();
Assert.AreEqual(true, transmit_successfully);
reader.Stop();
reader.CardDiscovered -= handler;
} }
} }
} }

View File

@ -1,7 +1,8 @@
using NFC.Interfaces; using NFC.Interfaces;
using NFC_PCSC; using NFC.PCSC;
using NUnit.Framework; using NUnit.Framework;
using System; using System;
using System.Collections.Generic;
namespace NFC_Test.REAL namespace NFC_Test.REAL
{ {
@ -14,12 +15,12 @@ namespace NFC_Test.REAL
[Test] [Test]
public void GetReaders() public void GetReaders()
{ {
IHardware hardware = new Hardware_PCSC(); INFCService nfcService = new NFCService();
string[] readers = hardware.GetReaders(); IList<string> readers = nfcService.GetReaderIDs();
Console.WriteLine("PCSC Readers detected: {0}", readers.Length); Console.WriteLine("PCSC Readers detected: {0}", readers.Count);
if (readers.Length > 0) if (readers.Count > 0)
{ {
Console.WriteLine("List of ReaderIDs:"); Console.WriteLine("List of ReaderIDs:");
foreach (string readerID in readers) foreach (string readerID in readers)
@ -33,30 +34,15 @@ namespace NFC_Test.REAL
/// Connect to specific PCSC Reader by ReaderID /// Connect to specific PCSC Reader by ReaderID
/// </summary> /// </summary>
/// <param name="readerID">ReaderID from GetReaders</param> /// <param name="readerID">ReaderID from GetReaders</param>
[TestCase("ACS ACR122U PICC Interface 0")] //[TestCase("ACS ACR122U PICC Interface 0")]
[TestCase("ACS ACR122 0")]
public void Connect(string readerID) public void Connect(string readerID)
{ {
IHardware hardware = new Hardware_PCSC(); INFCService nfcService = new NFCService();
IReader reader = hardware.OpenReader(readerID);
bool connected_successfully = false; nfcService.Connect(readerID);
ReaderEventHandler handler = (sender, card) => nfcService.Disconnect();
{
card.Connect();
connected_successfully = true;
card.Disconnect();
};
reader.CardDiscovered += handler;
reader.Start();
Assert.AreEqual(true, connected_successfully);
reader.Stop();
reader.CardDiscovered -= handler;
} }
} }
} }