Blame SOURCES/TestCryptoLevel.java

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