Search in sources :

Example 1 with KeyType

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

the class JWEUtil method selectAsymmetricKeyForEncryption.

/**
 * returns a key that has the correct key type and optionally use=enc
 * or null if no key was found
 * Only for RSA or EC keys
 * @param jwkSet
 * @param alg
 * @return
 */
public static JWK selectAsymmetricKeyForEncryption(JWKSet jwkSet, JWEAlgorithm alg) {
    if (jwkSet == null) {
        return null;
    }
    KeyType keyType = null;
    if (JWEAlgorithm.Family.RSA.contains(alg)) {
        keyType = KeyType.RSA;
    } else if (JWEAlgorithm.Family.ECDH_ES.contains(alg)) {
        keyType = KeyType.EC;
    }
    JWKMatcher jwkMatcher = new JWKMatcher.Builder().keyType(keyType).keyUses(KeyUse.ENCRYPTION, null).build();
    JWK currentMatch = null;
    for (JWK jwk : jwkSet.getKeys()) {
        if (jwkMatcher.matches(jwk)) {
            if (currentMatch == null) {
                currentMatch = jwk;
            } else {
                if (!KeyUse.ENCRYPTION.equals(currentMatch.getKeyUse()) && KeyUse.ENCRYPTION.equals(jwk.getKeyUse())) {
                    // this is a better match
                    currentMatch = jwk;
                }
            }
        }
    }
    return currentMatch;
}
Also used : KeyType(com.nimbusds.jose.jwk.KeyType) JWKMatcher(com.nimbusds.jose.jwk.JWKMatcher) JWK(com.nimbusds.jose.jwk.JWK)

Example 2 with KeyType

use of com.nimbusds.jose.jwk.KeyType in project concord by walmartlabs.

the class SignatureConfigurationFactory method create.

public static SignatureConfiguration create(String cfg) {
    if (cfg == null) {
        return null;
    }
    try {
        JSONObject json = objectMapper.readValue(cfg, JSONObject.class);
        KeyType kty = KeyType.parse(json.getAsString("kty"));
        if (KeyType.EC.equals(kty)) {
            KeyPair key = JwkHelper.buildECKeyPairFromJwk(json);
            return new ECSignatureConfiguration(key);
        } else if (KeyType.RSA.equals(kty)) {
            KeyPair key = JwkHelper.buildRSAKeyPairFromJwk(json);
            return new RSASignatureConfiguration(key);
        } else if (KeyType.OCT.equals(kty)) {
            String secret = JwkHelper.buildSecretFromJwk(json);
            return new SecretSignatureConfiguration(secret.getBytes(UTF_8));
        } else {
            throw new RuntimeException("unknown key type: " + kty);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : KeyPair(java.security.KeyPair) JSONObject(net.minidev.json.JSONObject) KeyType(com.nimbusds.jose.jwk.KeyType)

Example 3 with KeyType

use of com.nimbusds.jose.jwk.KeyType in project micronaut-security by micronaut-projects.

the class KeyTypeConverter method convert.

/**
 * @param object     e.g. EC
 * @param targetType The target type being converted to {@link com.nimbusds.jose.jwk.KeyType}
 * @param context    The {@link io.micronaut.core.convert.ConversionContext}
 * @return An optional {@link com.nimbusds.jose.jwk.KeyType}
 */
@Override
public Optional<KeyType> convert(CharSequence object, Class<KeyType> targetType, ConversionContext context) {
    if (object == null) {
        return Optional.empty();
    }
    String value = object.toString();
    KeyType keyType = KeyType.parse(value);
    return Optional.of(keyType);
}
Also used : KeyType(com.nimbusds.jose.jwk.KeyType)

Example 4 with KeyType

use of com.nimbusds.jose.jwk.KeyType in project concord by walmartlabs.

the class EncryptionConfigurationFactory method create.

public static EncryptionConfiguration create(String cfg) {
    if (cfg == null) {
        return null;
    }
    try {
        JSONObject json = objectMapper.readValue(cfg, JSONObject.class);
        KeyType kty = KeyType.parse(json.getAsString("kty"));
        if (KeyType.EC.equals(kty)) {
            KeyPair key = JwkHelper.buildECKeyPairFromJwk(json);
            return new ECEncryptionConfiguration(key);
        } else if (KeyType.RSA.equals(kty)) {
            KeyPair key = JwkHelper.buildRSAKeyPairFromJwk(json);
            return new RSAEncryptionConfiguration(key);
        } else if (KeyType.OCT.equals(kty)) {
            String secret = JwkHelper.buildSecretFromJwk(json);
            return new SecretEncryptionConfiguration(secret.getBytes(UTF_8));
        } else {
            throw new RuntimeException("unknown key type: " + kty);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : KeyPair(java.security.KeyPair) JSONObject(net.minidev.json.JSONObject) KeyType(com.nimbusds.jose.jwk.KeyType)

Example 5 with KeyType

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

the class KeyManager method getSigningPrivateKey.

public PrivateKey getSigningPrivateKey() {
    JWK signingKey = jwkSet.getKeyByKeyId(signingKeyId);
    KeyType keyType = signingKey.getKeyType();
    try {
        if (keyType.equals(KeyType.RSA)) {
            return ((RSAKey) signingKey).toPrivateKey();
        } else if (keyType.equals(KeyType.EC)) {
            return ((ECKey) signingKey).toPrivateKey();
        } else if (keyType.equals(KeyType.OKP)) {
            return ((OctetKeyPair) signingKey).toPrivateKey();
        } else {
            return null;
        }
    } catch (JOSEException e) {
        return null;
    }
}
Also used : RSAKey(com.nimbusds.jose.jwk.RSAKey) KeyType(com.nimbusds.jose.jwk.KeyType) OctetKeyPair(com.nimbusds.jose.jwk.OctetKeyPair) JOSEException(com.nimbusds.jose.JOSEException) JWK(com.nimbusds.jose.jwk.JWK)

Aggregations

KeyType (com.nimbusds.jose.jwk.KeyType)7 JWK (com.nimbusds.jose.jwk.JWK)4 JWKSet (com.nimbusds.jose.jwk.JWKSet)2 KeyPair (java.security.KeyPair)2 ParseException (java.text.ParseException)2 JSONObject (net.minidev.json.JSONObject)2 JsonObject (com.google.gson.JsonObject)1 JOSEException (com.nimbusds.jose.JOSEException)1 JWSAlgorithm (com.nimbusds.jose.JWSAlgorithm)1 JWKMatcher (com.nimbusds.jose.jwk.JWKMatcher)1 OctetKeyPair (com.nimbusds.jose.jwk.OctetKeyPair)1 RSAKey (com.nimbusds.jose.jwk.RSAKey)1