Skip to content

Commit 2238cc9

Browse files
authored
Detect if RSA-384 is supported on the platform
1 parent fabc884 commit 2238cc9

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

103+
private static readonly Lazy<bool> s_lazyIsRSA384Supported = new Lazy<bool>(() =>
104+
{
105+
// Linux and Apple are known to support RSA-384, so return true without checking.
106+
if (PlatformDetection.IsLinux || PlatformDetection.IsOSXLike)
107+
{
108+
return true;
109+
}
110+
111+
RSA rsa = RSA.Create();
112+
113+
try
114+
{
115+
rsa.ImportParameters(s_rsa384Parameters);
116+
return true;
117+
}
118+
catch (CryptographicException)
119+
{
120+
return false;
121+
}
122+
finally
123+
{
124+
rsa.Dispose();
125+
}
126+
});
127+
47128
// Platforms that use Apple Cryptography
48129
internal const TestPlatforms AppleCrypto = TestPlatforms.OSX | TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst;
49130
internal const TestPlatforms MobileAppleCrypto = TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst;
@@ -54,6 +135,8 @@ internal static class PlatformSupport
54135
// Whether or not the current platform supports RC2
55136
internal static readonly bool IsRC2Supported = !PlatformDetection.IsAndroid;
56137

138+
internal static bool IsRSA384Supported => s_lazyIsRSA384Supported.Value;
139+
57140
#if NETCOREAPP
58141
internal static readonly bool IsAndroidVersionAtLeast31 = OperatingSystem.IsAndroidVersionAtLeast(31);
59142
#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)