Blame SOURCES/TestCryptoLevel.java

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