Blame SOURCES/TestCryptoLevel.java

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