Skip to content

Commit

Permalink
Unnecessary cryptographic derived types obsoletions (#52303)
Browse files Browse the repository at this point in the history
* 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 <jeffhandley@users.noreply.github.com>
Co-authored-by: Jeff Handley <jeff.handley@microsoft.com>
  • Loading branch information
3 people authored May 11, 2021
1 parent d515841 commit ebcd102
Show file tree
Hide file tree
Showing 27 changed files with 65 additions and 36 deletions.
1 change: 1 addition & 0 deletions docs/project/list-of-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ The PR that reveals the implementation of the `<IncludeInternalObsoleteAttribute
| __`SYSLIB0018`__ | ReflectionOnly loading is not supported and throws PlatformNotSupportedException. |
| __`SYSLIB0019`__ | RuntimeEnvironment members SystemConfigurationFile, GetRuntimeInterfaceAsIntPtr, and GetRuntimeInterfaceAsObject are no longer supported and throw PlatformNotSupportedException. |
| __`SYSLIB0020`__ | JsonSerializerOptions.IgnoreNullValues is obsolete. To ignore null values when serializing, set DefaultIgnoreCondition to JsonIgnoreCondition.WhenWritingNull. |
| __`SYSLIB0021`__ | Derived cryptographic types are obsolete. Use the Create method on the base type instead. |
| __`SYSLIB0022`__ | The Rijndael and RijndaelManaged types are obsolete. Use Aes instead. |
| __`SYSLIB0023`__ | RNGCryptoServiceProvider is obsolete. To generate a random number, use one of the RandomNumberGenerator static methods instead. |

Expand Down
3 changes: 3 additions & 0 deletions src/libraries/Common/src/System/Obsoletions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ internal static class Obsoletions
internal const string JsonSerializerOptionsIgnoreNullValuesMessage = "JsonSerializerOptions.IgnoreNullValues is obsolete. To ignore null values when serializing, set DefaultIgnoreCondition to JsonIgnoreCondition.WhenWritingNull.";
internal const string JsonSerializerOptionsIgnoreNullValuesDiagId = "SYSLIB0020";

internal const string DerivedCryptographicTypesMessage = "Derived cryptographic types are obsolete. Use the Create method on the base type instead.";
internal const string DerivedCryptographicTypesDiagId = "SYSLIB0021";

internal const string RijndaelMessage = "The Rijndael and RijndaelManaged types are obsolete. Use Aes instead.";
internal const string RijndaelDiagId = "SYSLIB0022";

Expand Down
3 changes: 2 additions & 1 deletion src/libraries/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
SYSLIB0003: Code Access Security (CAS).
SYSLIB0004: Constrained Execution Region (CER).
SYSLIB0017: Strong name signing.
SYSLIB0021: Derived cryptographic types.
SYSLIB0022: Rijndael types.
SYSLIB0023: RNGCryptoServiceProvider.
-->
<NoWarn Condition="'$(IsPartialFacadeAssembly)' == 'true'">$(NoWarn);SYSLIB0003;SYSLIB0004;SYSLIB0015;SYSLIB0017;SYSLIB0022;SYSLIB0023</NoWarn>
<NoWarn Condition="'$(IsPartialFacadeAssembly)' == 'true'">$(NoWarn);SYSLIB0003;SYSLIB0004;SYSLIB0015;SYSLIB0017;SYSLIB0021;SYSLIB0022;SYSLIB0023</NoWarn>
<!-- Reset these properties back to blank, since they are defaulted by Microsoft.NET.Sdk -->
<WarningsAsErrors Condition="'$(WarningsAsErrors)' == 'NU1605'" />
<!-- Set the documentation output file globally. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public int GetHashCode(RuntimeTypeHandle obj)
public class DataContract
{
static Dictionary<RuntimeTypeHandle, DataContract> cache = new Dictionary<RuntimeTypeHandle, DataContract>(RuntimeTypeHandleEqualityComparer.Comparer);
static MD5CryptoServiceProvider md5 = null;
static MD5 md5 = null;

Type underlyingType;
bool isValueType;
Expand Down Expand Up @@ -267,7 +267,7 @@ internal static string ExpandGenericParameters(string format, IGenericNameProvid
private static string GetNamespacesDigest(string namespaces)
{
if (md5 == null)
md5 = new MD5CryptoServiceProvider();
md5 = MD5.Create();
byte[] namespaceBytes = Encoding.UTF8.GetBytes(namespaces);
byte[] digestBytes = md5.ComputeHash(namespaceBytes);
char[] digestChars = new char[24];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public void Dispose() { }
public void Encrypt(byte[] nonce, byte[] plaintext, byte[] ciphertext, byte[] tag, byte[]? associatedData = null) { }
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>)) { }
}
[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}")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
public sealed partial class AesManaged : System.Security.Cryptography.Aes
Expand Down Expand Up @@ -824,6 +825,7 @@ protected SHA1() { }
public static int HashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination) { throw null; }
public static bool TryHashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination, out int bytesWritten) { throw null; }
}
[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}")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public sealed partial class SHA1Managed : System.Security.Cryptography.SHA1
{
Expand All @@ -846,6 +848,7 @@ protected SHA256() { }
public static int HashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination) { throw null; }
public static bool TryHashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination, out int bytesWritten) { throw null; }
}
[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}")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public sealed partial class SHA256Managed : System.Security.Cryptography.SHA256
{
Expand All @@ -868,6 +871,7 @@ protected SHA384() { }
public static int HashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination) { throw null; }
public static bool TryHashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination, out int bytesWritten) { throw null; }
}
[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}")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public sealed partial class SHA384Managed : System.Security.Cryptography.SHA384
{
Expand All @@ -890,6 +894,7 @@ protected SHA512() { }
public static int HashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination) { throw null; }
public static bool TryHashData(System.ReadOnlySpan<byte> source, System.Span<byte> destination, out int bytesWritten) { throw null; }
}
[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}")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public sealed partial class SHA512Managed : System.Security.Cryptography.SHA512
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefineConstants>$(DefineConstants);INTERNAL_ASYMMETRIC_IMPLEMENTATIONS</DefineConstants>
Expand All @@ -16,14 +16,28 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="System\Security\Cryptography\CryptoConfig.Common.cs" />
<Compile Include="Internal\Cryptography\HashAlgorithmNames.cs" />
<Compile Include="Internal\Cryptography\HMACCommon.cs" />
<Compile Include="Internal\Cryptography\RandomNumberGeneratorImplementation.cs" />
<Compile Include="System\Security\Cryptography\IncrementalHash.cs" />
<Compile Include="System\Security\Cryptography\RandomNumberGenerator.cs" />
<Compile Include="System\Security\Cryptography\SHA1.cs" />
<Compile Include="System\Security\Cryptography\SHA1Managed.cs" />
<Compile Include="System\Security\Cryptography\SHA256.cs" />
<Compile Include="System\Security\Cryptography\SHA256Managed.cs" />
<Compile Include="System\Security\Cryptography\SHA384.cs" />
<Compile Include="System\Security\Cryptography\SHA384Managed.cs" />
<Compile Include="System\Security\Cryptography\SHA512.cs" />
<Compile Include="System\Security\Cryptography\SHA512Managed.cs" />
<Compile Include="$(CommonPath)Internal\Cryptography\HashProvider.cs"
Link="Internal\Cryptography\HashProvider.cs" />
<Compile Include="$(CommonPath)System\Obsoletions.cs"
Link="Common\System\Obsoletions.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsBrowser)' != 'true'">
<Compile Include="Internal\Cryptography\AesImplementation.cs" />
<Compile Include="Internal\Cryptography\DesImplementation.cs" />
<Compile Include="Internal\Cryptography\Helpers.cs" />
<Compile Include="Internal\Cryptography\HMACCommon.cs" />
<Compile Include="Internal\Cryptography\HashAlgorithmNames.cs" />
<Compile Include="Internal\Cryptography\RandomNumberGeneratorImplementation.cs" />
<Compile Include="Internal\Cryptography\RC2Implementation.cs" />
<Compile Include="Internal\Cryptography\RijndaelImplementation.cs" />
<Compile Include="Internal\Cryptography\TripleDesImplementation.cs" />
Expand Down Expand Up @@ -61,22 +75,12 @@
<Compile Include="System\Security\Cryptography\HKDF.cs" />
<Compile Include="System\Security\Cryptography\MaskGenerationMethod.cs" />
<Compile Include="System\Security\Cryptography\MD5.cs" />
<Compile Include="System\Security\Cryptography\SHA1.cs" />
<Compile Include="System\Security\Cryptography\SHA1Managed.cs" />
<Compile Include="System\Security\Cryptography\SHA256.cs" />
<Compile Include="System\Security\Cryptography\SHA256Managed.cs" />
<Compile Include="System\Security\Cryptography\SHA384.cs" />
<Compile Include="System\Security\Cryptography\SHA384Managed.cs" />
<Compile Include="System\Security\Cryptography\SHA512.cs" />
<Compile Include="System\Security\Cryptography\SHA512Managed.cs" />
<Compile Include="System\Security\Cryptography\HMACMD5.cs" />
<Compile Include="System\Security\Cryptography\HMACSHA1.cs" />
<Compile Include="System\Security\Cryptography\HMACSHA256.cs" />
<Compile Include="System\Security\Cryptography\HMACSHA384.cs" />
<Compile Include="System\Security\Cryptography\HMACSHA512.cs" />
<Compile Include="System\Security\Cryptography\IncrementalHash.cs" />
<Compile Include="System\Security\Cryptography\PKCS1MaskGenerationMethod.cs" />
<Compile Include="System\Security\Cryptography\RandomNumberGenerator.cs" />
<Compile Include="System\Security\Cryptography\RC2.cs" />
<Compile Include="System\Security\Cryptography\Rijndael.cs" />
<Compile Include="System\Security\Cryptography\RijndaelManaged.cs" />
Expand Down Expand Up @@ -104,8 +108,6 @@
Link="Internal\Cryptography\BasicSymmetricCipher.cs" />
<Compile Include="$(CommonPath)Internal\Cryptography\Helpers.cs"
Link="Internal\Cryptography\Helpers.cs" />
<Compile Include="$(CommonPath)Internal\Cryptography\HashProvider.cs"
Link="Internal\Cryptography\HashProvider.cs" />
<Compile Include="$(CommonPath)Internal\Cryptography\PemKeyImportHelpers.cs"
Link="Common\Internal\Cryptography\PemKeyImportHelpers.cs" />
<Compile Include="$(CommonPath)Internal\Cryptography\UniversalCryptoTransform.cs"
Expand All @@ -114,8 +116,6 @@
Link="Internal\Cryptography\UniversalCryptoEncryptor.cs" />
<Compile Include="$(CommonPath)Internal\Cryptography\UniversalCryptoDecryptor.cs"
Link="Internal\Cryptography\UniversalCryptoDecryptor.cs" />
<Compile Include="$(CommonPath)System\Obsoletions.cs"
Link="Common\System\Obsoletions.cs" />
<Compile Include="$(CommonPath)System\Memory\PointerMemoryManager.cs"
Link="Common\System\Memory\PointerMemoryManager.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\CryptoPool.cs"
Expand Down Expand Up @@ -688,34 +688,19 @@
<Compile Include="Internal\Cryptography\Pbkdf2Implementation.Managed.cs" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetsBrowser)' == 'true'">
<Compile Include="$(CommonPath)Internal\Cryptography\HashProvider.cs"
Link="Internal\Cryptography\HashProvider.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetRandomBytes.cs"
Link="Common\Interop\Unix\System.Native\Interop.GetRandomBytes.cs" />
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
Link="Common\Interop\Unix\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)System\Sha1ForNonSecretPurposes.cs"
Link="Common\System\Sha1ForNonSecretPurposes.cs" />
<Compile Include="Internal\Cryptography\HashAlgorithmNames.cs" />
<Compile Include="Internal\Cryptography\HashProviderDispenser.Browser.cs" />
<Compile Include="Internal\Cryptography\HMACCommon.cs" />
<Compile Include="Internal\Cryptography\RandomNumberGeneratorImplementation.cs" />
<Compile Include="Internal\Cryptography\RandomNumberGeneratorImplementation.Browser.cs" />
<Compile Include="Internal\Cryptography\SHAHashProvider.Browser.cs" />
<Compile Include="System\Security\Cryptography\AesCcm.NotSupported.cs" />
<Compile Include="System\Security\Cryptography\AesGcm.NotSupported.cs" />
<Compile Include="System\Security\Cryptography\ChaCha20Poly1305.NotSupported.cs" />
<Compile Include="System\Security\Cryptography\CryptoConfig.Browser.cs" />
<Compile Include="System\Security\Cryptography\RandomNumberGenerator.cs" />
<Compile Include="System\Security\Cryptography\IncrementalHash.cs" />
<Compile Include="System\Security\Cryptography\SHA1.cs" />
<Compile Include="System\Security\Cryptography\SHA1Managed.cs" />
<Compile Include="System\Security\Cryptography\SHA256.cs" />
<Compile Include="System\Security\Cryptography\SHA256Managed.cs" />
<Compile Include="System\Security\Cryptography\SHA384.cs" />
<Compile Include="System\Security\Cryptography\SHA384Managed.cs" />
<Compile Include="System\Security\Cryptography\SHA512.cs" />
<Compile Include="System\Security\Cryptography\SHA512Managed.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Collections" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace System.Security.Cryptography
{
[Obsolete(Obsoletions.DerivedCryptographicTypesMessage, DiagnosticId = Obsoletions.DerivedCryptographicTypesDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
[UnsupportedOSPlatform("browser")]
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class AesManaged : Aes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public partial class CryptoConfig

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

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ private static Dictionary<string, object> DefaultNameHT
#pragma warning disable SYSLIB0022 // Rijndael types are obsolete
Type RijndaelManagedType = typeof(System.Security.Cryptography.RijndaelManaged);
#pragma warning restore SYSLIB0022
#pragma warning disable SYSLIB0021 // Obsolete: derived cryptographic types
Type AesManagedType = typeof(System.Security.Cryptography.AesManaged);
Type SHA256DefaultType = typeof(System.Security.Cryptography.SHA256Managed);
Type SHA384DefaultType = typeof(System.Security.Cryptography.SHA384Managed);
Type SHA512DefaultType = typeof(System.Security.Cryptography.SHA512Managed);
#pragma warning restore SYSLIB0021

string SHA1CryptoServiceProviderType = "System.Security.Cryptography.SHA1CryptoServiceProvider, " + AssemblyName_Csp;
string MD5CryptoServiceProviderType = "System.Security.Cryptography.MD5CryptoServiceProvider," + AssemblyName_Csp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace System.Security.Cryptography
{
[Obsolete(Obsoletions.DerivedCryptographicTypesMessage, DiagnosticId = Obsoletions.DerivedCryptographicTypesDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
[EditorBrowsable(EditorBrowsableState.Never)]
// SHA1Managed has a copy of the same implementation as SHA1
public sealed class SHA1Managed : SHA1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace System.Security.Cryptography
{
[Obsolete(Obsoletions.DerivedCryptographicTypesMessage, DiagnosticId = Obsoletions.DerivedCryptographicTypesDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
[EditorBrowsable(EditorBrowsableState.Never)]
// SHA256Managed has a copy of the same implementation as SHA256
public sealed class SHA256Managed : SHA256
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace System.Security.Cryptography
{
[Obsolete(Obsoletions.DerivedCryptographicTypesMessage, DiagnosticId = Obsoletions.DerivedCryptographicTypesDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
[EditorBrowsable(EditorBrowsableState.Never)]
// SHA384Managed has a copy of the same implementation as SHA384
public sealed class SHA384Managed : SHA384
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace System.Security.Cryptography
{
[Obsolete(Obsoletions.DerivedCryptographicTypesMessage, DiagnosticId = Obsoletions.DerivedCryptographicTypesDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
[EditorBrowsable(EditorBrowsableState.Never)]
// SHA512Managed has a copy of the same implementation as SHA512
public sealed class SHA512Managed : SHA512
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Android;$(NetCoreAppCurrent)-Browser</TargetFrameworks>
<UseAndroidCrypto Condition="'$(TargetsAndroid)' == 'true'">true</UseAndroidCrypto>
<UseAppleCrypto Condition="'$(TargetsOSX)' == 'true' or '$(TargetsiOS)' == 'true' or '$(TargetstvOS)' == 'true'">true</UseAppleCrypto>
<!-- SYSLIB0021: Derived cryptographic types are obsolete -->
<NoWarn>$(NoWarn);SYSLIB0021</NoWarn>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(CommonTestPath)System\IO\PositionValueStream.cs"
Expand Down
Loading

0 comments on commit ebcd102

Please sign in to comment.