Blame SOURCES/TestCryptoLevel.java

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