Blame SOURCES/TestCryptoLevel.java

cadfac
/* TestCryptoLevel -- Ensure unlimited crypto policy is in use.
cadfac
   Copyright (C) 2012 Red Hat, Inc.
cadfac
cadfac
This program is free software: you can redistribute it and/or modify
cadfac
it under the terms of the GNU Affero General Public License as
cadfac
published by the Free Software Foundation, either version 3 of the
cadfac
License, or (at your option) any later version.
cadfac
cadfac
This program is distributed in the hope that it will be useful,
cadfac
but WITHOUT ANY WARRANTY; without even the implied warranty of
cadfac
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
cadfac
GNU Affero General Public License for more details.
cadfac
cadfac
You should have received a copy of the GNU Affero General Public License
cadfac
along with this program.  If not, see <http://www.gnu.org/licenses/>.
cadfac
*/
cadfac
cadfac
import java.lang.reflect.Field;
cadfac
import java.lang.reflect.Method;
cadfac
import java.lang.reflect.InvocationTargetException;
cadfac
cadfac
import java.security.Permission;
cadfac
import java.security.PermissionCollection;
cadfac
cadfac
public class TestCryptoLevel
cadfac
{
cadfac
  public static void main(String[] args)
cadfac
    throws NoSuchFieldException, ClassNotFoundException,
cadfac
           IllegalAccessException, InvocationTargetException
cadfac
  {
cadfac
    Class cls = null;
cadfac
    Method def = null, exempt = null;
cadfac
cadfac
    try
cadfac
      {
cadfac
        cls = Class.forName("javax.crypto.JceSecurity");
cadfac
      }
cadfac
    catch (ClassNotFoundException ex)
cadfac
      {
cadfac
        System.err.println("Running a non-Sun JDK.");
cadfac
        System.exit(0);
cadfac
      }
cadfac
    try
cadfac
      {
cadfac
        def = cls.getDeclaredMethod("getDefaultPolicy");
cadfac
        exempt = cls.getDeclaredMethod("getExemptPolicy");
cadfac
      }
cadfac
    catch (NoSuchMethodException ex)
cadfac
      {
cadfac
        System.err.println("Running IcedTea with the original crypto patch.");
cadfac
        System.exit(0);
cadfac
      }
cadfac
    def.setAccessible(true);
cadfac
    exempt.setAccessible(true);
cadfac
    PermissionCollection defPerms = (PermissionCollection) def.invoke(null);
cadfac
    PermissionCollection exemptPerms = (PermissionCollection) exempt.invoke(null);
cadfac
    Class apCls = Class.forName("javax.crypto.CryptoAllPermission");
cadfac
    Field apField = apCls.getDeclaredField("INSTANCE");
cadfac
    apField.setAccessible(true);
cadfac
    Permission allPerms = (Permission) apField.get(null);
cadfac
    if (defPerms.implies(allPerms) && (exemptPerms == null || exemptPerms.implies(allPerms)))
cadfac
      {
cadfac
        System.err.println("Running with the unlimited policy.");
cadfac
        System.exit(0);
cadfac
      }
cadfac
    else
cadfac
      {
cadfac
        System.err.println("WARNING: Running with a restricted crypto policy.");
cadfac
        System.exit(-1);
cadfac
      }
cadfac
  }
cadfac
}