Blame SOURCES/TestCryptoLevel.java

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