Added: Dummy

This commit is contained in:
TheJoKlLa 2023-02-02 05:26:48 +01:00
parent d737faa989
commit b86ecf23a2
3 changed files with 224 additions and 6 deletions

View File

@ -0,0 +1,63 @@
using Borepin.Page;
using FabAccessAPI.Schema;
using ImTools;
using NFC.Helper;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Borepin.Model
{
public class DESFireInterfaceDummy : User.ICardDESFireInterface
{
public Task Bind(IReadOnlyList<byte> token, IReadOnlyList<byte> auth_key, CancellationToken cancellationToken_ = default)
{
return Task.CompletedTask;
}
public void Dispose()
{
}
public Task<IReadOnlyList<byte>> GenCardToken(CancellationToken cancellationToken_ = default)
{
List<byte> token = new List<byte>();
token.AddRange(HexConverter.ConvertFromHexString("11 00 00 00 00 00 00 00 00 00 00 00 00 00 00 11".Replace(" ", "")));
return Task.FromResult((IReadOnlyList<byte>) token);
}
public Task<IReadOnlyList<byte>> GetMetaInfo(CancellationToken cancellationToken_ = default)
{
List<byte> data = new List<byte>();
data.AddRange(Encoding.ASCII.GetBytes("FABACCESS\0DESFIRE\01.0\0").Append((byte)0x00));
return Task.FromResult((IReadOnlyList<byte>)data);
}
public Task<IReadOnlyList<byte>> GetSpaceInfo(CancellationToken cancellationToken_ = default)
{
List<byte> data = new List<byte>();
data.AddRange(Encoding.ASCII.GetBytes("urn:fabaccess:lab:fabaccess_test").Append((byte)0x00));
return Task.FromResult((IReadOnlyList<byte>)data);
}
public Task<IReadOnlyList<IReadOnlyList<byte>>> GetTokenList(CancellationToken cancellationToken_ = default)
{
List<byte> token = new List<byte>();
token.AddRange(HexConverter.ConvertFromHexString("11 00 00 00 00 00 00 00 00 00 00 00 00 00 00 11".Replace(" ", "")));
IReadOnlyList<IReadOnlyList<byte>> list = new List<List<byte>>
{
token
};
return Task.FromResult(list);
}
public Task Unbind(IReadOnlyList<byte> token, CancellationToken cancellationToken_ = default)
{
return Task.CompletedTask;
}
}
}

View File

@ -1,6 +1,11 @@
using Borepin.Service.ErrorMessage; using Borepin.Service.ErrorMessage;
using NFC.Cards.NXP_MIFARE_DESFire;
using NFC.Cards.NXP_MIFARE_DESFire.Enums;
using NFC.Helper;
using NFC.Helper.Crypto;
using NFC.Interfaces; using NFC.Interfaces;
using System; using System;
using ZXing.Aztec.Internal;
namespace Borepin.Model namespace Borepin.Model
{ {
@ -20,14 +25,89 @@ namespace Borepin.Model
#endregion #endregion
#region Methods #region Methods
/// <summary>
/// Format Card
/// </summary>
/// <param name="readerID"></param>
/// <param name="cardConfig"></param>
public void FormatCard(string readerID, CardConfig cardConfig) public void FormatCard(string readerID, CardConfig cardConfig)
{ {
throw new NotImplementedException(); _NFCService.Connect(readerID);
NXP_MIFARE_DESFire card = new NXP_MIFARE_DESFire(_NFCService);
card.SelectApplication(0x000000);
card.AuthenticateISO_DES(0x00, cardConfig.PICCKey);
card.Format();
_NFCService.Disconnect();
} }
public void CreateCard(string readerID, CardConfig cardConfig) /// <summary>
/// Create DESFire Card for V1.0
/// </summary>
/// <param name="readerID"></param>
/// <param name="cardConfig"></param>
/// <returns>Key #1 for authentication </returns>
public byte[] CreateCard(string readerID, CardConfig cardConfig)
{ {
throw new NotImplementedException(); _NFCService.Connect(readerID);
NXP_MIFARE_DESFire card = new NXP_MIFARE_DESFire(_NFCService);
card.SelectApplication(0x000000);
if(cardConfig.DoFormat)
{
card.AuthenticateISO_DES(0x00, new CipherKey(CipherType.TDES));
}
else
{
card.AuthenticateISO_DES(0x00, cardConfig.PICCKey);
}
byte keySettings1 = card.GenerateKeySetting1
(
0x00,
ChangeMasterKeySettings.WITHMASTERKEY,
CreateDeleteFile.NOKEY,
FileDirectoryAccess.NOKEY,
ChangeMasterKey.CHANGEABLE
);
byte keySettings2 = card.GenerateKeySetting2
(
CryptoOperationsType.AES,
FileIdentifies.NOTUSED,
0x02
);
card.CreateApplication(0x464142, keySettings1, keySettings2);
card.SelectApplication(0x464142);
card.AuthenticateISO_AES(0x00, new CipherKey(CipherType.AES));
ushort file_access = card.GenerateFileAccessRights
(
FileAccessRights.FREE,
0x00,
0x00,
0x00
);
card.CreateFile_Standard(0x01, FileCommunication.PLAIN, file_access, cardConfig.MetaInfo.Length);
card.CreateFile_Standard(0x02, FileCommunication.PLAIN, file_access, cardConfig.SpaceInfo.Length);
card.CreateFile_Standard(0x03, FileCommunication.PLAIN, file_access, cardConfig.CardToken.Length);
card.WriteData(0x00, 0x00, cardConfig.MetaInfo);
card.WriteData(0x00, 0x00, cardConfig.SpaceInfo);
card.WriteData(0x00, 0x00, cardConfig.CardToken);
auth_key = cardConfig.GenerateRandomKey();
card.ChangeOtherKey_AES(0x01, auth_key, new CipherKey(CipherType.AES), 0x00);
card.ChangeKey_AES(0x00, cardConfig.APPKey, 0x00);
_NFCService.Disconnect();
return auth_key;
} }
#endregion #endregion
} }

View File

@ -12,6 +12,8 @@ using System.Collections.Generic;
using Borepin.Model; using Borepin.Model;
using Xamarin.Forms; using Xamarin.Forms;
using System; using System;
using Borepin.Service.ErrorMessage;
using System.Linq;
namespace Borepin.PageModel namespace Borepin.PageModel
{ {
@ -21,15 +23,18 @@ namespace Borepin.PageModel
private CardConfig _CardConfig; private CardConfig _CardConfig;
private User _User; private User _User;
private INFCService _NFCService; private INFCService _NFCService;
private User.ICardDESFireInterface _CardDESFireInterface;
private IErrorMessageService _ErrorMessageService;
private KeyTypes _ScanKeyType = KeyTypes.NONE; private KeyTypes _ScanKeyType = KeyTypes.NONE;
private KeyTypes _ScanedKeyType = KeyTypes.NONE; private KeyTypes _ScanedKeyType = KeyTypes.NONE;
#endregion #endregion
#region Contructors #region Contructors
public CreateCardPageModel(INavigationService navigationService, IPageDialogService pageDialogService, IAPIService apiService, INFCService nfcService) : base(navigationService, pageDialogService, apiService) public CreateCardPageModel(INavigationService navigationService, IPageDialogService pageDialogService, IAPIService apiService, INFCService nfcService, IErrorMessageService errorMessageService) : base(navigationService, pageDialogService, apiService)
{ {
_NFCService = nfcService; _NFCService = nfcService;
_ErrorMessageService = errorMessageService;
_CardConfig= new CardConfig(); _CardConfig= new CardConfig();
CreateCardCommand = new DelegateCommand(CreateCardCommandExecute); CreateCardCommand = new DelegateCommand(CreateCardCommandExecute);
@ -39,6 +44,9 @@ namespace Borepin.PageModel
RandomAPPKeyCommand = new DelegateCommand(RandomAPPKeyCommandExecute); RandomAPPKeyCommand = new DelegateCommand(RandomAPPKeyCommandExecute);
RefreshCommand = new DelegateCommand(async () => await RefreshCommandExecute().ConfigureAwait(true)); RefreshCommand = new DelegateCommand(async () => await RefreshCommandExecute().ConfigureAwait(true));
// TODO Use Server Interface
_CardDESFireInterface = new DESFireInterfaceDummy();
} }
#endregion #endregion
@ -230,7 +238,75 @@ namespace Borepin.PageModel
{ {
IsBusy = true; IsBusy = true;
await Task.Delay(1000).ConfigureAwait(false); try
{
_CardConfig.PICCKey = _CardConfig.ConvertFromString(PICCKey);
}
catch
{
Device.BeginInvokeOnMainThread(async () =>
{
await _PageDialogService.DisplayAlertAsync("NFC Error", "PICC Key invalid", Resources.Text.TextResource.OK).ConfigureAwait(false);
IsBusy = false;
});
return;
}
try
{
_CardConfig.APPKey = _CardConfig.ConvertFromString(APPKey);
}
catch
{
Device.BeginInvokeOnMainThread(async () =>
{
await _PageDialogService.DisplayAlertAsync("NFC Error", "APP Key invalid", Resources.Text.TextResource.OK).ConfigureAwait(false);
IsBusy = false;
});
return;
}
_CardConfig.DoFormat = FormatCard;
_CardConfig.CardToken = (await _CardDESFireInterface.GenCardToken().ConfigureAwait(false)).ToArray();
_CardConfig.SpaceInfo = (await _CardDESFireInterface.GetSpaceInfo().ConfigureAwait(false)).ToArray();
_CardConfig.MetaInfo = (await _CardDESFireInterface.GetMetaInfo().ConfigureAwait(false)).ToArray();
FabFireCard fabFireCard = new FabFireCard(_NFCService, _ErrorMessageService);
try
{
if(FormatCard)
{
fabFireCard.FormatCard(ReaderID, _CardConfig);
}
}
catch
{
Device.BeginInvokeOnMainThread(async () =>
{
await _PageDialogService.DisplayAlertAsync("NFC Error", "Format failed", Resources.Text.TextResource.OK).ConfigureAwait(false);
IsBusy = false;
});
}
try
{
byte[] key = fabFireCard.CreateCard(ReaderID, _CardConfig);
await _CardDESFireInterface.Bind(_CardConfig.CardToken, new List<byte>(key)).ConfigureAwait(false);
}
catch
{
Device.BeginInvokeOnMainThread(async () =>
{
await _PageDialogService.DisplayAlertAsync("NFC Error", "Card Creation failed", Resources.Text.TextResource.OK).ConfigureAwait(false);
IsBusy = false;
});
}
IsBusy = false; IsBusy = false;
} }
@ -344,7 +420,6 @@ namespace Borepin.PageModel
IsBusy = false; IsBusy = false;
} }
#endregion #endregion
#endregion #endregion
} }
} }