8000 org.keycloak.sdjwt 26.8.2: a JWS crit header naming an unknown extension is accepted by abstractj · Pull Request #55523 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Open
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
8 changes: 6 additions & 2 deletions core/src/main/java/org/keycloak/sdjwt/JwsToken.java
8000
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,19 @@ public String sign(SignatureSignerContext signerContext) {

public void verifySignature(SignatureVerifierContext verifier) throws VerificationException {
Objects.requireNonNull(verifier, "verifier must not be null");
String headerAlgorithm = jwsHeader == null || jwsHeader.getAlgorithm() == null
JWSHeader protectedHeader = jwsInput == null ? null : jwsInput.getHeader();
String headerAlgorithm = protectedHeader == null || protectedHeader.getAlgorithm() == null
? null
: jwsHeader.getRawAlgorithm();
: protectedHeader.getRawAlgorithm();
String verifierAlgorithm = verifier.getAlgorithm();
if (headerAlgorithm == null || verifierAlgorithm == null || !headerAlgorithm.equals(verifierAlgorithm)) {
throw new VerificationException(String.format(
"JWS header algorithm '%s' does not match verifier algorithm '%s'",
headerAlgorithm, verifierAlgorithm));
}
if (protectedHeader.getOtherClaims().containsKey("crit")) {
throw new VerificationException("Critical JWS header parameters are not supported");
}
try {
if (!verifier.verify(jwsInput.getEncodedSignatureInput().getBytes(StandardCharsets.UTF_8),
jwsInput.getSignature())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ public void testSdJwtVerification_FlatSdJwt() throws VerificationException {
}
}

@Test
public void issuerHeaderExtensionIsAcceptedOnlyWhenNonCritical() throws VerificationException {
SignatureSignerContext signer = testSettings.issuerSigContext;
ObjectNode header = mapper.createObjectNode().put("alg", signer.getAlgorithm()).put("example", true);
ObjectNode payload = exampleFlatSdJwtV1().build().getPayload();
IssuerSignedJwtVerificationOpts options = optionalTimeClaimVerificationOpts().build();

String signed = TestUtils.signWithProtectedHeader(header, payload, signer);
new SdJwt(new IssuerSignedJWT(signed), null).verify(defaultIssuerVerifyingKeys(), options);

header.putArray("crit").add("example");
String signedWithCrit = TestUtils.signWithProtectedHeader(header, payload, signer);
SdJwt sdJwt = new SdJwt(new IssuerSignedJWT(signedWithCrit), null);
assertThrows(VerificationException.class, () -> sdJwt.verify(defaultIssuerVerifyingKeys(), options));
}

@Test
public void sdJwtVerificationShouldFail_WhenHeaderAlgorithmDiffersFromVerifierAlgorithm() {
SdJwt sdJwt = SdJwt.builder()
Expand Down
10 changes: 10 additions & 0 deletions core/src/test/java/org/keycloak/sdjwt/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

import org.keycloak.common.util.Base64Url;
import org.keycloak.crypto.SignatureSignerContext;

import com.fasterxml.jackson.databind.node.ObjectNode;

Expand Down Expand Up @@ -79,4 +81,12 @@ public static String removeAlgorithmFromJwsHeader(String jws) {
}
}

// JWSBuilder only serializes the standard header fields.
public static String signWithProtectedHeader(ObjectNode header, ObjectNode payload, SignatureSignerContext signer) {
String encodedHeader = Base64Url.encode(header.toString().getBytes(StandardCharsets.UTF_8));
String encodedPayload = Base64Url.encode(payload.toString().getBytes(StandardCharsets.UTF_8));
String signingInput = encodedHeader + "." + encodedPayload;
return signingInput + "." + Base64Url.encode(signer.sign(signingInput.getBytes(StandardCharsets.US_ASCII)));
}

}
8000
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,24 @@ public void testShouldFail_IfKeyBindingJwtSignatureInvalid() {
);
}

@Test
public void keyBindingJwtWithUnsupportedCriticalHeaderIsRejected() {
SignatureSignerContext signer = testSettings.holderSigContext;
ObjectNode header = mapper.createObjectNode()
.put("alg", signer.getAlgorithm())
.put("typ", OID4VCConstants.KEYBINDING_JWT_TYP)
.put("example", true);
header.putArray("crit").add("example");
String keyBindingJwt = TestUtils.signWithProtectedHeader(header, exampleKbPayload(), signer);
SdJwtVP presentation = exampleSdJwtWithKeyBindingJwt(keyBindingJwt);

VerificationException exception = assertThrows(VerificationException.class, () -> presentation.verify(
defaultIssuerVerifyingKeys(),
defaultIssuerSignedJwtVerificationOpts().build(),
defaultKeyBindingJwtVerificationOpts().build()));
assertEquals("Critical JWS header parameters are not supported", exception.getCause().getMessage());
}

@Test
public void testShouldFail_IfKeyBindingHeaderAlgorithmDiffersFromVerifierAlgorithm() {
SignatureSignerContext signer = TestSettings.signerWithReportedAlgorithm(
Expand Down Expand Up @@ -570,10 +588,13 @@ private SdJwtVP exampleSdJwtWithCustomKbPayload(ObjectNode kbPayloadSubstitute,
.withPayload(kbPayloadSubstitute)
.withSignerContext(signerContext)
.build();
return exampleSdJwtWithKeyBindingJwt(keyBindingJWT.getJws());
}

private SdJwtVP exampleSdJwtWithKeyBindingJwt(String keyBindingJwt) {
String sdJwtVPString = TestUtils.readFileAsString(getClass(), "sdjwt/s20.1-sdjwt+kb.txt");
String sdJwtWithoutKb = sdJwtVPString.substring(0, sdJwtVPString.lastIndexOf(OID4VCConstants.SDJWT_DELIMITER) + 1);

return SdJwtVP.of(sdJwtWithoutKb + keyBindingJWT.getJws());
return SdJwtVP.of(sdJwtWithoutKb + keyBindingJwt);
}
}
12 changes: 12 additions & 0 deletions docs/documentation/upgrading/topics/changes/changes-27_0_0.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
Breaking changes are identified as those that might require changes for existing users to their configurations or applications.
In minor or patch releases, {project_name} will only introduce breaking changes to fix bugs.

=== SD-JWT verification rejects critical JOSE header extensions
// https://github.com/keycloak/keycloak/issues/55380

The SD-JWT verifier now rejects an issuer-signed JWT or a holder's key-binding JWT if its protected JOSE header contains `crit`.
Earlier versions ignored this header, so credentials and presentations that previously verified can now fail.
{project_name} currently supports no critical JOSE header extensions for SD-JWT verification.

Before upgrading, check the protected headers produced by your credential issuer and wallet.
If an extension is optional for your application, omit it from `crit` and omit the `crit` header entirely when no critical extensions remain.
Reissue affected credentials or generate a new key-binding JWT with the corrected header.
If your application relies on a critical extension, it requires a verifier that implements that extension.

=== Allow UTF-8 email setting requires SMTPUTF8 server support
// https://github.com/keycloak/keycloak/issues/55267

Expand Down
Loading
0