Skip to content

Commit

Permalink
Mark RNGCryptoServiceProvider as Obsolete (#52373)
Browse files Browse the repository at this point in the history
* Mark RNGCryptoServiceProvider as Obsolete

* Use the static methods on RandomNumberGenerator instead of Create()

* Use RandomNumberGenerator.GetBytes() instead of RandomNumberGenerator.Create().GetBytes()

* Revert "Use RandomNumberGenerator.GetBytes() instead of RandomNumberGenerator.Create().GetBytes()"

This reverts commit 447f848.

* Update src/libraries/System.IO.Pipes/tests/NamedPipeTests/NamedPipeTest.CurrentUserOnly.Windows.cs

Co-authored-by: Jeremy Barton <jbarton@microsoft.com>
  • Loading branch information
jeffhandley and bartonjs authored May 8, 2021
1 parent db85f31 commit ebd695f
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 9 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 @@ -76,6 +76,7 @@ The PR that reveals the implementation of the `<IncludeInternalObsoleteAttribute
| __`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. |
| __`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. |

## Analyzer Warnings

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 @@ -74,5 +74,8 @@ internal static class Obsoletions

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

internal const string RNGCryptoServiceProviderMessage = "RNGCryptoServiceProvider is obsolete. To generate a random number, use one of the RandomNumberGenerator static methods instead.";
internal const string RNGCryptoServiceProviderDiagId = "SYSLIB0023";
}
}
3 changes: 2 additions & 1 deletion src/libraries/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
SYSLIB0004: Constrained Execution Region (CER).
SYSLIB0017: Strong name signing.
SYSLIB0022: Rijndael types.
SYSLIB0023: RNGCryptoServiceProvider.
-->
<NoWarn Condition="'$(IsPartialFacadeAssembly)' == 'true'">$(NoWarn);SYSLIB0003;SYSLIB0004;SYSLIB0015;SYSLIB0017;SYSLIB0022</NoWarn>
<NoWarn Condition="'$(IsPartialFacadeAssembly)' == 'true'">$(NoWarn);SYSLIB0003;SYSLIB0004;SYSLIB0015;SYSLIB0017;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 @@ -22,14 +22,10 @@ public class TestAccountImpersonator : IDisposable
public TestAccountImpersonator()
{
string testAccountPassword;
using (RandomNumberGenerator rng = new RNGCryptoServiceProvider())
{
var randomBytes = new byte[33];
rng.GetBytes(randomBytes);
byte[] randomBytes = RandomNumberGenerator.GetBytes(33);

// Add special chars to ensure it satisfies password requirements.
testAccountPassword = Convert.ToBase64String(randomBytes) + "_-As@!%*(1)4#2";
}
// Add special chars to ensure it satisfies password requirements.
testAccountPassword = Convert.ToBase64String(randomBytes) + "_-As@!%*(1)4#2";

DateTime accountExpirationDate = DateTime.UtcNow + TimeSpan.FromMinutes(2);
using (var principalCtx = new PrincipalContext(ContextType.Machine))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public RC2CryptoServiceProvider() { }
public override void GenerateIV() { }
public override void GenerateKey() { }
}
[System.ObsoleteAttribute("RNGCryptoServiceProvider is obsolete. To generate a random number, use one of the RandomNumberGenerator static methods instead.", DiagnosticId = "SYSLIB0023", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
public sealed partial class RNGCryptoServiceProvider : System.Security.Cryptography.RandomNumberGenerator
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<Compile Include="Internal\Cryptography\Helpers.cs" />
<Compile Include="$(CommonPath)Internal\Cryptography\Helpers.cs"
Link="Internal\Cryptography\Helpers.cs" />
<Compile Include="$(CommonPath)System\Obsoletions.cs"
Link="Common\System\Obsoletions.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\KeySizeHelpers.cs"
Link="Common\System\Security\Cryptography\KeySizeHelpers.cs" />
<Compile Include="$(CommonPath)System\Security\Cryptography\CryptoPool.cs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace System.Security.Cryptography
{
[Obsolete(Obsoletions.RNGCryptoServiceProviderMessage, DiagnosticId = Obsoletions.RNGCryptoServiceProviderDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class RNGCryptoServiceProvider : RandomNumberGenerator
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Linq;
using Xunit;

#pragma warning disable SYSLIB0023 // RNGCryptoServiceProvider is obsolete

namespace System.Security.Cryptography.RNG.Tests
{
/// <summary>
Expand Down Expand Up @@ -120,3 +122,5 @@ public static void VerifyCtors()
}
}
}

#pragma warning restore SYSLIB0023
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public WindowsTestAccount(string userName)
private void CreateUser()
{
string testAccountPassword;
using (RandomNumberGenerator rng = new RNGCryptoServiceProvider())
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
byte[] randomBytes = new byte[33];
rng.GetBytes(randomBytes);
Expand Down

0 comments on commit ebd695f

Please sign in to comment.