Skip to content

Commit ea74872

Browse files
committed
Add Div Input configuration on NXP SAM authentication key
1 parent 80a836e commit ea74872

File tree

6 files changed

+72
-33
lines changed

6 files changed

+72
-33
lines changed

KeyManager.Library.KeyStore.NXP_SAM.UI/Properties/Resources.fr.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@
199199
<value>Désactiver la génération du MAC à partir d'une carte PICC</value>
200200
</data>
201201
<data name="DisableKeyEntry" xml:space="preserve">
202-
<value>Désactiver l'enregistrement de clés</value>
202+
<value>Désactiver la clé</value>
203203
</data>
204204
<data name="DisableVerifyMACFromPICC" xml:space="preserve">
205205
<value>Désactiver la vérification du MAC à partir d'une carte PICC</value>

KeyManager.Library.KeyStore.NXP_SAM.UI/SAMKeyStorePropertiesControl.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
</DataTemplate>
7272
</ComboBox.ItemTemplate>
7373
</ComboBox>
74+
<libui:DivInputControl Grid.Row="3" DivInput="{Binding SAMProperties.AuthenticationDivInput}" Margin="5, 15, 5, 5" />
7475
</StackPanel>
7576
</Expander>
7677
<ComboBox ItemsSource="{Binding CardTypes}" SelectedItem="{Binding SAMProperties.ForceCardType}"

KeyManager.Library.KeyStore.NXP_SAM/SAMKeyStore.cs

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public override Task Delete(KeyEntryId identifier, KeyEntryClass keClass, bool i
231231

232232
if (!_unlocked)
233233
{
234-
UnlockSAM(av2cmd, GetSAMProperties().AuthenticationMode, GetSAMProperties().AuthenticateKeyEntryIdentifier, GetSAMProperties().AuthenticateKeyVersion, KeyMaterial.GetValueAsString(Properties?.Secret, KeyValueStringFormat.HexStringWithSpace));
234+
UnlockSAM(av2cmd, GetSAMProperties().AuthenticationMode, GetSAMProperties().AuthenticateKeyEntryIdentifier, GetAuthenticationKey());
235235
_unlocked = true;
236236
}
237237

@@ -432,19 +432,7 @@ public override async Task Store(IList<IChangeKeyEntry> changes)
432432
public override Task Update(IChangeKeyEntry change, bool ignoreIfMissing)
433433
{
434434
log.Info(string.Format("Updating key entry `{0}`...", change.Identifier));
435-
436-
var key = new LibLogicalAccess.Card.DESFireKey();
437-
key.setKeyType(LibLogicalAccess.Card.DESFireKeyType.DF_KEY_AES);
438-
key.setKeyVersion(GetSAMProperties().AuthenticateKeyVersion);
439-
if (!string.IsNullOrEmpty(Properties?.Secret))
440-
{
441-
key.fromString(KeyMaterial.GetValueAsString(Properties.Secret, KeyValueStringFormat.HexStringWithSpace));
442-
}
443-
else
444-
{
445-
key.fromString("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00");
446-
}
447-
435+
var key = GetAuthenticationKey();
448436
if (change is SAMSymmetricKeyEntry samkey)
449437
{
450438
var cmd = Chip?.getCommands();
@@ -674,27 +662,72 @@ public static LibLogicalAccess.Card.DESFireKey CreateDESFireKey(LibLogicalAccess
674662
return key;
675663
}
676664

665+
public LibLogicalAccess.Card.DESFireKey GetAuthenticationKey()
666+
{
667+
var key = new LibLogicalAccess.Card.DESFireKey();
668+
key.setKeyType(GetSAMProperties().AuthenticateKeyType);
669+
key.setKeyVersion(GetSAMProperties().AuthenticateKeyVersion);
670+
if (!string.IsNullOrEmpty(Properties?.Secret))
671+
{
672+
key.fromString(KeyMaterial.GetValueAsString(Properties.Secret, KeyValueStringFormat.HexStringWithSpace));
673+
}
674+
else
675+
{
676+
key.fromString("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00");
677+
}
678+
if (GetSAMProperties().AuthenticationDivInput.Count > 0)
679+
{
680+
var divContext = new DivInput.DivInputContext
681+
{
682+
KeyStore = this
683+
};
684+
var div = new LibLogicalAccess.Card.NXPAV2KeyDiversification();
685+
var input = ComputeDivInput(divContext, GetSAMProperties().AuthenticationDivInput);
686+
if (!string.IsNullOrEmpty(input))
687+
{
688+
div.setDivInput([.. Convert.FromHexString(input)]);
689+
key.setKeyDiversification(div);
690+
}
691+
}
692+
return key;
693+
}
694+
677695
public void ActivateMifareSAM(LibLogicalAccess.Reader.SAMAV2ISO7816Commands av2cmd)
678696
{
679-
ActivateMifareSAM(av2cmd, GetSAMProperties().AuthenticateKeyEntryIdentifier, GetSAMProperties().AuthenticateKeyType, GetSAMProperties().AuthenticateKeyVersion, Properties?.Secret);
697+
ActivateMifareSAM(av2cmd, GetSAMProperties().AuthenticateKeyEntryIdentifier, GetAuthenticationKey());
680698
Close();
681699
Open();
682700
}
683701

684702
public static void ActivateMifareSAM(LibLogicalAccess.Reader.SAMAV2ISO7816Commands av2cmd, byte keyno, LibLogicalAccess.Card.DESFireKeyType keyType, byte keyVersion, string? keyValue)
685703
{
686704
var key = CreateDESFireKey(keyType, keyVersion, keyValue);
705+
ActivateMifareSAM(av2cmd, keyno, key);
706+
}
707+
708+
public static void ActivateMifareSAM(LibLogicalAccess.Reader.SAMAV2ISO7816Commands av2cmd, byte keyno, LibLogicalAccess.Card.DESFireKey key)
709+
{
687710
av2cmd.lockUnlock(key, LibLogicalAccess.Card.SAMLockUnlock.SwitchAV2Mode /* AV3 = Active Mifare SAM */, keyno, 0, 0);
688711
log.Info("Mifare SAM features activation completed.");
689712
}
690713

691714
public static void UnlockSAM(LibLogicalAccess.Reader.SAMAV2ISO7816Commands av2cmd, SAMAuthenticationMode mode, byte keyEntry, byte keyVersion, string? keyValue)
692715
{
693-
log.Info("Unlocking SAM...");
694716
var key = new LibLogicalAccess.Card.DESFireKey();
695717
key.setKeyType(LibLogicalAccess.Card.DESFireKeyType.DF_KEY_AES);
696718
key.setKeyVersion(keyVersion);
697719
key.fromString(keyValue ?? "");
720+
UnlockSAM(av2cmd, mode, keyEntry, key);
721+
}
722+
723+
public void UnlockSAM(LibLogicalAccess.Reader.SAMAV2ISO7816Commands av2cmd)
724+
{
725+
UnlockSAM(av2cmd, GetSAMProperties().AuthenticationMode, GetSAMProperties().AuthenticateKeyEntryIdentifier, GetAuthenticationKey());
726+
}
727+
728+
public static void UnlockSAM(LibLogicalAccess.Reader.SAMAV2ISO7816Commands av2cmd, SAMAuthenticationMode mode, byte keyEntry, LibLogicalAccess.Card.DESFireKey key)
729+
{
730+
log.Info("Unlocking SAM...");
698731
if (mode == SAMAuthenticationMode.AuthenticateHost)
699732
{
700733
av2cmd.authenticateHost(key, keyEntry);
@@ -756,18 +789,7 @@ public void UpdateCounter(SAMKeyUsageCounter counter)
756789
var cmd = Chip?.getCommands();
757790
if (cmd is LibLogicalAccess.Reader.SAMAV2ISO7816Commands av2cmd)
758791
{
759-
var key = new LibLogicalAccess.Card.DESFireKey();
760-
key.setKeyType(LibLogicalAccess.Card.DESFireKeyType.DF_KEY_AES);
761-
key.setKeyVersion(GetSAMProperties().AuthenticateKeyVersion);
762-
if (!string.IsNullOrEmpty(Properties?.Secret))
763-
{
764-
key.fromString(KeyMaterial.GetValueAsString(Properties.Secret, KeyValueStringFormat.HexStringWithSpace));
765-
}
766-
else
767-
{
768-
key.fromString("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00");
769-
}
770-
792+
var key = GetAuthenticationKey();
771793
var kucEntry = new LibLogicalAccess.Card.SAMKucEntry();
772794
var entry = kucEntry.getKucEntryStruct();
773795

@@ -804,7 +826,7 @@ public void UpdateCounter(SAMKeyUsageCounter counter)
804826
{
805827
if (!string.IsNullOrEmpty(GetSAMProperties().Secret) && !_unlocked)
806828
{
807-
UnlockSAM(av3cmd, GetSAMProperties().AuthenticationMode, GetSAMProperties().AuthenticateKeyEntryIdentifier, GetSAMProperties().AuthenticateKeyVersion, KeyMaterial.GetValueAsString(Properties?.Secret, KeyValueStringFormat.HexStringWithSpace));
829+
UnlockSAM(av3cmd);
808830
_unlocked = true;
809831
}
810832

@@ -862,7 +884,7 @@ public void UpdateCounter(SAMKeyUsageCounter counter)
862884
{
863885
if (!string.IsNullOrEmpty(GetSAMProperties().Secret) && !_unlocked)
864886
{
865-
UnlockSAM(av2cmd, GetSAMProperties().AuthenticationMode, GetSAMProperties().AuthenticateKeyEntryIdentifier, GetSAMProperties().AuthenticateKeyVersion, KeyMaterial.GetValueAsString(Properties?.Secret, KeyValueStringFormat.HexStringWithSpace));
887+
UnlockSAM(av2cmd);
866888
_unlocked = true;
867889
}
868890

KeyManager.Library.KeyStore.NXP_SAM/SAMKeyStoreProperties.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using LibLogicalAccess.Card;
1+
using Leosac.KeyManager.Library.DivInput;
2+
using LibLogicalAccess.Card;
3+
using System.Collections.ObjectModel;
24

35
namespace Leosac.KeyManager.Library.KeyStore.NXP_SAM
46
{
@@ -13,6 +15,7 @@ public SAMKeyStoreProperties()
1315
_authenticateKeyType = DESFireKeyType.DF_KEY_AES;
1416
_authenticateKeyVersion = 0;
1517
_authenticationMode = SAMAuthenticationMode.Unlock;
18+
AuthenticationDivInput = new ObservableCollection<DivInputFragment>();
1619
}
1720

1821
private string _readerProvider;
@@ -71,6 +74,8 @@ public SAMAuthenticationMode AuthenticationMode
7174
set => SetProperty(ref _authenticationMode, value);
7275
}
7376

77+
public ObservableCollection<DivInputFragment> AuthenticationDivInput { get; set; }
78+
7479
private string? _forceCardType;
7580
public string? ForceCardType
7681
{

KeyManager.Library.UI/Domain/KeyEntriesControlViewModel.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,17 @@ private async Task PrintSelection()
706706
flow.Blocks.Add(sign);
707707

708708
var footer = new Section() { Background = Brushes.Orange };
709+
footer.Blocks.Add(new Paragraph()
710+
{
711+
Inlines =
712+
{
713+
new Run(Properties.Resources.KeyExportDisclaimer1),
714+
new LineBreak(),
715+
new Run(Properties.Resources.KeyExportDisclaimer2)
716+
},
717+
TextAlignment = TextAlignment.Center,
718+
FontSize = 12
719+
});
709720
var fp = new Paragraph(new Run(Properties.Resources.DocumentGenerated)) { Margin = new Thickness(5) };
710721
fp.Inlines.Add(new Run(" Leosac Key Manager ") { FontWeight = FontWeights.Bold });
711722
fp.Inlines.Add(new Run(" - "));

KeyManager.Library/KeyStore/KeyStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ public virtual Task Diff(KeyStore store, Func<string, KeyStore?> getFavoriteKeyS
600600
}));
601601
}
602602

603-
private static string? ComputeDivInput(DivInputContext divContext, IList<DivInputFragment> divInput)
603+
protected static string? ComputeDivInput(DivInputContext divContext, IList<DivInputFragment> divInput)
604604
{
605605
divContext.CurrentDivInput = null;
606606
if (divInput != null && divInput.Count > 0)

0 commit comments

Comments
 (0)