forked from pedroigor/keycloak
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KEYCLOAK-9089 IllegalArgumentException when trying to use ES256 as OI…
…DC access token signature
- Loading branch information
Showing
22 changed files
with
466 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2017 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.crypto; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Marek Posolda</a> | ||
*/ | ||
public class HashException extends RuntimeException { | ||
|
||
public HashException(String message) { | ||
super(message); | ||
} | ||
|
||
public HashException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -18,51 +18,48 @@ | |
package org.keycloak.jose.jws.crypto; | ||
|
||
import org.keycloak.common.util.Base64Url; | ||
import org.keycloak.jose.jws.Algorithm; | ||
import org.keycloak.crypto.HashException; | ||
import org.keycloak.crypto.JavaAlgorithm; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.security.MessageDigest; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Marek Posolda</a> | ||
*/ | ||
public class HashProvider { | ||
public class HashUtils { | ||
|
||
// See "at_hash" and "c_hash" in OIDC specification | ||
public static String oidcHash(String jwtAlgorithmName, String input) { | ||
byte[] digest = digest(jwtAlgorithmName, input); | ||
|
||
int hashLength = digest.length / 2; | ||
byte[] hashInput = Arrays.copyOf(digest, hashLength); | ||
try { | ||
byte[] inputBytes = input.getBytes("UTF-8"); | ||
String javaAlgName = JavaAlgorithm.getJavaAlgorithmForHash(jwtAlgorithmName); | ||
byte[] hash = hash(javaAlgName, inputBytes); | ||
|
||
return Base64Url.encode(hashInput); | ||
return encodeHashToOIDC(hash); | ||
} catch (UnsupportedEncodingException e) { | ||
throw new HashException("Error when creating token hash", e); | ||
} | ||
} | ||
private static byte[] digest(String algorithm, String input) { | ||
String digestAlg = getJavaDigestAlgorithm(algorithm); | ||
|
||
|
||
public static byte[] hash(String javaAlgorithmName, byte[] inputBytes) { | ||
try { | ||
MessageDigest md = MessageDigest.getInstance(digestAlg); | ||
md.update(input.getBytes("UTF-8")); | ||
MessageDigest md = MessageDigest.getInstance(javaAlgorithmName); | ||
md.update(inputBytes); | ||
return md.digest(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
private static String getJavaDigestAlgorithm(String alg) { | ||
switch (alg) { | ||
case "RS256": | ||
return "SHA-256"; | ||
case "RS384": | ||
return "SHA-384"; | ||
case "RS512": | ||
return "SHA-512"; | ||
default: | ||
throw new IllegalArgumentException("Not an RSA Algorithm"); | ||
throw new HashException("Error when creating token hash", e); | ||
} | ||
} | ||
|
||
// See "at_hash" and "c_hash" in OIDC specification | ||
public static String oidcHash(Algorithm jwtAlgorithm, String input) { | ||
return oidcHash(jwtAlgorithm.name(), input); | ||
|
||
public static String encodeHashToOIDC(byte[] hash) { | ||
int hashLength = hash.length / 2; | ||
byte[] hashInput = Arrays.copyOf(hash, hashLength); | ||
|
||
return Base64Url.encode(hashInput); | ||
} | ||
|
||
} |
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
47 changes: 47 additions & 0 deletions
47
server-spi-private/src/main/java/org/keycloak/crypto/HashProvider.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,47 @@ | ||
/* | ||
* Copyright 2017 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.crypto; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
|
||
import org.keycloak.provider.Provider; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Marek Posolda</a> | ||
*/ | ||
public interface HashProvider extends Provider { | ||
|
||
|
||
default byte[] hash(String input) throws HashException { | ||
try { | ||
byte[] inputBytes = input.getBytes("UTF-8"); | ||
return hash(inputBytes); | ||
} catch (UnsupportedEncodingException e) { | ||
throw new HashException("Unsupported encoding when trying to hash", e); | ||
} | ||
} | ||
|
||
|
||
byte[] hash(byte[] input) throws HashException; | ||
|
||
|
||
@Override | ||
default void close() { | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
server-spi-private/src/main/java/org/keycloak/crypto/HashProviderFactory.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,40 @@ | ||
/* | ||
* Copyright 2017 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.crypto; | ||
|
||
import org.keycloak.Config; | ||
import org.keycloak.models.KeycloakSessionFactory; | ||
import org.keycloak.provider.ProviderFactory; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Marek Posolda</a> | ||
*/ | ||
public interface HashProviderFactory extends ProviderFactory<HashProvider> { | ||
|
||
@Override | ||
default void init(Config.Scope config) { | ||
} | ||
|
||
@Override | ||
default void postInit(KeycloakSessionFactory factory) { | ||
} | ||
|
||
@Override | ||
default void close() { | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
server-spi-private/src/main/java/org/keycloak/crypto/HashSpi.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,48 @@ | ||
/* | ||
* Copyright 2017 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.crypto; | ||
|
||
import org.keycloak.provider.Provider; | ||
import org.keycloak.provider.ProviderFactory; | ||
import org.keycloak.provider.Spi; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Marek Posolda</a> | ||
*/ | ||
public class HashSpi implements Spi { | ||
|
||
@Override | ||
public boolean isInternal() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "hash"; | ||
} | ||
|
||
@Override | ||
public Class<? extends Provider> getProviderClass() { | ||
return HashProvider.class; | ||
} | ||
|
||
@Override | ||
public Class<? extends ProviderFactory> getProviderFactoryClass() { | ||
return HashProviderFactory.class; | ||
} | ||
} |
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.