Blame SOURCES/TestCryptoLevel.java

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