Fix: Cards not properly reset

This commit is contained in:
nicolas.dorier 2024-02-08 18:17:26 +09:00
parent 1e7032b236
commit 5156ea0fbf
No known key found for this signature in database
GPG Key ID: 6618763EF09186FE
2 changed files with 41 additions and 9 deletions

View File

@ -302,20 +302,39 @@ retry:
{
await IsoSelectFile(ISOLevel.Application, cancellationToken);
await IsoSelectFile(DataFile.NDEF, cancellationToken);
var aa = (await SendAPDU(NtagCommands.ISOReadBinary with
{
P1 = 0,
P2 = 0,
Le = 2
}, cancellationToken)).Data;
var size = (await SendAPDU(NtagCommands.ISOReadBinary with
{
P1 = 0,
P2 = 0,
Le = 2
}, cancellationToken)).Data[1];
var data = (await SendAPDU(NtagCommands.ISOReadBinary with
try
{
P1 = 0,
P2 = 2,
Le = size
}, cancellationToken)).Data;
CurrentSession = null;
return NdefMessage.FromByteArray(data);
if (size == 0)
{
return new NdefMessage();
}
else
{
var data = (await SendAPDU(NtagCommands.ISOReadBinary with
{
P1 = 0,
P2 = 2,
Le = size
}, cancellationToken)).Data;
return NdefMessage.FromByteArray(data);
}
}
finally
{
CurrentSession = null;
}
}
public async Task<byte[]> ReadFile(DataFile file, int offset, int length, CancellationToken cancellationToken = default)
@ -372,6 +391,20 @@ retry:
)
}, cancellationToken);
}
public async Task ResetNDef(CancellationToken cancellationToken = default)
{
var content = new byte[220];
await SendAPDU(NtagCommands.WriteData with
{
CommMode = await GetCommMode(DataFile.NDEF, AccessRight.Write, cancellationToken),
Data = Concat(
GetFileNo(DataFile.NDEF),
new byte[] { 0x00, 0x00, 0x00 },
new byte[] { (byte)content.Length, 0, 0 },
content
)
}, cancellationToken);
}
public async Task ChangeKey(int keyNo, AESKey newKey, AESKey? oldKey = null, int version = 0, CancellationToken cancellationToken = default)
{
@ -435,7 +468,7 @@ retry:
if (CurrentSession!.KeyNo != 0)
throw new InvalidOperationException("Authentication required with KeyNo 0");
await WriteNDef(new NdefMessage());
await ResetNDef();
await ChangeFileSettings(file: DataFile.NDEF, new FileSettings(DataFile.NDEF));
await ChangeKey(4, AESKey.Default, keys.K4);

View File

@ -173,7 +173,6 @@ public class UnitTest1
Assert.Equal(uid1.ToHex(), uid2.ToHex());
}
[Fact]
public async Task CanDecryptAndCheckSUNMacOnDeterministicKey()