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

Commit e80c82a

Browse files
committed
Factor out HasConsistentPrivateKey, move check out of interop.
Moving the consistency check from the Interop type to the consumer removes the dependency on an SR value in all callers.
1 parent feed8e4 commit e80c82a

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/Common/src/Interop/Unix/libcrypto/Interop.Rsa.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,6 @@ internal static unsafe RSAParameters ExportRsaParameters(SafeRsaHandle key, bool
8383
rsaParameters.Q = ExtractBignum(rsaStructure->q, halfModulus);
8484
rsaParameters.DQ = ExtractBignum(rsaStructure->dmq1, halfModulus);
8585
rsaParameters.InverseQ = ExtractBignum(rsaStructure->iqmp, halfModulus);
86-
87-
if (rsaParameters.D == null ||
88-
rsaParameters.P == null ||
89-
rsaParameters.DP == null ||
90-
rsaParameters.Q == null ||
91-
rsaParameters.DQ == null ||
92-
rsaParameters.InverseQ == null)
93-
{
94-
throw new CryptographicException(SR.Cryptography_CSP_NoPrivateKey);
95-
}
9686
}
9787
}
9888
finally

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)

0 commit comments

Comments
 (0)