Blame SOURCES/TestCryptoLevel.java

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