Skip to content

Commit 027d755

Browse files
authored
Cleanup some code using ECAlgorithm
1 parent ca85979 commit 027d755

7 files changed

Lines changed: 38 additions & 109 deletions

File tree

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/EC/ECKeyFileTests.cs

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,13 @@
99
namespace System.Security.Cryptography.Tests
1010
{
1111
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
12-
public abstract partial class ECKeyFileTests<T> where T : AsymmetricAlgorithm
12+
public abstract partial class ECKeyFileTests<T> where T : ECAlgorithm
1313
{
1414
protected abstract T CreateKey();
15-
protected abstract byte[] ExportECPrivateKey(T key);
16-
protected abstract bool TryExportECPrivateKey(T key, Span<byte> destination, out int bytesWritten);
17-
protected abstract void ImportECPrivateKey(T key, ReadOnlySpan<byte> source, out int bytesRead);
18-
protected abstract void ImportParameters(T key, ECParameters ecParameters);
19-
protected abstract ECParameters ExportParameters(T key, bool includePrivate);
2015
protected abstract void Exercise(T key);
2116
protected virtual Func<T, byte[]> PublicKeyWriteArrayFunc { get; } = null;
2217
protected virtual WriteKeyToSpanFunc PublicKeyWriteSpanFunc { get; } = null;
23-
18+
2419
// This would need to be virtualized if there was ever a platform that
2520
// allowed explicit in ECDH or ECDSA but not the other.
2621
public static bool SupportsExplicitCurves { get; } = EcDiffieHellman.Tests.ECDiffieHellmanFactory.ExplicitCurvesSupported;
@@ -51,7 +46,7 @@ public void UseAfterDispose(bool importKey)
5146

5247
if (importKey)
5348
{
54-
ImportParameters(key, EccTestData.GetNistP256ReferenceKey());
49+
key.ImportParameters(EccTestData.GetNistP256ReferenceKey());
5550
}
5651

5752
byte[] ecPrivate;
@@ -72,20 +67,20 @@ public void UseAfterDispose(bool importKey)
7267
// Also ensures all of the inputs are valid for the disposed tests.
7368
using (key)
7469
{
75-
ecPrivate = ExportECPrivateKey(key);
70+
ecPrivate = key.ExportECPrivateKey();
7671
pkcs8Private = key.ExportPkcs8PrivateKey();
7772
pkcs8EncryptedPrivate = key.ExportEncryptedPkcs8PrivateKey(pwStr, pbeParameters);
7873
subjectPublicKeyInfo = key.ExportSubjectPublicKeyInfo();
7974
}
8075

81-
Assert.Throws<ObjectDisposedException>(() => ImportECPrivateKey(key, ecPrivate, out _));
76+
Assert.Throws<ObjectDisposedException>(() => key.ImportECPrivateKey(ecPrivate, out _));
8277
Assert.Throws<ObjectDisposedException>(() => key.ImportPkcs8PrivateKey(pkcs8Private, out _));
8378
Assert.Throws<ObjectDisposedException>(() => key.ImportEncryptedPkcs8PrivateKey(pwStr, pkcs8EncryptedPrivate, out _));
8479
Assert.Throws<ObjectDisposedException>(() => key.ImportEncryptedPkcs8PrivateKey(pwBytes, pkcs8EncryptedPrivate, out _));
8580
Assert.Throws<ObjectDisposedException>(() => key.ImportSubjectPublicKeyInfo(subjectPublicKeyInfo, out _));
8681

87-
Assert.Throws<ObjectDisposedException>(() => ExportECPrivateKey(key));
88-
Assert.Throws<ObjectDisposedException>(() => TryExportECPrivateKey(key, ecPrivate, out _));
82+
Assert.Throws<ObjectDisposedException>(() => key.ExportECPrivateKey());
83+
Assert.Throws<ObjectDisposedException>(() => key.TryExportECPrivateKey(ecPrivate, out _));
8984
Assert.Throws<ObjectDisposedException>(() => key.ExportPkcs8PrivateKey());
9085
Assert.Throws<ObjectDisposedException>(() => key.TryExportPkcs8PrivateKey(pkcs8Private, out _));
9186
Assert.Throws<ObjectDisposedException>(() => key.ExportEncryptedPkcs8PrivateKey(pwStr, pbeParameters));
@@ -654,7 +649,7 @@ public void NoFuzzySubjectPublicKeyInfo()
654649
using (T key = CreateKey())
655650
{
656651
int bytesRead = -1;
657-
byte[] ecPriv = ExportECPrivateKey(key);
652+
byte[] ecPriv = key.ExportECPrivateKey();
658653

659654
Assert.ThrowsAny<CryptographicException>(
660655
() => key.ImportSubjectPublicKeyInfo(ecPriv, out bytesRead));
@@ -693,14 +688,14 @@ public void NoFuzzyECPrivateKey()
693688
byte[] spki = key.ExportSubjectPublicKeyInfo();
694689

695690
Assert.ThrowsAny<CryptographicException>(
696-
() => ImportECPrivateKey(key, spki, out bytesRead));
691+
() => key.ImportECPrivateKey(spki, out bytesRead));
697692

698693
Assert.Equal(-1, bytesRead);
699694

700695
byte[] pkcs8 = key.ExportPkcs8PrivateKey();
701696

702697
Assert.ThrowsAny<CryptographicException>(
703-
() => ImportECPrivateKey(key, pkcs8, out bytesRead));
698+
() => key.ImportECPrivateKey(pkcs8, out bytesRead));
704699

705700
Assert.Equal(-1, bytesRead);
706701

@@ -714,7 +709,7 @@ public void NoFuzzyECPrivateKey()
714709
123));
715710

716711
Assert.ThrowsAny<CryptographicException>(
717-
() => ImportECPrivateKey(key, encryptedPkcs8, out bytesRead));
712+
() => key.ImportECPrivateKey(encryptedPkcs8, out bytesRead));
718713

719714
Assert.Equal(-1, bytesRead);
720715
}
@@ -733,7 +728,7 @@ public void NoFuzzyPkcs8()
733728

734729
Assert.Equal(-1, bytesRead);
735730

736-
byte[] ecPriv = ExportECPrivateKey(key);
731+
byte[] ecPriv = key.ExportECPrivateKey();
737732

738733
Assert.ThrowsAny<CryptographicException>(
739734
() => key.ImportPkcs8PrivateKey(ecPriv, out bytesRead));
@@ -770,7 +765,7 @@ public void NoFuzzyEncryptedPkcs8()
770765

771766
Assert.Equal(-1, bytesRead);
772767

773-
byte[] ecPriv = ExportECPrivateKey(key);
768+
byte[] ecPriv = key.ExportECPrivateKey();
774769

775770
Assert.ThrowsAny<CryptographicException>(
776771
() => key.ImportEncryptedPkcs8PrivateKey(empty, ecPriv, out bytesRead));
@@ -793,13 +788,13 @@ public void NoPrivKeyFromPublicOnly()
793788
{
794789
ECParameters parameters = EccTestData.GetNistP521Key2();
795790
parameters.D = null;
796-
ImportParameters(key, parameters);
791+
key.ImportParameters(parameters);
797792

798793
Assert.ThrowsAny<CryptographicException>(
799-
() => ExportECPrivateKey(key));
794+
() => key.ExportECPrivateKey());
800795

801796
Assert.ThrowsAny<CryptographicException>(
802-
() => TryExportECPrivateKey(key, Span<byte>.Empty, out _));
797+
() => key.TryExportECPrivateKey(Span<byte>.Empty, out _));
803798

804799
Assert.ThrowsAny<CryptographicException>(
805800
() => key.ExportPkcs8PrivateKey());
@@ -1100,17 +1095,17 @@ private void ReadWriteBase64ECPrivateKey(string base64Pkcs8, in ECParameters exp
11001095
base64Pkcs8,
11011096
expected,
11021097
(T key, ReadOnlySpan<byte> source, out int read) =>
1103-
ImportECPrivateKey(key, source, out read),
1104-
key => ExportECPrivateKey(key),
1098+
key.ImportECPrivateKey(source, out read),
1099+
key => key.ExportECPrivateKey(),
11051100
(T key, Span<byte> destination, out int bytesWritten) =>
1106-
TryExportECPrivateKey(key, destination, out bytesWritten));
1101+
key.TryExportECPrivateKey(destination, out bytesWritten));
11071102
}
11081103
else
11091104
{
11101105
using (T key = CreateKey())
11111106
{
11121107
Exception e = Assert.ThrowsAny<Exception>(
1113-
() => ImportECPrivateKey(key, Convert.FromBase64String(base64Pkcs8), out _));
1108+
() => key.ImportECPrivateKey(Convert.FromBase64String(base64Pkcs8), out _));
11141109

11151110
Assert.True(
11161111
e is PlatformNotSupportedException || e is CryptographicException,
@@ -1216,7 +1211,7 @@ private void ReadWriteKey(
12161211
Assert.Equal(arrayExport, publicArrayExport);
12171212
}
12181213

1219-
ECParameters ecParameters = ExportParameters(key, isPrivateKey);
1214+
ECParameters ecParameters = key.ExportParameters(isPrivateKey);
12201215
EccTestBase.AssertEqual(expected, ecParameters);
12211216
}
12221217

@@ -1240,7 +1235,7 @@ private void ReadWriteKey(
12401235
readAction(key, arrayExport, out int bytesRead);
12411236
Assert.Equal(arrayExport.Length, bytesRead);
12421237

1243-
ECParameters ecParameters = ExportParameters(key, isPrivateKey);
1238+
ECParameters ecParameters = key.ExportParameters(isPrivateKey);
12441239
EccTestBase.AssertEqual(expected, ecParameters);
12451240

12461241
Assert.False(

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/EC/ECKeyPemTests.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
namespace System.Security.Cryptography.Tests
99
{
1010
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
11-
public abstract class ECKeyPemTests<TAlg> where TAlg : AsymmetricAlgorithm
11+
public abstract class ECKeyPemTests<TAlg> where TAlg : ECAlgorithm
1212
{
1313
private const string AmbiguousExceptionMarker = "multiple keys";
1414
private const string EncryptedExceptionMarker = "encrypted key";
1515
private const string NoPemExceptionMarker = "No supported key";
1616

1717
protected abstract TAlg CreateKey();
18-
protected abstract ECParameters ExportParameters(TAlg key, bool includePrivateParameters);
1918

2019
[Fact]
2120
public void ImportFromPem_NoPem()
@@ -38,7 +37,7 @@ public void ImportFromPem_ECPrivateKey_Simple()
3837
AwEHoUQDQgAEgQHs5HRkpurXDPaabivT2IaRoyYtIsuk92Ner/JmgKjYoSumHVmS
3938
NfZ9nLTVjxeD08pD548KWrqmJAeZNsDDqQ==
4039
-----END EC PRIVATE KEY-----");
41-
ECParameters ecParameters = ExportParameters(key, true);
40+
ECParameters ecParameters = key.ExportParameters(true);
4241
ECParameters expected = EccTestData.GetNistP256ReferenceKey();
4342
EccTestBase.AssertEqual(expected, ecParameters);
4443
}
@@ -64,7 +63,7 @@ public void ImportFromPem_ECPrivateKey_IgnoresUnrelatedAlgorithm()
6463
AwEHoUQDQgAEgQHs5HRkpurXDPaabivT2IaRoyYtIsuk92Ner/JmgKjYoSumHVmS
6564
NfZ9nLTVjxeD08pD548KWrqmJAeZNsDDqQ==
6665
-----END EC PRIVATE KEY-----");
67-
ECParameters ecParameters = ExportParameters(key, true);
66+
ECParameters ecParameters = key.ExportParameters(true);
6867
ECParameters expected = EccTestData.GetNistP256ReferenceKey();
6968
EccTestBase.AssertEqual(expected, ecParameters);
7069
}
@@ -81,7 +80,7 @@ public void ImportFromPem_Pkcs8_Simple()
8180
whpHKz8E19aFG/Y0ny19WzRSs4qhRANCAASBAezkdGSm6tcM9ppuK9PYhpGjJi0i
8281
y6T3Y16v8maAqNihK6YdWZI19n2ctNWPF4PTykPnjwpauqYkB5k2wMOp
8382
-----END PRIVATE KEY-----");
84-
ECParameters ecParameters = ExportParameters(key, true);
83+
ECParameters ecParameters = key.ExportParameters(true);
8584
ECParameters expected = EccTestData.GetNistP256ReferenceKey();
8685
EccTestBase.AssertEqual(expected, ecParameters);
8786
}
@@ -107,7 +106,7 @@ public void ImportFromPem_Pkcs8_IgnoresUnrelatedAlgorithm()
107106
whpHKz8E19aFG/Y0ny19WzRSs4qhRANCAASBAezkdGSm6tcM9ppuK9PYhpGjJi0i
108107
y6T3Y16v8maAqNihK6YdWZI19n2ctNWPF4PTykPnjwpauqYkB5k2wMOp
109108
-----END PRIVATE KEY-----");
110-
ECParameters ecParameters = ExportParameters(key, true);
109+
ECParameters ecParameters = key.ExportParameters(true);
111110
ECParameters expected = EccTestData.GetNistP256ReferenceKey();
112111
EccTestBase.AssertEqual(expected, ecParameters);
113112
}
@@ -123,7 +122,7 @@ public void ImportFromPem_Spki_Simple()
123122
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEgQHs5HRkpurXDPaabivT2IaRoyYt
124123
Isuk92Ner/JmgKjYoSumHVmSNfZ9nLTVjxeD08pD548KWrqmJAeZNsDDqQ==
125124
-----END PUBLIC KEY-----");
126-
ECParameters ecParameters = ExportParameters(key, false);
125+
ECParameters ecParameters = key.ExportParameters(false);
127126
ECParameters expected = EccTestData.GetNistP256ReferenceKey();
128127
EccTestBase.ComparePublicKey(expected.Q, ecParameters.Q, isEqual: true);
129128
}
@@ -154,7 +153,7 @@ public void ImportFromPem_Spki_PrecedingUnrelatedPemIsIgnored()
154153
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEgQHs5HRkpurXDPaabivT2IaRoyYt
155154
Isuk92Ner/JmgKjYoSumHVmSNfZ9nLTVjxeD08pD548KWrqmJAeZNsDDqQ==
156155
-----END PUBLIC KEY-----");
157-
ECParameters ecParameters = ExportParameters(key, false);
156+
ECParameters ecParameters = key.ExportParameters(false);
158157
ECParameters expected = EccTestData.GetNistP256ReferenceKey();
159158
EccTestBase.ComparePublicKey(expected.Q, ecParameters.Q, isEqual: true);
160159
}
@@ -179,7 +178,7 @@ public void ImportFromPem_Spki_IgnoresUnrelatedAlgorithms()
179178
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEgQHs5HRkpurXDPaabivT2IaRoyYt
180179
Isuk92Ner/JmgKjYoSumHVmSNfZ9nLTVjxeD08pD548KWrqmJAeZNsDDqQ==
181180
-----END PUBLIC KEY-----");
182-
ECParameters ecParameters = ExportParameters(key, false);
181+
ECParameters ecParameters = key.ExportParameters(false);
183182
ECParameters expected = EccTestData.GetNistP256ReferenceKey();
184183
EccTestBase.ComparePublicKey(expected.Q, ecParameters.Q, isEqual: true);
185184
}
@@ -198,7 +197,7 @@ public void ImportFromPem_Spki_PrecedingMalformedPem()
198197
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEgQHs5HRkpurXDPaabivT2IaRoyYt
199198
Isuk92Ner/JmgKjYoSumHVmSNfZ9nLTVjxeD08pD548KWrqmJAeZNsDDqQ==
200199
-----END PUBLIC KEY-----");
201-
ECParameters ecParameters = ExportParameters(key, false);
200+
ECParameters ecParameters = key.ExportParameters(false);
202201
ECParameters expected = EccTestData.GetNistP256ReferenceKey();
203202
EccTestBase.ComparePublicKey(expected.Q, ecParameters.Q, isEqual: true);
204203
}
@@ -324,7 +323,7 @@ public void ImportFromEncryptedPem_Pkcs8_Char_Simple()
324323
Qh0fqdrNovgFLubbJFMQN/MwwIAfIuf0Mn0WFYYeQiBJ3kg=
325324
-----END ENCRYPTED PRIVATE KEY-----";
326325
key.ImportFromEncryptedPem(pem, "test");
327-
ECParameters ecParameters = ExportParameters(key, true);
326+
ECParameters ecParameters = key.ExportParameters(true);
328327
ECParameters expected = EccTestData.GetNistP256ReferenceKey();
329328
EccTestBase.AssertEqual(expected, ecParameters);
330329
}
@@ -345,7 +344,7 @@ public void ImportFromEncryptedPem_Pkcs8_Byte_Simple()
345344
-----END ENCRYPTED PRIVATE KEY-----";
346345
byte[] passwordBytes = Encoding.UTF8.GetBytes("test");
347346
key.ImportFromEncryptedPem(pem, passwordBytes);
348-
ECParameters ecParameters = ExportParameters(key, true);
347+
ECParameters ecParameters = key.ExportParameters(true);
349348
ECParameters expected = EccTestData.GetNistP256ReferenceKey();
350349
EccTestBase.AssertEqual(expected, ecParameters);
351350
}

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/ECDiffieHellman/ECDhKeyFileTests.cs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,7 @@ namespace System.Security.Cryptography.EcDiffieHellman.Tests
99
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
1010
public class ECDhKeyFileTests : ECKeyFileTests<ECDiffieHellman>
1111
{
12-
protected override ECDiffieHellman CreateKey()
13-
{
14-
return ECDiffieHellmanFactory.Create();
15-
}
16-
17-
protected override byte[] ExportECPrivateKey(ECDiffieHellman key)
18-
{
19-
return key.ExportECPrivateKey();
20-
}
21-
22-
protected override bool TryExportECPrivateKey(ECDiffieHellman key, Span<byte> destination, out int bytesWritten)
23-
{
24-
return key.TryExportECPrivateKey(destination, out bytesWritten);
25-
}
26-
27-
protected override void ImportECPrivateKey(ECDiffieHellman key, ReadOnlySpan<byte> source, out int bytesRead)
28-
{
29-
key.ImportECPrivateKey(source, out bytesRead);
30-
}
31-
32-
protected override void ImportParameters(ECDiffieHellman key, ECParameters ecParameters)
33-
{
34-
key.ImportParameters(ecParameters);
35-
}
36-
37-
protected override ECParameters ExportParameters(ECDiffieHellman key, bool includePrivate)
38-
{
39-
return key.ExportParameters(includePrivate);
40-
}
41-
12+
protected override ECDiffieHellman CreateKey() => ECDiffieHellmanFactory.Create();
4213
protected override void Exercise(ECDiffieHellman key) => key.Exercise();
4314

4415
protected override Func<ECDiffieHellman, byte[]> PublicKeyWriteArrayFunc { get; } =

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/ECDiffieHellman/ECDiffieHellmanKeyPemTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,5 @@ namespace System.Security.Cryptography.EcDsa.Tests
1010
public sealed class ECDiffieHellmanKeyPemTests : ECKeyPemTests<ECDiffieHellman>
1111
{
1212
protected override ECDiffieHellman CreateKey() => ECDiffieHellman.Create();
13-
protected override ECParameters ExportParameters(ECDiffieHellman key, bool includePrivateParameters) =>
14-
key.ExportParameters(includePrivateParameters);
1513
}
1614
}

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/ECDsa/ECDsaKeyFileTests.cs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,7 @@ namespace System.Security.Cryptography.EcDsa.Tests
99
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
1010
public class ECDsaKeyFileTests : ECKeyFileTests<ECDsa>
1111
{
12-
protected override ECDsa CreateKey()
13-
{
14-
return ECDsaFactory.Create();
15-
}
16-
17-
protected override byte[] ExportECPrivateKey(ECDsa key)
18-
{
19-
return key.ExportECPrivateKey();
20-
}
21-
22-
protected override bool TryExportECPrivateKey(ECDsa key, Span<byte> destination, out int bytesWritten)
23-
{
24-
return key.TryExportECPrivateKey(destination, out bytesWritten);
25-
}
26-
27-
protected override void ImportECPrivateKey(ECDsa key, ReadOnlySpan<byte> source, out int bytesRead)
28-
{
29-
key.ImportECPrivateKey(source, out bytesRead);
30-
}
31-
32-
protected override void ImportParameters(ECDsa key, ECParameters ecParameters)
33-
{
34-
key.ImportParameters(ecParameters);
35-
}
36-
37-
protected override ECParameters ExportParameters(ECDsa key, bool includePrivate)
38-
{
39-
return key.ExportParameters(includePrivate);
40-
}
41-
12+
protected override ECDsa CreateKey() => ECDsaFactory.Create();
4213
protected override void Exercise(ECDsa key) => key.Exercise();
4314
}
4415
}

src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/ECDsa/ECDsaKeyPemTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,5 @@ namespace System.Security.Cryptography.EcDsa.Tests
1010
public sealed class ECDsaKeyPemTests : ECKeyPemTests<ECDsa>
1111
{
1212
protected override ECDsa CreateKey() => ECDsa.Create();
13-
protected override ECParameters ExportParameters(ECDsa key, bool includePrivateParameters) =>
14-
key.ExportParameters(includePrivateParameters);
1513
}
1614
}

0 commit comments

Comments
 (0)