use of io.jans.as.model.exception.InvalidParameterException in project jans by JanssenProject.
the class AbstractCryptoProvider method processKey.
private PublicKey processKey(Algorithm requestedAlgorithm, String alias, JSONObject key) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidParameterSpecException, InvalidParameterException {
PublicKey publicKey = null;
AlgorithmFamily algorithmFamily = null;
if (key.has(JWKParameter.ALGORITHM)) {
Algorithm algorithm = Algorithm.fromString(key.optString(JWKParameter.ALGORITHM));
if (requestedAlgorithm != null && !requestedAlgorithm.equals(algorithm)) {
LOG.trace("kid matched but algorithm does not match. kid algorithm:" + algorithm + ", requestedAlgorithm:" + requestedAlgorithm + ", kid:" + alias);
return null;
}
algorithmFamily = algorithm.getFamily();
} else if (key.has(JWKParameter.KEY_TYPE)) {
algorithmFamily = AlgorithmFamily.fromString(key.getString(JWKParameter.KEY_TYPE));
} else {
throw new InvalidParameterException("Wrong key (JSONObject): doesn't contain 'alg' and 'kty' properties");
}
switch(algorithmFamily) {
case RSA:
{
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(1, Base64Util.base64urldecode(key.getString(JWKParameter.MODULUS))), new BigInteger(1, Base64Util.base64urldecode(key.getString(JWKParameter.EXPONENT))));
publicKey = keyFactory.generatePublic(pubKeySpec);
break;
}
case EC:
{
EllipticEdvardsCurve curve = EllipticEdvardsCurve.fromString(key.optString(JWKParameter.CURVE));
AlgorithmParameters parameters = AlgorithmParameters.getInstance(AlgorithmFamily.EC.toString());
parameters.init(new ECGenParameterSpec(curve.getAlias()));
ECParameterSpec ecParameters = parameters.getParameterSpec(ECParameterSpec.class);
publicKey = KeyFactory.getInstance(AlgorithmFamily.EC.toString()).generatePublic(new ECPublicKeySpec(new ECPoint(new BigInteger(1, Base64Util.base64urldecode(key.getString(JWKParameter.X))), new BigInteger(1, Base64Util.base64urldecode(key.getString(JWKParameter.Y)))), ecParameters));
break;
}
case ED:
{
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64Util.base64urldecode(key.getString(JWKParameter.X)));
publicKey = KeyFactory.getInstance(key.optString(JWKParameter.ALGORITHM)).generatePublic(publicKeySpec);
break;
}
default:
{
throw new InvalidParameterException(String.format("Wrong AlgorithmFamily value: %s", algorithmFamily));
}
}
if (key.has(JWKParameter.EXPIRATION_TIME)) {
checkKeyExpiration(alias, key.getLong(JWKParameter.EXPIRATION_TIME));
}
return publicKey;
}
use of io.jans.as.model.exception.InvalidParameterException in project jans by JanssenProject.
the class JwtUtil method getPublicKey.
public static io.jans.as.model.crypto.PublicKey getPublicKey(String jwksUri, String jwks, SignatureAlgorithm signatureAlgorithm, String keyId) {
JSONObject jsonKeyValue = getJsonKey(jwksUri, jwks, keyId);
if (jsonKeyValue == null) {
return null;
}
io.jans.as.model.crypto.PublicKey publicKey = null;
try {
String resultKeyId = jsonKeyValue.getString(KEY_ID);
if (signatureAlgorithm == null) {
signatureAlgorithm = SignatureAlgorithm.fromString(jsonKeyValue.getString(ALGORITHM));
if (signatureAlgorithm == null) {
log.error(String.format("Failed to determine key '%s' signature algorithm", resultKeyId));
return null;
}
}
JSONObject jsonPublicKey = jsonKeyValue;
if (jsonKeyValue.has(PUBLIC_KEY)) {
// Use internal jwks.json format
jsonPublicKey = jsonKeyValue.getJSONObject(PUBLIC_KEY);
}
AlgorithmFamily algorithmFamily = signatureAlgorithm.getFamily();
if (algorithmFamily == AlgorithmFamily.RSA) {
String exp = jsonPublicKey.getString(EXPONENT);
String mod = jsonPublicKey.getString(MODULUS);
BigInteger publicExponent = new BigInteger(1, Base64Util.base64urldecode(exp));
BigInteger modulus = new BigInteger(1, Base64Util.base64urldecode(mod));
publicKey = new RSAPublicKey(modulus, publicExponent);
} else if (algorithmFamily == AlgorithmFamily.EC) {
String xx = jsonPublicKey.getString(X);
String yy = jsonPublicKey.getString(Y);
BigInteger x = new BigInteger(1, Base64Util.base64urldecode(xx));
BigInteger y = new BigInteger(1, Base64Util.base64urldecode(yy));
publicKey = new ECDSAPublicKey(signatureAlgorithm, x, y);
} else if (algorithmFamily == AlgorithmFamily.ED) {
String xx = jsonPublicKey.getString(X);
BigInteger x = new BigInteger(1, Base64Util.base64urldecode(xx));
publicKey = new EDDSAPublicKey(signatureAlgorithm, x.toByteArray());
} else {
throw new InvalidParameterException("Wrong value of the AlgorithmFamily: algorithmFamily = " + algorithmFamily);
}
if (jsonKeyValue.has(CERTIFICATE_CHAIN)) {
final String BEGIN = "-----BEGIN CERTIFICATE-----";
final String END = "-----END CERTIFICATE-----";
JSONArray certChain = jsonKeyValue.getJSONArray(CERTIFICATE_CHAIN);
String certificateString = BEGIN + "\n" + certChain.getString(0) + "\n" + END;
StringReader sr = new StringReader(certificateString);
PEMParser pemReader = new PEMParser(sr);
X509Certificate cert = (X509CertificateObject) pemReader.readObject();
io.jans.as.model.crypto.Certificate certificate = new Certificate(signatureAlgorithm, cert);
publicKey.setCertificate(certificate);
}
publicKey.setKeyId(resultKeyId);
publicKey.setSignatureAlgorithm(signatureAlgorithm);
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
}
return publicKey;
}
Aggregations