// 
// Decompiled by Procyon v0.6.0
// 

package com.nimbusds.jose.jca;

import java.security.GeneralSecurityException;
import javax.crypto.NoSuchPaddingException;
import com.nimbusds.jose.crypto.impl.ECDSA;
import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.crypto.impl.RSASSA;
import javax.crypto.Mac;
import java.security.Security;
import java.security.Provider;
import com.nimbusds.jose.EncryptionMethod;
import com.nimbusds.jose.JWEAlgorithm;
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.Algorithm;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;

public final class JCASupport
{
    public static boolean isUnlimitedStrength() {
        try {
            return Cipher.getMaxAllowedKeyLength("AES") >= 256;
        }
        catch (final NoSuchAlgorithmException e) {
            return false;
        }
    }
    
    public static boolean isSupported(final Algorithm alg) {
        if (alg instanceof JWSAlgorithm) {
            return isSupported((JWSAlgorithm)alg);
        }
        if (alg instanceof JWEAlgorithm) {
            return isSupported((JWEAlgorithm)alg);
        }
        if (alg instanceof EncryptionMethod) {
            return isSupported((EncryptionMethod)alg);
        }
        throw new IllegalArgumentException("Unexpected algorithm class: " + alg.getClass().getCanonicalName());
    }
    
    public static boolean isSupported(final Algorithm alg, final Provider provider) {
        if (alg instanceof JWSAlgorithm) {
            return isSupported((JWSAlgorithm)alg, provider);
        }
        if (alg instanceof JWEAlgorithm) {
            return isSupported((JWEAlgorithm)alg, provider);
        }
        if (alg instanceof EncryptionMethod) {
            return isSupported((EncryptionMethod)alg, provider);
        }
        throw new IllegalArgumentException("Unexpected algorithm class: " + alg.getClass().getCanonicalName());
    }
    
    public static boolean isSupported(final JWSAlgorithm alg) {
        if (alg.getName().equals(Algorithm.NONE.getName())) {
            return true;
        }
        for (final Provider p : Security.getProviders()) {
            if (isSupported(alg, p)) {
                return true;
            }
        }
        return false;
    }
    
    public static boolean isSupported(final JWSAlgorithm alg, final Provider provider) {
        if (JWSAlgorithm.Family.HMAC_SHA.contains(alg)) {
            String jcaName;
            if (alg.equals(JWSAlgorithm.HS256)) {
                jcaName = "HMACSHA256";
            }
            else if (alg.equals(JWSAlgorithm.HS384)) {
                jcaName = "HMACSHA384";
            }
            else {
                if (!alg.equals(JWSAlgorithm.HS512)) {
                    return false;
                }
                jcaName = "HMACSHA512";
            }
            try {
                Mac.getInstance(jcaName, provider);
            }
            catch (final NoSuchAlgorithmException e) {
                return false;
            }
            return true;
        }
        if (JWSAlgorithm.Family.RSA.contains(alg)) {
            try {
                RSASSA.getSignerAndVerifier(alg, provider);
            }
            catch (final JOSEException e2) {
                return false;
            }
            return true;
        }
        if (JWSAlgorithm.Family.EC.contains(alg)) {
            try {
                ECDSA.getSignerAndVerifier(alg, provider);
            }
            catch (final JOSEException e2) {
                return false;
            }
            return true;
        }
        return false;
    }
    
    public static boolean isSupported(final JWEAlgorithm alg) {
        for (final Provider p : Security.getProviders()) {
            if (isSupported(alg, p)) {
                return true;
            }
        }
        return false;
    }
    
    public static boolean isSupported(final JWEAlgorithm alg, final Provider provider) {
        if (JWEAlgorithm.Family.RSA.contains(alg)) {
            String jcaName;
            if (alg.equals(JWEAlgorithm.RSA1_5)) {
                jcaName = "RSA/ECB/PKCS1Padding";
            }
            else if (alg.equals(JWEAlgorithm.RSA_OAEP)) {
                jcaName = "RSA/ECB/OAEPWithSHA-1AndMGF1Padding";
            }
            else if (alg.equals(JWEAlgorithm.RSA_OAEP_256)) {
                jcaName = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding";
            }
            else {
                if (!alg.equals(JWEAlgorithm.RSA_OAEP_512)) {
                    return false;
                }
                jcaName = "RSA/ECB/OAEPWithSHA-512AndMGF1Padding";
            }
            try {
                Cipher.getInstance(jcaName, provider);
            }
            catch (final NoSuchAlgorithmException | NoSuchPaddingException e) {
                return false;
            }
            return true;
        }
        if (JWEAlgorithm.Family.AES_KW.contains(alg)) {
            return provider.getService("Cipher", "AESWrap") != null;
        }
        if (JWEAlgorithm.Family.ECDH_ES.contains(alg)) {
            return provider.getService("KeyAgreement", "ECDH") != null;
        }
        if (JWEAlgorithm.Family.AES_GCM_KW.contains(alg)) {
            try {
                Cipher.getInstance("AES/GCM/NoPadding", provider);
            }
            catch (final NoSuchAlgorithmException | NoSuchPaddingException e) {
                return false;
            }
            return true;
        }
        if (JWEAlgorithm.Family.PBES2.contains(alg)) {
            String hmac;
            if (alg.equals(JWEAlgorithm.PBES2_HS256_A128KW)) {
                hmac = "HmacSHA256";
            }
            else if (alg.equals(JWEAlgorithm.PBES2_HS384_A192KW)) {
                hmac = "HmacSHA384";
            }
            else {
                hmac = "HmacSHA512";
            }
            return provider.getService("KeyGenerator", hmac) != null;
        }
        return JWEAlgorithm.DIR.equals(alg);
    }
    
    public static boolean isSupported(final EncryptionMethod enc) {
        for (final Provider p : Security.getProviders()) {
            if (isSupported(enc, p)) {
                return true;
            }
        }
        return false;
    }
    
    public static boolean isSupported(final EncryptionMethod enc, final Provider provider) {
        if (EncryptionMethod.Family.AES_CBC_HMAC_SHA.contains(enc)) {
            try {
                Cipher.getInstance("AES/CBC/PKCS5Padding", provider);
            }
            catch (final NoSuchAlgorithmException | NoSuchPaddingException e) {
                return false;
            }
            String hmac;
            if (enc.equals(EncryptionMethod.A128CBC_HS256)) {
                hmac = "HmacSHA256";
            }
            else if (enc.equals(EncryptionMethod.A192CBC_HS384)) {
                hmac = "HmacSHA384";
            }
            else {
                hmac = "HmacSHA512";
            }
            return provider.getService("KeyGenerator", hmac) != null;
        }
        if (EncryptionMethod.Family.AES_GCM.contains(enc)) {
            try {
                Cipher.getInstance("AES/GCM/NoPadding", provider);
            }
            catch (final NoSuchAlgorithmException | NoSuchPaddingException e) {
                return false;
            }
            return true;
        }
        return false;
    }
    
    private JCASupport() {
    }
}
