Blame SOURCES/TestCryptoLevel.java

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