Skip to content

Bring back support for netstandard2.0 #1675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</PropertyGroup>

<PropertyGroup>
<TargetFrameworks>netstandard2.1;net472;net6.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<LangVersion>12.0</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<CodeAnalysisRuleSet>..\..\MongoDB.ruleset</CodeAnalysisRuleSet>
Expand Down
2 changes: 2 additions & 0 deletions src/MongoDB.Bson/TargetFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ internal static class TargetFramework
"net472";
#elif NETSTANDARD2_1
"netstandard21";
#elif NETSTANDARD2_0
"netstandard20";
#elif NET6_0
"net60";
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static bool RsaSign(
#pragma warning disable CA1801
public static byte[] HashAndSignBytes(byte[] dataToSign, byte[] key)
{
#if !NET472
#if !NETSTANDARD2_0
using (var rsaProvider = new RSACryptoServiceProvider())
{
rsaProvider.ImportPkcs8PrivateKey(key, out _);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
using System.Security.Cryptography;
using System.Text;

// Use our vendored version of Rfc2898DeriveBytes for .NET Standard 2.0
// because this target does not support a version of Rfc2898DeriveBytes that allows to specify the hash algorithm
#if NETSTANDARD2_0
using Rfc2898DeriveBytes = MongoDB.Driver.Authentication.Vendored.Rfc2898DeriveBytes;
#else
using Rfc2898DeriveBytes = System.Security.Cryptography.Rfc2898DeriveBytes;
#endif

namespace MongoDB.Driver.Authentication.ScramSha
{
internal sealed class ScramSha1Algorithm : IScramShaAlgorithm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
using System.Text;
using MongoDB.Bson.IO;

// Use our vendored version of Rfc2898DeriveBytes for .NET Standard 2.0
// because this target does not support a version of Rfc2898DeriveBytes that allows to specify the hash algorithm
#if NETSTANDARD2_0
using Rfc2898DeriveBytes = MongoDB.Driver.Authentication.Vendored.Rfc2898DeriveBytes;
#else
using Rfc2898DeriveBytes = System.Security.Cryptography.Rfc2898DeriveBytes;
#endif

namespace MongoDB.Driver.Authentication.ScramSha
{
internal sealed class ScramSha256Algorithm : IScramShaAlgorithm
Expand Down
175 changes: 175 additions & 0 deletions src/MongoDB.Driver/Authentication/Vendored/CryptographyHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/* Original work:
* Copyright 2016 .NET Foundation and Contributors
*
* The MIT License (MIT)
*
* Copyright (c) .NET Foundation and Contributors
*
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Modified work:
* Copyright 2018–present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


using System.Diagnostics;
using System.Security.Cryptography;

namespace MongoDB.Driver.Authentication.Vendored
{
internal static class CryptographyHelpers
{

private static readonly string Cryptography_MissingIV
= "The cipher mode specified requires that an initialization vector (IV) be used.";

public static byte[] CloneByteArray(this byte[] src)
{
if (src == null)
{
return null;
}

return (byte[])(src.Clone());
}

public static KeySizes[] CloneKeySizesArray(this KeySizes[] src)
{
return (KeySizes[])(src.Clone());
}

public static bool UsesIv(this CipherMode cipherMode)
{
return cipherMode != CipherMode.ECB;
}

public static byte[] GetCipherIv(this CipherMode cipherMode, byte[] iv)
{
if (cipherMode.UsesIv())
{
if (iv == null)
{
throw new CryptographicException(Cryptography_MissingIV);
}

return iv;
}

return null;
}

public static bool IsLegalSize(this int size, KeySizes[] legalSizes)
{
for (int i = 0; i < legalSizes.Length; i++)
{
KeySizes currentSizes = legalSizes[i];

// If a cipher has only one valid key size, MinSize == MaxSize and SkipSize will be 0
if (currentSizes.SkipSize == 0)
{
if (currentSizes.MinSize == size)
return true;
}
else if (size >= currentSizes.MinSize && size <= currentSizes.MaxSize)
{
// If the number is in range, check to see if it's a legal increment above MinSize
int delta = size - currentSizes.MinSize;

// While it would be unusual to see KeySizes { 10, 20, 5 } and { 11, 14, 1 }, it could happen.
// So don't return false just because this one doesn't match.
if (delta % currentSizes.SkipSize == 0)
{
return true;
}
}
}
return false;
}

public static byte[] GenerateRandom(int count)
{
byte[] buffer = new byte[count];
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
rng.GetBytes(buffer);
}
return buffer;
}

// encodes the integer i into a 4-byte array, in big endian.
public static void WriteInt(uint i, byte[] arr, int offset)
{
unchecked
{
Debug.Assert(arr != null);
Debug.Assert(arr.Length >= offset + sizeof(uint));

arr[offset] = (byte)(i >> 24);
arr[offset + 1] = (byte)(i >> 16);
arr[offset + 2] = (byte)(i >> 8);
arr[offset + 3] = (byte)i;
}
}

public static byte[] FixupKeyParity(this byte[] key)
{
byte[] oddParityKey = new byte[key.Length];
for (int index = 0; index < key.Length; index++)
{
// Get the bits we are interested in
oddParityKey[index] = (byte)(key[index] & 0xfe);

// Get the parity of the sum of the previous bits
byte tmp1 = (byte)((oddParityKey[index] & 0xF) ^ (oddParityKey[index] >> 4));
byte tmp2 = (byte)((tmp1 & 0x3) ^ (tmp1 >> 2));
byte sumBitsMod2 = (byte)((tmp2 & 0x1) ^ (tmp2 >> 1));

// We need to set the last bit in oddParityKey[index] to the negation
// of the last bit in sumBitsMod2
if (sumBitsMod2 == 0)
oddParityKey[index] |= 1;
}
return oddParityKey;
}

internal static void ConvertIntToByteArray(uint value, byte[] dest)
{
Debug.Assert(dest != null);
Debug.Assert(dest.Length == 4);
dest[0] = (byte)((value & 0xFF000000) >> 24);
dest[1] = (byte)((value & 0xFF0000) >> 16);
dest[2] = (byte)((value & 0xFF00) >> 8);
dest[3] = (byte)(value & 0xFF);
}
}
}
Loading