Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestLogin & static key location map #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/MiFare/Classic/ICardReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ interface ICardReader
/// <returns>tru on success, false otherwise</returns>
Task<bool> Login(int sector, InternalKeyType key);

/// <summary>
/// Tests if login into the given sector using the given key is possible
/// </summary>
/// <param name="sector">sector to login into</param>
/// <param name="keytype">keytype to use (e.g. KeyA, KeyB)</param>
/// <param name="key">key to use</param>
/// <returns>tru on success, false otherwise</returns>
Task<bool> TestLogin(int sector,KeyType keytype, byte[] key);



/// <summary>
/// read a datablock from a sector
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions src/MiFare/Classic/MiFARECard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,20 @@ public async Task SetData(int sector, int dataBlock, byte[] data)
}
}



/// <summary>
/// Tests if login into the given sector using the given key is possible
/// </summary>
/// <param name="sector">sector to login into</param>
/// <param name="keytype">keytype to use (e.g. KeyA, KeyB)</param>
/// <param name="key">key to use</param>
/// <returns>tru on success, false otherwise</returns>
public async Task<bool> TestLogin(int sector, KeyType keytype, byte[] key)
{
return await GetSector(sector).TestLogin(keytype, key);
}

/// <summary>
/// reinitialize the object
/// </summary>
Expand Down
31 changes: 29 additions & 2 deletions src/MiFare/Classic/MiFareStandardCardReaderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ abstract class MiFareStandardCardReaderBase : ICardReader, IDisposable

private Dictionary<SectorKey, byte[]> keyMap = new Dictionary<SectorKey, byte[]>();

private byte nextKeySlot;
private static byte nextKeySlot;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can't be static since it wouldn't apply to a different card if placed under the reader.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry didn't think about this case. Maybe instead it would make sense to just remove the IDisposable interface? Or use as static and clear when card's removed?


private Dictionary<byte[], byte> keyToLocationMap = new Dictionary<byte[], byte>(KeyEqualityComparer.Default);
private static Dictionary<byte[], byte> keyToLocationMap = new Dictionary<byte[], byte>(KeyEqualityComparer.Default);

protected MiFareStandardCardReaderBase(IReadOnlyCollection<SectorKeySet> keys)
{
Expand Down Expand Up @@ -111,6 +111,33 @@ public async Task<bool> Login(int sector, InternalKeyType key)
return res.Succeeded;
}

public async Task<bool> TestLogin(int sector, KeyType keytype, byte[] key)
{
//for testing we ignore the keymap
var gaKeyType = (GeneralAuthenticate.GeneralAuthenticateKeyType)keytype;

// Get the block for the sector
var blockNumber = SectorToBlock(sector, 0);

// see if we have the key loaded already
byte location;
if (!keyToLocationMap.TryGetValue(key, out location))
{
location = nextKeySlot;
nextKeySlot++;
keyToLocationMap[key] = location;

// Load the key to the location
var r = await TransceiveAsync(new LoadKey(key, location));
if (!r.Succeeded)
return false; // could not load the key
}

var res = await TransceiveAsync(new PcSc.MiFareStandard.GeneralAuthenticate(blockNumber, location, gaKeyType));

return res.Succeeded;
}

public async Task<Tuple<bool, byte[]>> Read(int sector, int datablock)
{
var blockNumber = SectorToBlock(sector, datablock);
Expand Down
5 changes: 5 additions & 0 deletions src/MiFare/Classic/Sector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ private async Task FlushDataBlock(DataBlock dataBlock)
throw new CardWriteException($"Unable to write in sector {sector}, block {dataBlock.Number}");
}

public async Task<bool> TestLogin(KeyType keytype, byte[] key)
{
return await card.Reader.TestLogin(sector, keytype, key);
}

private async Task<DataBlock> GetDataBlockInt(int block)
{
var db = dataBlocks[block];
Expand Down