Skip to content
Closed
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 @@ -18,10 +18,12 @@

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

import org.keycloak.common.util.CertificateUtils;
import org.keycloak.common.util.KeyUtils;
import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.component.ComponentModel;
Expand Down Expand Up @@ -89,18 +91,18 @@ public void validateConfiguration(KeycloakSession session, RealmModel realm, Com
if (ecInNistRep == null) ecInNistRep = getDefaultEcEllipticCurve();

if (!(model.contains(getEcPrivateKeyKey()) && model.contains(getEcPublicKeyKey()))) {
generateKeys(model, ecInNistRep);
generateKeys(realm, model, ecInNistRep);
getLogger().debugv("Generated keys for {0}", realm.getName());
} else {
String currentEc = getCurveFromPublicKey(model.getConfig().getFirst(getEcPublicKeyKey()));
if (!ecInNistRep.equals(currentEc)) {
generateKeys(model, ecInNistRep);
generateKeys(realm, model, ecInNistRep);
getLogger().debugv("Elliptic Curve changed, generating new keys for {0}", realm.getName());
}
}
}

protected void generateKeys(ComponentModel model, String ecInNistRep) {
protected void generateKeys(RealmModel realm, ComponentModel model, String ecInNistRep) {
KeyPair keyPair;
try {
keyPair = KeyUtils.generateEcKeyPair(convertECDomainParmNistRepToSecRep(ecInNistRep));
Expand All @@ -110,6 +112,25 @@ protected void generateKeys(ComponentModel model, String ecInNistRep) {
} catch (Throwable t) {
throw new ComponentValidationException("Failed to generate EC keys", t);
}

String generateCertificate = model.get(Attributes.EC_GENERATE_CERTIFICATE_KEY);
if (generateCertificate != null && Boolean.parseBoolean(generateCertificate)) {
generateCertificate(realm, model, keyPair);
}
Comment on lines +116 to +119
}

private void generateCertificate(RealmModel realm, ComponentModel model, KeyPair keyPair) {
try {
X509Certificate certificate = CertificateUtils.generateV1SelfSignedCertificate(keyPair, realm.getName());
model.put(Attributes.CERTIFICATE_KEY, Base64.getEncoder().encodeToString(certificate.getEncoded()));
} catch (Throwable t) {
getLogger().warnf("Failed to generate certificate for key provider '%s' in realm '%s'. Details: %s",
model.getName(), realm.getName(), t.getMessage());
if (getLogger().isDebugEnabled()) {
getLogger().debug(t.getMessage(), t);
}
throw new ComponentValidationException("Failed to generate certificate", t);
}
}

protected String getCurveFromPublicKey(String publicEcKeyBase64Encoded) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,14 @@ private String supportedEc(String ecInNistRep, boolean withCertificate) {
ComponentRepresentation createdRep = realm.admin().components().component(id).toRepresentation();

// stands for the number of properties in the key provider config
assertEquals(withCertificate ? 3 : 2, createdRep.getConfig().size());
assertEquals(withCertificate ? 4 : 2, createdRep.getConfig().size());
assertEquals(Long.toString(priority), createdRep.getConfig().getFirst(Attributes.PRIORITY_KEY));
assertEquals(ecInNistRep, createdRep.getConfig().getFirst(ECDSA_ELLIPTIC_CURVE_KEY));
if (withCertificate) {
assertNotNull(createdRep.getConfig().getFirst(Attributes.EC_GENERATE_CERTIFICATE_KEY));
// The self-signed certificate must be persisted in the component config so that it stays
// stable across restarts and across cluster nodes sharing the same database
assertNotNull(createdRep.getConfig().getFirst(Attributes.CERTIFICATE_KEY));
}

KeysMetadataRepresentation keys = realm.admin().keys().getKeyMetadata();
Expand Down
Loading