Skip to content

Commit 9a73ff3

Browse files
Refactor remove nested if-statements
1 parent 4787202 commit 9a73ff3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+387
-503
lines changed

Yubico.YubiKey/examples/Fido2SampleCode/Run/Fido2SampleRun.Operations.cs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -364,25 +364,22 @@ public bool RunGetAssertions()
364364

365365
var salt = ReadOnlyMemory<byte>.Empty;
366366
bool isValid = Fido2Protocol.RunGetAuthenticatorInfo(_yubiKeyChosen, out var authenticatorInfo);
367-
if (isValid)
367+
if (isValid && authenticatorInfo.Extensions.Contains("hmac-secret"))
368368
{
369-
if (authenticatorInfo.Extensions.Contains("hmac-secret"))
370-
{
371-
SampleMenu.WriteMessage(
372-
MessageType.Title, 0,
373-
"\nWould you like the hmac-secret returned with the assertions?\n" +
374-
"If not, type Enter.\n" +
375-
"Otherwise, enter a string that will be used to derive a salt.\n" +
376-
"Normally, a salt is 32 random bytes or the digest of some identifying data.\n" +
377-
"This sample code will perform SHA-256 on the input you provide and send that\n" +
378-
"digest to the YubiKey as the salt.\n");
379-
_ = SampleMenu.ReadResponse(out string dataToDigest);
380-
byte[] dataBytes = System.Text.Encoding.Unicode.GetBytes(dataToDigest);
381-
var digester = CryptographyProviders.Sha256Creator();
382-
_ = digester.TransformFinalBlock(dataBytes, 0, dataBytes.Length);
383-
384-
salt = new ReadOnlyMemory<byte>(digester.Hash);
385-
}
369+
SampleMenu.WriteMessage(
370+
MessageType.Title, 0,
371+
"\nWould you like the hmac-secret returned with the assertions?\n" +
372+
"If not, type Enter.\n" +
373+
"Otherwise, enter a string that will be used to derive a salt.\n" +
374+
"Normally, a salt is 32 random bytes or the digest of some identifying data.\n" +
375+
"This sample code will perform SHA-256 on the input you provide and send that\n" +
376+
"digest to the YubiKey as the salt.\n");
377+
_ = SampleMenu.ReadResponse(out string dataToDigest);
378+
byte[] dataBytes = System.Text.Encoding.Unicode.GetBytes(dataToDigest);
379+
var digester = CryptographyProviders.Sha256Creator();
380+
_ = digester.TransformFinalBlock(dataBytes, 0, dataBytes.Length);
381+
382+
salt = new ReadOnlyMemory<byte>(digester.Hash);
386383
}
387384

388385
_keyCollector.Operation = Fido2KeyCollectorOperation.GetAssertion;

Yubico.YubiKey/examples/Fido2SampleCode/Run/Fido2SampleRun.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,9 @@ public void RunSample(bool displayGuiMessage = true)
8484
// inserted. If so, keep using it. If not, find another default.
8585
// does not require a chosen YubiKey, this method will do nothing
8686
// and return true.
87-
if (DefaultChooseYubiKey(menuItem))
87+
if (DefaultChooseYubiKey(menuItem) && !RunMenuItem(menuItem))
8888
{
89-
if (!RunMenuItem(menuItem))
90-
{
91-
menuItem = Fido2MainMenuItem.Exit;
92-
}
89+
menuItem = Fido2MainMenuItem.Exit;
9390
}
9491

9592
} while (menuItem != Fido2MainMenuItem.Exit);

Yubico.YubiKey/examples/PivSampleCode/Converters/DsaSignatureConverter.cs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,33 +83,29 @@ public static byte[] GetNonStandardDsaFromStandard(byte[] signature, KeyType alg
8383
int offsetR = 0;
8484
int offsetS = 0;
8585
bool isValid = false;
86-
if (tlvReader.TryReadNestedTlv(out var seqReader, 0x30))
86+
if (tlvReader.TryReadNestedTlv(out var seqReader, 0x30) &&
87+
seqReader.TryReadValue(out rValue, 0x02) &&
88+
seqReader.TryReadValue(out sValue, 0x02))
8789
{
88-
if (seqReader.TryReadValue(out rValue, 0x02))
90+
// Skip any leading 00 bytes.
91+
while (rValue.Span[offsetR] == 0)
8992
{
90-
if (seqReader.TryReadValue(out sValue, 0x02))
93+
offsetR++;
94+
if (offsetR == rValue.Length - 1)
9195
{
92-
// Skip any leading 00 bytes.
93-
while (rValue.Span[offsetR] == 0)
94-
{
95-
offsetR++;
96-
if (offsetR == rValue.Length - 1)
97-
{
98-
break;
99-
}
100-
}
101-
while (sValue.Span[offsetS] == 0)
102-
{
103-
offsetS++;
104-
if (offsetS == sValue.Length - 1)
105-
{
106-
break;
107-
}
108-
}
109-
110-
isValid = rValue.Length - offsetR <= elementLength && sValue.Length - offsetS <= elementLength;
96+
break;
11197
}
11298
}
99+
while (sValue.Span[offsetS] == 0)
100+
{
101+
offsetS++;
102+
if (offsetS == sValue.Length - 1)
103+
{
104+
break;
105+
}
106+
}
107+
108+
isValid = rValue.Length - offsetR <= elementLength && sValue.Length - offsetS <= elementLength;
113109
}
114110

115111
if (isValid)

Yubico.YubiKey/examples/PivSampleCode/Converters/PemOperations.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,10 @@ private static bool VerifyPemHeaderAndFooter(char[] pemKeyString, string title)
185185
char[] targetStart = (Part1 + title + Part2And4).ToCharArray();
186186
char[] targetEnd = (Part3 + title + Part2And4).ToCharArray();
187187
bool returnValue = false;
188-
if (pemKeyString.Length > targetStart.Length + targetEnd.Length)
188+
if (pemKeyString.Length > targetStart.Length + targetEnd.Length &&
189+
CompareToTarget(pemKeyString, 0, targetStart))
189190
{
190-
if (CompareToTarget(pemKeyString, 0, targetStart))
191-
{
192-
returnValue = CompareToTarget(pemKeyString, pemKeyString.Length - targetEnd.Length, targetEnd);
193-
}
191+
returnValue = CompareToTarget(pemKeyString, pemKeyString.Length - targetEnd.Length, targetEnd);
194192
}
195193

196194
return returnValue;

Yubico.YubiKey/examples/PivSampleCode/Converters/SignatureAlgIdConverter.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -262,25 +262,19 @@ private bool SetFromOid(ReadOnlyMemory<byte> oid)
262262
// Then verify the len(y) is supported.
263263
private void ReadPssParams(ReadOnlyMemory<byte> algIdParams)
264264
{
265-
if (algIdParams.Length == 2)
265+
if (algIdParams.Length == 2 && algIdParams.Span[0] == 0x30 && algIdParams.Span[1] == 0)
266266
{
267-
if (algIdParams.Span[0] == 0x30 && algIdParams.Span[1] == 0)
268-
{
269-
PssSaltLength = 20;
270-
}
267+
PssSaltLength = 20;
271268
}
272-
else if (algIdParams.Length == 50)
269+
else if (algIdParams.Length == 50 && algIdParams.Span[16] == algIdParams.Span[44])
273270
{
274-
if (algIdParams.Span[16] == algIdParams.Span[44])
271+
PssSaltLength = algIdParams.Span[16] switch
275272
{
276-
PssSaltLength = algIdParams.Span[16] switch
277-
{
278-
1 => 32,
279-
2 => 48,
280-
3 => 64,
281-
_ => 0,
282-
};
283-
}
273+
1 => 32,
274+
2 => 48,
275+
3 => 64,
276+
_ => 0,
277+
};
284278
}
285279

286280
switch (PssSaltLength)

Yubico.YubiKey/examples/PivSampleCode/Run/PivSampleRun.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,9 @@ public void RunSample()
8282
// inserted. If so, keep using it. If not, find another default.
8383
// does not require a chosen YubiKey, this method will do nothing
8484
// and return true.
85-
if (DefaultChooseYubiKey(menuItem))
85+
if (DefaultChooseYubiKey(menuItem) && !RunMenuItem(menuItem))
8686
{
87-
if (!RunMenuItem(menuItem))
88-
{
89-
menuItem = PivMainMenuItem.Exit;
90-
}
87+
menuItem = PivMainMenuItem.Exit;
9188
}
9289

9390
} while (menuItem != PivMainMenuItem.Exit);

Yubico.YubiKey/examples/U2fSampleCode/Run/U2fSampleRun.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,9 @@ public void RunSample()
7070
// inserted. If so, keep using it. If not, find another default.
7171
// does not require a chosen YubiKey, this method will do nothing
7272
// and return true.
73-
if (DefaultChooseYubiKey(menuItem))
73+
if (DefaultChooseYubiKey(menuItem) && !RunMenuItem(menuItem))
7474
{
75-
if (!RunMenuItem(menuItem))
76-
{
77-
menuItem = U2fMainMenuItem.Exit;
78-
}
75+
menuItem = U2fMainMenuItem.Exit;
7976
}
8077

8178
} while (menuItem != U2fMainMenuItem.Exit);

Yubico.YubiKey/src/Yubico/YubiKey/Fido2/Commands/EnumerateRpsBeginResponse.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,13 @@ public EnumerateRpsBeginResponse(ResponseApdu responseApdu)
7272
{
7373
var credentialManagementData = _response.GetData();
7474

75-
if (credentialManagementData.RelyingParty is not null
76-
&& credentialManagementData.RelyingPartyIdHash is not null
77-
&& credentialManagementData.TotalRelyingPartyCount is not null)
75+
if (credentialManagementData.RelyingParty is not null &&
76+
credentialManagementData.RelyingPartyIdHash is not null &&
77+
credentialManagementData.TotalRelyingPartyCount is not null &&
78+
credentialManagementData.RelyingParty.IsMatchingRelyingPartyId(credentialManagementData.RelyingPartyIdHash.Value))
7879
{
79-
if (credentialManagementData.RelyingParty.IsMatchingRelyingPartyId(credentialManagementData.RelyingPartyIdHash.Value))
80-
{
81-
return (credentialManagementData.TotalRelyingPartyCount.Value, credentialManagementData.RelyingParty);
82-
}
83-
}
80+
return (credentialManagementData.TotalRelyingPartyCount.Value, credentialManagementData.RelyingParty);
81+
}
8482

8583
throw new Ctap2DataException(ExceptionMessages.InvalidFido2Info);
8684
}

Yubico.YubiKey/src/Yubico/YubiKey/Fido2/Commands/EnumerateRpsGetNextResponse.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,11 @@ public EnumerateRpsGetNextResponse(ResponseApdu responseApdu)
4343
public RelyingParty GetData()
4444
{
4545
var credentialManagementData = _response.GetData();
46-
if (!(credentialManagementData.RelyingParty is null) &&
47-
!(credentialManagementData.RelyingPartyIdHash is null))
46+
if (credentialManagementData.RelyingParty is not null &&
47+
credentialManagementData.RelyingPartyIdHash is not null &&
48+
credentialManagementData.RelyingParty.IsMatchingRelyingPartyId(credentialManagementData.RelyingPartyIdHash.Value))
4849
{
49-
if (credentialManagementData.RelyingParty.IsMatchingRelyingPartyId(credentialManagementData.RelyingPartyIdHash.Value))
50-
{
51-
return credentialManagementData.RelyingParty;
52-
}
50+
return credentialManagementData.RelyingParty;
5351
}
5452

5553
throw new Ctap2DataException(ExceptionMessages.InvalidFido2Info);

Yubico.YubiKey/src/Yubico/YubiKey/Management/Commands/SetDeviceInfoBaseCommand.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,9 @@ public int? AutoEjectTimeout
6767

6868
set
6969
{
70-
if (value.HasValue)
70+
if (value.HasValue && (value < ushort.MinValue || value > ushort.MaxValue))
7171
{
72-
if (value < ushort.MinValue || value > ushort.MaxValue)
73-
{
74-
throw new ArgumentOutOfRangeException(nameof(value));
75-
}
72+
throw new ArgumentOutOfRangeException(nameof(value));
7673
}
7774

7875
_autoEjectTimeout = (ushort?)value;

0 commit comments

Comments
 (0)