forked from keycloak/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement advanced verification of SD-JWT in Keycloak (keycloak#30966)
closes keycloak#30907 Signed-off-by: Ingrid Kamga <[email protected]>
- Loading branch information
1 parent
4080ee2
commit 36a1410
Showing
44 changed files
with
2,620 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
core/src/main/java/org/keycloak/sdjwt/IssuerSignedJwtVerificationOpts.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright 2024 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.keycloak.sdjwt; | ||
|
||
import org.keycloak.crypto.SignatureVerifierContext; | ||
|
||
/** | ||
* Options for Issuer-signed JWT verification. | ||
* | ||
* @author <a href="mailto:[email protected]">Ingrid Kamga</a> | ||
*/ | ||
public class IssuerSignedJwtVerificationOpts { | ||
private final SignatureVerifierContext verifier; | ||
private final boolean validateIssuedAtClaim; | ||
private final boolean validateExpirationClaim; | ||
private final boolean validateNotBeforeClaim; | ||
|
||
public IssuerSignedJwtVerificationOpts( | ||
SignatureVerifierContext verifier, | ||
boolean validateIssuedAtClaim, | ||
boolean validateExpirationClaim, | ||
boolean validateNotBeforeClaim) { | ||
this.verifier = verifier; | ||
this.validateIssuedAtClaim = validateIssuedAtClaim; | ||
this.validateExpirationClaim = validateExpirationClaim; | ||
this.validateNotBeforeClaim = validateNotBeforeClaim; | ||
} | ||
|
||
public SignatureVerifierContext getVerifier() { | ||
return verifier; | ||
} | ||
|
||
public boolean mustValidateIssuedAtClaim() { | ||
return validateIssuedAtClaim; | ||
} | ||
|
||
public boolean mustValidateExpirationClaim() { | ||
return validateExpirationClaim; | ||
} | ||
|
||
public boolean mustValidateNotBeforeClaim() { | ||
return validateNotBeforeClaim; | ||
} | ||
|
||
public static IssuerSignedJwtVerificationOpts.Builder builder() { | ||
return new IssuerSignedJwtVerificationOpts.Builder(); | ||
} | ||
|
||
public static class Builder { | ||
private SignatureVerifierContext verifier; | ||
private boolean validateIssuedAtClaim; | ||
private boolean validateExpirationClaim = true; | ||
private boolean validateNotBeforeClaim = true; | ||
|
||
public Builder withVerifier(SignatureVerifierContext verifier) { | ||
this.verifier = verifier; | ||
return this; | ||
} | ||
|
||
public Builder withValidateIssuedAtClaim(boolean validateIssuedAtClaim) { | ||
this.validateIssuedAtClaim = validateIssuedAtClaim; | ||
return this; | ||
} | ||
|
||
public Builder withValidateExpirationClaim(boolean validateExpirationClaim) { | ||
this.validateExpirationClaim = validateExpirationClaim; | ||
return this; | ||
} | ||
|
||
public Builder withValidateNotBeforeClaim(boolean validateNotBeforeClaim) { | ||
this.validateNotBeforeClaim = validateNotBeforeClaim; | ||
return this; | ||
} | ||
|
||
public IssuerSignedJwtVerificationOpts build() { | ||
return new IssuerSignedJwtVerificationOpts( | ||
verifier, | ||
validateIssuedAtClaim, | ||
validateExpirationClaim, | ||
validateNotBeforeClaim | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,24 +17,27 @@ | |
package org.keycloak.sdjwt; | ||
|
||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.keycloak.common.VerificationException; | ||
import org.keycloak.crypto.SignatureSignerContext; | ||
import org.keycloak.crypto.SignatureVerifierContext; | ||
import org.keycloak.jose.jws.JWSBuilder; | ||
import org.keycloak.jose.jws.JWSHeader; | ||
import org.keycloak.jose.jws.JWSInput; | ||
import org.keycloak.jose.jws.JWSInputException; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
/** | ||
* Handle jws, either the issuer jwt or the holder key binding jwt. | ||
* | ||
* | ||
* @author <a href="mailto:[email protected]">Francis Pouatcha</a> | ||
* | ||
* | ||
*/ | ||
public class SdJws { | ||
public abstract class SdJws { | ||
private final JWSInput jwsInput; | ||
private final JsonNode payload; | ||
|
||
|
@@ -49,10 +52,6 @@ public JsonNode getPayload() { | |
return payload; | ||
} | ||
|
||
public String getJwsString() { | ||
return jwsInput.getWireString(); | ||
} | ||
|
||
// Constructor for unsigned JWS | ||
protected SdJws(JsonNode payload) { | ||
this.payload = payload; | ||
|
@@ -107,4 +106,75 @@ private static final JsonNode readPayload(JWSInput jwsInput) { | |
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public JWSHeader getHeader() { | ||
return this.jwsInput.getHeader(); | ||
} | ||
|
||
public void verifyIssuedAtClaim() throws VerificationException { | ||
long now = Instant.now().getEpochSecond(); | ||
long iat = SdJwtUtils.readTimeClaim(payload, "iat"); | ||
|
||
if (now < iat) { | ||
throw new VerificationException("jwt issued in the future"); | ||
} | ||
} | ||
|
||
public void verifyExpClaim() throws VerificationException { | ||
long now = Instant.now().getEpochSecond(); | ||
long exp = SdJwtUtils.readTimeClaim(payload, "exp"); | ||
|
||
if (now >= exp) { | ||
throw new VerificationException("jwt has expired"); | ||
} | ||
} | ||
|
||
public void verifyNotBeforeClaim() throws VerificationException { | ||
long now = Instant.now().getEpochSecond(); | ||
long nbf = SdJwtUtils.readTimeClaim(payload, "nbf"); | ||
|
||
if (now < nbf) { | ||
throw new VerificationException("jwt not valid yet"); | ||
} | ||
} | ||
|
||
/** | ||
* Verifies that the JWS is not too old. | ||
* | ||
* @param maxAge Maximum age in seconds | ||
* @throws VerificationException if too old | ||
*/ | ||
public void verifyAge(int maxAge) throws VerificationException { | ||
long now = Instant.now().getEpochSecond(); | ||
long iat = SdJwtUtils.readTimeClaim(getPayload(), "iat"); | ||
|
||
if (now - iat > maxAge) { | ||
throw new VerificationException("jwt is too old"); | ||
} | ||
} | ||
|
||
/** | ||
* Verifies that SD-JWT was issued by one of the provided issuers. | ||
* @param issuers List of trusted issuers | ||
*/ | ||
public void verifyIssClaim(List<String> issuers) throws VerificationException { | ||
verifyClaimAgainstTrustedValues(issuers, "iss"); | ||
} | ||
|
||
/** | ||
* Verifies that SD-JWT vct claim matches the expected one. | ||
* @param vcts list of supported verifiable credential types | ||
*/ | ||
public void verifyVctClaim(List<String> vcts) throws VerificationException { | ||
verifyClaimAgainstTrustedValues(vcts, "vct"); | ||
} | ||
|
||
private void verifyClaimAgainstTrustedValues(List<String> trustedValues, String claimName) | ||
throws VerificationException { | ||
String claimValue = SdJwtUtils.readClaim(payload, claimName); | ||
|
||
if (!trustedValues.contains(claimValue)) { | ||
throw new VerificationException(String.format("Unknown '%s' claim value: %s", claimName, claimValue)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.