99namespace 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 (
0 commit comments