@@ -24,6 +24,13 @@ namespace Yubico.YubiKey.Piv
24
24
// operations.
25
25
public sealed partial class PivSession : IDisposable
26
26
{
27
+ private static readonly Memory < byte > DefaultManagementKey = new byte [ ]
28
+ {
29
+ 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 ,
30
+ 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 ,
31
+ 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08
32
+ } ;
33
+
27
34
/// <summary>
28
35
/// This specifies the algorithm of the management key.
29
36
/// </summary>
@@ -71,12 +78,6 @@ public sealed partial class PivSession : IDisposable
71
78
/// </remarks>
72
79
public AuthenticateManagementKeyResult ManagementKeyAuthenticationResult { get ; private set ; }
73
80
74
- private PivAlgorithm DefaultManagementKeyAlgorithm =>
75
- YubiKey . HasFeature ( YubiKeyFeature . PivAesManagementKey ) &&
76
- YubiKey . FirmwareVersion >= FirmwareVersion . V5_7_0
77
- ? PivAlgorithm . Aes192
78
- : PivAlgorithm . TripleDes ;
79
-
80
81
/// <summary>
81
82
/// Try to authenticate the management key.
82
83
/// </summary>
@@ -278,7 +279,7 @@ private bool TryAuthenticateWithKeyCollector(bool mutualAuthentication)
278
279
{
279
280
keyEntryData . Clear ( ) ;
280
281
281
- if ( ! ( KeyCollector is null ) )
282
+ if ( KeyCollector is not null )
282
283
{
283
284
keyEntryData . Request = KeyEntryRequest . Release ;
284
285
_ = KeyCollector ( keyEntryData ) ;
@@ -656,7 +657,8 @@ public bool TryChangeManagementKey(PivTouchPolicy touchPolicy = PivTouchPolicy.D
656
657
/// </exception>
657
658
public bool TryChangeManagementKey ( PivTouchPolicy touchPolicy , PivAlgorithm newKeyAlgorithm )
658
659
{
659
- Logger . LogInformation ( "Try to change the management key, touch policy = {TouchPolicy}, algorithm = {PivALgorithm}." ,
660
+ Logger . LogInformation (
661
+ "Try to change the management key, touch policy = {TouchPolicy}, algorithm = {PivALgorithm}." ,
660
662
touchPolicy . ToString ( ) , newKeyAlgorithm . ToString ( ) ) ;
661
663
662
664
CheckManagementKeyAlgorithm ( newKeyAlgorithm , true ) ;
@@ -668,7 +670,7 @@ public bool TryChangeManagementKey(PivTouchPolicy touchPolicy, PivAlgorithm newK
668
670
669
671
try
670
672
{
671
- if ( TryAuthenticateWithKeyCollector ( true , keyEntryData ) == false )
673
+ if ( ! TryAuthenticateWithKeyCollector ( true , keyEntryData ) )
672
674
{
673
675
return false ;
674
676
}
@@ -691,10 +693,9 @@ public bool TryChangeManagementKey(PivTouchPolicy touchPolicy, PivAlgorithm newK
691
693
finally
692
694
{
693
695
keyEntryData . Clear ( ) ;
694
-
695
696
keyEntryData . Request = KeyEntryRequest . Release ;
696
697
697
- if ( ! ( KeyCollector is null ) )
698
+ if ( KeyCollector is not null )
698
699
{
699
700
_ = KeyCollector ( keyEntryData ) ;
700
701
}
@@ -766,10 +767,11 @@ public void ChangeManagementKey(PivTouchPolicy touchPolicy = PivTouchPolicy.Defa
766
767
/// </exception>
767
768
public void ChangeManagementKey ( PivTouchPolicy touchPolicy , PivAlgorithm newKeyAlgorithm )
768
769
{
769
- Logger . LogInformation ( "Change the management key, touch policy = {TouchPolicy}, algorithm = {PivAlgorithm}." ,
770
+ Logger . LogInformation (
771
+ "Change the management key, touch policy = {TouchPolicy}, algorithm = {PivAlgorithm}." ,
770
772
touchPolicy . ToString ( ) , newKeyAlgorithm . ToString ( ) ) ;
771
773
772
- if ( TryChangeManagementKey ( touchPolicy , newKeyAlgorithm ) == false )
774
+ if ( ! TryChangeManagementKey ( touchPolicy , newKeyAlgorithm ) )
773
775
{
774
776
throw new OperationCanceledException (
775
777
string . Format (
@@ -828,9 +830,10 @@ public void ChangeManagementKey(PivTouchPolicy touchPolicy, PivAlgorithm newKeyA
828
830
/// Mutual authentication was performed and the YubiKey was not
829
831
/// authenticated.
830
832
/// </exception>
831
- public bool TryChangeManagementKey ( ReadOnlyMemory < byte > currentKey ,
832
- ReadOnlyMemory < byte > newKey ,
833
- PivTouchPolicy touchPolicy = PivTouchPolicy . Default ) =>
833
+ public bool TryChangeManagementKey (
834
+ ReadOnlyMemory < byte > currentKey ,
835
+ ReadOnlyMemory < byte > newKey ,
836
+ PivTouchPolicy touchPolicy = PivTouchPolicy . Default ) =>
834
837
TryChangeManagementKey ( currentKey , newKey , touchPolicy , DefaultManagementKeyAlgorithm ) ;
835
838
836
839
/// <summary>
@@ -890,10 +893,11 @@ public bool TryChangeManagementKey(ReadOnlyMemory<byte> currentKey,
890
893
/// Mutual authentication was performed and the YubiKey was not
891
894
/// authenticated.
892
895
/// </exception>
893
- public bool TryChangeManagementKey ( ReadOnlyMemory < byte > currentKey ,
894
- ReadOnlyMemory < byte > newKey ,
895
- PivTouchPolicy touchPolicy ,
896
- PivAlgorithm newKeyAlgorithm )
896
+ public bool TryChangeManagementKey (
897
+ ReadOnlyMemory < byte > currentKey ,
898
+ ReadOnlyMemory < byte > newKey ,
899
+ PivTouchPolicy touchPolicy ,
900
+ PivAlgorithm newKeyAlgorithm )
897
901
{
898
902
CheckManagementKeyAlgorithm ( newKeyAlgorithm , true ) ;
899
903
@@ -902,10 +906,11 @@ public bool TryChangeManagementKey(ReadOnlyMemory<byte> currentKey,
902
906
903
907
// Try to change the management key, even if the YubiKey is set to
904
908
// PIN-derived.
905
- private bool TryForcedChangeManagementKey ( ReadOnlyMemory < byte > currentKey ,
906
- ReadOnlyMemory < byte > newKey ,
907
- PivTouchPolicy touchPolicy ,
908
- PivAlgorithm newKeyAlgorithm )
909
+ private bool TryForcedChangeManagementKey (
910
+ ReadOnlyMemory < byte > currentKey ,
911
+ ReadOnlyMemory < byte > newKey ,
912
+ PivTouchPolicy touchPolicy ,
913
+ PivAlgorithm newKeyAlgorithm )
909
914
{
910
915
if ( TryAuthenticateManagementKey ( currentKey , true ) )
911
916
{
@@ -974,9 +979,10 @@ private bool TryAuthenticateWithKeyCollector(bool mutualAuthentication, KeyEntry
974
979
// if the auth succeeds.
975
980
// If auth works, return true, otherwise, return false.
976
981
// Throw an exception if the YubiKey fails to auth.
977
- private bool TryAuthenticateManagementKey ( bool mutualAuthentication ,
978
- ReadOnlySpan < byte > mgmtKey ,
979
- PivAlgorithm algorithm )
982
+ private bool TryAuthenticateManagementKey (
983
+ bool mutualAuthentication ,
984
+ ReadOnlySpan < byte > mgmtKey ,
985
+ PivAlgorithm algorithm )
980
986
{
981
987
var initCommand = new InitializeAuthenticateManagementKeyCommand ( mutualAuthentication , algorithm ) ;
982
988
var initResponse = Connection . SendCommand ( initCommand ) ;
@@ -1058,26 +1064,27 @@ private void CheckManagementKeyAlgorithm(PivAlgorithm algorithm, bool checkMode)
1058
1064
}
1059
1065
}
1060
1066
1061
- bool isValid = IsValid ( algorithm ) ;
1062
- if ( ! isValid )
1067
+ if ( ! IsValidManagementKeyAlgorithm ( algorithm ) )
1063
1068
{
1064
1069
throw new ArgumentException (
1065
1070
string . Format (
1066
1071
CultureInfo . CurrentCulture ,
1067
1072
ExceptionMessages . UnsupportedAlgorithm ) ) ;
1068
1073
}
1069
-
1070
- return ;
1071
-
1072
- bool IsValid ( PivAlgorithm pa ) =>
1073
- pa switch
1074
- {
1075
- PivAlgorithm . TripleDes => true , // Default for keys below fw version 5.7
1076
- PivAlgorithm . Aes128 => YubiKey . HasFeature ( YubiKeyFeature . PivAesManagementKey ) ,
1077
- PivAlgorithm . Aes192 => YubiKey . HasFeature ( YubiKeyFeature . PivAesManagementKey ) ,
1078
- PivAlgorithm . Aes256 => YubiKey . HasFeature ( YubiKeyFeature . PivAesManagementKey ) ,
1079
- _ => false
1080
- } ;
1081
1074
}
1075
+
1076
+ private bool IsValidManagementKeyAlgorithm ( PivAlgorithm pivAlgorithm ) =>
1077
+ pivAlgorithm switch
1078
+ {
1079
+ PivAlgorithm . TripleDes => true , // Default for keys below fw version 5.7
1080
+ PivAlgorithm . Aes128 or PivAlgorithm . Aes192 or PivAlgorithm . Aes256 => YubiKey . HasFeature ( YubiKeyFeature . PivAesManagementKey ) ,
1081
+ _ => false
1082
+ } ;
1083
+
1084
+ private PivAlgorithm DefaultManagementKeyAlgorithm =>
1085
+ YubiKey . HasFeature ( YubiKeyFeature . PivAesManagementKey ) &&
1086
+ YubiKey . FirmwareVersion >= FirmwareVersion . V5_7_0
1087
+ ? PivAlgorithm . Aes192
1088
+ : PivAlgorithm . TripleDes ;
1082
1089
}
1083
1090
}
0 commit comments