Blame SOURCES/TestCryptoLevel.java

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