@@ -196,6 +196,54 @@ private void testStrictPKCS1Length(RSAKeyParameters pubParameters, RSAKeyParamet
196196 System .getProperties ().remove (PKCS1Encoding .NOT_STRICT_LENGTH_ENABLED_PROPERTY );
197197 }
198198
199+ private void testUnsafeModulusAndWrongExp ()
200+ {
201+ try
202+ {
203+ try
204+ {
205+ new RSAKeyParameters (false , mod , pubExp .multiply (BigInteger .valueOf (2 )));
206+
207+ fail ("no exception" );
208+ }
209+ catch (IllegalArgumentException e )
210+ {
211+ isTrue ("RSA publicExponent is even" .equals (e .getMessage ()));
212+ }
213+
214+ try
215+ {
216+ new RSAKeyParameters (false , mod .multiply (BigInteger .valueOf (2 )), pubExp );
217+
218+ fail ("no exception" );
219+ }
220+ catch (IllegalArgumentException e )
221+ {
222+ isTrue ("RSA modulus is even" .equals (e .getMessage ()));
223+ }
224+
225+ try
226+ {
227+ new RSAKeyParameters (false , mod .multiply (BigInteger .valueOf (3 )), pubExp );
228+
229+ fail ("no exception" );
230+ }
231+ catch (IllegalArgumentException e )
232+ {
233+ isTrue ("RSA modulus has a small prime factor" .equals (e .getMessage ()));
234+ }
235+
236+ System .setProperty ("org.bouncycastle.rsa.allow_unsafe_mod" , "true" );
237+
238+ // this should now work (sigh...)
239+ new RSAKeyParameters (false , mod .multiply (BigInteger .valueOf (3 )), pubExp );
240+ }
241+ finally
242+ {
243+ System .clearProperty ("org.bouncycastle.rsa.allow_unsafe_mod" );
244+ }
245+ }
246+
199247 private void testTruncatedPKCS1Block (RSAKeyParameters pubParameters , RSAKeyParameters privParameters )
200248 {
201249 checkForPKCS1Exception (pubParameters , privParameters , truncatedDataBlock , "block incorrect" );
@@ -649,6 +697,7 @@ public void performTest()
649697 testTruncatedPKCS1Block (pubParameters , privParameters );
650698 testWrongPaddingPKCS1Block (pubParameters , privParameters );
651699 test_CVE_2017_15361 ();
700+ testUnsafeModulusAndWrongExp ();
652701
653702 try
654703 {
0 commit comments