Set key version to 1 during boltcard programming

This commit is contained in:
nicolas.dorier 2024-09-04 18:20:41 +09:00
parent db04df5797
commit 7e0499b927
No known key found for this signature in database
GPG Key ID: 6618763EF09186FE
2 changed files with 26 additions and 5 deletions

View File

@ -452,6 +452,16 @@ retry:
await ResetCard(keys);
}
public async Task<int> GetKeyVersion(int keyNo, CancellationToken cancellationToken = default)
{
var resp = await SendAPDU(
NtagCommands.GetKeyVersion with
{
Data = new byte[] { (byte)keyNo },
}, cancellationToken);
return resp.Data[0];
}
/// <summary>
/// Reset the card to factory settings using current application keys
/// </summary>
@ -534,21 +544,22 @@ retry:
};
await ChangeFileSettings(fileSettings: settings);
await SetRandomUID();
var setupVersion = 1; // Match boltcard app creator
if (newKeys.EncryptionKey != oldKeys.EncryptionKey)
await ChangeKey(1, newKeys.EncryptionKey, oldKeys.EncryptionKey);
await ChangeKey(1, newKeys.EncryptionKey, oldKeys.EncryptionKey, setupVersion);
if (newKeys.AuthenticationKey != oldKeys.AuthenticationKey)
await ChangeKey(2, newKeys.AuthenticationKey, oldKeys.AuthenticationKey);
await ChangeKey(2, newKeys.AuthenticationKey, oldKeys.AuthenticationKey, setupVersion);
if (newKeys.K3 != oldKeys.K3)
await ChangeKey(3, newKeys.K3, oldKeys.K3);
await ChangeKey(3, newKeys.K3, oldKeys.K3, setupVersion);
if (newKeys.K4 != oldKeys.K4)
await ChangeKey(4, newKeys.K4, oldKeys.K4);
await ChangeKey(4, newKeys.K4, oldKeys.K4, setupVersion);
if (newKeys.AppMasterKey != CurrentSession!.Key)
{
await ChangeKey(0, newKeys.AppMasterKey); // No need of old key for 0
await ChangeKey(0, newKeys.AppMasterKey, version: setupVersion); // No need of old key for 0
await AuthenticateEV2First(0, newKeys.AppMasterKey);
}
}

View File

@ -258,11 +258,21 @@ public class UnitTest1
K4: new AESKey("00000000000000000000000000000005".HexToBytes()));
// await ntag.ResetCard(keys);
await ntag.SetupBoltcard("http://test.com", BoltcardKeys.Default, keys);
foreach (var i in new int[] { 0, 1, 2, 3, 4 })
{
Assert.Equal(1, await ntag.GetKeyVersion(i));
}
Logs.WriteLine((await ntag.GetKeyVersion(0)).ToString());
var uri = await ntag.TryReadNDefURI();
Assert.StartsWith("lnurlw://test.com/?p=", uri?.AbsoluteUri);
var piccData = PICCData.TryBoltcardDecryptCheck(keys.EncryptionKey, keys.AuthenticationKey, uri);
Assert.NotNull(piccData);
await ntag.ResetCard(keys);
await ntag.AuthenticateEV2First(0, AESKey.Default);
foreach (var i in new int[] { 0, 1, 2, 3, 4 })
{
Assert.Equal(0, await ntag.GetKeyVersion(i));
}
}
[Fact]