Skip to content

Commit 6851635

Browse files
committed
JCE: add RSA exponent sanitization in KeyPairGenerator
1 parent 528fa49 commit 6851635

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/main/java/com/wolfssl/provider/jce/WolfCryptKeyPairGenerator.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,13 @@ public synchronized void initialize(AlgorithmParameterSpec params,
156156
RSAKeyGenParameterSpec rsaSpec = (RSAKeyGenParameterSpec)params;
157157
this.keysize = rsaSpec.getKeysize();
158158

159-
this.publicExponent =
160-
rsaSpec.getPublicExponent().longValue();
159+
/* Exponent should be larger than 1 and odd */
160+
long exp = rsaSpec.getPublicExponent().longValue();
161+
if ((exp <= 1) || (exp % 2 == 0)) {
162+
throw new InvalidAlgorithmParameterException(
163+
"RSA public exponent must be positive and odd" );
164+
}
165+
this.publicExponent = exp;
161166

162167
/* Double check longValue() converted correctly. Some platforms
163168
* do not have longValueExact() */

src/test/java/com/wolfssl/provider/jce/test/WolfCryptKeyPairGeneratorTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,5 +632,59 @@ public void testKeyPairGeneratorRsassaPssKeyGeneration()
632632
}
633633
}
634634
}
635+
636+
@Test
637+
public void testKeyPairGenerationInvalidExponent()
638+
throws NoSuchProviderException, NoSuchAlgorithmException,
639+
InvalidAlgorithmParameterException {
640+
641+
if (testedRSAKeySizes.size() > 0) {
642+
643+
KeyPairGenerator kpg =
644+
KeyPairGenerator.getInstance("RSA", "wolfJCE");
645+
646+
/* Negative exponent */
647+
try {
648+
RSAKeyGenParameterSpec rsaSpec =
649+
new RSAKeyGenParameterSpec(testedRSAKeySizes.get(0),
650+
BigInteger.valueOf(-1));
651+
kpg.initialize(rsaSpec);
652+
fail("KeyPairGenerator.initialize() should throw " +
653+
"InvalidAlgorithmParameterException when given " +
654+
"invalid negative RSA public exponent");
655+
656+
} catch (InvalidAlgorithmParameterException e) {
657+
/* expected */
658+
}
659+
660+
/* Zero exponent */
661+
try {
662+
RSAKeyGenParameterSpec rsaSpec =
663+
new RSAKeyGenParameterSpec(testedRSAKeySizes.get(0),
664+
BigInteger.valueOf(0));
665+
kpg.initialize(rsaSpec);
666+
fail("KeyPairGenerator.initialize() should throw " +
667+
"InvalidAlgorithmParameterException when given " +
668+
"invalid RSA public exponent of zero");
669+
670+
} catch (InvalidAlgorithmParameterException e) {
671+
/* expected */
672+
}
673+
674+
/* Even exponent */
675+
try {
676+
RSAKeyGenParameterSpec rsaSpec =
677+
new RSAKeyGenParameterSpec(testedRSAKeySizes.get(0),
678+
BigInteger.valueOf(4));
679+
kpg.initialize(rsaSpec);
680+
fail("KeyPairGenerator.initialize() should throw " +
681+
"InvalidAlgorithmParameterException when given " +
682+
"invalid even RSA public exponent");
683+
684+
} catch (InvalidAlgorithmParameterException e) {
685+
/* expected */
686+
}
687+
}
688+
}
635689
}
636690

0 commit comments

Comments
 (0)