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