Skip to content

Commit

Permalink
Use SAM AV3 EncipherKeyEntry command for ResolveKeyEntryLink
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxhy committed Aug 20, 2024
1 parent cf8d47a commit b821e9d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
29 changes: 27 additions & 2 deletions KeyManager.Library.KeyStore.NXP_SAM/SAMKeyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -748,8 +748,33 @@ public void UpdateCounter(SAMKeyUsageCounter counter)

public override Task<string?> 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<string?>(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<string?> ResolveKeyLink(KeyEntryId keyIdentifier, KeyEntryClass keClass, string? containerSelector, string? divInput)
Expand Down
7 changes: 7 additions & 0 deletions KeyManager.Library/KeyStore/WrappingKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit b821e9d

Please sign in to comment.