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