Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a config to prevent thumbprint converting to hex before encoding #2331

Merged
merged 6 commits into from
Jan 22, 2024
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 @@ -19,6 +19,7 @@

import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.jwk.JWK;
import com.nimbusds.jose.jwk.KeyUse;
import com.nimbusds.jose.jwk.RSAKey;
import com.nimbusds.jose.util.Base64;
Expand Down Expand Up @@ -68,6 +69,8 @@ public class JwksEndpoint {
private static final String SECURITY_KEY_STORE_PW = "Security.KeyStore.Password";
private static final String KEYS = "keys";
private static final String ADD_PREVIOUS_VERSION_KID = "JWTValidatorConfigs.JWKSEndpoint.AddPreviousVersionKID";
public static final String JWKS_IS_THUMBPRINT_HEXIFY_REQUIRED = "JWTValidatorConfigs.JWKSEndpoint" +
".IsThumbprintHexifyRequired";

@GET
@Path(value = "/jwks")
Expand Down Expand Up @@ -168,7 +171,12 @@ private RSAKey.Builder getJWK(JWSAlgorithm algorithm, List<Base64> encodedCertLi
jwk.algorithm(algorithm);
jwk.keyUse(KeyUse.parse(KEY_USE));
jwk.x509CertChain(encodedCertList);
jwk.x509CertSHA256Thumbprint(new Base64URL(OAuth2Util.getThumbPrint(certificate, alias)));
if (!Boolean.parseBoolean(IdentityUtil.getProperty(JWKS_IS_THUMBPRINT_HEXIFY_REQUIRED))) {
JWK parsedJWK = JWK.parse(certificate);
jwk.x509CertSHA256Thumbprint(parsedJWK.getX509CertSHA256Thumbprint());
} else {
jwk.x509CertSHA256Thumbprint(new Base64URL(OAuth2Util.getThumbPrint(certificate, alias)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will produce double encoding right? Wihtin the getThumbprint method aren't we encode?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Base64URL is just taking the string value and create a Base64URL instance where it doesn't encode it again. hence this won't be a problem

}
return jwk;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.wso2.carbon.identity.oauth.endpoint.jwks;

import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.util.Base64URL;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -51,6 +50,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -84,10 +84,7 @@ public class JwksEndpointTest extends PowerMockIdentityBaseTest {
private static final String ALG = "RS256";
private static final String USE = "sig";
private static final JSONArray X5C_ARRAY = new JSONArray();
private static final String X5T = "YmUwN2EzOGI3ZTI0Y2NiNTNmZWFlZjI5MmVjZjdjZTYzZjI0M2MxNDQ1YjQwNjI3NjY" +
"yZmZlYzkwNzY0YjU4NQ";
private static final String rsa256Thumbprint = "be:07:a3:8b:7e:24:cc:b5:3f:ea:ef:29:2e:cf:7c:e6:3f:24:3c:" +
"14:45:b4:06:27:66:2f:fe:c9:07:64:b5:85";
private static final JSONArray X5T_ARRAY = new JSONArray();
private JwksEndpoint jwksEndpoint;
private Object identityUtilObj;

Expand Down Expand Up @@ -128,6 +125,8 @@ public void setUp() throws Exception {
"EJCSfsvswtLVDZ7GDvTHKojJjQvdVCzRj6XH5Truwefb4BJz9APtnlyJIvjHk1hdozqyOniVZd0QOxLAbcdt946chNdQvCm6aUOp" +
"utp8Xogr0KBnEy3U8es2cAfNZaEkPU8Va5bU6Xjny8zGQnXCXxPKp7sMpgO93nPBt/liX1qfyXM7xEotWoxmm6HZx8oWQ8U5aiXj" +
"Z5RKDWCCq4ZuXl6wVsUz1iE61suO5yWi8=");
X5T_ARRAY.put("vgeji34kzLU_6u8pLs985j8kPBRFtAYnZi_-yQdktYU");
X5T_ARRAY.put("UPDtpYmK86EVwsUIGUlW5-EU_iNHQ-nSL3Ca58uAG70");
}

@DataProvider(name = "provideTenantDomain")
Expand Down Expand Up @@ -216,16 +215,18 @@ protected Map<String, Object> initialValue() {
assertEquals(keyObject.get("alg"), ALG, "Incorrect alg value");
assertEquals(keyObject.get("use"), USE, "Incorrect use value");
assertEquals(keyObject.get("kty"), "RSA", "Incorrect kty value");
assertEquals(keyObject.get("x5t#S256"),
Base64URL.encode(rsa256Thumbprint.replaceAll(":", "")).toString());
assertEquals(keyObject.get("x5t#S256"), X5T, "Incorrect x5t#S256 value");
if ("foo.com".equals(tenantDomain)) {
assertEquals(objectArray.length(), 2, "Incorrect no of keysets");
assertEquals(((JSONArray) keyObject.get("x5c")).get(0), X5C_ARRAY.get(0), "Incorrect x5c value");
assertEquals(keyObject.get("x5t#S256"), X5T_ARRAY.get(0), "Incorrect x5t#S256 value");
} else {
assertEquals(objectArray.length(), 3, "Incorrect no of keysets");
assertEquals(((JSONArray) keyObject.get("x5c")).get(0), X5C_ARRAY.get(1), "Incorrect x5c value");
assertEquals(keyObject.get("x5t#S256"), X5T_ARRAY.get(1), "Incorrect x5t#S256 value");
}
String base64UrlEncodedString = (String) keyObject.get("x5t#S256");
byte[] decodedBytes = Base64.getUrlDecoder().decode(base64UrlEncodedString);
assertEquals(decodedBytes.length, 32, "Incorrect x5t#S256 size");
} catch (JSONException e) {
if ("invalid.com".equals(tenantDomain)) {
// This is expected. We don't validate for invalid tenants.
Expand Down
Loading