Search in sources :

Example 1 with ECDHEncrypter

use of com.nimbusds.jose.crypto.ECDHEncrypter in project OpenID-Connect-Java-Spring-Server by mitreid-connect.

the class DefaultJWTEncryptionAndDecryptionService method buildEncryptersAndDecrypters.

/**
 * Builds all the encrypters and decrypters for this service based on the key map.
 * @throws
 * @throws InvalidKeySpecException
 * @throws NoSuchAlgorithmException
 * @throws JOSEException
 */
private void buildEncryptersAndDecrypters() throws NoSuchAlgorithmException, InvalidKeySpecException, JOSEException {
    for (Map.Entry<String, JWK> jwkEntry : keys.entrySet()) {
        String id = jwkEntry.getKey();
        JWK jwk = jwkEntry.getValue();
        if (jwk instanceof RSAKey) {
            // build RSA encrypters and decrypters
            // there should always at least be the public key
            RSAEncrypter encrypter = new RSAEncrypter((RSAKey) jwk);
            encrypter.getJCAContext().setProvider(BouncyCastleProviderSingleton.getInstance());
            encrypters.put(id, encrypter);
            if (jwk.isPrivate()) {
                // we can decrypt!
                RSADecrypter decrypter = new RSADecrypter((RSAKey) jwk);
                decrypter.getJCAContext().setProvider(BouncyCastleProviderSingleton.getInstance());
                decrypters.put(id, decrypter);
            } else {
                logger.warn("No private key for key #" + jwk.getKeyID());
            }
        } else if (jwk instanceof ECKey) {
            // build EC Encrypters and decrypters
            ECDHEncrypter encrypter = new ECDHEncrypter((ECKey) jwk);
            encrypter.getJCAContext().setProvider(BouncyCastleProviderSingleton.getInstance());
            encrypters.put(id, encrypter);
            if (jwk.isPrivate()) {
                // we can decrypt too
                ECDHDecrypter decrypter = new ECDHDecrypter((ECKey) jwk);
                decrypter.getJCAContext().setProvider(BouncyCastleProviderSingleton.getInstance());
                decrypters.put(id, decrypter);
            } else {
                logger.warn("No private key for key # " + jwk.getKeyID());
            }
        } else if (jwk instanceof OctetSequenceKey) {
            // build symmetric encrypters and decrypters
            DirectEncrypter encrypter = new DirectEncrypter((OctetSequenceKey) jwk);
            encrypter.getJCAContext().setProvider(BouncyCastleProviderSingleton.getInstance());
            DirectDecrypter decrypter = new DirectDecrypter((OctetSequenceKey) jwk);
            decrypter.getJCAContext().setProvider(BouncyCastleProviderSingleton.getInstance());
            encrypters.put(id, encrypter);
            decrypters.put(id, decrypter);
        } else {
            logger.warn("Unknown key type: " + jwk);
        }
    }
}
Also used : DirectEncrypter(com.nimbusds.jose.crypto.DirectEncrypter) RSAKey(com.nimbusds.jose.jwk.RSAKey) RSAEncrypter(com.nimbusds.jose.crypto.RSAEncrypter) ECDHEncrypter(com.nimbusds.jose.crypto.ECDHEncrypter) ECKey(com.nimbusds.jose.jwk.ECKey) DirectDecrypter(com.nimbusds.jose.crypto.DirectDecrypter) ECDHDecrypter(com.nimbusds.jose.crypto.ECDHDecrypter) OctetSequenceKey(com.nimbusds.jose.jwk.OctetSequenceKey) HashMap(java.util.HashMap) Map(java.util.Map) JWK(com.nimbusds.jose.jwk.JWK) RSADecrypter(com.nimbusds.jose.crypto.RSADecrypter)

Aggregations

DirectDecrypter (com.nimbusds.jose.crypto.DirectDecrypter)1 DirectEncrypter (com.nimbusds.jose.crypto.DirectEncrypter)1 ECDHDecrypter (com.nimbusds.jose.crypto.ECDHDecrypter)1 ECDHEncrypter (com.nimbusds.jose.crypto.ECDHEncrypter)1 RSADecrypter (com.nimbusds.jose.crypto.RSADecrypter)1 RSAEncrypter (com.nimbusds.jose.crypto.RSAEncrypter)1 ECKey (com.nimbusds.jose.jwk.ECKey)1 JWK (com.nimbusds.jose.jwk.JWK)1 OctetSequenceKey (com.nimbusds.jose.jwk.OctetSequenceKey)1 RSAKey (com.nimbusds.jose.jwk.RSAKey)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1