Search in sources :

Example 1 with OctetKeyPairGenerator

use of com.nimbusds.jose.jwk.gen.OctetKeyPairGenerator in project gravitee-access-management by gravitee-io.

the class JWEEdwardCurveTest method encryptIdToken.

@Test
public void encryptIdToken() {
    try {
        final OctetKeyPair jwk = new OctetKeyPairGenerator(Curve.X25519).generate();
        OKPKey key = new OKPKey();
        key.setKid("okpEnc");
        key.setUse("enc");
        key.setCrv(jwk.getCurve().getName());
        key.setX(jwk.getX().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 -> {
            try {
                JWEObject jwe = JWEObject.parse((String) jweString);
                jwe.decrypt(new X25519Decrypter(jwk));
                return "JWT".equals(jwe.getPayload().toString());
            } catch (JOSEException e) {
                fail(e.getMessage());
            }
            return false;
        });
    } catch (JOSEException e) {
        fail(e.getMessage());
    }
}
Also used : X25519Decrypter(com.nimbusds.jose.crypto.X25519Decrypter) OctetKeyPair(com.nimbusds.jose.jwk.OctetKeyPair) JWEObject(com.nimbusds.jose.JWEObject) JWKSet(io.gravitee.am.model.oidc.JWKSet) Client(io.gravitee.am.model.oidc.Client) JOSEException(com.nimbusds.jose.JOSEException) OKPKey(io.gravitee.am.model.jose.OKPKey) TestObserver(io.reactivex.observers.TestObserver) OctetKeyPairGenerator(com.nimbusds.jose.jwk.gen.OctetKeyPairGenerator) Test(org.junit.Test)

Example 2 with OctetKeyPairGenerator

use of com.nimbusds.jose.jwk.gen.OctetKeyPairGenerator in project gravitee-access-management by gravitee-io.

the class JWEEdwardCurveTest method encryptUserinfo.

@Test
public void encryptUserinfo() {
    try {
        final OctetKeyPair jwk = new OctetKeyPairGenerator(Curve.X25519).generate();
        OKPKey key = new OKPKey();
        key.setKid("okpEnc");
        key.setUse("enc");
        key.setCrv(jwk.getCurve().getName());
        key.setX(jwk.getX().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 -> {
            try {
                JWEObject jwe = JWEObject.parse((String) jweString);
                jwe.decrypt(new X25519Decrypter(jwk));
                return "JWT".equals(jwe.getPayload().toString());
            } catch (JOSEException e) {
                fail(e.getMessage());
            }
            return false;
        });
    } catch (JOSEException e) {
        fail(e.getMessage());
    }
}
Also used : X25519Decrypter(com.nimbusds.jose.crypto.X25519Decrypter) OctetKeyPair(com.nimbusds.jose.jwk.OctetKeyPair) JWEObject(com.nimbusds.jose.JWEObject) JWKSet(io.gravitee.am.model.oidc.JWKSet) Client(io.gravitee.am.model.oidc.Client) JOSEException(com.nimbusds.jose.JOSEException) OKPKey(io.gravitee.am.model.jose.OKPKey) TestObserver(io.reactivex.observers.TestObserver) OctetKeyPairGenerator(com.nimbusds.jose.jwk.gen.OctetKeyPairGenerator) Test(org.junit.Test)

Example 3 with OctetKeyPairGenerator

use of com.nimbusds.jose.jwk.gen.OctetKeyPairGenerator in project gravitee-access-management by gravitee-io.

the class JWSServiceTest method testValidSignature_OKP.

@Test
public void testValidSignature_OKP() throws JOSEException {
    // Generate OKP key
    OctetKeyPair okp = new OctetKeyPairGenerator(Curve.Ed25519).generate();
    OKPKey key = new OKPKey();
    key.setKty("OKP");
    key.setKid(KID);
    key.setCrv(okp.getCurve().getStdName());
    key.setX(okp.getX().toString());
    // Sign JWT with Edward Curve algorithm
    SignedJWT signedJWT = new SignedJWT(new JWSHeader.Builder(JWSAlgorithm.EdDSA).keyID(KID).build(), new JWTClaimsSet.Builder().expirationTime(Date.from(Instant.now().plus(1, ChronoUnit.DAYS))).build());
    signedJWT.sign(new Ed25519Signer(okp));
    assertTrue("Should be ok", jwsService.isValidSignature(signedJWT, key));
}
Also used : Ed25519Signer(com.nimbusds.jose.crypto.Ed25519Signer) OctetKeyPair(com.nimbusds.jose.jwk.OctetKeyPair) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) SignedJWT(com.nimbusds.jwt.SignedJWT) OKPKey(io.gravitee.am.model.jose.OKPKey) JWSHeader(com.nimbusds.jose.JWSHeader) OctetKeyPairGenerator(com.nimbusds.jose.jwk.gen.OctetKeyPairGenerator) Test(org.junit.Test)

Example 4 with OctetKeyPairGenerator

use of com.nimbusds.jose.jwk.gen.OctetKeyPairGenerator in project conformance-suite by openid-certification.

the class OIDCCGenerateServerJWKs method createKeys.

/**
 * @param keyCount
 * @param keyType EC, RSA or OKP
 * @param keyUse if null keys won't have use
 * @param algorithm if null keys won't have alg
 * @throws JOSEException
 */
protected void createKeys(int keyCount, KeyType keyType, KeyUse keyUse, Algorithm algorithm, Curve curveForECKeys) throws JOSEException {
    if (keyCount < 1) {
        return;
    }
    int whichKeyToUse = getIndexOfKeyToUse(keyCount);
    for (int i = 0; i < keyCount; i++) {
        JWKGenerator<? extends JWK> jwkGenerator = null;
        if (KeyType.EC.equals(keyType)) {
            jwkGenerator = new ECKeyGenerator(curveForECKeys);
        } else if (KeyType.RSA.equals(keyType)) {
            jwkGenerator = new RSAKeyGenerator(rsaKeySize);
        } else if (KeyType.OKP.equals(keyType)) {
            jwkGenerator = new OctetKeyPairGenerator(edCurve);
        }
        if (keyUse != null) {
            jwkGenerator.keyUse(keyUse);
        }
        if (generateKids) {
            jwkGenerator.keyID(UUID.randomUUID().toString());
        }
        if (algorithm != null) {
            jwkGenerator.algorithm(algorithm);
        }
        JWK generatedJWK = jwkGenerator.generate();
        allGeneratedKeys.add(generatedJWK);
        if (keyUse.equals(KeyUse.ENCRYPTION)) {
            encryptionKeysToBeUsed.add(generatedJWK);
        }
        if (i == whichKeyToUse && (keyUse.equals(KeyUse.SIGNATURE))) {
            signingKeyToBeUsed.add(generatedJWK);
        }
    }
}
Also used : ECKeyGenerator(com.nimbusds.jose.jwk.gen.ECKeyGenerator) RSAKeyGenerator(com.nimbusds.jose.jwk.gen.RSAKeyGenerator) OctetKeyPairGenerator(com.nimbusds.jose.jwk.gen.OctetKeyPairGenerator) JWK(com.nimbusds.jose.jwk.JWK)

Aggregations

OctetKeyPairGenerator (com.nimbusds.jose.jwk.gen.OctetKeyPairGenerator)4 OctetKeyPair (com.nimbusds.jose.jwk.OctetKeyPair)3 OKPKey (io.gravitee.am.model.jose.OKPKey)3 Test (org.junit.Test)3 JOSEException (com.nimbusds.jose.JOSEException)2 JWEObject (com.nimbusds.jose.JWEObject)2 X25519Decrypter (com.nimbusds.jose.crypto.X25519Decrypter)2 Client (io.gravitee.am.model.oidc.Client)2 JWKSet (io.gravitee.am.model.oidc.JWKSet)2 TestObserver (io.reactivex.observers.TestObserver)2 JWSHeader (com.nimbusds.jose.JWSHeader)1 Ed25519Signer (com.nimbusds.jose.crypto.Ed25519Signer)1 JWK (com.nimbusds.jose.jwk.JWK)1 ECKeyGenerator (com.nimbusds.jose.jwk.gen.ECKeyGenerator)1 RSAKeyGenerator (com.nimbusds.jose.jwk.gen.RSAKeyGenerator)1 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)1 SignedJWT (com.nimbusds.jwt.SignedJWT)1