Skip to content

Commit 76e5dd6

Browse files
authored
Merge pull request #230 from Yubico/dennisdyallo/tests
fix,tests: Fixed bug where attest cert could not be RSA3072 or RSA4096, removed obsolete tests and consolidated Piv tests
2 parents 935c676 + 92da903 commit 76e5dd6

File tree

141 files changed

+4163
-9179
lines changed

Some content is hidden

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

141 files changed

+4163
-9179
lines changed

Yubico.YubiKey/examples/PivSampleCode/CertificateOperations/SampleCertificateOperations.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,9 @@ public static AsymmetricAlgorithm GetPublicKeyFromCertificate(X509Certificate2 c
497497

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

Yubico.YubiKey/examples/PivSampleCode/Converters/KeyConverter.Asymmetric.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ public static PivPublicKey GetPivPublicKeyFromDotNet(AsymmetricAlgorithm dotNetO
6565
offset += keySize + (keySize - eccParams.Q.Y.Length);
6666
Array.Copy(eccParams.Q.Y, 0, point, offset, eccParams.Q.Y.Length);
6767

68+
#pragma warning disable CS0618 // Type or member is obsolete
6869
var eccPubKey = new PivEccPublicKey(point);
70+
#pragma warning restore CS0618 // Type or member is obsolete
6971
return (PivPublicKey)eccPubKey;
7072
}
7173

Yubico.YubiKey/examples/PivSampleCode/YubiKeyOperations/KeyPairs.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public static bool RunGenerateKeyPair(
3333
{
3434
pivSession.KeyCollector = KeyCollectorDelegate;
3535

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

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

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

7983
// At this point you will likely want to save the public key and
8084
// other information. For this sample, we're simply going to

Yubico.YubiKey/examples/PivSampleCode/YubiKeyOperations/PrivateKeyOperations.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ public static bool RunKeyAgree(
170170
using (var pivSession = new PivSession(yubiKey))
171171
{
172172
pivSession.KeyCollector = KeyCollectorDelegate;
173+
#pragma warning disable CS0618 // Type or member is obsolete
173174
computedSecret = pivSession.KeyAgree(slotNumber, correspondentPublicKey);
175+
#pragma warning restore CS0618 // Type or member is obsolete
174176
}
175177

176178
return true;

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/AsnPublicKeyEncoder.cs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,12 @@ public static byte[] EncodeToSubjectPublicKeyInfo(
100100
/// </summary>
101101
/// <param name="parameters">The RSA public key parameters.</param>
102102
/// <returns>A byte array containing the ASN.1 DER encoded public key.</returns>
103-
/// <remarks>
104-
/// Only public key parameters are supported. The method will throw an <see cref="ArgumentException"/> if any of the private key parameters are set.
105-
/// </remarks>
106103
public static byte[] EncodeToSubjectPublicKeyInfo(RSAParameters parameters)
107104
{
108-
if (parameters.D != null ||
109-
parameters.P != null ||
110-
parameters.Q != null ||
111-
parameters.DP != null ||
112-
parameters.DQ != null ||
113-
parameters.InverseQ != null)
105+
if (parameters.Exponent == null ||
106+
parameters.Modulus == null)
114107
{
115-
throw new ArgumentException("Only public key parameters should be provided.");
108+
throw new InvalidOperationException("Cannot export public key, missing required parameters");
116109
}
117110

118111
return EncodeToSubjectPublicKeyInfo(parameters.Modulus, parameters.Exponent);
@@ -125,17 +118,7 @@ public static byte[] EncodeToSubjectPublicKeyInfo(RSAParameters parameters)
125118
/// <returns>A byte array containing the ASN.1 DER encoded public key.</returns>
126119
public static byte[] EncodeToSubjectPublicKeyInfo(ECParameters parameters)
127120
{
128-
if (parameters.D != null)
129-
{
130-
throw new ArgumentException("Only public key parameters should be provided.", nameof(parameters));
131-
}
132-
133-
if (parameters.Q.X == null)
134-
{
135-
throw new ArgumentException("EC point coordinates cannot be null.", nameof(parameters));
136-
}
137-
138-
if (parameters.Q.Y == null)
121+
if (parameters.Q.X == null || parameters.Q.Y == null)
139122
{
140123
throw new ArgumentException("EC point coordinates cannot be null.", nameof(parameters));
141124
}

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/ECPublicKey.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ protected ECPublicKey(ECDsa ecdsa)
9999

100100
/// <inheritdoc />
101101
public override byte[] ExportSubjectPublicKeyInfo() => AsnPublicKeyEncoder.EncodeToSubjectPublicKeyInfo(Parameters);
102-
103102

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

144143
/// <summary>
145-
/// Creates an instance of <see cref="IPublicKey"/> from a DER-encoded public key.
144+
/// Creates an instance of <see cref="ECPublicKey"/> from a DER-encoded public key.
146145
/// </summary>
147146
/// <param name="encodedKey">The DER-encoded public key.</param>
148147
/// <returns>An instance of <see cref="IPublicKey"/>.</returns>
149148
/// <exception cref="CryptographicException">
150149
/// Thrown if the public key is invalid.
151150
/// </exception>
152-
public static IPublicKey CreateFromPkcs8(ReadOnlyMemory<byte> encodedKey) =>
153-
AsnPublicKeyDecoder.CreatePublicKey(encodedKey);
151+
public static ECPublicKey CreateFromPkcs8(ReadOnlyMemory<byte> encodedKey) =>
152+
(ECPublicKey)AsnPublicKeyDecoder.CreatePublicKey(encodedKey);
154153
}

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/IPublicKey.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,3 @@ public interface IPublicKey : IKeyBase
2424
/// </returns>
2525
public byte[] ExportSubjectPublicKeyInfo();
2626
}
27-
28-
public abstract class PublicKey : IPublicKey
29-
{
30-
/// <inheritdoc />
31-
public abstract KeyType KeyType { get; }
32-
33-
/// <inheritdoc />
34-
public abstract byte[] ExportSubjectPublicKeyInfo();
35-
}

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/KeyDefinitions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ public static KeyDefinition GetByRSALength(int keySizeBits)
107107

108108
throw new NotSupportedException($"Unsupported RSA length: {keySizeBits}");
109109
}
110+
111+
public static KeyDefinition GetByRSAModulusLength(byte[] modulus) => GetByRSALength(modulus.Length * 8);
110112

111113
/// <summary>
112114
/// Gets a key definition by its curve type.
@@ -162,6 +164,12 @@ public static KeyDefinition GetByOid(string oid)
162164
throw new NotSupportedException(
163165
"RSA keys are not supported by this method as all RSA keys share the same OID.");
164166
}
167+
168+
if (string.Equals(oid, Oids.ECDSA, StringComparison.OrdinalIgnoreCase))
169+
{
170+
throw new NotSupportedException(
171+
"All ECDSA keys (P-256, P-384, P-521) share the same OID. Use the Curve OID instead.");
172+
}
165173

166174
var keyDefinition = AllDefinitions.Values.FirstOrDefault(d => d.AlgorithmOid == oid || d.CurveOid == oid);
167175
return keyDefinition ?? throw new NotSupportedException(

Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/KeyType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace Yubico.YubiKey.Cryptography;
1616

1717
/// <summary>
18-
/// Represents the type of a cryptographic key.
18+
/// Represents the type of cryptographic key.
1919
/// </summary>
2020
public enum KeyType
2121
{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2024 Yubico AB
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License").
4+
// You may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
namespace Yubico.YubiKey.Cryptography;
16+
17+
public abstract class PublicKey : IPublicKey
18+
{
19+
/// <inheritdoc />
20+
public abstract KeyType KeyType { get; }
21+
22+
/// <inheritdoc />
23+
public abstract byte[] ExportSubjectPublicKeyInfo();
24+
}

0 commit comments

Comments
 (0)