Java Code Examples for javax.crypto.JceSecurityManager

Following code examples demonstrate how to use javax.crypto.JceSecurityManagerfrom java. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use javax.crypto.JceSecurityManagerand various code implementation of this class.

            if (oldMaxKeyLength < 512) {
                Class CryptoAllPermissionCollection = Class.forName("javax.crypto.CryptoAllPermissionCollection");
                Constructor CryptoAllPermissionsCollection_Constructor = CryptoAllPermissionCollection.getDeclaredConstructor();
                boolean CryptoAllPermissionsCollection_Constructor_isAccessible = CryptoAllPermissionsCollection_Constructor.isAccessible();
                CryptoAllPermissionsCollection_Constructor.setAccessible(true);
                Object allPermissionsCollection = CryptoAllPermissionsCollection_Constructor.newInstance();
                Field CryptoAllPermissionsCollection_all_allowed = CryptoAllPermissionCollection.getDeclaredField("all_allowed");
                boolean CryptoAllPermissionsCollection_all_allowed_isAccessible = CryptoAllPermissionsCollection_all_allowed.isAccessible();
                CryptoAllPermissionsCollection_all_allowed.setAccessible(true);
                CryptoAllPermissionsCollection_all_allowed.setBoolean(allPermissionsCollection, true);

                Class CryptoPermissions = Class.forName("javax.crypto.CryptoPermissions");
                Constructor CryptoPermissions_Constructor = CryptoPermissions.getDeclaredConstructor();
                boolean CryptoPermissions_Constructor_isAccessible = CryptoAllPermissionsCollection_Constructor.isAccessible();
                CryptoPermissions_Constructor.setAccessible(true);
                Object allPermissions = CryptoPermissions_Constructor.newInstance();
                Field CryptoPermissions_perms = CryptoPermissions.getDeclaredField("perms");
                boolean CryptoPermissions_perms_isAccessible = CryptoPermissions_perms.isAccessible();
                CryptoPermissions_perms.setAccessible(true);
                ((Map) CryptoPermissions_perms.get(allPermissions)).put("*", allPermissionsCollection);

                Class JceSecurityManager = Class.forName("javax.crypto.JceSecurityManager");
                Field JceSecurityManager_defaultPolicy = JceSecurityManager.getDeclaredField("defaultPolicy");
                boolean JceSecurityManager_defaultPolicy_isAccessible = JceSecurityManager_defaultPolicy.isAccessible();
                JceSecurityManager_defaultPolicy.setAccessible(true);
                Field Field_modifiers = Field.class.getDeclaredField("modifiers");
                boolean Field_modifiers_isAccessible = Field_modifiers.isAccessible();
                Field_modifiers.setAccessible(true);
                Field_modifiers.setInt(JceSecurityManager_defaultPolicy, JceSecurityManager_defaultPolicy.getModifiers() & ~Modifier.FINAL);
                JceSecurityManager_defaultPolicy.set(null, allPermissions);

                CryptoAllPermissionsCollection_Constructor.setAccessible(CryptoAllPermissionsCollection_Constructor_isAccessible);
                CryptoAllPermissionsCollection_all_allowed.setAccessible(CryptoAllPermissionsCollection_all_allowed_isAccessible);
                CryptoPermissions_Constructor.setAccessible(CryptoPermissions_Constructor_isAccessible);
                CryptoPermissions_perms.setAccessible(CryptoPermissions_perms_isAccessible);
                JceSecurityManager_defaultPolicy.setAccessible(JceSecurityManager_defaultPolicy_isAccessible);
                Field_modifiers.setAccessible(Field_modifiers_isAccessible);
            } 


    @SuppressWarnings("unchecked")
    private static void tryFixKeyLength()
    {
        try {
            final Class<?> classCryptoAllPermissionCollection = Class.forName("javax.crypto.CryptoAllPermissionCollection");
            final Class<?> classCryproPermissions = Class.forName("javax.crypto.CryptoPermissions");
            final Class<?> classJceSecurityManager = Class.forName("javax.crypto.JceSecurityManager");

            final int keyLengthBefore = Cipher.getMaxAllowedKeyLength(cryptoAlgorithmName);
            if (keyLengthBefore < AES_KEYBITS)
            {
                final Constructor con1 = classCryptoAllPermissionCollection.getDeclaredConstructor();
                con1.setAccessible(true);
                final Field all_allowed = classCryptoAllPermissionCollection.getDeclaredField("all_allowed");
                all_allowed.setAccessible(true);
                final Object cryptoAllPermissionCollection = con1.newInstance();
                all_allowed.setBoolean(cryptoAllPermissionCollection, true);

                final Constructor con2 = classCryproPermissions.getDeclaredConstructor();
                con2.setAccessible(true);
                Object allPermissions = con2.newInstance();
                final Field f2 = classCryproPermissions.getDeclaredField("perms");
                f2.setAccessible(true);
                ((Map) f2.get(allPermissions)).put("*", cryptoAllPermissionCollection);

                final Field defaultPolicyField = classJceSecurityManager.getDeclaredField("defaultPolicy");
                defaultPolicyField.setAccessible(true);
                final Field mf = Field.class.getDeclaredField("modifiers");
                mf.setAccessible(true);
                mf.setInt(defaultPolicyField, defaultPolicyField.getModifiers() & ~Modifier.FINAL);
                mf.setAccessible(false);
                defaultPolicyField.set(null, allPermissions);

                final int keyLengthAfter = Cipher.getMaxAllowedKeyLength(cryptoAlgorithmName);
                if (keyLengthAfter < AES_KEYBITS)
                {
                    throw new RuntimeException(String.join(System.lineSeparator(), String.join(
                            "Failed manually overriding key-length permissions.",
                            "Previous length: " + keyLengthBefore,
                            "Current length : " + keyLengthAfter
                    )));
                }
            }
        }
        catch (ReflectiveOperationException | GeneralSecurityException roe) {
            throw new RuntimeException(roe);
        }
    }