Skip to content

Commit 3d816f3

Browse files
committed
tests: fix testkeys loading legacy pivprivatekeys
1 parent 2593ce5 commit 3d816f3

File tree

4 files changed

+23
-31
lines changed

4 files changed

+23
-31
lines changed

Yubico.YubiKey/tests/unit/Yubico/YubiKey/Piv/PivPublicKeyTests.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void Create_ReturnsPivPublicKey(
4949
{
5050
ReadOnlyMemory<byte> keyData = GetKeyData(keyType);
5151

52-
var keyObject = PivPublicKey.Create(keyData);
52+
var keyObject = PivPublicKey.Create(keyData, keyType.GetPivAlgorithm());
5353

5454
Assert.True(keyObject is PivPublicKey);
5555
}
@@ -64,7 +64,7 @@ public void Create_SetsAlgorithmCorrectly(
6464
{
6565
ReadOnlyMemory<byte> keyData = GetKeyData(keyType);
6666

67-
var keyObject = PivPublicKey.Create(keyData);
67+
var keyObject = PivPublicKey.Create(keyData, keyType.GetPivAlgorithm());
6868

6969
Assert.NotNull(keyObject);
7070
Assert.Equal(keyType.GetPivAlgorithm(), keyObject.Algorithm);
@@ -81,7 +81,7 @@ public void Create_SetsEncodedCorrectly(
8181
ReadOnlyMemory<byte> keyData = GetKeyData(keyType);
8282
ReadOnlyMemory<byte> encoding = GetCorrectEncoding(keyType);
8383

84-
var keyObject = PivPublicKey.Create(keyData);
84+
var keyObject = PivPublicKey.Create(keyData, keyType.GetPivAlgorithm());
8585
Assert.NotNull(keyObject);
8686

8787
ReadOnlyMemory<byte> getKeyData = keyObject.PivEncodedPublicKey;
@@ -101,7 +101,7 @@ public void Create_SetsMetadataEncodedCorrectly(
101101
ReadOnlyMemory<byte> keyData = GetKeyData(keyType);
102102
ReadOnlyMemory<byte> encoding = GetCorrectMetadataEncoding(keyType);
103103

104-
var keyObject = PivPublicKey.Create(keyData);
104+
var keyObject = PivPublicKey.Create(keyData, keyType.GetPivAlgorithm());
105105
Assert.NotNull(keyObject);
106106

107107
ReadOnlyMemory<byte> getKeyData = keyObject.YubiKeyEncodedPublicKey;
@@ -120,7 +120,7 @@ public void CreateRsa_SetsModulusCorrectly(
120120
ReadOnlyMemory<byte> keyData = GetKeyData(keyType);
121121
ReadOnlyMemory<byte> modulus = GetModulus(keyType);
122122

123-
var keyObject = PivPublicKey.Create(keyData);
123+
var keyObject = PivPublicKey.Create(keyData, keyType.GetPivAlgorithm());
124124

125125
Assert.NotNull(keyObject);
126126
Assert.True(keyObject is PivRsaPublicKey);
@@ -145,7 +145,7 @@ public void CreateRsa_SetsExponentCorrectly(
145145
ReadOnlyMemory<byte> keyData = SampleKeyPairs.GetPivPublicKey(keyType).PivEncodedPublicKey;
146146
ReadOnlyMemory<byte> exponent = GetExponent();
147147

148-
var keyObject = PivPublicKey.Create(keyData);
148+
var keyObject = PivPublicKey.Create(keyData, keyType.GetPivAlgorithm());
149149

150150
Assert.NotNull(keyObject);
151151
Assert.True(keyObject is PivRsaPublicKey);
@@ -168,7 +168,7 @@ public void CreateEcc_SetsPublicPointCorrectly(
168168
ReadOnlyMemory<byte> keyData = GetKeyData(keyType);
169169
ReadOnlyMemory<byte> publicPoint = GetPoint(keyType);
170170

171-
var keyObject = PivPublicKey.Create(keyData);
171+
var keyObject = PivPublicKey.Create(keyData, keyType.GetPivAlgorithm());
172172

173173
Assert.NotNull(keyObject);
174174
Assert.True(keyObject is PivEccPublicKey);
@@ -224,23 +224,23 @@ public void EccConstructor_Components_BuildsEncoding(
224224
[Fact]
225225
public void Create_NullData_ThrowsException()
226226
{
227-
_ = Assert.Throws<ArgumentException>(() => PivPublicKey.Create(null));
227+
_ = Assert.Throws<ArgumentException>(() => PivPublicKey.Create(null, PivAlgorithm.EccP256));
228228
}
229229

230230
[Fact]
231231
public void Create_BadTag_ThrowsExcpetion()
232232
{
233233
Memory<byte> keyData = GetKeyData(KeyType.P256);
234234
keyData.Span[0] = 0x84;
235-
_ = Assert.Throws<ArgumentException>(() => PivPublicKey.Create(keyData));
235+
_ = Assert.Throws<ArgumentException>(() => PivPublicKey.Create(keyData, KeyType.P256.GetPivAlgorithm()));
236236
}
237237

238238
[Fact]
239239
public void Rsa_NoExpo_ThrowsExcpetion()
240240
{
241241
Memory<byte> keyData = GetKeyData(KeyType.RSA1024);
242242
Memory<byte> badData = keyData.Slice(keyData.Length - 6);
243-
_ = Assert.Throws<ArgumentException>(() => PivPublicKey.Create(badData));
243+
_ = Assert.Throws<ArgumentException>(() => PivPublicKey.Create(badData, PivAlgorithm.Rsa1024));
244244
}
245245

246246
[Theory]
@@ -250,7 +250,7 @@ public void RsaConstructor_BadMod_ThrowsExcpetion(
250250
KeyType keyType)
251251
{
252252
Memory<byte> keyData = GetBadEncoding(keyType);
253-
_ = Assert.Throws<ArgumentException>(() => PivPublicKey.Create(keyData));
253+
_ = Assert.Throws<ArgumentException>(() => PivPublicKey.Create(keyData, keyType.GetPivAlgorithm()));
254254
}
255255

256256
[Fact]
@@ -275,7 +275,7 @@ public void EccConstructor_BadPoint_ThrowsExcpetion(
275275
KeyType keyType)
276276
{
277277
Memory<byte> keyData = GetBadEncoding(keyType);
278-
_ = Assert.Throws<ArgumentException>(() => PivPublicKey.Create(keyData));
278+
_ = Assert.Throws<ArgumentException>(() => PivPublicKey.Create(keyData, keyType.GetPivAlgorithm()));
279279
}
280280

281281
private static Memory<byte> GetModulus(

Yubico.YubiKey/tests/utilities/Yubico/YubiKey/TestUtilities/KeyConverter.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public KeyConverter(
150150
{
151151
if (pivPublicKey.Algorithm != PivAlgorithm.None)
152152
{
153-
_pivPublicKey = PivPublicKey.Create(pivPublicKey.PivEncodedPublicKey);
153+
_pivPublicKey = PivPublicKey.Create(pivPublicKey.PivEncodedPublicKey, pivPublicKey.Algorithm);
154154
}
155155

156156
SetProperties(true);
@@ -169,7 +169,7 @@ public KeyConverter(
169169
{
170170
if (pivPrivateKey.Algorithm != PivAlgorithm.None)
171171
{
172-
_pivPrivateKey = PivPrivateKey.Create(pivPrivateKey.EncodedPrivateKey);
172+
_pivPrivateKey = PivPrivateKey.Create(pivPrivateKey.EncodedPrivateKey, pivPrivateKey.Algorithm);
173173
}
174174

175175
SetProperties(true);
@@ -348,17 +348,9 @@ public bool IsKeyAvailable(
348348
// an ECC private key.
349349
public PivPublicKey GetPivPublicKey()
350350
{
351-
// if (_pivPublicKey.Algorithm == PivAlgorithm.EccX25519)
352-
// {
353-
// var testPublicKey = TestKeys.GetPublicKey(_pivPublicKey.Algorithm);
354-
// var last32Bytes = testPublicKey.KeyBytes.AsSpan()[^32..];
355-
// var pivPublicKey = new PivEccPublicKey(last32Bytes, KeyType.Ed25519);
356-
// return pivPublicKey;
357-
// }
358-
359351
if (_pivPublicKey.Algorithm != PivAlgorithm.None)
360352
{
361-
return PivPublicKey.Create(_pivPublicKey.PivEncodedPublicKey);
353+
return PivPublicKey.Create(_pivPublicKey.PivEncodedPublicKey, Algorithm);
362354
}
363355

364356
if (_pivPrivateKey.Algorithm == PivAlgorithm.Rsa1024 || _pivPrivateKey.Algorithm == PivAlgorithm.Rsa2048)
@@ -424,7 +416,7 @@ public PivPrivateKey GetPivPrivateKey()
424416

425417
if (_pivPrivateKey.Algorithm != PivAlgorithm.None)
426418
{
427-
return PivPrivateKey.Create(_pivPrivateKey.EncodedPrivateKey);
419+
return PivPrivateKey.Create(_pivPrivateKey.EncodedPrivateKey, _pivPrivateKey.Algorithm);
428420
}
429421

430422
throw new InvalidOperationException(

Yubico.YubiKey/tests/utilities/Yubico/YubiKey/TestUtilities/TestKeyExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,28 @@ public static class TestKeyExtensions
1010
/// Converts the key to a PIV private key format.
1111
/// </summary>
1212
/// <returns>PivPrivateKey instance</returns>
13-
public static PivPrivateKey AsPivPrivateKey( // now we can renmove the parser reader classes
13+
public static PivPrivateKey AsPivPrivateKey(
1414
this TestKey key)
1515
{
1616
var keyDefinition = key.KeyDefinition;
1717
if (keyDefinition.IsRsaKey)
1818
{
1919
var rsaPrivateKey = RSAPrivateKeyParameters.CreateFromPkcs8(key.EncodedKey);
2020
var rsaPivEncodedKey = rsaPrivateKey.ToPivEncodedPrivateKey();
21-
return PivPrivateKey.Create(rsaPivEncodedKey);
21+
return PivPrivateKey.Create(rsaPivEncodedKey, keyDefinition.KeyType.GetPivAlgorithm());
2222
}
2323

2424
if (keyDefinition is { IsEcKey: true, AlgorithmOid: KeyDefinitions.CryptoOids.ECDSA })
2525
{
2626
var ecPrivateKey = ECPrivateKeyParameters.CreateFromPkcs8(key.EncodedKey);
2727
var ecPivEncodedKey = ecPrivateKey.ToPivEncodedPrivateKey();
28-
return PivPrivateKey.Create(ecPivEncodedKey);
28+
return PivPrivateKey.Create(ecPivEncodedKey, keyDefinition.KeyType.GetPivAlgorithm());
2929
}
3030

3131
// Curve25519
3232
var cvPrivateKey = Curve25519PrivateKeyParameters.CreateFromPkcs8(key.EncodedKey);
3333
var cvPivEncodedKey = cvPrivateKey.ToPivEncodedPrivateKey();
34-
return PivPrivateKey.Create(cvPivEncodedKey);
34+
return PivPrivateKey.Create(cvPivEncodedKey, keyDefinition.KeyType.GetPivAlgorithm());
3535
}
3636

3737
/// <summary>
@@ -46,14 +46,14 @@ public static PivPublicKey AsPivPublicKey(
4646
{
4747
var rsaPublicKey = RSAPublicKeyParameters.CreateFromPkcs8(key.EncodedKey);
4848
var rsaPivEncodedKey = rsaPublicKey.ToPivEncodedPublicKey();
49-
return PivPublicKey.Create(rsaPivEncodedKey);
49+
return PivPublicKey.Create(rsaPivEncodedKey, key.KeyType.GetPivAlgorithm());
5050
}
5151

5252
if (keyDefinition is { IsEcKey: true, AlgorithmOid: KeyDefinitions.CryptoOids.ECDSA })
5353
{
5454
var ecPublicKey = ECPublicKeyParameters.CreateFromPkcs8(key.EncodedKey);
5555
var ecPivEncodedKey = ecPublicKey.ToPivEncodedPublicKey();
56-
return PivPublicKey.Create(ecPivEncodedKey);
56+
return PivPublicKey.Create(ecPivEncodedKey, key.KeyType.GetPivAlgorithm());
5757
}
5858

5959
// Curve25519

Yubico.YubiKey/tests/utilities/Yubico/YubiKey/TestUtilities/TestKeys.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public static TestKey GetTestPrivateKey(
291291
public static TestKey GetTestPrivateKey(
292292
PivAlgorithm algorithm) => GetTestPrivateKey(algorithm.GetKeyType());
293293

294-
public static (TestKey publicKey, TestKey privateKey) GetKeyPair(
294+
public static (TestKey testPublicKey, TestKey testPrivateKey) GetKeyPair(
295295
KeyType keyType) => (GetTestPublicKey(keyType), GetTestPrivateKey(keyType));
296296

297297

0 commit comments

Comments
 (0)