@@ -231,7 +231,7 @@ public override Task Delete(KeyEntryId identifier, KeyEntryClass keClass, bool i
231
231
232
232
if ( ! _unlocked )
233
233
{
234
- UnlockSAM ( av2cmd , GetSAMProperties ( ) . AuthenticationMode , GetSAMProperties ( ) . AuthenticateKeyEntryIdentifier , GetSAMProperties ( ) . AuthenticateKeyVersion , KeyMaterial . GetValueAsString ( Properties ? . Secret , KeyValueStringFormat . HexStringWithSpace ) ) ;
234
+ UnlockSAM ( av2cmd , GetSAMProperties ( ) . AuthenticationMode , GetSAMProperties ( ) . AuthenticateKeyEntryIdentifier , GetAuthenticationKey ( ) ) ;
235
235
_unlocked = true ;
236
236
}
237
237
@@ -432,19 +432,7 @@ public override async Task Store(IList<IChangeKeyEntry> changes)
432
432
public override Task Update ( IChangeKeyEntry change , bool ignoreIfMissing )
433
433
{
434
434
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 ( ) ;
448
436
if ( change is SAMSymmetricKeyEntry samkey )
449
437
{
450
438
var cmd = Chip ? . getCommands ( ) ;
@@ -674,27 +662,72 @@ public static LibLogicalAccess.Card.DESFireKey CreateDESFireKey(LibLogicalAccess
674
662
return key ;
675
663
}
676
664
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
+
677
695
public void ActivateMifareSAM ( LibLogicalAccess . Reader . SAMAV2ISO7816Commands av2cmd )
678
696
{
679
- ActivateMifareSAM ( av2cmd , GetSAMProperties ( ) . AuthenticateKeyEntryIdentifier , GetSAMProperties ( ) . AuthenticateKeyType , GetSAMProperties ( ) . AuthenticateKeyVersion , Properties ? . Secret ) ;
697
+ ActivateMifareSAM ( av2cmd , GetSAMProperties ( ) . AuthenticateKeyEntryIdentifier , GetAuthenticationKey ( ) ) ;
680
698
Close ( ) ;
681
699
Open ( ) ;
682
700
}
683
701
684
702
public static void ActivateMifareSAM ( LibLogicalAccess . Reader . SAMAV2ISO7816Commands av2cmd , byte keyno , LibLogicalAccess . Card . DESFireKeyType keyType , byte keyVersion , string ? keyValue )
685
703
{
686
704
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
+ {
687
710
av2cmd . lockUnlock ( key , LibLogicalAccess . Card . SAMLockUnlock . SwitchAV2Mode /* AV3 = Active Mifare SAM */ , keyno , 0 , 0 ) ;
688
711
log . Info ( "Mifare SAM features activation completed." ) ;
689
712
}
690
713
691
714
public static void UnlockSAM ( LibLogicalAccess . Reader . SAMAV2ISO7816Commands av2cmd , SAMAuthenticationMode mode , byte keyEntry , byte keyVersion , string ? keyValue )
692
715
{
693
- log . Info ( "Unlocking SAM..." ) ;
694
716
var key = new LibLogicalAccess . Card . DESFireKey ( ) ;
695
717
key . setKeyType ( LibLogicalAccess . Card . DESFireKeyType . DF_KEY_AES ) ;
696
718
key . setKeyVersion ( keyVersion ) ;
697
719
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..." ) ;
698
731
if ( mode == SAMAuthenticationMode . AuthenticateHost )
699
732
{
700
733
av2cmd . authenticateHost ( key , keyEntry ) ;
@@ -756,18 +789,7 @@ public void UpdateCounter(SAMKeyUsageCounter counter)
756
789
var cmd = Chip ? . getCommands ( ) ;
757
790
if ( cmd is LibLogicalAccess . Reader . SAMAV2ISO7816Commands av2cmd )
758
791
{
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 ( ) ;
771
793
var kucEntry = new LibLogicalAccess . Card . SAMKucEntry ( ) ;
772
794
var entry = kucEntry . getKucEntryStruct ( ) ;
773
795
@@ -804,7 +826,7 @@ public void UpdateCounter(SAMKeyUsageCounter counter)
804
826
{
805
827
if ( ! string . IsNullOrEmpty ( GetSAMProperties ( ) . Secret ) && ! _unlocked )
806
828
{
807
- UnlockSAM ( av3cmd , GetSAMProperties ( ) . AuthenticationMode , GetSAMProperties ( ) . AuthenticateKeyEntryIdentifier , GetSAMProperties ( ) . AuthenticateKeyVersion , KeyMaterial . GetValueAsString ( Properties ? . Secret , KeyValueStringFormat . HexStringWithSpace ) ) ;
829
+ UnlockSAM ( av3cmd ) ;
808
830
_unlocked = true ;
809
831
}
810
832
@@ -862,7 +884,7 @@ public void UpdateCounter(SAMKeyUsageCounter counter)
862
884
{
863
885
if ( ! string . IsNullOrEmpty ( GetSAMProperties ( ) . Secret ) && ! _unlocked )
864
886
{
865
- UnlockSAM ( av2cmd , GetSAMProperties ( ) . AuthenticationMode , GetSAMProperties ( ) . AuthenticateKeyEntryIdentifier , GetSAMProperties ( ) . AuthenticateKeyVersion , KeyMaterial . GetValueAsString ( Properties ? . Secret , KeyValueStringFormat . HexStringWithSpace ) ) ;
887
+ UnlockSAM ( av2cmd ) ;
866
888
_unlocked = true ;
867
889
}
868
890
0 commit comments