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
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected KeyWrapper createKeyWrapper(KeyPair keyPair, String algorithm, KeyUse
key.setProviderId(model.getId());
key.setProviderPriority(model.get("priority", 0l));

key.setKid(KeyUtils.createKeyId(keyPair.getPublic()));
key.setKid(model.get(Attributes.KID_KEY) != null ? model.get(Attributes.KID_KEY) : KeyUtils.createKeyId(keyPair.getPublic()));
key.setUse(keyUse);
key.setType(KeyType.EC);
key.setAlgorithm(algorithm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected KeyWrapper createKeyWrapper(KeyPair keyPair, String curveName) {
key.setProviderId(model.getId());
key.setProviderPriority(model.get("priority", 0l));

key.setKid(KeyUtils.createKeyId(keyPair.getPublic()));
key.setKid(model.get(Attributes.KID_KEY) != null ? model.get(Attributes.KID_KEY) : KeyUtils.createKeyId(keyPair.getPublic()));
key.setUse(KeyUse.SIG);
key.setType(KeyType.OKP);
key.setAlgorithm(Algorithm.EdDSA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,27 @@ private void unsupportedEc(String ecInNistRep, String algorithmMode) {
response.close();
}

@Test
public void customKid() {
String kid = "019a91a4-5b7e-7c3d-8f2a-4b6c8d0e1f23";

ComponentRepresentation rep = createRep("valid", GeneratedEcdhKeyProviderFactory.ID);
rep.getConfig().putSingle(Attributes.PRIORITY_KEY, Long.toString(System.currentTimeMillis()));
rep.getConfig().putSingle(ECDH_ALGORITHM_KEY, Algorithm.ECDH_ES);
rep.getConfig().putSingle(Attributes.KID_KEY, kid);

Response response = realm.admin().components().add(rep);
String id = ApiUtil.getCreatedId(response);
realm.cleanup().add(r -> r.components().component(id).remove());
response.close();

KeyMetadataRepresentation key = realm.admin().keys().getKeyMetadata().getKeys().stream()
.filter(k -> KeyType.EC.equals(k.getType()) && id.equals(k.getProviderId()))
.findFirst()
.orElseThrow();
assertEquals(kid, key.getKid());
}

@Test
public void changeCurveFromP256ToP384Direct() throws Exception {
changeCurve("P-256", "P-384", Algorithm.ECDH_ES, Algorithm.ECDH_ES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
package org.keycloak.tests.keys;

import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.cert.X509Certificate;
import java.security.interfaces.ECPublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

import jakarta.ws.rs.core.Response;

import org.keycloak.common.util.KeyUtils;
import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.common.util.PemUtils;
import org.keycloak.crypto.KeyType;
Expand Down Expand Up @@ -171,6 +173,54 @@ private void unsupportedEc(String ecInNistRep) {
Assertions.assertEquals(400, response.getStatus());
}

@Test
public void defaultKidIsDerivedFromPublicKey() throws Exception {
String id = createKeyProvider(null);

KeyMetadataRepresentation key = getKey(id);
PublicKey publicKey = KeyFactory.getInstance("EC")
.generatePublic(new X509EncodedKeySpec(Base64.getMimeDecoder().decode(key.getPublicKey())));
assertEquals(KeyUtils.createKeyId(publicKey), key.getKid());
}

@Test
public void customKid() {
String kid = "019a91a4-5b7e-7c3d-8f2a-4b6c8d0e1f23";
String id = createKeyProvider(kid);

assertEquals(kid, getKey(id).getKid());

// kid is kept when the key is regenerated
ComponentRepresentation rep = realm.admin().components().component(id).toRepresentation();
rep.getConfig().putSingle(ECDSA_ELLIPTIC_CURVE_KEY, "P-384");
realm.admin().components().component(id).update(rep);

KeyMetadataRepresentation key = getKey(id);
assertEquals(kid, key.getKid());
assertEquals("P-384", GeneratedEcdsaKeyProviderFactory.convertJWSAlgorithmToECDomainParmNistRep(key.getAlgorithm()));
}

private String createKeyProvider(String kid) {
ComponentRepresentation rep = createRep("valid", GeneratedEcdsaKeyProviderFactory.ID);
rep.getConfig().putSingle(Attributes.PRIORITY_KEY, Long.toString(System.currentTimeMillis()));
if (kid != null) {
rep.getConfig().putSingle(Attributes.KID_KEY, kid);
}

Response response = realm.admin().components().add(rep);
String id = ApiUtil.getCreatedId(response);
realm.cleanup().add(r -> r.components().component(id).remove());
response.close();
return id;
}

private KeyMetadataRepresentation getKey(String componentId) {
return realm.admin().keys().getKeyMetadata().getKeys().stream()
.filter(k -> KeyType.EC.equals(k.getType()) && componentId.equals(k.getProviderId()))
.findFirst()
.orElseThrow();
}

@Test
public void changeCurveFromP256ToP384() throws Exception {
changeCurve("P-256", "P-384");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2026 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.tests.keys;

import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

import jakarta.ws.rs.core.Response;

import org.keycloak.common.util.KeyUtils;
import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.crypto.KeyType;
import org.keycloak.keys.Attributes;
import org.keycloak.keys.GeneratedEddsaKeyProviderFactory;
import org.keycloak.keys.KeyProvider;
import org.keycloak.representations.idm.ComponentRepresentation;
import org.keycloak.representations.idm.KeysMetadataRepresentation.KeyMetadataRepresentation;
import org.keycloak.testframework.annotations.InjectRealm;
import org.keycloak.testframework.annotations.KeycloakIntegrationTest;
import org.keycloak.testframework.realm.ManagedRealm;
import org.keycloak.testframework.util.ApiUtil;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

@KeycloakIntegrationTest
public class GeneratedEddsaKeyProviderTest {

@InjectRealm
ManagedRealm realm;

@Test
public void defaultKidIsDerivedFromPublicKey() throws Exception {
String id = createKeyProvider(null);

KeyMetadataRepresentation key = getKey(id);
PublicKey publicKey = KeyFactory.getInstance("EdDSA")
.generatePublic(new X509EncodedKeySpec(Base64.getMimeDecoder().decode(key.getPublicKey())));
assertEquals(KeyUtils.createKeyId(publicKey), key.getKid());
}

@Test
public void customKid() {
String kid = "019a91a4-5b7e-7c3d-8f2a-4b6c8d0e1f23";
String id = createKeyProvider(kid);

assertEquals(kid, getKey(id).getKid());
}

private String createKeyProvider(String kid) {
ComponentRepresentation rep = new ComponentRepresentation();
rep.setName("valid");
rep.setParentId(realm.admin().toRepresentation().getId());
rep.setProviderId(GeneratedEddsaKeyProviderFactory.ID);
rep.setProviderType(KeyProvider.class.getName());
rep.setConfig(new MultivaluedHashMap<>());
rep.getConfig().putSingle(Attributes.PRIORITY_KEY, Long.toString(System.currentTimeMillis()));
if (kid != null) {
rep.getConfig().putSingle(Attributes.KID_KEY, kid);
}

Response response = realm.admin().components().add(rep);
String id = ApiUtil.getCreatedId(response);
realm.cleanup().add(r -> r.components().component(id).remove());
response.close();
return id;
}

private KeyMetadataRepresentation getKey(String componentId) {
return realm.admin().keys().getKeyMetadata().getKeys().stream()
.filter(k -> KeyType.OKP.equals(k.getType()) && componentId.equals(k.getProviderId()))
.findFirst()
.orElseThrow();
}
}
Loading