Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit c459f08

Browse files
committed
Merge pull request #2233 from bartonjs/UnixRsa-1984
Fix outstanding issues in Unix RSA tests
2 parents 4d96146 + e80c82a commit c459f08

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

src/System.Security.Cryptography.RSA/src/Internal/Cryptography/RsaOpenSsl.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,15 @@ public override RSAParameters ExportParameters(bool includePrivateParameters)
142142

143143
CheckInvalidKey(key);
144144

145-
return Interop.libcrypto.ExportRsaParameters(key, includePrivateParameters);
145+
RSAParameters rsaParameters = Interop.libcrypto.ExportRsaParameters(key, includePrivateParameters);
146+
bool hasPrivateKey = rsaParameters.D != null;
147+
148+
if (hasPrivateKey != includePrivateParameters || !HasConsistentPrivateKey(ref rsaParameters))
149+
{
150+
throw new CryptographicException(SR.Cryptography_CSP_NoPrivateKey);
151+
}
152+
153+
return rsaParameters;
146154
}
147155

148156
public override unsafe void ImportParameters(RSAParameters parameters)
@@ -218,6 +226,12 @@ private static void ValidateParameters(ref RSAParameters parameters)
218226
if (parameters.Modulus == null || parameters.Exponent == null)
219227
throw new CryptographicException(SR.Argument_InvalidValue);
220228

229+
if (!HasConsistentPrivateKey(ref parameters))
230+
throw new CryptographicException(SR.Argument_InvalidValue);
231+
}
232+
233+
private static bool HasConsistentPrivateKey(ref RSAParameters parameters)
234+
{
221235
if (parameters.D == null)
222236
{
223237
if (parameters.P != null ||
@@ -226,7 +240,7 @@ private static void ValidateParameters(ref RSAParameters parameters)
226240
parameters.DQ != null ||
227241
parameters.InverseQ != null)
228242
{
229-
throw new CryptographicException(SR.Argument_InvalidValue);
243+
return false;
230244
}
231245
}
232246
else
@@ -237,9 +251,11 @@ private static void ValidateParameters(ref RSAParameters parameters)
237251
parameters.DQ == null ||
238252
parameters.InverseQ == null)
239253
{
240-
throw new CryptographicException(SR.Argument_InvalidValue);
254+
return false;
241255
}
242256
}
257+
258+
return true;
243259
}
244260

245261
private static void CheckInvalidKey(SafeRsaHandle key)

src/System.Security.Cryptography.RSA/src/System/Security/Cryptography/RSACryptoServiceProvider.Unix.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ public sealed partial class RSACryptoServiceProvider : RSA, ICspAsymmetricAlgori
1717
public RSACryptoServiceProvider()
1818
{
1919
_defer = new RSAOpenSsl();
20+
_legalKeySizesValue = _defer.LegalKeySizes;
2021
}
2122

2223
public RSACryptoServiceProvider(int dwKeySize)
2324
{
2425
_defer = new RSAOpenSsl(dwKeySize);
26+
_legalKeySizesValue = _defer.LegalKeySizes;
2527
}
2628

2729
public RSACryptoServiceProvider(int dwKeySize, CspParameters parameters)

src/System.Security.Cryptography.RSA/tests/ImportExport.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ public static void MultiExport()
155155
}
156156

157157
[Fact]
158-
[ActiveIssue(1984, PlatformID.AnyUnix)]
159158
public static void PublicOnlyPrivateExport()
160159
{
161160
RSAParameters imported = new RSAParameters

src/System.Security.Cryptography.RSA/tests/KeyGeneration.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ namespace System.Security.Cryptography.Rsa.Tests
88
public class KeyGeneration
99
{
1010
[Fact]
11-
[ActiveIssue(1984, PlatformID.AnyUnix)]
1211
public static void GenerateMinKey()
1312
{
1413
GenerateKey(rsa => GetMin(rsa.LegalKeySizes));
1514
}
1615

1716
[Fact]
18-
[ActiveIssue(1984, PlatformID.AnyUnix)]
1917
public static void GenerateSecondMinKey()
2018
{
2119
GenerateKey(rsa => GetSecondMin(rsa.LegalKeySizes));

0 commit comments

Comments
 (0)