From e502a18c3f272ecfbc1c52efe7b9743029ed3bb0 Mon Sep 17 00:00:00 2001 From: Pranav Senthilnathan Date: Sat, 27 Jun 2026 06:26:38 +0000 Subject: [PATCH 01/10] Consolidate RSA tests into S.S.C. --- .../RSA/EncryptDecrypt.cs | 48 ++++-- .../RSA/EncryptDecrypt.netcoreapp.cs | 16 +- .../RSA/ImportExport.cs | 63 +++---- .../RSA/KeyGeneration.cs | 18 +- .../RSA/RSAFactory.cs | 52 ------ .../RSA/RSAFactoryTests.cs | 12 +- ...ter.cs => RSAKeyExchangeFormatterTests.cs} | 12 +- .../RSA/RSAKeyFileTests.cs | 106 +++++++----- .../RSA/RSAKeyPemTests.cs | 56 +++--- .../RSA/RSAProvider.cs | 56 ++++++ ...atter.cs => RSASignatureFormatterTests.cs} | 21 ++- .../AlgorithmImplementations/RSA/RSAXml.cs | 70 ++++---- .../RSA/SignVerify.cs | 163 +++++++++++------- .../RSA/SignVerify.netcoreapp.cs | 10 +- .../tests/RSACngProvider.cs | 34 ---- ...tem.Security.Cryptography.Cng.Tests.csproj | 36 ---- .../tests/RSACryptoServiceProviderProvider.cs | 37 ---- ...tem.Security.Cryptography.Csp.Tests.csproj | 36 ---- .../tests/RSAOpenSslProvider.cs | 35 ---- ...Security.Cryptography.OpenSsl.Tests.csproj | 36 ---- .../AsymmetricSignatureFormatterTests.cs | 4 +- .../tests/DefaultRSAProvider.cs | 29 ++-- .../tests/{RSATests.cs => DefaultRSATests.cs} | 24 +-- .../tests/RSACngProvider.cs | 33 ++++ .../RSACryptoServiceProviderBackCompat.cs | 4 +- .../tests/RSACryptoServiceProviderProvider.cs | 36 ++++ .../tests/RSACryptoServiceProviderTests.cs | 13 +- .../tests/RSAImportExportCspBlobTests.cs | 0 ...> RSAKeyExchangeFormatterTests.Default.cs} | 2 +- .../tests/RSAOpenSslProvider.cs | 34 ++++ ...cs => RSASignatureFormatterTests.NoKey.cs} | 6 +- .../tests/RSATestRegistration.Cng.cs | 85 +++++++++ .../tests/RSATestRegistration.Csp.cs | 85 +++++++++ .../tests/RSATestRegistration.Default.cs | 85 +++++++++ .../tests/RSATestRegistration.OpenSsl.cs | 85 +++++++++ .../tests/RsaCngTests.cs | 5 +- .../tests/RsaOpenSslTests.cs | 0 .../System.Security.Cryptography.Tests.csproj | 35 +++- 38 files changed, 910 insertions(+), 572 deletions(-) delete mode 100644 src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAFactory.cs rename src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/{RSAKeyExchangeFormatter.cs => RSAKeyExchangeFormatterTests.cs} (93%) create mode 100644 src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAProvider.cs rename src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/{RSASignatureFormatter.cs => RSASignatureFormatterTests.cs} (86%) delete mode 100644 src/libraries/System.Security.Cryptography.Cng/tests/RSACngProvider.cs delete mode 100644 src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderProvider.cs delete mode 100644 src/libraries/System.Security.Cryptography.OpenSsl/tests/RSAOpenSslProvider.cs rename src/libraries/System.Security.Cryptography/tests/{RSATests.cs => DefaultRSATests.cs} (98%) create mode 100644 src/libraries/System.Security.Cryptography/tests/RSACngProvider.cs rename src/libraries/{System.Security.Cryptography.Csp => System.Security.Cryptography}/tests/RSACryptoServiceProviderBackCompat.cs (97%) create mode 100644 src/libraries/System.Security.Cryptography/tests/RSACryptoServiceProviderProvider.cs rename src/libraries/{System.Security.Cryptography.Csp => System.Security.Cryptography}/tests/RSACryptoServiceProviderTests.cs (97%) rename src/libraries/{System.Security.Cryptography.Csp => System.Security.Cryptography}/tests/RSAImportExportCspBlobTests.cs (100%) rename src/libraries/System.Security.Cryptography/tests/{RSAKeyExchangeFormatterTests.cs => RSAKeyExchangeFormatterTests.Default.cs} (97%) create mode 100644 src/libraries/System.Security.Cryptography/tests/RSAOpenSslProvider.cs rename src/libraries/System.Security.Cryptography/tests/{RSASignatureFormatterTests.cs => RSASignatureFormatterTests.NoKey.cs} (59%) create mode 100644 src/libraries/System.Security.Cryptography/tests/RSATestRegistration.Cng.cs create mode 100644 src/libraries/System.Security.Cryptography/tests/RSATestRegistration.Csp.cs create mode 100644 src/libraries/System.Security.Cryptography/tests/RSATestRegistration.Default.cs create mode 100644 src/libraries/System.Security.Cryptography/tests/RSATestRegistration.OpenSsl.cs rename src/libraries/{System.Security.Cryptography.Cng => System.Security.Cryptography}/tests/RsaCngTests.cs (97%) rename src/libraries/{System.Security.Cryptography.OpenSsl => System.Security.Cryptography}/tests/RsaOpenSslTests.cs (100%) 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 882f61873624b4..5625cddc460087 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 @@ -4,13 +4,14 @@ using System.Collections.Generic; using System.Diagnostics; using System.Numerics; +using System.Security.Cryptography.Tests; using Test.Cryptography; using Microsoft.DotNet.XUnitExtensions; using Xunit; namespace System.Security.Cryptography.Rsa.Tests { - public sealed class EncryptDecrypt_Array : EncryptDecrypt + public abstract class EncryptDecrypt_Array : EncryptDecrypt { protected override byte[] Encrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding) => rsa.Encrypt(data, padding); @@ -31,7 +32,9 @@ public void NullArray_Throws() [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] public abstract class EncryptDecrypt { - public static bool SupportsSha2Oaep => RSAFactory.SupportsSha2Oaep; + public bool SupportsSha2Oaep => RSAFactory.SupportsSha2Oaep; + + protected abstract RSAProvider RSAFactory { get; } protected abstract byte[] Encrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding); protected abstract byte[] Decrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding); @@ -473,9 +476,11 @@ public void RsaDecryptOaep_ExpectFailure() } } - [ConditionalFact(typeof(EncryptDecrypt), nameof(SupportsSha2Oaep))] + [ConditionalFact] public void RsaDecryptOaepWrongAlgorithm() { + SkipTestException.ThrowUnless(SupportsSha2Oaep); + using (RSA rsa = RSAFactory.Create(TestData.RSA2048Params)) { byte[] data = TestData.HelloBytes; @@ -644,9 +649,11 @@ public void RsaDecryptAfterExport() Assert.Equal(TestData.HelloBytes, output); } - [ConditionalFact(typeof(ImportExport), nameof(ImportExport.Supports16384))] + [ConditionalFact] public void LargeKeyCryptRoundtrip() { + SkipTestException.ThrowUnless(RSAFactory.Supports16384); + byte[] output; using (RSA rsa = RSAFactory.Create()) @@ -691,8 +698,17 @@ public void UnusualExponentCryptRoundtrip() [Theory] [MemberData(nameof(OaepPaddingModes))] - public void NonPowerOfTwoKeySizeOaepRoundtrip(RSAEncryptionPadding oaepPaddingMode) + public void NonPowerOfTwoKeySizeOaepRoundtrip( + RSAEncryptionPadding oaepPaddingMode, + bool requiresSha2Oaep, + bool requiresSha3) { + if ((requiresSha2Oaep && !RSAFactory.SupportsSha2Oaep) || + (requiresSha3 && !RSAFactory.SupportsSha3)) + { + return; + } + // Key generation can transiently fail on some platforms due to resource contention. // Retry a few times before failing the test. RetryHelper.Execute(() => @@ -840,21 +856,15 @@ public static IEnumerable OaepPaddingModes { get { - yield return new object[] { RSAEncryptionPadding.OaepSHA1 }; + yield return new object[] { RSAEncryptionPadding.OaepSHA1, false, false }; - if (RSAFactory.SupportsSha2Oaep) - { - yield return new object[] { RSAEncryptionPadding.OaepSHA256 }; - yield return new object[] { RSAEncryptionPadding.OaepSHA384 }; - yield return new object[] { RSAEncryptionPadding.OaepSHA512 }; - } - - if (RSAFactory.SupportsSha3) - { - yield return new object[] { RSAEncryptionPadding.OaepSHA3_256 }; - yield return new object[] { RSAEncryptionPadding.OaepSHA3_384 }; - yield return new object[] { RSAEncryptionPadding.OaepSHA3_512 }; - } + yield return new object[] { RSAEncryptionPadding.OaepSHA256, true, false }; + yield return new object[] { RSAEncryptionPadding.OaepSHA384, true, false }; + yield return new object[] { RSAEncryptionPadding.OaepSHA512, true, false }; + + yield return new object[] { RSAEncryptionPadding.OaepSHA3_256, false, true }; + yield return new object[] { RSAEncryptionPadding.OaepSHA3_384, false, true }; + yield return new object[] { RSAEncryptionPadding.OaepSHA3_512, false, true }; } } 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 a9e17abe3389bb..fb413c2cfb4699 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 @@ -2,12 +2,14 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Linq; +using System.Security.Cryptography.Tests; +using Microsoft.DotNet.XUnitExtensions; using Xunit; 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 { protected override byte[] Encrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding) => WithOutputArray(dest => rsa.Encrypt(data, dest, padding)); @@ -36,7 +38,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 { protected override byte[] Encrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding) => rsa.Encrypt(new ReadOnlySpan(data), padding); @@ -46,7 +48,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 { protected override byte[] Encrypt(RSA rsa, byte[] data, RSAEncryptionPadding padding) => TryWithOutputArray(dest => rsa.TryEncrypt(data, dest, padding, out int bytesWritten) ? (true, bytesWritten) : (false, 0)); @@ -139,14 +141,16 @@ public void Decrypt_WrongKey_OAEP_SHA1() Decrypt_WrongKey(RSAEncryptionPadding.OaepSHA1); } - [ConditionalFact(typeof(EncryptDecrypt_TrySpan), nameof(SupportsSha2Oaep))] + [ConditionalFact] public void Decrypt_WrongKey_OAEP_SHA256() { + SkipTestException.ThrowUnless(RSAFactory.SupportsSha2Oaep); + Decrypt_WrongKey(RSAEncryptionPadding.OaepSHA256); } [Fact] - public static void EncryptDefaultSpan() + public void EncryptDefaultSpan() { using (RSA rsa = RSAFactory.Create()) { @@ -164,7 +168,7 @@ public static void EncryptDefaultSpan() } } - private static void Decrypt_WrongKey(RSAEncryptionPadding padding) + private void Decrypt_WrongKey(RSAEncryptionPadding padding) { using (RSA rsa1 = RSAFactory.Create()) using (RSA rsa2 = RSAFactory.Create()) 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 1a911bebd8e88a..2bd214ceecc268 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 @@ -3,17 +3,19 @@ using System.Linq; using System.Numerics; +using System.Security.Cryptography.Tests; +using Microsoft.DotNet.XUnitExtensions; using Xunit; namespace System.Security.Cryptography.Rsa.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public partial class ImportExport + public abstract class ImportExport { - public static bool Supports16384 { get; } = TestRsa16384(); + protected abstract RSAProvider RSAFactory { get; } [Fact] - public static void ExportAutoKey() + public void ExportAutoKey() { RSAParameters privateParams; RSAParameters publicParams; @@ -43,7 +45,7 @@ public static void ExportAutoKey() } [Fact] - public static void PaddedExport() + public void PaddedExport() { // OpenSSL's numeric type for the storage of RSA key parts disregards zero-valued // prefix bytes. @@ -69,9 +71,11 @@ public static void PaddedExport() RSATestHelpers.AssertKeyEquals(diminishedDPParameters, exported); } - [ConditionalFact(typeof(ImportExport), nameof(ImportExport.Supports16384))] - public static void LargeKeyImportExport() + [ConditionalFact] + public void LargeKeyImportExport() { + SkipTestException.ThrowUnless(RSAFactory.Supports16384); + RSAParameters imported = TestData.RSA16384Params; using (RSA rsa = RSAFactory.Create()) @@ -99,7 +103,7 @@ public static void LargeKeyImportExport() } [Fact] - public static void UnusualExponentImportExport() + public void UnusualExponentImportExport() { // Most choices for the Exponent value in an RSA key use a Fermat prime. // Since a Fermat prime is 2^(2^m) + 1, it always only has two bits set, and @@ -123,7 +127,7 @@ public static void UnusualExponentImportExport() } [Fact] - public static void ImportExport1032() + public void ImportExport1032() { RSAParameters imported = TestData.RSA1032Parameters; RSAParameters exported; @@ -144,7 +148,7 @@ public static void ImportExport1032() } [Fact] - public static void ImportReset() + public void ImportReset() { using (RSA rsa = RSAFactory.Create()) { @@ -174,7 +178,7 @@ public static void ImportReset() } [Fact] - public static void ImportPrivateExportPublic() + public void ImportPrivateExportPublic() { RSAParameters imported = TestData.RSA1024Params; @@ -192,7 +196,7 @@ public static void ImportPrivateExportPublic() } [Fact] - public static void MultiExport() + public void MultiExport() { RSAParameters imported = TestData.RSA1024Params; @@ -223,7 +227,7 @@ public static void MultiExport() } [Fact] - public static void PublicOnlyPrivateExport() + public void PublicOnlyPrivateExport() { RSAParameters imported = new RSAParameters { @@ -239,7 +243,7 @@ public static void PublicOnlyPrivateExport() } [Fact] - public static void ImportNoExponent() + public void ImportNoExponent() { RSAParameters imported = new RSAParameters { @@ -256,7 +260,7 @@ public static void ImportNoExponent() } [Fact] - public static void ImportNoModulus() + public void ImportNoModulus() { RSAParameters imported = new RSAParameters { @@ -276,7 +280,7 @@ public static void ImportNoModulus() #if TESTING_CNG_IMPLEMENTATION [ActiveIssue("https://github.com/dotnet/runtime/issues/21341", TargetFrameworkMonikers.NetFramework)] #endif - public static void ImportNoDP() + public void ImportNoDP() { // Because RSAParameters is a struct, this is a copy, // so assigning DP is not destructive to other tests. @@ -292,7 +296,7 @@ public static void ImportNoDP() [Theory] [InlineData(true)] [InlineData(false)] - public static void ExportAfterDispose(bool importKey) + public void ExportAfterDispose(bool importKey) { RSA rsa = importKey ? RSAFactory.Create(TestData.RSA2048Params) : RSAFactory.Create(1024); @@ -314,7 +318,7 @@ public static void ExportAfterDispose(bool importKey) [Theory] [InlineData(true)] [InlineData(false)] - public static void ImportZeroModulus(bool includePrivateParameters) + public void ImportZeroModulus(bool includePrivateParameters) { RSAParameters zeroModulus = CopyRSAParameters(TestData.RSA2048Params); zeroModulus.Modulus.AsSpan().Clear(); @@ -365,31 +369,6 @@ internal static RSAParameters MakePublic(in RSAParameters rsaParams) }; } - private static bool TestRsa16384() - { - if (PlatformDetection.IsAndroid) - { - // We cannot detect this on Android at the moment. Even attempting to generate or import a 16K RSA key - // may leave the error queue in the incorrect state. See https://github.com/google/conscrypt/issues/1507 - return false; - } - - try - { - using (RSA rsa = RSAFactory.Create()) - { - rsa.ImportParameters(TestData.RSA16384Params); - } - - return true; - } - catch (Exception e) when (e is CryptographicException or PlatformNotSupportedException) - { - // The key is too big for this platform or the platform is not supported. - return false; - } - } - private static RSAParameters CopyRSAParameters(in RSAParameters rsaParams) { static byte[] CopyBytes(byte[] data) => data is null ? null : data.AsSpan().ToArray(); 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..f69294ec26dbad 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,44 +6,46 @@ namespace System.Security.Cryptography.Rsa.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public class KeyGeneration + public abstract class KeyGeneration { + protected abstract RSAProvider RSAFactory { get; } + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotSymCryptOpenSsl))] - public static void GenerateMinKey() + public void GenerateMinKey() { GenerateKey(rsa => GetMin(rsa.LegalKeySizes)); } [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotSymCryptOpenSsl))] - public static void GenerateSecondMinKey() + public void GenerateSecondMinKey() { GenerateKey(rsa => GetSecondMin(rsa.LegalKeySizes)); } [ConditionalFact(typeof(TestEnvironment), nameof(TestEnvironment.IsStressModeEnabled))] - public static void GenerateMaxKey() + public void GenerateMaxKey() { GenerateKey(rsa => GetMax(rsa.LegalKeySizes)); } [Fact] - public static void GenerateKey_2048() + public void GenerateKey_2048() { GenerateKey(2048); } [Fact] - public static void GenerateKey_4096() + public void GenerateKey_4096() { GenerateKey(4096); } - private static void GenerateKey(int size) + private void GenerateKey(int size) { GenerateKey(rsa => size); } - private static void GenerateKey(Func getSize) + private void GenerateKey(Func getSize) { int 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 deleted file mode 100644 index a01390dec6c982..00000000000000 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAFactory.cs +++ /dev/null @@ -1,52 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace System.Security.Cryptography.Rsa.Tests -{ - public interface IRSAProvider - { - RSA Create(); - RSA Create(int keySize); - bool Supports384PrivateKey { get; } - bool SupportsLargeExponent { get; } - bool SupportsSha2Oaep { get; } - bool SupportsPss { get; } - bool SupportsSha1Signatures { get; } - 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..88a17daedbca54 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,31 +6,33 @@ namespace System.Security.Cryptography.Rsa.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public static class RSAFactoryTests + public abstract class RSAFactoryTests { + protected abstract RSAProvider RSAFactory { get; } + [Fact] - public static void RSACreateDefault_Equals_SameInstance() + public void RSACreateDefault_Equals_SameInstance() { using RSA rsa = RSAFactory.Create(); AssertExtensions.TrueExpression(rsa.Equals(rsa)); } [Fact] - public static void RSACreateKeySize_Equals_SameInstance() + public void RSACreateKeySize_Equals_SameInstance() { using RSA rsa = RSAFactory.Create(2048); AssertExtensions.TrueExpression(rsa.Equals(rsa)); } [Fact] - public static void RSACreateParameters_Equals_SameInstance() + public void RSACreateParameters_Equals_SameInstance() { using RSA rsa = RSAFactory.Create(TestData.RSA2048Params); AssertExtensions.TrueExpression(rsa.Equals(rsa)); } [Fact] - public static void RSACreateParameters_Equals_DifferentInstance_FalseForSameKeyMaterial() + public void RSACreateParameters_Equals_DifferentInstance_FalseForSameKeyMaterial() { using RSA rsa1 = RSAFactory.Create(TestData.RSA2048Params); using RSA rsa2 = RSAFactory.Create(TestData.RSA2048Params); diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAKeyExchangeFormatter.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAKeyExchangeFormatterTests.cs similarity index 93% rename from src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAKeyExchangeFormatter.cs rename to src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAKeyExchangeFormatterTests.cs index 3e9071c25c8bea..f58ac3c5325be2 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAKeyExchangeFormatter.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAKeyExchangeFormatterTests.cs @@ -8,10 +8,12 @@ namespace System.Security.Cryptography.Rsa.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public partial class RSAKeyExchangeFormatterTests + public abstract class RSAKeyExchangeFormatterTests { + protected abstract RSAProvider RSAFactory { get; } + [Fact] - public static void VerifyDecryptKeyExchangeOaep() + public void VerifyDecryptKeyExchangeOaep() { using (RSA rsa = RSAFactory.Create()) { @@ -24,7 +26,7 @@ public static void VerifyDecryptKeyExchangeOaep() } [Fact] - public static void VerifyDecryptKeyExchangePkcs1() + public void VerifyDecryptKeyExchangePkcs1() { using (RSA rsa = RSAFactory.Create()) { @@ -37,7 +39,7 @@ public static void VerifyDecryptKeyExchangePkcs1() } [Fact] - public static void TestKnownValueOaep() + public void TestKnownValueOaep() { using (RSA rsa = RSAFactory.Create()) { @@ -54,7 +56,7 @@ public static void TestKnownValueOaep() } [Fact] - public static void TestKnownValuePkcs1() + public void TestKnownValuePkcs1() { using (RSA rsa = RSAFactory.Create()) { 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 4a0fe990232ec3..76403d6358fdf9 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 @@ -2,22 +2,26 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Security.Cryptography.Encryption.RC2.Tests; +using System.Security.Cryptography.Tests; using System.Text; +using Microsoft.DotNet.XUnitExtensions; using Test.Cryptography; using Xunit; namespace System.Security.Cryptography.Rsa.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public static class RSAKeyFileTests + public abstract class RSAKeyFileTests { - public static bool Supports384BitPrivateKeyAndRC2 { get; } = RSAFactory.Supports384PrivateKey && RC2Factory.IsSupported; - public static bool SupportsLargeExponent { get; } = RSAFactory.SupportsLargeExponent; + protected abstract RSAProvider RSAFactory { get; } + + public bool Supports384BitPrivateKeyAndRC2 => RSAFactory.Supports384PrivateKey && RC2Factory.IsSupported; + public bool SupportsLargeExponent => RSAFactory.SupportsLargeExponent; [Theory] [InlineData(false)] [InlineData(true)] - public static void UseAfterDispose(bool importKey) + public void UseAfterDispose(bool importKey) { RSA rsa = importKey ? RSAFactory.Create(TestData.RSA2048Params) : RSAFactory.Create(1024); byte[] pkcs1Public; @@ -73,9 +77,11 @@ public static void UseAfterDispose(bool importKey) Assert.Throws(() => rsa.ImportEncryptedPkcs8PrivateKey(pwBytes, pkcs8EncryptedPrivate, out _)); } - [ConditionalFact(typeof(RSAKeyFileTests), nameof(SupportsLargeExponent))] - public static void ReadWriteBigExponentPrivatePkcs1() + [ConditionalFact] + public void ReadWriteBigExponentPrivatePkcs1() { + SkipTestException.ThrowUnless(SupportsLargeExponent); + ReadWriteBase64PrivatePkcs1( @" MIIEpQIBAAKCAQEAr4HBy9ggP2JKU57WYIF1NyOTooN9SJDkihne02lzEVYglo1r @@ -108,7 +114,7 @@ public static void ReadWriteBigExponentPrivatePkcs1() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/62547", TestPlatforms.Android)] - public static void ReadWriteDiminishedDPPrivatePkcs1() + public void ReadWriteDiminishedDPPrivatePkcs1() { ReadWriteBase64PrivatePkcs1( @" @@ -122,9 +128,11 @@ public static void ReadWriteDiminishedDPPrivatePkcs1() TestData.DiminishedDPParameters); } - [ConditionalFact(typeof(ImportExport), nameof(ImportExport.Supports16384))] - public static void ReadWritePublicPkcs1() + [ConditionalFact] + public void ReadWritePublicPkcs1() { + SkipTestException.ThrowUnless(RSAFactory.Supports16384); + ReadWriteBase64PublicPkcs1( @" MIIICgKCCAEAmyxwX6kQNx+LSMao1StC1p5rKCEwcBjzI136An3B/BjthgezAOuu @@ -173,9 +181,11 @@ public static void ReadWritePublicPkcs1() TestData.RSA16384Params); } - [ConditionalFact(typeof(RSAKeyFileTests), nameof(SupportsLargeExponent))] - public static void ReadWriteSubjectPublicKeyInfo() + [ConditionalFact] + public void ReadWriteSubjectPublicKeyInfo() { + SkipTestException.ThrowUnless(SupportsLargeExponent); + ReadWriteBase64SubjectPublicKeyInfo( @" MIIBJDANBgkqhkiG9w0BAQEFAAOCAREAMIIBDAKCAQEAr4HBy9ggP2JKU57WYIF1 @@ -189,7 +199,7 @@ public static void ReadWriteSubjectPublicKeyInfo() } [Fact] - public static void ReadWriteSubjectPublicKeyInfo_DiminishedDPKey() + public void ReadWriteSubjectPublicKeyInfo_DiminishedDPKey() { ReadWriteBase64SubjectPublicKeyInfo( @" @@ -198,9 +208,11 @@ public static void ReadWriteSubjectPublicKeyInfo_DiminishedDPKey() TestData.DiminishedDPParameters); } - [ConditionalFact(typeof(ImportExport), nameof(ImportExport.Supports16384))] - public static void ReadWriteRsa16384SubjectPublicKeyInfo() + [ConditionalFact] + public void ReadWriteRsa16384SubjectPublicKeyInfo() { + SkipTestException.ThrowUnless(RSAFactory.Supports16384); + ReadWriteBase64SubjectPublicKeyInfo( @" MIIIIjANBgkqhkiG9w0BAQEFAAOCCA8AMIIICgKCCAEAmyxwX6kQNx+LSMao1StC @@ -250,9 +262,11 @@ public static void ReadWriteRsa16384SubjectPublicKeyInfo() TestData.RSA16384Params); } - [ConditionalFact(typeof(ImportExport), nameof(ImportExport.Supports16384))] - public static void ReadWrite16384Pkcs8() + [ConditionalFact] + public void ReadWrite16384Pkcs8() { + SkipTestException.ThrowUnless(RSAFactory.Supports16384); + ReadWriteBase64Pkcs8( @" MIIkQgIBADANBgkqhkiG9w0BAQEFAASCJCwwgiQoAgEAAoIIAQCbLHBfqRA3H4tI @@ -453,7 +467,7 @@ public static void ReadWrite16384Pkcs8() } [Fact] - public static void ReadWriteDiminishedDPPkcs8() + public void ReadWriteDiminishedDPPkcs8() { ReadWriteBase64Pkcs8( @" @@ -469,7 +483,7 @@ public static void ReadWriteDiminishedDPPkcs8() } [Fact] - public static void ReadEncryptedDiminishedDP() + public void ReadEncryptedDiminishedDP() { // PBES1: PbeWithMD5AndDESCBC const string base64 = @" @@ -494,7 +508,7 @@ public static void ReadEncryptedDiminishedDP() } [Fact] - public static void ReadEncryptedRsa1032() + public void ReadEncryptedRsa1032() { // PBES2: PBKDF2 + aes192 const string base64 = @" @@ -525,9 +539,11 @@ public static void ReadEncryptedRsa1032() TestData.RSA1032Parameters); } - [ConditionalFact(typeof(ImportExport), nameof(ImportExport.Supports16384))] - public static void ReadEncryptedRsa16384() + [ConditionalFact] + public void ReadEncryptedRsa16384() { + SkipTestException.ThrowUnless(RSAFactory.Supports16384); + // PBES2: PBKDF2 + des (single DES, not 3DES). const string base64 = @" MIIkizA9BgkqhkiG9w0BBQ0wMDAbBgkqhkiG9w0BBQwwDgQI63upT8JPNNcCAggA @@ -737,7 +753,7 @@ public static void ReadEncryptedRsa16384() } [ConditionalFact(typeof(RC2Factory), nameof(RC2Factory.IsSupported))] - public static void ReadPbes2Rc2EncryptedDiminishedDP() + public void ReadPbes2Rc2EncryptedDiminishedDP() { // PBES2: PBKDF2 + RC2-128 const string base64 = @" @@ -763,7 +779,7 @@ public static void ReadPbes2Rc2EncryptedDiminishedDP() } [ConditionalFact(typeof(RC2Factory), nameof(RC2Factory.IsSupported))] - public static void ReadPbes2Rc2EncryptedDiminishedDP_PasswordBytes() + public void ReadPbes2Rc2EncryptedDiminishedDP_PasswordBytes() { // PBES2: PBKDF2 + RC2-128 // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Suppression approved. Unit test key.")] @@ -791,7 +807,7 @@ public static void ReadPbes2Rc2EncryptedDiminishedDP_PasswordBytes() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/62547", TestPlatforms.Android)] - public static void ReadEncryptedDiminishedDP_EmptyPassword() + public void ReadEncryptedDiminishedDP_EmptyPassword() { // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Suppression approved. Unit test key.")] const string base64 = @" @@ -816,7 +832,7 @@ public static void ReadEncryptedDiminishedDP_EmptyPassword() } [Fact] - public static void ReadEncryptedDiminishedDP_EmptyPasswordBytes() + public void ReadEncryptedDiminishedDP_EmptyPasswordBytes() { // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Suppression approved. Unit test key.")] const string base64 = @" @@ -840,9 +856,11 @@ public static void ReadEncryptedDiminishedDP_EmptyPasswordBytes() TestData.DiminishedDPParameters); } - [ConditionalFact(typeof(RSAKeyFileTests), nameof(Supports384BitPrivateKeyAndRC2))] - public static void ReadPbes1Rc2EncryptedRsa384() + [ConditionalFact] + public void ReadPbes1Rc2EncryptedRsa384() { + SkipTestException.ThrowUnless(Supports384BitPrivateKeyAndRC2); + // PbeWithSha1AndRC2CBC const string base64 = @" MIIBMTAbBgkqhkiG9w0BBQswDgQIboOZHKKNEM8CAggABIIBEKOc+r+d5gI+TK7V @@ -864,7 +882,7 @@ public static void ReadPbes1Rc2EncryptedRsa384() } [Fact] - public static void NoFuzzyRSAPublicKey() + public void NoFuzzyRSAPublicKey() { using (RSA key = RSAFactory.Create()) { @@ -907,7 +925,7 @@ public static void NoFuzzyRSAPublicKey() } [Fact] - public static void NoFuzzySubjectPublicKeyInfo() + public void NoFuzzySubjectPublicKeyInfo() { using (RSA key = RSAFactory.Create()) { @@ -950,7 +968,7 @@ public static void NoFuzzySubjectPublicKeyInfo() } [Fact] - public static void NoFuzzyRSAPrivateKey() + public void NoFuzzyRSAPrivateKey() { using (RSA key = RSAFactory.Create()) { @@ -993,7 +1011,7 @@ public static void NoFuzzyRSAPrivateKey() } [Fact] - public static void NoFuzzyPkcs8() + public void NoFuzzyPkcs8() { using (RSA key = RSAFactory.Create()) { @@ -1037,7 +1055,7 @@ public static void NoFuzzyPkcs8() } [Fact] - public static void NoFuzzyEncryptedPkcs8() + public void NoFuzzyEncryptedPkcs8() { using (RSA key = RSAFactory.Create()) { @@ -1074,7 +1092,7 @@ public static void NoFuzzyEncryptedPkcs8() } [Fact] - public static void NoPrivKeyFromPublicOnly() + public void NoPrivKeyFromPublicOnly() { using (RSA key = RSAFactory.Create()) { @@ -1114,7 +1132,7 @@ public static void NoPrivKeyFromPublicOnly() } [Fact] - public static void BadPbeParameters() + public void BadPbeParameters() { using (RSA key = RSAFactory.Create()) { @@ -1236,7 +1254,7 @@ public static void BadPbeParameters() } [Fact] - public static void DecryptPkcs12WithBytes() + public void DecryptPkcs12WithBytes() { using (RSA key = RSAFactory.Create()) { @@ -1257,7 +1275,7 @@ public static void DecryptPkcs12WithBytes() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/62547", TestPlatforms.Android)] - public static void DecryptPkcs12PbeTooManyIterations() + public void DecryptPkcs12PbeTooManyIterations() { // pbeWithSHAAnd3-KeyTripleDES-CBC with 600,001 iterations byte[] high3DesIterationKey = Convert.FromBase64String(@" @@ -1294,7 +1312,7 @@ public static void DecryptPkcs12PbeTooManyIterations() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/62547", TestPlatforms.Android)] - public static void ReadWriteRsa2048EncryptedPkcs8_Pbes2HighIterations() + public void ReadWriteRsa2048EncryptedPkcs8_Pbes2HighIterations() { // pkcs5PBES2 hmacWithSHA256 aes128-CBC with 600,001 iterations ReadBase64EncryptedPkcs8(@" @@ -1330,7 +1348,7 @@ public static void ReadWriteRsa2048EncryptedPkcs8_Pbes2HighIterations() TestData.RSA2048Params); } - private static void ReadBase64EncryptedPkcs8( + private void ReadBase64EncryptedPkcs8( string base64EncPkcs8, string password, PbeParameters pbeParameters, @@ -1347,7 +1365,7 @@ private static void ReadBase64EncryptedPkcs8( isEncrypted: true); } - private static void ReadBase64EncryptedPkcs8( + private void ReadBase64EncryptedPkcs8( string base64EncPkcs8, byte[] passwordBytes, PbeParameters pbeParameters, @@ -1364,7 +1382,7 @@ private static void ReadBase64EncryptedPkcs8( isEncrypted: true); } - private static void ReadWriteBase64PublicPkcs1( + private void ReadWriteBase64PublicPkcs1( string base64PublicPkcs1, in RSAParameters expected) { @@ -1384,7 +1402,7 @@ private static void ReadWriteBase64PublicPkcs1( rsa.TryExportRSAPublicKey(destination, out written)); } - private static void ReadWriteBase64SubjectPublicKeyInfo( + private void ReadWriteBase64SubjectPublicKeyInfo( string base64SubjectPublicKeyInfo, in RSAParameters expected) { @@ -1404,7 +1422,7 @@ private static void ReadWriteBase64SubjectPublicKeyInfo( rsa.TryExportSubjectPublicKeyInfo(destination, out written)); } - private static void ReadWriteBase64PrivatePkcs1( + private void ReadWriteBase64PrivatePkcs1( string base64PrivatePkcs1, in RSAParameters expected) { @@ -1418,7 +1436,7 @@ private static void ReadWriteBase64PrivatePkcs1( rsa.TryExportRSAPrivateKey(destination, out written)); } - private static void ReadWriteBase64Pkcs8(string base64Pkcs8, in RSAParameters expected) + private void ReadWriteBase64Pkcs8(string base64Pkcs8, in RSAParameters expected) { ReadWriteKey( base64Pkcs8, @@ -1430,7 +1448,7 @@ private static void ReadWriteBase64Pkcs8(string base64Pkcs8, in RSAParameters ex rsa.TryExportPkcs8PrivateKey(destination, out written)); } - private static void ReadWriteKey( + private void ReadWriteKey( string base64, in RSAParameters expected, ReadKeyAction readAction, 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..a11b3ba92da794 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,14 +8,16 @@ namespace System.Security.Cryptography.Rsa.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public static class RSAKeyPemTests + public abstract class RSAKeyPemTests { private const string AmbiguousExceptionMarker = "multiple keys"; private const string EncryptedExceptionMarker = "encrypted key"; private const string NoPemExceptionMarker = "No supported key"; + protected abstract RSAProvider RSAFactory { get; } + [Fact] - public static void ImportFromPem_NoPem() + public void ImportFromPem_NoPem() { using (RSA rsa = RSAFactory.Create()) { @@ -26,7 +28,7 @@ public static void ImportFromPem_NoPem() } [Fact] - public static void ImportFromPem_RSAPrivateKey_Simple() + public void ImportFromPem_RSAPrivateKey_Simple() { using (RSA rsa = RSAFactory.Create()) { @@ -49,7 +51,7 @@ public static void ImportFromPem_RSAPrivateKey_Simple() } [Fact] - public static void ImportFromPem_Pkcs8UnEncrypted_Simple() + public void ImportFromPem_Pkcs8UnEncrypted_Simple() { using (RSA rsa = RSAFactory.Create()) { @@ -73,7 +75,7 @@ public static void ImportFromPem_Pkcs8UnEncrypted_Simple() } [Fact] - public static void ImportFromPem_Pkcs8UnEncrypted_UnrelatedAlgorithmIsIgnored() + public void ImportFromPem_Pkcs8UnEncrypted_UnrelatedAlgorithmIsIgnored() { using (RSA rsa = RSAFactory.Create()) { @@ -102,7 +104,7 @@ public static void ImportFromPem_Pkcs8UnEncrypted_UnrelatedAlgorithmIsIgnored() } [Fact] - public static void ImportFromPem_SubjectPublicKeyInfo_Simple() + public void ImportFromPem_SubjectPublicKeyInfo_Simple() { using (RSA rsa = RSAFactory.Create()) { @@ -114,12 +116,12 @@ public static void ImportFromPem_SubjectPublicKeyInfo_Simple() rsa.ImportFromPem(pem); RSAParameters rsaParameters = rsa.ExportParameters(false); - RSATestHelpers.AssertKeyEquals(TestData.DiminishedDPParameters.ToPublic(), rsaParameters); + RSATestHelpers.AssertKeyEquals(ToPublic(TestData.DiminishedDPParameters), rsaParameters); } } [Fact] - public static void ImportFromPem_SubjectPublicKeyInfo_IgnoresUnrelatedAlgorithm() + public void ImportFromPem_SubjectPublicKeyInfo_IgnoresUnrelatedAlgorithm() { using (RSA rsa = RSAFactory.Create()) { @@ -136,12 +138,12 @@ public static void ImportFromPem_SubjectPublicKeyInfo_IgnoresUnrelatedAlgorithm( rsa.ImportFromPem(pem); RSAParameters rsaParameters = rsa.ExportParameters(false); - RSATestHelpers.AssertKeyEquals(TestData.DiminishedDPParameters.ToPublic(), rsaParameters); + RSATestHelpers.AssertKeyEquals(ToPublic(TestData.DiminishedDPParameters), rsaParameters); } } [Fact] - public static void ImportFromPem_RSAPublicKey_Simple() + public void ImportFromPem_RSAPublicKey_Simple() { using (RSA rsa = RSAFactory.Create()) { @@ -154,12 +156,12 @@ public static void ImportFromPem_RSAPublicKey_Simple() rsa.ImportFromPem(pem); RSAParameters rsaParameters = rsa.ExportParameters(false); - RSATestHelpers.AssertKeyEquals(TestData.DiminishedDPParameters.ToPublic(), rsaParameters); + RSATestHelpers.AssertKeyEquals(ToPublic(TestData.DiminishedDPParameters), rsaParameters); } } [Fact] - public static void ImportFromPem_RSAPrivateKey_PrecedingUnrelatedPem() + public void ImportFromPem_RSAPrivateKey_PrecedingUnrelatedPem() { using (RSA rsa = RSAFactory.Create()) { @@ -196,7 +198,7 @@ public static void ImportFromPem_RSAPrivateKey_PrecedingUnrelatedPem() } [Fact] - public static void ImportFromPem_RSAPrivateKey_PrecedingMalformedPem() + public void ImportFromPem_RSAPrivateKey_PrecedingMalformedPem() { using (RSA rsa = RSAFactory.Create()) { @@ -221,7 +223,7 @@ public static void ImportFromPem_RSAPrivateKey_PrecedingMalformedPem() } [Fact] - public static void ImportFromPem_RSAPrivateKey_IgnoresOtherAlgorithms() + public void ImportFromPem_RSAPrivateKey_IgnoresOtherAlgorithms() { using (RSA rsa = RSAFactory.Create()) { @@ -248,7 +250,7 @@ public static void ImportFromPem_RSAPrivateKey_IgnoresOtherAlgorithms() } [Fact] - public static void ImportFromPem_RSAPrivateKey_AmbiguousKey_RSAPrivateKey() + public void ImportFromPem_RSAPrivateKey_AmbiguousKey_RSAPrivateKey() { using (RSA rsa = RSAFactory.Create()) { @@ -271,7 +273,7 @@ public static void ImportFromPem_RSAPrivateKey_AmbiguousKey_RSAPrivateKey() } [Fact] - public static void ImportFromPem_RSAPrivateKey_AmbiguousKey_SubjectPublicKeyInfo() + public void ImportFromPem_RSAPrivateKey_AmbiguousKey_SubjectPublicKeyInfo() { using (RSA rsa = RSAFactory.Create()) { @@ -294,7 +296,7 @@ public static void ImportFromPem_RSAPrivateKey_AmbiguousKey_SubjectPublicKeyInfo } [Fact] - public static void ImportFromPem_RSAPrivateKey_AmbiguousKey_RSAPublicKey() + public void ImportFromPem_RSAPrivateKey_AmbiguousKey_RSAPublicKey() { using (RSA rsa = RSAFactory.Create()) { @@ -317,7 +319,7 @@ public static void ImportFromPem_RSAPrivateKey_AmbiguousKey_RSAPublicKey() } [Fact] - public static void ImportFromPem_RSAPrivateKey_AmbiguousKey_EncryptedPkcs8() + public void ImportFromPem_RSAPrivateKey_AmbiguousKey_EncryptedPkcs8() { using (RSA rsa = RSAFactory.Create()) { @@ -340,7 +342,7 @@ public static void ImportFromPem_RSAPrivateKey_AmbiguousKey_EncryptedPkcs8() } [Fact] - public static void ImportFromPem_EncryptedPrivateKeyFails() + public void ImportFromPem_EncryptedPrivateKeyFails() { using (RSA rsa = RSAFactory.Create()) { @@ -363,7 +365,7 @@ public static void ImportFromPem_EncryptedPrivateKeyFails() } [Fact] - public static void ImportFromPem_Pkcs8AlgorithmMismatch_Throws() + public void ImportFromPem_Pkcs8AlgorithmMismatch_Throws() { using (RSA rsa = RSAFactory.Create()) { @@ -383,7 +385,7 @@ The below PEM is a 1024-bit DSA key. } [Fact] - public static void ImportFromEncryptedPem_Pkcs8Encrypted_Char_Simple() + public void ImportFromEncryptedPem_Pkcs8Encrypted_Char_Simple() { using (RSA rsa = RSAFactory.Create()) { @@ -408,7 +410,7 @@ public static void ImportFromEncryptedPem_Pkcs8Encrypted_Char_Simple() } [Fact] - public static void ImportFromEncryptedPem_Pkcs8Encrypted_Byte_Simple() + public void ImportFromEncryptedPem_Pkcs8Encrypted_Byte_Simple() { using (RSA rsa = RSAFactory.Create()) { @@ -433,7 +435,7 @@ public static void ImportFromEncryptedPem_Pkcs8Encrypted_Byte_Simple() } [Fact] - public static void ImportFromEncryptedPem_Pkcs8Encrypted_AmbiguousPem() + public void ImportFromEncryptedPem_Pkcs8Encrypted_AmbiguousPem() { using (RSA rsa = RSAFactory.Create()) { @@ -469,7 +471,7 @@ public static void ImportFromEncryptedPem_Pkcs8Encrypted_AmbiguousPem() } [Fact] - public static void ImportFromEncryptedPem_Pkcs8Encrypted_Byte_NoPem() + public void ImportFromEncryptedPem_Pkcs8Encrypted_Byte_NoPem() { using (RSA rsa = RSAFactory.Create()) { @@ -481,7 +483,7 @@ public static void ImportFromEncryptedPem_Pkcs8Encrypted_Byte_NoPem() } [Fact] - public static void ImportFromEncryptedPem_NoEncryptedPem() + public void ImportFromEncryptedPem_NoEncryptedPem() { using (RSA rsa = RSAFactory.Create()) { @@ -503,7 +505,7 @@ public static void ImportFromEncryptedPem_NoEncryptedPem() } [Fact] - public static void ImportFromEncryptedPem_Pkcs8Encrypted_Char_NoPem() + public void ImportFromEncryptedPem_Pkcs8Encrypted_Char_NoPem() { using (RSA rsa = RSAFactory.Create()) { @@ -515,7 +517,7 @@ public static void ImportFromEncryptedPem_Pkcs8Encrypted_Char_NoPem() } } - private static RSAParameters ToPublic(this RSAParameters rsaParams) + private static RSAParameters ToPublic(RSAParameters rsaParams) { return new RSAParameters { diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAProvider.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAProvider.cs new file mode 100644 index 00000000000000..a167fde7e27032 --- /dev/null +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAProvider.cs @@ -0,0 +1,56 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Security.Cryptography.Rsa.Tests +{ + public abstract class RSAProvider + { + private bool? _supports16384; + + public abstract RSA Create(); + public abstract RSA Create(int keySize); + public abstract bool Supports384PrivateKey { get; } + public abstract bool SupportsLargeExponent { get; } + public abstract bool SupportsSha2Oaep { get; } + public abstract bool SupportsPss { get; } + public abstract bool SupportsSha1Signatures { get; } + public abstract bool SupportsMd5Signatures { get; } + public abstract bool SupportsSha3 { get; } + + public bool NoSupportsSha3 => !SupportsSha3; + + public bool Supports16384 => _supports16384 ??= TestRsa16384(); + + public RSA Create(RSAParameters rsaParameters) + { + RSA rsa = Create(); + rsa.ImportParameters(rsaParameters); + return rsa; + } + + private bool TestRsa16384() + { + if (PlatformDetection.IsAndroid) + { + // We cannot detect this on Android at the moment. Even attempting to generate or import a 16K RSA key + // may leave the error queue in the incorrect state. See https://github.com/google/conscrypt/issues/1507 + return false; + } + + try + { + using (RSA rsa = Create()) + { + rsa.ImportParameters(TestData.RSA16384Params); + } + + return true; + } + catch (Exception e) when (e is CryptographicException or PlatformNotSupportedException) + { + // The key is too big for this platform or the platform is not supported. + return false; + } + } + } +} diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSASignatureFormatter.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSASignatureFormatterTests.cs similarity index 86% rename from src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSASignatureFormatter.cs rename to src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSASignatureFormatterTests.cs index c1ce8e1754b87a..94d036b15f37f5 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSASignatureFormatter.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSASignatureFormatterTests.cs @@ -2,17 +2,22 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Security.Cryptography.Tests; +using Microsoft.DotNet.XUnitExtensions; using Test.Cryptography; using Xunit; namespace System.Security.Cryptography.Rsa.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public partial class RSASignatureFormatterTests : AsymmetricSignatureFormatterTests + public abstract class RSASignatureFormatterTests : AsymmetricSignatureFormatterTests { - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] - public static void VerifySignature_SHA1() + protected abstract RSAProvider RSAFactory { get; } + + [ConditionalFact] + public void VerifySignature_SHA1() { + SkipTestException.ThrowUnless(RSAFactory.SupportsSha1Signatures); + using (RSA rsa = RSAFactory.Create()) { rsa.ImportParameters(TestData.RSA2048Params); @@ -29,7 +34,7 @@ public static void VerifySignature_SHA1() } [Fact] - public static void VerifySignature_SHA256() + public void VerifySignature_SHA256() { using (RSA rsa = RSAFactory.Create()) { @@ -47,7 +52,7 @@ public static void VerifySignature_SHA256() } [Fact] - public static void InvalidHashAlgorithm() + public void InvalidHashAlgorithm() { using (RSA rsa = RSAFactory.Create()) { @@ -66,9 +71,11 @@ public static void InvalidHashAlgorithm() } } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] - public static void VerifyKnownSignature() + [ConditionalFact] + public void VerifyKnownSignature() { + SkipTestException.ThrowUnless(RSAFactory.SupportsSha1Signatures); + byte[] hash = "012d161304fa0c6321221516415813022320620c".HexToByteArray(); byte[] sig; 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..bdb1700043bdc6 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 @@ -2,16 +2,20 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; +using System.Security.Cryptography.Tests; using System.Xml.Linq; +using Microsoft.DotNet.XUnitExtensions; using Xunit; namespace System.Security.Cryptography.Rsa.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public static class RSAXml + public abstract class RSAXml { + protected abstract RSAProvider RSAFactory { get; } + [Fact] - public static void TestRead1032Parameters_Public() + public void TestRead1032Parameters_Public() { RSAParameters expectedParameters = ImportExport.MakePublic(TestData.RSA1032Parameters); @@ -33,7 +37,7 @@ public static void TestRead1032Parameters_Public() } [Fact] - public static void TestRead1032Parameters_Private() + public void TestRead1032Parameters_Private() { // Bonus trait of this XML: the root element name is wrong TestReadXml( @@ -76,9 +80,11 @@ public static void TestRead1032Parameters_Private() TestData.RSA1032Parameters); } - [ConditionalFact(typeof(ImportExport), nameof(ImportExport.Supports16384))] - public static void TestRead16384Parameters_Public() + [ConditionalFact] + public void TestRead16384Parameters_Public() { + SkipTestException.ThrowUnless(RSAFactory.Supports16384); + RSAParameters expectedParameters = ImportExport.MakePublic(TestData.RSA16384Params); // Bonus trait of this XML: the Modulus and Exponent parameters @@ -157,9 +163,11 @@ iC2wXFMDafnWp1lxXiGcVVu9dE2LeglCgnMUps9QlJD0aXaJHYi2VDQ3zFdMvn8A imlqKtZGdGf9 expectedParameters); } - [ConditionalFact(typeof(ImportExport), nameof(ImportExport.Supports16384))] - public static void TestRead16384Parameters_Private() + [ConditionalFact] + public void TestRead16384Parameters_Private() { + SkipTestException.ThrowUnless(RSAFactory.Supports16384); + // Bonus trait of this XML: the D parameter is not in // canonical order. TestReadXml( @@ -383,7 +391,7 @@ public static void TestRead16384Parameters_Private() } [Fact] - public static void TestReadDiminishedDPParameters_Public() + public void TestReadDiminishedDPParameters_Public() { RSAParameters expectedParameters = ImportExport.MakePublic(TestData.DiminishedDPParameters); @@ -404,7 +412,7 @@ public static void TestReadDiminishedDPParameters_Public() } [Fact] - public static void TestReadDiminishedDPParameters_Private_Base64Binary() + public void TestReadDiminishedDPParameters_Private_Base64Binary() { // This test uses the base64Binary version of the DP value, where the 0x00 // is written down. @@ -446,7 +454,7 @@ public static void TestReadDiminishedDPParameters_Private_Base64Binary() [Fact] [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] - public static void TestReadDiminishedDPParameters_Private_CryptoBinary() + public void TestReadDiminishedDPParameters_Private_CryptoBinary() { // This test writes the DP value as a CryptoBinary, meaning the leading // 0x00 is not written down. @@ -491,7 +499,7 @@ public static void TestReadDiminishedDPParameters_Private_CryptoBinary() [Theory] [InlineData(true)] [InlineData(false)] - public static void TestWrite1024Parameters(bool includePrivateParameters) + public void TestWrite1024Parameters(bool includePrivateParameters) { TestWriteXml( TestData.RSA1024Params, @@ -538,7 +546,7 @@ public static void TestWrite1024Parameters(bool includePrivateParameters) [Theory] [InlineData(true)] [InlineData(false)] - public static void TestWrite1032Parameters(bool includePrivateParameters) + public void TestWrite1032Parameters(bool includePrivateParameters) { TestWriteXml( TestData.RSA1032Parameters, @@ -585,7 +593,7 @@ public static void TestWrite1032Parameters(bool includePrivateParameters) [Theory] [InlineData(true)] [InlineData(false)] - public static void TestWrite2048Parameters(bool includePrivateParameters) + public void TestWrite2048Parameters(bool includePrivateParameters) { TestWriteXml( TestData.RSA2048Params, @@ -634,11 +642,13 @@ public static void TestWrite2048Parameters(bool includePrivateParameters) )); } - [ConditionalTheory(typeof(ImportExport), nameof(ImportExport.Supports16384))] + [ConditionalTheory] [InlineData(true)] [InlineData(false)] - public static void TestWrite16384Parameters(bool includePrivateParameters) + public void TestWrite16384Parameters(bool includePrivateParameters) { + SkipTestException.ThrowUnless(RSAFactory.Supports16384); + TestWriteXml( TestData.RSA16384Params, includePrivateParameters, @@ -904,7 +914,7 @@ public static void TestWrite16384Parameters(bool includePrivateParameters) [Theory] [InlineData(true)] [InlineData(false)] - public static void TestWriteDiminishedDPParameters(bool includePrivateParameters) + public void TestWriteDiminishedDPParameters(bool includePrivateParameters) { // This test checks for the base64Binary version of DP (leading 0x00 written), // instead of the CryptoBinary version (leading 0x00 removed). @@ -935,7 +945,7 @@ public static void TestWriteDiminishedDPParameters(bool includePrivateParameters [Theory] [InlineData(true)] [InlineData(false)] - public static void TestWriteUnusualExponentParameters(bool includePrivateParameters) + public void TestWriteUnusualExponentParameters(bool includePrivateParameters) { // This test ensures we pay attention to the Exponent value, instead of assuming // AQAB (0x010001 / 65537) @@ -982,7 +992,7 @@ public static void TestWriteUnusualExponentParameters(bool includePrivateParamet } [Fact] - public static void FromToXml() + public void FromToXml() { using (RSA rsa = RSAFactory.Create()) { @@ -1010,7 +1020,7 @@ public static void FromToXml() } [Fact] - public static void FromXml_MissingModulus() + public void FromXml_MissingModulus() { using (RSA rsa = RSAFactory.Create()) { @@ -1045,7 +1055,7 @@ public static void FromXml_MissingModulus() } [Fact] - public static void FromXml_MissingExponent() + public void FromXml_MissingExponent() { using (RSA rsa = RSAFactory.Create()) { @@ -1083,7 +1093,7 @@ public static void FromXml_MissingExponent() } [Fact] - public static void FromXml_MissingQ() + public void FromXml_MissingQ() { using (RSA rsa = RSAFactory.Create()) { @@ -1119,7 +1129,7 @@ public static void FromXml_MissingQ() } [Fact] - public static void FromXml_MissingDP() + public void FromXml_MissingDP() { using (RSA rsa = RSAFactory.Create()) { @@ -1155,7 +1165,7 @@ public static void FromXml_MissingDP() } [Fact] - public static void FromXml_MissingDQ() + public void FromXml_MissingDQ() { using (RSA rsa = RSAFactory.Create()) { @@ -1191,7 +1201,7 @@ public static void FromXml_MissingDQ() } [Fact] - public static void FromXml_MissingInverseQ() + public void FromXml_MissingInverseQ() { using (RSA rsa = RSAFactory.Create()) { @@ -1227,7 +1237,7 @@ public static void FromXml_MissingInverseQ() } [Fact] - public static void FromXml_BadBase64() + public void FromXml_BadBase64() { using (RSA rsa = RSAFactory.Create()) { @@ -1265,7 +1275,7 @@ public static void FromXml_BadBase64() } } - private static void TestReadXml(string xmlString, in RSAParameters expectedParameters) + private void TestReadXml(string xmlString, in RSAParameters expectedParameters) { using (RSA rsa = RSAFactory.Create()) { @@ -1281,7 +1291,7 @@ private static void TestReadXml(string xmlString, in RSAParameters expectedParam } [Fact] - public static void FromNullXml() + public void FromNullXml() { using (RSA rsa = RSAFactory.Create()) { @@ -1292,7 +1302,7 @@ public static void FromNullXml() } [Fact] - public static void FromInvalidXml() + public void FromInvalidXml() { using (RSA rsa = RSAFactory.Create()) { @@ -1341,7 +1351,7 @@ public static void FromInvalidXml() [Fact] [ActiveIssue("https://github.com/dotnet/runtime/issues/29515", TestPlatforms.OSX)] - public static void FromNonsenseXml() + public void FromNonsenseXml() { // This is DiminishedDPParameters XML, but with a P that is way too long. using (RSA rsa = RSAFactory.Create()) @@ -1382,7 +1392,7 @@ public static void FromNonsenseXml() - private static void TestWriteXml( + private void TestWriteXml( in RSAParameters keyParameters, bool includePrivateParameters, string expectedModulus, 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 a409e18b677986..1291b1fe5341e0 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 @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; +using System.Security.Cryptography.Tests; using Microsoft.DotNet.XUnitExtensions; using Test.Cryptography; using Test.IO.Streams; @@ -11,7 +12,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 { protected override byte[] SignData(RSA rsa, byte[] data, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding) => rsa.SignData(data, hashAlgorithm, padding); @@ -42,7 +43,9 @@ public void NullArray_Throws() [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] public abstract class SignVerify { - public static bool SupportsPss => RSAFactory.SupportsPss; + protected abstract RSAProvider RSAFactory { get; } + + public bool SupportsPss => RSAFactory.SupportsPss; protected abstract byte[] SignData(RSA rsa, byte[] data, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding); protected abstract byte[] SignHash(RSA rsa, byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding); @@ -148,9 +151,11 @@ public void SignEmptyHash() } } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact] public void ExpectedSignature_SHA1_384() { + SkipTestException.ThrowUnless(RSAFactory.SupportsSha1Signatures); + byte[] expectedSignature = { 0x79, 0xD9, 0x3C, 0xBF, 0x54, 0xFA, 0x55, 0x8C, @@ -179,9 +184,11 @@ public void ExpectedSignature_SHA1_384() } } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact] public void ExpectedSignature_SHA1_1032() { + SkipTestException.ThrowUnless(RSAFactory.SupportsSha1Signatures); + byte[] expectedSignature = { 0x49, 0xBC, 0x1C, 0xBE, 0x72, 0xEF, 0x83, 0x6E, @@ -206,9 +213,11 @@ public void ExpectedSignature_SHA1_1032() ExpectSignature(expectedSignature, TestData.HelloBytes, "SHA1", TestData.RSA1032Parameters); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact] public void ExpectedSignature_SHA1_2048() { + SkipTestException.ThrowUnless(RSAFactory.SupportsSha1Signatures); + byte[] expectedSignature = new byte[] { 0xA1, 0xFC, 0x74, 0x67, 0x49, 0x91, 0xF4, 0x28, @@ -351,9 +360,11 @@ public void ExpectSignature_SHA256_1024_Stream() Assert.Equal(expectedSignature, signature); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact] public void VerifySignature_SHA1_384() { + SkipTestException.ThrowUnless(RSAFactory.SupportsSha1Signatures); + byte[] signature = { 0x79, 0xD9, 0x3C, 0xBF, 0x54, 0xFA, 0x55, 0x8C, @@ -367,9 +378,11 @@ public void VerifySignature_SHA1_384() VerifySignature(signature, TestData.HelloBytes, "SHA1", TestData.RSA384Parameters); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact] public void VerifySignature_SHA1_1032() { + SkipTestException.ThrowUnless(RSAFactory.SupportsSha1Signatures); + byte[] signature = { 0x49, 0xBC, 0x1C, 0xBE, 0x72, 0xEF, 0x83, 0x6E, @@ -394,9 +407,11 @@ public void VerifySignature_SHA1_1032() VerifySignature(signature, TestData.HelloBytes, "SHA1", TestData.RSA1032Parameters); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact] public void VerifySignature_SHA1_2048() { + SkipTestException.ThrowUnless(RSAFactory.SupportsSha1Signatures); + byte[] signature = new byte[] { 0xA1, 0xFC, 0x74, 0x67, 0x49, 0x91, 0xF4, 0x28, @@ -504,9 +519,11 @@ public void VerifySignature_SHA256_2048() VerifySignature(signature, TestData.HelloBytes, "SHA256", TestData.RSA2048Params); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha3))] + [ConditionalFact] public void VerifySignature_SHA3_256_RSA2048() { + SkipTestException.ThrowUnless(RSAFactory.SupportsSha3); + byte[] signature = new byte[] { 0x37, 0xfc, 0x5b, 0xa7, 0xcd, 0x72, 0x93, 0x55, 0x43, 0x6f, 0x2c, 0x5c, 0xce, 0x10, 0x20, 0x67, @@ -530,9 +547,11 @@ public void VerifySignature_SHA3_256_RSA2048() VerifySignature(signature, TestData.HelloBytes, HashAlgorithmName.SHA3_256.Name, TestData.RSA2048Params); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha3))] + [ConditionalFact] public void VerifySignature_SHA3_384_RSA2048() { + SkipTestException.ThrowUnless(RSAFactory.SupportsSha3); + byte[] signature = new byte[] { 0x96, 0x76, 0xdf, 0xa3, 0xd3, 0xac, 0x74, 0x08, 0x18, 0x41, 0x0a, 0xa1, 0x03, 0x6b, 0x28, 0x5d, @@ -556,9 +575,11 @@ public void VerifySignature_SHA3_384_RSA2048() VerifySignature(signature, TestData.HelloBytes, HashAlgorithmName.SHA3_384.Name, TestData.RSA2048Params); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha3))] + [ConditionalFact] public void VerifySignature_SHA3_512_RSA2048() { + SkipTestException.ThrowUnless(RSAFactory.SupportsSha3); + byte[] signature = new byte[] { 0x18, 0x83, 0xab, 0x01, 0xc1, 0xaf, 0x98, 0xa9, 0xc2, 0xde, 0x2d, 0x63, 0x4f, 0x8a, 0xf6, 0x47, @@ -584,8 +605,18 @@ public void VerifySignature_SHA3_512_RSA2048() [Theory] [MemberData(nameof(RoundTripTheories))] - public void SignAndVerify_Roundtrip(string hashAlgorithm, RSAParameters rsaParameters) + public void SignAndVerify_Roundtrip( + string hashAlgorithm, + RSAParameters rsaParameters, + bool requiresSha1Signature, + bool requiresMd5Signature) { + if ((requiresSha1Signature && !RSAFactory.SupportsSha1Signatures) || + (requiresMd5Signature && !RSAFactory.SupportsMd5Signatures)) + { + return; + } + SignAndVerify(TestData.HelloBytes, hashAlgorithm, rsaParameters); } @@ -595,21 +626,13 @@ public static IEnumerable RoundTripTheories { foreach (RSAParameters rsaParameters in new[] { TestData.RSA1024Params, TestData.RSA2048Params }) { - if (RSAFactory.SupportsSha1Signatures) - { - yield return new object[] { nameof(HashAlgorithmName.SHA1), rsaParameters }; - } - - if (RSAFactory.SupportsMd5Signatures) - { - yield return new object[] { nameof(HashAlgorithmName.MD5), rsaParameters }; - } - - yield return new object[] { nameof(HashAlgorithmName.SHA256), rsaParameters }; + yield return new object[] { nameof(HashAlgorithmName.SHA1), rsaParameters, true, false }; + yield return new object[] { nameof(HashAlgorithmName.MD5), rsaParameters, false, true }; + yield return new object[] { nameof(HashAlgorithmName.SHA256), rsaParameters, false, false }; } - yield return new object[] { nameof(HashAlgorithmName.SHA384), TestData.RSA2048Params }; - yield return new object[] { nameof(HashAlgorithmName.SHA512), TestData.RSA2048Params }; + yield return new object[] { nameof(HashAlgorithmName.SHA384), TestData.RSA2048Params, false, false }; + yield return new object[] { nameof(HashAlgorithmName.SHA512), TestData.RSA2048Params, false, false }; } } @@ -697,9 +720,11 @@ public void PkcsSignHash_MismatchedHashSize() } } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact] public void ExpectedHashSignature_SHA1_2048() { + SkipTestException.ThrowUnless(RSAFactory.SupportsSha1Signatures); + byte[] expectedHashSignature = new byte[] { 0xA1, 0xFC, 0x74, 0x67, 0x49, 0x91, 0xF4, 0x28, @@ -828,9 +853,11 @@ public void ExpectedHashSignature_SHA256_2048() ExpectHashSignature(expectedHashSignature, dataHash, "SHA256", TestData.RSA2048Params); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact] public void VerifyHashSignature_SHA1_2048() { + SkipTestException.ThrowUnless(RSAFactory.SupportsSha1Signatures); + byte[] hashSignature = new byte[] { 0xA1, 0xFC, 0x74, 0x67, 0x49, 0x91, 0xF4, 0x28, @@ -961,8 +988,13 @@ public void VerifyHashSignature_SHA256_2048() [Theory] [MemberData(nameof(HashAlgorithmNames))] - public void PssRoundtrip(string hashAlgorithmName) + public void PssRoundtrip(string hashAlgorithmName, Func isSupported) { + if (!isSupported(RSAFactory)) + { + return; + } + RSAParameters privateParameters = TestData.RSA2048Params; RSAParameters publicParameters = new RSAParameters { @@ -1022,9 +1054,11 @@ public void VerifyExpectedSignature_PssSha256_RSA2048() modulus2048Signature); } - [ConditionalFact(typeof(ImportExport), nameof(ImportExport.Supports16384))] + [ConditionalFact] public void VerifyExpectedSignature_PssSha256_RSA16384() { + SkipTestException.ThrowUnless(RSAFactory.Supports16384); + byte[] modulus2048Signature = ( "1D92D529567F6922866FFDE4BF44C427FA511BF5EDF163ED51A0D14ADECD98FB" + "C6A61A61532404AF74C3AB65119BB1358855A68362FBABAED7D8E56403EE9AFA" + @@ -1098,9 +1132,11 @@ public void VerifyExpectedSignature_PssSha256_RSA16384() modulus2048Signature); } - [ConditionalFact(typeof(ImportExport), nameof(ImportExport.Supports16384))] + [ConditionalFact] public void VerifyExpectedSignature_PssSha384() { + SkipTestException.ThrowUnless(RSAFactory.Supports16384); + byte[] bigModulusSignature = ( "70F48CA4E8640701369DB986C4D09C91E4C197DB1BE4F32C3F37A67AEC4BA95D" + "733EAACAE139B7B9C8E66C5BC82629971C3BEBF93A949CB81763FECDF96B73DA" + @@ -1135,9 +1171,11 @@ public void VerifyExpectedSignature_PssSha512() helloSignature); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha3))] + [ConditionalFact] public void VerifyExpectedSignature_PssSha3_256() { + SkipTestException.ThrowUnless(RSAFactory.SupportsSha3); + // Signature independently created with // echo -n Hello | openssl dgst -sha3-256 -binary | openssl pkeyutl -sign -inkey key.pem -pkeyopt rsa_padding_mode:pss -pkeyopt digest:sha3-256 | xxd -i -c 16 byte[] helloSignature = @@ -1167,9 +1205,11 @@ public void VerifyExpectedSignature_PssSha3_256() helloSignature); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha3))] + [ConditionalFact] public void VerifyExpectedSignature_PssSha3_384() { + SkipTestException.ThrowUnless(RSAFactory.SupportsSha3); + // Signature independently created with // echo -n Hello | openssl dgst -sha3-384 -binary | openssl pkeyutl -sign -inkey key.pem -pkeyopt rsa_padding_mode:pss -pkeyopt digest:sha3-384 | xxd -i -c 16 byte[] helloSignature = @@ -1199,9 +1239,11 @@ public void VerifyExpectedSignature_PssSha3_384() helloSignature); } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha3))] + [ConditionalFact] public void VerifyExpectedSignature_PssSha3_512() { + SkipTestException.ThrowUnless(RSAFactory.SupportsSha3); + // Signature independently created with // echo -n Hello | openssl dgst -sha3-512 -binary | openssl pkeyutl -sign -inkey key.pem -pkeyopt rsa_padding_mode:pss -pkeyopt digest:sha3-512 | xxd -i -c 16 byte[] helloSignature = @@ -1231,12 +1273,14 @@ public void VerifyExpectedSignature_PssSha3_512() helloSignature); } - [ConditionalTheory(typeof(RSAFactory), nameof(RSAFactory.NoSupportsSha3))] + [ConditionalTheory] [InlineData("SHA3-256")] [InlineData("SHA3-384")] [InlineData("SHA3-512")] public void Pkcs1UnsupportedHashAlgorithm(string hashAlgorithm) { + SkipTestException.ThrowUnless(RSAFactory.NoSupportsSha3); + using (RSA rsa = RSAFactory.Create()) { Exception ex = Assert.ThrowsAny(() => @@ -1255,12 +1299,14 @@ public void Pkcs1UnsupportedHashAlgorithm(string hashAlgorithm) } } - [ConditionalTheory(typeof(RSAFactory), nameof(RSAFactory.NoSupportsSha3))] + [ConditionalTheory] [InlineData("SHA3-256")] [InlineData("SHA3-384")] [InlineData("SHA3-512")] public void PssUnsupportedHashAlgorithm(string hashAlgorithm) { + SkipTestException.ThrowUnless(RSAFactory.NoSupportsSha3); + using (RSA rsa = RSAFactory.Create()) { Exception ex = Assert.ThrowsAny(() => @@ -1337,9 +1383,11 @@ private void VerifyExpectedSignature_Pss( } } - [ConditionalFact(typeof(SignVerify), nameof(SupportsPss))] + [ConditionalFact] public void PssSignature_WrongHashAlgorithm() { + SkipTestException.ThrowUnless(RSAFactory.SupportsPss); + RSASignaturePadding padding = RSASignaturePadding.Pss; byte[] data = TestData.HelloBytes; @@ -1350,10 +1398,12 @@ public void PssSignature_WrongHashAlgorithm() } } - [ConditionalFact(typeof(SignVerify), nameof(SupportsPss))] + [ConditionalFact] [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] public void PssVerifyHash_MismatchedHashSize() { + SkipTestException.ThrowUnless(RSAFactory.SupportsPss); + // This is a legal SHA-1 value, which we're going to use with SHA-2-256 instead. byte[] hash = "75ED7E627DB9AECBD870E27EED49ED7D9AEB2F52".HexToByteArray(); @@ -1373,10 +1423,12 @@ public void PssVerifyHash_MismatchedHashSize() } } - [ConditionalFact(typeof(SignVerify), nameof(SupportsPss))] + [ConditionalFact] [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)] public void PssSignHash_MismatchedHashSize() { + SkipTestException.ThrowUnless(RSAFactory.SupportsPss); + RSASignaturePadding padding = RSASignaturePadding.Pss; using (RSA rsa = RSAFactory.Create(TestData.RSA2048Params)) @@ -1397,9 +1449,11 @@ public void PssSignHash_MismatchedHashSize() } } - [ConditionalFact(typeof(SignVerify), nameof(SupportsPss))] + [ConditionalFact] public void PssSignature_WrongData() { + SkipTestException.ThrowUnless(RSAFactory.SupportsPss); + RSASignaturePadding padding = RSASignaturePadding.Pss; byte[] dataCopy = (byte[])TestData.HelloBytes.Clone(); HashAlgorithmName hashAlgorithmName = HashAlgorithmName.SHA256; @@ -1412,9 +1466,11 @@ public void PssSignature_WrongData() } } - [ConditionalFact(typeof(SignVerify), nameof(SupportsPss))] + [ConditionalFact] public void PssSignature_WrongLength() { + SkipTestException.ThrowUnless(RSAFactory.SupportsPss); + RSASignaturePadding padding = RSASignaturePadding.Pss; byte[] data = TestData.HelloBytes; HashAlgorithmName hashAlgorithmName = HashAlgorithmName.SHA256; @@ -1590,26 +1646,17 @@ public static IEnumerable HashAlgorithmNames { get { - yield return new object[] { HashAlgorithmName.SHA256.Name }; - yield return new object[] { HashAlgorithmName.SHA384.Name }; - yield return new object[] { HashAlgorithmName.SHA512.Name }; - - if (RSAFactory.SupportsMd5Signatures) - { - yield return new object[] { HashAlgorithmName.MD5.Name }; - } + yield return new object[] { HashAlgorithmName.SHA256.Name, (RSAProvider _) => true }; + yield return new object[] { HashAlgorithmName.SHA384.Name, (RSAProvider _) => true }; + yield return new object[] { HashAlgorithmName.SHA512.Name, (RSAProvider _) => true }; - if (RSAFactory.SupportsSha1Signatures) - { - yield return new object[] { HashAlgorithmName.SHA1.Name }; - } + yield return new object[] { HashAlgorithmName.MD5.Name, (RSAProvider p) => p.SupportsMd5Signatures }; - if (RSAFactory.SupportsSha3) - { - yield return new object[] { HashAlgorithmName.SHA3_256.Name }; - yield return new object[] { HashAlgorithmName.SHA3_384.Name }; - yield return new object[] { HashAlgorithmName.SHA3_512.Name }; - } + yield return new object[] { HashAlgorithmName.SHA1.Name, (RSAProvider p) => p.SupportsSha1Signatures }; + + yield return new object[] { HashAlgorithmName.SHA3_256.Name, (RSAProvider p) => p.SupportsSha3 }; + yield return new object[] { HashAlgorithmName.SHA3_384.Name, (RSAProvider p) => p.SupportsSha3 }; + yield return new object[] { HashAlgorithmName.SHA3_512.Name, (RSAProvider p) => p.SupportsSha3 }; } } } 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..39237083ff7de2 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 { 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 { 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 { 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)); @@ -83,7 +83,7 @@ private static byte[] TryWithOutputArray(Func func) } [Fact] - public static void SignDefaultSpanHash() + public void SignDefaultSpanHash() { using (RSA rsa = RSAFactory.Create()) { @@ -98,7 +98,7 @@ public static void SignDefaultSpanHash() } [Fact] - public static void VerifyDefaultSpanHash() + public void VerifyDefaultSpanHash() { using (RSA rsa = RSAFactory.Create()) { diff --git a/src/libraries/System.Security.Cryptography.Cng/tests/RSACngProvider.cs b/src/libraries/System.Security.Cryptography.Cng/tests/RSACngProvider.cs deleted file mode 100644 index 8b831ea9b041f1..00000000000000 --- a/src/libraries/System.Security.Cryptography.Cng/tests/RSACngProvider.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Runtime.InteropServices; -using Test.Cryptography; - -namespace System.Security.Cryptography.Rsa.Tests -{ - public class RSACngProvider : IRSAProvider - { - public RSA Create() => new RSACng(); - - public RSA Create(int keySize) => new RSACng(keySize); - - public bool Supports384PrivateKey => PlatformSupport.IsRSA384Supported; - - public bool SupportsLargeExponent => true; - - public bool SupportsSha2Oaep => true; - - public bool SupportsPss => true; - - public bool SupportsSha1Signatures => true; - - public bool SupportsMd5Signatures => true; - - 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(); - } -} diff --git a/src/libraries/System.Security.Cryptography.Cng/tests/System.Security.Cryptography.Cng.Tests.csproj b/src/libraries/System.Security.Cryptography.Cng/tests/System.Security.Cryptography.Cng.Tests.csproj index eadc4248aeec39..b9dbe959784beb 100644 --- a/src/libraries/System.Security.Cryptography.Cng/tests/System.Security.Cryptography.Cng.Tests.csproj +++ b/src/libraries/System.Security.Cryptography.Cng/tests/System.Security.Cryptography.Cng.Tests.csproj @@ -10,7 +10,6 @@ - @@ -43,23 +42,6 @@ Link="CommonTest\System\Security\Cryptography\CryptoUtils.cs" /> - - - - - - - - - @@ -68,24 +50,6 @@ - - - - - - - - - new RSACryptoServiceProvider(); - - public RSA Create(int keySize) => new RSACryptoServiceProvider(keySize); - - public bool Supports384PrivateKey => PlatformSupport.IsRSA384Supported; - - public bool SupportsLargeExponent => false; - - public bool SupportsSha2Oaep => false; - - public bool SupportsPss => false; - - public bool SupportsSha1Signatures => _supportsSha1Signatures ??= SignatureSupport.CanProduceSha1Signature(Create()); - public bool SupportsMd5Signatures => _supportsMd5Signatures ??= SignatureSupport.CanProduceMd5Signature(Create()); - - public bool SupportsSha3 => false; - } - - public partial class RSAFactory - { - private static readonly IRSAProvider s_provider = new RSACryptoServiceProviderProvider(); - } -} diff --git a/src/libraries/System.Security.Cryptography.Csp/tests/System.Security.Cryptography.Csp.Tests.csproj b/src/libraries/System.Security.Cryptography.Csp/tests/System.Security.Cryptography.Csp.Tests.csproj index d5e19fdccac64c..40a78160fa01d2 100644 --- a/src/libraries/System.Security.Cryptography.Csp/tests/System.Security.Cryptography.Csp.Tests.csproj +++ b/src/libraries/System.Security.Cryptography.Csp/tests/System.Security.Cryptography.Csp.Tests.csproj @@ -11,10 +11,6 @@ - - - - @@ -28,22 +24,6 @@ Link="CommonTest\System\Security\Cryptography\PlatformSupport.cs" /> - - - - - - - - @@ -56,8 +36,6 @@ - - - - - - - - diff --git a/src/libraries/System.Security.Cryptography.OpenSsl/tests/RSAOpenSslProvider.cs b/src/libraries/System.Security.Cryptography.OpenSsl/tests/RSAOpenSslProvider.cs deleted file mode 100644 index 18aa9528877d9d..00000000000000 --- a/src/libraries/System.Security.Cryptography.OpenSsl/tests/RSAOpenSslProvider.cs +++ /dev/null @@ -1,35 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Security.Cryptography.Tests; - -namespace System.Security.Cryptography.Rsa.Tests -{ - public class RSAOpenSslProvider : IRSAProvider - { - private bool? _supportsSha1Signatures; - private bool? _supportsMd5Signatures; - - public RSA Create() => new RSAOpenSsl(); - - public RSA Create(int keySize) => new RSAOpenSsl(keySize); - - public bool Supports384PrivateKey => true; - - public bool SupportsLargeExponent => true; - - public bool SupportsSha2Oaep => true; - - public bool SupportsPss => true; - - public bool SupportsSha1Signatures => _supportsSha1Signatures ??= SignatureSupport.CanProduceSha1Signature(Create()); - public bool SupportsMd5Signatures => _supportsMd5Signatures ??= SignatureSupport.CanProduceMd5Signature(Create()); - - 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(); - } -} diff --git a/src/libraries/System.Security.Cryptography.OpenSsl/tests/System.Security.Cryptography.OpenSsl.Tests.csproj b/src/libraries/System.Security.Cryptography.OpenSsl/tests/System.Security.Cryptography.OpenSsl.Tests.csproj index c9c4f20f33186b..1a74cbf937367d 100644 --- a/src/libraries/System.Security.Cryptography.OpenSsl/tests/System.Security.Cryptography.OpenSsl.Tests.csproj +++ b/src/libraries/System.Security.Cryptography.OpenSsl/tests/System.Security.Cryptography.OpenSsl.Tests.csproj @@ -10,7 +10,6 @@ - - - - - - - - - - - - - - - - - - - diff --git a/src/libraries/System.Security.Cryptography/tests/AsymmetricSignatureFormatterTests.cs b/src/libraries/System.Security.Cryptography/tests/AsymmetricSignatureFormatterTests.cs index 5704de3fd56d5f..654fd047c4782d 100644 --- a/src/libraries/System.Security.Cryptography/tests/AsymmetricSignatureFormatterTests.cs +++ b/src/libraries/System.Security.Cryptography/tests/AsymmetricSignatureFormatterTests.cs @@ -11,14 +11,14 @@ namespace System.Security.Cryptography.Tests [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] public partial class AsymmetricSignatureFormatterTests { - protected static void InvalidFormatterArguments(AsymmetricSignatureFormatter formatter) + protected internal static void InvalidFormatterArguments(AsymmetricSignatureFormatter formatter) { Assert.Throws(() => formatter.SetKey(null)); Assert.Throws(() => formatter.CreateSignature((byte[])null)); Assert.Throws(() => formatter.CreateSignature(new byte[] { 0, 1, 2, 3 })); } - protected static void InvalidDeformatterArguments(AsymmetricSignatureDeformatter deformatter) + protected internal static void InvalidDeformatterArguments(AsymmetricSignatureDeformatter deformatter) { Assert.Throws(() => deformatter.SetKey(null)); Assert.Throws(() => deformatter.VerifySignature((byte[])null, new byte[] { 0, 1, 2 })); diff --git a/src/libraries/System.Security.Cryptography/tests/DefaultRSAProvider.cs b/src/libraries/System.Security.Cryptography/tests/DefaultRSAProvider.cs index affcb571f1307f..b2c736c76768cf 100644 --- a/src/libraries/System.Security.Cryptography/tests/DefaultRSAProvider.cs +++ b/src/libraries/System.Security.Cryptography/tests/DefaultRSAProvider.cs @@ -7,14 +7,18 @@ namespace System.Security.Cryptography.Rsa.Tests { - public class DefaultRSAProvider : IRSAProvider + public sealed class DefaultRSAProvider : RSAProvider { + public static readonly DefaultRSAProvider Instance = new DefaultRSAProvider(); + private bool? _supportsSha1Signatures; private bool? _supportsMd5Signatures; - public RSA Create() => RSA.Create(); + private DefaultRSAProvider() { } + + public override RSA Create() => RSA.Create(); - public RSA Create(int keySize) + public override RSA Create(int keySize) { #if NET return RSA.Create(keySize); @@ -26,21 +30,16 @@ public RSA Create(int keySize) #endif } - public bool Supports384PrivateKey => PlatformSupport.IsRSA384Supported; - public bool SupportsSha1Signatures => _supportsSha1Signatures ??= SignatureSupport.CanProduceSha1Signature(Create()); - public bool SupportsMd5Signatures => _supportsMd5Signatures ??= SignatureSupport.CanProduceMd5Signature(Create()); + public override bool Supports384PrivateKey => PlatformSupport.IsRSA384Supported; + public override bool SupportsSha1Signatures => _supportsSha1Signatures ??= SignatureSupport.CanProduceSha1Signature(Create()); + public override bool SupportsMd5Signatures => _supportsMd5Signatures ??= SignatureSupport.CanProduceMd5Signature(Create()); - public bool SupportsLargeExponent => true; + public override bool SupportsLargeExponent => true; - public bool SupportsSha2Oaep { get; } = true; + public override bool SupportsSha2Oaep { get; } = true; - public bool SupportsPss { get; } = true; + public override bool SupportsPss { get; } = true; - 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(); + public override bool SupportsSha3 { get; } = SHA3_256.IsSupported; // If SHA3_256 is supported, assume 384 and 512 are, too. } } diff --git a/src/libraries/System.Security.Cryptography/tests/RSATests.cs b/src/libraries/System.Security.Cryptography/tests/DefaultRSATests.cs similarity index 98% rename from src/libraries/System.Security.Cryptography/tests/RSATests.cs rename to src/libraries/System.Security.Cryptography/tests/DefaultRSATests.cs index 7b55391916c587..26ebc731679134 100644 --- a/src/libraries/System.Security.Cryptography/tests/RSATests.cs +++ b/src/libraries/System.Security.Cryptography/tests/DefaultRSATests.cs @@ -8,10 +8,10 @@ namespace System.Security.Cryptography.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public class RSATests + public static class DefaultRSATests { [Fact] - public void BaseVirtualsNotImplementedException() + public static void BaseVirtualsNotImplementedException() { var rsa = new EmptyRSA(); Assert.Throws(() => rsa.Decrypt(null, null)); @@ -21,7 +21,7 @@ public void BaseVirtualsNotImplementedException() } [Fact] - public void TryDecrypt_UsesDecrypt() + public static void TryDecrypt_UsesDecrypt() { var rsa = new DelegateRSA { DecryptDelegate = (data, padding) => data }; int bytesWritten; @@ -46,7 +46,7 @@ public void TryDecrypt_UsesDecrypt() } [Fact] - public void TryEncrypt_UsesEncrypt() + public static void TryEncrypt_UsesEncrypt() { var rsa = new DelegateRSA { EncryptDelegate = (data, padding) => data }; int bytesWritten; @@ -71,7 +71,7 @@ public void TryEncrypt_UsesEncrypt() } [Fact] - public void TryHashData_UsesHashData() + public static void TryHashData_UsesHashData() { var rsa = new DelegateRSA { HashDataArrayDelegate = (data, offset, count, name) => new Span(data, offset, count).ToArray() }; int bytesWritten; @@ -96,7 +96,7 @@ public void TryHashData_UsesHashData() } [Fact] - public void TrySignHash_UsesSignHash() + public static void TrySignHash_UsesSignHash() { var rsa = new DelegateRSA { SignHashDelegate = (data, name, padding) => data }; int bytesWritten; @@ -121,7 +121,7 @@ public void TrySignHash_UsesSignHash() } [Fact] - public void VerifyHashSpan_UsesVerifyHashArray() + public static void VerifyHashSpan_UsesVerifyHashArray() { bool invoked = false; var rsa = new DelegateRSA { VerifyHashDelegate = delegate { invoked = true; return true; } }; @@ -130,7 +130,7 @@ public void VerifyHashSpan_UsesVerifyHashArray() } [Fact] - public void SignDataArray_UsesHashDataAndSignHash() + public static void SignDataArray_UsesHashDataAndSignHash() { var rsa = new DelegateRSA(); @@ -155,7 +155,7 @@ public void SignDataArray_UsesHashDataAndSignHash() } [Fact] - public void SignDataStream_UsesHashDataAndSignHash() + public static void SignDataStream_UsesHashDataAndSignHash() { var rsa = new DelegateRSA(); @@ -172,7 +172,7 @@ public void SignDataStream_UsesHashDataAndSignHash() } [Fact] - public void VerifyDataStream_UsesHashDataAndVerifyHash() + public static void VerifyDataStream_UsesHashDataAndVerifyHash() { var rsa = new DelegateRSA(); @@ -191,7 +191,7 @@ public void VerifyDataStream_UsesHashDataAndVerifyHash() } [Fact] - public void RSAEncryptionPadding_Equality() + public static void RSAEncryptionPadding_Equality() { Assert.True(RSAEncryptionPadding.Pkcs1.Equals(RSAEncryptionPadding.Pkcs1)); Assert.True(RSAEncryptionPadding.Pkcs1.Equals((object)RSAEncryptionPadding.Pkcs1)); @@ -210,7 +210,7 @@ public void RSAEncryptionPadding_Equality() } [Fact] - public void RSASignaturePadding_Equality() + public static void RSASignaturePadding_Equality() { Assert.True(RSASignaturePadding.Pkcs1.Equals(RSASignaturePadding.Pkcs1)); Assert.True(RSASignaturePadding.Pkcs1.Equals((object)RSASignaturePadding.Pkcs1)); diff --git a/src/libraries/System.Security.Cryptography/tests/RSACngProvider.cs b/src/libraries/System.Security.Cryptography/tests/RSACngProvider.cs new file mode 100644 index 00000000000000..abcb107f4f58bd --- /dev/null +++ b/src/libraries/System.Security.Cryptography/tests/RSACngProvider.cs @@ -0,0 +1,33 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Runtime.InteropServices; +using Test.Cryptography; + +namespace System.Security.Cryptography.Rsa.Tests +{ + public sealed class RSACngProvider : RSAProvider + { + public static readonly RSACngProvider Instance = new RSACngProvider(); + + private RSACngProvider() { } + + public override RSA Create() => new RSACng(); + + public override RSA Create(int keySize) => new RSACng(keySize); + + public override bool Supports384PrivateKey => PlatformSupport.IsRSA384Supported; + + public override bool SupportsLargeExponent => true; + + public override bool SupportsSha2Oaep => true; + + public override bool SupportsPss => true; + + public override bool SupportsSha1Signatures => true; + + public override bool SupportsMd5Signatures => true; + + public override bool SupportsSha3 { get; } = SHA3_256.IsSupported; // If SHA3_256 is supported, assume 384 and 512 are, too. + } +} diff --git a/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderBackCompat.cs b/src/libraries/System.Security.Cryptography/tests/RSACryptoServiceProviderBackCompat.cs similarity index 97% rename from src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderBackCompat.cs rename to src/libraries/System.Security.Cryptography/tests/RSACryptoServiceProviderBackCompat.cs index 8ef0c9474d73d7..eceaef2a4206cf 100644 --- a/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderBackCompat.cs +++ b/src/libraries/System.Security.Cryptography/tests/RSACryptoServiceProviderBackCompat.cs @@ -156,14 +156,14 @@ public static void VerifyLegacySignVerifyHash(bool useLegacySign, bool useLegacy public static IEnumerable AlgorithmIdentifiers() { - if (RSAFactory.SupportsMd5Signatures) + if (RSACryptoServiceProviderProvider.Instance.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 (RSACryptoServiceProviderProvider.Instance.SupportsSha1Signatures) { yield return new object[] { "SHA1", SHA1.Create() }; yield return new object[] { "SHA1", typeof(SHA1) }; diff --git a/src/libraries/System.Security.Cryptography/tests/RSACryptoServiceProviderProvider.cs b/src/libraries/System.Security.Cryptography/tests/RSACryptoServiceProviderProvider.cs new file mode 100644 index 00000000000000..c5994bc87ffdf4 --- /dev/null +++ b/src/libraries/System.Security.Cryptography/tests/RSACryptoServiceProviderProvider.cs @@ -0,0 +1,36 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Runtime.InteropServices; +using System.Security.Cryptography.Tests; +using Test.Cryptography; + +namespace System.Security.Cryptography.Rsa.Tests +{ + public sealed class RSACryptoServiceProviderProvider : RSAProvider + { + public static readonly RSACryptoServiceProviderProvider Instance = new RSACryptoServiceProviderProvider(); + + private bool? _supportsSha1Signatures; + private bool? _supportsMd5Signatures; + + private RSACryptoServiceProviderProvider() { } + + public override RSA Create() => new RSACryptoServiceProvider(); + + public override RSA Create(int keySize) => new RSACryptoServiceProvider(keySize); + + public override bool Supports384PrivateKey => PlatformSupport.IsRSA384Supported; + + public override bool SupportsLargeExponent => false; + + public override bool SupportsSha2Oaep => false; + + public override bool SupportsPss => false; + + public override bool SupportsSha1Signatures => _supportsSha1Signatures ??= SignatureSupport.CanProduceSha1Signature(Create()); + public override bool SupportsMd5Signatures => _supportsMd5Signatures ??= SignatureSupport.CanProduceMd5Signature(Create()); + + public override bool SupportsSha3 => false; + } +} diff --git a/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderTests.cs b/src/libraries/System.Security.Cryptography/tests/RSACryptoServiceProviderTests.cs similarity index 97% rename from src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderTests.cs rename to src/libraries/System.Security.Cryptography/tests/RSACryptoServiceProviderTests.cs index b8ad2892374190..97c3b5640e92c0 100644 --- a/src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderTests.cs +++ b/src/libraries/System.Security.Cryptography/tests/RSACryptoServiceProviderTests.cs @@ -1,13 +1,14 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Security.Cryptography.Tests; using System.Security.Cryptography.Rsa.Tests; +using System.Security.Cryptography.Tests; +using Microsoft.DotNet.XUnitExtensions; using Xunit; namespace System.Security.Cryptography.Csp.Tests { - public class RSACryptoServiceProviderTests + public static class RSACryptoServiceProviderTests { const int PROV_RSA_FULL = 1; const int PROV_RSA_AES = 24; @@ -306,9 +307,11 @@ public static void ImportParameters_ExponentTooBig_Throws() } } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact] public static void SignHash_DefaultAlgorithm_Success() { + SkipTestException.ThrowUnless(RSACryptoServiceProviderProvider.Instance.SupportsSha1Signatures); + byte[] hashVal = SHA1.HashData(TestData.HelloBytes); using (var rsa = new RSACryptoServiceProvider()) @@ -318,9 +321,11 @@ public static void SignHash_DefaultAlgorithm_Success() } } - [ConditionalFact(typeof(RSAFactory), nameof(RSAFactory.SupportsSha1Signatures))] + [ConditionalFact] public static void VerifyHash_DefaultAlgorithm_Success() { + SkipTestException.ThrowUnless(RSACryptoServiceProviderProvider.Instance.SupportsSha1Signatures); + byte[] hashVal = SHA1.HashData(TestData.HelloBytes); using (var rsa = new RSACryptoServiceProvider()) diff --git a/src/libraries/System.Security.Cryptography.Csp/tests/RSAImportExportCspBlobTests.cs b/src/libraries/System.Security.Cryptography/tests/RSAImportExportCspBlobTests.cs similarity index 100% rename from src/libraries/System.Security.Cryptography.Csp/tests/RSAImportExportCspBlobTests.cs rename to src/libraries/System.Security.Cryptography/tests/RSAImportExportCspBlobTests.cs diff --git a/src/libraries/System.Security.Cryptography/tests/RSAKeyExchangeFormatterTests.cs b/src/libraries/System.Security.Cryptography/tests/RSAKeyExchangeFormatterTests.Default.cs similarity index 97% rename from src/libraries/System.Security.Cryptography/tests/RSAKeyExchangeFormatterTests.cs rename to src/libraries/System.Security.Cryptography/tests/RSAKeyExchangeFormatterTests.Default.cs index f0a0bca594b43a..f05a228216a9e3 100644 --- a/src/libraries/System.Security.Cryptography/tests/RSAKeyExchangeFormatterTests.cs +++ b/src/libraries/System.Security.Cryptography/tests/RSAKeyExchangeFormatterTests.Default.cs @@ -5,7 +5,7 @@ namespace System.Security.Cryptography.Tests { - public partial class RSAKeyExchangeFormatterTests + public static class RSAKeyExchangeFormatterTests_Default { [Fact] public static void RSAOAEPFormatterArguments() diff --git a/src/libraries/System.Security.Cryptography/tests/RSAOpenSslProvider.cs b/src/libraries/System.Security.Cryptography/tests/RSAOpenSslProvider.cs new file mode 100644 index 00000000000000..84a2d0d3a565e8 --- /dev/null +++ b/src/libraries/System.Security.Cryptography/tests/RSAOpenSslProvider.cs @@ -0,0 +1,34 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Security.Cryptography.Tests; + +namespace System.Security.Cryptography.Rsa.Tests +{ + public sealed class RSAOpenSslProvider : RSAProvider + { + public static readonly RSAOpenSslProvider Instance = new RSAOpenSslProvider(); + + private bool? _supportsSha1Signatures; + private bool? _supportsMd5Signatures; + + private RSAOpenSslProvider() { } + + public override RSA Create() => new RSAOpenSsl(); + + public override RSA Create(int keySize) => new RSAOpenSsl(keySize); + + public override bool Supports384PrivateKey => true; + + public override bool SupportsLargeExponent => true; + + public override bool SupportsSha2Oaep => true; + + public override bool SupportsPss => true; + + public override bool SupportsSha1Signatures => _supportsSha1Signatures ??= SignatureSupport.CanProduceSha1Signature(Create()); + public override bool SupportsMd5Signatures => _supportsMd5Signatures ??= SignatureSupport.CanProduceMd5Signature(Create()); + + public override bool SupportsSha3 => SHA3_256.IsSupported; // If SHA3_256 is supported, assume 384 and 512 are, too. + } +} diff --git a/src/libraries/System.Security.Cryptography/tests/RSASignatureFormatterTests.cs b/src/libraries/System.Security.Cryptography/tests/RSASignatureFormatterTests.NoKey.cs similarity index 59% rename from src/libraries/System.Security.Cryptography/tests/RSASignatureFormatterTests.cs rename to src/libraries/System.Security.Cryptography/tests/RSASignatureFormatterTests.NoKey.cs index a6f0d4909344e7..e2da9adb39d65f 100644 --- a/src/libraries/System.Security.Cryptography/tests/RSASignatureFormatterTests.cs +++ b/src/libraries/System.Security.Cryptography/tests/RSASignatureFormatterTests.NoKey.cs @@ -5,18 +5,18 @@ namespace System.Security.Cryptography.Tests { - public partial class RSASignatureFormatterTests : AsymmetricSignatureFormatterTests + public static class RSASignatureFormatterTests_NoKey { [Fact] public static void InvalidFormatterArguments_RSA() { - InvalidFormatterArguments(new RSAPKCS1SignatureFormatter()); + AsymmetricSignatureFormatterTests.InvalidFormatterArguments(new RSAPKCS1SignatureFormatter()); } [Fact] public static void InvalidDeformatterArguments_RSA() { - InvalidDeformatterArguments(new RSAPKCS1SignatureDeformatter()); + AsymmetricSignatureFormatterTests.InvalidDeformatterArguments(new RSAPKCS1SignatureDeformatter()); } } } diff --git a/src/libraries/System.Security.Cryptography/tests/RSATestRegistration.Cng.cs b/src/libraries/System.Security.Cryptography/tests/RSATestRegistration.Cng.cs new file mode 100644 index 00000000000000..ba907a9fe517a8 --- /dev/null +++ b/src/libraries/System.Security.Cryptography/tests/RSATestRegistration.Cng.cs @@ -0,0 +1,85 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Security.Cryptography.Rsa.Tests +{ + public sealed class SignVerify_Array_Cng : SignVerify_Array + { + protected override RSAProvider RSAFactory => RSACngProvider.Instance; + } + + public sealed class SignVerify_AllocatingSpan_Cng : SignVerify_AllocatingSpan + { + protected override RSAProvider RSAFactory => RSACngProvider.Instance; + } + + public sealed class SignVerify_Span_Cng : SignVerify_Span + { + protected override RSAProvider RSAFactory => RSACngProvider.Instance; + } + + public sealed class SignVerify_TrySpan_Cng : SignVerify_TrySpan + { + protected override RSAProvider RSAFactory => RSACngProvider.Instance; + } + + public sealed class RSASignatureFormatterTests_Cng : RSASignatureFormatterTests + { + protected override RSAProvider RSAFactory => RSACngProvider.Instance; + } + + public sealed class EncryptDecrypt_Array_Cng : EncryptDecrypt_Array + { + protected override RSAProvider RSAFactory => RSACngProvider.Instance; + } + + public sealed class EncryptDecrypt_Span_Cng : EncryptDecrypt_Span + { + protected override RSAProvider RSAFactory => RSACngProvider.Instance; + } + + public sealed class EncryptDecrypt_AllocatingSpan_Cng : EncryptDecrypt_AllocatingSpan + { + protected override RSAProvider RSAFactory => RSACngProvider.Instance; + } + + public sealed class EncryptDecrypt_TrySpan_Cng : EncryptDecrypt_TrySpan + { + protected override RSAProvider RSAFactory => RSACngProvider.Instance; + } + + public sealed class RSAFactoryTests_Cng : RSAFactoryTests + { + protected override RSAProvider RSAFactory => RSACngProvider.Instance; + } + + public sealed class ImportExport_Cng : ImportExport + { + protected override RSAProvider RSAFactory => RSACngProvider.Instance; + } + + public sealed class RSAXml_Cng : RSAXml + { + protected override RSAProvider RSAFactory => RSACngProvider.Instance; + } + + public sealed class RSAKeyFileTests_Cng : RSAKeyFileTests + { + protected override RSAProvider RSAFactory => RSACngProvider.Instance; + } + + public sealed class RSAKeyPemTests_Cng : RSAKeyPemTests + { + protected override RSAProvider RSAFactory => RSACngProvider.Instance; + } + + public sealed class RSAKeyExchangeFormatterTests_Cng : RSAKeyExchangeFormatterTests + { + protected override RSAProvider RSAFactory => RSACngProvider.Instance; + } + + public sealed class KeyGeneration_Cng : KeyGeneration + { + protected override RSAProvider RSAFactory => RSACngProvider.Instance; + } +} diff --git a/src/libraries/System.Security.Cryptography/tests/RSATestRegistration.Csp.cs b/src/libraries/System.Security.Cryptography/tests/RSATestRegistration.Csp.cs new file mode 100644 index 00000000000000..a12c3f09f5b214 --- /dev/null +++ b/src/libraries/System.Security.Cryptography/tests/RSATestRegistration.Csp.cs @@ -0,0 +1,85 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Security.Cryptography.Rsa.Tests +{ + public sealed class SignVerify_Array_Csp : SignVerify_Array + { + protected override RSAProvider RSAFactory => RSACryptoServiceProviderProvider.Instance; + } + + public sealed class SignVerify_AllocatingSpan_Csp : SignVerify_AllocatingSpan + { + protected override RSAProvider RSAFactory => RSACryptoServiceProviderProvider.Instance; + } + + public sealed class SignVerify_Span_Csp : SignVerify_Span + { + protected override RSAProvider RSAFactory => RSACryptoServiceProviderProvider.Instance; + } + + public sealed class SignVerify_TrySpan_Csp : SignVerify_TrySpan + { + protected override RSAProvider RSAFactory => RSACryptoServiceProviderProvider.Instance; + } + + public sealed class RSASignatureFormatterTests_Csp : RSASignatureFormatterTests + { + protected override RSAProvider RSAFactory => RSACryptoServiceProviderProvider.Instance; + } + + public sealed class EncryptDecrypt_Array_Csp : EncryptDecrypt_Array + { + protected override RSAProvider RSAFactory => RSACryptoServiceProviderProvider.Instance; + } + + public sealed class EncryptDecrypt_Span_Csp : EncryptDecrypt_Span + { + protected override RSAProvider RSAFactory => RSACryptoServiceProviderProvider.Instance; + } + + public sealed class EncryptDecrypt_AllocatingSpan_Csp : EncryptDecrypt_AllocatingSpan + { + protected override RSAProvider RSAFactory => RSACryptoServiceProviderProvider.Instance; + } + + public sealed class EncryptDecrypt_TrySpan_Csp : EncryptDecrypt_TrySpan + { + protected override RSAProvider RSAFactory => RSACryptoServiceProviderProvider.Instance; + } + + public sealed class RSAFactoryTests_Csp : RSAFactoryTests + { + protected override RSAProvider RSAFactory => RSACryptoServiceProviderProvider.Instance; + } + + public sealed class ImportExport_Csp : ImportExport + { + protected override RSAProvider RSAFactory => RSACryptoServiceProviderProvider.Instance; + } + + public sealed class RSAXml_Csp : RSAXml + { + protected override RSAProvider RSAFactory => RSACryptoServiceProviderProvider.Instance; + } + + public sealed class RSAKeyFileTests_Csp : RSAKeyFileTests + { + protected override RSAProvider RSAFactory => RSACryptoServiceProviderProvider.Instance; + } + + public sealed class RSAKeyPemTests_Csp : RSAKeyPemTests + { + protected override RSAProvider RSAFactory => RSACryptoServiceProviderProvider.Instance; + } + + public sealed class RSAKeyExchangeFormatterTests_Csp : RSAKeyExchangeFormatterTests + { + protected override RSAProvider RSAFactory => RSACryptoServiceProviderProvider.Instance; + } + + public sealed class KeyGeneration_Csp : KeyGeneration + { + protected override RSAProvider RSAFactory => RSACryptoServiceProviderProvider.Instance; + } +} diff --git a/src/libraries/System.Security.Cryptography/tests/RSATestRegistration.Default.cs b/src/libraries/System.Security.Cryptography/tests/RSATestRegistration.Default.cs new file mode 100644 index 00000000000000..30b080854fdbf7 --- /dev/null +++ b/src/libraries/System.Security.Cryptography/tests/RSATestRegistration.Default.cs @@ -0,0 +1,85 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Security.Cryptography.Rsa.Tests +{ + public sealed class SignVerify_Array_Default : SignVerify_Array + { + protected override RSAProvider RSAFactory => DefaultRSAProvider.Instance; + } + + public sealed class SignVerify_AllocatingSpan_Default : SignVerify_AllocatingSpan + { + protected override RSAProvider RSAFactory => DefaultRSAProvider.Instance; + } + + public sealed class SignVerify_Span_Default : SignVerify_Span + { + protected override RSAProvider RSAFactory => DefaultRSAProvider.Instance; + } + + public sealed class SignVerify_TrySpan_Default : SignVerify_TrySpan + { + protected override RSAProvider RSAFactory => DefaultRSAProvider.Instance; + } + + public sealed class RSASignatureFormatterTests_Default : RSASignatureFormatterTests + { + protected override RSAProvider RSAFactory => DefaultRSAProvider.Instance; + } + + public sealed class EncryptDecrypt_Array_Default : EncryptDecrypt_Array + { + protected override RSAProvider RSAFactory => DefaultRSAProvider.Instance; + } + + public sealed class EncryptDecrypt_Span_Default : EncryptDecrypt_Span + { + protected override RSAProvider RSAFactory => DefaultRSAProvider.Instance; + } + + public sealed class EncryptDecrypt_AllocatingSpan_Default : EncryptDecrypt_AllocatingSpan + { + protected override RSAProvider RSAFactory => DefaultRSAProvider.Instance; + } + + public sealed class EncryptDecrypt_TrySpan_Default : EncryptDecrypt_TrySpan + { + protected override RSAProvider RSAFactory => DefaultRSAProvider.Instance; + } + + public sealed class RSAFactoryTests_Default : RSAFactoryTests + { + protected override RSAProvider RSAFactory => DefaultRSAProvider.Instance; + } + + public sealed class ImportExport_Default : ImportExport + { + protected override RSAProvider RSAFactory => DefaultRSAProvider.Instance; + } + + public sealed class RSAXml_Default : RSAXml + { + protected override RSAProvider RSAFactory => DefaultRSAProvider.Instance; + } + + public sealed class RSAKeyFileTests_Default : RSAKeyFileTests + { + protected override RSAProvider RSAFactory => DefaultRSAProvider.Instance; + } + + public sealed class RSAKeyPemTests_Default : RSAKeyPemTests + { + protected override RSAProvider RSAFactory => DefaultRSAProvider.Instance; + } + + public sealed class RSAKeyExchangeFormatterTests_Default : RSAKeyExchangeFormatterTests + { + protected override RSAProvider RSAFactory => DefaultRSAProvider.Instance; + } + + public sealed class KeyGeneration_Default : KeyGeneration + { + protected override RSAProvider RSAFactory => DefaultRSAProvider.Instance; + } +} diff --git a/src/libraries/System.Security.Cryptography/tests/RSATestRegistration.OpenSsl.cs b/src/libraries/System.Security.Cryptography/tests/RSATestRegistration.OpenSsl.cs new file mode 100644 index 00000000000000..b612f22e875cf7 --- /dev/null +++ b/src/libraries/System.Security.Cryptography/tests/RSATestRegistration.OpenSsl.cs @@ -0,0 +1,85 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Security.Cryptography.Rsa.Tests +{ + public sealed class SignVerify_Array_OpenSsl : SignVerify_Array + { + protected override RSAProvider RSAFactory => RSAOpenSslProvider.Instance; + } + + public sealed class SignVerify_AllocatingSpan_OpenSsl : SignVerify_AllocatingSpan + { + protected override RSAProvider RSAFactory => RSAOpenSslProvider.Instance; + } + + public sealed class SignVerify_Span_OpenSsl : SignVerify_Span + { + protected override RSAProvider RSAFactory => RSAOpenSslProvider.Instance; + } + + public sealed class SignVerify_TrySpan_OpenSsl : SignVerify_TrySpan + { + protected override RSAProvider RSAFactory => RSAOpenSslProvider.Instance; + } + + public sealed class RSASignatureFormatterTests_OpenSsl : RSASignatureFormatterTests + { + protected override RSAProvider RSAFactory => RSAOpenSslProvider.Instance; + } + + public sealed class EncryptDecrypt_Array_OpenSsl : EncryptDecrypt_Array + { + protected override RSAProvider RSAFactory => RSAOpenSslProvider.Instance; + } + + public sealed class EncryptDecrypt_Span_OpenSsl : EncryptDecrypt_Span + { + protected override RSAProvider RSAFactory => RSAOpenSslProvider.Instance; + } + + public sealed class EncryptDecrypt_AllocatingSpan_OpenSsl : EncryptDecrypt_AllocatingSpan + { + protected override RSAProvider RSAFactory => RSAOpenSslProvider.Instance; + } + + public sealed class EncryptDecrypt_TrySpan_OpenSsl : EncryptDecrypt_TrySpan + { + protected override RSAProvider RSAFactory => RSAOpenSslProvider.Instance; + } + + public sealed class RSAFactoryTests_OpenSsl : RSAFactoryTests + { + protected override RSAProvider RSAFactory => RSAOpenSslProvider.Instance; + } + + public sealed class ImportExport_OpenSsl : ImportExport + { + protected override RSAProvider RSAFactory => RSAOpenSslProvider.Instance; + } + + public sealed class RSAXml_OpenSsl : RSAXml + { + protected override RSAProvider RSAFactory => RSAOpenSslProvider.Instance; + } + + public sealed class RSAKeyFileTests_OpenSsl : RSAKeyFileTests + { + protected override RSAProvider RSAFactory => RSAOpenSslProvider.Instance; + } + + public sealed class RSAKeyPemTests_OpenSsl : RSAKeyPemTests + { + protected override RSAProvider RSAFactory => RSAOpenSslProvider.Instance; + } + + public sealed class RSAKeyExchangeFormatterTests_OpenSsl : RSAKeyExchangeFormatterTests + { + protected override RSAProvider RSAFactory => RSAOpenSslProvider.Instance; + } + + public sealed class KeyGeneration_OpenSsl : KeyGeneration + { + protected override RSAProvider RSAFactory => RSAOpenSslProvider.Instance; + } +} diff --git a/src/libraries/System.Security.Cryptography.Cng/tests/RsaCngTests.cs b/src/libraries/System.Security.Cryptography/tests/RsaCngTests.cs similarity index 97% rename from src/libraries/System.Security.Cryptography.Cng/tests/RsaCngTests.cs rename to src/libraries/System.Security.Cryptography/tests/RsaCngTests.cs index 1d6310797ab28b..1cbbc1e21374ce 100644 --- a/src/libraries/System.Security.Cryptography.Cng/tests/RsaCngTests.cs +++ b/src/libraries/System.Security.Cryptography/tests/RsaCngTests.cs @@ -7,6 +7,7 @@ using System.Text; using System.Collections.Generic; using System.Security.Cryptography; +using System.Security.Cryptography.Rsa.Tests; using Test.Cryptography; using Xunit; @@ -93,14 +94,14 @@ public static void RSACng_Ctor_UnusualKeysize_384() { RSACng_Ctor_UnusualKeysize(ExpectedKeySize, keyBlob, expected); - Assert.True(Rsa.Tests.RSAFactory.Supports384PrivateKey, "RSAFactory.Supports384PrivateKey"); + Assert.True(RSACngProvider.Instance.Supports384PrivateKey, "RSAFactory.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 (RSACngProvider.Instance.Supports384PrivateKey) { throw; } diff --git a/src/libraries/System.Security.Cryptography.OpenSsl/tests/RsaOpenSslTests.cs b/src/libraries/System.Security.Cryptography/tests/RsaOpenSslTests.cs similarity index 100% rename from src/libraries/System.Security.Cryptography.OpenSsl/tests/RsaOpenSslTests.cs rename to src/libraries/System.Security.Cryptography/tests/RsaOpenSslTests.cs diff --git a/src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj b/src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj index c69819fbeaf6d1..b8262a828050f9 100644 --- a/src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj +++ b/src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj @@ -422,18 +422,18 @@ Link="CommonTest\System\Security\Cryptography\AlgorithmImplementations\RSA\ImportExport.cs" /> - + - + - + + @@ -593,10 +594,10 @@ - - - + + + @@ -713,6 +714,14 @@ + + + + + + + + + + + + + + + + From 34782799fc98310c799e09b9542b143c48a64f1f Mon Sep 17 00:00:00 2001 From: Pranav Senthilnathan Date: Tue, 30 Jun 2026 15:55:57 -0700 Subject: [PATCH 02/10] fix indentation Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- .../tests/RSATestRegistration.OpenSsl.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Security.Cryptography/tests/RSATestRegistration.OpenSsl.cs b/src/libraries/System.Security.Cryptography/tests/RSATestRegistration.OpenSsl.cs index b612f22e875cf7..7a24fc76ad28ae 100644 --- a/src/libraries/System.Security.Cryptography/tests/RSATestRegistration.OpenSsl.cs +++ b/src/libraries/System.Security.Cryptography/tests/RSATestRegistration.OpenSsl.cs @@ -28,7 +28,7 @@ public sealed class RSASignatureFormatterTests_OpenSsl : RSASignatureFormatterTe protected override RSAProvider RSAFactory => RSAOpenSslProvider.Instance; } - public sealed class EncryptDecrypt_Array_OpenSsl : EncryptDecrypt_Array + public sealed class EncryptDecrypt_Array_OpenSsl : EncryptDecrypt_Array { protected override RSAProvider RSAFactory => RSAOpenSslProvider.Instance; } From 456a12fbc22a906be00301dc664c4341bc94f530 Mon Sep 17 00:00:00 2001 From: Pranav Senthilnathan Date: Tue, 30 Jun 2026 23:55:04 +0000 Subject: [PATCH 03/10] Fix assert message --- src/libraries/System.Security.Cryptography/tests/RsaCngTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Security.Cryptography/tests/RsaCngTests.cs b/src/libraries/System.Security.Cryptography/tests/RsaCngTests.cs index 1cbbc1e21374ce..1b3d4f244e047e 100644 --- a/src/libraries/System.Security.Cryptography/tests/RsaCngTests.cs +++ b/src/libraries/System.Security.Cryptography/tests/RsaCngTests.cs @@ -94,7 +94,7 @@ public static void RSACng_Ctor_UnusualKeysize_384() { RSACng_Ctor_UnusualKeysize(ExpectedKeySize, keyBlob, expected); - Assert.True(RSACngProvider.Instance.Supports384PrivateKey, "RSAFactory.Supports384PrivateKey"); + AssertExtensions.TrueExpression(RSACngProvider.Instance.Supports384PrivateKey); } catch (CryptographicException) { From 063d82038e8597b14f16b3f3f3de31444c5f450f Mon Sep 17 00:00:00 2001 From: Pranav Senthilnathan Date: Wed, 1 Jul 2026 00:48:40 +0000 Subject: [PATCH 04/10] remove unused usings --- .../System.Security.Cryptography/tests/RSACngProvider.cs | 1 - .../tests/RSACryptoServiceProviderProvider.cs | 1 - 2 files changed, 2 deletions(-) diff --git a/src/libraries/System.Security.Cryptography/tests/RSACngProvider.cs b/src/libraries/System.Security.Cryptography/tests/RSACngProvider.cs index abcb107f4f58bd..50ba09b00d042d 100644 --- a/src/libraries/System.Security.Cryptography/tests/RSACngProvider.cs +++ b/src/libraries/System.Security.Cryptography/tests/RSACngProvider.cs @@ -1,7 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Runtime.InteropServices; using Test.Cryptography; namespace System.Security.Cryptography.Rsa.Tests diff --git a/src/libraries/System.Security.Cryptography/tests/RSACryptoServiceProviderProvider.cs b/src/libraries/System.Security.Cryptography/tests/RSACryptoServiceProviderProvider.cs index c5994bc87ffdf4..d766169d0b64d9 100644 --- a/src/libraries/System.Security.Cryptography/tests/RSACryptoServiceProviderProvider.cs +++ b/src/libraries/System.Security.Cryptography/tests/RSACryptoServiceProviderProvider.cs @@ -1,7 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Runtime.InteropServices; using System.Security.Cryptography.Tests; using Test.Cryptography; From 5881ce986bcaa13f6aa9935f18b251f0dd6794d8 Mon Sep 17 00:00:00 2001 From: Pranav Senthilnathan Date: Thu, 2 Jul 2026 23:42:00 +0000 Subject: [PATCH 05/10] Rename RSATests back --- .../tests/{DefaultRSATests.cs => RSATests.cs} | 2 +- .../tests/System.Security.Cryptography.Tests.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/libraries/System.Security.Cryptography/tests/{DefaultRSATests.cs => RSATests.cs} (99%) diff --git a/src/libraries/System.Security.Cryptography/tests/DefaultRSATests.cs b/src/libraries/System.Security.Cryptography/tests/RSATests.cs similarity index 99% rename from src/libraries/System.Security.Cryptography/tests/DefaultRSATests.cs rename to src/libraries/System.Security.Cryptography/tests/RSATests.cs index 26ebc731679134..404a94a77da9ee 100644 --- a/src/libraries/System.Security.Cryptography/tests/DefaultRSATests.cs +++ b/src/libraries/System.Security.Cryptography/tests/RSATests.cs @@ -8,7 +8,7 @@ namespace System.Security.Cryptography.Tests { [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] - public static class DefaultRSATests + public static class RSATests { [Fact] public static void BaseVirtualsNotImplementedException() diff --git a/src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj b/src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj index b8262a828050f9..caaad14498b290 100644 --- a/src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj +++ b/src/libraries/System.Security.Cryptography/tests/System.Security.Cryptography.Tests.csproj @@ -541,7 +541,6 @@ - @@ -598,6 +597,7 @@ + From 3c8bc0ebe813919e3152603c7bf22f45011b489f Mon Sep 17 00:00:00 2001 From: Pranav Senthilnathan Date: Tue, 7 Jul 2026 03:38:54 +0000 Subject: [PATCH 06/10] prevent tearing during parallel initialization --- .../RSA/RSAProvider.cs | 19 +++++++++++++++++-- .../tests/DefaultRSAProvider.cs | 8 ++++---- .../tests/RSACryptoServiceProviderProvider.cs | 8 ++++---- .../tests/RSAOpenSslProvider.cs | 8 ++++---- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAProvider.cs b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAProvider.cs index a167fde7e27032..45fa1c01abe332 100644 --- a/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAProvider.cs +++ b/src/libraries/Common/tests/System/Security/Cryptography/AlgorithmImplementations/RSA/RSAProvider.cs @@ -5,7 +5,7 @@ namespace System.Security.Cryptography.Rsa.Tests { public abstract class RSAProvider { - private bool? _supports16384; + private LazyBool _supports16384; public abstract RSA Create(); public abstract RSA Create(int keySize); @@ -19,7 +19,7 @@ public abstract class RSAProvider public bool NoSupportsSha3 => !SupportsSha3; - public bool Supports16384 => _supports16384 ??= TestRsa16384(); + public bool Supports16384 => _supports16384.GetOrInit(TestRsa16384); public RSA Create(RSAParameters rsaParameters) { @@ -52,5 +52,20 @@ private bool TestRsa16384() return false; } } + + protected struct LazyBool + { + private byte _value; + + internal bool GetOrInit(Func valueFactory) + { + if (_value == 0) + { + _value = (byte)(valueFactory() ? 1 : 2); + } + + return _value == 1; + } + } } } diff --git a/src/libraries/System.Security.Cryptography/tests/DefaultRSAProvider.cs b/src/libraries/System.Security.Cryptography/tests/DefaultRSAProvider.cs index b2c736c76768cf..3729bdf9c1f0bc 100644 --- a/src/libraries/System.Security.Cryptography/tests/DefaultRSAProvider.cs +++ b/src/libraries/System.Security.Cryptography/tests/DefaultRSAProvider.cs @@ -11,8 +11,8 @@ public sealed class DefaultRSAProvider : RSAProvider { public static readonly DefaultRSAProvider Instance = new DefaultRSAProvider(); - private bool? _supportsSha1Signatures; - private bool? _supportsMd5Signatures; + private LazyBool _supportsSha1Signatures; + private LazyBool _supportsMd5Signatures; private DefaultRSAProvider() { } @@ -31,8 +31,8 @@ public override RSA Create(int keySize) } public override bool Supports384PrivateKey => PlatformSupport.IsRSA384Supported; - public override bool SupportsSha1Signatures => _supportsSha1Signatures ??= SignatureSupport.CanProduceSha1Signature(Create()); - public override bool SupportsMd5Signatures => _supportsMd5Signatures ??= SignatureSupport.CanProduceMd5Signature(Create()); + public override bool SupportsSha1Signatures => _supportsSha1Signatures.GetOrInit(() => SignatureSupport.CanProduceSha1Signature(Create())); + public override bool SupportsMd5Signatures => _supportsMd5Signatures.GetOrInit(() => SignatureSupport.CanProduceMd5Signature(Create())); public override bool SupportsLargeExponent => true; diff --git a/src/libraries/System.Security.Cryptography/tests/RSACryptoServiceProviderProvider.cs b/src/libraries/System.Security.Cryptography/tests/RSACryptoServiceProviderProvider.cs index d766169d0b64d9..be759f11de9da2 100644 --- a/src/libraries/System.Security.Cryptography/tests/RSACryptoServiceProviderProvider.cs +++ b/src/libraries/System.Security.Cryptography/tests/RSACryptoServiceProviderProvider.cs @@ -10,8 +10,8 @@ public sealed class RSACryptoServiceProviderProvider : RSAProvider { public static readonly RSACryptoServiceProviderProvider Instance = new RSACryptoServiceProviderProvider(); - private bool? _supportsSha1Signatures; - private bool? _supportsMd5Signatures; + private LazyBool _supportsSha1Signatures; + private LazyBool _supportsMd5Signatures; private RSACryptoServiceProviderProvider() { } @@ -27,8 +27,8 @@ private RSACryptoServiceProviderProvider() { } public override bool SupportsPss => false; - public override bool SupportsSha1Signatures => _supportsSha1Signatures ??= SignatureSupport.CanProduceSha1Signature(Create()); - public override bool SupportsMd5Signatures => _supportsMd5Signatures ??= SignatureSupport.CanProduceMd5Signature(Create()); + public override bool SupportsSha1Signatures => _supportsSha1Signatures.GetOrInit(() => SignatureSupport.CanProduceSha1Signature(Create())); + public override bool SupportsMd5Signatures => _supportsMd5Signatures.GetOrInit(() => SignatureSupport.CanProduceMd5Signature(Create())); public override bool SupportsSha3 => false; } diff --git a/src/libraries/System.Security.Cryptography/tests/RSAOpenSslProvider.cs b/src/libraries/System.Security.Cryptography/tests/RSAOpenSslProvider.cs index 84a2d0d3a565e8..ceda6072b57f68 100644 --- a/src/libraries/System.Security.Cryptography/tests/RSAOpenSslProvider.cs +++ b/src/libraries/System.Security.Cryptography/tests/RSAOpenSslProvider.cs @@ -9,8 +9,8 @@ public sealed class RSAOpenSslProvider : RSAProvider { public static readonly RSAOpenSslProvider Instance = new RSAOpenSslProvider(); - private bool? _supportsSha1Signatures; - private bool? _supportsMd5Signatures; + private LazyBool _supportsSha1Signatures; + private LazyBool _supportsMd5Signatures; private RSAOpenSslProvider() { } @@ -26,8 +26,8 @@ private RSAOpenSslProvider() { } public override bool SupportsPss => true; - public override bool SupportsSha1Signatures => _supportsSha1Signatures ??= SignatureSupport.CanProduceSha1Signature(Create()); - public override bool SupportsMd5Signatures => _supportsMd5Signatures ??= SignatureSupport.CanProduceMd5Signature(Create()); + public override bool SupportsSha1Signatures => _supportsSha1Signatures.GetOrInit(() => SignatureSupport.CanProduceSha1Signature(Create())); + public override bool SupportsMd5Signatures => _supportsMd5Signatures.GetOrInit(() => SignatureSupport.CanProduceMd5Signature(Create())); public override bool SupportsSha3 => SHA3_256.IsSupported; // If SHA3_256 is supported, assume 384 and 512 are, too. } From af3c09a0295ee27dd0b5871f370abd0a6a44093e Mon Sep 17 00:00:00 2001 From: Pranav Senthilnathan Date: Thu, 9 Jul 2026 18:27:25 -0700 Subject: [PATCH 07/10] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- .../Cryptography/AlgorithmImplementations/RSA/SignVerify.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 1291b1fe5341e0..00bd7cf87ba497 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 @@ -1653,7 +1653,7 @@ public static IEnumerable HashAlgorithmNames yield return new object[] { HashAlgorithmName.MD5.Name, (RSAProvider p) => p.SupportsMd5Signatures }; yield return new object[] { HashAlgorithmName.SHA1.Name, (RSAProvider p) => p.SupportsSha1Signatures }; - + yield return new object[] { HashAlgorithmName.SHA3_256.Name, (RSAProvider p) => p.SupportsSha3 }; yield return new object[] { HashAlgorithmName.SHA3_384.Name, (RSAProvider p) => p.SupportsSha3 }; yield return new object[] { HashAlgorithmName.SHA3_512.Name, (RSAProvider p) => p.SupportsSha3 }; From 455faa5bd218da56df5bbd0cf1161cbf75fbc850 Mon Sep 17 00:00:00 2001 From: Pranav Senthilnathan Date: Thu, 9 Jul 2026 18:29:14 -0700 Subject: [PATCH 08/10] Apply suggestion from @PranavSenthilnathan --- .../Cryptography/AlgorithmImplementations/RSA/EncryptDecrypt.cs | 1 - 1 file changed, 1 deletion(-) 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 5625cddc460087..93cfbd8f827cc3 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 @@ -861,7 +861,6 @@ public static IEnumerable OaepPaddingModes yield return new object[] { RSAEncryptionPadding.OaepSHA256, true, false }; yield return new object[] { RSAEncryptionPadding.OaepSHA384, true, false }; yield return new object[] { RSAEncryptionPadding.OaepSHA512, true, false }; - yield return new object[] { RSAEncryptionPadding.OaepSHA3_256, false, true }; yield return new object[] { RSAEncryptionPadding.OaepSHA3_384, false, true }; yield return new object[] { RSAEncryptionPadding.OaepSHA3_512, false, true }; From c7e1299a99cc07238519253cc3c8d21788b84224 Mon Sep 17 00:00:00 2001 From: Pranav Senthilnathan Date: Thu, 9 Jul 2026 18:31:23 -0700 Subject: [PATCH 09/10] fix whitespace --- .../Cryptography/AlgorithmImplementations/RSA/EncryptDecrypt.cs | 1 + 1 file changed, 1 insertion(+) 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 93cfbd8f827cc3..a56b6150579a37 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 @@ -861,6 +861,7 @@ public static IEnumerable OaepPaddingModes yield return new object[] { RSAEncryptionPadding.OaepSHA256, true, false }; yield return new object[] { RSAEncryptionPadding.OaepSHA384, true, false }; yield return new object[] { RSAEncryptionPadding.OaepSHA512, true, false }; + yield return new object[] { RSAEncryptionPadding.OaepSHA3_256, false, true }; yield return new object[] { RSAEncryptionPadding.OaepSHA3_384, false, true }; yield return new object[] { RSAEncryptionPadding.OaepSHA3_512, false, true }; From 778f57f0b5d491a9d6d5b65aac8656a2a42f0e51 Mon Sep 17 00:00:00 2001 From: Pranav Senthilnathan Date: Mon, 13 Jul 2026 21:19:31 -0400 Subject: [PATCH 10/10] Address copilot feedback --- .../tests/AsymmetricSignatureFormatterTests.cs | 4 ++-- .../tests/RSASignatureFormatterTests.NoKey.cs | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Security.Cryptography/tests/AsymmetricSignatureFormatterTests.cs b/src/libraries/System.Security.Cryptography/tests/AsymmetricSignatureFormatterTests.cs index 654fd047c4782d..3bb920c80c77cf 100644 --- a/src/libraries/System.Security.Cryptography/tests/AsymmetricSignatureFormatterTests.cs +++ b/src/libraries/System.Security.Cryptography/tests/AsymmetricSignatureFormatterTests.cs @@ -11,14 +11,14 @@ namespace System.Security.Cryptography.Tests [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] public partial class AsymmetricSignatureFormatterTests { - protected internal static void InvalidFormatterArguments(AsymmetricSignatureFormatter formatter) + internal static void InvalidFormatterArguments(AsymmetricSignatureFormatter formatter) { Assert.Throws(() => formatter.SetKey(null)); Assert.Throws(() => formatter.CreateSignature((byte[])null)); Assert.Throws(() => formatter.CreateSignature(new byte[] { 0, 1, 2, 3 })); } - protected internal static void InvalidDeformatterArguments(AsymmetricSignatureDeformatter deformatter) + internal static void InvalidDeformatterArguments(AsymmetricSignatureDeformatter deformatter) { Assert.Throws(() => deformatter.SetKey(null)); Assert.Throws(() => deformatter.VerifySignature((byte[])null, new byte[] { 0, 1, 2 })); diff --git a/src/libraries/System.Security.Cryptography/tests/RSASignatureFormatterTests.NoKey.cs b/src/libraries/System.Security.Cryptography/tests/RSASignatureFormatterTests.NoKey.cs index e2da9adb39d65f..daeaa9a22728d3 100644 --- a/src/libraries/System.Security.Cryptography/tests/RSASignatureFormatterTests.NoKey.cs +++ b/src/libraries/System.Security.Cryptography/tests/RSASignatureFormatterTests.NoKey.cs @@ -1,10 +1,12 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using Microsoft.DotNet.XUnitExtensions; using Xunit; namespace System.Security.Cryptography.Tests { + [SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")] public static class RSASignatureFormatterTests_NoKey { [Fact]