All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mule.modules.ftpclient.sftp.UnrestrictedCryptographyEnabler Maven / Gradle / Ivy

There is a newer version: 1.0.8
Show newest version
package org.mule.modules.ftpclient.sftp;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.security.Permission;
import java.security.PermissionCollection;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * If possible, it removes the restriction on key length which are enforced in
 * Oracle JDK.
 */
public class UnrestrictedCryptographyEnabler {

    private static final Logger LOGGER = LoggerFactory.getLogger(UnrestrictedCryptographyEnabler.class);

    private static boolean unlocked;

    private UnrestrictedCryptographyEnabler() {
        // no instances allowed
    }

    public static synchronized void enable() {
        if (unlocked) {
            return;
        }
        if (!isRestrictedCryptography()) {
            return;
        }
        try {
            final Class jceSecurity = Class.forName("javax.crypto.JceSecurity");
            final Class cryptoPermissions = Class.forName("javax.crypto.CryptoPermissions");
            final Class cryptoAllPermission = Class.forName("javax.crypto.CryptoAllPermission");

            Field isRestrictedField = jceSecurity.getDeclaredField("isRestricted");
            isRestrictedField.setAccessible(true);
            setFinalStatic(isRestrictedField, true);
            isRestrictedField.set(null, false);

            final Field defaultPolicyField = jceSecurity.getDeclaredField("defaultPolicy");
            defaultPolicyField.setAccessible(true);
            final PermissionCollection defaultPolicy = (PermissionCollection) defaultPolicyField.get(null);

            final Field perms = cryptoPermissions.getDeclaredField("perms");
            perms.setAccessible(true);
            ((Map) perms.get(defaultPolicy)).clear();

            final Field instance = cryptoAllPermission.getDeclaredField("INSTANCE");
            instance.setAccessible(true);
            defaultPolicy.add((Permission) instance.get(null));
        } catch (NoSuchFieldException | IllegalAccessException | ClassNotFoundException e) {
            LOGGER.info("Ignore, may we are still happy with restricted cryptography...", e);
        }
        unlocked = true;
    }

    private static void setFinalStatic(Field field, Object newValue)
            throws NoSuchFieldException, IllegalAccessException {
        field.setAccessible(true);

        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

        field.set(null, newValue);
    }

    private static boolean isRestrictedCryptography() {
        // This simply matches the Oracle JRE, but not OpenJDK.
        return "Java(TM) SE Runtime Environment".equals(System.getProperty("java.runtime.name"));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy