diff --git a/src/libraries/System.Security.Cryptography/src/System.Security.Cryptography.csproj b/src/libraries/System.Security.Cryptography/src/System.Security.Cryptography.csproj index f0bd081b40ce66..15d061748edd74 100644 --- a/src/libraries/System.Security.Cryptography/src/System.Security.Cryptography.csproj +++ b/src/libraries/System.Security.Cryptography/src/System.Security.Cryptography.csproj @@ -489,11 +489,9 @@ - + - - @@ -1943,7 +1941,7 @@ - + diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AsymmetricAlgorithm.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AsymmetricAlgorithm.cs index 28e21bfef5c7dc..b5dfa0666927ce 100644 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AsymmetricAlgorithm.cs +++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/AsymmetricAlgorithm.cs @@ -18,9 +18,9 @@ public static AsymmetricAlgorithm Create() => throw new PlatformNotSupportedException(SR.Cryptography_DefaultAlgorithm_NotSupported); [Obsolete(Obsoletions.CryptoStringFactoryMessage, DiagnosticId = Obsoletions.CryptoStringFactoryDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] - [RequiresUnreferencedCode(CryptoConfigForwarder.CreateFromNameUnreferencedCodeMessage)] + [RequiresUnreferencedCode(CryptoConfig.CreateFromNameUnreferencedCodeMessage)] public static AsymmetricAlgorithm? Create(string algName) => - CryptoConfigForwarder.CreateFromName(algName); + CryptoConfig.CreateFromName(algName); public virtual int KeySize { diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CryptoConfig.Common.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CryptoConfig.Common.cs deleted file mode 100644 index 950fd5282b4323..00000000000000 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CryptoConfig.Common.cs +++ /dev/null @@ -1,13 +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 -{ - public partial class CryptoConfig - { - internal const string CreateFromNameUnreferencedCodeMessage = "The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead."; - - // .NET Core does not support AllowOnlyFipsAlgorithms - public static bool AllowOnlyFipsAlgorithms => false; - } -} diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CryptoConfig.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CryptoConfig.cs index c5a45f8ed76f88..1cbf5c0a861262 100644 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CryptoConfig.cs +++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CryptoConfig.cs @@ -11,8 +11,13 @@ namespace System.Security.Cryptography { - public partial class CryptoConfig + public class CryptoConfig { + internal const string CreateFromNameUnreferencedCodeMessage = "The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead."; + + // .NET does not support AllowOnlyFipsAlgorithms + public static bool AllowOnlyFipsAlgorithms => false; + #if !BROWSER private const string AssemblyName_Pkcs = "System.Security.Cryptography.Pkcs"; @@ -493,6 +498,21 @@ public static void AddAlgorithm(Type algorithm, params string[] names) return CreateFromName(name, null); } + [RequiresUnreferencedCode(CreateFromNameUnreferencedCodeMessage)] + internal static T? CreateFromName(string name) where T : class + { + object? o = CreateFromName(name); + try + { + return (T?)o; + } + catch + { + (o as IDisposable)?.Dispose(); + throw; + } + } + [UnsupportedOSPlatform("browser")] public static void AddOID(string oid, params string[] names) { diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CryptoConfigForwarder.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CryptoConfigForwarder.cs deleted file mode 100644 index e734e4f65b9f00..00000000000000 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/CryptoConfigForwarder.cs +++ /dev/null @@ -1,61 +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.Diagnostics.CodeAnalysis; -using System.Reflection; - -[assembly: UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", - Target = "M:System.Security.Cryptography.CryptoConfigForwarder.#cctor", - Scope = "member", - Justification = "The cctor caches the RequiresUnreferencedCode call in a delegate, and usage of that delegate is marked with RequiresUnreferencedCode.")] - -namespace System.Security.Cryptography -{ - internal static class CryptoConfigForwarder - { - internal const string CreateFromNameUnreferencedCodeMessage = "The default algorithm implementations might be removed, use strong type references like 'RSA.Create()' instead."; - - // Suppressed for the ILLink by the assembly-level UnconditionalSuppressMessageAttribute - // https://github.com/dotnet/linker/issues/2648 -#pragma warning disable IL2026 - private static readonly Func s_createFromName = BindCreateFromName(); -#pragma warning restore IL2026 - - [RequiresUnreferencedCode(CreateFromNameUnreferencedCodeMessage)] - private static Func BindCreateFromName() - { - const string CryptoConfigTypeName = - "System.Security.Cryptography.CryptoConfig, System.Security.Cryptography.Algorithms"; - - const string CreateFromNameMethodName = "CreateFromName"; - - Type t = Type.GetType(CryptoConfigTypeName, throwOnError: true)!; - MethodInfo? createFromName = t.GetMethod(CreateFromNameMethodName, new[] { typeof(string) }); - - if (createFromName == null) - { - throw new MissingMethodException(t.FullName, CreateFromNameMethodName); - } - - return createFromName.CreateDelegate>(); - } - - [RequiresUnreferencedCode(CreateFromNameUnreferencedCodeMessage)] - internal static T? CreateFromName(string name) where T : class - { - object? o = s_createFromName(name); - try - { - return (T?)o; - } - catch - { - (o as IDisposable)?.Dispose(); - throw; - } - } - - internal static HashAlgorithm CreateDefaultHashAlgorithm() => - throw new PlatformNotSupportedException(SR.Cryptography_DefaultAlgorithm_NotSupported); - } -} diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/HMAC.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/HMAC.cs index d542ba3a5cf32b..796d07d6f315b3 100644 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/HMAC.cs +++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/HMAC.cs @@ -23,9 +23,9 @@ protected HMAC() { } throw new PlatformNotSupportedException(SR.Cryptography_DefaultAlgorithm_NotSupported); [Obsolete(Obsoletions.CryptoStringFactoryMessage, DiagnosticId = Obsoletions.CryptoStringFactoryDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] - [RequiresUnreferencedCode(CryptoConfigForwarder.CreateFromNameUnreferencedCodeMessage)] + [RequiresUnreferencedCode(CryptoConfig.CreateFromNameUnreferencedCodeMessage)] public static new HMAC? Create(string algorithmName) => - CryptoConfigForwarder.CreateFromName(algorithmName); + CryptoConfig.CreateFromName(algorithmName); public string HashName { diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/HashAlgorithm.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/HashAlgorithm.cs index 1f570eeb22fc5f..a27048d66b3e2f 100644 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/HashAlgorithm.cs +++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/HashAlgorithm.cs @@ -20,12 +20,12 @@ protected HashAlgorithm() { } [Obsolete(Obsoletions.DefaultCryptoAlgorithmsMessage, DiagnosticId = Obsoletions.DefaultCryptoAlgorithmsDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] public static HashAlgorithm Create() => - CryptoConfigForwarder.CreateDefaultHashAlgorithm(); + throw new PlatformNotSupportedException(SR.Cryptography_DefaultAlgorithm_NotSupported); [Obsolete(Obsoletions.CryptoStringFactoryMessage, DiagnosticId = Obsoletions.CryptoStringFactoryDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] - [RequiresUnreferencedCode(CryptoConfigForwarder.CreateFromNameUnreferencedCodeMessage)] + [RequiresUnreferencedCode(CryptoConfig.CreateFromNameUnreferencedCodeMessage)] public static HashAlgorithm? Create(string hashName) => - CryptoConfigForwarder.CreateFromName(hashName); + CryptoConfig.CreateFromName(hashName); public virtual int HashSize => HashSizeValue; diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/KeyedHashAlgorithm.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/KeyedHashAlgorithm.cs index 7217e96d6b55e7..3da325913c0bde 100644 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/KeyedHashAlgorithm.cs +++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/KeyedHashAlgorithm.cs @@ -15,9 +15,9 @@ protected KeyedHashAlgorithm() { } throw new PlatformNotSupportedException(SR.Cryptography_DefaultAlgorithm_NotSupported); [Obsolete(Obsoletions.CryptoStringFactoryMessage, DiagnosticId = Obsoletions.CryptoStringFactoryDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] - [RequiresUnreferencedCode(CryptoConfigForwarder.CreateFromNameUnreferencedCodeMessage)] + [RequiresUnreferencedCode(CryptoConfig.CreateFromNameUnreferencedCodeMessage)] public static new KeyedHashAlgorithm? Create(string algName) => - CryptoConfigForwarder.CreateFromName(algName); + CryptoConfig.CreateFromName(algName); public virtual byte[] Key { diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/SymmetricAlgorithm.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/SymmetricAlgorithm.cs index 77664722cc2c36..277b0eab24db9e 100644 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/SymmetricAlgorithm.cs +++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/SymmetricAlgorithm.cs @@ -20,9 +20,9 @@ public static SymmetricAlgorithm Create() => throw new PlatformNotSupportedException(SR.Cryptography_DefaultAlgorithm_NotSupported); [Obsolete(Obsoletions.CryptoStringFactoryMessage, DiagnosticId = Obsoletions.CryptoStringFactoryDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] - [RequiresUnreferencedCode(CryptoConfigForwarder.CreateFromNameUnreferencedCodeMessage)] + [RequiresUnreferencedCode(CryptoConfig.CreateFromNameUnreferencedCodeMessage)] public static SymmetricAlgorithm? Create(string algName) => - CryptoConfigForwarder.CreateFromName(algName); + CryptoConfig.CreateFromName(algName); public virtual int FeedbackSize {