Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface IRSAProvider
bool SupportsSha2Oaep { get; }
bool SupportsPss { get; }
bool SupportsSha1Signatures { get; }
bool SupportsMd5Signatures { get; }
bool SupportsSha3 { get; }
}

Expand Down Expand Up @@ -43,6 +44,7 @@ public static RSA Create(RSAParameters rsaParameters)
public static bool SupportsPss => s_provider.SupportsPss;

public static bool SupportsSha1Signatures => s_provider.SupportsSha1Signatures;
public static bool SupportsMd5Signatures => s_provider.SupportsMd5Signatures;

public static bool SupportsSha3 => s_provider.SupportsSha3;
public static bool NoSupportsSha3 => !SupportsSha3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,11 @@ public static IEnumerable<object[]> RoundTripTheories
yield return new object[] { nameof(HashAlgorithmName.SHA1), rsaParameters };
}

yield return new object[] { nameof(HashAlgorithmName.MD5), rsaParameters };
if (RSAFactory.SupportsMd5Signatures)
{
yield return new object[] { nameof(HashAlgorithmName.MD5), rsaParameters };
}

yield return new object[] { nameof(HashAlgorithmName.SHA256), rsaParameters };
}

Expand Down Expand Up @@ -1589,7 +1593,11 @@ public static IEnumerable<object[]> HashAlgorithmNames
yield return new object[] { HashAlgorithmName.SHA256.Name };
yield return new object[] { HashAlgorithmName.SHA384.Name };
yield return new object[] { HashAlgorithmName.SHA512.Name };
yield return new object[] { HashAlgorithmName.MD5.Name };

if (RSAFactory.SupportsMd5Signatures)
{
yield return new object[] { HashAlgorithmName.MD5.Name };
}

if (RSAFactory.SupportsSha1Signatures)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ namespace System.Security.Cryptography.Tests
{
internal static class SignatureSupport
{
internal static bool CanProduceSha1Signature(AsymmetricAlgorithm algorithm)
internal static bool CanProduceSha1Signature(AsymmetricAlgorithm algorithm) => CanProduceSignature(algorithm, HashAlgorithmName.SHA1);
internal static bool CanProduceMd5Signature(AsymmetricAlgorithm algorithm) => CanProduceSignature(algorithm, HashAlgorithmName.MD5);

private static bool CanProduceSignature(AsymmetricAlgorithm algorithm, HashAlgorithmName hashAlgorithmName)
{
using (algorithm)
{
#if NETFRAMEWORK
return true;
#else
// We expect all non-Linux platforms to support SHA1 signatures, currently.
// We expect all non-Linux platforms to support any signatures, currently.
if (!OperatingSystem.IsLinux())
{
return true;
Expand All @@ -23,7 +26,7 @@ internal static bool CanProduceSha1Signature(AsymmetricAlgorithm algorithm)
case ECDsa ecdsa:
try
{
ecdsa.SignData(Array.Empty<byte>(), HashAlgorithmName.SHA1);
ecdsa.SignData(Array.Empty<byte>(), hashAlgorithmName);
return true;
}
catch (CryptographicException)
Expand All @@ -33,7 +36,7 @@ internal static bool CanProduceSha1Signature(AsymmetricAlgorithm algorithm)
case RSA rsa:
try
{
rsa.SignData(Array.Empty<byte>(), HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
rsa.SignData(Array.Empty<byte>(), hashAlgorithmName, RSASignaturePadding.Pkcs1);
return true;
}
catch (CryptographicException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static partial class PlatformDetection
public static bool IsFedora => IsDistroAndVersion("fedora");
public static bool IsLinuxBionic => IsBionic();
public static bool IsRedHatFamily => IsRedHatFamilyAndVersion();
public static bool IsAzureLinux => IsDistroAndVersionOrHigher("azurelinux", 3);

public static bool IsMonoLinuxArm64 => IsMonoRuntime && IsLinux && IsArm64Process;
public static bool IsNotMonoLinuxArm64 => !IsMonoLinuxArm64;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public bool Supports384PrivateKey

public bool SupportsSha1Signatures => true;

public bool SupportsMd5Signatures => true;

public bool SupportsSha3 { get; } = SHA3_256.IsSupported; // If SHA3_256 is supported, assume 384 and 512 are, too.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,12 @@ public static void VerifyLegacySignVerifyHash(bool useLegacySign, bool useLegacy

public static IEnumerable<object[]> AlgorithmIdentifiers()
{
yield return new object[] { "MD5", MD5.Create() };
yield return new object[] { "MD5", typeof(MD5) };
yield return new object[] { "MD5", "1.2.840.113549.2.5" };
if (RSAFactory.SupportsMd5Signatures)
{
yield return new object[] { "MD5", MD5.Create() };
yield return new object[] { "MD5", typeof(MD5) };
yield return new object[] { "MD5", "1.2.840.113549.2.5" };
}

if (RSAFactory.SupportsSha1Signatures)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace System.Security.Cryptography.Rsa.Tests
public class RSACryptoServiceProviderProvider : IRSAProvider
{
private bool? _supportsSha1Signatures;
private bool? _supportsMd5Signatures;

public RSA Create() => new RSACryptoServiceProvider();

Expand All @@ -23,6 +24,7 @@ public class RSACryptoServiceProviderProvider : IRSAProvider
public bool SupportsPss => false;

public bool SupportsSha1Signatures => _supportsSha1Signatures ??= SignatureSupport.CanProduceSha1Signature(Create());
public bool SupportsMd5Signatures => _supportsMd5Signatures ??= SignatureSupport.CanProduceMd5Signature(Create());

public bool SupportsSha3 => false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace System.Security.Cryptography.Rsa.Tests
public class RSAOpenSslProvider : IRSAProvider
{
private bool? _supportsSha1Signatures;
private bool? _supportsMd5Signatures;

public RSA Create() => new RSAOpenSsl();

Expand All @@ -22,6 +23,7 @@ public class RSAOpenSslProvider : IRSAProvider
public bool SupportsPss => true;

public bool SupportsSha1Signatures => _supportsSha1Signatures ??= SignatureSupport.CanProduceSha1Signature(Create());
public bool SupportsMd5Signatures => _supportsMd5Signatures ??= SignatureSupport.CanProduceMd5Signature(Create());

public bool SupportsSha3 => SHA3_256.IsSupported; // If SHA3_256 is supported, assume 384 and 512 are, too.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class DefaultRSAProvider : IRSAProvider
{
private bool? _supports384PrivateKey;
private bool? _supportsSha1Signatures;
private bool? _supportsMd5Signatures;

public RSA Create() => RSA.Create();

Expand Down Expand Up @@ -41,6 +42,7 @@ public bool Supports384PrivateKey
}

public bool SupportsSha1Signatures => _supportsSha1Signatures ??= SignatureSupport.CanProduceSha1Signature(Create());
public bool SupportsMd5Signatures => _supportsMd5Signatures ??= SignatureSupport.CanProduceMd5Signature(Create());

public bool SupportsLargeExponent => true;

Expand Down
7 changes: 4 additions & 3 deletions src/libraries/System.Security.Cryptography/tests/HKDFTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public abstract class HKDFTests
protected abstract byte[] Expand(HashAlgorithmName hash, byte[] prk, int outputLength, byte[] info);
protected abstract byte[] DeriveKey(HashAlgorithmName hash, byte[] ikm, int outputLength, byte[] salt, byte[] info);

internal static bool MD5Supported => !PlatformDetection.IsBrowser && !PlatformDetection.IsAzureLinux;

[Theory]
[MemberData(nameof(GetHkdfTestCases))]
public void ExtractTests(HkdfTestCase test)
Expand All @@ -22,9 +24,8 @@ public void ExtractTests(HkdfTestCase test)
Assert.Equal(test.Prk, prk);
}

[Theory]
[ConditionalTheory(nameof(MD5Supported))]
[MemberData(nameof(GetHkdfTestCases))]
[SkipOnPlatform(TestPlatforms.Browser, "MD5 is not supported on Browser")]
public void ExtractTamperHashTests(HkdfTestCase test)
{
byte[] prk = Extract(HashAlgorithmName.MD5, 128 / 8, test.Ikm, test.Salt);
Expand Down Expand Up @@ -257,7 +258,7 @@ public static IEnumerable<object[]> GetPrkTooShortTestCases()
yield return new object[] { HashAlgorithmName.SHA256, 256 / 8 - 1 };
yield return new object[] { HashAlgorithmName.SHA512, 512 / 8 - 1 };

if (!PlatformDetection.IsBrowser)
if (MD5Supported)
{
yield return new object[] { HashAlgorithmName.MD5, 128 / 8 - 1 };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

namespace System.Security.Cryptography.Tests
{
[SkipOnPlatform(TestPlatforms.Browser, "Not supported on Browser")]
[ConditionalClass(typeof(HmacMD5Tests.Traits), nameof(HmacMD5Tests.Traits.IsSupported))]
public class HmacMD5Tests : Rfc2202HmacTests<HmacMD5Tests.Traits>
{
public sealed class Traits : IHmacTrait
{
public static bool IsSupported => true;
public static bool IsSupported => !PlatformDetection.IsAzureLinux && !PlatformDetection.IsBrowser;
public static int HashSizeInBytes => HMACMD5.HashSizeInBytes;
}

Expand Down