Skip to content

Commit e3fff88

Browse files
authored
Detect if RSA-384 is supported on the platform
1 parent 5305d44 commit e3fff88

File tree

4 files changed

+89
-34
lines changed

4 files changed

+89
-34
lines changed

src/libraries/Common/tests/System/Security/Cryptography/PlatformSupport.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,89 @@ namespace Test.Cryptography
1111
{
1212
internal static class PlatformSupport
1313
{
14+
private static readonly RSAParameters s_rsa384Parameters = new RSAParameters
15+
{
16+
Modulus = new byte[]
17+
{
18+
0xDA, 0xCC, 0x22, 0xD8, 0x6E, 0x67, 0x15, 0x75,
19+
0x03, 0x2E, 0x31, 0xF2, 0x06, 0xDC, 0xFC, 0x19,
20+
0x2C, 0x65, 0xE2, 0xD5, 0x10, 0x89, 0xE5, 0x11,
21+
0x2D, 0x09, 0x6F, 0x28, 0x82, 0xAF, 0xDB, 0x5B,
22+
0x78, 0xCD, 0xB6, 0x57, 0x2F, 0xD2, 0xF6, 0x1D,
23+
0xB3, 0x90, 0x47, 0x22, 0x32, 0xE3, 0xD9, 0xF5,
24+
},
25+
Exponent = new byte[]
26+
{
27+
0x01, 0x00, 0x01,
28+
},
29+
D = new byte[]
30+
{
31+
0x7A, 0x59, 0xBD, 0x02, 0x9A, 0x7A, 0x3A, 0x9D,
32+
0x7C, 0x71, 0xD0, 0xAC, 0x2E, 0xFA, 0x54, 0x5F,
33+
0x1F, 0x5C, 0xBA, 0x43, 0xBB, 0x43, 0xE1, 0x3B,
34+
0x78, 0x77, 0xAF, 0x82, 0xEF, 0xEB, 0x40, 0xC3,
35+
0x8D, 0x1E, 0xCD, 0x73, 0x7F, 0x5B, 0xF9, 0xC8,
36+
0x96, 0x92, 0xB2, 0x9C, 0x87, 0x5E, 0xD6, 0xE1,
37+
},
38+
P = new byte[]
39+
{
40+
0xFA, 0xDB, 0xD7, 0xF8, 0xA1, 0x8B, 0x3A, 0x75,
41+
0xA4, 0xF6, 0xDF, 0xAE, 0xE3, 0x42, 0x6F, 0xD0,
42+
0xFF, 0x8B, 0xAC, 0x74, 0xB6, 0x72, 0x2D, 0xEF,
43+
},
44+
DP = new byte[]
45+
{
46+
0x24, 0xFF, 0xBB, 0xD0, 0xDD, 0xF2, 0xAD, 0x02,
47+
0xA0, 0xFC, 0x10, 0x6D, 0xB8, 0xF3, 0x19, 0x8E,
48+
0xD7, 0xC2, 0x00, 0x03, 0x8E, 0xCD, 0x34, 0x5D,
49+
},
50+
Q = new byte[]
51+
{
52+
0xDF, 0x48, 0x14, 0x4A, 0x6D, 0x88, 0xA7, 0x80,
53+
0x14, 0x4F, 0xCE, 0xA6, 0x6B, 0xDC, 0xDA, 0x50,
54+
0xD6, 0x07, 0x1C, 0x54, 0xE5, 0xD0, 0xDA, 0x5B,
55+
},
56+
DQ = new byte[]
57+
{
58+
0x85, 0xDF, 0x73, 0xBB, 0x04, 0x5D, 0x91, 0x00,
59+
0x6C, 0x2D, 0x45, 0x9B, 0xE6, 0xC4, 0x2E, 0x69,
60+
0x95, 0x4A, 0x02, 0x24, 0xAC, 0xFE, 0x42, 0x4D,
61+
},
62+
InverseQ = new byte[]
63+
{
64+
0x1A, 0x3A, 0x76, 0x9C, 0x21, 0x26, 0x2B, 0x84,
65+
0xCA, 0x9C, 0xA9, 0x62, 0x0F, 0x98, 0xD2, 0xF4,
66+
0x3E, 0xAC, 0xCC, 0xD4, 0x87, 0x9A, 0x6F, 0xFD,
67+
},
68+
};
69+
1470
private static readonly Dictionary<CngAlgorithm, bool> s_platformCryptoSupportedAlgorithms = new();
1571

72+
private static readonly Lazy<bool> s_lazyIsRSA384Supported = new Lazy<bool>(() =>
73+
{
74+
// Linux and Apple are known to support RSA-384, so return true without checking.
75+
if (PlatformDetection.IsLinux || PlatformDetection.IsApplePlatform)
76+
{
77+
return true;
78+
}
79+
80+
RSA rsa = RSA.Create();
81+
82+
try
83+
{
84+
rsa.ImportParameters(s_rsa384Parameters);
85+
return true;
86+
}
87+
catch (CryptographicException)
88+
{
89+
return false;
90+
}
91+
finally
92+
{
93+
rsa.Dispose();
94+
}
95+
});
96+
1697
private static bool PlatformCryptoProviderFunctional(CngAlgorithm algorithm)
1798
{
1899
// Use a full lock around a non-concurrent dictionary. We do not want the value factory for
@@ -91,6 +172,8 @@ private static bool CheckIfVbsAvailable()
91172
// Whether or not the current platform supports RC2
92173
internal static readonly bool IsRC2Supported = !PlatformDetection.IsAndroid;
93174

175+
internal static bool IsRSA384Supported => s_lazyIsRSA384Supported.Value;
176+
94177
#if NET
95178
internal static readonly bool IsAndroidVersionAtLeast31 = OperatingSystem.IsAndroidVersionAtLeast(31);
96179
#else

src/libraries/System.Security.Cryptography.Cng/tests/RSACngProvider.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,17 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Runtime.InteropServices;
5+
using Test.Cryptography;
56

67
namespace System.Security.Cryptography.Rsa.Tests
78
{
89
public class RSACngProvider : IRSAProvider
910
{
10-
private bool? _supports384PrivateKey;
11-
1211
public RSA Create() => new RSACng();
1312

1413
public RSA Create(int keySize) => new RSACng(keySize);
1514

16-
public bool Supports384PrivateKey
17-
{
18-
get
19-
{
20-
if (!_supports384PrivateKey.HasValue)
21-
{
22-
// For Windows 7 (Microsoft Windows 6.1) and Windows 8 (Microsoft Windows 6.2) this is false for RSACng.
23-
_supports384PrivateKey =
24-
!RuntimeInformation.OSDescription.Contains("Windows 6.1") &&
25-
!RuntimeInformation.OSDescription.Contains("Windows 6.2");
26-
}
27-
28-
return _supports384PrivateKey.Value;
29-
}
30-
}
15+
public bool Supports384PrivateKey => PlatformSupport.IsRSA384Supported;
3116

3217
public bool SupportsLargeExponent => true;
3318

src/libraries/System.Security.Cryptography.Csp/tests/RSACryptoServiceProviderProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Runtime.InteropServices;
55
using System.Security.Cryptography.Tests;
6+
using Test.Cryptography;
67

78
namespace System.Security.Cryptography.Rsa.Tests
89
{
@@ -15,7 +16,7 @@ public class RSACryptoServiceProviderProvider : IRSAProvider
1516

1617
public RSA Create(int keySize) => new RSACryptoServiceProvider(keySize);
1718

18-
public bool Supports384PrivateKey => true;
19+
public bool Supports384PrivateKey => PlatformSupport.IsRSA384Supported;
1920

2021
public bool SupportsLargeExponent => false;
2122

src/libraries/System.Security.Cryptography/tests/DefaultRSAProvider.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
using System.Runtime.InteropServices;
55
using System.Security.Cryptography.Tests;
6+
using Test.Cryptography;
67

78
namespace System.Security.Cryptography.Rsa.Tests
89
{
910
public class DefaultRSAProvider : IRSAProvider
1011
{
11-
private bool? _supports384PrivateKey;
1212
private bool? _supportsSha1Signatures;
1313
private bool? _supportsMd5Signatures;
1414

@@ -26,21 +26,7 @@ public RSA Create(int keySize)
2626
#endif
2727
}
2828

29-
public bool Supports384PrivateKey
30-
{
31-
get
32-
{
33-
if (!_supports384PrivateKey.HasValue)
34-
{
35-
// For Windows 7 (Microsoft Windows 6.1) and Windows 8 (Microsoft Windows 6.2) this is false for RSACng.
36-
_supports384PrivateKey = !RuntimeInformation.OSDescription.Contains("Windows 6.1") &&
37-
!RuntimeInformation.OSDescription.Contains("Windows 6.2");
38-
}
39-
40-
return _supports384PrivateKey.Value;
41-
}
42-
}
43-
29+
public bool Supports384PrivateKey => PlatformSupport.IsRSA384Supported;
4430
public bool SupportsSha1Signatures => _supportsSha1Signatures ??= SignatureSupport.CanProduceSha1Signature(Create());
4531
public bool SupportsMd5Signatures => _supportsMd5Signatures ??= SignatureSupport.CanProduceMd5Signature(Create());
4632

0 commit comments

Comments
 (0)