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