2016-10-05 11:06:54 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 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.keys;
|
|
|
|
|
2018-06-22 14:05:42 +00:00
|
|
|
import org.keycloak.common.util.KeyUtils;
|
2016-10-05 11:06:54 +00:00
|
|
|
import org.keycloak.component.ComponentModel;
|
2021-03-23 11:37:50 +00:00
|
|
|
import org.keycloak.crypto.*;
|
2022-02-17 16:41:54 +00:00
|
|
|
import org.keycloak.jose.jwe.JWEConstants;
|
2016-10-05 11:06:54 +00:00
|
|
|
import org.keycloak.models.RealmModel;
|
|
|
|
|
|
|
|
import java.security.KeyPair;
|
|
|
|
import java.security.cert.X509Certificate;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2020-10-27 19:41:33 +00:00
|
|
|
import java.util.stream.Stream;
|
2016-10-05 11:06:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
|
|
|
|
*/
|
2018-06-22 14:05:42 +00:00
|
|
|
public abstract class AbstractRsaKeyProvider implements KeyProvider {
|
2016-10-05 11:06:54 +00:00
|
|
|
|
2018-06-22 14:05:42 +00:00
|
|
|
private final KeyStatus status;
|
2016-10-05 11:06:54 +00:00
|
|
|
|
|
|
|
private final ComponentModel model;
|
|
|
|
|
2018-06-22 14:05:42 +00:00
|
|
|
private final KeyWrapper key;
|
2016-10-05 11:06:54 +00:00
|
|
|
|
2018-08-20 11:14:33 +00:00
|
|
|
private final String algorithm;
|
|
|
|
|
2016-10-05 11:06:54 +00:00
|
|
|
public AbstractRsaKeyProvider(RealmModel realm, ComponentModel model) {
|
|
|
|
this.model = model;
|
2018-06-22 14:05:42 +00:00
|
|
|
this.status = KeyStatus.from(model.get(Attributes.ACTIVE_KEY, true), model.get(Attributes.ENABLED_KEY, true));
|
2022-02-17 16:41:54 +00:00
|
|
|
|
|
|
|
String defaultAlgorithmKey = KeyUse.ENC.name().equals(model.get(Attributes.KEY_USE)) ? JWEConstants.RSA_OAEP : Algorithm.RS256;
|
|
|
|
this.algorithm = model.get(Attributes.ALGORITHM_KEY, defaultAlgorithmKey);
|
2016-10-05 11:06:54 +00:00
|
|
|
|
2018-06-22 14:05:42 +00:00
|
|
|
if (model.hasNote(KeyWrapper.class.getName())) {
|
|
|
|
key = model.getNote(KeyWrapper.class.getName());
|
2016-10-05 11:06:54 +00:00
|
|
|
} else {
|
2018-06-22 14:05:42 +00:00
|
|
|
key = loadKey(realm, model);
|
|
|
|
model.setNote(KeyWrapper.class.getName(), key);
|
2016-10-05 11:06:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 14:05:42 +00:00
|
|
|
protected abstract KeyWrapper loadKey(RealmModel realm, ComponentModel model);
|
2016-10-05 11:06:54 +00:00
|
|
|
|
|
|
|
@Override
|
2020-10-27 19:41:33 +00:00
|
|
|
public Stream<KeyWrapper> getKeysStream() {
|
|
|
|
return Stream.of(key);
|
2016-10-05 11:06:54 +00:00
|
|
|
}
|
|
|
|
|
2021-07-05 20:04:13 +00:00
|
|
|
protected KeyWrapper createKeyWrapper(KeyPair keyPair, X509Certificate certificate, KeyUse keyUse) {
|
|
|
|
return createKeyWrapper(keyPair, certificate, Collections.emptyList(), keyUse);
|
2021-03-23 11:37:50 +00:00
|
|
|
}
|
|
|
|
|
2021-07-05 20:04:13 +00:00
|
|
|
protected KeyWrapper createKeyWrapper(KeyPair keyPair, X509Certificate certificate, List<X509Certificate> certificateChain,
|
|
|
|
KeyUse keyUse) {
|
2018-06-22 14:05:42 +00:00
|
|
|
KeyWrapper key = new KeyWrapper();
|
2016-10-05 11:06:54 +00:00
|
|
|
|
2018-06-22 14:05:42 +00:00
|
|
|
key.setProviderId(model.getId());
|
|
|
|
key.setProviderPriority(model.get("priority", 0l));
|
2016-10-05 11:06:54 +00:00
|
|
|
|
2018-06-22 14:05:42 +00:00
|
|
|
key.setKid(KeyUtils.createKeyId(keyPair.getPublic()));
|
2021-07-05 20:04:13 +00:00
|
|
|
key.setUse(keyUse == null ? KeyUse.SIG : keyUse);
|
2018-06-22 14:05:42 +00:00
|
|
|
key.setType(KeyType.RSA);
|
2018-08-20 11:14:33 +00:00
|
|
|
key.setAlgorithm(algorithm);
|
2018-06-22 14:05:42 +00:00
|
|
|
key.setStatus(status);
|
2018-12-05 06:31:48 +00:00
|
|
|
key.setPrivateKey(keyPair.getPrivate());
|
|
|
|
key.setPublicKey(keyPair.getPublic());
|
2018-06-22 14:05:42 +00:00
|
|
|
key.setCertificate(certificate);
|
2016-10-05 11:06:54 +00:00
|
|
|
|
2021-03-23 11:37:50 +00:00
|
|
|
if (!certificateChain.isEmpty()) {
|
|
|
|
if (certificate != null && !certificate.equals(certificateChain.get(0))) {
|
|
|
|
// just in case the chain does not contain the end-user certificate
|
|
|
|
certificateChain.add(0, certificate);
|
|
|
|
}
|
|
|
|
key.setCertificateChain(certificateChain);
|
|
|
|
}
|
|
|
|
|
2018-06-22 14:05:42 +00:00
|
|
|
return key;
|
2016-10-05 11:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|