Blame SOURCES/TestCryptoLevel.java

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