Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.text.ParseException;
import java.util.Collections;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -84,11 +85,14 @@ public static JWTClaimsSet verify(String jwToken, JWKSet jwkSet) {
String kid = header.getKeyID();
JWK jwKey = jwkSet.getKeys().stream().filter(e -> kid.equals(e.getKeyID())).findFirst().orElseThrow();
UaaMacSigner internal = new UaaMacSigner((String) jwKey.toJSONObject().get(JWKParameterNames.OCT_KEY_VALUE));
// symmetric signature check: create internal signature and compare if matches the signature from token
if (token.getSignature().equals(internal.sign(header, token.getSigningInput())) && SUPPORTED_ALGORITHMS.contains(header.getAlgorithm())) {
return token.getJWTClaimsSet();
// symmetric signature check: create internal signature and compare if matches the signature from token, using a constant-time comparison
if (SUPPORTED_ALGORITHMS.contains(header.getAlgorithm())) {
Base64URL expectedSignature = internal.sign(header, token.getSigningInput());
if (MessageDigest.isEqual(token.getSignature().decode(), expectedSignature.decode())) {
return token.getJWTClaimsSet();
}
}
Comment thread
duanemay marked this conversation as resolved.
throw new InvalidSignatureException("HMAC not mached");
throw new InvalidSignatureException("HMAC not matched");
} catch (ParseException | JOSEException e) {
throw new InvalidSignatureException("Invalid signed token", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import java.util.Arrays;
import java.security.MessageDigest;
import java.util.function.Supplier;
Comment thread
Copilot marked this conversation as resolved.

/**
Expand Down Expand Up @@ -117,7 +117,7 @@ public DirContextOperations localCompareAuthenticate(DirContextOperations user,
} else {
String encodedPassword = passwordEncoder.encode(password);
byte[] passwordBytes = Utf8.encode(encodedPassword);
match = Arrays.equals(passwordBytes, valBytes);
match = MessageDigest.isEqual(passwordBytes, valBytes);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -658,13 +659,20 @@ protected <T extends AbstractExternalOAuthIdentityProviderDefinition<T>> Map<Str
final var algorithm = Optional.ofNullable(jsonData)
.map(it -> it.get("algorithm"))
.orElse(null);
if (algorithm != null && !"HMAC-SHA256".equals(algorithm)) {
log.debug("Unknown algorithm was used to sign request! No claims returned.");
if (!"HMAC-SHA256".equals(algorithm)) {
log.debug("Missing or unknown algorithm was used to sign request! No claims returned.");
return null;
}
//check if data is signed correctly
if (!hmacSignAndEncode(signedRequests[1], secret).equals(signature)) {
log.debug("Signature is not correct, possibly the data was tampered with! No claims returned.");
// check if data is signed correctly using constant-time comparison
Comment on lines +662 to +666
try {
byte[] expectedMac = Base64.decodeBase64(hmacSignAndEncode(signedRequests[1], secret));
byte[] suppliedMac = Base64.decodeBase64(signature);
if (!MessageDigest.isEqual(expectedMac, suppliedMac)) {
log.debug("Signature is not correct, possibly the data was tampered with! No claims returned.");
return null;
}
} catch (Exception _) {
log.debug("Failed to validate signature due to encoding or computation error! No claims returned.");
return null;
}
Comment thread
duanemay marked this conversation as resolved.
return jsonData;
Expand Down
Loading