Search in sources :

Example 16 with KeyWrapper

use of org.keycloak.crypto.KeyWrapper in project keycloak by keycloak.

the class AbstractEcdsaKeyProvider method createKeyWrapper.

protected KeyWrapper createKeyWrapper(KeyPair keyPair, String ecInNistRep) {
    KeyWrapper key = new KeyWrapper();
    key.setProviderId(model.getId());
    key.setProviderPriority(model.get("priority", 0l));
    key.setKid(KeyUtils.createKeyId(keyPair.getPublic()));
    key.setUse(KeyUse.SIG);
    key.setType(KeyType.EC);
    key.setAlgorithm(AbstractEcdsaKeyProviderFactory.convertECDomainParmNistRepToAlgorithm(ecInNistRep));
    key.setStatus(status);
    key.setPrivateKey(keyPair.getPrivate());
    key.setPublicKey(keyPair.getPublic());
    return key;
}
Also used : KeyWrapper(org.keycloak.crypto.KeyWrapper)

Example 17 with KeyWrapper

use of org.keycloak.crypto.KeyWrapper in project keycloak by keycloak.

the class OAuthClient method findKey.

private KeyWrapper findKey(JSONWebKeySet jsonWebKeySet, String algoritm, String kid) {
    for (JWK k : jsonWebKeySet.getKeys()) {
        if (k.getKeyId().equals(kid) && k.getAlgorithm().equals(algoritm)) {
            PublicKey publicKey = JWKParser.create(k).toPublicKey();
            KeyWrapper key = new KeyWrapper();
            key.setKid(k.getKeyId());
            key.setAlgorithm(k.getAlgorithm());
            key.setPublicKey(publicKey);
            key.setUse(KeyUse.SIG);
            return key;
        }
    }
    return null;
}
Also used : KeyWrapper(org.keycloak.crypto.KeyWrapper) PublicKey(java.security.PublicKey) JWK(org.keycloak.jose.jwk.JWK)

Example 18 with KeyWrapper

use of org.keycloak.crypto.KeyWrapper in project keycloak by keycloak.

the class OAuthClient method createSigner.

public SignatureSignerContext createSigner(PrivateKey privateKey, String kid, String algorithm) {
    KeyWrapper keyWrapper = new KeyWrapper();
    keyWrapper.setAlgorithm(algorithm);
    keyWrapper.setKid(kid);
    keyWrapper.setPrivateKey(privateKey);
    SignatureSignerContext signer;
    switch(algorithm) {
        case Algorithm.ES256:
        case Algorithm.ES384:
        case Algorithm.ES512:
            signer = new ServerECDSASignatureSignerContext(keyWrapper);
            break;
        default:
            signer = new AsymmetricSignatureSignerContext(keyWrapper);
    }
    return signer;
}
Also used : KeyWrapper(org.keycloak.crypto.KeyWrapper) ServerECDSASignatureSignerContext(org.keycloak.crypto.ServerECDSASignatureSignerContext) AsymmetricSignatureSignerContext(org.keycloak.crypto.AsymmetricSignatureSignerContext) SignatureSignerContext(org.keycloak.crypto.SignatureSignerContext) ServerECDSASignatureSignerContext(org.keycloak.crypto.ServerECDSASignatureSignerContext) AsymmetricSignatureSignerContext(org.keycloak.crypto.AsymmetricSignatureSignerContext)

Example 19 with KeyWrapper

use of org.keycloak.crypto.KeyWrapper in project keycloak by keycloak.

the class OAuthClient method getRealmPublicKey.

private KeyWrapper getRealmPublicKey(String realm, String algoritm, String kid) {
    boolean loadedKeysFromServer = false;
    JSONWebKeySet jsonWebKeySet = publicKeys.get(realm);
    if (jsonWebKeySet == null) {
        jsonWebKeySet = getRealmKeys(realm);
        publicKeys.put(realm, jsonWebKeySet);
        loadedKeysFromServer = true;
    }
    KeyWrapper key = findKey(jsonWebKeySet, algoritm, kid);
    if (key == null && !loadedKeysFromServer) {
        jsonWebKeySet = getRealmKeys(realm);
        publicKeys.put(realm, jsonWebKeySet);
        key = findKey(jsonWebKeySet, algoritm, kid);
    }
    if (key == null) {
        throw new RuntimeException("Public key for realm:" + realm + ", algorithm: " + algoritm + " not found");
    }
    return key;
}
Also used : KeyWrapper(org.keycloak.crypto.KeyWrapper) JSONWebKeySet(org.keycloak.jose.jwk.JSONWebKeySet)

Aggregations

KeyWrapper (org.keycloak.crypto.KeyWrapper)19 PublicKey (java.security.PublicKey)5 PrivateKey (java.security.PrivateKey)3 SecretKey (javax.crypto.SecretKey)3 SignatureSignerContext (org.keycloak.crypto.SignatureSignerContext)3 JSONWebKeySet (org.keycloak.jose.jwk.JSONWebKeySet)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Key (java.security.Key)2 Comparator (java.util.Comparator)2 Optional (java.util.Optional)2 Stream (java.util.stream.Stream)2 SecretKeySpec (javax.crypto.spec.SecretKeySpec)2 Logger (org.jboss.logging.Logger)2 Algorithm (org.keycloak.crypto.Algorithm)2 CekManagementProvider (org.keycloak.crypto.CekManagementProvider)2 ContentEncryptionProvider (org.keycloak.crypto.ContentEncryptionProvider)2 KeyUse (org.keycloak.crypto.KeyUse)2 JWEException (org.keycloak.jose.jwe.JWEException)2 JWEAlgorithmProvider (org.keycloak.jose.jwe.alg.JWEAlgorithmProvider)2 JWEEncryptionProvider (org.keycloak.jose.jwe.enc.JWEEncryptionProvider)2