Blame SOURCES/TestCryptoLevel.java

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