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