diff --git a/KeyManager.Library.KeyStore.NXP_SAM/SAMKeyStore.cs b/KeyManager.Library.KeyStore.NXP_SAM/SAMKeyStore.cs index 0d6ef21..df4b8ec 100644 --- a/KeyManager.Library.KeyStore.NXP_SAM/SAMKeyStore.cs +++ b/KeyManager.Library.KeyStore.NXP_SAM/SAMKeyStore.cs @@ -748,8 +748,33 @@ public void UpdateCounter(SAMKeyUsageCounter counter) public override Task ResolveKeyEntryLink(KeyEntryId keyIdentifier, KeyEntryClass keClass, string? divInput, WrappingKey? wrappingKey) { - // Will be supported with SAM AV3 - throw new NotSupportedException(); + log.Info(string.Format("Resolving key entry link with Key Entry Identifier `{0}` and Wrapping Key Entry Identifier `{1}`...", keyIdentifier, wrappingKey?.KeyId)); + if (wrappingKey == null || !wrappingKey.KeyId.IsConfigured()) + { + log.Error("Wrapping Key Entry Identifier parameter is expected."); + throw new KeyStoreException("Wrapping Key Entry Identifier parameter is expected."); + } + + var cmd = Chip?.getCommands(); + if (cmd is LibLogicalAccess.Reader.SAMAV3ISO7816Commands av3cmd) + { + if (!string.IsNullOrEmpty(GetSAMProperties().Secret) && !_unlocked) + { + UnlockSAM(av3cmd, GetSAMProperties().AuthenticateKeyEntryIdentifier, GetSAMProperties().AuthenticateKeyVersion, KeyMaterial.GetValueAsString(Properties?.Secret, KeyValueStringFormat.HexStringWithSpace)); + _unlocked = true; + } + + byte entry = byte.Parse(keyIdentifier.Id!); + + var keyCipheredVector = av3cmd.encipherKeyEntry(entry, entry, wrappingKey.ChangeCounter ?? 0); + log.Info("Key link completed."); + return Task.FromResult(Convert.ToHexString(keyCipheredVector.ToArray())); + } + else + { + log.Error("Inserted SAM is not AV3."); + throw new KeyStoreException("Inserted SAM is not in AV3."); + } } public override async Task ResolveKeyLink(KeyEntryId keyIdentifier, KeyEntryClass keClass, string? containerSelector, string? divInput) diff --git a/KeyManager.Library/KeyStore/WrappingKey.cs b/KeyManager.Library/KeyStore/WrappingKey.cs index 78b5989..6ec1ba0 100644 --- a/KeyManager.Library/KeyStore/WrappingKey.cs +++ b/KeyManager.Library/KeyStore/WrappingKey.cs @@ -23,5 +23,12 @@ public string? ContainerSelector get => _containerSelector; set => SetProperty(ref _containerSelector, value); } + + public ushort? _changeCounter; + public ushort? ChangeCounter + { + get => _changeCounter; + set => SetProperty(ref _changeCounter, value); + } } }