diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/EncryptDecrypt.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/EncryptDecrypt.cs index 0aaffebe542ab0..c5915b4ecac7c5 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/EncryptDecrypt.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/EncryptDecrypt.cs @@ -10,7 +10,7 @@ namespace System.Security.Cryptography.Rsa.Tests { - public sealed class EncryptDecrypt_Array : EncryptDecrypt + public abstract class EncryptDecrypt_Array : EncryptDecrypt where TProvider : IRSAProvider, new() { protected override byte[] Encrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding) => rsa.Encrypt(data, padding); @@ -20,7 +20,7 @@ protected override byte[] Decrypt(RSA rsa, byte[] data, RSAEncryptionPadding pad [Fact] public void NullArray_Throws() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { AssertExtensions.Throws("data", () => rsa.Encrypt(null, RSAEncryptionPadding.OaepSHA1)); AssertExtensions.Throws("data", () => rsa.Decrypt(null, RSAEncryptionPadding.OaepSHA1)); @@ -29,9 +29,18 @@ public void NullArray_Throws() } [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public abstract class EncryptDecrypt + public abstract class EncryptDecrypt where TProvider : IRSAProvider, new() { - public static bool SupportsSha2Oaep => RSAFactory.SupportsSha2Oaep; + protected static readonly TProvider s_provider = new TProvider(); + + public static bool SupportsSha2Oaep => s_provider.SupportsSha2Oaep; + + protected static RSA CreateRSA(RSAParameters rsaParameters) + { + RSA rsa = s_provider.Create(); + rsa.ImportParameters(rsaParameters); + return rsa; + } protected abstract byte[] Encrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding); protected abstract byte[] Decrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding); @@ -39,7 +48,7 @@ public abstract class EncryptDecrypt [Fact] public void NullPadding_Throws() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { AssertExtensions.Throws("padding", () => Encrypt(rsa, TestData.HelloBytes, null)); AssertExtensions.Throws("padding", () => Decrypt(rsa, TestData.HelloBytes, null)); @@ -51,7 +60,7 @@ public void NullPadding_Throws() [InlineData(true)] public void UseAfterDispose(bool importKey) { - RSA rsa = importKey ? RSAFactory.Create(TestData.RSA2048Params) : RSAFactory.Create(1024); + RSA rsa = importKey ? CreateRSA(TestData.RSA2048Params) : s_provider.Create(1024); byte[] data = TestData.HelloBytes; byte[] enc; @@ -89,7 +98,7 @@ public void DecryptSavedAnswer() byte[] output; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.RSA1024Params); output = Decrypt(rsa, cipherBytes, RSAEncryptionPadding.OaepSHA1); @@ -121,7 +130,7 @@ public void DecryptWithPublicKey_Fails() 0x8A, 0x9C, 0xCD, 0x58, 0x1A, 0x27, 0x79, 0x97, }; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { RSAParameters parameters = TestData.RSA1024Params; RSAParameters pubParameters = new RSAParameters @@ -152,11 +161,11 @@ public void DecryptSavedAnswer_OaepSHA256() byte[] output; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.RSA2048Params); - if (RSAFactory.SupportsSha2Oaep) + if (s_provider.SupportsSha2Oaep) { output = Decrypt(rsa, cipherBytes, RSAEncryptionPadding.OaepSHA256); } @@ -213,11 +222,11 @@ public void DecryptSavedAnswer_OaepSHA384() byte[] output; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.RSA2048Params); - if (RSAFactory.SupportsSha2Oaep) + if (s_provider.SupportsSha2Oaep) { output = Decrypt(rsa, cipherBytes, RSAEncryptionPadding.OaepSHA384); } @@ -248,11 +257,11 @@ public void DecryptSavedAnswer_OaepSHA512() byte[] output; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.RSA2048Params); - if (RSAFactory.SupportsSha2Oaep) + if (s_provider.SupportsSha2Oaep) { output = Decrypt(rsa, cipherBytes, RSAEncryptionPadding.OaepSHA512); } @@ -293,7 +302,7 @@ public void DecryptSavedAnswerUnusualExponent() byte[] output; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.UnusualExponentParameters); output = Decrypt(rsa, cipherBytes, RSAEncryptionPadding.OaepSHA1); @@ -307,34 +316,34 @@ public void DecryptSavedAnswerUnusualExponent() [Fact] public void RsaCryptRoundtrip_OaepSHA256() => - RsaCryptRoundtrip(RSAEncryptionPadding.OaepSHA256, RSAFactory.SupportsSha2Oaep); + RsaCryptRoundtrip(RSAEncryptionPadding.OaepSHA256, s_provider.SupportsSha2Oaep); [Fact] public void RsaCryptRoundtrip_OaepSHA384() => - RsaCryptRoundtrip(RSAEncryptionPadding.OaepSHA384, RSAFactory.SupportsSha2Oaep); + RsaCryptRoundtrip(RSAEncryptionPadding.OaepSHA384, s_provider.SupportsSha2Oaep); [Fact] public void RsaCryptRoundtrip_OaepSHA512() => - RsaCryptRoundtrip(RSAEncryptionPadding.OaepSHA512, RSAFactory.SupportsSha2Oaep); + RsaCryptRoundtrip(RSAEncryptionPadding.OaepSHA512, s_provider.SupportsSha2Oaep); [Fact] public void RsaCryptRoundtrip_OaepSHA3_256() => - RsaCryptRoundtrip(RSAEncryptionPadding.OaepSHA3_256, RSAFactory.SupportsSha3); + RsaCryptRoundtrip(RSAEncryptionPadding.OaepSHA3_256, s_provider.SupportsSha3); [Fact] public void RsaCryptRoundtrip_OaepSHA3_384() => - RsaCryptRoundtrip(RSAEncryptionPadding.OaepSHA3_384, RSAFactory.SupportsSha3); + RsaCryptRoundtrip(RSAEncryptionPadding.OaepSHA3_384, s_provider.SupportsSha3); [Fact] public void RsaCryptRoundtrip_OaepSHA3_512() => - RsaCryptRoundtrip(RSAEncryptionPadding.OaepSHA3_512, RSAFactory.SupportsSha3); + RsaCryptRoundtrip(RSAEncryptionPadding.OaepSHA3_512, s_provider.SupportsSha3); private void RsaCryptRoundtrip(RSAEncryptionPadding paddingMode, bool expectSuccess = true) { byte[] crypt; byte[] output; - using (RSA rsa = RSAFactory.Create(2048)) + using (RSA rsa = s_provider.Create(2048)) { if (!expectSuccess) { @@ -359,7 +368,7 @@ private void RsaCryptRoundtrip(RSAEncryptionPadding paddingMode, bool expectSucc [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] public void RoundtripEmptyArray() { - using (RSA rsa = RSAFactory.Create(TestData.RSA2048Params)) + using (RSA rsa = CreateRSA(TestData.RSA2048Params)) { void RoundtripEmpty(RSAEncryptionPadding paddingMode) { @@ -372,14 +381,14 @@ void RoundtripEmpty(RSAEncryptionPadding paddingMode) RoundtripEmpty(RSAEncryptionPadding.Pkcs1); RoundtripEmpty(RSAEncryptionPadding.OaepSHA1); - if (RSAFactory.SupportsSha2Oaep) + if (s_provider.SupportsSha2Oaep) { RoundtripEmpty(RSAEncryptionPadding.OaepSHA256); RoundtripEmpty(RSAEncryptionPadding.OaepSHA384); RoundtripEmpty(RSAEncryptionPadding.OaepSHA512); } - if (RSAFactory.SupportsSha3) + if (s_provider.SupportsSha3) { RoundtripEmpty(RSAEncryptionPadding.OaepSHA3_256); RoundtripEmpty(RSAEncryptionPadding.OaepSHA3_384); @@ -393,7 +402,7 @@ public void RsaPkcsEncryptMaxSize() { RSAParameters rsaParameters = TestData.RSA2048Params; - using (RSA rsa = RSAFactory.Create(rsaParameters)) + using (RSA rsa = CreateRSA(rsaParameters)) { RSAEncryptionPadding paddingMode1 = RSAEncryptionPadding.Pkcs1; // The overhead required is 8 + 3 => 11. @@ -419,7 +428,7 @@ public void RsaOaepMaxSize() { RSAParameters rsaParameters = TestData.RSA2048Params; - using (RSA rsa = RSAFactory.Create(rsaParameters)) + using (RSA rsa = CreateRSA(rsaParameters)) { void Test(RSAEncryptionPadding paddingMode, int hashSizeInBits) { @@ -442,7 +451,7 @@ void Test(RSAEncryptionPadding paddingMode, int hashSizeInBits) Test(RSAEncryptionPadding.OaepSHA1, 160); - if (RSAFactory.SupportsSha2Oaep) + if (s_provider.SupportsSha2Oaep) { Test(RSAEncryptionPadding.OaepSHA256, 256); Test(RSAEncryptionPadding.OaepSHA384, 384); @@ -466,7 +475,7 @@ public void RsaDecryptOaep_ExpectFailure() "0176AFB1D3A5AE474B708B882ACA88447046E13D44E5EA8D66421DFC177A683B" + "7B395F18886AAFD9CED072079739ED1D390354976D188C50A29AAD58784886E6").HexToByteArray(); - using (RSA rsa = RSAFactory.Create(TestData.RSA2048Params)) + using (RSA rsa = CreateRSA(TestData.RSA2048Params)) { Assert.ThrowsAny( () => Decrypt(rsa, encrypted, RSAEncryptionPadding.OaepSHA384)); @@ -476,7 +485,7 @@ public void RsaDecryptOaep_ExpectFailure() [ConditionalFact(nameof(SupportsSha2Oaep))] public void RsaDecryptOaepWrongAlgorithm() { - using (RSA rsa = RSAFactory.Create(TestData.RSA2048Params)) + using (RSA rsa = CreateRSA(TestData.RSA2048Params)) { byte[] data = TestData.HelloBytes; byte[] encrypted = Encrypt(rsa, data, RSAEncryptionPadding.OaepSHA256); @@ -489,7 +498,7 @@ public void RsaDecryptOaepWrongAlgorithm() [Fact] public void RsaDecryptOaepWrongData() { - using (RSA rsa = RSAFactory.Create(TestData.RSA2048Params)) + using (RSA rsa = CreateRSA(TestData.RSA2048Params)) { byte[] data = TestData.HelloBytes; byte[] encrypted = Encrypt(rsa, data, RSAEncryptionPadding.OaepSHA1); @@ -498,7 +507,7 @@ public void RsaDecryptOaepWrongData() Assert.ThrowsAny( () => Decrypt(rsa, encrypted, RSAEncryptionPadding.OaepSHA1)); - if (RSAFactory.SupportsSha2Oaep) + if (s_provider.SupportsSha2Oaep) { encrypted = Encrypt(rsa, data, RSAEncryptionPadding.OaepSHA256); encrypted[1] ^= 0xFF; @@ -524,7 +533,7 @@ public void RsaDecryptPkcs1LeadingZero() "0079CEFA8972F02D05C4204078BD9ADF98571CE5374AB94BF01918F0EA31A815" + "59F065A4C3FA0DD0E3086530608CA54387F86F25ED77D46C7576376B64BE3C91").HexToByteArray(); - using (RSA rsa = RSAFactory.Create(TestData.RSA2048Params)) + using (RSA rsa = CreateRSA(TestData.RSA2048Params)) { byte[] decrypted = Decrypt(rsa, encrypted, RSAEncryptionPadding.Pkcs1); Assert.Equal(TestData.HelloBytes, decrypted); @@ -552,7 +561,7 @@ public void RsaDecryptPkcs1Deficient() byte[] correctlyPadded = new byte[encrypted.Length + 1]; Buffer.BlockCopy(encrypted, 0, correctlyPadded, 1, encrypted.Length); - using (RSA rsa = RSAFactory.Create(TestData.RSA2048Params)) + using (RSA rsa = CreateRSA(TestData.RSA2048Params)) { byte[] decrypted = Decrypt(rsa, correctlyPadded, RSAEncryptionPadding.Pkcs1); Assert.NotNull(decrypted); @@ -565,7 +574,7 @@ public void RsaDecryptPkcs1Deficient() [Fact] public void RsaDecryptPkcs1WrongDataLength() { - using (RSA rsa = RSAFactory.Create(TestData.RSA2048Params)) + using (RSA rsa = CreateRSA(TestData.RSA2048Params)) { byte[] data = TestData.HelloBytes; @@ -590,7 +599,7 @@ public void RsaDecryptPkcs1WrongDataLength() [Fact] public void RsaDecryptOaepWrongDataLength() { - using (RSA rsa = RSAFactory.Create(TestData.RSA2048Params)) + using (RSA rsa = CreateRSA(TestData.RSA2048Params)) { byte[] data = TestData.HelloBytes; @@ -608,7 +617,7 @@ public void RsaDecryptOaepWrongDataLength() Assert.ThrowsAny( () => Decrypt(rsa, encrypted, RSAEncryptionPadding.OaepSHA1)); - if (RSAFactory.SupportsSha2Oaep) + if (s_provider.SupportsSha2Oaep) { encrypted = Encrypt(rsa, data, RSAEncryptionPadding.OaepSHA256); Array.Resize(ref encrypted, encrypted.Length + 1); @@ -632,7 +641,7 @@ public void RsaDecryptAfterExport() { byte[] output; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { byte[] crypt = Encrypt(rsa, TestData.HelloBytes, RSAEncryptionPadding.OaepSHA1); @@ -649,7 +658,7 @@ public void LargeKeyCryptRoundtrip() { byte[] output; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { try { @@ -677,7 +686,7 @@ public void UnusualExponentCryptRoundtrip() byte[] crypt; byte[] output; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.UnusualExponentParameters); @@ -696,7 +705,7 @@ public void NonPowerOfTwoKeySizeOaepRoundtrip(RSAEncryptionPadding oaepPaddingMo byte[] crypt; byte[] output; - using (RSA rsa = RSAFactory.Create(3072)) + using (RSA rsa = s_provider.Create(3072)) { crypt = Encrypt(rsa, TestData.HelloBytes, oaepPaddingMode); output = Decrypt(rsa, crypt, oaepPaddingMode); @@ -709,7 +718,7 @@ public void NonPowerOfTwoKeySizeOaepRoundtrip(RSAEncryptionPadding oaepPaddingMo [Fact] public void NotSupportedValueMethods() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { #pragma warning disable SYSLIB0048 Assert.Throws(() => rsa.DecryptValue(null)); @@ -739,7 +748,7 @@ public void Decrypt_Pkcs1_BadPadding() ref byte lastByte = ref buf[^1]; afterMinPadding = 0; - using (RSA rsa = RSAFactory.Create(keyParams)) + using (RSA rsa = CreateRSA(keyParams)) { RawEncrypt(buf, e, n, c); // Assert.NoThrow, check that manual padding is coherent @@ -837,14 +846,14 @@ public static IEnumerable OaepPaddingModes { yield return new object[] { RSAEncryptionPadding.OaepSHA1 }; - if (RSAFactory.SupportsSha2Oaep) + if (s_provider.SupportsSha2Oaep) { yield return new object[] { RSAEncryptionPadding.OaepSHA256 }; yield return new object[] { RSAEncryptionPadding.OaepSHA384 }; yield return new object[] { RSAEncryptionPadding.OaepSHA512 }; } - if (RSAFactory.SupportsSha3) + if (s_provider.SupportsSha3) { yield return new object[] { RSAEncryptionPadding.OaepSHA3_256 }; yield return new object[] { RSAEncryptionPadding.OaepSHA3_384 }; diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/EncryptDecrypt.netcoreapp.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/EncryptDecrypt.netcoreapp.cs index 6c926659ae4fd6..258e15e495a67b 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/EncryptDecrypt.netcoreapp.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/EncryptDecrypt.netcoreapp.cs @@ -7,7 +7,7 @@ namespace System.Security.Cryptography.Rsa.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public sealed class EncryptDecrypt_Span : EncryptDecrypt + public abstract class EncryptDecrypt_Span : EncryptDecrypt where TProvider : IRSAProvider, new() { protected override byte[] Encrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding) => WithOutputArray(dest => rsa.Encrypt(data, dest, padding)); @@ -36,7 +36,7 @@ private static byte[] WithOutputArray(Func func) } [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public sealed class EncryptDecrypt_AllocatingSpan : EncryptDecrypt + public abstract class EncryptDecrypt_AllocatingSpan : EncryptDecrypt where TProvider : IRSAProvider, new() { protected override byte[] Encrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding) => rsa.Encrypt(new ReadOnlySpan(data), padding); @@ -46,7 +46,7 @@ protected override byte[] Decrypt(RSA rsa, byte[] data, RSAEncryptionPadding pad } [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public sealed class EncryptDecrypt_TrySpan : EncryptDecrypt + public abstract class EncryptDecrypt_TrySpan : EncryptDecrypt where TProvider : IRSAProvider, new() { protected override byte[] Encrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding) => TryWithOutputArray(dest => rsa.TryEncrypt(data, dest, padding, out int bytesWritten) ? (true, bytesWritten) : (false, 0)); @@ -71,7 +71,7 @@ private static byte[] TryWithOutputArray(Func func) [Fact] public void Decrypt_VariousSizeSpans_Success() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.RSA1024Params); byte[] cipherBytes = Encrypt(rsa, TestData.HelloBytes, RSAEncryptionPadding.OaepSHA1); @@ -103,7 +103,7 @@ public void Decrypt_VariousSizeSpans_Success() [Fact] public void Encrypt_VariousSizeSpans_Success() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.RSA1024Params); byte[] cipherBytes = Encrypt(rsa, TestData.HelloBytes, RSAEncryptionPadding.OaepSHA1); @@ -148,7 +148,7 @@ public void Decrypt_WrongKey_OAEP_SHA256() [Fact] public static void EncryptDefaultSpan() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { byte[] dest = new byte[rsa.KeySize / 8]; @@ -166,8 +166,8 @@ public static void EncryptDefaultSpan() private static void Decrypt_WrongKey(RSAEncryptionPadding padding) { - using (RSA rsa1 = RSAFactory.Create()) - using (RSA rsa2 = RSAFactory.Create()) + using (RSA rsa1 = s_provider.Create()) + using (RSA rsa2 = s_provider.Create()) { byte[] input = TestData.HelloBytes; byte[] encrypted = rsa1.Encrypt(input, padding); diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/ImportExport.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/ImportExport.cs index 909c455a8134b2..39afabba1fc620 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/ImportExport.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/ImportExport.cs @@ -8,8 +8,10 @@ namespace System.Security.Cryptography.Rsa.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public partial class ImportExport + public abstract partial class ImportExport where TProvider : IRSAProvider, new() { + private static readonly TProvider s_provider = new TProvider(); + public static bool Supports16384 { get; } = TestRsa16384(); [Fact] @@ -19,7 +21,7 @@ public static void ExportAutoKey() RSAParameters publicParams; int keySize; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { keySize = rsa.KeySize; @@ -58,7 +60,7 @@ public static void PaddedExport() RSAParameters diminishedDPParameters = TestData.DiminishedDPParameters; RSAParameters exported; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(diminishedDPParameters); exported = rsa.ExportParameters(true); @@ -74,7 +76,7 @@ public static void LargeKeyImportExport() { RSAParameters imported = TestData.RSA16384Params; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { try { @@ -111,7 +113,7 @@ public static void UnusualExponentImportExport() RSAParameters unusualExponentParameters = TestData.UnusualExponentParameters; RSAParameters exported; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(unusualExponentParameters); exported = rsa.ExportParameters(true); @@ -129,7 +131,7 @@ public static void ImportExport1032() RSAParameters exported; RSAParameters exportedPublic; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(imported); exported = rsa.ExportParameters(true); @@ -146,7 +148,7 @@ public static void ImportExport1032() [Fact] public static void ImportReset() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { RSAParameters exported = rsa.ExportParameters(true); RSAParameters imported; @@ -178,7 +180,7 @@ public static void ImportPrivateExportPublic() { RSAParameters imported = TestData.RSA1024Params; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(imported); @@ -196,7 +198,7 @@ public static void MultiExport() { RSAParameters imported = TestData.RSA1024Params; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(imported); @@ -231,7 +233,7 @@ public static void PublicOnlyPrivateExport() Exponent = TestData.RSA1024Params.Exponent, }; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(imported); Assert.ThrowsAny(() => rsa.ExportParameters(true)); @@ -246,7 +248,7 @@ public static void ImportNoExponent() Modulus = TestData.RSA1024Params.Modulus, }; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { if (rsa is RSACng && PlatformDetection.IsNetFramework) AssertExtensions.Throws(null, () => rsa.ImportParameters(imported)); @@ -263,7 +265,7 @@ public static void ImportNoModulus() Exponent = TestData.RSA1024Params.Exponent, }; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { if (rsa is RSACng && PlatformDetection.IsNetFramework) AssertExtensions.Throws(null, () => rsa.ImportParameters(imported)); @@ -283,7 +285,7 @@ public static void ImportNoDP() RSAParameters imported = TestData.RSA1024Params; imported.DP = null; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { Assert.ThrowsAny(() => rsa.ImportParameters(imported)); } @@ -294,7 +296,7 @@ public static void ImportNoDP() [InlineData(false)] public static void ExportAfterDispose(bool importKey) { - RSA rsa = importKey ? RSAFactory.Create(TestData.RSA2048Params) : RSAFactory.Create(1024); + RSA rsa = importKey ? CreateRSA(TestData.RSA2048Params) : s_provider.Create(1024); // Ensure that the key got created, and then Dispose it. using (rsa) @@ -324,7 +326,7 @@ public static void ImportZeroModulus(bool includePrivateParameters) zeroModulus = MakePublic(zeroModulus); } - Assert.ThrowsAny(() => RSAFactory.Create(zeroModulus)); + Assert.ThrowsAny(() => CreateRSA(zeroModulus)); } internal static void ValidateParameters(ref RSAParameters rsaParams) @@ -365,11 +367,18 @@ internal static RSAParameters MakePublic(in RSAParameters rsaParams) }; } + private static RSA CreateRSA(RSAParameters rsaParameters) + { + RSA rsa = s_provider.Create(); + rsa.ImportParameters(rsaParameters); + return rsa; + } + private static bool TestRsa16384() { try { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.RSA16384Params); } diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/KeyGeneration.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/KeyGeneration.cs index f69cf0c1ec5fd9..6c9fe33e90ca2f 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/KeyGeneration.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/KeyGeneration.cs @@ -6,8 +6,9 @@ namespace System.Security.Cryptography.Rsa.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public class KeyGeneration + public abstract class KeyGeneration where TProvider : IRSAProvider, new() { + private static readonly TProvider s_provider = new TProvider(); [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotSymCryptOpenSsl))] public static void GenerateMinKey() { @@ -47,19 +48,19 @@ private static void GenerateKey(Func getSize) { int keySize; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { keySize = getSize(rsa); } - using (RSA rsa = RSAFactory.Create(keySize)) + using (RSA rsa = s_provider.Create(keySize)) { Assert.Equal(keySize, rsa.KeySize); // Some providers may generate the key in the constructor, but // all of them should have generated it before answering ExportParameters. RSAParameters keyParameters = rsa.ExportParameters(false); - ImportExport.ValidateParameters(ref keyParameters); + ImportExport.ValidateParameters(ref keyParameters); // KeySize should still be what we set it to originally. Assert.Equal(keySize, rsa.KeySize); diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAFactory.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAFactory.cs index a01390dec6c982..752ac20a1b18d6 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAFactory.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAFactory.cs @@ -15,38 +15,4 @@ public interface IRSAProvider bool SupportsMd5Signatures { get; } bool SupportsSha3 { get; } } - - public static partial class RSAFactory - { - public static RSA Create() - { - return s_provider.Create(); - } - - public static RSA Create(int keySize) - { - return s_provider.Create(keySize); - } - - public static RSA Create(RSAParameters rsaParameters) - { - RSA rsa = Create(); - rsa.ImportParameters(rsaParameters); - return rsa; - } - - public static bool Supports384PrivateKey => s_provider.Supports384PrivateKey; - - public static bool SupportsLargeExponent => s_provider.SupportsLargeExponent; - - public static bool SupportsSha2Oaep => s_provider.SupportsSha2Oaep; - - public static bool SupportsPss => s_provider.SupportsPss; - - public static bool SupportsSha1Signatures => s_provider.SupportsSha1Signatures; - public static bool SupportsMd5Signatures => s_provider.SupportsMd5Signatures; - - public static bool SupportsSha3 => s_provider.SupportsSha3; - public static bool NoSupportsSha3 => !SupportsSha3; - } } diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAFactoryTests.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAFactoryTests.cs index f08a584709aa9e..6c1fcce793089b 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAFactoryTests.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAFactoryTests.cs @@ -6,34 +6,43 @@ namespace System.Security.Cryptography.Rsa.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public static class RSAFactoryTests + public abstract class RSAFactoryTests where TProvider : IRSAProvider, new() { + private static readonly TProvider s_provider = new TProvider(); + + private static RSA CreateRSA(RSAParameters rsaParameters) + { + RSA rsa = s_provider.Create(); + rsa.ImportParameters(rsaParameters); + return rsa; + } + [Fact] public static void RSACreateDefault_Equals_SameInstance() { - using RSA rsa = RSAFactory.Create(); + using RSA rsa = s_provider.Create(); AssertExtensions.TrueExpression(rsa.Equals(rsa)); } [Fact] public static void RSACreateKeySize_Equals_SameInstance() { - using RSA rsa = RSAFactory.Create(2048); + using RSA rsa = s_provider.Create(2048); AssertExtensions.TrueExpression(rsa.Equals(rsa)); } [Fact] public static void RSACreateParameters_Equals_SameInstance() { - using RSA rsa = RSAFactory.Create(TestData.RSA2048Params); + using RSA rsa = CreateRSA(TestData.RSA2048Params); AssertExtensions.TrueExpression(rsa.Equals(rsa)); } [Fact] public static void RSACreateParameters_Equals_DifferentInstance_FalseForSameKeyMaterial() { - using RSA rsa1 = RSAFactory.Create(TestData.RSA2048Params); - using RSA rsa2 = RSAFactory.Create(TestData.RSA2048Params); + using RSA rsa1 = CreateRSA(TestData.RSA2048Params); + using RSA rsa2 = CreateRSA(TestData.RSA2048Params); AssertExtensions.FalseExpression(rsa1.Equals(rsa2)); } } diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAKeyExchangeFormatter.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAKeyExchangeFormatter.cs index 3e9071c25c8bea..d112f00dada211 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAKeyExchangeFormatter.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAKeyExchangeFormatter.cs @@ -8,12 +8,20 @@ namespace System.Security.Cryptography.Rsa.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public partial class RSAKeyExchangeFormatterTests + public abstract partial class RSAKeyExchangeFormatterTests where TProvider : IRSAProvider, new() { + private static readonly TProvider s_provider = new TProvider(); + + private static RSA CreateRSA(RSAParameters rsaParameters) + { + RSA rsa = s_provider.Create(); + rsa.ImportParameters(rsaParameters); + return rsa; + } [Fact] public static void VerifyDecryptKeyExchangeOaep() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.RSA2048Params); @@ -26,7 +34,7 @@ public static void VerifyDecryptKeyExchangeOaep() [Fact] public static void VerifyDecryptKeyExchangePkcs1() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.RSA2048Params); @@ -39,7 +47,7 @@ public static void VerifyDecryptKeyExchangePkcs1() [Fact] public static void TestKnownValueOaep() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.RSA1024Params); byte[] encrypted = @@ -56,7 +64,7 @@ public static void TestKnownValueOaep() [Fact] public static void TestKnownValuePkcs1() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.RSA1024Params); byte[] encrypted = diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAKeyFileTests.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAKeyFileTests.cs index 6ac8f53aa78385..6531f33fbfce37 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAKeyFileTests.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAKeyFileTests.cs @@ -9,17 +9,28 @@ namespace System.Security.Cryptography.Rsa.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public static class RSAKeyFileTests + public abstract class RSAKeyFileTests where TProvider : IRSAProvider, new() { - public static bool Supports384BitPrivateKeyAndRC2 { get; } = RSAFactory.Supports384PrivateKey && RC2Factory.IsSupported; - public static bool SupportsLargeExponent { get; } = RSAFactory.SupportsLargeExponent; + private static readonly TProvider s_provider = new TProvider(); + + private static RSA CreateRSA(RSAParameters rsaParameters) + { + RSA rsa = s_provider.Create(); + rsa.ImportParameters(rsaParameters); + return rsa; + } + + public static bool Supports384BitPrivateKeyAndRC2 { get; } = s_provider.Supports384PrivateKey && RC2Factory.IsSupported; + public static bool SupportsLargeExponent { get; } = s_provider.SupportsLargeExponent; + public static bool Supports16384 => ImportExport.Supports16384; + public static bool RC2IsSupported => RC2Factory.IsSupported; [Theory] [InlineData(false)] [InlineData(true)] public static void UseAfterDispose(bool importKey) { - RSA rsa = importKey ? RSAFactory.Create(TestData.RSA2048Params) : RSAFactory.Create(1024); + RSA rsa = importKey ? CreateRSA(TestData.RSA2048Params) : s_provider.Create(1024); byte[] pkcs1Public; byte[] pkcs1Private; byte[] pkcs8Private; @@ -122,7 +133,7 @@ public static void ReadWriteDiminishedDPPrivatePkcs1() TestData.DiminishedDPParameters); } - [ConditionalFact(typeof(ImportExport), nameof(ImportExport.Supports16384))] + [ConditionalFact(nameof(Supports16384))] public static void ReadWritePublicPkcs1() { ReadWriteBase64PublicPkcs1( @@ -198,7 +209,7 @@ public static void ReadWriteSubjectPublicKeyInfo_DiminishedDPKey() TestData.DiminishedDPParameters); } - [ConditionalFact(typeof(ImportExport), nameof(ImportExport.Supports16384))] + [ConditionalFact(nameof(Supports16384))] public static void ReadWriteRsa16384SubjectPublicKeyInfo() { ReadWriteBase64SubjectPublicKeyInfo( @@ -250,7 +261,7 @@ public static void ReadWriteRsa16384SubjectPublicKeyInfo() TestData.RSA16384Params); } - [ConditionalFact(typeof(ImportExport), nameof(ImportExport.Supports16384))] + [ConditionalFact(nameof(Supports16384))] public static void ReadWrite16384Pkcs8() { ReadWriteBase64Pkcs8( @@ -525,7 +536,7 @@ public static void ReadEncryptedRsa1032() TestData.RSA1032Parameters); } - [ConditionalFact(typeof(ImportExport), nameof(ImportExport.Supports16384))] + [ConditionalFact(nameof(Supports16384))] public static void ReadEncryptedRsa16384() { // PBES2: PBKDF2 + des (single DES, not 3DES). @@ -736,7 +747,7 @@ public static void ReadEncryptedRsa16384() TestData.RSA16384Params); } - [ConditionalFact(typeof(RC2Factory), nameof(RC2Factory.IsSupported))] + [ConditionalFact(nameof(RC2IsSupported))] public static void ReadPbes2Rc2EncryptedDiminishedDP() { // PBES2: PBKDF2 + RC2-128 @@ -762,7 +773,7 @@ public static void ReadPbes2Rc2EncryptedDiminishedDP() TestData.DiminishedDPParameters); } - [ConditionalFact(typeof(RC2Factory), nameof(RC2Factory.IsSupported))] + [ConditionalFact(nameof(RC2IsSupported))] public static void ReadPbes2Rc2EncryptedDiminishedDP_PasswordBytes() { // PBES2: PBKDF2 + RC2-128 @@ -866,7 +877,7 @@ public static void ReadPbes1Rc2EncryptedRsa384() [Fact] public static void NoFuzzyRSAPublicKey() { - using (RSA key = RSAFactory.Create()) + using (RSA key = s_provider.Create()) { int bytesRead = -1; byte[] rsaPriv = key.ExportRSAPrivateKey(); @@ -909,7 +920,7 @@ public static void NoFuzzyRSAPublicKey() [Fact] public static void NoFuzzySubjectPublicKeyInfo() { - using (RSA key = RSAFactory.Create()) + using (RSA key = s_provider.Create()) { int bytesRead = -1; byte[] rsaPriv = key.ExportRSAPrivateKey(); @@ -952,7 +963,7 @@ public static void NoFuzzySubjectPublicKeyInfo() [Fact] public static void NoFuzzyRSAPrivateKey() { - using (RSA key = RSAFactory.Create()) + using (RSA key = s_provider.Create()) { int bytesRead = -1; byte[] rsaPub = key.ExportRSAPublicKey(); @@ -995,7 +1006,7 @@ public static void NoFuzzyRSAPrivateKey() [Fact] public static void NoFuzzyPkcs8() { - using (RSA key = RSAFactory.Create()) + using (RSA key = s_provider.Create()) { int bytesRead = -1; @@ -1039,7 +1050,7 @@ public static void NoFuzzyPkcs8() [Fact] public static void NoFuzzyEncryptedPkcs8() { - using (RSA key = RSAFactory.Create()) + using (RSA key = s_provider.Create()) { int bytesRead = -1; byte[] empty = Array.Empty(); @@ -1076,7 +1087,7 @@ public static void NoFuzzyEncryptedPkcs8() [Fact] public static void NoPrivKeyFromPublicOnly() { - using (RSA key = RSAFactory.Create()) + using (RSA key = s_provider.Create()) { RSAParameters srcParameters = TestData.RSA2048Params; RSAParameters rsaParameters = new RSAParameters @@ -1116,7 +1127,7 @@ public static void NoPrivKeyFromPublicOnly() [Fact] public static void BadPbeParameters() { - using (RSA key = RSAFactory.Create()) + using (RSA key = s_provider.Create()) { Assert.ThrowsAny( () => key.ExportEncryptedPkcs8PrivateKey( @@ -1238,7 +1249,7 @@ public static void BadPbeParameters() [Fact] public static void DecryptPkcs12WithBytes() { - using (RSA key = RSAFactory.Create()) + using (RSA key = s_provider.Create()) { string charBased = "hello"; byte[] byteBased = Encoding.UTF8.GetBytes(charBased); @@ -1285,7 +1296,7 @@ public static void DecryptPkcs12PbeTooManyIterations() i33DDR38LaRqG9ho3brf466OkNooBv4MpD5SA63yfooytxOgeuaqbuTzKP/OSRqJNab9wctA9nfJ gms2YM+honjUS1sXk1zdm/8="); - using (RSA key = RSAFactory.Create()) + using (RSA key = s_provider.Create()) { Assert.ThrowsAny( () => key.ImportEncryptedPkcs8PrivateKey((ReadOnlySpan)"test", high3DesIterationKey, out _)); @@ -1446,7 +1457,7 @@ private static void ReadWriteKey( const int OverAllocate = 30; const int WriteShift = 6; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { readAction(rsa, derBytes, out int bytesRead); Assert.Equal(derBytes.Length, bytesRead); @@ -1466,7 +1477,7 @@ private static void ReadWriteKey( Assert.Equal(derBytes.ByteArrayToHex(), arrayExport.ByteArrayToHex()); } - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { Assert.ThrowsAny( () => readAction(rsa, arrayExport.AsSpan(1), out _)); @@ -1521,7 +1532,7 @@ private static void ReadWriteKey( } } - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { readAction(rsa, tooBig.AsSpan(WriteShift), out int bytesRead); Assert.Equal(arrayExport.Length, bytesRead); diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAKeyPemTests.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAKeyPemTests.cs index 1ef844ac1d0890..be568086c84147 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAKeyPemTests.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAKeyPemTests.cs @@ -8,8 +8,10 @@ namespace System.Security.Cryptography.Rsa.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public static class RSAKeyPemTests + public abstract class RSAKeyPemTests where TProvider : IRSAProvider, new() { + private static readonly TProvider s_provider = new TProvider(); + private const string AmbiguousExceptionMarker = "multiple keys"; private const string EncryptedExceptionMarker = "encrypted key"; private const string NoPemExceptionMarker = "No supported key"; @@ -17,7 +19,7 @@ public static class RSAKeyPemTests [Fact] public static void ImportFromPem_NoPem() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @"these aren't the PEMs you're looking for"; ArgumentException ae = AssertExtensions.Throws("input", () => rsa.ImportFromPem(pem)); @@ -28,7 +30,7 @@ public static void ImportFromPem_NoPem() [Fact] public static void ImportFromPem_RSAPrivateKey_Simple() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @" -----BEGIN RSA PRIVATE KEY----- @@ -51,7 +53,7 @@ public static void ImportFromPem_RSAPrivateKey_Simple() [Fact] public static void ImportFromPem_Pkcs8UnEncrypted_Simple() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @" -----BEGIN PRIVATE KEY----- @@ -75,7 +77,7 @@ public static void ImportFromPem_Pkcs8UnEncrypted_Simple() [Fact] public static void ImportFromPem_Pkcs8UnEncrypted_UnrelatedAlgorithmIsIgnored() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @" -----BEGIN EC PRIVATE KEY----- @@ -104,7 +106,7 @@ public static void ImportFromPem_Pkcs8UnEncrypted_UnrelatedAlgorithmIsIgnored() [Fact] public static void ImportFromPem_SubjectPublicKeyInfo_Simple() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @" -----BEGIN PUBLIC KEY----- @@ -121,7 +123,7 @@ public static void ImportFromPem_SubjectPublicKeyInfo_Simple() [Fact] public static void ImportFromPem_SubjectPublicKeyInfo_IgnoresUnrelatedAlgorithm() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @" -----BEGIN EC PRIVATE KEY----- @@ -143,7 +145,7 @@ public static void ImportFromPem_SubjectPublicKeyInfo_IgnoresUnrelatedAlgorithm( [Fact] public static void ImportFromPem_RSAPublicKey_Simple() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @" -----BEGIN RSA PUBLIC KEY----- @@ -161,7 +163,7 @@ public static void ImportFromPem_RSAPublicKey_Simple() [Fact] public static void ImportFromPem_RSAPrivateKey_PrecedingUnrelatedPem() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @" -----BEGIN CERTIFICATE----- @@ -198,7 +200,7 @@ public static void ImportFromPem_RSAPrivateKey_PrecedingUnrelatedPem() [Fact] public static void ImportFromPem_RSAPrivateKey_PrecedingMalformedPem() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @" -----BEGIN CERTIFICATE----- @@ -223,7 +225,7 @@ public static void ImportFromPem_RSAPrivateKey_PrecedingMalformedPem() [Fact] public static void ImportFromPem_RSAPrivateKey_IgnoresOtherAlgorithms() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @" -----BEGIN EC PRIVATE KEY----- @@ -250,7 +252,7 @@ public static void ImportFromPem_RSAPrivateKey_IgnoresOtherAlgorithms() [Fact] public static void ImportFromPem_RSAPrivateKey_AmbiguousKey_RSAPrivateKey() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @" -----BEGIN RSA PRIVATE KEY----- @@ -273,7 +275,7 @@ public static void ImportFromPem_RSAPrivateKey_AmbiguousKey_RSAPrivateKey() [Fact] public static void ImportFromPem_RSAPrivateKey_AmbiguousKey_SubjectPublicKeyInfo() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @" -----BEGIN PUBLIC KEY----- @@ -296,7 +298,7 @@ public static void ImportFromPem_RSAPrivateKey_AmbiguousKey_SubjectPublicKeyInfo [Fact] public static void ImportFromPem_RSAPrivateKey_AmbiguousKey_RSAPublicKey() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @" -----BEGIN RSA PUBLIC KEY----- @@ -319,7 +321,7 @@ public static void ImportFromPem_RSAPrivateKey_AmbiguousKey_RSAPublicKey() [Fact] public static void ImportFromPem_RSAPrivateKey_AmbiguousKey_EncryptedPkcs8() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @" -----BEGIN ENCRYPTED PRIVATE KEY----- @@ -342,7 +344,7 @@ public static void ImportFromPem_RSAPrivateKey_AmbiguousKey_EncryptedPkcs8() [Fact] public static void ImportFromPem_EncryptedPrivateKeyFails() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @" -----BEGIN ENCRYPTED PRIVATE KEY----- @@ -365,7 +367,7 @@ public static void ImportFromPem_EncryptedPrivateKeyFails() [Fact] public static void ImportFromPem_Pkcs8AlgorithmMismatch_Throws() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @" The below PEM is a 1024-bit DSA key. @@ -385,7 +387,7 @@ The below PEM is a 1024-bit DSA key. [Fact] public static void ImportFromEncryptedPem_Pkcs8Encrypted_Char_Simple() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @" -----BEGIN ENCRYPTED PRIVATE KEY----- @@ -410,7 +412,7 @@ public static void ImportFromEncryptedPem_Pkcs8Encrypted_Char_Simple() [Fact] public static void ImportFromEncryptedPem_Pkcs8Encrypted_Byte_Simple() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @" -----BEGIN ENCRYPTED PRIVATE KEY----- @@ -435,7 +437,7 @@ public static void ImportFromEncryptedPem_Pkcs8Encrypted_Byte_Simple() [Fact] public static void ImportFromEncryptedPem_Pkcs8Encrypted_AmbiguousPem() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @" -----BEGIN ENCRYPTED PRIVATE KEY----- @@ -471,7 +473,7 @@ public static void ImportFromEncryptedPem_Pkcs8Encrypted_AmbiguousPem() [Fact] public static void ImportFromEncryptedPem_Pkcs8Encrypted_Byte_NoPem() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = "these aren't the PEMs we're looking for."; ArgumentException ae = AssertExtensions.Throws("input", () => @@ -483,7 +485,7 @@ public static void ImportFromEncryptedPem_Pkcs8Encrypted_Byte_NoPem() [Fact] public static void ImportFromEncryptedPem_NoEncryptedPem() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = @" -----BEGIN PRIVATE KEY----- @@ -505,7 +507,7 @@ public static void ImportFromEncryptedPem_NoEncryptedPem() [Fact] public static void ImportFromEncryptedPem_Pkcs8Encrypted_Char_NoPem() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { string pem = "go about your business"; string password = "test"; @@ -514,8 +516,11 @@ public static void ImportFromEncryptedPem_Pkcs8Encrypted_Char_NoPem() Assert.Contains(NoPemExceptionMarker, ae.Message); } } + } - private static RSAParameters ToPublic(this RSAParameters rsaParams) + internal static class RSAParametersExtensions + { + internal static RSAParameters ToPublic(this RSAParameters rsaParams) { return new RSAParameters { diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSASignatureFormatter.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSASignatureFormatter.cs index c1ce8e1754b87a..17bb707bd47661 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSASignatureFormatter.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSASignatureFormatter.cs @@ -8,12 +8,23 @@ namespace System.Security.Cryptography.Rsa.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public partial class RSASignatureFormatterTests : AsymmetricSignatureFormatterTests + public abstract partial class RSASignatureFormatterTests : AsymmetricSignatureFormatterTests where TProvider : IRSAProvider, new() { - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + private static readonly TProvider s_provider = new TProvider(); + + public static bool SupportsSha1Signatures => s_provider.SupportsSha1Signatures; + + private static RSA CreateRSA(RSAParameters rsaParameters) + { + RSA rsa = s_provider.Create(); + rsa.ImportParameters(rsaParameters); + return rsa; + } + + [ConditionalFact(nameof(SupportsSha1Signatures))] public static void VerifySignature_SHA1() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.RSA2048Params); @@ -31,7 +42,7 @@ public static void VerifySignature_SHA1() [Fact] public static void VerifySignature_SHA256() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.RSA2048Params); @@ -49,7 +60,7 @@ public static void VerifySignature_SHA256() [Fact] public static void InvalidHashAlgorithm() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { var formatter = new RSAPKCS1SignatureFormatter(rsa); var deformatter = new RSAPKCS1SignatureDeformatter(rsa); @@ -66,13 +77,13 @@ public static void InvalidHashAlgorithm() } } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact(nameof(SupportsSha1Signatures))] public static void VerifyKnownSignature() { byte[] hash = "012d161304fa0c6321221516415813022320620c".HexToByteArray(); byte[] sig; - using (RSA key = RSAFactory.Create()) + using (RSA key = s_provider.Create()) { key.ImportParameters(TestData.RSA1024Params); RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(key); @@ -86,7 +97,7 @@ public static void VerifyKnownSignature() Assert.Equal(expectedSig, sig); } - using (RSA key = RSAFactory.Create()) // Test against a different instance + using (RSA key = s_provider.Create()) // Test against a different instance { key.ImportParameters(TestData.RSA1024Params); RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(key); diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAXml.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAXml.cs index 69389f2adef05d..3b40d615bff9f6 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAXml.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAXml.cs @@ -8,12 +8,22 @@ namespace System.Security.Cryptography.Rsa.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public static class RSAXml + public abstract class RSAXml where TProvider : IRSAProvider, new() { + private static readonly TProvider s_provider = new TProvider(); + + public static bool Supports16384 => ImportExport.Supports16384; + + private static RSA CreateRSA(RSAParameters rsaParameters) + { + RSA rsa = s_provider.Create(); + rsa.ImportParameters(rsaParameters); + return rsa; + } [Fact] public static void TestRead1032Parameters_Public() { - RSAParameters expectedParameters = ImportExport.MakePublic(TestData.RSA1032Parameters); + RSAParameters expectedParameters = ImportExport.MakePublic(TestData.RSA1032Parameters); // Bonus trait of this XML: the elements are all in different namespaces, // showing that isn't part of the reading consideration. @@ -76,10 +86,10 @@ public static void TestRead1032Parameters_Private() TestData.RSA1032Parameters); } - [ConditionalFact(typeof(ImportExport), nameof(ImportExport.Supports16384))] + [ConditionalFact(nameof(Supports16384))] public static void TestRead16384Parameters_Public() { - RSAParameters expectedParameters = ImportExport.MakePublic(TestData.RSA16384Params); + RSAParameters expectedParameters = ImportExport.MakePublic(TestData.RSA16384Params); // Bonus trait of this XML: the Modulus and Exponent parameters // are not in canonical order. @@ -157,7 +167,7 @@ iC2wXFMDafnWp1lxXiGcVVu9dE2LeglCgnMUps9QlJD0aXaJHYi2VDQ3zFdMvn8A imlqKtZGdGf9 expectedParameters); } - [ConditionalFact(typeof(ImportExport), nameof(ImportExport.Supports16384))] + [ConditionalFact(nameof(Supports16384))] public static void TestRead16384Parameters_Private() { // Bonus trait of this XML: the D parameter is not in @@ -386,7 +396,7 @@ public static void TestRead16384Parameters_Private() public static void TestReadDiminishedDPParameters_Public() { RSAParameters expectedParameters = - ImportExport.MakePublic(TestData.DiminishedDPParameters); + ImportExport.MakePublic(TestData.DiminishedDPParameters); TestReadXml( // Bonus trait of this XML: Canonical element order, pretty-printed. @@ -634,7 +644,7 @@ public static void TestWrite2048Parameters(bool includePrivateParameters) )); } - [ConditionalTheory(typeof(ImportExport), nameof(ImportExport.Supports16384))] + [ConditionalTheory(nameof(Supports16384))] [InlineData(true)] [InlineData(false)] public static void TestWrite16384Parameters(bool includePrivateParameters) @@ -984,7 +994,7 @@ public static void TestWriteUnusualExponentParameters(bool includePrivateParamet [Fact] public static void FromToXml() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { RSAParameters pubOnly = rsa.ExportParameters(false); RSAParameters pubPriv = rsa.ExportParameters(true); @@ -992,14 +1002,14 @@ public static void FromToXml() string xmlPub = rsa.ToXmlString(false); string xmlPriv = rsa.ToXmlString(true); - using (RSA rsaPub = RSAFactory.Create()) + using (RSA rsaPub = s_provider.Create()) { rsaPub.FromXmlString(xmlPub); RSATestHelpers.AssertKeyEquals(pubOnly, rsaPub.ExportParameters(false)); } - using (RSA rsaPriv = RSAFactory.Create()) + using (RSA rsaPriv = s_provider.Create()) { rsaPriv.FromXmlString(xmlPriv); @@ -1012,7 +1022,7 @@ public static void FromToXml() [Fact] public static void FromXml_MissingModulus() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { Assert.ThrowsAny( () => rsa.FromXmlString( @@ -1047,7 +1057,7 @@ public static void FromXml_MissingModulus() [Fact] public static void FromXml_MissingExponent() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { Assert.ThrowsAny( () => rsa.FromXmlString( @@ -1085,7 +1095,7 @@ public static void FromXml_MissingExponent() [Fact] public static void FromXml_MissingQ() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { Assert.ThrowsAny( () => rsa.FromXmlString( @@ -1121,7 +1131,7 @@ public static void FromXml_MissingQ() [Fact] public static void FromXml_MissingDP() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { Assert.ThrowsAny( () => rsa.FromXmlString( @@ -1157,7 +1167,7 @@ public static void FromXml_MissingDP() [Fact] public static void FromXml_MissingDQ() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { Assert.ThrowsAny( () => rsa.FromXmlString( @@ -1193,7 +1203,7 @@ public static void FromXml_MissingDQ() [Fact] public static void FromXml_MissingInverseQ() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { Assert.ThrowsAny( () => rsa.FromXmlString( @@ -1229,7 +1239,7 @@ public static void FromXml_MissingInverseQ() [Fact] public static void FromXml_BadBase64() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { // The D value is missing the terminating ==. Assert.Throws( @@ -1267,7 +1277,7 @@ public static void FromXml_BadBase64() private static void TestReadXml(string xmlString, in RSAParameters expectedParameters) { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.FromXmlString(xmlString); Assert.Equal(expectedParameters.Modulus.Length * 8, rsa.KeySize); @@ -1283,7 +1293,7 @@ private static void TestReadXml(string xmlString, in RSAParameters expectedParam [Fact] public static void FromNullXml() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { AssertExtensions.Throws( "xmlString", @@ -1294,7 +1304,7 @@ public static void FromNullXml() [Fact] public static void FromInvalidXml() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { Exception exception = Assert.ThrowsAny( () => rsa.FromXmlString( @@ -1344,7 +1354,7 @@ public static void FromInvalidXml() public static void FromNonsenseXml() { // This is DiminishedDPParameters XML, but with a P that is way too long. - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { Assert.ThrowsAny( () => rsa.FromXmlString( @@ -1397,7 +1407,7 @@ private static void TestWriteXml( { IEnumerator iter; - using (RSA rsa = RSAFactory.Create(keyParameters)) + using (RSA rsa = CreateRSA(keyParameters)) { iter = VerifyRootAndGetChildren(rsa, includePrivateParameters); } diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/SignVerify.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/SignVerify.cs index cc4d396d01ffb7..65e6ec2016886e 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/SignVerify.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/SignVerify.cs @@ -11,7 +11,7 @@ namespace System.Security.Cryptography.Rsa.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public sealed class SignVerify_Array : SignVerify + public abstract class SignVerify_Array : SignVerify where TProvider : IRSAProvider, new() { protected override byte[] SignData(RSA rsa, byte[] data, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding) => rsa.SignData(data, hashAlgorithm, padding); @@ -25,7 +25,7 @@ protected override bool VerifyHash(RSA rsa, byte[] hash, byte[] signature, HashA [Fact] public void NullArray_Throws() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { AssertExtensions.Throws("data", () => SignData(rsa, null, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1)); AssertExtensions.Throws("hash", () => SignHash(rsa, null, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1)); @@ -40,9 +40,21 @@ public void NullArray_Throws() } [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public abstract class SignVerify + public abstract class SignVerify where TProvider : IRSAProvider, new() { - public static bool SupportsPss => RSAFactory.SupportsPss; + protected static readonly TProvider s_provider = new TProvider(); + + public static bool SupportsPss => s_provider.SupportsPss; + public static bool SupportsSha1Signatures => s_provider.SupportsSha1Signatures; + public static bool SupportsSha3 => s_provider.SupportsSha3; + public static bool NoSupportsSha3 => !s_provider.SupportsSha3; + + protected static RSA CreateRSA(RSAParameters rsaParameters) + { + RSA rsa = s_provider.Create(); + rsa.ImportParameters(rsaParameters); + return rsa; + } protected abstract byte[] SignData(RSA rsa, byte[] data, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding); protected abstract byte[] SignHash(RSA rsa, byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding); @@ -52,7 +64,7 @@ public abstract class SignVerify [Fact] public void InvalidHashAlgorithmName_Throws() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { var invalidName = new HashAlgorithmName(null); AssertExtensions.Throws("hashAlgorithm", () => SignData(rsa, new byte[1], invalidName, RSASignaturePadding.Pkcs1)); @@ -71,7 +83,7 @@ public void InvalidHashAlgorithmName_Throws() [Fact] public void NullPadding_Throws() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { AssertExtensions.Throws("padding", () => SignData(rsa, new byte[1], HashAlgorithmName.SHA256, null)); AssertExtensions.Throws("padding", () => SignHash(rsa, new byte[1], HashAlgorithmName.SHA256, null)); @@ -85,7 +97,7 @@ public void NullPadding_Throws() [InlineData(true)] public void UseAfterDispose(bool importKey) { - RSA rsa = importKey ? RSAFactory.Create(TestData.RSA2048Params) : RSAFactory.Create(1024); + RSA rsa = importKey ? CreateRSA(TestData.RSA2048Params) : s_provider.Create(1024); byte[] data = TestData.HelloBytes; byte[] sig; HashAlgorithmName alg = HashAlgorithmName.SHA256; @@ -114,7 +126,7 @@ public void UseAfterDispose(bool importKey) [Fact] public void InvalidKeySize_DoesNotInvalidateKey() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { byte[] signature = SignData(rsa, TestData.HelloBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); @@ -128,8 +140,8 @@ public void InvalidKeySize_DoesNotInvalidateKey() [Fact] public void PublicKey_CannotSign() { - using (RSA rsa = RSAFactory.Create()) - using (RSA rsaPub = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) + using (RSA rsaPub = s_provider.Create()) { rsaPub.ImportParameters(rsa.ExportParameters(false)); @@ -141,14 +153,14 @@ public void PublicKey_CannotSign() [Fact] public void SignEmptyHash() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { Assert.ThrowsAny( () => SignHash(rsa, Array.Empty(), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1)); } } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact(nameof(SupportsSha1Signatures))] public void ExpectedSignature_SHA1_384() { byte[] expectedSignature = @@ -165,21 +177,21 @@ public void ExpectedSignature_SHA1_384() { ExpectSignature(expectedSignature, TestData.HelloBytes, "SHA1", TestData.RSA384Parameters); - Assert.True(RSAFactory.Supports384PrivateKey, "RSAFactory.Supports384PrivateKey"); + Assert.True(s_provider.Supports384PrivateKey, "s_provider.Supports384PrivateKey"); } catch (CryptographicException) { // If the provider is not known to fail loading a 384-bit key, let the exception be the // test failure. (If it is known to fail loading that key, we've now suppressed the throw, // and the test will pass.) - if (RSAFactory.Supports384PrivateKey) + if (s_provider.Supports384PrivateKey) { throw; } } } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact(nameof(SupportsSha1Signatures))] public void ExpectedSignature_SHA1_1032() { byte[] expectedSignature = @@ -206,7 +218,7 @@ public void ExpectedSignature_SHA1_1032() ExpectSignature(expectedSignature, TestData.HelloBytes, "SHA1", TestData.RSA1032Parameters); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact(nameof(SupportsSha1Signatures))] public void ExpectedSignature_SHA1_2048() { byte[] expectedSignature = new byte[] @@ -342,7 +354,7 @@ public void ExpectSignature_SHA256_1024_Stream() byte[] signature; using (Stream stream = new PositionValueStream(10)) - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.RSA1024Params); signature = rsa.SignData(stream, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); @@ -351,7 +363,7 @@ public void ExpectSignature_SHA256_1024_Stream() Assert.Equal(expectedSignature, signature); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact(nameof(SupportsSha1Signatures))] public void VerifySignature_SHA1_384() { byte[] signature = @@ -367,7 +379,7 @@ public void VerifySignature_SHA1_384() VerifySignature(signature, TestData.HelloBytes, "SHA1", TestData.RSA384Parameters); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact(nameof(SupportsSha1Signatures))] public void VerifySignature_SHA1_1032() { byte[] signature = @@ -394,7 +406,7 @@ public void VerifySignature_SHA1_1032() VerifySignature(signature, TestData.HelloBytes, "SHA1", TestData.RSA1032Parameters); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact(nameof(SupportsSha1Signatures))] public void VerifySignature_SHA1_2048() { byte[] signature = new byte[] @@ -504,7 +516,7 @@ public void VerifySignature_SHA256_2048() VerifySignature(signature, TestData.HelloBytes, "SHA256", TestData.RSA2048Params); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha3))] + [ConditionalFact(nameof(SupportsSha3))] public void VerifySignature_SHA3_256_RSA2048() { byte[] signature = new byte[] @@ -530,7 +542,7 @@ public void VerifySignature_SHA3_256_RSA2048() VerifySignature(signature, TestData.HelloBytes, HashAlgorithmName.SHA3_256.Name, TestData.RSA2048Params); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha3))] + [ConditionalFact(nameof(SupportsSha3))] public void VerifySignature_SHA3_384_RSA2048() { byte[] signature = new byte[] @@ -556,7 +568,7 @@ public void VerifySignature_SHA3_384_RSA2048() VerifySignature(signature, TestData.HelloBytes, HashAlgorithmName.SHA3_384.Name, TestData.RSA2048Params); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha3))] + [ConditionalFact(nameof(SupportsSha3))] public void VerifySignature_SHA3_512_RSA2048() { byte[] signature = new byte[] @@ -595,12 +607,12 @@ public static IEnumerable RoundTripTheories { foreach (RSAParameters rsaParameters in new[] { TestData.RSA1024Params, TestData.RSA2048Params }) { - if (RSAFactory.SupportsSha1Signatures) + if (s_provider.SupportsSha1Signatures) { yield return new object[] { nameof(HashAlgorithmName.SHA1), rsaParameters }; } - if (RSAFactory.SupportsMd5Signatures) + if (s_provider.SupportsMd5Signatures) { yield return new object[] { nameof(HashAlgorithmName.MD5), rsaParameters }; } @@ -616,7 +628,7 @@ public static IEnumerable RoundTripTheories [Fact] public void NegativeVerify_WrongAlgorithm() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.RSA2048Params); byte[] signature = SignData(rsa, TestData.HelloBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); @@ -629,7 +641,7 @@ public void NegativeVerify_WrongAlgorithm() [Fact] public void NegativeVerify_WrongSignature() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.RSA2048Params); byte[] signature = SignData(rsa, TestData.HelloBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); @@ -645,7 +657,7 @@ public void NegativeVerify_WrongSignature() [Fact] public void NegativeVerify_TamperedData() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.RSA2048Params); byte[] signature = SignData(rsa, TestData.HelloBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); @@ -659,13 +671,13 @@ public void NegativeVerify_BadKeysize() { byte[] signature; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.RSA2048Params); signature = SignData(rsa, TestData.HelloBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); } - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(TestData.RSA1024Params); bool signatureMatched = VerifyData(rsa, TestData.HelloBytes, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); @@ -679,7 +691,7 @@ public void PkcsSignHash_MismatchedHashSize() { RSASignaturePadding padding = RSASignaturePadding.Pkcs1; - using (RSA rsa = RSAFactory.Create(TestData.RSA2048Params)) + using (RSA rsa = CreateRSA(TestData.RSA2048Params)) { byte[] data152 = new byte[152 / 8]; byte[] data168 = new byte[168 / 8]; @@ -697,7 +709,7 @@ public void PkcsSignHash_MismatchedHashSize() } } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact(nameof(SupportsSha1Signatures))] public void ExpectedHashSignature_SHA1_2048() { byte[] expectedHashSignature = new byte[] @@ -828,7 +840,7 @@ public void ExpectedHashSignature_SHA256_2048() ExpectHashSignature(expectedHashSignature, dataHash, "SHA256", TestData.RSA2048Params); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact(nameof(SupportsSha1Signatures))] public void VerifyHashSignature_SHA1_2048() { byte[] hashSignature = new byte[] @@ -970,8 +982,8 @@ public void PssRoundtrip(string hashAlgorithmName) Exponent = privateParameters.Exponent, }; - using (RSA privateKey = RSAFactory.Create()) - using (RSA publicKey = RSAFactory.Create()) + using (RSA privateKey = s_provider.Create()) + using (RSA publicKey = s_provider.Create()) { privateKey.ImportParameters(privateParameters); publicKey.ImportParameters(publicParameters); @@ -980,7 +992,7 @@ public void PssRoundtrip(string hashAlgorithmName) HashAlgorithmName hashAlgorithm = new HashAlgorithmName(hashAlgorithmName); RSASignaturePadding padding = RSASignaturePadding.Pss; - if (RSAFactory.SupportsPss) + if (s_provider.SupportsPss) { byte[] signature = SignData(privateKey, data, hashAlgorithm, padding); @@ -1135,7 +1147,7 @@ public void VerifyExpectedSignature_PssSha512() helloSignature); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha3))] + [ConditionalFact(nameof(SupportsSha3))] public void VerifyExpectedSignature_PssSha3_256() { // Signature independently created with @@ -1167,7 +1179,7 @@ public void VerifyExpectedSignature_PssSha3_256() helloSignature); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha3))] + [ConditionalFact(nameof(SupportsSha3))] public void VerifyExpectedSignature_PssSha3_384() { // Signature independently created with @@ -1199,7 +1211,7 @@ public void VerifyExpectedSignature_PssSha3_384() helloSignature); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha3))] + [ConditionalFact(nameof(SupportsSha3))] public void VerifyExpectedSignature_PssSha3_512() { // Signature independently created with @@ -1231,13 +1243,13 @@ public void VerifyExpectedSignature_PssSha3_512() helloSignature); } - [ConditionalTheory(typeof(RSAFactory), nameof(RSAFactory.NoSupportsSha3))] + [ConditionalTheory(nameof(NoSupportsSha3))] [InlineData("SHA3-256")] [InlineData("SHA3-384")] [InlineData("SHA3-512")] public void Pkcs1UnsupportedHashAlgorithm(string hashAlgorithm) { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { Exception ex = Assert.ThrowsAny(() => SignData(rsa, new byte[] { 1 }, new HashAlgorithmName(hashAlgorithm), RSASignaturePadding.Pkcs1)); @@ -1255,13 +1267,13 @@ public void Pkcs1UnsupportedHashAlgorithm(string hashAlgorithm) } } - [ConditionalTheory(typeof(RSAFactory), nameof(RSAFactory.NoSupportsSha3))] + [ConditionalTheory(nameof(NoSupportsSha3))] [InlineData("SHA3-256")] [InlineData("SHA3-384")] [InlineData("SHA3-512")] public void PssUnsupportedHashAlgorithm(string hashAlgorithm) { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { Exception ex = Assert.ThrowsAny(() => SignData(rsa, new byte[] { 1 }, new HashAlgorithmName(hashAlgorithm), RSASignaturePadding.Pss)); @@ -1294,8 +1306,8 @@ private void VerifyExpectedSignature_Pss( RSASignaturePadding padding = RSASignaturePadding.Pss; - using (RSA rsaPublic = RSAFactory.Create()) - using (RSA rsaPrivate = RSAFactory.Create()) + using (RSA rsaPublic = s_provider.Create()) + using (RSA rsaPrivate = s_provider.Create()) { try { @@ -1316,7 +1328,7 @@ private void VerifyExpectedSignature_Pss( Console.WriteLine($"{callerName}: {signature.ByteArrayToHex()}"); } - if (RSAFactory.SupportsPss) + if (s_provider.SupportsPss) { Assert.True( VerifyData(rsaPublic, data, signature, hashAlgorithm, padding), @@ -1343,7 +1355,7 @@ public void PssSignature_WrongHashAlgorithm() RSASignaturePadding padding = RSASignaturePadding.Pss; byte[] data = TestData.HelloBytes; - using (RSA rsa = RSAFactory.Create(TestData.RSA2048Params)) + using (RSA rsa = CreateRSA(TestData.RSA2048Params)) { byte[] signature = SignData(rsa, data, HashAlgorithmName.SHA256, padding); Assert.False(VerifyData(rsa, data, signature, HashAlgorithmName.SHA384, padding)); @@ -1367,7 +1379,7 @@ public void PssVerifyHash_MismatchedHashSize() "56E2AF4213FDCA6BF801C06AF6381DAC61288C13B08806A323B3E956A13BCB29" + "680F62CCA9880A8A1FD1A2CA61DCFE008AC7FC55E98ACCE9B7BE010E5BCB836A").HexToByteArray(); - using (RSA rsa = RSAFactory.Create(TestData.RSA2048Params)) + using (RSA rsa = CreateRSA(TestData.RSA2048Params)) { Assert.False(VerifyHash(rsa, hash, sig, HashAlgorithmName.SHA256, RSASignaturePadding.Pss)); } @@ -1379,7 +1391,7 @@ public void PssSignHash_MismatchedHashSize() { RSASignaturePadding padding = RSASignaturePadding.Pss; - using (RSA rsa = RSAFactory.Create(TestData.RSA2048Params)) + using (RSA rsa = CreateRSA(TestData.RSA2048Params)) { byte[] data152 = new byte[152 / 8]; byte[] data168 = new byte[168 / 8]; @@ -1404,7 +1416,7 @@ public void PssSignature_WrongData() byte[] dataCopy = (byte[])TestData.HelloBytes.Clone(); HashAlgorithmName hashAlgorithmName = HashAlgorithmName.SHA256; - using (RSA rsa = RSAFactory.Create(TestData.RSA2048Params)) + using (RSA rsa = CreateRSA(TestData.RSA2048Params)) { byte[] signature = SignData(rsa, dataCopy, hashAlgorithmName, padding); dataCopy[0] ^= 0xFF; @@ -1419,7 +1431,7 @@ public void PssSignature_WrongLength() byte[] data = TestData.HelloBytes; HashAlgorithmName hashAlgorithmName = HashAlgorithmName.SHA256; - using (RSA rsa = RSAFactory.Create(TestData.RSA2048Params)) + using (RSA rsa = CreateRSA(TestData.RSA2048Params)) { byte[] signature = SignData(rsa, data, hashAlgorithmName, padding); @@ -1499,7 +1511,7 @@ private void ExpectSignature( // the signature is deterministic, so we can safely verify it here. byte[] signature; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(rsaParameters); signature = SignData(rsa, data, new HashAlgorithmName(hashAlgorithmName), RSASignaturePadding.Pkcs1); @@ -1520,7 +1532,7 @@ private void ExpectHashSignature( // the signature is deterministic, so we can safely verify it here. byte[] signature; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(rsaParameters); signature = SignHash(rsa, dataHash, new HashAlgorithmName(hashAlgorithmName), RSASignaturePadding.Pkcs1); @@ -1543,7 +1555,7 @@ private void VerifySignature( bool signatureMatched; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(publicOnly); signatureMatched = VerifyData(rsa, data, signature, new HashAlgorithmName(hashAlgorithmName), RSASignaturePadding.Pkcs1); @@ -1566,7 +1578,7 @@ private void VerifyHashSignature( bool signatureMatched; - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(publicOnly); signatureMatched = VerifyHash(rsa, dataHash, signature, new HashAlgorithmName(hashAlgorithmName), RSASignaturePadding.Pkcs1); @@ -1577,7 +1589,7 @@ private void VerifyHashSignature( private void SignAndVerify(byte[] data, string hashAlgorithmName, RSAParameters rsaParameters) { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { rsa.ImportParameters(rsaParameters); byte[] signature = SignData(rsa, data, new HashAlgorithmName(hashAlgorithmName), RSASignaturePadding.Pkcs1); @@ -1594,17 +1606,17 @@ public static IEnumerable HashAlgorithmNames yield return new object[] { HashAlgorithmName.SHA384.Name }; yield return new object[] { HashAlgorithmName.SHA512.Name }; - if (RSAFactory.SupportsMd5Signatures) + if (s_provider.SupportsMd5Signatures) { yield return new object[] { HashAlgorithmName.MD5.Name }; } - if (RSAFactory.SupportsSha1Signatures) + if (s_provider.SupportsSha1Signatures) { yield return new object[] { HashAlgorithmName.SHA1.Name }; } - if (RSAFactory.SupportsSha3) + if (s_provider.SupportsSha3) { yield return new object[] { HashAlgorithmName.SHA3_256.Name }; yield return new object[] { HashAlgorithmName.SHA3_384.Name }; diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/SignVerify.netcoreapp.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/SignVerify.netcoreapp.cs index 10ce40b962f8a4..965d797dba6964 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/SignVerify.netcoreapp.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/SignVerify.netcoreapp.cs @@ -5,7 +5,7 @@ namespace System.Security.Cryptography.Rsa.Tests { - public sealed class SignVerify_AllocatingSpan : SignVerify + public abstract class SignVerify_AllocatingSpan : SignVerify where TProvider : IRSAProvider, new() { protected override byte[] SignData(RSA rsa, byte[] data, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding) => rsa.SignData(new ReadOnlySpan(data), hashAlgorithm, padding); @@ -20,7 +20,7 @@ protected override bool VerifyHash(RSA rsa, byte[] hash, byte[] signature, HashA rsa.VerifyHash(new ReadOnlySpan(hash), (ReadOnlySpan)signature, hashAlgorithm, padding); } - public sealed class SignVerify_Span : SignVerify + public abstract class SignVerify_Span : SignVerify where TProvider : IRSAProvider, new() { protected override byte[] SignData(RSA rsa, byte[] data, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding) => WithOutputArray(dest => rsa.SignData(data, dest, hashAlgorithm, padding)); @@ -54,7 +54,7 @@ private static byte[] WithOutputArray(Func func) } } - public sealed class SignVerify_TrySpan : SignVerify + public abstract class SignVerify_TrySpan : SignVerify where TProvider : IRSAProvider, new() { protected override byte[] SignData(RSA rsa, byte[] data, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding) => TryWithOutputArray(dest => rsa.TrySignData(data, dest, hashAlgorithm, padding, out int bytesWritten) ? (true, bytesWritten) : (false, 0)); @@ -85,7 +85,7 @@ private static byte[] TryWithOutputArray(Func func) [Fact] public static void SignDefaultSpanHash() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { byte[] signature = new byte[2048 / 8]; @@ -100,14 +100,14 @@ public static void SignDefaultSpanHash() [Fact] public static void VerifyDefaultSpanHash() { - using (RSA rsa = RSAFactory.Create()) + using (RSA rsa = s_provider.Create()) { byte[] signature = new byte[2048 / 8]; Assert.False( rsa.VerifyHash(ReadOnlySpan.Empty, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1)); - if (RSAFactory.SupportsPss) + if (SupportsPss) { Assert.False( rsa.VerifyHash(ReadOnlySpan.Empty, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pss)); diff --git a/src/libraries/System.Security.Cryptography.Cng/tests/RSACngProvider.cs b/src/libraries/System.Security.Cryptography.Cng/tests/RSACngProvider.cs index 8b831ea9b041f1..0295b6ab9e7a8c 100644 --- a/src/libraries/System.Security.Cryptography.Cng/tests/RSACngProvider.cs +++ b/src/libraries/System.Security.Cryptography.Cng/tests/RSACngProvider.cs @@ -27,8 +27,21 @@ public class RSACngProvider : IRSAProvider public bool SupportsSha3 { get; } = SHA3_256.IsSupported; // If SHA3_256 is supported, assume 384 and 512 are, too. } - public partial class RSAFactory - { - private static readonly IRSAProvider s_provider = new RSACngProvider(); - } + // Concrete test classes for RSACngProvider + public class RSACngImportExport : ImportExport { } + public class RSACngEncryptDecrypt_Array : EncryptDecrypt_Array { } + public class RSACngEncryptDecrypt_Span : EncryptDecrypt_Span { } + public class RSACngEncryptDecrypt_AllocatingSpan : EncryptDecrypt_AllocatingSpan { } + public class RSACngEncryptDecrypt_TrySpan : EncryptDecrypt_TrySpan { } + public class RSACngSignVerify_Array : SignVerify_Array { } + public class RSACngSignVerify_AllocatingSpan : SignVerify_AllocatingSpan { } + public class RSACngSignVerify_Span : SignVerify_Span { } + public class RSACngSignVerify_TrySpan : SignVerify_TrySpan { } + public class RSACngKeyGeneration : KeyGeneration { } + public class RSACngXml : RSAXml { } + public class RSACngSignatureFormatterTests : RSASignatureFormatterTests { } + public class RSACngKeyExchangeFormatterTests : RSAKeyExchangeFormatterTests { } + public class RSACngFactoryTests : RSAFactoryTests { } + public class RSACngKeyPemTests : RSAKeyPemTests { } + public class RSACngKeyFileTests : RSAKeyFileTests { } } diff --git a/src/libraries/System.Security.Cryptography.Cng/tests/RsaCngTests.cs b/src/libraries/System.Security.Cryptography.Cng/tests/RsaCngTests.cs index 1d6310797ab28b..f96744d7faf75b 100644 --- a/src/libraries/System.Security.Cryptography.Cng/tests/RsaCngTests.cs +++ b/src/libraries/System.Security.Cryptography.Cng/tests/RsaCngTests.cs @@ -15,6 +15,9 @@ namespace System.Security.Cryptography.Cng.Tests { public static class RsaCngTests { + private static readonly Rsa.Tests.RSACngProvider s_provider = new Rsa.Tests.RSACngProvider(); + + public static bool SupportsSha1Signatures => s_provider.SupportsSha1Signatures; [Fact] public static void SignVerifyHashRoundTrip() { @@ -93,14 +96,14 @@ public static void RSACng_Ctor_UnusualKeysize_384() { RSACng_Ctor_UnusualKeysize(ExpectedKeySize, keyBlob, expected); - Assert.True(Rsa.Tests.RSAFactory.Supports384PrivateKey, "RSAFactory.Supports384PrivateKey"); + Assert.True(s_provider.Supports384PrivateKey, "s_provider.Supports384PrivateKey"); } catch (CryptographicException) { // If the provider is not known to fail loading a 384-bit key, let the exception be the // test failure. (If it is known to fail loading that key, we've now suppressed the throw, // and the test will pass.) - if (Rsa.Tests.RSAFactory.Supports384PrivateKey) + if (s_provider.Supports384PrivateKey) { throw; } diff --git a/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderBackCompat.cs b/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderBackCompat.cs index 8ef0c9474d73d7..9c44595bcc7492 100644 --- a/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderBackCompat.cs +++ b/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderBackCompat.cs @@ -10,6 +10,7 @@ namespace System.Security.Cryptography.Csp.Tests { public class RSACryptoServiceProviderBackCompat { + private static readonly RSACryptoServiceProviderProvider s_provider = new RSACryptoServiceProviderProvider(); private static readonly byte[] s_dataToSign = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; [Theory] @@ -156,14 +157,14 @@ public static void VerifyLegacySignVerifyHash(bool useLegacySign, bool useLegacy public static IEnumerable AlgorithmIdentifiers() { - if (RSAFactory.SupportsMd5Signatures) + if (s_provider.SupportsMd5Signatures) { yield return new object[] { "MD5", MD5.Create() }; yield return new object[] { "MD5", typeof(MD5) }; yield return new object[] { "MD5", "1.2.840.113549.2.5" }; } - if (RSAFactory.SupportsSha1Signatures) + if (s_provider.SupportsSha1Signatures) { yield return new object[] { "SHA1", SHA1.Create() }; yield return new object[] { "SHA1", typeof(SHA1) }; diff --git a/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderProvider.cs b/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderProvider.cs index 84825bcbb5c3b5..20fcf2087a0624 100644 --- a/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderProvider.cs +++ b/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderProvider.cs @@ -30,8 +30,21 @@ public class RSACryptoServiceProviderProvider : IRSAProvider public bool SupportsSha3 => false; } - public partial class RSAFactory - { - private static readonly IRSAProvider s_provider = new RSACryptoServiceProviderProvider(); - } + // Concrete test classes for RSACryptoServiceProviderProvider + public class RSACryptoServiceProviderImportExport : ImportExport { } + public class RSACryptoServiceProviderEncryptDecrypt_Array : EncryptDecrypt_Array { } + public class RSACryptoServiceProviderEncryptDecrypt_Span : EncryptDecrypt_Span { } + public class RSACryptoServiceProviderEncryptDecrypt_AllocatingSpan : EncryptDecrypt_AllocatingSpan { } + public class RSACryptoServiceProviderEncryptDecrypt_TrySpan : EncryptDecrypt_TrySpan { } + public class RSACryptoServiceProviderSignVerify_Array : SignVerify_Array { } + public class RSACryptoServiceProviderSignVerify_AllocatingSpan : SignVerify_AllocatingSpan { } + public class RSACryptoServiceProviderSignVerify_Span : SignVerify_Span { } + public class RSACryptoServiceProviderSignVerify_TrySpan : SignVerify_TrySpan { } + public class RSACryptoServiceProviderKeyGeneration : KeyGeneration { } + public class RSACryptoServiceProviderXml : RSAXml { } + public class RSACryptoServiceProviderSignatureFormatterTests : RSASignatureFormatterTests { } + public class RSACryptoServiceProviderKeyExchangeFormatterTests : RSAKeyExchangeFormatterTests { } + public class RSACryptoServiceProviderFactoryTests : RSAFactoryTests { } + public class RSACryptoServiceProviderKeyPemTests : RSAKeyPemTests { } + public class RSACryptoServiceProviderKeyFileTests : RSAKeyFileTests { } } diff --git a/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderTests.cs b/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderTests.cs index 89c9405d84b4d8..c2b942523d7fe3 100644 --- a/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderTests.cs +++ b/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderTests.cs @@ -9,6 +9,10 @@ namespace System.Security.Cryptography.Csp.Tests { public class RSACryptoServiceProviderTests { + private static readonly Rsa.Tests.RSACryptoServiceProviderProvider s_provider = new Rsa.Tests.RSACryptoServiceProviderProvider(); + + public static bool SupportsSha1Signatures => s_provider.SupportsSha1Signatures; + const int PROV_RSA_FULL = 1; const int PROV_RSA_AES = 24; @@ -306,7 +310,7 @@ public static void ImportParameters_ExponentTooBig_Throws() } } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact(nameof(SupportsSha1Signatures))] public static void SignHash_DefaultAlgorithm_Success() { byte[] hashVal = SHA1.HashData(TestData.HelloBytes); @@ -318,7 +322,7 @@ public static void SignHash_DefaultAlgorithm_Success() } } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact(nameof(SupportsSha1Signatures))] public static void VerifyHash_DefaultAlgorithm_Success() { byte[] hashVal = SHA1.HashData(TestData.HelloBytes); diff --git a/src/libraries/System.Security.Cryptography.OpenSsl/tests/RSAOpenSslProvider.cs b/src/libraries/System.Security.Cryptography.OpenSsl/tests/RSAOpenSslProvider.cs index 18aa9528877d9d..07b82bb22cf248 100644 --- a/src/libraries/System.Security.Cryptography.OpenSsl/tests/RSAOpenSslProvider.cs +++ b/src/libraries/System.Security.Cryptography.OpenSsl/tests/RSAOpenSslProvider.cs @@ -28,8 +28,21 @@ public class RSAOpenSslProvider : IRSAProvider public bool SupportsSha3 => SHA3_256.IsSupported; // If SHA3_256 is supported, assume 384 and 512 are, too. } - public partial class RSAFactory - { - private static readonly IRSAProvider s_provider = new RSAOpenSslProvider(); - } + // Concrete test classes for RSAOpenSslProvider + public class RSAOpenSslImportExport : ImportExport { } + public class RSAOpenSslEncryptDecrypt_Array : EncryptDecrypt_Array { } + public class RSAOpenSslEncryptDecrypt_Span : EncryptDecrypt_Span { } + public class RSAOpenSslEncryptDecrypt_AllocatingSpan : EncryptDecrypt_AllocatingSpan { } + public class RSAOpenSslEncryptDecrypt_TrySpan : EncryptDecrypt_TrySpan { } + public class RSAOpenSslSignVerify_Array : SignVerify_Array { } + public class RSAOpenSslSignVerify_AllocatingSpan : SignVerify_AllocatingSpan { } + public class RSAOpenSslSignVerify_Span : SignVerify_Span { } + public class RSAOpenSslSignVerify_TrySpan : SignVerify_TrySpan { } + public class RSAOpenSslKeyGeneration : KeyGeneration { } + public class RSAOpenSslXml : RSAXml { } + public class RSAOpenSslSignatureFormatterTests : RSASignatureFormatterTests { } + public class RSAOpenSslKeyExchangeFormatterTests : RSAKeyExchangeFormatterTests { } + public class RSAOpenSslFactoryTests : RSAFactoryTests { } + public class RSAOpenSslKeyPemTests : RSAKeyPemTests { } + public class RSAOpenSslKeyFileTests : RSAKeyFileTests { } } diff --git a/src/libraries/System.Security.Cryptography/tests/DefaultRSAProvider.cs b/src/libraries/System.Security.Cryptography/tests/DefaultRSAProvider.cs index affcb571f1307f..199327ab1d5664 100644 --- a/src/libraries/System.Security.Cryptography/tests/DefaultRSAProvider.cs +++ b/src/libraries/System.Security.Cryptography/tests/DefaultRSAProvider.cs @@ -39,8 +39,21 @@ public RSA Create(int keySize) public bool SupportsSha3 { get; } = SHA3_256.IsSupported; // If SHA3_256 is supported, assume 384 and 512 are, too. } - public partial class RSAFactory - { - private static readonly IRSAProvider s_provider = new DefaultRSAProvider(); - } + // Concrete test classes for DefaultRSAProvider + public class DefaultRSAImportExport : ImportExport { } + public class DefaultRSAEncryptDecrypt_Array : EncryptDecrypt_Array { } + public class DefaultRSAEncryptDecrypt_Span : EncryptDecrypt_Span { } + public class DefaultRSAEncryptDecrypt_AllocatingSpan : EncryptDecrypt_AllocatingSpan { } + public class DefaultRSAEncryptDecrypt_TrySpan : EncryptDecrypt_TrySpan { } + public class DefaultRSASignVerify_Array : SignVerify_Array { } + public class DefaultRSASignVerify_AllocatingSpan : SignVerify_AllocatingSpan { } + public class DefaultRSASignVerify_Span : SignVerify_Span { } + public class DefaultRSASignVerify_TrySpan : SignVerify_TrySpan { } + public class DefaultRSAKeyGeneration : KeyGeneration { } + public class DefaultRSAXml : RSAXml { } + public class DefaultRSASignatureFormatterTests : RSASignatureFormatterTests { } + public class DefaultRSAKeyExchangeFormatterTests : RSAKeyExchangeFormatterTests { } + public class DefaultRSAFactoryTests : RSAFactoryTests { } + public class DefaultRSAKeyFileTests : RSAKeyFileTests { } + public class DefaultRSAKeyPemTests : RSAKeyPemTests { } }