Blame SOURCES/TestCryptoLevel.java

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