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

Commit 73cbbdd

Browse files
committed
Fix SegFault in RSAOpenSsl::ImportParameters caused by calling RSA_size when no Modulus was provided.
In addition to ensuring that Modulus is present, complete verification of the RSAParameters input object to pass the rest of the invalid input tests. Even though OpenSSL's default RSA engine can process private key operations with only D, for compatibility on Windows D requires the rest of { P, DP, Q, DQ, InverseQ }. Additionally, none of the "extra" private key parameters can be present without D.
1 parent d802ffc commit 73cbbdd

File tree

1 file changed

+31
-0
lines changed
  • src/System.Security.Cryptography.RSA/src/Internal/Cryptography

1 file changed

+31
-0
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ public override RSAParameters ExportParameters(bool includePrivateParameters)
145145

146146
public override unsafe void ImportParameters(RSAParameters parameters)
147147
{
148+
ValidateParameters(ref parameters);
149+
148150
SafeRsaHandle key = Interop.libcrypto.RSA_new();
149151
bool imported = false;
150152

@@ -209,6 +211,35 @@ private void FreeKey()
209211
}
210212
}
211213

214+
private static void ValidateParameters(ref RSAParameters parameters)
215+
{
216+
if (parameters.Modulus == null || parameters.Exponent == null)
217+
throw new CryptographicException(SR.Argument_InvalidValue);
218+
219+
if (parameters.D == null)
220+
{
221+
if (parameters.P != null ||
222+
parameters.DP != null ||
223+
parameters.Q != null ||
224+
parameters.DQ != null ||
225+
parameters.InverseQ != null)
226+
{
227+
throw new CryptographicException(SR.Argument_InvalidValue);
228+
}
229+
}
230+
else
231+
{
232+
if (parameters.P == null ||
233+
parameters.DP == null ||
234+
parameters.Q == null ||
235+
parameters.DQ == null ||
236+
parameters.InverseQ == null)
237+
{
238+
throw new CryptographicException(SR.Argument_InvalidValue);
239+
}
240+
}
241+
}
242+
212243
private static void CheckInvalidKey(SafeRsaHandle key)
213244
{
214245
if (key == null || key.IsInvalid)

0 commit comments

Comments
 (0)