Skip to content

Commit ebcd102

Browse files
Unnecessary cryptographic derived types obsoletions (dotnet#52303)
* add new obsoletion ID * nowarn for new obsoletion * add obsoletions to csproj * AesCryptoServiceProvider obsoletion * DESCryptoServiceProvider obsoletion * MD5CryptoServiceProvider obsoletion * RC2CryptoServiceProvider obsoletion * SHA1CryptoServiceProvider obsoletion * SHA256CryptoServiceProvider obsoletion * SHA384CryptoServiceProvider obsoletion * SHA512CryptoServiceProvider obsoletion * TripleDESCryptoServiceProvider obsoletion * add obsoletions to csproj * *Managed obsoletions * add pragma suppress for types usages * add nowarn in tests csproj * add documentation * add nowarn to new identified tests csproj * add pragma suppress for types usages #2 * update documentation * fix md5 usage * Fix indentation * fix obsoletions inclusion in csproj * remove extra obsoletions inclusion in csproj * De-dupe project items that were in both the browser and non-browser configs. Ensure Obsoletions.cs is included in all configs. * Remove duplicate RandomNumberGeneratorImplementation.cs reference * Update documentation * Remove duplicate Obsoletions.cs from project file (introduced in merge) Co-authored-by: Jeff Handley <[email protected]> Co-authored-by: Jeff Handley <[email protected]>
1 parent d515841 commit ebcd102

27 files changed

+65
-36
lines changed

docs/project/list-of-diagnostics.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ The PR that reveals the implementation of the `<IncludeInternalObsoleteAttribute
7575
| __`SYSLIB0018`__ | ReflectionOnly loading is not supported and throws PlatformNotSupportedException. |
7676
| __`SYSLIB0019`__ | RuntimeEnvironment members SystemConfigurationFile, GetRuntimeInterfaceAsIntPtr, and GetRuntimeInterfaceAsObject are no longer supported and throw PlatformNotSupportedException. |
7777
| __`SYSLIB0020`__ | JsonSerializerOptions.IgnoreNullValues is obsolete. To ignore null values when serializing, set DefaultIgnoreCondition to JsonIgnoreCondition.WhenWritingNull. |
78+
| __`SYSLIB0021`__ | Derived cryptographic types are obsolete. Use the Create method on the base type instead. |
7879
| __`SYSLIB0022`__ | The Rijndael and RijndaelManaged types are obsolete. Use Aes instead. |
7980
| __`SYSLIB0023`__ | RNGCryptoServiceProvider is obsolete. To generate a random number, use one of the RandomNumberGenerator static methods instead. |
8081

src/libraries/Common/src/System/Obsoletions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ internal static class Obsoletions
7272
internal const string JsonSerializerOptionsIgnoreNullValuesMessage = "JsonSerializerOptions.IgnoreNullValues is obsolete. To ignore null values when serializing, set DefaultIgnoreCondition to JsonIgnoreCondition.WhenWritingNull.";
7373
internal const string JsonSerializerOptionsIgnoreNullValuesDiagId = "SYSLIB0020";
7474

75+
internal const string DerivedCryptographicTypesMessage = "Derived cryptographic types are obsolete. Use the Create method on the base type instead.";
76+
internal const string DerivedCryptographicTypesDiagId = "SYSLIB0021";
77+
7578
internal const string RijndaelMessage = "The Rijndael and RijndaelManaged types are obsolete. Use Aes instead.";
7679
internal const string RijndaelDiagId = "SYSLIB0022";
7780

src/libraries/Directory.Build.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
SYSLIB0003: Code Access Security (CAS).
2121
SYSLIB0004: Constrained Execution Region (CER).
2222
SYSLIB0017: Strong name signing.
23+
SYSLIB0021: Derived cryptographic types.
2324
SYSLIB0022: Rijndael types.
2425
SYSLIB0023: RNGCryptoServiceProvider.
2526
-->
26-
<NoWarn Condition="'$(IsPartialFacadeAssembly)' == 'true'">$(NoWarn);SYSLIB0003;SYSLIB0004;SYSLIB0015;SYSLIB0017;SYSLIB0022;SYSLIB0023</NoWarn>
27+
<NoWarn Condition="'$(IsPartialFacadeAssembly)' == 'true'">$(NoWarn);SYSLIB0003;SYSLIB0004;SYSLIB0015;SYSLIB0017;SYSLIB0021;SYSLIB0022;SYSLIB0023</NoWarn>
2728
<!-- Reset these properties back to blank, since they are defaulted by Microsoft.NET.Sdk -->
2829
<WarningsAsErrors Condition="'$(WarningsAsErrors)' == 'NU1605'" />
2930
<!-- Set the documentation output file globally. -->

src/libraries/System.Runtime.Serialization.Xml/tests/SerializationTestTypes/DataContract.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public int GetHashCode(RuntimeTypeHandle obj)
208208
public class DataContract
209209
{
210210
static Dictionary<RuntimeTypeHandle, DataContract> cache = new Dictionary<RuntimeTypeHandle, DataContract>(RuntimeTypeHandleEqualityComparer.Comparer);
211-
static MD5CryptoServiceProvider md5 = null;
211+
static MD5 md5 = null;
212212

213213
Type underlyingType;
214214
bool isValueType;
@@ -267,7 +267,7 @@ internal static string ExpandGenericParameters(string format, IGenericNameProvid
267267
private static string GetNamespacesDigest(string namespaces)
268268
{
269269
if (md5 == null)
270-
md5 = new MD5CryptoServiceProvider();
270+
md5 = MD5.Create();
271271
byte[] namespaceBytes = Encoding.UTF8.GetBytes(namespaces);
272272
byte[] digestBytes = md5.ComputeHash(namespaceBytes);
273273
char[] digestChars = new char[24];

src/libraries/System.Security.Cryptography.Algorithms/ref/System.Security.Cryptography.Algorithms.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public void Dispose() { }
4242
public void Encrypt(byte[] nonce, byte[] plaintext, byte[] ciphertext, byte[] tag, byte[]? associatedData = null) { }
4343
public void Encrypt(System.ReadOnlySpan<byte> nonce, System.ReadOnlySpan<byte> plaintext, System.Span<byte> ciphertext, System.Span<byte> tag, System.ReadOnlySpan<byte> associatedData = default(System.ReadOnlySpan<byte>)) { }
4444
}
45+
[System.ObsoleteAttribute("Derived cryptographic types are obsolete. Use the Create method on the base type instead.", DiagnosticId = "SYSLIB0021", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
4546
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
4647
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
4748
public sealed partial class AesManaged : System.Security.Cryptography.Aes
@@ -824,6 +825,7 @@ protected SHA1() { }
824825
public static int HashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination) { throw null; }
825826
public static bool TryHashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination, out int bytesWritten) { throw null; }
826827
}
828+
[System.ObsoleteAttribute("Derived cryptographic types are obsolete. Use the Create method on the base type instead.", DiagnosticId = "SYSLIB0021", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
827829
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
828830
public sealed partial class SHA1Managed : System.Security.Cryptography.SHA1
829831
{
@@ -846,6 +848,7 @@ protected SHA256() { }
846848
public static int HashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination) { throw null; }
847849
public static bool TryHashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination, out int bytesWritten) { throw null; }
848850
}
851+
[System.ObsoleteAttribute("Derived cryptographic types are obsolete. Use the Create method on the base type instead.", DiagnosticId = "SYSLIB0021", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
849852
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
850853
public sealed partial class SHA256Managed : System.Security.Cryptography.SHA256
851854
{
@@ -868,6 +871,7 @@ protected SHA384() { }
868871
public static int HashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination) { throw null; }
869872
public static bool TryHashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination, out int bytesWritten) { throw null; }
870873
}
874+
[System.ObsoleteAttribute("Derived cryptographic types are obsolete. Use the Create method on the base type instead.", DiagnosticId = "SYSLIB0021", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
871875
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
872876
public sealed partial class SHA384Managed : System.Security.Cryptography.SHA384
873877
{
@@ -890,6 +894,7 @@ protected SHA512() { }
890894
public static int HashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination) { throw null; }
891895
public static bool TryHashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination, out int bytesWritten) { throw null; }
892896
}
897+
[System.ObsoleteAttribute("Derived cryptographic types are obsolete. Use the Create method on the base type instead.", DiagnosticId = "SYSLIB0021", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
893898
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
894899
public sealed partial class SHA512Managed : System.Security.Cryptography.SHA512
895900
{

src/libraries/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
44
<DefineConstants>$(DefineConstants);INTERNAL_ASYMMETRIC_IMPLEMENTATIONS</DefineConstants>
@@ -16,14 +16,28 @@
1616
</PropertyGroup>
1717
<ItemGroup>
1818
<Compile Include="System\Security\Cryptography\CryptoConfig.Common.cs" />
19+
<Compile Include="Internal\Cryptography\HashAlgorithmNames.cs" />
20+
<Compile Include="Internal\Cryptography\HMACCommon.cs" />
21+
<Compile Include="Internal\Cryptography\RandomNumberGeneratorImplementation.cs" />
22+
<Compile Include="System\Security\Cryptography\IncrementalHash.cs" />
23+
<Compile Include="System\Security\Cryptography\RandomNumberGenerator.cs" />
24+
<Compile Include="System\Security\Cryptography\SHA1.cs" />
25+
<Compile Include="System\Security\Cryptography\SHA1Managed.cs" />
26+
<Compile Include="System\Security\Cryptography\SHA256.cs" />
27+
<Compile Include="System\Security\Cryptography\SHA256Managed.cs" />
28+
<Compile Include="System\Security\Cryptography\SHA384.cs" />
29+
<Compile Include="System\Security\Cryptography\SHA384Managed.cs" />
30+
<Compile Include="System\Security\Cryptography\SHA512.cs" />
31+
<Compile Include="System\Security\Cryptography\SHA512Managed.cs" />
32+
<Compile Include="$(CommonPath)Internal\Cryptography\HashProvider.cs"
33+
Link="Internal\Cryptography\HashProvider.cs" />
34+
<Compile Include="$(CommonPath)System\Obsoletions.cs"
35+
Link="Common\System\Obsoletions.cs" />
1936
</ItemGroup>
2037
<ItemGroup Condition="'$(TargetsBrowser)' != 'true'">
2138
<Compile Include="Internal\Cryptography\AesImplementation.cs" />
2239
<Compile Include="Internal\Cryptography\DesImplementation.cs" />
2340
<Compile Include="Internal\Cryptography\Helpers.cs" />
24-
<Compile Include="Internal\Cryptography\HMACCommon.cs" />
25-
<Compile Include="Internal\Cryptography\HashAlgorithmNames.cs" />
26-
<Compile Include="Internal\Cryptography\RandomNumberGeneratorImplementation.cs" />
2741
<Compile Include="Internal\Cryptography\RC2Implementation.cs" />
2842
<Compile Include="Internal\Cryptography\RijndaelImplementation.cs" />
2943
<Compile Include="Internal\Cryptography\TripleDesImplementation.cs" />
@@ -61,22 +75,12 @@
6175
<Compile Include="System\Security\Cryptography\HKDF.cs" />
6276
<Compile Include="System\Security\Cryptography\MaskGenerationMethod.cs" />
6377
<Compile Include="System\Security\Cryptography\MD5.cs" />
64-
<Compile Include="System\Security\Cryptography\SHA1.cs" />
65-
<Compile Include="System\Security\Cryptography\SHA1Managed.cs" />
66-
<Compile Include="System\Security\Cryptography\SHA256.cs" />
67-
<Compile Include="System\Security\Cryptography\SHA256Managed.cs" />
68-
<Compile Include="System\Security\Cryptography\SHA384.cs" />
69-
<Compile Include="System\Security\Cryptography\SHA384Managed.cs" />
70-
<Compile Include="System\Security\Cryptography\SHA512.cs" />
71-
<Compile Include="System\Security\Cryptography\SHA512Managed.cs" />
7278
<Compile Include="System\Security\Cryptography\HMACMD5.cs" />
7379
<Compile Include="System\Security\Cryptography\HMACSHA1.cs" />
7480
<Compile Include="System\Security\Cryptography\HMACSHA256.cs" />
7581
<Compile Include="System\Security\Cryptography\HMACSHA384.cs" />
7682
<Compile Include="System\Security\Cryptography\HMACSHA512.cs" />
77-
<Compile Include="System\Security\Cryptography\IncrementalHash.cs" />
7883
<Compile Include="System\Security\Cryptography\PKCS1MaskGenerationMethod.cs" />
79-
<Compile Include="System\Security\Cryptography\RandomNumberGenerator.cs" />
8084
<Compile Include="System\Security\Cryptography\RC2.cs" />
8185
<Compile Include="System\Security\Cryptography\Rijndael.cs" />
8286
<Compile Include="System\Security\Cryptography\RijndaelManaged.cs" />
@@ -104,8 +108,6 @@
104108
Link="Internal\Cryptography\BasicSymmetricCipher.cs" />
105109
<Compile Include="$(CommonPath)Internal\Cryptography\Helpers.cs"
106110
Link="Internal\Cryptography\Helpers.cs" />
107-
<Compile Include="$(CommonPath)Internal\Cryptography\HashProvider.cs"
108-
Link="Internal\Cryptography\HashProvider.cs" />
109111
<Compile Include="$(CommonPath)Internal\Cryptography\PemKeyImportHelpers.cs"
110112
Link="Common\Internal\Cryptography\PemKeyImportHelpers.cs" />
111113
<Compile Include="$(CommonPath)Internal\Cryptography\UniversalCryptoTransform.cs"
@@ -114,8 +116,6 @@
114116
Link="Internal\Cryptography\UniversalCryptoEncryptor.cs" />
115117
<Compile Include="$(CommonPath)Internal\Cryptography\UniversalCryptoDecryptor.cs"
116118
Link="Internal\Cryptography\UniversalCryptoDecryptor.cs" />
117-
<Compile Include="$(CommonPath)System\Obsoletions.cs"
118-
Link="Common\System\Obsoletions.cs" />
119119
<Compile Include="$(CommonPath)System\Memory\PointerMemoryManager.cs"
120120
Link="Common\System\Memory\PointerMemoryManager.cs" />
121121
<Compile Include="$(CommonPath)System\Security\Cryptography\CryptoPool.cs"
@@ -688,34 +688,19 @@
688688
<Compile Include="Internal\Cryptography\Pbkdf2Implementation.Managed.cs" />
689689
</ItemGroup>
690690
<ItemGroup Condition=" '$(TargetsBrowser)' == 'true'">
691-
<Compile Include="$(CommonPath)Internal\Cryptography\HashProvider.cs"
692-
Link="Internal\Cryptography\HashProvider.cs" />
693691
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetRandomBytes.cs"
694692
Link="Common\Interop\Unix\System.Native\Interop.GetRandomBytes.cs" />
695693
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
696694
Link="Common\Interop\Unix\Interop.Libraries.cs" />
697695
<Compile Include="$(CommonPath)System\Sha1ForNonSecretPurposes.cs"
698696
Link="Common\System\Sha1ForNonSecretPurposes.cs" />
699-
<Compile Include="Internal\Cryptography\HashAlgorithmNames.cs" />
700697
<Compile Include="Internal\Cryptography\HashProviderDispenser.Browser.cs" />
701-
<Compile Include="Internal\Cryptography\HMACCommon.cs" />
702-
<Compile Include="Internal\Cryptography\RandomNumberGeneratorImplementation.cs" />
703698
<Compile Include="Internal\Cryptography\RandomNumberGeneratorImplementation.Browser.cs" />
704699
<Compile Include="Internal\Cryptography\SHAHashProvider.Browser.cs" />
705700
<Compile Include="System\Security\Cryptography\AesCcm.NotSupported.cs" />
706701
<Compile Include="System\Security\Cryptography\AesGcm.NotSupported.cs" />
707702
<Compile Include="System\Security\Cryptography\ChaCha20Poly1305.NotSupported.cs" />
708703
<Compile Include="System\Security\Cryptography\CryptoConfig.Browser.cs" />
709-
<Compile Include="System\Security\Cryptography\RandomNumberGenerator.cs" />
710-
<Compile Include="System\Security\Cryptography\IncrementalHash.cs" />
711-
<Compile Include="System\Security\Cryptography\SHA1.cs" />
712-
<Compile Include="System\Security\Cryptography\SHA1Managed.cs" />
713-
<Compile Include="System\Security\Cryptography\SHA256.cs" />
714-
<Compile Include="System\Security\Cryptography\SHA256Managed.cs" />
715-
<Compile Include="System\Security\Cryptography\SHA384.cs" />
716-
<Compile Include="System\Security\Cryptography\SHA384Managed.cs" />
717-
<Compile Include="System\Security\Cryptography\SHA512.cs" />
718-
<Compile Include="System\Security\Cryptography\SHA512Managed.cs" />
719704
</ItemGroup>
720705
<ItemGroup>
721706
<Reference Include="System.Collections" />

src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/AesManaged.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace System.Security.Cryptography
88
{
9+
[Obsolete(Obsoletions.DerivedCryptographicTypesMessage, DiagnosticId = Obsoletions.DerivedCryptographicTypesDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
910
[UnsupportedOSPlatform("browser")]
1011
[EditorBrowsable(EditorBrowsableState.Never)]
1112
public sealed class AesManaged : Aes

src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CryptoConfig.Browser.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public partial class CryptoConfig
3333

3434
switch (name)
3535
{
36+
#pragma warning disable SYSLIB0021 // Obsolete: derived cryptographic types
3637
// hardcode mapping for SHA* algorithm names from https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptoconfig?view=net-5.0#remarks
3738
case "SHA":
3839
case "SHA1":
@@ -50,6 +51,7 @@ public partial class CryptoConfig
5051
case "SHA-512":
5152
case "System.Security.Cryptography.SHA512":
5253
return new SHA512Managed();
54+
#pragma warning restore SYSLIB0021
5355
}
5456

5557
return null;

src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/CryptoConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,12 @@ private static Dictionary<string, object> DefaultNameHT
122122
#pragma warning disable SYSLIB0022 // Rijndael types are obsolete
123123
Type RijndaelManagedType = typeof(System.Security.Cryptography.RijndaelManaged);
124124
#pragma warning restore SYSLIB0022
125+
#pragma warning disable SYSLIB0021 // Obsolete: derived cryptographic types
125126
Type AesManagedType = typeof(System.Security.Cryptography.AesManaged);
126127
Type SHA256DefaultType = typeof(System.Security.Cryptography.SHA256Managed);
127128
Type SHA384DefaultType = typeof(System.Security.Cryptography.SHA384Managed);
128129
Type SHA512DefaultType = typeof(System.Security.Cryptography.SHA512Managed);
130+
#pragma warning restore SYSLIB0021
129131

130132
string SHA1CryptoServiceProviderType = "System.Security.Cryptography.SHA1CryptoServiceProvider, " + AssemblyName_Csp;
131133
string MD5CryptoServiceProviderType = "System.Security.Cryptography.MD5CryptoServiceProvider," + AssemblyName_Csp;

src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/SHA1Managed.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace System.Security.Cryptography
88
{
9+
[Obsolete(Obsoletions.DerivedCryptographicTypesMessage, DiagnosticId = Obsoletions.DerivedCryptographicTypesDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
910
[EditorBrowsable(EditorBrowsableState.Never)]
1011
// SHA1Managed has a copy of the same implementation as SHA1
1112
public sealed class SHA1Managed : SHA1

0 commit comments

Comments
 (0)