Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -364,25 +364,22 @@ public bool RunGetAssertions()

var salt = ReadOnlyMemory<byte>.Empty;
bool isValid = Fido2Protocol.RunGetAuthenticatorInfo(_yubiKeyChosen, out var authenticatorInfo);
if (isValid)
if (isValid && authenticatorInfo.Extensions.Contains("hmac-secret"))
{
if (authenticatorInfo.Extensions.Contains("hmac-secret"))
{
SampleMenu.WriteMessage(
MessageType.Title, 0,
"\nWould you like the hmac-secret returned with the assertions?\n" +
"If not, type Enter.\n" +
"Otherwise, enter a string that will be used to derive a salt.\n" +
"Normally, a salt is 32 random bytes or the digest of some identifying data.\n" +
"This sample code will perform SHA-256 on the input you provide and send that\n" +
"digest to the YubiKey as the salt.\n");
_ = SampleMenu.ReadResponse(out string dataToDigest);
byte[] dataBytes = System.Text.Encoding.Unicode.GetBytes(dataToDigest);
var digester = CryptographyProviders.Sha256Creator();
_ = digester.TransformFinalBlock(dataBytes, 0, dataBytes.Length);

salt = new ReadOnlyMemory<byte>(digester.Hash);
}
SampleMenu.WriteMessage(
MessageType.Title, 0,
"\nWould you like the hmac-secret returned with the assertions?\n" +
"If not, type Enter.\n" +
"Otherwise, enter a string that will be used to derive a salt.\n" +
"Normally, a salt is 32 random bytes or the digest of some identifying data.\n" +
"This sample code will perform SHA-256 on the input you provide and send that\n" +
"digest to the YubiKey as the salt.\n");
_ = SampleMenu.ReadResponse(out string dataToDigest);
byte[] dataBytes = System.Text.Encoding.Unicode.GetBytes(dataToDigest);
var digester = CryptographyProviders.Sha256Creator();
_ = digester.TransformFinalBlock(dataBytes, 0, dataBytes.Length);

salt = new ReadOnlyMemory<byte>(digester.Hash);
}

_keyCollector.Operation = Fido2KeyCollectorOperation.GetAssertion;
Expand Down
7 changes: 2 additions & 5 deletions Yubico.YubiKey/examples/Fido2SampleCode/Run/Fido2SampleRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,9 @@ public void RunSample(bool displayGuiMessage = true)
// inserted. If so, keep using it. If not, find another default.
// does not require a chosen YubiKey, this method will do nothing
// and return true.
if (DefaultChooseYubiKey(menuItem))
if (DefaultChooseYubiKey(menuItem) && !RunMenuItem(menuItem))
{
if (!RunMenuItem(menuItem))
{
menuItem = Fido2MainMenuItem.Exit;
}
menuItem = Fido2MainMenuItem.Exit;
}

} while (menuItem != Fido2MainMenuItem.Exit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,29 @@ public static byte[] GetNonStandardDsaFromStandard(byte[] signature, KeyType alg
int offsetR = 0;
int offsetS = 0;
bool isValid = false;
if (tlvReader.TryReadNestedTlv(out var seqReader, 0x30))
if (tlvReader.TryReadNestedTlv(out var seqReader, 0x30) &&
seqReader.TryReadValue(out rValue, 0x02) &&
seqReader.TryReadValue(out sValue, 0x02))
{
if (seqReader.TryReadValue(out rValue, 0x02))
// Skip any leading 00 bytes.
while (rValue.Span[offsetR] == 0)
{
if (seqReader.TryReadValue(out sValue, 0x02))
offsetR++;
if (offsetR == rValue.Length - 1)
{
// Skip any leading 00 bytes.
while (rValue.Span[offsetR] == 0)
{
offsetR++;
if (offsetR == rValue.Length - 1)
{
break;
}
}
while (sValue.Span[offsetS] == 0)
{
offsetS++;
if (offsetS == sValue.Length - 1)
{
break;
}
}

isValid = rValue.Length - offsetR <= elementLength && sValue.Length - offsetS <= elementLength;
break;
}
}
while (sValue.Span[offsetS] == 0)
{
offsetS++;
if (offsetS == sValue.Length - 1)
{
break;
}
}

isValid = rValue.Length - offsetR <= elementLength && sValue.Length - offsetS <= elementLength;
}

if (isValid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,10 @@ private static bool VerifyPemHeaderAndFooter(char[] pemKeyString, string title)
char[] targetStart = (Part1 + title + Part2And4).ToCharArray();
char[] targetEnd = (Part3 + title + Part2And4).ToCharArray();
bool returnValue = false;
if (pemKeyString.Length > targetStart.Length + targetEnd.Length)
if (pemKeyString.Length > targetStart.Length + targetEnd.Length &&
CompareToTarget(pemKeyString, 0, targetStart))
{
if (CompareToTarget(pemKeyString, 0, targetStart))
{
returnValue = CompareToTarget(pemKeyString, pemKeyString.Length - targetEnd.Length, targetEnd);
}
returnValue = CompareToTarget(pemKeyString, pemKeyString.Length - targetEnd.Length, targetEnd);
}

return returnValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,25 +262,19 @@ private bool SetFromOid(ReadOnlyMemory<byte> oid)
// Then verify the len(y) is supported.
private void ReadPssParams(ReadOnlyMemory<byte> algIdParams)
{
if (algIdParams.Length == 2)
if (algIdParams.Length == 2 && algIdParams.Span[0] == 0x30 && algIdParams.Span[1] == 0)
{
if (algIdParams.Span[0] == 0x30 && algIdParams.Span[1] == 0)
{
PssSaltLength = 20;
}
PssSaltLength = 20;
}
else if (algIdParams.Length == 50)
else if (algIdParams.Length == 50 && algIdParams.Span[16] == algIdParams.Span[44])
{
if (algIdParams.Span[16] == algIdParams.Span[44])
PssSaltLength = algIdParams.Span[16] switch
{
PssSaltLength = algIdParams.Span[16] switch
{
1 => 32,
2 => 48,
3 => 64,
_ => 0,
};
}
1 => 32,
2 => 48,
3 => 64,
_ => 0,
};
}

switch (PssSaltLength)
Expand Down
7 changes: 2 additions & 5 deletions Yubico.YubiKey/examples/PivSampleCode/Run/PivSampleRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,9 @@ public void RunSample()
// inserted. If so, keep using it. If not, find another default.
// does not require a chosen YubiKey, this method will do nothing
// and return true.
if (DefaultChooseYubiKey(menuItem))
if (DefaultChooseYubiKey(menuItem) && !RunMenuItem(menuItem))
{
if (!RunMenuItem(menuItem))
{
menuItem = PivMainMenuItem.Exit;
}
menuItem = PivMainMenuItem.Exit;
}

} while (menuItem != PivMainMenuItem.Exit);
Expand Down
7 changes: 2 additions & 5 deletions Yubico.YubiKey/examples/U2fSampleCode/Run/U2fSampleRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,9 @@ public void RunSample()
// inserted. If so, keep using it. If not, find another default.
// does not require a chosen YubiKey, this method will do nothing
// and return true.
if (DefaultChooseYubiKey(menuItem))
if (DefaultChooseYubiKey(menuItem) && !RunMenuItem(menuItem))
{
if (!RunMenuItem(menuItem))
{
menuItem = U2fMainMenuItem.Exit;
}
menuItem = U2fMainMenuItem.Exit;
}

} while (menuItem != U2fMainMenuItem.Exit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,12 @@ public EnumerateRpsBeginResponse(ResponseApdu responseApdu)
{
var credentialManagementData = _response.GetData();

if (credentialManagementData.RelyingParty is not null
&& credentialManagementData.RelyingPartyIdHash is not null
&& credentialManagementData.TotalRelyingPartyCount is not null)
if (credentialManagementData.RelyingParty is not null &&
credentialManagementData.RelyingPartyIdHash is not null &&
credentialManagementData.TotalRelyingPartyCount is not null &&
credentialManagementData.RelyingParty.IsMatchingRelyingPartyId(credentialManagementData.RelyingPartyIdHash.Value))
{
if (credentialManagementData.RelyingParty.IsMatchingRelyingPartyId(credentialManagementData.RelyingPartyIdHash.Value))
{
return (credentialManagementData.TotalRelyingPartyCount.Value, credentialManagementData.RelyingParty);
}
return (credentialManagementData.TotalRelyingPartyCount.Value, credentialManagementData.RelyingParty);
}

throw new Ctap2DataException(ExceptionMessages.InvalidFido2Info);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ public EnumerateRpsGetNextResponse(ResponseApdu responseApdu)
public RelyingParty GetData()
{
var credentialManagementData = _response.GetData();
if (!(credentialManagementData.RelyingParty is null) &&
!(credentialManagementData.RelyingPartyIdHash is null))

if (credentialManagementData.RelyingParty is not null &&
credentialManagementData.RelyingPartyIdHash is not null &&
credentialManagementData.RelyingParty.IsMatchingRelyingPartyId(credentialManagementData.RelyingPartyIdHash.Value))
{
if (credentialManagementData.RelyingParty.IsMatchingRelyingPartyId(credentialManagementData.RelyingPartyIdHash.Value))
{
return credentialManagementData.RelyingParty;
}
return credentialManagementData.RelyingParty;
}

throw new Ctap2DataException(ExceptionMessages.InvalidFido2Info);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,9 @@ public int? AutoEjectTimeout

set
{
if (value.HasValue)
if (value.HasValue && (value < ushort.MinValue || value > ushort.MaxValue))
{
if (value < ushort.MinValue || value > ushort.MaxValue)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
throw new ArgumentOutOfRangeException(nameof(value));
}

_autoEjectTimeout = (ushort?)value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,9 @@ public void SetPassword()
/// </returns>
public bool TrySetPassword(ReadOnlyMemory<byte> currentPassword, ReadOnlyMemory<byte> newPassword)
{
if (IsPasswordProtected || !currentPassword.IsEmpty)
if ((IsPasswordProtected || !currentPassword.IsEmpty) && !TryVerifyPassword(currentPassword))
{
if (!TryVerifyPassword(currentPassword))
{
return false;
}
return false;
}

var setPasswordResponse = Connection.SendCommand(new SetPasswordCommand(newPassword, _oathData));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,12 @@ public void Dispose()
// disposed.
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
if (!_disposed && disposing)
{
if (disposing)
{
Scp03Keys.Dispose();
_session.Dispose();

_disposed = true;
}
Scp03Keys.Dispose();
_session.Dispose();

_disposed = true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,12 @@ public AuthenticateManagementKeyResult GetData()
// means the OffCard authenticated. If the expected response
// is correct, change it to fully authenticated.
var tlvReader = new TlvReader(ResponseApdu.Data);
if (tlvReader.TryReadNestedTlv(out tlvReader, EncodingTag))
if (tlvReader.TryReadNestedTlv(out var seqReader, EncodingTag) &&
seqReader.TryReadValue(out var tlvBytes, ResponseTag))
{
if (tlvReader.TryReadValue(out var tlvBytes, ResponseTag))
{
return tlvBytes.Span.SequenceEqual(YubiKeyAuthenticationExpectedResponse.Span)
? AuthenticateManagementKeyResult.MutualFullyAuthenticated
: AuthenticateManagementKeyResult.MutualYubiKeyAuthenticationFailed;
}
return tlvBytes.Span.SequenceEqual(YubiKeyAuthenticationExpectedResponse.Span)
? AuthenticateManagementKeyResult.MutualFullyAuthenticated
: AuthenticateManagementKeyResult.MutualYubiKeyAuthenticationFailed;
}

throw new MalformedYubiKeyResponseException(
Expand Down
17 changes: 8 additions & 9 deletions Yubico.YubiKey/src/Yubico/YubiKey/Piv/Commands/GetDataCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,15 @@ public int DataTag
get => _tag;
set
{
if (value < MinimumVendorTag || value > MaximumVendorTag)
if ((value < MinimumVendorTag || value > MaximumVendorTag) &&
value != DiscoveryTag &&
value != BiometricGroupTemplateTag)
{
if (value != DiscoveryTag && value != BiometricGroupTemplateTag)
{
throw new ArgumentException(
string.Format(
CultureInfo.CurrentCulture,
ExceptionMessages.InvalidDataTag,
value));
}
throw new ArgumentException(
string.Format(
CultureInfo.CurrentCulture,
ExceptionMessages.InvalidDataTag,
value));
}
_tag = value;
}
Expand Down
38 changes: 17 additions & 21 deletions Yubico.YubiKey/src/Yubico/YubiKey/Piv/Commands/PutDataCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,13 @@ public int DataTag
get => _tag;
set
{
if (value < MinimumVendorTag || value > MaximumVendorTag)
if ((value < MinimumVendorTag || value > MaximumVendorTag) && value != BiometricGroupTemplateTag)
{
if (value != BiometricGroupTemplateTag)
{
throw new ArgumentException(
string.Format(
CultureInfo.CurrentCulture,
ExceptionMessages.InvalidDataTag,
value));
}
throw new ArgumentException(
string.Format(
CultureInfo.CurrentCulture,
ExceptionMessages.InvalidDataTag,
value));
}
_tag = value;
}
Expand Down Expand Up @@ -456,21 +453,20 @@ private byte[] BuildPutDataApduData()
// 53 03 01 02 03 04
private static bool IsDataEncoded(ReadOnlyMemory<byte> encoding)
{
if (encoding.Length != 0)
if (encoding.Length == 0)
{
var tlvReader = new TlvReader(encoding);
if (tlvReader.PeekTag() == PivPutDataTag)
{
if (tlvReader.TryReadValue(out _, PivPutDataTag))
{
if (!tlvReader.HasData)
{
return true;
}
}
}
return false;
}

var tlvReader = new TlvReader(encoding);

if (tlvReader.PeekTag() == PivPutDataTag &&
tlvReader.TryReadValue(out _, PivPutDataTag) &&
!tlvReader.HasData)
{
return true;
}

return false;
}

Expand Down
Loading
Loading