Skip to content

Commit a890e5f

Browse files
CSHARP-3623: Check uses of Rfc2898DeriveBytes when targeting netstandard2.0 (mongodb#512)
1 parent cf5c661 commit a890e5f

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/MongoDB.Driver.Core/Core/Authentication/ScramSha1Authenticator.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121
#endif
2222
using MongoDB.Driver.Core.Misc;
2323

24-
// use our vendored version of Rfc2898DeriveBytes because .NET Standard 1.5 and .NET Framework 4.5 do not support
25-
// a version of Rfc2898DeriveBytes that allows us to specify to hash algorithm to be used
24+
// Use our vendored version of Rfc2898DeriveBytes for .NET Standard 1.5, .NET Standard 2.0 and .NET Framework 4.5.2
25+
// because these targets do not support a version of Rfc2898DeriveBytes that allows to specify the hash algorithm
26+
#if NETSTANDARD2_1
27+
using Rfc2898DeriveBytes = System.Security.Cryptography.Rfc2898DeriveBytes;
28+
#else
2629
using Rfc2898DeriveBytes = MongoDB.Driver.Core.Authentication.Vendored.Rfc2898DeriveBytes;
30+
#endif
2731

2832
namespace MongoDB.Driver.Core.Authentication
2933
{
@@ -81,8 +85,12 @@ private static byte[] H1(byte[] data)
8185
private static byte[] Hi1(UsernamePasswordCredential credential, byte[] salt, int iterations)
8286
{
8387
var passwordDigest = AuthenticationHelper.MongoPasswordDigest(credential.Username, credential.Password);
84-
// 20 is the length of output of a sha-1 hmac
85-
return new Rfc2898DeriveBytes(passwordDigest, salt, iterations).GetBytes(20);
88+
89+
using (var deriveBytes = new Rfc2898DeriveBytes(passwordDigest, salt, iterations, HashAlgorithmName.SHA1))
90+
{
91+
// 20 is the length of output of a sha-1 hmac
92+
return deriveBytes.GetBytes(20);
93+
}
8694
}
8795

8896
private static byte[] Hmac1(UTF8Encoding encoding, byte[] data, string key)

src/MongoDB.Driver.Core/Core/Authentication/ScramSha256Authenticator.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@
2626
#endif
2727
using MongoDB.Driver.Core.Misc;
2828

29-
// use our vendored version of Rfc2898DeriveBytes because .NET Standard 1.5 and .NET Framework 4.5 do not support
30-
// a version of Rfc2898DeriveBytes that allows us to specify to hash algorithm to be used
29+
// Use our vendored version of Rfc2898DeriveBytes for .NET Standard 1.5, .NET Standard 2.0 and .NET Framework 4.5.2
30+
// because these targets do not support a version of Rfc2898DeriveBytes that allows to specify the hash algorithm
31+
#if NETSTANDARD2_1
32+
using Rfc2898DeriveBytes = System.Security.Cryptography.Rfc2898DeriveBytes;
33+
#else
3134
using Rfc2898DeriveBytes = MongoDB.Driver.Core.Authentication.Vendored.Rfc2898DeriveBytes;
35+
#endif
3236

3337
namespace MongoDB.Driver.Core.Authentication
3438
{
@@ -121,12 +125,16 @@ private static byte[] Hi256(char[] passwordChars, byte[] salt, int iterations)
121125
{
122126
var passwordBytes = new byte[Utf8Encodings.Strict.GetByteCount(passwordChars)];
123127
var passwordBytesHandle = GCHandle.Alloc(passwordBytes, GCHandleType.Pinned);
128+
124129
try
125130
{
126131
Utf8Encodings.Strict.GetBytes(passwordChars, 0, passwordChars.Length, passwordBytes, 0);
127132

128-
// 32 is the length of output of a sha-256 hmac
129-
return new Rfc2898DeriveBytes(passwordBytes, salt, iterations, HashAlgorithmName.SHA256).GetBytes(32);
133+
using (var deriveBytes = new Rfc2898DeriveBytes(passwordBytes, salt, iterations, HashAlgorithmName.SHA256))
134+
{
135+
// 32 is the length of output of a sha-256 hmac
136+
return deriveBytes.GetBytes(32);
137+
}
130138
}
131139
finally
132140
{

0 commit comments

Comments
 (0)