Skip to content

Commit a7b54cc

Browse files
committed
refactor: PivSession
- added field DefaultManagementKey - early exit when fail TryVerifyPin - use range operator
1 parent 96196da commit a7b54cc

File tree

3 files changed

+60
-52
lines changed

3 files changed

+60
-52
lines changed

Yubico.YubiKey/src/Yubico/YubiKey/Piv/Objects/PinProtectedData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public void SetManagementKey(ReadOnlyMemory<byte> managementKey)
142142
if (IsValidKeyLength(managementKey.Length))
143143
{
144144
managementKey.CopyTo(_mgmtKey);
145-
ManagementKey = _mgmtKey.Slice(0, managementKey.Length);
145+
ManagementKey = _mgmtKey[..managementKey.Length];
146146
return;
147147
}
148148

Yubico.YubiKey/src/Yubico/YubiKey/Piv/PivSession.ManagementKey.cs

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ namespace Yubico.YubiKey.Piv
2424
// operations.
2525
public sealed partial class PivSession : IDisposable
2626
{
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+
2734
/// <summary>
2835
/// This specifies the algorithm of the management key.
2936
/// </summary>
@@ -71,12 +78,6 @@ public sealed partial class PivSession : IDisposable
7178
/// </remarks>
7279
public AuthenticateManagementKeyResult ManagementKeyAuthenticationResult { get; private set; }
7380

74-
private PivAlgorithm DefaultManagementKeyAlgorithm =>
75-
YubiKey.HasFeature(YubiKeyFeature.PivAesManagementKey) &&
76-
YubiKey.FirmwareVersion >= FirmwareVersion.V5_7_0
77-
? PivAlgorithm.Aes192
78-
: PivAlgorithm.TripleDes;
79-
8081
/// <summary>
8182
/// Try to authenticate the management key.
8283
/// </summary>
@@ -278,7 +279,7 @@ private bool TryAuthenticateWithKeyCollector(bool mutualAuthentication)
278279
{
279280
keyEntryData.Clear();
280281

281-
if (!(KeyCollector is null))
282+
if (KeyCollector is not null)
282283
{
283284
keyEntryData.Request = KeyEntryRequest.Release;
284285
_ = KeyCollector(keyEntryData);
@@ -656,7 +657,8 @@ public bool TryChangeManagementKey(PivTouchPolicy touchPolicy = PivTouchPolicy.D
656657
/// </exception>
657658
public bool TryChangeManagementKey(PivTouchPolicy touchPolicy, PivAlgorithm newKeyAlgorithm)
658659
{
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}.",
660662
touchPolicy.ToString(), newKeyAlgorithm.ToString());
661663

662664
CheckManagementKeyAlgorithm(newKeyAlgorithm, true);
@@ -668,7 +670,7 @@ public bool TryChangeManagementKey(PivTouchPolicy touchPolicy, PivAlgorithm newK
668670

669671
try
670672
{
671-
if (TryAuthenticateWithKeyCollector(true, keyEntryData) == false)
673+
if (!TryAuthenticateWithKeyCollector(true, keyEntryData))
672674
{
673675
return false;
674676
}
@@ -691,10 +693,9 @@ public bool TryChangeManagementKey(PivTouchPolicy touchPolicy, PivAlgorithm newK
691693
finally
692694
{
693695
keyEntryData.Clear();
694-
695696
keyEntryData.Request = KeyEntryRequest.Release;
696697

697-
if (!(KeyCollector is null))
698+
if (KeyCollector is not null)
698699
{
699700
_ = KeyCollector(keyEntryData);
700701
}
@@ -766,10 +767,11 @@ public void ChangeManagementKey(PivTouchPolicy touchPolicy = PivTouchPolicy.Defa
766767
/// </exception>
767768
public void ChangeManagementKey(PivTouchPolicy touchPolicy, PivAlgorithm newKeyAlgorithm)
768769
{
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}.",
770772
touchPolicy.ToString(), newKeyAlgorithm.ToString());
771773

772-
if (TryChangeManagementKey(touchPolicy, newKeyAlgorithm) == false)
774+
if (!TryChangeManagementKey(touchPolicy, newKeyAlgorithm))
773775
{
774776
throw new OperationCanceledException(
775777
string.Format(
@@ -828,9 +830,10 @@ public void ChangeManagementKey(PivTouchPolicy touchPolicy, PivAlgorithm newKeyA
828830
/// Mutual authentication was performed and the YubiKey was not
829831
/// authenticated.
830832
/// </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) =>
834837
TryChangeManagementKey(currentKey, newKey, touchPolicy, DefaultManagementKeyAlgorithm);
835838

836839
/// <summary>
@@ -890,10 +893,11 @@ public bool TryChangeManagementKey(ReadOnlyMemory<byte> currentKey,
890893
/// Mutual authentication was performed and the YubiKey was not
891894
/// authenticated.
892895
/// </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)
897901
{
898902
CheckManagementKeyAlgorithm(newKeyAlgorithm, true);
899903

@@ -902,10 +906,11 @@ public bool TryChangeManagementKey(ReadOnlyMemory<byte> currentKey,
902906

903907
// Try to change the management key, even if the YubiKey is set to
904908
// 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)
909914
{
910915
if (TryAuthenticateManagementKey(currentKey, true))
911916
{
@@ -974,9 +979,10 @@ private bool TryAuthenticateWithKeyCollector(bool mutualAuthentication, KeyEntry
974979
// if the auth succeeds.
975980
// If auth works, return true, otherwise, return false.
976981
// 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)
980986
{
981987
var initCommand = new InitializeAuthenticateManagementKeyCommand(mutualAuthentication, algorithm);
982988
var initResponse = Connection.SendCommand(initCommand);
@@ -1058,26 +1064,27 @@ private void CheckManagementKeyAlgorithm(PivAlgorithm algorithm, bool checkMode)
10581064
}
10591065
}
10601066

1061-
bool isValid = IsValid(algorithm);
1062-
if (!isValid)
1067+
if (!IsValidManagementKeyAlgorithm(algorithm))
10631068
{
10641069
throw new ArgumentException(
10651070
string.Format(
10661071
CultureInfo.CurrentCulture,
10671072
ExceptionMessages.UnsupportedAlgorithm));
10681073
}
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-
};
10811074
}
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;
10821089
}
10831090
}

Yubico.YubiKey/src/Yubico/YubiKey/Piv/PivSession.Pin.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -288,20 +288,21 @@ public bool TryVerifyPin(ReadOnlyMemory<byte> pin, out int? retriesRemaining)
288288
var response = Connection.SendCommand(command);
289289

290290
PinVerified = response.Status == ResponseStatus.Success;
291-
if (!PinVerified)
291+
if (PinVerified)
292292
{
293-
retriesRemaining = response.GetData();
293+
return true;
294+
}
294295

295-
if ((retriesRemaining ?? 1) == 0)
296-
{
297-
throw new SecurityException(
298-
string.Format(
299-
CultureInfo.CurrentCulture,
300-
ExceptionMessages.NoMoreRetriesRemaining));
301-
}
296+
retriesRemaining = response.GetData();
297+
if ((retriesRemaining ?? 1) == 0)
298+
{
299+
throw new SecurityException(
300+
string.Format(
301+
CultureInfo.CurrentCulture,
302+
ExceptionMessages.NoMoreRetriesRemaining));
302303
}
303304

304-
return PinVerified;
305+
return false;
305306
}
306307

307308
/// <summary>

0 commit comments

Comments
 (0)