Skip to content

fix,tests: Fixed bug where attest cert could not be RSA3072 or RSA4096, removed obsolete tests and consolidated Piv tests #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 47 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
2472f0e
tests: added ability to select usbcbio
DennisDyallo Apr 22, 2025
3f735f2
wip: remake tests
DennisDyallo Apr 23, 2025
f5ef846
samples: fix sample project
DennisDyallo Apr 23, 2025
3d1dbc6
Remove incorrect auth token truncation from various commands
AaFortner Apr 18, 2025
1971953
commits from PR204
equijano21 Apr 16, 2025
1f51a6b
typos, other minor fixes
equijano21 Apr 16, 2025
74838b0
misc: add comments, clarify return type
DennisDyallo Apr 24, 2025
fcd00ff
tests: remove bio requirement on fido tests
DennisDyallo Apr 24, 2025
59ea863
tests: improved usability of TestKeys
DennisDyallo Apr 29, 2025
30b3db7
docs: fix wording
DennisDyallo Apr 29, 2025
4131d0e
misc: separate into files
DennisDyallo Apr 29, 2025
4dea521
tests: wording, misc
DennisDyallo Apr 29, 2025
836635d
tests: added tests for RSA keys
DennisDyallo Apr 29, 2025
0ec46e1
tests: refactor
DennisDyallo Apr 29, 2025
3c1632f
change: EncodeToSubjectPublicKeyInfo will throw on missing public key…
DennisDyallo Apr 29, 2025
07ea148
tests: refactor CryptoSupport.cs
DennisDyallo Apr 29, 2025
c3a6621
tests: refactored tests
DennisDyallo Apr 29, 2025
23b6ee9
tests: update cert data to include CA extensions
DennisDyallo Apr 30, 2025
46295ec
tests: remove obsolete class
DennisDyallo Apr 30, 2025
9b82282
tests: piv test use reusable base class and all pass
DennisDyallo Apr 30, 2025
37fe763
misc: removed comments
DennisDyallo May 7, 2025
0e0d65b
misc: Rename comment
DennisDyallo May 7, 2025
745b9b1
tests: fixed some tests
DennisDyallo May 7, 2025
80f09a9
tests: refactor piv tests to reuse base class
DennisDyallo May 9, 2025
279fd05
tests: improved PivSession tests
DennisDyallo May 14, 2025
3465c0a
refactor: PivSession
DennisDyallo May 14, 2025
8c52f4e
tests: misc Scp test changes
DennisDyallo May 14, 2025
c268c1d
tests:
DennisDyallo May 14, 2025
32256bf
tests: PivSession
DennisDyallo May 14, 2025
96196da
tests: Fix PinOnlyWithResetTests.cs for devices gt v5.7
DennisDyallo May 15, 2025
a7b54cc
refactor: PivSession
DennisDyallo May 15, 2025
abf8c71
fix: bug where SDK would print a failure log even though we were succ…
DennisDyallo May 16, 2025
32f35d8
tests: minor refactor for tests in PivSession
DennisDyallo May 16, 2025
d307713
misc: added logging for changing pin / puk
DennisDyallo May 16, 2025
ace1a0a
refactor: reuse logic for GetMetadata (PivSession)
DennisDyallo May 23, 2025
46999f0
tests:
DennisDyallo May 25, 2025
5c5e93a
refactor: minor work on Piv Attestation and RSAPublicKey
DennisDyallo May 25, 2025
7bca86e
misc: code cleanup
DennisDyallo May 25, 2025
78b1f2c
misc: remove add default management key
DennisDyallo May 25, 2025
2705379
tests: PivSessionIntegrationTestBase.cs can now manage Fips credentials
DennisDyallo May 26, 2025
3eea339
tests: rename class
DennisDyallo May 26, 2025
4e3aade
misc: added logging when generate key
DennisDyallo May 26, 2025
9b2a69f
tests: added FIPS tests for generate
DennisDyallo May 26, 2025
68fecf7
test: comments, restructure
DennisDyallo May 26, 2025
b3f5446
Merge branch 'develop' into dennisdyallo/tests
DennisDyallo May 26, 2025
dbb6718
Merge branch 'develop' into dennisdyallo/tests
DennisDyallo May 26, 2025
92da903
misc: pr feedback
DennisDyallo May 27, 2025
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 @@ -497,7 +497,9 @@ public static AsymmetricAlgorithm GetPublicKeyFromCertificate(X509Certificate2 c

if (string.Equals(certificate.PublicKey.Oid.FriendlyName, "ECC", StringComparison.Ordinal))
{
#pragma warning disable CS0618 // Type or member is obsolete
var pivPub = new PivEccPublicKey(certificate.PublicKey.EncodedKeyValue.RawData);
#pragma warning restore CS0618 // Type or member is obsolete
return KeyConverter.GetDotNetFromPivPublicKey(pivPub);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public static PivPublicKey GetPivPublicKeyFromDotNet(AsymmetricAlgorithm dotNetO
offset += keySize + (keySize - eccParams.Q.Y.Length);
Array.Copy(eccParams.Q.Y, 0, point, offset, eccParams.Q.Y.Length);

#pragma warning disable CS0618 // Type or member is obsolete
var eccPubKey = new PivEccPublicKey(point);
#pragma warning restore CS0618 // Type or member is obsolete
return (PivPublicKey)eccPubKey;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public static bool RunGenerateKeyPair(
{
pivSession.KeyCollector = KeyCollectorDelegate;

#pragma warning disable CS0618 // Type or member is obsolete
var pivPublicKey = pivSession.GenerateKeyPair(slotNumber, algorithm, pinPolicy, touchPolicy);
#pragma warning restore CS0618 // Type or member is obsolete

// At this point you will likely want to save the public key and
// other information. For this sample, we're simply going to
Expand Down Expand Up @@ -74,7 +76,9 @@ public static bool RunImportPrivateKey(
{
pivSession.KeyCollector = KeyCollectorDelegate;

#pragma warning disable CS0618 // Type or member is obsolete
pivSession.ImportPrivateKey(slotNumber, privateKey, pinPolicy, touchPolicy);
#pragma warning restore CS0618 // Type or member is obsolete

// At this point you will likely want to save the public key and
// other information. For this sample, we're simply going to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ public static bool RunKeyAgree(
using (var pivSession = new PivSession(yubiKey))
{
pivSession.KeyCollector = KeyCollectorDelegate;
#pragma warning disable CS0618 // Type or member is obsolete
computedSecret = pivSession.KeyAgree(slotNumber, correspondentPublicKey);
#pragma warning restore CS0618 // Type or member is obsolete
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,12 @@ public static byte[] EncodeToSubjectPublicKeyInfo(
/// </summary>
/// <param name="parameters">The RSA public key parameters.</param>
/// <returns>A byte array containing the ASN.1 DER encoded public key.</returns>
/// <remarks>
/// Only public key parameters are supported. The method will throw an <see cref="ArgumentException"/> if any of the private key parameters are set.
/// </remarks>
public static byte[] EncodeToSubjectPublicKeyInfo(RSAParameters parameters)
{
if (parameters.D != null ||
parameters.P != null ||
parameters.Q != null ||
parameters.DP != null ||
parameters.DQ != null ||
parameters.InverseQ != null)
if (parameters.Exponent == null ||
parameters.Modulus == null)
{
throw new ArgumentException("Only public key parameters should be provided.");
throw new InvalidOperationException("Cannot export public key, missing required parameters");
}

return EncodeToSubjectPublicKeyInfo(parameters.Modulus, parameters.Exponent);
Expand All @@ -125,17 +118,7 @@ public static byte[] EncodeToSubjectPublicKeyInfo(RSAParameters parameters)
/// <returns>A byte array containing the ASN.1 DER encoded public key.</returns>
public static byte[] EncodeToSubjectPublicKeyInfo(ECParameters parameters)
{
if (parameters.D != null)
{
throw new ArgumentException("Only public key parameters should be provided.", nameof(parameters));
}

if (parameters.Q.X == null)
{
throw new ArgumentException("EC point coordinates cannot be null.", nameof(parameters));
}

if (parameters.Q.Y == null)
if (parameters.Q.X == null || parameters.Q.Y == null)
{
throw new ArgumentException("EC point coordinates cannot be null.", nameof(parameters));
}
Expand Down
9 changes: 4 additions & 5 deletions Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/ECPublicKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ protected ECPublicKey(ECDsa ecdsa)

/// <inheritdoc />
public override byte[] ExportSubjectPublicKeyInfo() => AsnPublicKeyEncoder.EncodeToSubjectPublicKeyInfo(Parameters);


/// <summary>
/// Creates an instance of <see cref="ECPublicKey"/> from the given <paramref name="parameters"/>.
Expand All @@ -118,7 +117,7 @@ protected ECPublicKey(ECDsa ecdsa)
/// <exception cref="ArgumentException">
/// Thrown if the key type is not a valid EC key.
/// </exception>
public static IPublicKey CreateFromValue(ReadOnlyMemory<byte> publicPoint, KeyType keyType)
public static ECPublicKey CreateFromValue(ReadOnlyMemory<byte> publicPoint, KeyType keyType)
{
var keyDefinition = KeyDefinitions.GetByKeyType(keyType);
if (keyDefinition.AlgorithmOid is not Oids.ECDSA)
Expand All @@ -142,13 +141,13 @@ public static IPublicKey CreateFromValue(ReadOnlyMemory<byte> publicPoint, KeyTy
}

/// <summary>
/// Creates an instance of <see cref="IPublicKey"/> from a DER-encoded public key.
/// Creates an instance of <see cref="ECPublicKey"/> from a DER-encoded public key.
/// </summary>
/// <param name="encodedKey">The DER-encoded public key.</param>
/// <returns>An instance of <see cref="IPublicKey"/>.</returns>
/// <exception cref="CryptographicException">
/// Thrown if the public key is invalid.
/// </exception>
public static IPublicKey CreateFromPkcs8(ReadOnlyMemory<byte> encodedKey) =>
AsnPublicKeyDecoder.CreatePublicKey(encodedKey);
public static ECPublicKey CreateFromPkcs8(ReadOnlyMemory<byte> encodedKey) =>
(ECPublicKey)AsnPublicKeyDecoder.CreatePublicKey(encodedKey);
}
9 changes: 0 additions & 9 deletions Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/IPublicKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,3 @@ public interface IPublicKey : IKeyBase
/// </returns>
public byte[] ExportSubjectPublicKeyInfo();
}

public abstract class PublicKey : IPublicKey
{
/// <inheritdoc />
public abstract KeyType KeyType { get; }

/// <inheritdoc />
public abstract byte[] ExportSubjectPublicKeyInfo();
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public static KeyDefinition GetByRSALength(int keySizeBits)

throw new NotSupportedException($"Unsupported RSA length: {keySizeBits}");
}

public static KeyDefinition GetByRSAModulusLength(byte[] modulus) => GetByRSALength(modulus.Length * 8);

/// <summary>
/// Gets a key definition by its curve type.
Expand Down Expand Up @@ -162,6 +164,12 @@ public static KeyDefinition GetByOid(string oid)
throw new NotSupportedException(
"RSA keys are not supported by this method as all RSA keys share the same OID.");
}

if (string.Equals(oid, Oids.ECDSA, StringComparison.OrdinalIgnoreCase))
{
throw new NotSupportedException(
"All ECDSA keys (P-256, P-384, P-521) share the same OID. Use the Curve OID instead.");
}

var keyDefinition = AllDefinitions.Values.FirstOrDefault(d => d.AlgorithmOid == oid || d.CurveOid == oid);
return keyDefinition ?? throw new NotSupportedException(
Expand Down
2 changes: 1 addition & 1 deletion Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/KeyType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace Yubico.YubiKey.Cryptography;

/// <summary>
/// Represents the type of a cryptographic key.
/// Represents the type of cryptographic key.
/// </summary>
public enum KeyType
{
Expand Down
24 changes: 24 additions & 0 deletions Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/PublicKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2024 Yubico AB
//
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Yubico.YubiKey.Cryptography;

public abstract class PublicKey : IPublicKey
{
/// <inheritdoc />
public abstract KeyType KeyType { get; }

/// <inheritdoc />
public abstract byte[] ExportSubjectPublicKeyInfo();
}
34 changes: 19 additions & 15 deletions Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/RSAPublicKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,11 @@ public sealed class RSAPublicKey : PublicKey

private RSAPublicKey(RSAParameters parameters)
{
if (parameters.D != null ||
parameters.P != null ||
parameters.Q != null ||
parameters.DP != null ||
parameters.DQ != null ||
parameters.InverseQ != null
)
{
throw new ArgumentException("Parameters must not contain private key data");
}

Parameters = parameters.DeepCopy();
KeyDefinition = KeyDefinitions.GetByRSALength(parameters.Modulus.Length * 8);
KeyDefinition = KeyDefinitions.GetByRSAModulusLength(parameters.Modulus);
}

/// <inheritdoc />
public override byte[] ExportSubjectPublicKeyInfo()
{
if (Parameters.Exponent == null ||
Expand All @@ -83,7 +73,21 @@ public override byte[] ExportSubjectPublicKeyInfo()
/// <exception cref="ArgumentException">
/// Thrown if the parameters contain private key data.
/// </exception>
public static RSAPublicKey CreateFromParameters(RSAParameters parameters) => new(parameters);
public static RSAPublicKey CreateFromParameters(RSAParameters parameters)
{
if (parameters.D != null ||
parameters.P != null ||
parameters.Q != null ||
parameters.DP != null ||
parameters.DQ != null ||
parameters.InverseQ != null
)
{
throw new ArgumentException("Parameters must not contain private key data");
}

return new RSAPublicKey(parameters);
}

/// <summary>
/// Creates a new instance of <see cref="IPublicKey"/> from a DER-encoded public key.
Expand All @@ -93,6 +97,6 @@ public override byte[] ExportSubjectPublicKeyInfo()
/// <exception cref="CryptographicException">
/// Thrown if the public key is invalid.
/// </exception>
public static IPublicKey CreateFromPkcs8(ReadOnlyMemory<byte> encodedKey) =>
AsnPublicKeyDecoder.CreatePublicKey(encodedKey);
public static RSAPublicKey CreateFromPkcs8(ReadOnlyMemory<byte> encodedKey) =>
(RSAPublicKey)AsnPublicKeyDecoder.CreatePublicKey(encodedKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

using System;
using System.Globalization;
using System.Linq;
using Yubico.Core.Iso7816;
using Yubico.YubiKey.Cryptography;

namespace Yubico.YubiKey.Piv.Commands
{
Expand Down Expand Up @@ -90,13 +92,6 @@ public sealed class AuthenticateSignCommand : AuthenticateCommand, IYubiKeyComma
{
private const byte DigestTag = 0x81;

private const int Rsa1024DigestDataLength = 128;
private const int Rsa2048DigestDataLength = 256;
private const int Rsa3072DigestDataLength = 384;
private const int Rsa4096DigestDataLength = 512;
private const int EccP256DigestDataLength = 32;
private const int EccP384DigestDataLength = 48;

// The default constructor explicitly defined. We don't want it to be
// used.
private AuthenticateSignCommand()
Expand Down Expand Up @@ -166,25 +161,21 @@ public AuthenticateSignCommand(ReadOnlyMemory<byte> digestData, byte slotNumber)
DataTag = DigestTag;
Data = digestData;
SlotNumber = slotNumber;
Algorithm = GetPivAlgorithm(digestData);
}

// Determine the algorithm based on the length of the digest data.
// Currently, the length of the data must be 128 (RSA-1024), 256
// (RSA-2048), 384 (RSA-3072), 512 (RSA-4096), 32 (ECC-P256), or 48 (ECC-P384).
// Return the PivAlgorithm, or if the length is not supported, throw an
// exception.
Algorithm = digestData.Length switch
private static PivAlgorithm GetPivAlgorithm(ReadOnlyMemory<byte> digestData)
{
if (KeyDefinitions.All.Values
.Where(k => k.IsRSA || k.IsEllipticCurve)
.Where(k => !k.KeyType.IsCurve25519())
.SingleOrDefault(k => k.LengthInBytes == digestData.Length) is { } keyDefinition)
{
Rsa1024DigestDataLength => PivAlgorithm.Rsa1024,
Rsa2048DigestDataLength => PivAlgorithm.Rsa2048,
Rsa3072DigestDataLength => PivAlgorithm.Rsa3072,
Rsa4096DigestDataLength => PivAlgorithm.Rsa4096,
EccP256DigestDataLength => PivAlgorithm.EccP256,
EccP384DigestDataLength => PivAlgorithm.EccP384,
_ => throw new ArgumentException(
string.Format(
CultureInfo.CurrentCulture,
ExceptionMessages.IncorrectDigestLength)),
};
return keyDefinition.KeyType.GetPivAlgorithm();
}

throw new ArgumentException(
string.Format(CultureInfo.CurrentCulture, ExceptionMessages.IncorrectDigestLength));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public byte SlotNumber
get => _slotNumber;
set
{
if (PivSlot.IsValidSlotNumberForSigning(value) == false)
if (!PivSlot.IsValidSlotNumberForSigning(value))
{
throw new ArgumentException(
string.Format(
Expand Down Expand Up @@ -152,7 +152,7 @@ public CreateAttestationStatementCommand()
/// </exception>
public CommandApdu CreateCommandApdu()
{
if (PivSlot.IsValidSlotNumberForSigning(_slotNumber) == false)
if (!PivSlot.IsValidSlotNumberForSigning(_slotNumber))
{
throw new InvalidOperationException(
string.Format(
Expand All @@ -161,7 +161,7 @@ public CommandApdu CreateCommandApdu()
_slotNumber));
}

return new CommandApdu()
return new CommandApdu
{
Ins = AttestInstruction,
P1 = SlotNumber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public GetMetadataCommand(byte slotNumber)
/// </remarks>
public GetMetadataCommand()
{
_slotNumber = 0;

}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void SetManagementKey(ReadOnlyMemory<byte> managementKey)
if (IsValidKeyLength(managementKey.Length))
{
managementKey.CopyTo(_mgmtKey);
ManagementKey = _mgmtKey.Slice(0, managementKey.Length);
ManagementKey = _mgmtKey[..managementKey.Length];
return;
}

Expand Down
Loading
Loading