Skip to content

Commit 29bcca1

Browse files
committed
github bcgit#449 added unsafe override for poor key use.
1 parent bd8572a commit 29bcca1

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

core/src/main/java/org/bouncycastle/crypto/params/RSAKeyParameters.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.math.BigInteger;
44

5+
import org.bouncycastle.util.Properties;
6+
57
public class RSAKeyParameters
68
extends AsymmetricKeyParameter
79
{
@@ -39,6 +41,13 @@ public RSAKeyParameters(
3941

4042
private BigInteger validate(BigInteger modulus)
4143
{
44+
// If you need to set this you need to have a serious word to whoever is generating
45+
// your keys.
46+
if (Properties.isOverrideSet("org.bouncycastle.rsa.allow_unsafe_mod"))
47+
{
48+
return modulus;
49+
}
50+
4251
if ((modulus.intValue() & 1) == 0)
4352
{
4453
throw new IllegalArgumentException("RSA modulus is even");

core/src/test/java/org/bouncycastle/crypto/test/RSATest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)