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;
}
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);
}
}
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);
}
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);
}
}
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;
}
}
Aggregations