Search in sources :

Example 1 with EllipticEdvardsCurve

use of io.jans.as.model.crypto.signature.EllipticEdvardsCurve 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;
}
Also used : PublicKey(java.security.PublicKey) ECGenParameterSpec(java.security.spec.ECGenParameterSpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) EllipticEdvardsCurve(io.jans.as.model.crypto.signature.EllipticEdvardsCurve) ECPoint(java.security.spec.ECPoint) SignatureAlgorithm(io.jans.as.model.crypto.signature.SignatureAlgorithm) Algorithm(io.jans.as.model.jwk.Algorithm) AlgorithmFamily(io.jans.as.model.crypto.signature.AlgorithmFamily) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) InvalidParameterException(io.jans.as.model.exception.InvalidParameterException) ECParameterSpec(java.security.spec.ECParameterSpec) BigInteger(java.math.BigInteger) KeyFactory(java.security.KeyFactory) AlgorithmParameters(java.security.AlgorithmParameters)

Aggregations

AlgorithmFamily (io.jans.as.model.crypto.signature.AlgorithmFamily)1 EllipticEdvardsCurve (io.jans.as.model.crypto.signature.EllipticEdvardsCurve)1 SignatureAlgorithm (io.jans.as.model.crypto.signature.SignatureAlgorithm)1 InvalidParameterException (io.jans.as.model.exception.InvalidParameterException)1 Algorithm (io.jans.as.model.jwk.Algorithm)1 BigInteger (java.math.BigInteger)1 AlgorithmParameters (java.security.AlgorithmParameters)1 KeyFactory (java.security.KeyFactory)1 PublicKey (java.security.PublicKey)1 ECGenParameterSpec (java.security.spec.ECGenParameterSpec)1 ECParameterSpec (java.security.spec.ECParameterSpec)1 ECPoint (java.security.spec.ECPoint)1 ECPublicKeySpec (java.security.spec.ECPublicKeySpec)1 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)1 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)1