Search in sources :

Example 1 with ECDHDecrypter

use of com.nimbusds.jose.crypto.ECDHDecrypter in project gravitee-access-management by gravitee-io.

the class JWEEllipticCurveTest method encryptIdToken.

@Test
public void encryptIdToken() {
    try {
        // prepare encryption private & public key
        com.nimbusds.jose.jwk.ECKey jwk = new ECKeyGenerator(this.crv).generate();
        ECKey key = new ECKey();
        key.setKid("ecEnc");
        key.setUse("enc");
        key.setCrv(jwk.getCurve().getName());
        key.setX(jwk.getX().toString());
        key.setY(jwk.getY().toString());
        Client client = new Client();
        client.setIdTokenEncryptedResponseAlg(alg);
        client.setIdTokenEncryptedResponseEnc(enc);
        when(jwkService.getKeys(client)).thenReturn(Maybe.just(new JWKSet()));
        when(jwkService.filter(any(), any())).thenReturn(Maybe.just(key));
        TestObserver testObserver = jweService.encryptIdToken("JWT", client).test();
        testObserver.assertNoErrors();
        testObserver.assertComplete();
        testObserver.assertValue(jweString -> {
            JWEObject jwe = JWEObject.parse((String) jweString);
            jwe.decrypt(new ECDHDecrypter(jwk));
            return "JWT".equals(jwe.getPayload().toString());
        });
    } catch (JOSEException e) {
        fail(e.getMessage());
    }
}
Also used : ECDHDecrypter(com.nimbusds.jose.crypto.ECDHDecrypter) JWEObject(com.nimbusds.jose.JWEObject) JWKSet(io.gravitee.am.model.oidc.JWKSet) ECKeyGenerator(com.nimbusds.jose.jwk.gen.ECKeyGenerator) ECKey(io.gravitee.am.model.jose.ECKey) Client(io.gravitee.am.model.oidc.Client) JOSEException(com.nimbusds.jose.JOSEException) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 2 with ECDHDecrypter

use of com.nimbusds.jose.crypto.ECDHDecrypter in project gravitee-access-management by gravitee-io.

the class JWEEllipticCurveTest method encryptUserinfo.

@Test
public void encryptUserinfo() {
    try {
        // prepare encryption private & public key
        com.nimbusds.jose.jwk.ECKey jwk = new ECKeyGenerator(this.crv).generate();
        ECKey key = new ECKey();
        key.setKid("ecEnc");
        key.setUse("enc");
        key.setCrv(jwk.getCurve().getName());
        key.setX(jwk.getX().toString());
        key.setY(jwk.getY().toString());
        Client client = new Client();
        client.setUserinfoEncryptedResponseAlg(alg);
        client.setUserinfoEncryptedResponseEnc(enc);
        when(jwkService.getKeys(client)).thenReturn(Maybe.just(new JWKSet()));
        when(jwkService.filter(any(), any())).thenReturn(Maybe.just(key));
        TestObserver testObserver = jweService.encryptUserinfo("JWT", client).test();
        testObserver.assertNoErrors();
        testObserver.assertComplete();
        testObserver.assertValue(jweString -> {
            JWEObject jwe = JWEObject.parse((String) jweString);
            jwe.decrypt(new ECDHDecrypter(jwk));
            return "JWT".equals(jwe.getPayload().toString());
        });
    } catch (JOSEException e) {
        fail(e.getMessage());
    }
}
Also used : ECDHDecrypter(com.nimbusds.jose.crypto.ECDHDecrypter) JWEObject(com.nimbusds.jose.JWEObject) JWKSet(io.gravitee.am.model.oidc.JWKSet) ECKeyGenerator(com.nimbusds.jose.jwk.gen.ECKeyGenerator) ECKey(io.gravitee.am.model.jose.ECKey) Client(io.gravitee.am.model.oidc.Client) JOSEException(com.nimbusds.jose.JOSEException) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 3 with ECDHDecrypter

use of com.nimbusds.jose.crypto.ECDHDecrypter 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

ECDHDecrypter (com.nimbusds.jose.crypto.ECDHDecrypter)3 JOSEException (com.nimbusds.jose.JOSEException)2 JWEObject (com.nimbusds.jose.JWEObject)2 ECKeyGenerator (com.nimbusds.jose.jwk.gen.ECKeyGenerator)2 ECKey (io.gravitee.am.model.jose.ECKey)2 Client (io.gravitee.am.model.oidc.Client)2 JWKSet (io.gravitee.am.model.oidc.JWKSet)2 TestObserver (io.reactivex.observers.TestObserver)2 Test (org.junit.Test)2 DirectDecrypter (com.nimbusds.jose.crypto.DirectDecrypter)1 DirectEncrypter (com.nimbusds.jose.crypto.DirectEncrypter)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