Merge pull request #69 from jwtk/ISSUE-68

Issue 68
This commit is contained in:
Les Hazlewood 2015-11-21 15:23:44 -08:00
commit d1058b0933
2 changed files with 62 additions and 7 deletions

View File

@ -22,15 +22,17 @@ import java.security.InvalidKeyException;
import java.security.Key;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAKey;
public class RsaSigner extends RsaProvider implements Signer {
public RsaSigner(SignatureAlgorithm alg, Key key) {
super(alg, key);
if (!(key instanceof RSAPrivateKey)) {
String msg = "RSA signatures must be computed using an RSAPrivateKey. The specified key of type " +
key.getClass().getName() + " is not an RSAPrivateKey.";
// https://github.com/jwtk/jjwt/issues/68
// Instead of checking for an instance of RSAPrivateKey, check for PrivateKey and RSAKey:
if (!(key instanceof PrivateKey && key instanceof RSAKey)) {
String msg = "RSA signatures must be computed using an RSA PrivateKey. The specified key of type " +
key.getClass().getName() + " is not an RSA PrivateKey.";
throw new IllegalArgumentException(msg);
}
}

View File

@ -22,6 +22,7 @@ import javax.crypto.spec.SecretKeySpec
import java.security.InvalidKeyException
import java.security.KeyPair
import java.security.KeyPairGenerator
import java.security.MessageDigest
import java.security.PrivateKey
import java.security.PublicKey
@ -48,18 +49,50 @@ class RsaSignerTest {
}
@Test
void testConstructorWithoutRsaPrivateKey() {
void testConstructorWithoutPrivateKey() {
byte[] bytes = new byte[16]
rng.nextBytes(bytes)
SecretKeySpec key = new SecretKeySpec(bytes, 'HmacSHA256')
try {
//noinspection GroovyResultOfObjectAllocationIgnored
new RsaSigner(SignatureAlgorithm.RS256, key);
fail('RsaSigner should reject non RSAPrivateKey instances.')
} catch (IllegalArgumentException expected) {
assertEquals expected.message, "RSA signatures must be computed using an RSAPrivateKey. The specified key of type " +
key.getClass().getName() + " is not an RSAPrivateKey.";
assertEquals expected.message, "RSA signatures must be computed using an RSA PrivateKey. The specified key of type " +
key.getClass().getName() + " is not an RSA PrivateKey.";
}
}
@Test
void testConstructorWithoutRSAKey() {
//private key, but not an RSAKey instance:
PrivateKey key = new PrivateKey() {
@Override
String getAlgorithm() {
return null
}
@Override
String getFormat() {
return null
}
@Override
byte[] getEncoded() {
return new byte[0]
}
}
try {
//noinspection GroovyResultOfObjectAllocationIgnored
new RsaSigner(SignatureAlgorithm.RS256, key);
fail('RsaSigner should reject non RSAPrivateKey instances.')
} catch (IllegalArgumentException expected) {
assertEquals expected.message, "RSA signatures must be computed using an RSA PrivateKey. The specified key of type " +
key.getClass().getName() + " is not an RSA PrivateKey.";
}
}
@ -126,4 +159,24 @@ class RsaSignerTest {
assertSame se.cause, ex
}
}
@Test
void testSignSuccessful() {
KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("RSA");
keyGenerator.initialize(1024);
KeyPair kp = keyGenerator.genKeyPair();
PrivateKey privateKey = kp.getPrivate();
byte[] bytes = new byte[16]
rng.nextBytes(bytes)
RsaSigner signer = new RsaSigner(SignatureAlgorithm.RS256, privateKey);
byte[] out1 = signer.sign(bytes)
byte[] out2 = signer.sign(bytes)
assertTrue(MessageDigest.isEqual(out1, out2))
}
}