Skip to content

Commit 208d7b6

Browse files
committed
Improved order comparison
1 parent 3135ba7 commit 208d7b6

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

lib/src/main/java/com/auth0/jwt/algorithms/ECDSAAlgorithm.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,12 @@ void validateSignatureStructure(byte[] joseSignature, ECPublicKey publicKey) thr
191191
BigInteger order = publicKey.getParams().getOrder();
192192

193193
// R and S must be less than N
194-
if (order.compareTo(r) < 1 || order.compareTo(s) < 1) {
195-
throw new SignatureException("The difference between R or S value and order should be greater than one.");
194+
if (order.compareTo(r) < 1) {
195+
throw new SignatureException("The difference between R value and order should be greater than one.");
196+
}
197+
198+
if (order.compareTo(s) < 1){
199+
throw new SignatureException("The difference between S value and order should be greater than one.");
196200
}
197201
}
198202

lib/src/test/java/com/auth0/jwt/algorithms/ECDSAAlgorithmTest.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ public void signatureWithSZeroShouldFail() throws Exception {
14291429
}
14301430

14311431
@Test
1432-
public void signatureWithRSValueNotLessThanOrderShouldFail() throws Exception {
1432+
public void signatureWithRValueNotLessThanOrderShouldFail() throws Exception {
14331433
exception.expect(SignatureException.class);
14341434

14351435
ECPublicKey publicKey = (ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC");
@@ -1444,4 +1444,22 @@ public void signatureWithRSValueNotLessThanOrderShouldFail() throws Exception {
14441444
ECDSAAlgorithm algorithm256 = (ECDSAAlgorithm) Algorithm.ECDSA256(publicKey, privateKey);
14451445
algorithm256.validateSignatureStructure(invalidSignature, publicKey);
14461446
}
1447+
1448+
@Test
1449+
public void signatureWithSValueNotLessThanOrderShouldFail() throws Exception {
1450+
exception.expect(SignatureException.class);
1451+
1452+
ECPublicKey publicKey = (ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC");
1453+
ECPrivateKey privateKey = (ECPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_256, "EC");
1454+
1455+
String signedJwt = JWT.create().sign(Algorithm.ECDSA256(publicKey, privateKey));
1456+
String jwtWithInvalidSig = signedJwt.substring(0, signedJwt.lastIndexOf('.') + 1) + "_____wAAAAD__________7zm-q2nF56E87nKwvxjJVH_____AAAAAP__________vOb6racXnoTzucrC_GMlUQ";
1457+
1458+
String[] chunks = jwtWithInvalidSig.split("\\.");
1459+
byte[] invalidSignature = Base64.getUrlDecoder().decode(chunks[2]);
1460+
invalidSignature[0] = Byte.MAX_VALUE;
1461+
1462+
ECDSAAlgorithm algorithm256 = (ECDSAAlgorithm) Algorithm.ECDSA256(publicKey, privateKey);
1463+
algorithm256.validateSignatureStructure(invalidSignature, publicKey);
1464+
}
14471465
}

0 commit comments

Comments
 (0)